diff --git a/src/main/cpp/gl/buffer.cpp b/src/main/cpp/gl/buffer.cpp index 7a06f82..5069f93 100644 --- a/src/main/cpp/gl/buffer.cpp +++ b/src/main/cpp/gl/buffer.cpp @@ -19,6 +19,14 @@ static GLenum get_binding_query(GLenum target) { } } +void glBufferData(GLenum target, GLsizeiptr size, const void *data, GLenum usage) { + LOG() + LOG_D("glBufferData, target = %s, size = %d, data = 0x%x, usage = %s", + glEnumToString(target), size, data, glEnumToString(usage)) + LOAD_GLES_FUNC(glBufferData) + gles_glBufferData(target, size, data, usage); +} + void* glMapBuffer(GLenum target, GLenum access) { LOG() LOG_D("glMapBuffer, target = %s, access = %s", glEnumToString(target), glEnumToString(access)) @@ -30,7 +38,7 @@ void* glMapBuffer(GLenum target, GLenum access) { if (current_buffer == 0) { return NULL; } - if (g_active_mapping.mapped_ptr != NULL) { + if (g_active_mappings[current_buffer].mapped_ptr != NULL) { return NULL; } GLint buffer_size; @@ -52,12 +60,23 @@ void* glMapBuffer(GLenum target, GLenum access) { mapping.target = target; mapping.buffer_id = (GLuint)current_buffer; mapping.mapped_ptr = ptr; +//#if GLOBAL_DEBUG || DEBUG +// if (target == GL_PIXEL_UNPACK_BUFFER) +// mapping.client_buf.resize(buffer_size, 0xFF); +//#endif mapping.size = buffer_size; mapping.flags = flags; mapping.is_dirty = (flags & GL_MAP_WRITE_BIT) ? GL_TRUE : GL_FALSE; g_active_mappings[current_buffer] = mapping; CHECK_GL_ERROR +//#if GLOBAL_DEBUG || DEBUG +// if (target == GL_PIXEL_UNPACK_BUFFER) +// return mapping.client_buf.data(); +// else +// return ptr; +//#else return ptr; +//#endif } GLboolean force_unmap() { @@ -82,6 +101,11 @@ GLboolean force_unmap() { return GL_TRUE; } +#if GLOBAL_DEBUG || DEBUG +#include +#define BIN_FILE_PREFIX "/sdcard/MG/buf/" +#endif + GLboolean glUnmapBuffer(GLenum target) { LOG() @@ -92,6 +116,20 @@ GLboolean glUnmapBuffer(GLenum target) { if (buffer == 0) return GL_FALSE; +#if GLOBAL_DEBUG || DEBUG + // Blit data from client side to OpenGL here +// if (target == GL_PIXEL_UNPACK_BUFFER) { +// auto &mapping = g_active_mappings[buffer]; +// +// std::fstream fs(std::string(BIN_FILE_PREFIX) + "buf" + std::to_string(buffer) + ".bin", std::ios::out | std::ios::binary | std::ios::trunc); +// fs.write((const char*)mapping.client_buf.data(), mapping.size); +// fs.close(); +// +//// memset(mapping.mapped_ptr, 0xFF, mapping.size); +// memcpy(mapping.mapped_ptr, mapping.client_buf.data(), mapping.size); +// } +#endif + LOAD_GLES(glUnmapBuffer, GLboolean, GLenum target); GLboolean result = gles_glUnmapBuffer(target); diff --git a/src/main/cpp/gl/buffer.h b/src/main/cpp/gl/buffer.h index c7aa321..80f9ec6 100644 --- a/src/main/cpp/gl/buffer.h +++ b/src/main/cpp/gl/buffer.h @@ -11,20 +11,23 @@ #include "../gles/loader.h" #include "mg.h" -#ifdef __cplusplus -extern "C" { -#endif +#include typedef struct { GLenum target; GLuint buffer_id; void *mapped_ptr; +#if GLOBAL_DEBUG || DEBUG + std::vector client_buf; +#endif GLsizeiptr size; GLbitfield flags; GLboolean is_dirty; } BufferMapping; -static BufferMapping g_active_mapping = {0}; +#ifdef __cplusplus +extern "C" { +#endif static GLenum get_binding_query(GLenum target); @@ -34,6 +37,8 @@ GLAPI GLAPIENTRY GLboolean glUnmapBuffer(GLenum target); GLAPI GLAPIENTRY void *glMapBuffer(GLenum target, GLenum access); +GLAPI GLAPIENTRY void glBufferData(GLenum target, GLsizeiptr size, const void *data, GLenum usage); + GLAPI GLAPIENTRY void glBufferStorage(GLenum target, GLsizeiptr size, const void* data, GLbitfield flags); GLAPI GLAPIENTRY void glBindBuffer(GLenum target, GLuint buffer); diff --git a/src/main/cpp/gl/framebuffer.cpp b/src/main/cpp/gl/framebuffer.cpp index 3772ad5..5e71f7b 100644 --- a/src/main/cpp/gl/framebuffer.cpp +++ b/src/main/cpp/gl/framebuffer.cpp @@ -171,6 +171,14 @@ void glDrawBuffers(GLsizei n, const GLenum *bufs) { CHECK_GL_ERROR } +void glReadBuffer(GLenum src) { + LOG() + LOG_D("glReadBuffer, src = %s", glEnumToString(src)) + + LOAD_GLES_FUNC(glReadBuffer) + gles_glReadBuffer(src); +} + GLenum glCheckFramebufferStatus(GLenum target) { LOG() LOAD_GLES_FUNC(glCheckFramebufferStatus) diff --git a/src/main/cpp/gl/framebuffer.h b/src/main/cpp/gl/framebuffer.h index a6d00b1..8135388 100644 --- a/src/main/cpp/gl/framebuffer.h +++ b/src/main/cpp/gl/framebuffer.h @@ -33,6 +33,8 @@ GLAPI GLAPIENTRY void glDrawBuffer(GLenum buf); GLAPI GLAPIENTRY void glDrawBuffers(GLsizei n, const GLenum *bufs); +GLAPI GLAPIENTRY void glReadBuffer(GLenum src); + GLAPI GLAPIENTRY GLenum glCheckFramebufferStatus(GLenum target); #ifdef __cplusplus diff --git a/src/main/cpp/gl/getter.cpp b/src/main/cpp/gl/getter.cpp index 6748a99..88ed628 100644 --- a/src/main/cpp/gl/getter.cpp +++ b/src/main/cpp/gl/getter.cpp @@ -11,7 +11,7 @@ void glGetIntegerv(GLenum pname, GLint *params) { LOG(); - LOG_D("glGetIntegerv, pname: 0x%x",pname); + LOG_D("glGetIntegerv, pname: %s", glEnumToString(pname)); if (pname == GL_CONTEXT_PROFILE_MASK) { (*params) = GL_CONTEXT_CORE_PROFILE_BIT; return; diff --git a/src/main/cpp/gl/gl_native.cpp b/src/main/cpp/gl/gl_native.cpp index 79ea303..f5858a6 100644 --- a/src/main/cpp/gl/gl_native.cpp +++ b/src/main/cpp/gl/gl_native.cpp @@ -24,7 +24,7 @@ NATIVE_FUNCTION_HEAD(void, glBlendEquation, GLenum mode) NATIVE_FUNCTION_END_NO_ NATIVE_FUNCTION_HEAD(void, glBlendEquationSeparate, GLenum modeRGB, GLenum modeAlpha) NATIVE_FUNCTION_END_NO_RETURN(void, glBlendEquationSeparate, modeRGB,modeAlpha) NATIVE_FUNCTION_HEAD(void, glBlendFunc, GLenum sfactor, GLenum dfactor) NATIVE_FUNCTION_END_NO_RETURN(void, glBlendFunc, sfactor,dfactor) NATIVE_FUNCTION_HEAD(void, glBlendFuncSeparate, GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha) NATIVE_FUNCTION_END_NO_RETURN(void, glBlendFuncSeparate, sfactorRGB,dfactorRGB,sfactorAlpha,dfactorAlpha) -NATIVE_FUNCTION_HEAD(void, glBufferData, GLenum target, GLsizeiptr size, const void *data, GLenum usage) NATIVE_FUNCTION_END_NO_RETURN(void, glBufferData, target,size,data,usage) +//NATIVE_FUNCTION_HEAD(void, glBufferData, GLenum target, GLsizeiptr size, const void *data, GLenum usage) NATIVE_FUNCTION_END_NO_RETURN(void, glBufferData, target,size,data,usage) NATIVE_FUNCTION_HEAD(void, glBufferSubData, GLenum target, GLintptr offset, GLsizeiptr size, const void *data) NATIVE_FUNCTION_END_NO_RETURN(void, glBufferSubData, target,offset,size,data) //NATIVE_FUNCTION_HEAD(GLenum, glCheckFramebufferStatus, GLenum target) NATIVE_FUNCTION_END(GLenum, glCheckFramebufferStatus, target) NATIVE_FUNCTION_HEAD(void, glClear, GLbitfield mask) NATIVE_FUNCTION_END_NO_RETURN(void, glClear, mask) @@ -153,7 +153,7 @@ NATIVE_FUNCTION_HEAD(void, glVertexAttrib4f, GLuint index, GLfloat x, GLfloat y, NATIVE_FUNCTION_HEAD(void, glVertexAttrib4fv, GLuint index, const GLfloat *v) NATIVE_FUNCTION_END_NO_RETURN(void, glVertexAttrib4fv, index,v) NATIVE_FUNCTION_HEAD(void, glVertexAttribPointer, GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer) NATIVE_FUNCTION_END_NO_RETURN(void, glVertexAttribPointer, index,size,type,normalized,stride,pointer) NATIVE_FUNCTION_HEAD(void, glViewport, GLint x, GLint y, GLsizei width, GLsizei height) NATIVE_FUNCTION_END_NO_RETURN(void, glViewport, x,y,width,height) -NATIVE_FUNCTION_HEAD(void, glReadBuffer, GLenum src) NATIVE_FUNCTION_END_NO_RETURN(void, glReadBuffer, src) +//NATIVE_FUNCTION_HEAD(void, glReadBuffer, GLenum src) NATIVE_FUNCTION_END_NO_RETURN(void, glReadBuffer, src) NATIVE_FUNCTION_HEAD(void, glDrawRangeElements, GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const void *indices) NATIVE_FUNCTION_END_NO_RETURN(void, glDrawRangeElements, mode,start,end,count,type,indices) //NATIVE_FUNCTION_HEAD(void, glTexImage3D, GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void *pixels) NATIVE_FUNCTION_END_NO_RETURN(void, glTexImage3D, target,level,internalformat,width,height,depth,border,format,type,pixels) NATIVE_FUNCTION_HEAD(void, glTexSubImage3D, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void *pixels) NATIVE_FUNCTION_END_NO_RETURN(void, glTexSubImage3D, target,level,xoffset,yoffset,zoffset,width,height,depth,format,type,pixels) diff --git a/src/main/cpp/gl/log.cpp b/src/main/cpp/gl/log.cpp index e17f205..09dd602 100644 --- a/src/main/cpp/gl/log.cpp +++ b/src/main/cpp/gl/log.cpp @@ -26,6 +26,18 @@ const char* glEnumToString(GLenum e) { CASE(GL_4_BYTES) CASE(GL_DOUBLE) + CASE(GL_UNSIGNED_BYTE_3_3_2) + CASE(GL_UNSIGNED_BYTE_2_3_3_REV) + CASE(GL_UNSIGNED_SHORT_5_6_5) + CASE(GL_UNSIGNED_SHORT_5_6_5_REV) + CASE(GL_UNSIGNED_SHORT_4_4_4_4) + CASE(GL_UNSIGNED_SHORT_4_4_4_4_REV) + CASE(GL_UNSIGNED_SHORT_5_5_5_1) + CASE(GL_UNSIGNED_SHORT_1_5_5_5_REV) + CASE(GL_UNSIGNED_INT_8_8_8_8) + CASE(GL_UNSIGNED_INT_8_8_8_8_REV) + CASE(GL_UNSIGNED_INT_10_10_10_2) + /* Primitives */ CASE(GL_LINE_LOOP) CASE(GL_LINE_STRIP) @@ -949,6 +961,21 @@ const char* glEnumToString(GLenum e) { CASE(GL_NUM_SAMPLE_COUNTS) CASE(GL_TEXTURE_IMMUTABLE_LEVELS) + CASE(GL_TEXTURE_RECTANGLE) + CASE(GL_TEXTURE_CUBE_MAP_POSITIVE_X) + CASE(GL_TEXTURE_CUBE_MAP_NEGATIVE_X) + CASE(GL_TEXTURE_CUBE_MAP_POSITIVE_Y) + CASE(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y) + CASE(GL_TEXTURE_CUBE_MAP_POSITIVE_Z) + CASE(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) + CASE(GL_TEXTURE_CUBE_MAP_ARRAY) + + CASE(GL_BGR) + CASE(GL_BGRA) + CASE(GL_GREEN_INTEGER) + CASE(GL_BLUE_INTEGER) + CASE(GL_BGR_INTEGER) + CASE(GL_BGRA_INTEGER) /* * Miscellaneous */ diff --git a/src/main/cpp/gl/texture.cpp b/src/main/cpp/gl/texture.cpp index 6c7b8af..c286643 100644 --- a/src/main/cpp/gl/texture.cpp +++ b/src/main/cpp/gl/texture.cpp @@ -725,6 +725,7 @@ void glDeleteTextures(GLsizei n, const GLuint *textures) { } void glGenerateTextureMipmap(GLuint texture) { + LOG() GLint currentTexture; auto& tex = g_textures[bound_texture]; GLenum target = tex.target; @@ -737,55 +738,71 @@ void glGenerateTextureMipmap(GLuint texture) { } void glGetTexImage(GLenum target, GLint level, GLenum format, GLenum type, void* pixels) { - GLint prevFBO; - glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prevFBO); - GLenum bindingTarget = get_binding_for_target(target); - if (bindingTarget == 0) return; - GLint oldTexBinding; - glActiveTexture(GL_TEXTURE0); - glGetIntegerv(bindingTarget, &oldTexBinding); - GLuint texture = static_cast(oldTexBinding); - if (texture == 0) return; - GLint width, height; - glBindTexture(target, texture); - glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width); - glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height); - glBindTexture(target, oldTexBinding); - if (width <= 0 || height <= 0) return; - GLuint fbo; - glGenFramebuffers(1, &fbo); - glBindFramebuffer(GL_FRAMEBUFFER, fbo); - if (target == GL_TEXTURE_2D || (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)) { - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, texture, level); - } else { - glDeleteFramebuffers(1, &fbo); - glBindFramebuffer(GL_FRAMEBUFFER, prevFBO); - return; - } - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { - glDeleteFramebuffers(1, &fbo); - glBindFramebuffer(GL_FRAMEBUFFER, prevFBO); - return; - } - GLint oldViewport[4]; - glGetIntegerv(GL_VIEWPORT, oldViewport); - glViewport(0, 0, width, height); - GLint oldPackAlignment; - glGetIntegerv(GL_PACK_ALIGNMENT, &oldPackAlignment); - glPixelStorei(GL_PACK_ALIGNMENT, 1); - glReadBuffer(GL_COLOR_ATTACHMENT0); - glReadPixels(0, 0, width, height, format, type, pixels); - glPixelStorei(GL_PACK_ALIGNMENT, oldPackAlignment); - glViewport(oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]); - glDeleteFramebuffers(1, &fbo); - glBindFramebuffer(GL_FRAMEBUFFER, prevFBO); + LOG() + LOG_D("glGetTexImage, target = %s, level = %d, format = %s, type = %s, pixel = 0x%x", + glEnumToString(target), level, glEnumToString(format), glEnumToString(type), pixels) + + return; + +// GLint prevFBO; +// glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prevFBO); +// GLenum bindingTarget = get_binding_for_target(target); +// if (bindingTarget == 0) return; +// GLint oldTexBinding; +// glActiveTexture(GL_TEXTURE0); +// glGetIntegerv(bindingTarget, &oldTexBinding); +// GLuint texture = static_cast(oldTexBinding); +// if (texture == 0) return; +// GLint width, height; +// glBindTexture(target, texture); +// glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width); +// glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height); +// glBindTexture(target, oldTexBinding); +// if (width <= 0 || height <= 0) return; +// GLuint fbo; +// glGenFramebuffers(1, &fbo); +// glBindFramebuffer(GL_FRAMEBUFFER, fbo); +// if (target == GL_TEXTURE_2D || (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)) { +// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, texture, level); +// } else { +// glDeleteFramebuffers(1, &fbo); +// glBindFramebuffer(GL_FRAMEBUFFER, prevFBO); +// return; +// } +// if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { +// glDeleteFramebuffers(1, &fbo); +// glBindFramebuffer(GL_FRAMEBUFFER, prevFBO); +// return; +// } +// GLint oldViewport[4]; +// glGetIntegerv(GL_VIEWPORT, oldViewport); +// glViewport(0, 0, width, height); +// GLint oldPackAlignment; +// glGetIntegerv(GL_PACK_ALIGNMENT, &oldPackAlignment); +// glPixelStorei(GL_PACK_ALIGNMENT, 1); +// glReadBuffer(GL_COLOR_ATTACHMENT0); +// glReadPixels(0, 0, width, height, format, type, pixels); +// glPixelStorei(GL_PACK_ALIGNMENT, oldPackAlignment); +// glViewport(oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]); +// glDeleteFramebuffers(1, &fbo); +// glBindFramebuffer(GL_FRAMEBUFFER, prevFBO); } +#if GLOBAL_DEBUG || DEBUG +#include +#define PX_FILE_PREFIX "/sdcard/MG/readpixels/" +#endif + void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels) { LOG() LOAD_GLES_FUNC(glReadPixels) LOG_D("glReadPixels, x=%d, y=%d, width=%d, height=%d, format=0x%x, type=0x%x, pixels=0x%x", x, y, width, height, format, type, pixels) + + static int count = 0; + + GLenum prevFormat = format; + if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8) { format = GL_RGBA; type = GL_UNSIGNED_BYTE; @@ -793,6 +810,19 @@ void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format LOG_D("glReadPixels converted, x=%d, y=%d, width=%d, height=%d, format=0x%x, type=0x%x, pixels=0x%x", x, y, width, height, format, type, pixels) gles_glReadPixels(x, y, width, height, format, type, pixels); + +#if GLOBAL_DEBUG || DEBUG + if (prevFormat == GL_BGRA && type == GL_UNSIGNED_BYTE) { + std::vector px(width * height * sizeof(uint8_t) * 4, 0); + LOAD_GLES_FUNC(glBindBuffer) + gles_glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); + gles_glReadPixels(x, y, width, height, format, type, px.data()); + + std::fstream fs(std::string(PX_FILE_PREFIX) + std::to_string(count++) + ".bin", std::ios::out | std::ios::binary | std::ios::trunc); + fs.write((const char*)px.data(), px.size()); + fs.close(); + } +#endif CHECK_GL_ERROR }