aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-08-19 02:02:20 +0200
committerhachem <im@hachem.wtf>2026-08-19 02:02:20 +0200
commitf7e05555fb4ff6d8e51bdb5734db4dab1841d593 (patch)
tree353d350b19da7b84ca11e5801d33a921407a963e /src
parent5c5fc10d8938fd2da1922f01c638d42e70eb1ab1 (diff)
[feat]: migrate to slang from glsl
Diffstat (limited to 'src')
-rw-r--r--src/Platform/OpenGL/OpenGLShader.cpp47
-rw-r--r--src/Platform/OpenGL/OpenGLShader.h4
-rw-r--r--src/Platform/OpenGL/OpenGLTexture.cpp6
3 files changed, 42 insertions, 15 deletions
diff --git a/src/Platform/OpenGL/OpenGLShader.cpp b/src/Platform/OpenGL/OpenGLShader.cpp
index 2992cdc..5d6dd66 100644
--- a/src/Platform/OpenGL/OpenGLShader.cpp
+++ b/src/Platform/OpenGL/OpenGLShader.cpp
@@ -19,9 +19,30 @@ namespace Donut
return 0;
}
+ // Shaders are authored in Slang and compiled to Assets/Shaders/generated/
+ // <name>.glsl by Tools/compile-shaders.sh. Given a legacy ".../<name>.glsl"
+ // path, prefer that generated file when present; otherwise fall back to the
+ // hand-written GLSL (e.g. shaders not yet ported to Slang).
+ 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 = ReadFile(filepath);
+ std::string resolved = ResolveShaderPath(filepath);
+ m_IsSlang = (resolved != filepath);
+ std::string source = ReadFile(resolved);
auto shaderSources = PreProcess(source);
Compile(shaderSources);
@@ -33,7 +54,7 @@ namespace Donut
}
OpenGLShader::OpenGLShader(const std::string& name, const std::string& vertexSrc, const std::string& fragmentSrc)
- : m_Name(name)
+ : m_Name(name)
{
std::unordered_map<uint32_t, std::string> sources;
sources[GL_VERTEX_SHADER] = vertexSrc;
@@ -42,7 +63,7 @@ namespace Donut
}
OpenGLShader::OpenGLShader(const std::string& name, const std::string& computeSrc)
- : m_Name(name)
+ : m_Name(name)
{
std::unordered_map<uint32_t, std::string> sources;
sources[GL_COMPUTE_SHADER] = computeSrc;
@@ -57,9 +78,9 @@ namespace Donut
std::string OpenGLShader::ReadFile(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 +111,7 @@ namespace Donut
size_t nextLinePos = source.find_first_not_of("\r\n", eol);
pos = source.find(typeToken, nextLinePos);
- shaderSources[ShaderTypeFromString(type)] = (pos == std::string::npos) ? source.substr(nextLinePos) :
+ shaderSources[ShaderTypeFromString(type)] = (pos == std::string::npos) ? source.substr(nextLinePos) :
source.substr(nextLinePos, pos - nextLinePos);
}
@@ -225,34 +246,34 @@ namespace Donut
glUniform1f(location, value);
}
- void OpenGLShader::UploadUniformFloat2(const std::string& name, const glm::vec2& value)
+ void OpenGLShader::UploadUniformFloat2(const std::string& name, const glm::vec2& value)
{
int location = glGetUniformLocation(m_RendererID, name.c_str());
glUniform2f(location, value.x, value.y);
}
- void OpenGLShader::UploadUniformFloat3(const std::string& name, const glm::vec3& value)
+ void OpenGLShader::UploadUniformFloat3(const std::string& name, const glm::vec3& value)
{
int location = glGetUniformLocation(m_RendererID, name.c_str());
glUniform3f(location, value.x, value.y, value.z);
}
- void OpenGLShader::UploadUniformFloat4(const std::string& name, const glm::vec4& value)
+ void OpenGLShader::UploadUniformFloat4(const std::string& name, const glm::vec4& value)
{
int location = glGetUniformLocation(m_RendererID, name.c_str());
glUniform4f(location, value.x, value.y, value.z, value.w);
}
- void OpenGLShader::UploadUniformMat3(const std::string& name, const glm::mat3& matrix)
+ void OpenGLShader::UploadUniformMat3(const std::string& name, const glm::mat3& matrix)
{
int location = glGetUniformLocation(m_RendererID, name.c_str());
- glUniformMatrix3fv(location, 1, GL_FALSE, glm::value_ptr(matrix));
+ glUniformMatrix3fv(location, 1, m_IsSlang ? GL_TRUE : GL_FALSE, glm::value_ptr(matrix));
}
- void OpenGLShader::UploadUniformMat4(const std::string& name, const glm::mat4& matrix)
+ void OpenGLShader::UploadUniformMat4(const std::string& name, const glm::mat4& matrix)
{
int location = glGetUniformLocation(m_RendererID, name.c_str());
- glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(matrix));
+ glUniformMatrix4fv(location, 1, m_IsSlang ? GL_TRUE : GL_FALSE, glm::value_ptr(matrix));
}
void OpenGLShader::Dispatch(uint32_t x, uint32_t y, uint32_t z)
diff --git a/src/Platform/OpenGL/OpenGLShader.h b/src/Platform/OpenGL/OpenGLShader.h
index 72d1d6e..4b63597 100644
--- a/src/Platform/OpenGL/OpenGLShader.h
+++ b/src/Platform/OpenGL/OpenGLShader.h
@@ -50,5 +50,9 @@ namespace Donut
private:
uint32_t m_RendererID = 0;
std::string m_Name;
+ // True when loaded from a Slang-compiled GLSL. Slang expects row-major
+ // matrix data, so matrix uniforms are transposed on upload (glm is
+ // column-major) to keep all matrix math correct.
+ bool m_IsSlang = false;
};
};
diff --git a/src/Platform/OpenGL/OpenGLTexture.cpp b/src/Platform/OpenGL/OpenGLTexture.cpp
index 1895575..16892c6 100644
--- a/src/Platform/OpenGL/OpenGLTexture.cpp
+++ b/src/Platform/OpenGL/OpenGLTexture.cpp
@@ -225,7 +225,9 @@ namespace Donut
glUseProgram(shaderProgram);
glUniform1i(glGetUniformLocation(shaderProgram, "u_EquirectangularMap"), 0);
- glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "u_Projection"), 1, GL_FALSE, &captureProjection[0][0]);
+ // EquirectToCubemap is authored in Slang (row-major); transpose glm's
+ // column-major matrices on upload (GL_TRUE) to match.
+ glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "u_Projection"), 1, GL_TRUE, &captureProjection[0][0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, hdrTexture);
@@ -233,7 +235,7 @@ namespace Donut
glBindFramebuffer(GL_FRAMEBUFFER, captureFBO);
for (unsigned int i = 0; i < 6; ++i)
{
- glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "u_View"), 1, GL_FALSE, &captureViews[i][0][0]);
+ glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "u_View"), 1, GL_TRUE, &captureViews[i][0][0]);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, m_RendererID, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(cubeVAO);