OpenGL Renderer: Greatly simplify the fog rendering pass.

- There is no more need to switch to the working texture as the destination for the fog output. This change will be essential for future commits.
- The dual-source blending method has been obsoleted and removed.
- FBOs are no longer required for the fog feature, easing requirements for ancient GPUs.
- Ancient GPUs may see a small performance benefit due to shader simplification.
This commit is contained in:
rogerman 2024-07-18 12:17:25 -07:00
parent 70fef83ded
commit 0c7cb99d78
5 changed files with 133 additions and 135 deletions

View File

@ -646,20 +646,17 @@ void main() \n\
static const char *FogFragShader_100 = {"\
varying vec2 texCoord;\n\
\n\
uniform sampler2D texInFragColor;\n\
uniform sampler2D texInFragDepth;\n\
uniform sampler2D texInFogAttributes;\n\
uniform sampler1D texFogDensityTable;\n\
uniform bool stateEnableFogAlphaOnly;\n\
uniform vec4 stateFogColor;\n\
\n\
void main()\n\
{\n\
vec4 inFragColor = texture2D(texInFragColor, texCoord);\n\
float inFragDepth = texture2D(texInFragDepth, texCoord).r;\n\
vec4 inFogAttributes = texture2D(texInFogAttributes, texCoord);\n\
bool polyEnableFog = (inFogAttributes.r > 0.999);\n\
vec4 newFoggedColor = inFragColor;\n\
vec4 outFogWeight = vec4(0.0);\n\
\n\
float fogMixWeight = 0.0;\n\
if (FOG_STEP == 0)\n\
@ -673,10 +670,10 @@ void main()\n\
\n\
if (polyEnableFog)\n\
{\n\
newFoggedColor = mix(inFragColor, (stateEnableFogAlphaOnly) ? vec4(inFragColor.rgb, stateFogColor.a) : stateFogColor, fogMixWeight);\n\
outFogWeight = (stateEnableFogAlphaOnly) ? vec4(vec3(0.0), fogMixWeight) : vec4(fogMixWeight);\n\
}\n\
\n\
gl_FragColor = newFoggedColor;\n\
gl_FragColor = outFogWeight;\n\
}\n\
"};
@ -2753,7 +2750,7 @@ Render3DError OpenGLRenderer_1_2::InitExtensions()
this->willFlipAndConvertFramebufferOnGPU = this->isShaderSupported && this->isVBOSupported;
this->willFlipOnlyFramebufferOnGPU = this->willFlipAndConvertFramebufferOnGPU || this->_isFBOBlitSupported;
this->_deviceInfo.isEdgeMarkSupported = this->isShaderSupported && this->isVBOSupported && this->isFBOSupported;
this->_deviceInfo.isFogSupported = this->isShaderSupported && this->isVBOSupported && this->isFBOSupported;
this->_deviceInfo.isFogSupported = this->isShaderSupported && this->isVBOSupported;
this->_deviceInfo.isTextureSmoothingSupported = this->isShaderSupported;
this->_isDepthLEqualPolygonFacingSupported = this->isShaderSupported && this->isVBOSupported && this->isFBOSupported;
@ -3584,7 +3581,6 @@ Render3DError OpenGLRenderer_1_2::CreateFogProgram(const OGLFogProgramKey fogPro
glUniform1i(uniformTexFogDensityTable, OGLTextureUnitID_LookupTable);
OGLRef.uniformStateEnableFogAlphaOnly = glGetUniformLocation(shaderID.program, "stateEnableFogAlphaOnly");
OGLRef.uniformStateFogColor = glGetUniformLocation(shaderID.program, "stateFogColor");
return OGLERROR_NOERR;
}
@ -4698,11 +4694,14 @@ Render3DError OpenGLRenderer_1_2::PostprocessFramebuffer()
return OGLERROR_NOERR;
}
if ( !(this->_enableEdgeMark && this->_deviceInfo.isEdgeMarkSupported) &&
!(this->_enableFog && this->_deviceInfo.isFogSupported) )
{
return OGLERROR_NOERR;
}
OGLRenderRef &OGLRef = *this->ref;
if ( (this->_enableEdgeMark && this->_deviceInfo.isEdgeMarkSupported) ||
(this->_enableFog && this->_deviceInfo.isFogSupported) )
{
// Set up the postprocessing states
glViewport(0, 0, (GLsizei)this->_framebufferWidth, (GLsizei)this->_framebufferHeight);
glDisable(GL_DEPTH_TEST);
@ -4720,11 +4719,6 @@ Render3DError OpenGLRenderer_1_2::PostprocessFramebuffer()
glVertexAttribPointer(OGLVertexAttributeID_Position, 2, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(OGLVertexAttributeID_TexCoord0, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)(sizeof(GLfloat) * 8));
}
}
else
{
return OGLERROR_NOERR;
}
if (this->_enableEdgeMark && this->_deviceInfo.isEdgeMarkSupported)
{
@ -4795,16 +4789,28 @@ Render3DError OpenGLRenderer_1_2::PostprocessFramebuffer()
OGLFogShaderID shaderID = this->_fogProgramMap[this->_fogProgramKey.key];
glDrawBuffer(OGL_WORKING_ATTACHMENT_ID);
if (this->isFBOSupported)
{
glDrawBuffer(OGL_COLOROUT_ATTACHMENT_ID);
}
glUseProgram(shaderID.program);
glUniform1i(OGLRef.uniformStateEnableFogAlphaOnly, this->_pendingRenderStates.enableFogAlphaOnly);
glUniform4fv(OGLRef.uniformStateFogColor, 1, (const GLfloat *)&this->_pendingRenderStates.fogColor);
glBlendFuncSeparate(GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_CONSTANT_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
glBlendColor( this->_pendingRenderStates.fogColor.r,
this->_pendingRenderStates.fogColor.g,
this->_pendingRenderStates.fogColor.b,
this->_pendingRenderStates.fogColor.a );
glDisable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST);
glDisable(GL_BLEND);
glEnable(GL_BLEND);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
this->_lastTextureDrawTarget = OGLTextureUnitID_FinalColor;
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_DST_ALPHA);
glBlendEquationSeparate(GL_FUNC_ADD, GL_MAX);
}
if (this->isVAOSupported)
@ -4826,6 +4832,11 @@ Render3DError OpenGLRenderer_1_2::EndRender()
texCache.Evict();
this->ReadBackPixels();
GLenum oglerror = glGetError();
if (oglerror != GL_NO_ERROR)
{
INFO("OpenGL: error = %i\n", (int)oglerror);
}
ENDGL();

View File

@ -28,7 +28,7 @@
#include "types.h"
// OPENGL PLATFORM-SPECIFIC INCLUDES
#if defined(__ANGLE__) || defined(__ANDROID__)
#if defined(__ANGLE__) || defined(__ANDROID__) || defined(__linux__)
#define OPENGL_VARIANT_ES
#define _NO_SDL_TYPES
#include <GLES3/gl3.h>
@ -331,7 +331,10 @@ EXTERNOGLEXT(PFNGLDELETERENDERBUFFERSEXTPROC, glDeleteRenderbuffersEXT)
// modification. In other words, these are one-to-one drop-in replacements.
typedef GLclampf GLclampd;
#define glClearDepth(depth) glClearDepthf(depth)
#ifndef OPENGL_VARIANT_ES
#define glDrawBuffer(x) glDrawBuffers(1, ((GLenum[]){x}))
#endif
// 1D textures may not exist for a particular OpenGL variant, so they will be promoted to
// 2D textures instead. Implementations need to modify their GLSL shaders accordingly to
@ -627,8 +630,16 @@ struct OGLRenderRef
GLuint fboClearImageID;
GLuint fboRenderID;
GLuint fboRenderColor0ID[8];
GLuint fboRenderWorking0ID[8];
GLuint fboPolyID;
GLuint fboFogAttrID;
GLuint fboFramebufferFlipID;
GLuint fboColorOutMainID;
GLuint fboColorOutWorkingID;
GLuint fboMSIntermediateRenderID;
GLuint fboMSIntermediateRenderColor0ID[8];
GLuint fboMSFogAttrID;
GLuint selectedRenderingFBO;
// Shader states
@ -658,7 +669,6 @@ struct OGLRenderRef
GLint uniformStateEnableFogAlphaOnly;
GLint uniformStateClearPolyID;
GLint uniformStateClearDepth;
GLint uniformStateFogColor;
GLint uniformStateAlphaTestRef[256];
GLint uniformPolyTexScale[256];
@ -1030,4 +1040,47 @@ public:
virtual Render3DError RenderFlush(bool willFlushBuffer32, bool willFlushBuffer16);
};
#ifdef OPENGL_VARIANT_ES
#define glDrawBuffer my_glDrawBuffer
/*
static inline void my_glDrawBuffer(GLenum attach) {
switch(attach) {
case GL_NONE: {
GLenum bufs[1] = {GL_NONE };
glDrawBuffers(1, bufs);
break;
}
case GL_COLOR_ATTACHMENT0: {
GLenum bufs[1] = { attach };
glDrawBuffers(1, bufs);
break;
}
case GL_COLOR_ATTACHMENT1: {
GLenum bufs[2] = {GL_NONE, attach };
glDrawBuffers(2, bufs);
break;
}
case GL_COLOR_ATTACHMENT2: {
GLenum bufs[3] = {GL_NONE, GL_NONE, attach };
glDrawBuffers(3, bufs);
break;
}
case GL_COLOR_ATTACHMENT3: {
GLenum bufs[4] = {GL_NONE, GL_NONE, GL_NONE, attach };
glDrawBuffers(4, bufs);
break;
}
}
}
*/
static inline void my_glDrawBuffer(GLenum attach)
{
GLenum bufs[] = {attach};
glDrawBuffers(1, bufs);
}
#endif
#endif // OGLRENDER_H

View File

@ -559,27 +559,17 @@ uniform sampler2D texInFragDepth;\n\
uniform sampler2D texInFogAttributes;\n\
uniform sampler2D texFogDensityTable;\n\
\n\
#if USE_DUAL_SOURCE_BLENDING\n\
OUT_FOG_COLOR vec4 outFogColor;\n\
OUT_FOG_WEIGHT vec4 outFogWeight;\n\
#else\n\
uniform sampler2D texInFragColor;\n\
OUT_COLOR vec4 outFragColor;\n\
#endif\n\
OUT_COLOR vec4 outFogWeight;\n\
\n\
void main()\n\
{\n\
#if USE_DUAL_SOURCE_BLENDING\n\
outFogColor = state.fogColor;\n\
outFogWeight = vec4(0.0);\n\
#else\n\
outFragColor = texture(texInFragColor, texCoord);\n\
#endif\n\
\n\
float inFragDepth = texture(texInFragDepth, texCoord).r;\n\
vec4 inFogAttributes = texture(texInFogAttributes, texCoord);\n\
bool polyEnableFog = (inFogAttributes.r > 0.999);\n\
outFogWeight = vec4(0.0);\n\
\n\
if (polyEnableFog)\n\
{\n\
float fogMixWeight = 0.0;\n\
if (FOG_STEP == 0)\n\
{\n\
@ -590,14 +580,7 @@ void main()\n\
fogMixWeight = texture( texFogDensityTable, vec2( (inFragDepth * (1024.0/float(FOG_STEP))) + (((-float(FOG_OFFSET)/float(FOG_STEP)) - 0.5) / 32.0), 0.0 ) ).r;\n\
}\n\
\n\
if (polyEnableFog)\n\
{\n\
\n\
#if USE_DUAL_SOURCE_BLENDING\n\
outFogWeight = (state.enableFogAlphaOnly) ? vec4(vec3(0.0), fogMixWeight) : vec4(fogMixWeight);\n\
#else\n\
outFragColor = mix(outFragColor, (state.enableFogAlphaOnly) ? vec4(outFragColor.rgb, state.fogColor.a) : state.fogColor, fogMixWeight);\n\
#endif\n\
}\n\
}\n\
"};
@ -649,7 +632,6 @@ OpenGLRenderer_3_2::OpenGLRenderer_3_2()
_is64kUBOSupported = false;
_isTBOSupported = false;
_isShaderFixedLocationSupported = false;
_isDualSourceBlendingSupported = false;
_isSampleShadingSupported = false;
_isConservativeDepthSupported = false;
_isConservativeDepthAMDSupported = false;
@ -716,9 +698,6 @@ Render3DError OpenGLRenderer_3_2::InitExtensions()
this->willFlipOnlyFramebufferOnGPU = true;
this->willFlipAndConvertFramebufferOnGPU = true;
#ifdef GL_VERSION_3_3
this->_isDualSourceBlendingSupported = this->IsExtensionPresent(&oglExtensionSet, "GL_ARB_blend_func_extended");
#endif
this->_isSampleShadingSupported = this->IsExtensionPresent(&oglExtensionSet, "GL_ARB_sample_shading");
this->_isConservativeDepthSupported = this->IsExtensionPresent(&oglExtensionSet, "GL_ARB_conservative_depth") && IsOpenGLDriverVersionSupported(4, 0, 0);
this->_isConservativeDepthAMDSupported = this->IsExtensionPresent(&oglExtensionSet, "GL_AMD_conservative_depth") && IsOpenGLDriverVersionSupported(4, 0, 0);
@ -1812,8 +1791,6 @@ Render3DError OpenGLRenderer_3_2::CreateFogProgram(const OGLFogProgramKey fogPro
}
std::stringstream fsHeader;
fsHeader << "#define USE_DUAL_SOURCE_BLENDING " << ((this->_isDualSourceBlendingSupported) ? 1 : 0) << "\n";
fsHeader << "\n";
fsHeader << "#define FOG_OFFSET " << fogOffset << "\n";
fsHeader << "#define FOG_OFFSETF " << fogOffsetf << (((fogOffsetf == 0.0f) || (fogOffsetf == 1.0f)) ? ".0" : "") << "\n";
fsHeader << "#define FOG_STEP " << fogStep << "\n";
@ -1821,14 +1798,10 @@ Render3DError OpenGLRenderer_3_2::CreateFogProgram(const OGLFogProgramKey fogPro
if (this->_isShaderFixedLocationSupported)
{
fsHeader << "#define OUT_FOG_COLOR layout (location = 0, index = 0) out\n";
fsHeader << "#define OUT_FOG_WEIGHT layout (location = 0, index = 1) out\n";
fsHeader << "#define OUT_COLOR layout (location = 0) out\n";
}
else
{
fsHeader << "#define OUT_FOG_COLOR out\n";
fsHeader << "#define OUT_FOG_WEIGHT out\n";
fsHeader << "#define OUT_COLOR out\n";
}
@ -1860,18 +1833,7 @@ Render3DError OpenGLRenderer_3_2::CreateFogProgram(const OGLFogProgramKey fogPro
{
glBindAttribLocation(shaderID.program, OGLVertexAttributeID_Position, "inPosition");
glBindAttribLocation(shaderID.program, OGLVertexAttributeID_TexCoord0, "inTexCoord0");
#ifdef GL_VERSION_3_3
if (this->_isDualSourceBlendingSupported)
{
glBindFragDataLocationIndexed(shaderID.program, 0, 0, "outFogColor");
glBindFragDataLocationIndexed(shaderID.program, 0, 1, "outFogWeight");
}
else
#endif
{
glBindFragDataLocation(shaderID.program, 0, "outFragColor");
}
glBindFragDataLocation(shaderID.program, 0, "outFogWeight");
}
#endif
@ -1897,12 +1859,6 @@ Render3DError OpenGLRenderer_3_2::CreateFogProgram(const OGLFogProgramKey fogPro
glUniform1i(uniformTexGFog, OGLTextureUnitID_FogAttr);
glUniform1i(uniformTexFogDensityTable, OGLTextureUnitID_LookupTable);
if (!this->_isDualSourceBlendingSupported)
{
const GLint uniformTexGColor = glGetUniformLocation(shaderID.program, "texInFragColor");
glUniform1i(uniformTexGColor, OGLTextureUnitID_GColor);
}
return OGLERROR_NOERR;
}
@ -2498,26 +2454,20 @@ Render3DError OpenGLRenderer_3_2::BeginRender(const GFX3D_State &renderState, co
Render3DError OpenGLRenderer_3_2::PostprocessFramebuffer()
{
if (this->_clippedPolyCount < 1)
if ( (this->_clippedPolyCount < 1) ||
(!this->_enableEdgeMark && !this->_enableFog) )
{
return OGLERROR_NOERR;
}
OGLRenderRef &OGLRef = *this->ref;
if (this->_enableEdgeMark || this->_enableFog)
{
// Set up the postprocessing states
glViewport(0, 0, (GLsizei)this->_framebufferWidth, (GLsizei)this->_framebufferHeight);
glDisable(GL_DEPTH_TEST);
glBindBuffer(GL_ARRAY_BUFFER, OGLRef.vboPostprocessVtxID);
glBindVertexArray(OGLRef.vaoPostprocessStatesID);
}
else
{
return OGLERROR_NOERR;
}
if (this->_enableEdgeMark)
{
@ -2571,32 +2521,24 @@ Render3DError OpenGLRenderer_3_2::PostprocessFramebuffer()
}
OGLFogShaderID shaderID = this->_fogProgramMap[this->_fogProgramKey.key];
glUseProgram(shaderID.program);
glDisable(GL_STENCIL_TEST);
#ifdef GL_VERSION_3_3
if (this->_isDualSourceBlendingSupported)
{
glDrawBuffer(OGL_COLOROUT_ATTACHMENT_ID);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC1_COLOR, GL_ONE_MINUS_SRC1_COLOR);
glBlendEquation(GL_FUNC_ADD);
glUseProgram(shaderID.program);
glBlendFuncSeparate(GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_CONSTANT_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
glBlendColor( this->_pendingRenderStates.fogColor.r,
this->_pendingRenderStates.fogColor.g,
this->_pendingRenderStates.fogColor.b,
this->_pendingRenderStates.fogColor.a );
glDisable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST);
glEnable(GL_BLEND);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_DST_ALPHA);
glBlendEquationSeparate(GL_FUNC_ADD, GL_MAX);
}
else
#endif
{
glDrawBuffer(OGL_WORKING_ATTACHMENT_ID);
glDisable(GL_BLEND);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
this->_lastTextureDrawTarget = OGLTextureUnitID_FinalColor;
}
}
glBindVertexArray(0);

View File

@ -64,7 +64,6 @@ protected:
bool _is64kUBOSupported;
bool _isTBOSupported;
bool _isShaderFixedLocationSupported;
bool _isDualSourceBlendingSupported;
bool _isSampleShadingSupported;
bool _isConservativeDepthSupported;
bool _isConservativeDepthAMDSupported;

View File

@ -778,14 +778,10 @@ Render3DError OpenGLESRenderer_3_0::CreateFogProgram(const OGLFogProgramKey fogP
vsHeader << "#define IN_VTX_COLOR layout (location = " << OGLVertexAttributeID_Color << ") in\n";
std::stringstream fsHeader;
fsHeader << "#define USE_DUAL_SOURCE_BLENDING " << ((this->_isDualSourceBlendingSupported) ? 1 : 0) << "\n";
fsHeader << "\n";
fsHeader << "#define FOG_OFFSET " << fogOffset << "\n";
fsHeader << "#define FOG_OFFSETF " << fogOffsetf << (((fogOffsetf == 0.0f) || (fogOffsetf == 1.0f)) ? ".0" : "") << "\n";
fsHeader << "#define FOG_STEP " << fogStep << "\n";
fsHeader << "\n";
fsHeader << "#define OUT_FOG_COLOR layout (location = 0, index = 0) out\n";
fsHeader << "#define OUT_FOG_WEIGHT layout (location = 0, index = 1) out\n";
fsHeader << "#define OUT_COLOR layout (location = 0) out\n";
std::string vtxShaderCode = shaderHeader.str() + vsHeader.str() + std::string(vtxShaderCString);
@ -833,9 +829,6 @@ Render3DError OpenGLESRenderer_3_0::CreateFogProgram(const OGLFogProgramKey fogP
glUniform1i(uniformTexGFog, OGLTextureUnitID_FogAttr);
glUniform1i(uniformTexFogDensityTable, OGLTextureUnitID_LookupTable);
const GLint uniformTexGColor = glGetUniformLocation(shaderID.program, "texInFragColor");
glUniform1i(uniformTexGColor, OGLTextureUnitID_GColor);
return OGLERROR_NOERR;
}