Show source lines when reporting shader compilation errors

This commit is contained in:
Nikita Lisitsa 2021-01-29 10:34:40 +03:00
parent fe873209c6
commit e8c0d9492b

View file

@ -5,6 +5,7 @@
#include <memory>
#include <vector>
#include <sstream>
namespace psemek::gfx
{
@ -209,6 +210,32 @@ namespace psemek::gfx
gl::Uniform2f(location_, i.min, i.max);
}
static std::string annotated_source(std::string_view source)
{
std::ostringstream os;
bool first = true;
int line = 0;
for (char c : source)
{
if (first)
{
os << line << ": ";
first = false;
}
os.put(c);
if (c == '\n')
{
++line;
first = true;
}
}
return os.str();
}
static void load_shader(GLuint shader, std::string_view source)
{
char const * vert_sources[1] { source.data() };
@ -224,7 +251,7 @@ namespace psemek::gfx
gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &log_len);
std::unique_ptr<char[]> log(new char [log_len]);
gl::GetShaderInfoLog(shader, log_len, nullptr, log.get());
throw std::runtime_error(util::to_string("Shader compilation failed: ", log.get(), "\nShader source: \n", source));
throw std::runtime_error(util::to_string("Shader compilation failed: ", log.get(), "\nShader source: \n", annotated_source(source)));
}
}