Skip to content

Commit

Permalink
OpenGL Renderer: Simplify the code by removing the references to mult…
Browse files Browse the repository at this point in the history
…iple output shaders, since commit 0c7cb99 has obsoleted them.
  • Loading branch information
rogerman committed Aug 7, 2024
1 parent c5ab8a6 commit 176e38c
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 180 deletions.
146 changes: 50 additions & 96 deletions desmume/src/OGLRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,6 @@ OpenGLRenderer::OpenGLRenderer()
_needsZeroDstAlphaPass = true;
_currentPolyIndex = 0;
_enableAlphaBlending = true;
_lastTextureDrawTarget = OGLTextureUnitID_GColor;
_geometryProgramFlags.value = 0;
_fogProgramKey.key = 0;
_fogProgramMap.clear();
Expand Down Expand Up @@ -3509,7 +3508,7 @@ void OpenGLRenderer_1_2::DestroyFogPrograms()
}
}

Render3DError OpenGLRenderer_1_2::CreateFramebufferOutput6665Program(const size_t outColorIndex, const char *vtxShaderCString, const char *fragShaderCString)
Render3DError OpenGLRenderer_1_2::CreateFramebufferOutput6665Program(const char *vtxShaderCString, const char *fragShaderCString)
{
Render3DError error = OGLERROR_NOERR;
OGLRenderRef &OGLRef = *this->ref;
Expand All @@ -3528,7 +3527,7 @@ Render3DError OpenGLRenderer_1_2::CreateFramebufferOutput6665Program(const size_

error = this->ShaderProgramCreate(OGLRef.vertexFramebufferOutput6665ShaderID,
OGLRef.fragmentFramebufferRGBA6665OutputShaderID,
OGLRef.programFramebufferRGBA6665OutputID[outColorIndex],
OGLRef.programFramebufferRGBA6665OutputID,
vtxShaderCode.c_str(),
fragShaderCString);
if (error != OGLERROR_NOERR)
Expand All @@ -3539,29 +3538,31 @@ Render3DError OpenGLRenderer_1_2::CreateFramebufferOutput6665Program(const size_
return error;
}

glBindAttribLocation(OGLRef.programFramebufferRGBA6665OutputID[outColorIndex], OGLVertexAttributeID_Position, "inPosition");
glBindAttribLocation(OGLRef.programFramebufferRGBA6665OutputID[outColorIndex], OGLVertexAttributeID_TexCoord0, "inTexCoord0");
glBindAttribLocation(OGLRef.programFramebufferRGBA6665OutputID, OGLVertexAttributeID_Position, "inPosition");
glBindAttribLocation(OGLRef.programFramebufferRGBA6665OutputID, OGLVertexAttributeID_TexCoord0, "inTexCoord0");

glLinkProgram(OGLRef.programFramebufferRGBA6665OutputID[outColorIndex]);
if (!this->ValidateShaderProgramLink(OGLRef.programFramebufferRGBA6665OutputID[outColorIndex]))
glLinkProgram(OGLRef.programFramebufferRGBA6665OutputID);
if (!this->ValidateShaderProgramLink(OGLRef.programFramebufferRGBA6665OutputID))
{
INFO("OpenGL: Failed to link the FRAMEBUFFER OUTPUT RGBA6665 shader program.\n");
glUseProgram(0);
this->DestroyFramebufferOutput6665Programs();
return OGLERROR_SHADER_CREATE_ERROR;
}

glValidateProgram(OGLRef.programFramebufferRGBA6665OutputID[outColorIndex]);
glUseProgram(OGLRef.programFramebufferRGBA6665OutputID[outColorIndex]);
glValidateProgram(OGLRef.programFramebufferRGBA6665OutputID);
glUseProgram(OGLRef.programFramebufferRGBA6665OutputID);

const GLint uniformTexGColor = glGetUniformLocation(OGLRef.programFramebufferRGBA6665OutputID[outColorIndex], "texInFragColor");
if (outColorIndex == 0)
const GLint uniformTexGColor = glGetUniformLocation(OGLRef.programFramebufferRGBA6665OutputID, "texInFragColor");
if (this->isFBOSupported)
{
glUniform1i(uniformTexGColor, OGLTextureUnitID_FinalColor);
glUniform1i(uniformTexGColor, OGLTextureUnitID_GColor);
}
else
{
glUniform1i(uniformTexGColor, OGLTextureUnitID_GColor);
// Reading back the output framebuffer without FBOs requires
// sampling from the working buffer.
glUniform1i(uniformTexGColor, OGLTextureUnitID_FinalColor);
}

return OGLERROR_NOERR;
Expand All @@ -3576,20 +3577,12 @@ void OpenGLRenderer_1_2::DestroyFramebufferOutput6665Programs()
return;
}

if (OGLRef.programFramebufferRGBA6665OutputID[0] != 0)
if (OGLRef.programFramebufferRGBA6665OutputID != 0)
{
glDetachShader(OGLRef.programFramebufferRGBA6665OutputID[0], OGLRef.vertexFramebufferOutput6665ShaderID);
glDetachShader(OGLRef.programFramebufferRGBA6665OutputID[0], OGLRef.fragmentFramebufferRGBA6665OutputShaderID);
glDeleteProgram(OGLRef.programFramebufferRGBA6665OutputID[0]);
OGLRef.programFramebufferRGBA6665OutputID[0] = 0;
}

if (OGLRef.programFramebufferRGBA6665OutputID[1] != 0)
{
glDetachShader(OGLRef.programFramebufferRGBA6665OutputID[1], OGLRef.vertexFramebufferOutput6665ShaderID);
glDetachShader(OGLRef.programFramebufferRGBA6665OutputID[1], OGLRef.fragmentFramebufferRGBA6665OutputShaderID);
glDeleteProgram(OGLRef.programFramebufferRGBA6665OutputID[1]);
OGLRef.programFramebufferRGBA6665OutputID[1] = 0;
glDetachShader(OGLRef.programFramebufferRGBA6665OutputID, OGLRef.vertexFramebufferOutput6665ShaderID);
glDetachShader(OGLRef.programFramebufferRGBA6665OutputID, OGLRef.fragmentFramebufferRGBA6665OutputShaderID);
glDeleteProgram(OGLRef.programFramebufferRGBA6665OutputID);
OGLRef.programFramebufferRGBA6665OutputID = 0;
}

glDeleteShader(OGLRef.vertexFramebufferOutput6665ShaderID);
Expand All @@ -3598,7 +3591,7 @@ void OpenGLRenderer_1_2::DestroyFramebufferOutput6665Programs()
OGLRef.fragmentFramebufferRGBA6665OutputShaderID = 0;
}

Render3DError OpenGLRenderer_1_2::CreateFramebufferOutput8888Program(const size_t outColorIndex, const char *vtxShaderCString, const char *fragShaderCString)
Render3DError OpenGLRenderer_1_2::CreateFramebufferOutput8888Program(const char *vtxShaderCString, const char *fragShaderCString)
{
Render3DError error = OGLERROR_NOERR;
OGLRenderRef &OGLRef = *this->ref;
Expand All @@ -3617,7 +3610,7 @@ Render3DError OpenGLRenderer_1_2::CreateFramebufferOutput8888Program(const size_

error = this->ShaderProgramCreate(OGLRef.vertexFramebufferOutput8888ShaderID,
OGLRef.fragmentFramebufferRGBA8888OutputShaderID,
OGLRef.programFramebufferRGBA8888OutputID[outColorIndex],
OGLRef.programFramebufferRGBA8888OutputID,
vtxShaderCode.c_str(),
fragShaderCString);
if (error != OGLERROR_NOERR)
Expand All @@ -3628,29 +3621,31 @@ Render3DError OpenGLRenderer_1_2::CreateFramebufferOutput8888Program(const size_
return error;
}

glBindAttribLocation(OGLRef.programFramebufferRGBA8888OutputID[outColorIndex], OGLVertexAttributeID_Position, "inPosition");
glBindAttribLocation(OGLRef.programFramebufferRGBA8888OutputID[outColorIndex], OGLVertexAttributeID_TexCoord0, "inTexCoord0");
glBindAttribLocation(OGLRef.programFramebufferRGBA8888OutputID, OGLVertexAttributeID_Position, "inPosition");
glBindAttribLocation(OGLRef.programFramebufferRGBA8888OutputID, OGLVertexAttributeID_TexCoord0, "inTexCoord0");

glLinkProgram(OGLRef.programFramebufferRGBA8888OutputID[outColorIndex]);
if (!this->ValidateShaderProgramLink(OGLRef.programFramebufferRGBA8888OutputID[outColorIndex]))
glLinkProgram(OGLRef.programFramebufferRGBA8888OutputID);
if (!this->ValidateShaderProgramLink(OGLRef.programFramebufferRGBA8888OutputID))
{
INFO("OpenGL: Failed to link the FRAMEBUFFER OUTPUT RGBA8888 shader program.\n");
glUseProgram(0);
this->DestroyFramebufferOutput8888Programs();
return OGLERROR_SHADER_CREATE_ERROR;
}

glValidateProgram(OGLRef.programFramebufferRGBA8888OutputID[outColorIndex]);
glUseProgram(OGLRef.programFramebufferRGBA8888OutputID[outColorIndex]);
glValidateProgram(OGLRef.programFramebufferRGBA8888OutputID);
glUseProgram(OGLRef.programFramebufferRGBA8888OutputID);

const GLint uniformTexGColor = glGetUniformLocation(OGLRef.programFramebufferRGBA8888OutputID[outColorIndex], "texInFragColor");
if (outColorIndex == 0)
const GLint uniformTexGColor = glGetUniformLocation(OGLRef.programFramebufferRGBA8888OutputID, "texInFragColor");
if (this->isFBOSupported)
{
glUniform1i(uniformTexGColor, OGLTextureUnitID_FinalColor);
glUniform1i(uniformTexGColor, OGLTextureUnitID_GColor);
}
else
{
glUniform1i(uniformTexGColor, OGLTextureUnitID_GColor);
// Reading back the output framebuffer without FBOs requires
// sampling from the working buffer.
glUniform1i(uniformTexGColor, OGLTextureUnitID_FinalColor);
}

return OGLERROR_NOERR;
Expand All @@ -3665,20 +3660,12 @@ void OpenGLRenderer_1_2::DestroyFramebufferOutput8888Programs()
return;
}

if (OGLRef.programFramebufferRGBA8888OutputID[0] != 0)
if (OGLRef.programFramebufferRGBA8888OutputID != 0)
{
glDetachShader(OGLRef.programFramebufferRGBA8888OutputID[0], OGLRef.vertexFramebufferOutput8888ShaderID);
glDetachShader(OGLRef.programFramebufferRGBA8888OutputID[0], OGLRef.fragmentFramebufferRGBA8888OutputShaderID);
glDeleteProgram(OGLRef.programFramebufferRGBA8888OutputID[0]);
OGLRef.programFramebufferRGBA8888OutputID[0] = 0;
}

if (OGLRef.programFramebufferRGBA8888OutputID[1] != 0)
{
glDetachShader(OGLRef.programFramebufferRGBA8888OutputID[1], OGLRef.vertexFramebufferOutput8888ShaderID);
glDetachShader(OGLRef.programFramebufferRGBA8888OutputID[1], OGLRef.fragmentFramebufferRGBA8888OutputShaderID);
glDeleteProgram(OGLRef.programFramebufferRGBA8888OutputID[1]);
OGLRef.programFramebufferRGBA8888OutputID[1] = 0;
glDetachShader(OGLRef.programFramebufferRGBA8888OutputID, OGLRef.vertexFramebufferOutput8888ShaderID);
glDetachShader(OGLRef.programFramebufferRGBA8888OutputID, OGLRef.fragmentFramebufferRGBA8888OutputShaderID);
glDeleteProgram(OGLRef.programFramebufferRGBA8888OutputID);
OGLRef.programFramebufferRGBA8888OutputID = 0;
}

glDeleteShader(OGLRef.vertexFramebufferOutput8888ShaderID);
Expand Down Expand Up @@ -3739,25 +3726,13 @@ Render3DError OpenGLRenderer_1_2::InitPostprocessingPrograms(const char *edgeMar
}
}

error = this->CreateFramebufferOutput6665Program(0, framebufferOutputVtxShaderCString, framebufferOutputRGBA6665FragShaderCString);
error = this->CreateFramebufferOutput6665Program(framebufferOutputVtxShaderCString, framebufferOutputRGBA6665FragShaderCString);
if (error != OGLERROR_NOERR)
{
return error;
}

error = this->CreateFramebufferOutput6665Program(1, framebufferOutputVtxShaderCString, framebufferOutputRGBA6665FragShaderCString);
if (error != OGLERROR_NOERR)
{
return error;
}

error = this->CreateFramebufferOutput8888Program(0, framebufferOutputVtxShaderCString, framebufferOutputRGBA8888FragShaderCString);
if (error != OGLERROR_NOERR)
{
return error;
}

error = this->CreateFramebufferOutput8888Program(1, framebufferOutputVtxShaderCString, framebufferOutputRGBA8888FragShaderCString);
error = this->CreateFramebufferOutput8888Program(framebufferOutputVtxShaderCString, framebufferOutputRGBA8888FragShaderCString);
if (error != OGLERROR_NOERR)
{
return error;
Expand Down Expand Up @@ -4126,32 +4101,17 @@ Render3DError OpenGLRenderer_1_2::ReadBackPixels()
// should be necessary at this point.
if (this->willFlipAndConvertFramebufferOnGPU)
{
const GLuint convertProgramID = (this->_outputFormat == NDSColorFormat_BGR666_Rev) ? OGLRef.programFramebufferRGBA6665OutputID : OGLRef.programFramebufferRGBA8888OutputID;
glUseProgram(convertProgramID);

if (this->isFBOSupported)
{
if (this->_lastTextureDrawTarget == OGLTextureUnitID_GColor)
{
const GLuint convertProgramID = (this->_outputFormat == NDSColorFormat_BGR666_Rev) ? OGLRef.programFramebufferRGBA6665OutputID[1] : OGLRef.programFramebufferRGBA8888OutputID[1];
glUseProgram(convertProgramID);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, OGLRef.fboRenderID);
glReadBuffer(OGL_WORKING_ATTACHMENT_ID);
glDrawBuffer(OGL_WORKING_ATTACHMENT_ID);
this->_lastTextureDrawTarget = OGLTextureUnitID_FinalColor;
}
else
{
const GLuint convertProgramID = (this->_outputFormat == NDSColorFormat_BGR666_Rev) ? OGLRef.programFramebufferRGBA6665OutputID[0] : OGLRef.programFramebufferRGBA8888OutputID[0];
glUseProgram(convertProgramID);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, OGLRef.fboRenderID);
glReadBuffer(OGL_COLOROUT_ATTACHMENT_ID);
glDrawBuffer(OGL_COLOROUT_ATTACHMENT_ID);
this->_lastTextureDrawTarget = OGLTextureUnitID_GColor;
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, OGLRef.fboRenderID);
glReadBuffer(OGL_WORKING_ATTACHMENT_ID);
glDrawBuffer(OGL_WORKING_ATTACHMENT_ID);
}
else
{
const GLuint convertProgramID = (this->_outputFormat == NDSColorFormat_BGR666_Rev) ? OGLRef.programFramebufferRGBA6665OutputID[0] : OGLRef.programFramebufferRGBA8888OutputID[0];
glUseProgram(convertProgramID);

glActiveTexture(GL_TEXTURE0 + OGLTextureUnitID_FinalColor);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, (GLsizei)this->_framebufferWidth, (GLsizei)this->_framebufferHeight);
glActiveTexture(GL_TEXTURE0);
Expand Down Expand Up @@ -4563,7 +4523,6 @@ Render3DError OpenGLRenderer_1_2::RenderGeometry()
if (!this->_willUseMultisampleShaders)
{
this->_ResolveGeometry();
this->_lastTextureDrawTarget = OGLTextureUnitID_GColor;
}

return OGLERROR_NOERR;
Expand Down Expand Up @@ -4716,7 +4675,6 @@ Render3DError OpenGLRenderer_1_2::EndRender()
if (this->_willUseMultisampleShaders)
{
this->_ResolveFinalFramebuffer();
this->_lastTextureDrawTarget = OGLTextureUnitID_GColor;
}

this->ReadBackPixels();
Expand Down Expand Up @@ -5459,17 +5417,13 @@ Render3DError OpenGLRenderer_1_2::SetFramebufferSize(size_t w, size_t h)

if (OGLRef.readPixelsBestFormat == GL_BGRA)
{
this->CreateFramebufferOutput6665Program(0, FramebufferOutputVtxShader_100, FramebufferOutputBGRA6665FragShader_100);
this->CreateFramebufferOutput6665Program(1, FramebufferOutputVtxShader_100, FramebufferOutputBGRA6665FragShader_100);
this->CreateFramebufferOutput8888Program(0, FramebufferOutputVtxShader_100, FramebufferOutputBGRA8888FragShader_100);
this->CreateFramebufferOutput8888Program(1, FramebufferOutputVtxShader_100, FramebufferOutputBGRA8888FragShader_100);
this->CreateFramebufferOutput6665Program(FramebufferOutputVtxShader_100, FramebufferOutputBGRA6665FragShader_100);
this->CreateFramebufferOutput8888Program(FramebufferOutputVtxShader_100, FramebufferOutputBGRA8888FragShader_100);
}
else
{
this->CreateFramebufferOutput6665Program(0, FramebufferOutputVtxShader_100, FramebufferOutputRGBA6665FragShader_100);
this->CreateFramebufferOutput6665Program(1, FramebufferOutputVtxShader_100, FramebufferOutputRGBA6665FragShader_100);
this->CreateFramebufferOutput8888Program(0, FramebufferOutputVtxShader_100, FramebufferOutputRGBA8888FragShader_100);
this->CreateFramebufferOutput8888Program(1, FramebufferOutputVtxShader_100, FramebufferOutputRGBA8888FragShader_100);
this->CreateFramebufferOutput6665Program(FramebufferOutputVtxShader_100, FramebufferOutputRGBA6665FragShader_100);
this->CreateFramebufferOutput8888Program(FramebufferOutputVtxShader_100, FramebufferOutputRGBA8888FragShader_100);
}
}

Expand Down
13 changes: 6 additions & 7 deletions desmume/src/OGLRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -673,8 +673,8 @@ struct OGLRenderRef
GLuint fragmentFramebufferRGBA6665OutputShaderID;
GLuint fragmentFramebufferRGBA8888OutputShaderID;
GLuint programEdgeMarkID;
GLuint programFramebufferRGBA6665OutputID[2];
GLuint programFramebufferRGBA8888OutputID[2];
GLuint programFramebufferRGBA6665OutputID;
GLuint programFramebufferRGBA8888OutputID;

GLint uniformStateEnableFogAlphaOnly;
GLint uniformStateClearPolyID;
Expand Down Expand Up @@ -891,7 +891,6 @@ class OpenGLRenderer : public Render3D
bool _needsZeroDstAlphaPass;
size_t _currentPolyIndex;
bool _enableAlphaBlending;
OGLTextureUnitID _lastTextureDrawTarget;
OGLGeometryFlags _geometryProgramFlags;
OGLFogProgramKey _fogProgramKey;
std::map<u32, OGLFogShaderID> _fogProgramMap;
Expand Down Expand Up @@ -946,9 +945,9 @@ class OpenGLRenderer : public Render3D
virtual Render3DError CreateFogProgram(const OGLFogProgramKey fogProgramKey, const bool isMultisample, const char *vtxShaderCString, const char *fragShaderCString) = 0;
virtual void DestroyFogProgram(const OGLFogProgramKey fogProgramKey) = 0;
virtual void DestroyFogPrograms() = 0;
virtual Render3DError CreateFramebufferOutput6665Program(const size_t outColorIndex, const char *vtxShaderCString, const char *fragShaderCString) = 0;
virtual Render3DError CreateFramebufferOutput6665Program(const char *vtxShaderCString, const char *fragShaderCString) = 0;
virtual void DestroyFramebufferOutput6665Programs() = 0;
virtual Render3DError CreateFramebufferOutput8888Program(const size_t outColorIndex, const char *vtxShaderCString, const char *fragShaderCString) = 0;
virtual Render3DError CreateFramebufferOutput8888Program(const char *vtxShaderCString, const char *fragShaderCString) = 0;
virtual void DestroyFramebufferOutput8888Programs() = 0;

virtual Render3DError InitFinalRenderStates(const std::set<std::string> *oglExtensionSet) = 0;
Expand Down Expand Up @@ -1024,9 +1023,9 @@ class OpenGLRenderer_1_2 : public OpenGLRenderer
virtual Render3DError CreateFogProgram(const OGLFogProgramKey fogProgramKey, const bool isMultisample, const char *vtxShaderCString, const char *fragShaderCString);
virtual void DestroyFogProgram(const OGLFogProgramKey fogProgramKey);
virtual void DestroyFogPrograms();
virtual Render3DError CreateFramebufferOutput6665Program(const size_t outColorIndex, const char *vtxShaderCString, const char *fragShaderCString);
virtual Render3DError CreateFramebufferOutput6665Program(const char *vtxShaderCString, const char *fragShaderCString);
virtual void DestroyFramebufferOutput6665Programs();
virtual Render3DError CreateFramebufferOutput8888Program(const size_t outColorIndex, const char *vtxShaderCString, const char *fragShaderCString);
virtual Render3DError CreateFramebufferOutput8888Program(const char *vtxShaderCString, const char *fragShaderCString);
virtual void DestroyFramebufferOutput8888Programs();

virtual Render3DError InitFinalRenderStates(const std::set<std::string> *oglExtensionSet);
Expand Down
Loading

0 comments on commit 176e38c

Please sign in to comment.