Skip to content
Snippets Groups Projects

Add shader source output on compilation errors

Merged Jonathan Kunstwald requested to merge feature/add-shader-error-line-output into develop
1 file
+ 42
13
Compare changes
  • Side-by-side
  • Inline
+ 42
13
#include "Shader.hh"
#include <cctype>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <glow/glow.hh>
#include <glow/util/DefaultShaderParser.hh>
@@ -39,14 +42,46 @@ void Shader::compile()
glGetShaderiv(mObjectName, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 1)
{
std::vector<GLchar> log;
log.resize(logLength + 1);
glGetShaderInfoLog(mObjectName, logLength + 1, nullptr, log.data());
std::string logOutput;
{
std::vector<GLchar> log;
log.resize(static_cast<unsigned>(logLength) + 1u);
glGetShaderInfoLog(mObjectName, logLength + 1, nullptr, log.data());
logOutput = std::string(log.data());
}
error() << "Log for " << (mFileName.empty() ? to_string(this) : mFileName);
error() << "Shader compiler: " << log.data();
error() << "Shader compiler: " << logOutput;
mHasErrors = true;
mCompiled = false; // is this true?
// Check if NVidia-style line information is present in the log
if (strncmp(logOutput.c_str(), "0(", 2) == 0)
{
auto it = logOutput.begin() + 2;
while (isdigit(*it))
++it;
auto const lineNumberString = std::string(logOutput.begin() + 2, it);
auto const lineNumber = strtoul(lineNumberString.data(), nullptr, 0);
if (lineNumber != 0)
{
// Found a line number
unsigned readLineNumber = 1;
std::stringstream sourceStream(mSources.at(0));
for (; readLineNumber < lineNumber - 1; ++readLineNumber)
sourceStream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::string line;
line.reserve(256);
error() << "Source provoking error:";
for (auto num = (readLineNumber != 1) ? 0 : 1; std::getline(sourceStream, line, '\n') && num < 3; ++readLineNumber, ++num)
error() << readLineNumber << " | " << line;
}
}
}
else
{
@@ -76,10 +111,7 @@ void Shader::reload()
compile();
}
void Shader::setSource(const std::string& source)
{
setSource(std::vector<std::string>({source}));
}
void Shader::setSource(const std::string& source) { setSource(std::vector<std::string>({source})); }
void Shader::setSource(const std::vector<std::string>& sources)
{
@@ -93,7 +125,7 @@ void Shader::setSource(const std::vector<std::string>& sources)
srcs.resize(parsedSources.size());
for (auto i = 0u; i < parsedSources.size(); ++i)
srcs[i] = parsedSources[i].c_str();
glShaderSource(mObjectName, srcs.size(), srcs.data(), nullptr);
glShaderSource(mObjectName, static_cast<GLint>(srcs.size()), srcs.data(), nullptr);
}
SharedShader Shader::createFromSource(GLenum shaderType, const std::string& source)
@@ -160,10 +192,7 @@ bool Shader::newerVersionAvailable()
return false;
}
void Shader::setShaderParser(const SharedShaderParser& parser)
{
sParser = parser;
}
void Shader::setShaderParser(const SharedShaderParser& parser) { sParser = parser; }
bool Shader::resolveFile(const std::string& name, GLenum& shaderType, std::string& content, std::string& realFileName)
{
Loading