From ba305de5960410c1fdecd26d4d59a4c8ebfa2830 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 25 Dec 2024 00:34:24 -0600 Subject: [PATCH] prog --- assets/shaders/hello-world.slang | 24 +++---- .../material/SimpleTexturedMaterial.hpp | 30 ++++++--- src/dawn/display/shader/CMakeLists.txt | 4 +- src/dawn/display/shader/IShaderData.cpp | 20 ++++++ src/dawn/display/shader/IShaderData.hpp | 42 +++++++++++++ src/dawnopengl/display/shader/CMakeLists.txt | 1 + src/dawnopengl/display/shader/ShaderData.cpp | 63 +++++++++++++++++++ src/dawnopengl/display/shader/ShaderData.hpp | 24 +++++++ .../display/shader/ShaderProgram.cpp | 59 ++++++++++------- .../display/shader/ShaderProgram.hpp | 9 ++- 10 files changed, 227 insertions(+), 49 deletions(-) create mode 100644 src/dawn/display/shader/IShaderData.cpp create mode 100644 src/dawn/display/shader/IShaderData.hpp create mode 100644 src/dawnopengl/display/shader/ShaderData.cpp create mode 100644 src/dawnopengl/display/shader/ShaderData.hpp diff --git a/assets/shaders/hello-world.slang b/assets/shaders/hello-world.slang index 819d16eb..5d9be466 100644 --- a/assets/shaders/hello-world.slang +++ b/assets/shaders/hello-world.slang @@ -1,11 +1,9 @@ -struct Uniforms { - float4x4 projection; - float4x4 view; - float4x4 model; - float4 u_Color; - bool u_HasTexture; - Sampler2D u_Texture; -}; +uniform float4x4 u_Projection; +uniform float4x4 u_View; +uniform float4x4 u_Model; +uniform float4 u_Color; +uniform bool u_HasTexture; +uniform Sampler2D u_Texture; struct AssembledVertex { float3 position : POSITION; @@ -25,8 +23,6 @@ float4 someFunction(float4 color) { return color * float4(0.5, 0.5, 0.5, 1.0); } -uniform ParameterBlock uniforms; - [shader("vertex")] VertexStageOutput vertexMain( AssembledVertex assembledVertex @@ -39,7 +35,7 @@ VertexStageOutput vertexMain( output.sv_position = mul( float4(position, 1.0), - mul(uniforms.model, mul(uniforms.view, uniforms.projection)) + mul(u_Model, mul(u_View, u_Projection)) ); return output; @@ -50,10 +46,10 @@ Fragment fragmentMain( float2 uv: UV ) : SV_Target { Fragment output; - if(uniforms.u_HasTexture) { - output.color = uniforms.u_Texture.Sample(uv) * uniforms.u_Color; + if(u_HasTexture) { + output.color = u_Texture.Sample(uv) * u_Color; } else { - output.color = someFunction(uniforms.u_Color); + output.color = someFunction(u_Color); } return output; } \ No newline at end of file diff --git a/src/dawn/component/display/material/SimpleTexturedMaterial.hpp b/src/dawn/component/display/material/SimpleTexturedMaterial.hpp index 25f21238..4433402a 100644 --- a/src/dawn/component/display/material/SimpleTexturedMaterial.hpp +++ b/src/dawn/component/display/material/SimpleTexturedMaterial.hpp @@ -5,17 +5,29 @@ #pragma once #include "component/display/material/Material.hpp" -#include "display/Texture.hpp" +#include "display/shader/ShaderData.hpp" namespace Dawn { - struct SimpleTexturedMaterialShaderData { - int32_t t; - struct Color color; - glm::mat4 model; - glm::mat4 projection; - glm::mat4 view; - bool hasTexture; - std::shared_ptr texture; + struct SimpleTexturedMaterialShaderData : public ShaderData { + protected: + void writeData() override { + this->beginStruct(); + // this->writeMat4(this->projection); + // this->writeMat4(this->view); + // this->writeMat4(this->model); + // this->writeColor(this->color); + // this->writeBoolean(this->hasTexture); + // this->writeTexture(this->texture); + this->endStruct(); + } + + public: + struct Color color; + glm::mat4 model; + glm::mat4 projection; + glm::mat4 view; + bool hasTexture; + std::shared_ptr texture; }; class SimpleTexturedMaterial : public Material { diff --git a/src/dawn/display/shader/CMakeLists.txt b/src/dawn/display/shader/CMakeLists.txt index 1258ebd7..a2b83b40 100644 --- a/src/dawn/display/shader/CMakeLists.txt +++ b/src/dawn/display/shader/CMakeLists.txt @@ -6,9 +6,9 @@ # Sources target_sources(${DAWN_TARGET_NAME} PRIVATE - IShaderProgram.cpp - IShaderStage.cpp ShaderManager.cpp ShaderManagerSlangFileSystem.cpp + IShaderStage.cpp IShaderProgram.cpp + IShaderData.cpp ) \ No newline at end of file diff --git a/src/dawn/display/shader/IShaderData.cpp b/src/dawn/display/shader/IShaderData.cpp new file mode 100644 index 00000000..94c60524 --- /dev/null +++ b/src/dawn/display/shader/IShaderData.cpp @@ -0,0 +1,20 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "IShaderData.hpp" +#include "assert/assert.hpp" + +using namespace Dawn; + +void IShaderData::write(std::shared_ptr shader) { + assertNotNull(shader, "Shader cannot be null."); + this->shader = shader; + this->writeData(); + this->shader = nullptr; +} + +std::shared_ptr IShaderData::getShader() { + return this->shader; +} \ No newline at end of file diff --git a/src/dawn/display/shader/IShaderData.hpp b/src/dawn/display/shader/IShaderData.hpp new file mode 100644 index 00000000..388f14a5 --- /dev/null +++ b/src/dawn/display/shader/IShaderData.hpp @@ -0,0 +1,42 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "display/Texture.hpp" +#include "display/shader/ShaderProgram.hpp" + +namespace Dawn { + class IShaderData { + private: + std::shared_ptr shader; + + protected: + virtual void writeData() = 0; + + virtual void beginStruct() = 0; + virtual void writeMat4(const glm::mat4 &mat) = 0; + virtual void writeVec3(const glm::vec3 &vec) = 0; + virtual void writeVec4(const glm::vec4 &vec) = 0; + virtual void writeColor(const struct Color &color) = 0; + virtual void writeBoolean(const bool &value) = 0; + virtual void writeTexture(std::shared_ptr &texture) = 0; + virtual void endStruct() = 0; + + public: + /** + * Writes the data to the shader. + * + * @param shader The shader to write to. + */ + void write(std::shared_ptr shader); + + /** + * Returns the shader that was written to. + * + * @return The shader that was written to. + */ + std::shared_ptr getShader(); + }; +} \ No newline at end of file diff --git a/src/dawnopengl/display/shader/CMakeLists.txt b/src/dawnopengl/display/shader/CMakeLists.txt index 753875ec..2fda0d69 100644 --- a/src/dawnopengl/display/shader/CMakeLists.txt +++ b/src/dawnopengl/display/shader/CMakeLists.txt @@ -8,4 +8,5 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE ShaderProgram.cpp ShaderStage.cpp + ShaderData.cpp ) \ No newline at end of file diff --git a/src/dawnopengl/display/shader/ShaderData.cpp b/src/dawnopengl/display/shader/ShaderData.cpp new file mode 100644 index 00000000..434e5b89 --- /dev/null +++ b/src/dawnopengl/display/shader/ShaderData.cpp @@ -0,0 +1,63 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "ShaderData.hpp" +#include "assert/assert.hpp" +#include "assert/assertgl.hpp" + +using namespace Dawn; + +void ShaderData::beginStruct() { + // Do nothing +} + +void ShaderData::writeMat4(const glm::mat4 &mat) { + char_t name[1024]; + GLsizei length; + GLint size; + GLenum type; + glGetActiveUniform(getShader()->id, i, sizeof(name), &length, &size, &type, name); + assertNoGLError(); + std::cout << "Uniform: " << name << std::endl; + + glUniformMatrix4fv(this->i, 1, GL_FALSE, glm::value_ptr(mat)); + assertNoGLError(); + this->i++; +} + +void ShaderData::writeVec3(const glm::vec3 &vec) { + assertUnreachable("Not implemented"); +} + +void ShaderData::writeVec4(const glm::vec4 &vec) { + assertUnreachable("Not implemented"); +} + +void ShaderData::writeColor(const struct Color &color) { + glUniform4fv(this->i, 1, &color.r); + assertNoGLError(); + this->i++; +} + +void ShaderData::writeBoolean(const bool &value) { + glUniform1i(this->i, value ? 1 : 0); + assertNoGLError(); + this->i++; +} + +void ShaderData::writeTexture(std::shared_ptr &texture) { + if(texture == nullptr) { + this->i++; + return; + } + texture->bind(0); + glUniform1i(this->i, 0); + assertNoGLError(); + this->i++; +} + +void ShaderData::endStruct() { + // Do nothing +} \ No newline at end of file diff --git a/src/dawnopengl/display/shader/ShaderData.hpp b/src/dawnopengl/display/shader/ShaderData.hpp new file mode 100644 index 00000000..035bd72b --- /dev/null +++ b/src/dawnopengl/display/shader/ShaderData.hpp @@ -0,0 +1,24 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "display/shader/IShaderData.hpp" + +namespace Dawn { + class ShaderData : public IShaderData { + private: + int32_t i = 0; + + protected: + void beginStruct() override; + void writeMat4(const glm::mat4 &mat) override; + void writeVec3(const glm::vec3 &vec) override; + void writeVec4(const glm::vec4 &vec) override; + void writeColor(const struct Color &color) override; + void writeBoolean(const bool &value) override; + void writeTexture(std::shared_ptr &texture) override; + void endStruct() override; + }; +} \ No newline at end of file diff --git a/src/dawnopengl/display/shader/ShaderProgram.cpp b/src/dawnopengl/display/shader/ShaderProgram.cpp index 9620fec8..e8bd2ec6 100644 --- a/src/dawnopengl/display/shader/ShaderProgram.cpp +++ b/src/dawnopengl/display/shader/ShaderProgram.cpp @@ -7,6 +7,7 @@ #include "assert/assert.hpp" #include "assert/assertgl.hpp" +#include "component/display/material/SimpleTexturedMaterial.hpp" using namespace Dawn; @@ -50,31 +51,43 @@ void ShaderProgram::init( // So the uniforms that are in slang are kinda odd when compiled. //DEBUGGING - GLint numUniforms = 0; - glGetProgramiv(this->id, GL_ACTIVE_UNIFORMS, &numUniforms); + // GLint numUniforms = 0; + // glGetProgramiv(this->id, GL_ACTIVE_UNIFORMS, &numUniforms); + // assertNoGLError(); + + // for(GLint i = 0; i < numUniforms; ++i) { + // char name[1024]; + // GLsizei length; + // GLint size; + // GLenum type; + + // glGetActiveUniform(this->id, i, sizeof(name), &length, &size, &type, name); + // assertNoGLError(); + // std::cout << "Uniform: " << i << ": " << name << " has size " << size << " and length " << length << std::endl; + // } + // GLint numUniformBlocks = 0; + // glGetProgramiv(this->id, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks); + + // for(GLint i = 0; i < numUniformBlocks; ++i) { + // GLint size; + // glGetActiveUniformBlockiv(this->id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &size); + // assertNoGLError(); + // std::cout << "Uniform Block: " << i << " has size " << size << std::endl; + // } + + glUseProgram(this->id); assertNoGLError(); - for(GLint i = 0; i < numUniforms; ++i) { - char name[1024]; - GLsizei length; - GLint size; - GLenum type; - - glGetActiveUniform(this->id, i, sizeof(name), &length, &size, &type, name); - assertNoGLError(); - std::cout << "Uniform: " << i << ": " << name << " has size " << size << " and length " << length << std::endl; - } - - - GLint numUniformBlocks = 0; - glGetProgramiv(this->id, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks); - - for(GLint i = 0; i < numUniformBlocks; ++i) { - GLint size; - glGetActiveUniformBlockiv(this->id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &size); - assertNoGLError(); - std::cout << "Uniform Block: " << i << " has size " << size << std::endl; - } + auto data = std::make_shared(); + data->color = COLOR_WHITE; + data->model = glm::mat4(1.0f); + data->projection = glm::perspective( + glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f + ); + data->view = glm::lookAt( + glm::vec3(4,3,3), glm::vec3(0,0,0), glm::vec3(0,1,0) + ); + data->write(shared_from_this()); } ShaderProgram::~ShaderProgram() { diff --git a/src/dawnopengl/display/shader/ShaderProgram.hpp b/src/dawnopengl/display/shader/ShaderProgram.hpp index 7ef33e46..2a15fc50 100644 --- a/src/dawnopengl/display/shader/ShaderProgram.hpp +++ b/src/dawnopengl/display/shader/ShaderProgram.hpp @@ -8,7 +8,12 @@ #include "dawnopengl.hpp" namespace Dawn { - class ShaderProgram : public IShaderProgram { + class ShaderData; + + class ShaderProgram : + public IShaderProgram, + public std::enable_shared_from_this + { private: GLuint id = -1; @@ -17,5 +22,7 @@ namespace Dawn { const std::vector> &stages ) override; ~ShaderProgram(); + + friend class ShaderData; }; } \ No newline at end of file