From 7f3ed11618df0cce5c3d799e0b3f4c009714f2c3 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sat, 27 Nov 2021 19:03:36 +0500 Subject: Added ShaderParametersBuffer to GalOgl --- cwd/assets/altcraft/shaders/vert/face.vs | 5 ++- src/Gal.hpp | 40 +++++++------------- src/GalOgl.cpp | 65 +++++++++++++++++++++++++++----- src/Render.cpp | 1 + src/RenderConfigs.hpp | 4 ++ src/RendererWorld.cpp | 3 ++ 6 files changed, 82 insertions(+), 36 deletions(-) diff --git a/cwd/assets/altcraft/shaders/vert/face.vs b/cwd/assets/altcraft/shaders/vert/face.vs index ac92831..d73e164 100644 --- a/cwd/assets/altcraft/shaders/vert/face.vs +++ b/cwd/assets/altcraft/shaders/vert/face.vs @@ -13,9 +13,12 @@ out vec3 faceNormal; out vec3 faceAddColor; out vec2 faceLight; -uniform mat4 projView; uniform float GlobalTime; +layout (std140) uniform Globals { + mat4 projView; +}; + void main() { gl_Position = projView * vec4(position[gl_VertexID], 1.0f); diff --git a/src/Gal.hpp b/src/Gal.hpp index 4757e37..234e49a 100644 --- a/src/Gal.hpp +++ b/src/Gal.hpp @@ -20,7 +20,7 @@ namespace Gal { struct PipelineInstance; struct FramebufferConfig; struct Framebuffer; - struct ShaderParameters; + struct ShaderParametersBuffer; struct Shader; @@ -131,7 +131,7 @@ namespace Gal { virtual std::shared_ptr GetDefaultFramebuffer() = 0; - virtual std::shared_ptr GetGlobalShaderParameters() = 0; + virtual std::shared_ptr GetGlobalShaderParameters() = 0; virtual std::shared_ptr LoadVertexShader(std::string_view code) = 0; @@ -250,34 +250,22 @@ namespace Gal { virtual void SetTexture(size_t location, std::shared_ptr texture) = 0; }; - struct ShaderParameters { - virtual ~ShaderParameters() = default; + struct ShaderParametersBuffer { + virtual ~ShaderParametersBuffer() = default; - virtual void AddGlobalShaderParameter(std::string_view name, Type type) = 0; + template + T* Get() { + return reinterpret_cast(GetDataPtr()); + } - virtual void SetGlobalShaderParameter(std::string_view name, float value) = 0; + template + void Resize() { + Resize(sizeof(T)); + } - virtual void SetGlobalShaderParameter(std::string_view name, double value) = 0; + virtual std::byte* GetDataPtr() = 0; - virtual void SetGlobalShaderParameter(std::string_view name, int8_t value) = 0; - - virtual void SetGlobalShaderParameter(std::string_view name, int16_t value) = 0; - - virtual void SetGlobalShaderParameter(std::string_view name, int32_t value) = 0; - - virtual void SetGlobalShaderParameter(std::string_view name, uint8_t value) = 0; - - virtual void SetGlobalShaderParameter(std::string_view name, uint16_t value) = 0; - - virtual void SetGlobalShaderParameter(std::string_view name, uint32_t value) = 0; - - virtual void SetGlobalShaderParameter(std::string_view name, glm::vec2 value) = 0; - - virtual void SetGlobalShaderParameter(std::string_view name, glm::vec3 value) = 0; - - virtual void SetGlobalShaderParameter(std::string_view name, glm::vec4 value) = 0; - - virtual void SetGlobalShaderParameter(std::string_view name, glm::mat4 value) = 0; + virtual void Resize(size_t newSize) = 0; }; struct Shader { diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index 75369ab..854ab40 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -74,9 +74,10 @@ public: using namespace Gal; -class ImplOgl; -class ShaderOgl; -class FramebufferOgl; +struct ImplOgl; +struct ShaderOgl; +struct FramebufferOgl; +struct ShaderParametersBufferOgl; class OglState { GLuint activeFbo = 0; @@ -165,6 +166,7 @@ public: std::unique_ptr impl; std::shared_ptr fbDefault; +std::shared_ptr spbDefault; size_t GalTypeGetComponents(Gal::Type type) { switch (type) { @@ -731,6 +733,22 @@ struct FramebufferConfigOgl : public FramebufferConfig { } }; +struct ShaderParametersBufferOgl : public ShaderParametersBuffer { + std::shared_ptr buffer; + bool dirty = true; + std::vector data; + + virtual std::byte* GetDataPtr() override { + dirty = true; + return data.data(); + } + + virtual void Resize(size_t newSize) override { + dirty = true; + data.resize(newSize); + } +}; + struct PipelineConfigOgl : public PipelineConfig { std::shared_ptr vertexShader, pixelShader; @@ -834,7 +852,7 @@ struct PipelineInstanceOgl : public PipelineInstance { }; struct PipelineOgl : public Pipeline { - + std::vector> spbs; std::map shaderParameters; std::vector> staticTextures; GlResource program; @@ -861,6 +879,13 @@ struct PipelineOgl : public Pipeline { for (size_t i = 0; i < staticTextures.size(); i++) { oglState.BindTexture(staticTextures[i]->type, staticTextures[i]->texture, i); } + + for (auto& spb : spbs) { + if (spb->dirty) { + spb->buffer->SetData(std::vector(spb->data)); + spb->dirty = false; + } + } } virtual void SetDynamicTexture(std::string_view name, std::shared_ptr texture) override { @@ -1205,6 +1230,23 @@ struct ImplOgl : public Impl { glCheckError(); + /* + * Shader parameters buffers + */ + constexpr auto spbGlobalsName = "Globals"; + size_t spbGlobalsBind = 0; + size_t spbIndex = glGetUniformBlockIndex(program, spbGlobalsName); + if (spbIndex != GL_INVALID_VALUE) { + glUniformBlockBinding(program, spbIndex, spbGlobalsBind); + auto spbGlobals = std::static_pointer_cast(GetGlobalShaderParameters()); + glBindBufferBase(GL_UNIFORM_BUFFER, spbGlobalsBind, spbGlobals->buffer->vbo); + pipeline->spbs.emplace_back(spbGlobals); + } + else + LOG(ERROR) << "Cannot bind Globals UBO to shader. Maybe uniform block Globals missing?"; + glCheckError(); + + /* * Shader parameters */ @@ -1323,16 +1365,21 @@ struct ImplOgl : public Impl { } virtual std::shared_ptr GetDefaultFramebuffer() override { - if (!fbDefault) + if (!fbDefault) { fbDefault = std::make_shared(); - fbDefault->fbo = GlResource(0, GlResourceType::None); - fbDefault->attachments.push_back(GL_COLOR_ATTACHMENT0); + fbDefault->fbo = GlResource(0, GlResourceType::None); + fbDefault->attachments.push_back(GL_COLOR_ATTACHMENT0); + } return std::static_pointer_cast(fbDefault); } - virtual std::shared_ptr GetGlobalShaderParameters() override { - return nullptr; + virtual std::shared_ptr GetGlobalShaderParameters() override { + if (!spbDefault) { + spbDefault = std::make_shared(); + spbDefault->buffer = std::static_pointer_cast(GetImplementation()->CreateBuffer()); + } + return spbDefault; } virtual std::shared_ptr LoadVertexShader(std::string_view code) override { diff --git a/src/Render.cpp b/src/Render.cpp index fadec80..8a5253c 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -123,6 +123,7 @@ void Render::InitSdl(unsigned int WinWidth, unsigned int WinHeight, std::string void Render::InitGlew() { auto gal = Gal::GetImplementation(); gal->Init(); + gal->GetGlobalShaderParameters()->Resize(); int width, height; SDL_GL_GetDrawableSize(window, &width, &height); diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index 793dbb2..a9d6c00 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -2,6 +2,10 @@ #include "Gal.hpp" +struct GlobalShaderParameters { + glm::mat4 projView; +}; + std::shared_ptr LoadVertexShader(std::string_view assetPath); std::shared_ptr LoadPixelShader(std::string_view assetPath); diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index d2c69fd..2434ed1 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -12,6 +12,7 @@ #include "Section.hpp" #include "RendererSectionData.hpp" #include "Game.hpp" +#include "RenderConfigs.hpp" void RendererWorld::WorkerFunction(size_t workerId) { OPTICK_THREAD("Worker"); @@ -289,6 +290,8 @@ void RendererWorld::Render(float screenRatio) { glm::mat4 view = GetGameState()->GetViewMatrix(); glm::mat4 projView = projection * view; + Gal::GetImplementation()->GetGlobalShaderParameters()->Get()->projView = projView; + //Render Entities constexpr size_t entitiesVerticesCount = 240; entitiesPipeline->Activate(); -- cgit v1.2.3