From 28904d8c639461211643d3452a3fcdd3c18ab483 Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Tue, 29 May 2012 22:45:04 -0700 Subject: [PATCH] Added ARB_get_program_binary extension. --- src/glimports.h | 106 +++++++++++++++++++++++--------------------- src/renderer_gl.cpp | 78 ++++++++++++++++++++++++++------ src/renderer_gl.h | 48 +++++--------------- 3 files changed, 129 insertions(+), 103 deletions(-) diff --git a/src/glimports.h b/src/glimports.h index 0da076fdf..017038d1b 100644 --- a/src/glimports.h +++ b/src/glimports.h @@ -4,56 +4,60 @@ */ #ifndef GL_IMPORT -# error GL_IMPORT must be defined! +# error GL_IMPORT(_optional, _proto, _func) must be defined! #endif // GL_IMPORT -GL_IMPORT(PFNGLACTIVETEXTUREPROC, glActiveTexture); -GL_IMPORT(PFNGLCOMPRESSEDTEXIMAGE2DPROC, glCompressedTexImage2D); -GL_IMPORT(PFNGLBINDBUFFERPROC, glBindBuffer); -GL_IMPORT(PFNGLDELETEBUFFERSPROC, glDeleteBuffers); -GL_IMPORT(PFNGLGENBUFFERSPROC, glGenBuffers); -GL_IMPORT(PFNGLBUFFERDATAPROC, glBufferData); -GL_IMPORT(PFNGLBUFFERSUBDATAPROC, glBufferSubData); -GL_IMPORT(PFNGLCREATEPROGRAMPROC, glCreateProgram); -GL_IMPORT(PFNGLCREATESHADERPROC, glCreateShader); -GL_IMPORT(PFNGLDELETEPROGRAMPROC, glDeleteProgram); -GL_IMPORT(PFNGLDELETESHADERPROC, glDeleteShader); -GL_IMPORT(PFNGLATTACHSHADERPROC, glAttachShader); -GL_IMPORT(PFNGLCOMPILESHADERPROC, glCompileShader); -GL_IMPORT(PFNGLSHADERSOURCEPROC, glShaderSource); -GL_IMPORT(PFNGLGETSHADERIVPROC, glGetShaderiv); -GL_IMPORT(PFNGLGETSHADERINFOLOGPROC, glGetShaderInfoLog); -GL_IMPORT(PFNGLLINKPROGRAMPROC, glLinkProgram); -GL_IMPORT(PFNGLGETPROGRAMIVPROC, glGetProgramiv); -GL_IMPORT(PFNGLGETPROGRAMINFOLOGPROC, glGetProgramInfoLog); -GL_IMPORT(PFNGLUSEPROGRAMPROC, glUseProgram); -GL_IMPORT(PFNGLGETACTIVEATTRIBPROC, glGetActiveAttrib); -GL_IMPORT(PFNGLGETATTRIBLOCATIONPROC, glGetAttribLocation); -GL_IMPORT(PFNGLGETACTIVEUNIFORMPROC, glGetActiveUniform); -GL_IMPORT(PFNGLGETUNIFORMLOCATIONPROC, glGetUniformLocation); -GL_IMPORT(PFNGLENABLEVERTEXATTRIBARRAYPROC, glEnableVertexAttribArray); -GL_IMPORT(PFNGLDISABLEVERTEXATTRIBARRAYPROC, glDisableVertexAttribArray); -GL_IMPORT(PFNGLVERTEXATTRIBPOINTERPROC, glVertexAttribPointer); -GL_IMPORT(PFNGLVERTEXATTRIB1FPROC, glVertexAttrib1f); -GL_IMPORT(PFNGLVERTEXATTRIB2FPROC, glVertexAttrib2f); -GL_IMPORT(PFNGLVERTEXATTRIB3FPROC, glVertexAttrib3f); -GL_IMPORT(PFNGLVERTEXATTRIB4FPROC, glVertexAttrib4f); -GL_IMPORT(PFNGLBINDFRAMEBUFFERPROC, glBindFramebuffer); -GL_IMPORT(PFNGLGENFRAMEBUFFERSPROC, glGenFramebuffers); -GL_IMPORT(PFNGLDELETEFRAMEBUFFERSPROC, glDeleteFramebuffers); -GL_IMPORT(PFNGLCHECKFRAMEBUFFERSTATUSPROC, glCheckFramebufferStatus); -GL_IMPORT(PFNGLFRAMEBUFFERRENDERBUFFERPROC, glFramebufferRenderbuffer); -GL_IMPORT(PFNGLFRAMEBUFFERTEXTURE2DPROC, glFramebufferTexture2D); -GL_IMPORT(PFNGLBINDRENDERBUFFERPROC, glBindRenderbuffer); -GL_IMPORT(PFNGLGENRENDERBUFFERSPROC, glGenRenderbuffers); -GL_IMPORT(PFNGLDELETERENDERBUFFERSPROC, glDeleteRenderbuffers); -GL_IMPORT(PFNGLRENDERBUFFERSTORAGEPROC, glRenderbufferStorage); -GL_IMPORT(PFNGLUNIFORM1IPROC, glUniform1i); -GL_IMPORT(PFNGLUNIFORM1IVPROC, glUniform1iv); -GL_IMPORT(PFNGLUNIFORM1FPROC, glUniform1f); -GL_IMPORT(PFNGLUNIFORM1FVPROC, glUniform1fv); -GL_IMPORT(PFNGLUNIFORM2FVPROC, glUniform2fv); -GL_IMPORT(PFNGLUNIFORM3FVPROC, glUniform3fv); -GL_IMPORT(PFNGLUNIFORM4FVPROC, glUniform4fv); -GL_IMPORT(PFNGLUNIFORMMATRIX3FVPROC, glUniformMatrix3fv); -GL_IMPORT(PFNGLUNIFORMMATRIX4FVPROC, glUniformMatrix4fv); +GL_IMPORT(false, PFNGLACTIVETEXTUREPROC, glActiveTexture); +GL_IMPORT(false, PFNGLCOMPRESSEDTEXIMAGE2DPROC, glCompressedTexImage2D); +GL_IMPORT(false, PFNGLBINDBUFFERPROC, glBindBuffer); +GL_IMPORT(false, PFNGLDELETEBUFFERSPROC, glDeleteBuffers); +GL_IMPORT(false, PFNGLGENBUFFERSPROC, glGenBuffers); +GL_IMPORT(false, PFNGLBUFFERDATAPROC, glBufferData); +GL_IMPORT(false, PFNGLBUFFERSUBDATAPROC, glBufferSubData); +GL_IMPORT(false, PFNGLCREATEPROGRAMPROC, glCreateProgram); +GL_IMPORT(false, PFNGLCREATESHADERPROC, glCreateShader); +GL_IMPORT(false, PFNGLDELETEPROGRAMPROC, glDeleteProgram); +GL_IMPORT(false, PFNGLDELETESHADERPROC, glDeleteShader); +GL_IMPORT(false, PFNGLATTACHSHADERPROC, glAttachShader); +GL_IMPORT(false, PFNGLCOMPILESHADERPROC, glCompileShader); +GL_IMPORT(false, PFNGLSHADERSOURCEPROC, glShaderSource); +GL_IMPORT(false, PFNGLGETSHADERIVPROC, glGetShaderiv); +GL_IMPORT(false, PFNGLGETSHADERINFOLOGPROC, glGetShaderInfoLog); +GL_IMPORT(false, PFNGLLINKPROGRAMPROC, glLinkProgram); +GL_IMPORT(false, PFNGLGETPROGRAMIVPROC, glGetProgramiv); +GL_IMPORT(false, PFNGLGETPROGRAMINFOLOGPROC, glGetProgramInfoLog); +GL_IMPORT(false, PFNGLUSEPROGRAMPROC, glUseProgram); +GL_IMPORT(false, PFNGLGETACTIVEATTRIBPROC, glGetActiveAttrib); +GL_IMPORT(false, PFNGLGETATTRIBLOCATIONPROC, glGetAttribLocation); +GL_IMPORT(false, PFNGLGETACTIVEUNIFORMPROC, glGetActiveUniform); +GL_IMPORT(false, PFNGLGETUNIFORMLOCATIONPROC, glGetUniformLocation); +GL_IMPORT(false, PFNGLENABLEVERTEXATTRIBARRAYPROC, glEnableVertexAttribArray); +GL_IMPORT(false, PFNGLDISABLEVERTEXATTRIBARRAYPROC, glDisableVertexAttribArray); +GL_IMPORT(false, PFNGLVERTEXATTRIBPOINTERPROC, glVertexAttribPointer); +GL_IMPORT(false, PFNGLVERTEXATTRIB1FPROC, glVertexAttrib1f); +GL_IMPORT(false, PFNGLVERTEXATTRIB2FPROC, glVertexAttrib2f); +GL_IMPORT(false, PFNGLVERTEXATTRIB3FPROC, glVertexAttrib3f); +GL_IMPORT(false, PFNGLVERTEXATTRIB4FPROC, glVertexAttrib4f); +GL_IMPORT(false, PFNGLBINDFRAMEBUFFERPROC, glBindFramebuffer); +GL_IMPORT(false, PFNGLGENFRAMEBUFFERSPROC, glGenFramebuffers); +GL_IMPORT(false, PFNGLDELETEFRAMEBUFFERSPROC, glDeleteFramebuffers); +GL_IMPORT(false, PFNGLCHECKFRAMEBUFFERSTATUSPROC, glCheckFramebufferStatus); +GL_IMPORT(false, PFNGLFRAMEBUFFERRENDERBUFFERPROC, glFramebufferRenderbuffer); +GL_IMPORT(false, PFNGLFRAMEBUFFERTEXTURE2DPROC, glFramebufferTexture2D); +GL_IMPORT(false, PFNGLBINDRENDERBUFFERPROC, glBindRenderbuffer); +GL_IMPORT(false, PFNGLGENRENDERBUFFERSPROC, glGenRenderbuffers); +GL_IMPORT(false, PFNGLDELETERENDERBUFFERSPROC, glDeleteRenderbuffers); +GL_IMPORT(false, PFNGLRENDERBUFFERSTORAGEPROC, glRenderbufferStorage); +GL_IMPORT(false, PFNGLUNIFORM1IPROC, glUniform1i); +GL_IMPORT(false, PFNGLUNIFORM1IVPROC, glUniform1iv); +GL_IMPORT(false, PFNGLUNIFORM1FPROC, glUniform1f); +GL_IMPORT(false, PFNGLUNIFORM1FVPROC, glUniform1fv); +GL_IMPORT(false, PFNGLUNIFORM2FVPROC, glUniform2fv); +GL_IMPORT(false, PFNGLUNIFORM3FVPROC, glUniform3fv); +GL_IMPORT(false, PFNGLUNIFORM4FVPROC, glUniform4fv); +GL_IMPORT(false, PFNGLUNIFORMMATRIX3FVPROC, glUniformMatrix3fv); +GL_IMPORT(false, PFNGLUNIFORMMATRIX4FVPROC, glUniformMatrix4fv); + +GL_IMPORT(true, PFNGLGETPROGRAMBINARYPROC, glGetProgramBinary); +GL_IMPORT(true, PFNGLPROGRAMBINARYPROC, glProgramBinary); +GL_IMPORT(true, PFNGLPROGRAMPARAMETERIPROC, glProgramParameteri); diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index cc87d13a1..c88ac5c09 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -17,7 +17,7 @@ namespace bgfx { #if BX_PLATFORM_WINDOWS -#define GL_IMPORT(_proto, _func) _proto _func +#define GL_IMPORT(_optional, _proto, _func) _proto _func #include "glimports.h" #undef GL_IMPORT #endif // BX_PLATFORM_WINDOWS @@ -136,10 +136,10 @@ namespace bgfx result = wglMakeCurrent(m_hdc, m_context); BX_CHECK(0 != result, "wglMakeCurrent failed!"); -# define GL_IMPORT(_proto, _func) \ +# define GL_IMPORT(_optional, _proto, _func) \ { \ _func = (_proto)wglGetProcAddress(#_func); \ - BGFX_FATAL(NULL != _func, bgfx::Fatal::OPENGL_UnableToCreateContext, "Failed to create OpenGL context. wglGetProcAddress %s", #_func); \ + BGFX_FATAL(!_optional && NULL != _func, bgfx::Fatal::OPENGL_UnableToCreateContext, "Failed to create OpenGL context. wglGetProcAddress %s", #_func); \ } # include "glimports.h" # undef GL_IMPORT @@ -254,7 +254,7 @@ namespace bgfx glXMakeCurrent(display, window, m_context); - glClearColor(0, 0.5, 1, 1); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glXSwapBuffers(display, window); @@ -382,11 +382,12 @@ namespace bgfx { enum Enum { - GL_EXT_texture_format_BGRA8888, - GL_EXT_texture_compression_dxt1, - GL_CHROMIUM_texture_compression_dxt3, - GL_CHROMIUM_texture_compression_dxt5, - GL_OES_standard_derivatives, + EXT_texture_format_BGRA8888, + EXT_texture_compression_dxt1, + CHROMIUM_texture_compression_dxt3, + CHROMIUM_texture_compression_dxt5, + OES_standard_derivatives, + ARB_get_program_binary, Count }; @@ -405,6 +406,7 @@ namespace bgfx { "GL_CHROMIUM_texture_compression_dxt3", false, true }, { "GL_CHROMIUM_texture_compression_dxt5", false, true }, { "GL_OES_standard_derivatives", false, true }, + { "GL_ARB_get_program_binary", false, false }, }; static const GLenum s_primType[] = @@ -596,6 +598,54 @@ namespace bgfx return ConstantType::End; } + void Material::create(const Shader& _vsh, const Shader& _fsh) + { + m_id = glCreateProgram(); + BX_TRACE("material create: %d: %d, %d", m_id, _vsh.m_id, _fsh.m_id); + + GL_CHECK(glAttachShader(m_id, _vsh.m_id) ); + GL_CHECK(glAttachShader(m_id, _fsh.m_id) ); + GL_CHECK(glLinkProgram(m_id) ); + + if (s_extension[Extension::ARB_get_program_binary].m_supported) + { + GL_CHECK(glProgramParameteri(m_id, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE) ); + } + + GLint linked = 0; + GL_CHECK(glGetProgramiv(m_id, GL_LINK_STATUS, &linked) ); + + if (0 == linked) + { + char log[1024]; + GL_CHECK(glGetProgramInfoLog(m_id, sizeof(log), NULL, log) ); + BX_TRACE("%d: %s", linked, log); + + GL_CHECK(glDeleteProgram(m_id) ); + return; + } + + if (s_extension[Extension::ARB_get_program_binary].m_supported) + { + GLint length; + GLenum format; + GL_CHECK(glGetProgramiv(m_id, GL_PROGRAM_BINARY_LENGTH, &length) ); + void* data = g_realloc(NULL, length); + GL_CHECK(glGetProgramBinary(m_id, length, NULL, &format, data) ); + g_free(data); + + dbgPrintfData(data, length, "Binary 0x%08x", format); + } + + init(); + } + + void Material::destroy() + { + GL_CHECK(glUseProgram(0) ); + GL_CHECK(glDeleteProgram(m_id) ); + } + void Material::init() { GLint activeAttribs; @@ -815,7 +865,7 @@ namespace bgfx if (!s_renderCtx.m_dxtSupport || 0 == dds.m_type) { - fmt = s_extension[Extension::GL_EXT_texture_format_BGRA8888].m_supported ? GL_BGRA_EXT : GL_RGBA; + fmt = s_extension[Extension::EXT_texture_format_BGRA8888].m_supported ? GL_BGRA_EXT : GL_RGBA; uint8_t bpp = 4; if (dds.m_type == 0 @@ -1329,9 +1379,9 @@ namespace bgfx } s_renderCtx.m_dxtSupport = true - && s_extension[Extension::GL_EXT_texture_compression_dxt1].m_supported - && s_extension[Extension::GL_CHROMIUM_texture_compression_dxt3].m_supported - && s_extension[Extension::GL_CHROMIUM_texture_compression_dxt5].m_supported + && s_extension[Extension::EXT_texture_compression_dxt1].m_supported + && s_extension[Extension::CHROMIUM_texture_compression_dxt3].m_supported + && s_extension[Extension::CHROMIUM_texture_compression_dxt5].m_supported ; } @@ -1659,7 +1709,7 @@ namespace bgfx if ( (BGFX_STATE_PT_POINTS|BGFX_STATE_POINT_SIZE_MASK) & changedFlags) { - float pointSize = (float)( (newFlags&BGFX_STATE_POINT_SIZE_MASK)>>BGFX_STATE_POINT_SIZE_SHIFT); + float pointSize = (float)(uint32_max(1, (newFlags&BGFX_STATE_POINT_SIZE_MASK)>>BGFX_STATE_POINT_SIZE_SHIFT) ); GL_CHECK(glPointSize(pointSize) ); } #endif // BGFX_CONFIG_RENDERER_OPENGLES diff --git a/src/renderer_gl.h b/src/renderer_gl.h index a6e8ea178..34903ef3f 100644 --- a/src/renderer_gl.h +++ b/src/renderer_gl.h @@ -6,20 +6,21 @@ #ifndef __RENDERER_GL_H__ #define __RENDERER_GL_H__ -#if BX_PLATFORM_NACL +#if BGFX_CONFIG_RENDERER_OPENGL +# include +# include +#elif BGFX_CONFIG_RENDERER_OPENGLES # include +#endif // BGFX_CONFIG_RENDERER_OPENGL + +#if BX_PLATFORM_NACL # include # include # include # include -#elif BX_PLATFORM_ANDROID -# include #elif BX_PLATFORM_WINDOWS # include -# include -# include #elif BX_PLATFORM_LINUX -# include # include # include #endif // BX_PLATFORM_ @@ -65,7 +66,7 @@ namespace bgfx #endif // BGFX_CONFIG_DEBUG #if BX_PLATFORM_WINDOWS -#define GL_IMPORT(_proto, _func) extern _proto _func +#define GL_IMPORT(_optional, _proto, _func) extern _proto _func #include "glimports.h" #undef GL_IMPORT #endif // BX_PLATFORM_WINDOWS @@ -218,37 +219,8 @@ namespace bgfx struct Material { - void create(const Shader& _vsh, const Shader& _fsh) - { - m_id = glCreateProgram(); - BX_TRACE("material create: %d: %d, %d", m_id, _vsh.m_id, _fsh.m_id); - - GL_CHECK(glAttachShader(m_id, _vsh.m_id) ); - GL_CHECK(glAttachShader(m_id, _fsh.m_id) ); - GL_CHECK(glLinkProgram(m_id) ); - - GLint linked = 0; - GL_CHECK(glGetProgramiv(m_id, GL_LINK_STATUS, &linked) ); - - if (0 == linked) - { - char log[1024]; - GL_CHECK(glGetProgramInfoLog(m_id, sizeof(log), NULL, log) ); - BX_TRACE("%d: %s", linked, log); - - GL_CHECK(glDeleteProgram(m_id) ); - return; - } - - init(); - } - - void destroy() - { - GL_CHECK(glUseProgram(0) ); - GL_CHECK(glDeleteProgram(m_id) ); - } - + void create(const Shader& _vsh, const Shader& _fsh); + void destroy(); void init(); void bindAttributes(const VertexDecl& _vertexDecl, uint32_t _baseVertex = 0);