Skip to content

Commit

Permalink
CStdShaderProgram: Move validation from Link into its own function to…
Browse files Browse the repository at this point in the history
… fix validation errors due to uniforms that have not yet been assigned a value to
  • Loading branch information
Fulgen301 committed Oct 3, 2023
1 parent fdbc246 commit da02a0e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/StdDDraw2.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ class CStdShaderProgram
bool AddShader(CStdShader *shader);

virtual void Link() = 0;
virtual void Validate() = 0;
void Select();
static void Deselect();
virtual void Clear();
Expand Down
72 changes: 35 additions & 37 deletions src/StdGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,45 +143,9 @@ void CStdGLShader::PrepareSource()
void CStdGLShaderProgram::Link()
{
EnsureProgram();

glLinkProgram(shaderProgram);

GLint status = 0;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &status);
if (!status)
{
GLint size = 0;
glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &size);
assert(size);
if (size)
{
std::string errorMessage;
errorMessage.resize(size);
glGetProgramInfoLog(shaderProgram, size, nullptr, errorMessage.data());
errorMessage = errorMessage.c_str();
throw Exception{errorMessage};
}

throw Exception{"Link failed"};
}

glValidateProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_VALIDATE_STATUS, &status);
if (!status)
{
GLint size = 0;
glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &size);
if (size)
{
std::string errorMessage;
errorMessage.resize(size);
glGetProgramInfoLog(shaderProgram, size, nullptr, errorMessage.data());
errorMessage = errorMessage.c_str();
throw Exception{errorMessage};
}

throw Exception{"Validation failed"};
}
CheckStatus(GL_LINK_STATUS);

for (const auto &shader : shaders)
{
Expand All @@ -191,6 +155,13 @@ void CStdGLShaderProgram::Link()
shaders.clear();
}

void CStdGLShaderProgram::Validate()
{
EnsureProgram();
glValidateProgram(shaderProgram);
CheckStatus(GL_VALIDATE_STATUS);
}

void CStdGLShaderProgram::Clear()
{
for (const auto &shader : shaders)
Expand Down Expand Up @@ -241,6 +212,27 @@ void CStdGLShaderProgram::OnDeselect()
glUseProgram(GL_NONE);
}

void CStdGLShaderProgram::CheckStatus(const GLenum type)
{
GLint status{0};
glGetProgramiv(shaderProgram, type, &status);
if (!status)
{
GLint size = 0;
glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &size);
if (size)
{
std::string errorMessage;
errorMessage.resize(size);
glGetProgramInfoLog(shaderProgram, size, NULL, errorMessage.data());
errorMessage = errorMessage.c_str();
throw Exception{errorMessage};
}

throw Exception{"Status error"};
}
}

template<GLenum T, std::size_t Dimensions>
CStdGLTexture<T, Dimensions>::CStdGLTexture(std::array<int32_t, Dimensions> dimensions, const GLenum internalFormat, const GLenum format, const GLenum type)
: dimensions{std::move(dimensions)}, internalFormat{internalFormat}, format{format}, type{type}
Expand Down Expand Up @@ -1238,18 +1230,24 @@ bool CStdGL::RestoreDeviceObjects()
};

setUniforms(BlitShader);
BlitShader.Validate();

setUniforms(BlitShaderMod2);
BlitShaderMod2.Validate();

if (DummyShader)
{
setUniforms(DummyShader);
DummyShader.Validate();
}

setUniforms(LandscapeShader); // Last so that the shader is selected for the subsequent calls

LandscapeShader.SetUniform("maskSampler", glUniform1i, 1);
LandscapeShader.SetUniform("liquidSampler", glUniform1i, 2);

LandscapeShader.Validate();

CStdShaderProgram::Deselect();

if (!Config.Graphics.DisableGamma)
Expand Down
4 changes: 4 additions & 0 deletions src/StdGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class CStdGLShaderProgram : public CStdShaderProgram
explicit operator bool() const override { return /*glIsProgram(*/shaderProgram/*)*/; }

void Link() override;
void Validate() override;
void Clear() override;

void EnsureProgram() override;
Expand Down Expand Up @@ -121,6 +122,9 @@ class CStdGLShaderProgram : public CStdShaderProgram
return true;
}

private:
void CheckStatus(GLenum type);

protected:
GLuint shaderProgram{GL_NONE};

Expand Down

0 comments on commit da02a0e

Please sign in to comment.