diff options
Diffstat (limited to '3rdparty/bgfx/src/renderer_gl.cpp')
-rw-r--r-- | 3rdparty/bgfx/src/renderer_gl.cpp | 150 |
1 files changed, 109 insertions, 41 deletions
diff --git a/3rdparty/bgfx/src/renderer_gl.cpp b/3rdparty/bgfx/src/renderer_gl.cpp index 1ed6333c217..0fea6a7c9d8 100644 --- a/3rdparty/bgfx/src/renderer_gl.cpp +++ b/3rdparty/bgfx/src/renderer_gl.cpp @@ -223,8 +223,8 @@ namespace bgfx { namespace gl { GL_ZERO, GL_ZERO, GL_ZERO, GL_ZERO, false }, // R1 { GL_ALPHA, GL_ZERO, GL_ALPHA, GL_UNSIGNED_BYTE, false }, // A8 { GL_R8, GL_ZERO, GL_RED, GL_UNSIGNED_BYTE, false }, // R8 - { GL_R8I, GL_ZERO, GL_RED, GL_BYTE, false }, // R8S - { GL_R8UI, GL_ZERO, GL_RED, GL_UNSIGNED_BYTE, false }, // R8S + { GL_R8I, GL_ZERO, GL_RED, GL_BYTE, false }, // R8I + { GL_R8UI, GL_ZERO, GL_RED, GL_UNSIGNED_BYTE, false }, // R8U { GL_R8_SNORM, GL_ZERO, GL_RED, GL_BYTE, false }, // R8S { GL_R16, GL_ZERO, GL_RED, GL_UNSIGNED_SHORT, false }, // R16 { GL_R16I, GL_ZERO, GL_RED, GL_SHORT, false }, // R16I @@ -239,8 +239,8 @@ namespace bgfx { namespace gl { GL_RG8UI, GL_ZERO, GL_RG, GL_UNSIGNED_BYTE, false }, // RG8U { GL_RG8_SNORM, GL_ZERO, GL_RG, GL_BYTE, false }, // RG8S { GL_RG16, GL_ZERO, GL_RG, GL_UNSIGNED_SHORT, false }, // RG16 - { GL_RG16I, GL_ZERO, GL_RG, GL_SHORT, false }, // RG16 - { GL_RG16UI, GL_ZERO, GL_RG, GL_UNSIGNED_SHORT, false }, // RG16 + { GL_RG16I, GL_ZERO, GL_RG, GL_SHORT, false }, // RG16I + { GL_RG16UI, GL_ZERO, GL_RG, GL_UNSIGNED_SHORT, false }, // RG16U { GL_RG16F, GL_ZERO, GL_RG, GL_FLOAT, false }, // RG16F { GL_RG16_SNORM, GL_ZERO, GL_RG, GL_SHORT, false }, // RG16S { GL_RG32I, GL_ZERO, GL_RG, GL_INT, false }, // RG32I @@ -1110,10 +1110,10 @@ namespace bgfx { namespace gl tfi.m_type = _type; } - void initTestTexture(TextureFormat::Enum _format, bool srgb = false) + GLenum initTestTexture(TextureFormat::Enum _format, bool _srgb, bool _mipmaps) { const TextureFormatInfo& tfi = s_textureFormat[_format]; - GLenum internalFmt = srgb + GLenum internalFmt = _srgb ? tfi.m_internalFmtSrgb : tfi.m_internalFmt ; @@ -1121,20 +1121,48 @@ namespace bgfx { namespace gl GLsizei size = (16*16*getBitsPerPixel(_format) )/8; void* data = bx::alignPtr(alloca(size+16), 0, 16); + GLenum err = 0; + if (isCompressed(_format) ) { glCompressedTexImage2D(GL_TEXTURE_2D, 0, internalFmt, 16, 16, 0, size, data); + err |= glGetError(); + if (_mipmaps) + { + glCompressedTexImage2D(GL_TEXTURE_2D, 1, internalFmt, 8, 8, 0, size, data); + err |= glGetError(); + glCompressedTexImage2D(GL_TEXTURE_2D, 2, internalFmt, 4, 4, 0, size, data); + err |= glGetError(); + glCompressedTexImage2D(GL_TEXTURE_2D, 3, internalFmt, 2, 2, 0, size, data); + err |= glGetError(); + glCompressedTexImage2D(GL_TEXTURE_2D, 4, internalFmt, 1, 1, 0, size, data); + err |= glGetError(); + } } else { glTexImage2D(GL_TEXTURE_2D, 0, internalFmt, 16, 16, 0, tfi.m_fmt, tfi.m_type, data); + err |= glGetError(); + if (_mipmaps) + { + glTexImage2D(GL_TEXTURE_2D, 1, internalFmt, 8, 8, 0, tfi.m_fmt, tfi.m_type, data); + err |= glGetError(); + glTexImage2D(GL_TEXTURE_2D, 2, internalFmt, 4, 4, 0, tfi.m_fmt, tfi.m_type, data); + err |= glGetError(); + glTexImage2D(GL_TEXTURE_2D, 3, internalFmt, 2, 2, 0, tfi.m_fmt, tfi.m_type, data); + err |= glGetError(); + glTexImage2D(GL_TEXTURE_2D, 4, internalFmt, 1, 1, 0, tfi.m_fmt, tfi.m_type, data); + err |= glGetError(); + } } + + return err; } - static bool isTextureFormatValid(TextureFormat::Enum _format, bool srgb = false) + static bool isTextureFormatValid(TextureFormat::Enum _format, bool _srgb = false, bool _mipAutogen = false) { const TextureFormatInfo& tfi = s_textureFormat[_format]; - GLenum internalFmt = srgb + GLenum internalFmt = _srgb ? tfi.m_internalFmtSrgb : tfi.m_internalFmt ; @@ -1146,11 +1174,17 @@ namespace bgfx { namespace gl GLuint id; GL_CHECK(glGenTextures(1, &id) ); GL_CHECK(glBindTexture(GL_TEXTURE_2D, id) ); - initTestTexture(_format); - GLenum err = glGetError(); + GLenum err = initTestTexture(_format, _srgb, _mipAutogen); BX_WARN(0 == err, "TextureFormat::%s is not supported (%x: %s).", getName(_format), err, glEnumName(err) ); + if (0 == err + && _mipAutogen) + { + glGenerateMipmap(GL_TEXTURE_2D); + err = glGetError(); + } + GL_CHECK(glDeleteTextures(1, &id) ); return 0 == err; @@ -1186,10 +1220,10 @@ namespace bgfx { namespace gl return 0 == err; } - static bool isFramebufferFormatValid(TextureFormat::Enum _format, bool srgb = false) + static bool isFramebufferFormatValid(TextureFormat::Enum _format, bool _srgb = false) { const TextureFormatInfo& tfi = s_textureFormat[_format]; - GLenum internalFmt = srgb + GLenum internalFmt = _srgb ? tfi.m_internalFmtSrgb : tfi.m_internalFmt ; @@ -1207,9 +1241,7 @@ namespace bgfx { namespace gl GL_CHECK(glGenTextures(1, &id) ); GL_CHECK(glBindTexture(GL_TEXTURE_2D, id) ); - initTestTexture(_format); - - GLenum err = glGetError(); + GLenum err = initTestTexture(_format, _srgb, false); GLenum attachment; if (isDepth(_format) ) @@ -1686,7 +1718,7 @@ namespace bgfx { namespace gl for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii) { - uint8_t supported = 0; + uint16_t supported = BGFX_CAPS_FORMAT_TEXTURE_NONE; supported |= s_textureFormat[ii].m_supported ? BGFX_CAPS_FORMAT_TEXTURE_2D | BGFX_CAPS_FORMAT_TEXTURE_3D @@ -1701,6 +1733,11 @@ namespace bgfx { namespace gl : BGFX_CAPS_FORMAT_TEXTURE_NONE ; + supported |= isTextureFormatValid(TextureFormat::Enum(ii), false, true) + ? BGFX_CAPS_FORMAT_TEXTURE_MIP_AUTOGEN + : BGFX_CAPS_FORMAT_TEXTURE_NONE + ; + supported |= computeSupport && isImageFormatValid(TextureFormat::Enum(ii) ) ? BGFX_CAPS_FORMAT_TEXTURE_IMAGE @@ -2269,7 +2306,7 @@ namespace bgfx { namespace gl } } - void resizeTexture(TextureHandle _handle, uint16_t _width, uint16_t _height) BX_OVERRIDE + void resizeTexture(TextureHandle _handle, uint16_t _width, uint16_t _height, uint8_t _numMips) BX_OVERRIDE { TextureGL& texture = m_textures[_handle.idx]; @@ -2285,7 +2322,7 @@ namespace bgfx { namespace gl tc.m_height = _height; tc.m_sides = 0; tc.m_depth = 0; - tc.m_numMips = 1; + tc.m_numMips = _numMips; tc.m_format = TextureFormat::Enum(texture.m_requestedFormat); tc.m_cubeMap = false; tc.m_mem = NULL; @@ -2573,21 +2610,16 @@ namespace bgfx { namespace gl uint32_t setFrameBuffer(FrameBufferHandle _fbh, uint32_t _height, uint16_t _discard = BGFX_CLEAR_NONE, bool _msaa = true) { if (isValid(m_fbh) - && m_fbh.idx != _fbh.idx - && (BGFX_CLEAR_NONE != m_fbDiscard || m_rtMsaa) ) + && m_fbh.idx != _fbh.idx) { FrameBufferGL& frameBuffer = m_frameBuffers[m_fbh.idx]; - if (m_rtMsaa) - { - frameBuffer.resolve(); - } + frameBuffer.resolve(); if (BGFX_CLEAR_NONE != m_fbDiscard) { frameBuffer.discard(m_fbDiscard); + m_fbDiscard = BGFX_CLEAR_NONE; } - - m_fbDiscard = BGFX_CLEAR_NONE; } m_glctx.makeCurrent(NULL); @@ -4315,9 +4347,10 @@ namespace bgfx { namespace gl uint32_t msaaQuality = ( (m_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT); msaaQuality = bx::uint32_satsub(msaaQuality, 1); msaaQuality = bx::uint32_min(s_renderGL->m_maxMsaa, msaaQuality == 0 ? 0 : 1<<msaaQuality); + const bool msaaSample = 0 != (m_flags&BGFX_TEXTURE_MSAA_SAMPLE); - if (0 != msaaQuality - || writeOnly) + if (!msaaSample + && (0 != msaaQuality || writeOnly) ) { GL_CHECK(glGenRenderbuffers(1, &m_rbo) ); BX_CHECK(0 != m_rbo, "Failed to generate renderbuffer id."); @@ -4822,6 +4855,18 @@ namespace bgfx { namespace gl } } + void TextureGL::resolve() const + { + const bool renderTarget = 0 != (m_flags&BGFX_TEXTURE_RT_MASK); + if (renderTarget + && 1 < m_numMips) + { + GL_CHECK(glBindTexture(m_target, m_id) ); + GL_CHECK(glGenerateMipmap(m_target) ); + GL_CHECK(glBindTexture(m_target, 0) ); + } + } + void writeString(bx::WriterI* _writer, const char* _str) { bx::write(_writer, _str, (int32_t)strlen(_str) ); @@ -4920,10 +4965,11 @@ namespace bgfx { namespace gl if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES) && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES < 30) ) { - writeString(&writer - , "#define flat\n" - "#define smooth\n" - "#define noperspective\n" + writeString(&writer, + "#define centroid\n" + "#define flat\n" + "#define noperspective\n" + "#define smooth\n" ); bool usesDerivatives = s_extension[Extension::OES_standard_derivatives].m_supported @@ -5082,9 +5128,10 @@ namespace bgfx { namespace gl && s_extension[Extension::ARB_shader_texture_lod].m_supported && bx::findIdentifierMatch(code, s_ARB_shader_texture_lod) ; + const bool usesGpuShader5 = !!bx::findIdentifierMatch(code, s_ARB_gpu_shader5); const bool usesIUsamplers = !!bx::findIdentifierMatch(code, s_uisamplers); const bool usesTexelFetch = !!bx::findIdentifierMatch(code, s_texelFetch); - const bool usesGpuShader5 = !!bx::findIdentifierMatch(code, s_ARB_gpu_shader5); + const bool usesTextureMS = !!bx::findIdentifierMatch(code, s_ARB_texture_multisample); const bool usesPacking = !!bx::findIdentifierMatch(code, s_ARB_shading_language_packing); uint32_t version = @@ -5116,6 +5163,11 @@ namespace bgfx { namespace gl writeString(&writer, "#extension GL_ARB_shading_language_packing : enable\n"); } + if (usesTextureMS) + { + writeString(&writer, "#extension GL_ARB_texture_multisample : enable\n"); + } + if (130 <= version) { if (m_type == GL_FRAGMENT_SHADER) @@ -5166,13 +5218,14 @@ namespace bgfx { namespace gl } } - writeString(&writer - , "#define lowp\n" - "#define mediump\n" - "#define highp\n" - "#define flat\n" - "#define smooth\n" - "#define noperspective\n" + writeString(&writer, + "#define lowp\n" + "#define mediump\n" + "#define highp\n" + "#define centroid\n" + "#define flat\n" + "#define noperspective\n" + "#define smooth\n" ); bx::write(&writer, code, codeLen); @@ -5539,6 +5592,19 @@ namespace bgfx { namespace gl GL_CHECK(glReadBuffer(GL_NONE) ); GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, s_renderGL->m_msaaBackBufferFbo) ); } + + if (0 < m_numTh) + { + for (uint32_t ii = 0; ii < m_numTh; ++ii) + { + TextureHandle handle = m_attachment[ii].handle; + if (isValid(handle) ) + { + const TextureGL& texture = s_renderGL->m_textures[handle.idx]; + texture.resolve(); + } + } + } } void FrameBufferGL::discard(uint16_t _flags) @@ -6785,12 +6851,14 @@ namespace bgfx { namespace gl if (m_timerQuerySupport) { m_gpuTimer.end(); - while (m_gpuTimer.get() ) + do { elapsedGl = m_gpuTimer.m_elapsed; elapsedGpuMs = double(elapsedGl)/1e6; maxGpuElapsed = elapsedGpuMs > maxGpuElapsed ? elapsedGpuMs : maxGpuElapsed; } + while (m_gpuTimer.get() ); + maxGpuLatency = bx::uint32_imax(maxGpuLatency, m_gpuTimer.m_control.available()-1); } |