diff options
author | LaG1924 <lag1924@gmail.com> | 2021-11-27 15:03:36 +0100 |
---|---|---|
committer | LaG1924 <lag1924@gmail.com> | 2021-11-27 15:03:36 +0100 |
commit | 7f3ed11618df0cce5c3d799e0b3f4c009714f2c3 (patch) | |
tree | 8827c3ba45b52cdda6524df70ce58c8ca1c55e79 | |
parent | Added PostProcess class (diff) | |
download | AltCraft-7f3ed11618df0cce5c3d799e0b3f4c009714f2c3.tar AltCraft-7f3ed11618df0cce5c3d799e0b3f4c009714f2c3.tar.gz AltCraft-7f3ed11618df0cce5c3d799e0b3f4c009714f2c3.tar.bz2 AltCraft-7f3ed11618df0cce5c3d799e0b3f4c009714f2c3.tar.lz AltCraft-7f3ed11618df0cce5c3d799e0b3f4c009714f2c3.tar.xz AltCraft-7f3ed11618df0cce5c3d799e0b3f4c009714f2c3.tar.zst AltCraft-7f3ed11618df0cce5c3d799e0b3f4c009714f2c3.zip |
-rw-r--r-- | cwd/assets/altcraft/shaders/vert/face.vs | 5 | ||||
-rw-r--r-- | src/Gal.hpp | 40 | ||||
-rw-r--r-- | src/GalOgl.cpp | 65 | ||||
-rw-r--r-- | src/Render.cpp | 1 | ||||
-rw-r--r-- | src/RenderConfigs.hpp | 4 | ||||
-rw-r--r-- | 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<Framebuffer> GetDefaultFramebuffer() = 0; - virtual std::shared_ptr<ShaderParameters> GetGlobalShaderParameters() = 0; + virtual std::shared_ptr<ShaderParametersBuffer> GetGlobalShaderParameters() = 0; virtual std::shared_ptr<Shader> LoadVertexShader(std::string_view code) = 0; @@ -250,34 +250,22 @@ namespace Gal { virtual void SetTexture(size_t location, std::shared_ptr<Texture> texture) = 0; }; - struct ShaderParameters { - virtual ~ShaderParameters() = default; + struct ShaderParametersBuffer { + virtual ~ShaderParametersBuffer() = default; - virtual void AddGlobalShaderParameter(std::string_view name, Type type) = 0; + template<typename T> + T* Get() { + return reinterpret_cast<T*>(GetDataPtr()); + } - virtual void SetGlobalShaderParameter(std::string_view name, float value) = 0; + template<typename T> + 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<ImplOgl> impl; std::shared_ptr<FramebufferOgl> fbDefault; +std::shared_ptr<ShaderParametersBufferOgl> spbDefault; size_t GalTypeGetComponents(Gal::Type type) { switch (type) { @@ -731,6 +733,22 @@ struct FramebufferConfigOgl : public FramebufferConfig { } }; +struct ShaderParametersBufferOgl : public ShaderParametersBuffer { + std::shared_ptr<BufferOgl> buffer; + bool dirty = true; + std::vector<std::byte> 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<ShaderOgl> vertexShader, pixelShader; @@ -834,7 +852,7 @@ struct PipelineInstanceOgl : public PipelineInstance { }; struct PipelineOgl : public Pipeline { - + std::vector<std::shared_ptr<ShaderParametersBufferOgl>> spbs; std::map<std::string, size_t> shaderParameters; std::vector<std::shared_ptr<TextureOgl>> 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<std::byte>(spb->data)); + spb->dirty = false; + } + } } virtual void SetDynamicTexture(std::string_view name, std::shared_ptr<Texture> texture) override { @@ -1206,6 +1231,23 @@ struct ImplOgl : public Impl { /* + * 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<ShaderParametersBufferOgl, ShaderParametersBuffer>(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 */ for (auto&& [name, type] : config->shaderParameters) { @@ -1323,16 +1365,21 @@ struct ImplOgl : public Impl { } virtual std::shared_ptr<Framebuffer> GetDefaultFramebuffer() override { - if (!fbDefault) + if (!fbDefault) { fbDefault = std::make_shared<FramebufferOgl>(); - 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<Framebuffer, FramebufferOgl>(fbDefault); } - virtual std::shared_ptr<ShaderParameters> GetGlobalShaderParameters() override { - return nullptr; + virtual std::shared_ptr<ShaderParametersBuffer> GetGlobalShaderParameters() override { + if (!spbDefault) { + spbDefault = std::make_shared<ShaderParametersBufferOgl>(); + spbDefault->buffer = std::static_pointer_cast<BufferOgl, Buffer>(GetImplementation()->CreateBuffer()); + } + return spbDefault; } virtual std::shared_ptr<Shader> 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<GlobalShaderParameters>(); 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<Gal::Shader> LoadVertexShader(std::string_view assetPath); std::shared_ptr<Gal::Shader> 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<GlobalShaderParameters>()->projView = projView; + //Render Entities constexpr size_t entitiesVerticesCount = 240; entitiesPipeline->Activate(); |