From f58e20f5e18ceef64e80b8584928b5fe84854973 Mon Sep 17 00:00:00 2001 From: hachem Date: Wed, 19 Aug 2026 02:02:20 +0200 Subject: feat: migrate to slang from glsl --- src/platform/opengl/opengl_shader.cpp | 43 ++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 13 deletions(-) (limited to 'src/platform/opengl/opengl_shader.cpp') diff --git a/src/platform/opengl/opengl_shader.cpp b/src/platform/opengl/opengl_shader.cpp index ddcd5e2..e7c3d6a 100644 --- a/src/platform/opengl/opengl_shader.cpp +++ b/src/platform/opengl/opengl_shader.cpp @@ -19,9 +19,26 @@ namespace Donut return 0; } + static std::string ResolveShaderPath(const std::string& filepath) + { + size_t slash = filepath.find_last_of("/\\"); + std::string dir = (slash == std::string::npos) ? std::string() : filepath.substr(0, slash + 1); + std::string file = (slash == std::string::npos) ? filepath : filepath.substr(slash + 1); + size_t dot = file.rfind('.'); + std::string base = (dot == std::string::npos) ? file : file.substr(0, dot); + + std::string generated = dir + "generated/" + base + ".glsl"; + std::ifstream test(generated); + if (test.good()) + return generated; + return filepath; + } + OpenGLShader::OpenGLShader(const std::string& filepath) { - std::string source = read_file(filepath); + std::string resolved = ResolveShaderPath(filepath); + m_is_slang = (resolved != filepath); + std::string source = read_file(resolved); auto shader_sources = pre_process(source); compile(shader_sources); @@ -33,7 +50,7 @@ namespace Donut } OpenGLShader::OpenGLShader(const std::string& name, const std::string& vertex_src, const std::string& fragment_src) - : m_name(name) + : m_name(name) { std::unordered_map sources; sources[GL_VERTEX_SHADER] = vertex_src; @@ -42,7 +59,7 @@ namespace Donut } OpenGLShader::OpenGLShader(const std::string& name, const std::string& compute_src) - : m_name(name) + : m_name(name) { std::unordered_map sources; sources[GL_COMPUTE_SHADER] = compute_src; @@ -57,9 +74,9 @@ namespace Donut std::string OpenGLShader::read_file(const std::string& filepath) { std::string result; - std::ifstream in(filepath, std::ios::in | + std::ifstream in(filepath, std::ios::in | std::ios::binary); - + if (in) { in.seekg(0, std::ios::end); @@ -90,7 +107,7 @@ namespace Donut size_t next_line_pos = source.find_first_not_of("\r\n", eol); pos = source.find(type_token, next_line_pos); - shader_sources[ShaderTypeFromString(type)] = (pos == std::string::npos) ? source.substr(next_line_pos) : + shader_sources[ShaderTypeFromString(type)] = (pos == std::string::npos) ? source.substr(next_line_pos) : source.substr(next_line_pos, pos - next_line_pos); } @@ -222,34 +239,34 @@ namespace Donut glUniform1f(location, value); } - void OpenGLShader::upload_uniform_float2(const std::string& name, const glm::vec2& value) + void OpenGLShader::upload_uniform_float2(const std::string& name, const glm::vec2& value) { int location = glGetUniformLocation(m_renderer_id, name.c_str()); glUniform2f(location, value.x, value.y); } - void OpenGLShader::upload_uniform_float3(const std::string& name, const glm::vec3& value) + void OpenGLShader::upload_uniform_float3(const std::string& name, const glm::vec3& value) { int location = glGetUniformLocation(m_renderer_id, name.c_str()); glUniform3f(location, value.x, value.y, value.z); } - void OpenGLShader::upload_uniform_float4(const std::string& name, const glm::vec4& value) + void OpenGLShader::upload_uniform_float4(const std::string& name, const glm::vec4& value) { int location = glGetUniformLocation(m_renderer_id, name.c_str()); glUniform4f(location, value.x, value.y, value.z, value.w); } - void OpenGLShader::upload_uniform_mat3(const std::string& name, const glm::mat3& matrix) + void OpenGLShader::upload_uniform_mat3(const std::string& name, const glm::mat3& matrix) { int location = glGetUniformLocation(m_renderer_id, name.c_str()); - glUniformMatrix3fv(location, 1, GL_FALSE, glm::value_ptr(matrix)); + glUniformMatrix3fv(location, 1, m_is_slang ? GL_TRUE : GL_FALSE, glm::value_ptr(matrix)); } - void OpenGLShader::upload_uniform_mat4(const std::string& name, const glm::mat4& matrix) + void OpenGLShader::upload_uniform_mat4(const std::string& name, const glm::mat4& matrix) { int location = glGetUniformLocation(m_renderer_id, name.c_str()); - glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(matrix)); + glUniformMatrix4fv(location, 1, m_is_slang ? GL_TRUE : GL_FALSE, glm::value_ptr(matrix)); } void OpenGLShader::dispatch(uint32_t x, uint32_t y, uint32_t z) -- cgit v1.3