14constexpr std::string_view GL_STEM_SUFFIX =
"_gl";
17std::string StemFromPath(
const std::string& path) {
18 std::string stem = std::filesystem::path(path).filename().string();
19 stem = stem.substr(0, stem.find(
'.'));
20 if (stem.ends_with(GL_STEM_SUFFIX)) {
21 stem.resize(stem.size() - GL_STEM_SUFFIX.size());
27std::string ReadInfoLog(GLuint
object, PFNGLGETSHADERIVPROC getParam,
28 PFNGLGETSHADERINFOLOGPROC getLog) {
30 getParam(
object, GL_INFO_LOG_LENGTH, &length);
31 if (length <= 0)
return {};
32 std::string log(
static_cast<size_t>(length),
'\0');
34 getLog(
object, length, &written, log.data());
35 log.resize(
static_cast<size_t>(written));
36 while (!log.empty() && (log.back() ==
'\n' || log.back() ==
'\r')) {
42#ifdef SLEAK_HOT_RELOAD
44std::unordered_set<OpenGLShader*>& LiveShaders() {
45 static auto* shaders =
new std::unordered_set<OpenGLShader*>();
53#ifdef SLEAK_HOT_RELOAD
54 LiveShaders().insert(
this);
59#ifdef SLEAK_HOT_RELOAD
60 LiveShaders().erase(
this);
63 glDeleteProgram(m_program);
68#ifdef SLEAK_HOT_RELOAD
69const std::unordered_set<OpenGLShader*>& OpenGLShader::GetLiveShaders() {
76 std::string basePath = shaderPath;
77 auto hlslPos = basePath.rfind(
".hlsl");
78 if (hlslPos != std::string::npos) {
79 basePath = basePath.substr(0, hlslPos);
81 return compile(basePath +
"_gl.vert", basePath +
"_gl.frag");
85 const std::string& fragPath) {
86 m_vertPath = vertPath;
87 m_fragPath = fragPath;
88 m_vertStem = StemFromPath(vertPath);
89 m_fragStem = StemFromPath(fragPath);
90 return BuildProgram();
94 if (m_vertPath.empty() || m_fragPath.empty())
return false;
95 return BuildProgram();
98bool OpenGLShader::BuildProgram() {
99 const std::string vertSource = ReadFile(m_vertPath);
100 const std::string fragSource = ReadFile(m_fragPath);
102 if (vertSource.empty() || fragSource.empty()) {
103 SLEAK_ERROR(
"Failed to read shader files: {} and {}", m_vertPath,
108 GLuint vertShader = 0;
109 GLuint fragShader = 0;
110 if (!CompileShaderSource(vertSource, GL_VERTEX_SHADER, m_vertPath,
114 if (!CompileShaderSource(fragSource, GL_FRAGMENT_SHADER, m_fragPath,
116 glDeleteShader(vertShader);
120 GLuint program = glCreateProgram();
121 glAttachShader(program, vertShader);
122 glAttachShader(program, fragShader);
123 glLinkProgram(program);
124 glDetachShader(program, vertShader);
125 glDetachShader(program, fragShader);
126 glDeleteShader(vertShader);
127 glDeleteShader(fragShader);
129 GLint linked = GL_FALSE;
130 glGetProgramiv(program, GL_LINK_STATUS, &linked);
131 if (linked != GL_TRUE) {
132 SLEAK_ERROR(
"Shader program linking failed ({} + {}): {}", m_vertPath,
134 ReadInfoLog(program, glGetProgramiv, glGetProgramInfoLog));
135 glDeleteProgram(program);
139 if (m_program != 0) glDeleteProgram(m_program);
145 if (m_program != 0) {
146 glUseProgram(m_program);
150bool OpenGLShader::CompileShaderSource(
const std::string& source, GLenum type,
151 const std::string& path,
153 shader = glCreateShader(type);
154 const char* src = source.c_str();
155 glShaderSource(shader, 1, &src,
nullptr);
156 glCompileShader(shader);
158 GLint success = GL_FALSE;
159 glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
160 if (success != GL_TRUE) {
161 SLEAK_ERROR(
"Shader compilation failed ({}): {}", path,
162 ReadInfoLog(shader, glGetShaderiv, glGetShaderInfoLog));
163 glDeleteShader(shader);
170std::string OpenGLShader::ReadFile(
const std::string& path) {
171 std::ifstream file(path);
172 if (!file.is_open()) {
176 std::stringstream ss;
bool Reload()
Rebuilds the program from its files, keeping the old one on failure.
void bind() override
Binds this program as the active shader for subsequent draws.
bool compile(const std::string &shaderPath) override
Splits a combined shader file into vertex/fragment stages and links them.
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.