OpenGLRenderer: Add shader source on compilation error

This commit is contained in:
Jérôme Leclercq 2022-02-18 13:05:19 +01:00
parent c33ab779d1
commit ebd1318512
3 changed files with 30 additions and 3 deletions

View File

@ -26,7 +26,8 @@ namespace Nz::GL
inline void Compile();
inline bool GetCompilationStatus(std::string* error = nullptr);
inline bool GetCompilationStatus(std::string* error = nullptr) const;
inline std::string GetSource() const;
inline void SetBinarySource(GLenum binaryFormat, const void* binary, GLsizei length);
inline void SetSource(const char* source, GLint length);

View File

@ -16,7 +16,7 @@ namespace Nz::GL
context.glCompileShader(m_objectId);
}
inline bool Shader::GetCompilationStatus(std::string* error)
inline bool Shader::GetCompilationStatus(std::string* error) const
{
assert(m_objectId);
const Context& context = EnsureDeviceContext();
@ -45,6 +45,28 @@ namespace Nz::GL
return true;
}
inline std::string Shader::GetSource() const
{
assert(m_objectId);
const Context& context = EnsureDeviceContext();
GLint sourceLength;
context.glGetShaderiv(m_objectId, GL_SHADER_SOURCE_LENGTH, &sourceLength);
if (sourceLength <= 1)
return {};
std::string source;
source.resize(sourceLength - 1);
GLsizei length;
context.glGetShaderSource(m_objectId, sourceLength, &length, &source[0]);
assert(length == sourceLength - 1);
return source;
}
inline void Shader::SetBinarySource(GLenum binaryFormat, const void* binary, GLsizei length)
{
assert(m_objectId);

View File

@ -159,6 +159,10 @@ namespace Nz
{
std::string errorLog;
if (!shader.GetCompilationStatus(&errorLog))
throw std::runtime_error("Failed to compile shader: " + errorLog);
{
std::string source = shader.GetSource();
throw std::runtime_error("Failed to compile shader: " + errorLog + "\nSource: " + source);
}
}
}