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 --- src/GalOgl.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 9 deletions(-) (limited to 'src/GalOgl.cpp') 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 { -- cgit v1.2.3