Added shader param buffer support
This commit is contained in:
		
							
								
								
									
										99
									
								
								src/dawnopengl/display/shader/ShaderParameterBuffer.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								src/dawnopengl/display/shader/ShaderParameterBuffer.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,99 @@ | ||||
| // Copyright (c) 2023 Dominic Masters | ||||
| //  | ||||
| // This software is released under the MIT License. | ||||
| // https://opensource.org/licenses/MIT | ||||
|  | ||||
| #pragma once | ||||
| #include "assert/assert.hpp" | ||||
| #include "dawnopengl.hpp" | ||||
| #include "display/shader/_ShaderParameterBuffer.hpp" | ||||
|  | ||||
| namespace Dawn { | ||||
|   typedef GLuint shaderbufferslot_t; | ||||
|   typedef GLuint shaderbufferlocation_t; | ||||
|    | ||||
|   template<typename T> | ||||
|   class ShaderParameterBuffer : public IShaderParameterBuffer<T, shaderbufferslot_t> { | ||||
|     protected: | ||||
|       shaderbufferlocation_t id = -1; | ||||
|       size_t size; | ||||
|  | ||||
|     public: | ||||
|       void init() { | ||||
|         assertTrue(this->id == -1); | ||||
|         this->size = sizeof(T); | ||||
|  | ||||
|         glGenBuffers(1, &this->id); | ||||
|  | ||||
|         glBindBuffer(GL_UNIFORM_BUFFER, this->id); | ||||
|         glBufferData(GL_UNIFORM_BUFFER, this->size, NULL, GL_DYNAMIC_DRAW); | ||||
|       } | ||||
|  | ||||
|       void buffer(T *data) override { | ||||
|         this->bufferRaw((void*)data); | ||||
|       } | ||||
|  | ||||
|       void bind(shaderbufferslot_t location) override { | ||||
|         glBindBuffer(GL_UNIFORM_BUFFER, this->id); | ||||
|         glBindBufferBase(GL_UNIFORM_BUFFER, location, this->id);  | ||||
|       } | ||||
|  | ||||
|       /** | ||||
|        * Buffers the entire contents of the data struct to this shader param | ||||
|        * buffer object. | ||||
|        *  | ||||
|        * @param data Raw data to buffer. | ||||
|        */ | ||||
|       void bufferRaw(void *data) { | ||||
|         this->bufferRaw(data, 0, this->size); | ||||
|       } | ||||
|  | ||||
|       /** | ||||
|        * Buffers a specific range of data to this shader param buffer object. | ||||
|        *  | ||||
|        * @param data Raw data to buffer. | ||||
|        * @param start Start position of the data to buffer. | ||||
|        * @param length Length of the data to buffer. | ||||
|        */ | ||||
|       void bufferRaw(void *data, size_t start, size_t length) { | ||||
|         glBindBuffer(GL_UNIFORM_BUFFER, this->id); | ||||
|         glBufferSubData(GL_UNIFORM_BUFFER, start, length, (void*)((size_t)data + start)); | ||||
|       } | ||||
|        | ||||
|       /** | ||||
|        * Buffers a sub-range of data to this shader param buffer object. | ||||
|        *  | ||||
|        * @param data Raw data to buffer. | ||||
|        * @param sub Pointer to the start of the sub-range. | ||||
|        */ | ||||
|       template<typename D> | ||||
|       void buffer(T* data, D* sub) { | ||||
|         size_t start = (size_t)sub - (size_t)data; | ||||
|         this->bufferRaw((void*)data, start, sizeof(D)); | ||||
|       } | ||||
|  | ||||
|       /** | ||||
|        * Buffers a sub-range of data to this shader param buffer object. | ||||
|        *  | ||||
|        * @param data Raw data to buffer. | ||||
|        * @param subStart Pointer to the start of the sub-range. | ||||
|        * @param subEnd Pointer to the end of the sub-range, inclusive. | ||||
|        */ | ||||
|       template<typename D0, typename D1> | ||||
|       void buffer(T *data, D0 *subStart, D1 *subEnd) { | ||||
|         this->bufferRaw( | ||||
|           (void*)data, | ||||
|           (size_t)subStart - (size_t)data, | ||||
|           ((size_t)subEnd - (size_t)subStart) + sizeof(D1) | ||||
|         ); | ||||
|       } | ||||
|  | ||||
|       /** | ||||
|        * Destroys this shader parameter buffer. | ||||
|        */ | ||||
|       ~ShaderParameterBuffer() { | ||||
|         glDeleteBuffers(1, &this->id); | ||||
|         this->id = -1; | ||||
|       } | ||||
|   }; | ||||
| } | ||||
| @@ -88,6 +88,14 @@ shaderparameter_t ShaderProgram::getParameterByName(std::string name) { | ||||
|   return glGetUniformLocation(this->shaderProgram, name.c_str()); | ||||
| } | ||||
|  | ||||
| shaderbufferlocation_t ShaderProgram::getBufferLocationByName(std::string name) { | ||||
|   return glGetUniformBlockIndex(this->shaderProgram, name.c_str()); | ||||
| } | ||||
|  | ||||
| void ShaderProgram::setParameterBuffer(shaderbufferlocation_t location, shaderbufferslot_t slot) { | ||||
|   glUniformBlockBinding(this->shaderProgram, location, slot); | ||||
| } | ||||
|  | ||||
| void ShaderProgram::setMatrix(shaderparameter_t uniform, glm::mat4 matrix) { | ||||
|   glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(matrix)); | ||||
| } | ||||
|   | ||||
| @@ -9,6 +9,8 @@ | ||||
| #include "display/Color.hpp" | ||||
| #include "debug/debug.hpp" | ||||
|  | ||||
| #include "ShaderParameterBuffer.hpp" | ||||
|  | ||||
| typedef GLuint shaderparameter_t; | ||||
|  | ||||
| namespace Dawn { | ||||
| @@ -56,6 +58,14 @@ namespace Dawn { | ||||
|        */ | ||||
|       shaderparameter_t getParameterByName(std::string name); | ||||
|  | ||||
|       /** | ||||
|        * Locate a shader buffer parameter set by its name. | ||||
|        *  | ||||
|        * @param name Name of the buffer to get. | ||||
|        * @return The shader buffer. | ||||
|        */ | ||||
|       shaderbufferlocation_t getBufferLocationByName(std::string name); | ||||
|  | ||||
|       /** | ||||
|        * Method to request that this shader be compiled and put on the GPU. This | ||||
|        * method should call the protected compileShader method. | ||||
| @@ -63,6 +73,7 @@ namespace Dawn { | ||||
|       virtual void compile() = 0; | ||||
|  | ||||
|       void bind() override; | ||||
|       void setParameterBuffer(shaderbufferlocation_t location, shaderbufferslot_t slot); | ||||
|       void setMatrix(shaderparameter_t parameter, glm::mat4 matrix) override; | ||||
|       void setBoolean(shaderparameter_t parameter, bool_t value) override; | ||||
|       void setColor(shaderparameter_t parameter, struct Color color) override; | ||||
|   | ||||
| @@ -33,6 +33,13 @@ void SimpleTexturedShaderProgram::compile() { | ||||
|  | ||||
|       // Fragment Shader | ||||
|       "#version 330 core\n" | ||||
|  | ||||
|       "layout (std140) uniform uniTest {\n" | ||||
|         "float r;\n" | ||||
|         "float g;\n" | ||||
|         "float b;\n" | ||||
|       "};\n" | ||||
|  | ||||
|       "in vec2 o_TextCoord;\n" | ||||
|       "out vec4 o_Color;\n" | ||||
|       "uniform vec4 u_Color;\n" | ||||
| @@ -40,11 +47,12 @@ void SimpleTexturedShaderProgram::compile() { | ||||
|       "uniform sampler2D u_Text;\n" | ||||
|  | ||||
|       "void main() {\n" | ||||
|         "if(u_HasTexture) {\n" | ||||
|           "o_Color = texture(u_Text, o_TextCoord) * u_Color;\n" | ||||
|         "} else {\n" | ||||
|           "o_Color = u_Color;" | ||||
|         "}\n" | ||||
|         // "if(u_HasTexture) {\n" | ||||
|         //   "o_Color = texture(u_Text, o_TextCoord) * u_Color;\n" | ||||
|         // "} else {\n" | ||||
|         //   "o_Color = u_Color;" | ||||
|         // "}\n" | ||||
|         "o_Color = vec4(r, g, b, 1);\n" | ||||
|       "}\n" | ||||
|     ); | ||||
|   #elif DAWN_OPENGL_HLSL | ||||
| @@ -94,6 +102,29 @@ void SimpleTexturedShaderProgram::compile() { | ||||
|   this->paramColor = this->getParameterByName("u_Color"); | ||||
|   this->paramTexture = this->getParameterByName("u_Text"); | ||||
|   this->paramHasTexture = this->getParameterByName("u_HasTexture"); | ||||
|  | ||||
|  | ||||
|   this->test.init(); | ||||
|   this->test.bind(0); | ||||
|   auto bufferLoc = this->getBufferLocationByName("uniTest"); | ||||
|   this->setParameterBuffer(bufferLoc, 0); | ||||
|  | ||||
|   struct Test123 data; | ||||
|   data.r = 1.0f; | ||||
|   data.g = 0.0f; | ||||
|   data.b = 1.0f; | ||||
|   this->test.buffer(&data); | ||||
|  | ||||
|   data.g = 1.0f; | ||||
|   this->test.buffer(&data); | ||||
|  | ||||
|   data.g = 0.0f; | ||||
|   data.b = 0.0f; | ||||
|   this->test.buffer(&data, &data.g); | ||||
|  | ||||
|   data.g = 1.0f; | ||||
|   data.b = 0.0f; | ||||
|   this->test.buffer(&data, &data.g, &data.b); | ||||
| } | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -6,10 +6,22 @@ | ||||
| #pragma once | ||||
| #include "display/shader/ShaderManager.hpp" | ||||
| #include "scene/components/display/material/SimpleTexturedMaterial.hpp" | ||||
| #include "display/shader/ShaderParameterBuffer.hpp" | ||||
|  | ||||
| namespace Dawn { | ||||
|   struct Test123 { | ||||
|     float_t r; | ||||
|     float_t g; | ||||
|     float_t b; | ||||
|   }; | ||||
|  | ||||
|   class SimpleTexturedShaderTest : public ShaderParameterBuffer<struct Test123> { | ||||
|   }; | ||||
|    | ||||
|   class SimpleTexturedShaderProgram : public ShaderProgram { | ||||
|     public: | ||||
|       SimpleTexturedShaderTest test; | ||||
|  | ||||
|       shaderparameter_t paramProjection; | ||||
|       shaderparameter_t paramView; | ||||
|       shaderparameter_t paramModel; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user