From a12779bc153425407b131bce541c0bb97cccca39 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Wed, 8 Dec 2021 20:33:09 +0500 Subject: Removed unnecessary framebuffers copying --- cwd/assets/altcraft/shaders/frag/copy.fs | 11 +++ cwd/assets/altcraft/shaders/frag/fbo.fs | 11 --- cwd/assets/altcraft/shaders/vert/fbo.vs | 22 ------ cwd/assets/altcraft/shaders/vert/pp.vs | 22 ------ cwd/assets/altcraft/shaders/vert/quad.vs | 22 ++++++ src/Gal.hpp | 2 + src/GalOgl.cpp | 4 + src/Render.cpp | 121 ++++++++++++++----------------- src/Render.hpp | 8 +- src/RenderConfigs.cpp | 93 +++++++++++++++++++++++- src/RenderConfigs.hpp | 28 +++++++ 11 files changed, 216 insertions(+), 128 deletions(-) create mode 100644 cwd/assets/altcraft/shaders/frag/copy.fs delete mode 100644 cwd/assets/altcraft/shaders/frag/fbo.fs delete mode 100644 cwd/assets/altcraft/shaders/vert/fbo.vs delete mode 100644 cwd/assets/altcraft/shaders/vert/pp.vs create mode 100644 cwd/assets/altcraft/shaders/vert/quad.vs diff --git a/cwd/assets/altcraft/shaders/frag/copy.fs b/cwd/assets/altcraft/shaders/frag/copy.fs new file mode 100644 index 0000000..a5a7a2b --- /dev/null +++ b/cwd/assets/altcraft/shaders/frag/copy.fs @@ -0,0 +1,11 @@ +#version 330 core + +in vec2 uv; + +out vec4 fragColor; + +uniform sampler2D inputTexture; + +void main() { + fragColor = texture(inputTexture, uv); +} diff --git a/cwd/assets/altcraft/shaders/frag/fbo.fs b/cwd/assets/altcraft/shaders/frag/fbo.fs deleted file mode 100644 index a5a7a2b..0000000 --- a/cwd/assets/altcraft/shaders/frag/fbo.fs +++ /dev/null @@ -1,11 +0,0 @@ -#version 330 core - -in vec2 uv; - -out vec4 fragColor; - -uniform sampler2D inputTexture; - -void main() { - fragColor = texture(inputTexture, uv); -} diff --git a/cwd/assets/altcraft/shaders/vert/fbo.vs b/cwd/assets/altcraft/shaders/vert/fbo.vs deleted file mode 100644 index 0e05eaf..0000000 --- a/cwd/assets/altcraft/shaders/vert/fbo.vs +++ /dev/null @@ -1,22 +0,0 @@ -#version 330 core - -in vec2 pos; -in vec2 uvPos; - -out vec2 uv; - -layout (std140) uniform Globals { - mat4 projView; - mat4 proj; - mat4 view; - uvec2 viewportSize; - vec4 ssaoKernels[64]; - float globalTime; - float dayTime; - float gamma; -}; - -void main() { - gl_Position = vec4(pos.x, pos.y, 0.0, 1.0); - uv = uvPos; -} diff --git a/cwd/assets/altcraft/shaders/vert/pp.vs b/cwd/assets/altcraft/shaders/vert/pp.vs deleted file mode 100644 index 118ea5b..0000000 --- a/cwd/assets/altcraft/shaders/vert/pp.vs +++ /dev/null @@ -1,22 +0,0 @@ -#version 330 core - -in vec2 pos; -in vec2 uvPos; - -out vec2 uv; - -layout (std140) uniform Globals { - mat4 projView; - mat4 proj; - mat4 view; - uvec2 viewportSize; - vec4 ssaoKernels[64]; - float globalTime; - float dayTime; - float gamma; -}; - -void main() { - gl_Position = vec4(pos.x, pos.y, 0.0, 1.0); - uv = uvPos; -} diff --git a/cwd/assets/altcraft/shaders/vert/quad.vs b/cwd/assets/altcraft/shaders/vert/quad.vs new file mode 100644 index 0000000..118ea5b --- /dev/null +++ b/cwd/assets/altcraft/shaders/vert/quad.vs @@ -0,0 +1,22 @@ +#version 330 core + +in vec2 pos; +in vec2 uvPos; + +out vec2 uv; + +layout (std140) uniform Globals { + mat4 projView; + mat4 proj; + mat4 view; + uvec2 viewportSize; + vec4 ssaoKernels[64]; + float globalTime; + float dayTime; + float gamma; +}; + +void main() { + gl_Position = vec4(pos.x, pos.y, 0.0, 1.0); + uv = uvPos; +} diff --git a/src/Gal.hpp b/src/Gal.hpp index fe99dc7..0aa61c7 100644 --- a/src/Gal.hpp +++ b/src/Gal.hpp @@ -171,6 +171,8 @@ namespace Gal { struct Texture { virtual ~Texture() = default; + virtual std::tuple GetSize() = 0; + virtual void SetData(std::vector&& data, size_t mipLevel = 0) = 0; virtual void SetSubData(size_t x, size_t y, size_t z, size_t width, size_t height, size_t depth, std::vector &&data, size_t mipLevel = 0) = 0; diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index 6452a44..94c2146 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -683,6 +683,10 @@ struct TextureOgl : public Texture { size_t width, height, depth; bool linear; + virtual std::tuple GetSize() override { + return { width, height, depth }; + } + virtual void SetData(std::vector&& data, size_t mipLevel = 0) override { size_t expectedSize = width * height * depth * GalFormatGetSize(format); if (data.size() != expectedSize && !data.empty()) diff --git a/src/Render.cpp b/src/Render.cpp index be7e2b2..304f25f 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -145,20 +145,16 @@ void Render::PrepareToRendering() { gal->GetGlobalShaderParameters()->Get()->gamma = Settings::ReadDouble("gamma", 2.2); gbuffer.reset(); - fbTarget.reset(); + resizeTextureCopy.reset(); + fbTextureCopy.reset(); fbTextureColor.reset(); fbTextureDepthStencil.reset(); + fbTarget.reset(); bool useDeffered = Settings::ReadBool("deffered", false); + bool useResize = scaledW != width; if (useDeffered) { - int ssaoSamples = Settings::ReadDouble("ssaoSamples", 0.5f); - float ssaoScale = Settings::ReadDouble("ssaoScale", 0.5f); - size_t ssaoW = scaledW * ssaoScale, ssaoH = scaledH * ssaoScale; - - gbuffer = std::make_unique(scaledW, scaledH, scaledW, scaledH, ssaoSamples, ssaoW, ssaoH); - gbuffer->SetRenderBuff(renderBuff); - std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); std::uniform_real_distribution dis(-1.0f, 1.0f); auto& ssaoKernels = gal->GetGlobalShaderParameters()->Get()->ssaoKernels; @@ -174,63 +170,51 @@ void Render::PrepareToRendering() { scale = glm::mix(0.1f, 1.0f, scale * scale); ssaoKernels[i] *= scale; } - } else { - auto fbTextureColorConf = gal->CreateTexture2DConfig(scaledW, scaledH, Gal::Format::R8G8B8); - fbTextureColorConf->SetMinFilter(Gal::Filtering::Bilinear); - fbTextureColorConf->SetMaxFilter(Gal::Filtering::Bilinear); - fbTextureColor = gal->BuildTexture(fbTextureColorConf); - - auto fbTextureDepthStencilConf = gal->CreateTexture2DConfig(scaledW, scaledH, Gal::Format::D24S8); - fbTextureDepthStencilConf->SetMinFilter(Gal::Filtering::Bilinear); - fbTextureDepthStencilConf->SetMaxFilter(Gal::Filtering::Bilinear); - fbTextureDepthStencil = gal->BuildTexture(fbTextureDepthStencilConf); - - auto fbTargetConf = gal->CreateFramebufferConfig(); - fbTargetConf->SetTexture(0, fbTextureColor); - fbTargetConf->SetDepthStencil(fbTextureDepthStencil); - - fbTarget = gal->BuildFramebuffer(fbTargetConf); - fbTarget->SetViewport(0, 0, scaledW, scaledH); - } - std::string vertexSource, pixelSource; - { - auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/fbo"); - vertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); + int ssaoSamples = Settings::ReadDouble("ssaoSamples", 0); + float ssaoScale = Settings::ReadDouble("ssaoScale", 0.5f); + size_t ssaoW = scaledW * ssaoScale, ssaoH = scaledH * ssaoScale; - auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/fbo"); - pixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); + gbuffer = std::make_unique(scaledW, scaledH, scaledW, scaledH, ssaoSamples, ssaoW, ssaoH); + gbuffer->SetRenderBuff(renderBuff); + fbTarget = gbuffer->GetGeometryTarget(); + if (useResize) { + auto fbTextureColorConf = gal->CreateTexture2DConfig(scaledW, scaledH, Gal::Format::R8G8B8); + fbTextureColorConf->SetMinFilter(Gal::Filtering::Bilinear); + fbTextureColorConf->SetMaxFilter(Gal::Filtering::Bilinear); + fbTextureColor = gal->BuildTexture(fbTextureColorConf); + + resizeTextureCopy = std::make_unique(gbuffer->GetFinalTexture(), fbTextureColor); + fbTextureCopy = std::make_unique(fbTextureColor, gal->GetDefaultFramebuffer()); + } else { + fbTextureCopy = std::make_unique(gbuffer->GetFinalTexture(), gal->GetDefaultFramebuffer()); + } + } else { + if (useResize) { + auto fbTextureColorConf = gal->CreateTexture2DConfig(scaledW, scaledH, Gal::Format::R8G8B8); + fbTextureColorConf->SetMinFilter(Gal::Filtering::Bilinear); + fbTextureColorConf->SetMaxFilter(Gal::Filtering::Bilinear); + fbTextureColor = gal->BuildTexture(fbTextureColorConf); + + auto fbTextureDepthStencilConf = gal->CreateTexture2DConfig(scaledW, scaledH, Gal::Format::D24S8); + fbTextureDepthStencilConf->SetMinFilter(Gal::Filtering::Bilinear); + fbTextureDepthStencilConf->SetMaxFilter(Gal::Filtering::Bilinear); + fbTextureDepthStencil = gal->BuildTexture(fbTextureDepthStencilConf); + + auto fbTargetConf = gal->CreateFramebufferConfig(); + fbTargetConf->SetTexture(0, fbTextureColor); + fbTargetConf->SetDepthStencil(fbTextureDepthStencil); + fbTarget = gal->BuildFramebuffer(fbTargetConf); + fbTarget->SetViewport(0, 0, scaledW, scaledH); + + fbTextureCopy = std::make_unique(fbTextureColor, gal->GetDefaultFramebuffer()); + } else { + fbTarget = gal->GetDefaultFramebuffer(); + } } - constexpr float quadVertices[] = { - // positions // texCoords - -1.0f, 1.0f, 0.0f, 1.0f, - -1.0f, -1.0f, 0.0f, 0.0f, - 1.0f, -1.0f, 1.0f, 0.0f, - - -1.0f, 1.0f, 0.0f, 1.0f, - 1.0f, -1.0f, 1.0f, 0.0f, - 1.0f, 1.0f, 1.0f, 1.0f - }; - fbBuffer = gal->CreateBuffer(); - fbBuffer->SetData({ reinterpret_cast(quadVertices), reinterpret_cast(quadVertices) + sizeof(quadVertices) }); - auto fbPPC = gal->CreatePipelineConfig(); - fbPPC->SetTarget(gal->GetDefaultFramebuffer()); - fbPPC->SetVertexShader(gal->LoadVertexShader(vertexSource)); - fbPPC->SetPixelShader(gal->LoadPixelShader(pixelSource)); - fbPPC->AddStaticTexture("inputTexture", useDeffered ? gbuffer->GetFinalTexture() : fbTextureColor); - auto fbColorBB = fbPPC->BindVertexBuffer({ - {"pos", Gal::Type::Vec2}, - {"uvPos", Gal::Type::Vec2} - }); - - fbPipeline = gal->BuildPipeline(fbPPC); - fbPipelineInstance = fbPipeline->CreateInstance({ - {fbColorBB, fbBuffer} - }); - if (world) - world->PrepareRender(useDeffered ? gbuffer->GetGeometryTarget() : fbTarget, useDeffered); + world->PrepareRender(fbTarget, useDeffered); } void Render::UpdateKeyboard() { @@ -259,6 +243,10 @@ void Render::RenderFrame() { gbuffer->Clear(); if (fbTarget) fbTarget->Clear(); + if (resizeTextureCopy) + resizeTextureCopy->Clear(); + if (fbTextureCopy) + fbTextureCopy->Clear(); if (isWireframe) Gal::GetImplementation()->SetWireframe(true); @@ -270,10 +258,10 @@ void Render::RenderFrame() { if (gbuffer) gbuffer->Render(); - - fbPipeline->Activate(); - fbPipelineInstance->Activate(); - fbPipelineInstance->Render(0, 6); + if (resizeTextureCopy) + resizeTextureCopy->Copy(); + if (fbTextureCopy) + fbTextureCopy->Copy(); RenderGui(); @@ -546,10 +534,7 @@ void Render::InitEvents() { listener.RegisterHandler("PlayerConnected", [this](const Event&) { stateString = "Loading terrain..."; - world = std::make_unique( - Settings::ReadBool("deffered", false) ? gbuffer->GetGeometryTarget() : fbTarget, - Settings::ReadBool("deffered", false) - ); + world = std::make_unique(fbTarget, Settings::ReadBool("deffered", false)); world->MaxRenderingDistance = Settings::ReadDouble("renderDistance", 2.0f); PUSH_EVENT("UpdateSectionsRender", 0); }); diff --git a/src/Render.hpp b/src/Render.hpp index 944a4cb..3ac76e3 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -12,6 +12,7 @@ #include "Gal.hpp" class Gbuffer; +class TextureFbCopy; class RendererWorld; class RmlRenderInterface; class RmlSystemInterface; @@ -36,12 +37,11 @@ class Render { bool HasFocus=true; float sensetivity = 0.1f; bool isWireframe = false; - std::shared_ptr fbPipeline; - std::shared_ptr fbPipelineInstance; - std::shared_ptr fbBuffer; - std::shared_ptr fbTarget; + std::unique_ptr resizeTextureCopy; + std::unique_ptr fbTextureCopy; std::shared_ptr fbTextureColor; std::shared_ptr fbTextureDepthStencil; + std::shared_ptr fbTarget; std::unique_ptr gbuffer; EventListener listener; std::string stateString; diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index 56c24bf..af1cde9 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -22,6 +22,96 @@ std::shared_ptr LoadPixelShader(std::string_view assetPath) { return gal->LoadPixelShader(LoadShaderCode(assetPath)); } +TextureFbCopy::TextureFbCopy( + std::shared_ptr inputTexture, + std::shared_ptr outputTexture, + std::shared_ptr copyShader) { + + auto gal = Gal::GetImplementation(); + + auto fbConf = gal->CreateFramebufferConfig(); + fbConf->SetTexture(0, outputTexture); + + auto [outputW, outputH, outputD] = outputTexture->GetSize(); + + framebuffer = gal->BuildFramebuffer(fbConf); + framebuffer->SetViewport(0, 0, outputW, outputH); + + auto fbPPC = gal->CreatePipelineConfig(); + fbPPC->SetTarget(framebuffer); + + fbPPC->AddStaticTexture("inputTexture", inputTexture); + + fbPPC->SetVertexShader(LoadVertexShader("/altcraft/shaders/vert/quad")); + fbPPC->SetPixelShader(copyShader ? copyShader : LoadPixelShader("/altcraft/shaders/frag/copy")); + auto fbBufferBB = fbPPC->BindVertexBuffer({ + {"pos", Gal::Type::Vec2}, + {"uvPos", Gal::Type::Vec2} + }); + + pipeline = gal->BuildPipeline(fbPPC); + + fbBuffer = gal->CreateBuffer(); + constexpr float quadVertices[] = { + // pos // uv + -1.0f, 1.0f, 0.0f, 1.0f, + -1.0f, -1.0f, 0.0f, 0.0f, + 1.0f, -1.0f, 1.0f, 0.0f, + + -1.0f, 1.0f, 0.0f, 1.0f, + 1.0f, -1.0f, 1.0f, 0.0f, + 1.0f, 1.0f, 1.0f, 1.0f + }; + auto quadPtr = reinterpret_cast(quadVertices); + fbBuffer->SetData({ quadPtr, quadPtr + sizeof(quadVertices) }); + + pipelineInstance = pipeline->CreateInstance({ + {fbBufferBB, fbBuffer} + }); +} + +TextureFbCopy::TextureFbCopy( + std::shared_ptr inputTexture, + std::shared_ptr outputFb, + std::shared_ptr copyShader) { + + auto gal = Gal::GetImplementation(); + + framebuffer = outputFb; + + auto fbPPC = gal->CreatePipelineConfig(); + fbPPC->SetTarget(framebuffer); + + fbPPC->AddStaticTexture("inputTexture", inputTexture); + + fbPPC->SetVertexShader(LoadVertexShader("/altcraft/shaders/vert/quad")); + fbPPC->SetPixelShader(copyShader ? copyShader : LoadPixelShader("/altcraft/shaders/frag/copy")); + auto fbBufferBB = fbPPC->BindVertexBuffer({ + {"pos", Gal::Type::Vec2}, + {"uvPos", Gal::Type::Vec2} + }); + + pipeline = gal->BuildPipeline(fbPPC); + + fbBuffer = gal->CreateBuffer(); + constexpr float quadVertices[] = { + // pos // uv + -1.0f, 1.0f, 0.0f, 1.0f, + -1.0f, -1.0f, 0.0f, 0.0f, + 1.0f, -1.0f, 1.0f, 0.0f, + + -1.0f, 1.0f, 0.0f, 1.0f, + 1.0f, -1.0f, 1.0f, 0.0f, + 1.0f, 1.0f, 1.0f, 1.0f + }; + auto quadPtr = reinterpret_cast(quadVertices); + fbBuffer->SetData({ quadPtr, quadPtr + sizeof(quadVertices) }); + + pipelineInstance = pipeline->CreateInstance({ + {fbBufferBB, fbBuffer} + }); +} + PostProcess::PostProcess( std::shared_ptr pixelShader, std::vector>> inputTextures, @@ -52,7 +142,7 @@ PostProcess::PostProcess( for (auto&& [name, type] : inputParameters) { fbPPC->AddShaderParameter(name, type); } - fbPPC->SetVertexShader(LoadVertexShader("/altcraft/shaders/vert/pp")); + fbPPC->SetVertexShader(LoadVertexShader("/altcraft/shaders/vert/quad")); fbPPC->SetPixelShader(pixelShader); auto fbBufferBB = fbPPC->BindVertexBuffer({ {"pos", Gal::Type::Vec2}, @@ -208,3 +298,4 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s lightingPass->SetShaderParameter("applySsao", ssaoSamples); } + diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index d76647e..8ca5a9e 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -22,6 +22,34 @@ std::shared_ptr LoadVertexShader(std::string_view assetPath); std::shared_ptr LoadPixelShader(std::string_view assetPath); +class TextureFbCopy { + std::shared_ptr framebuffer; + std::shared_ptr fbBuffer; + std::shared_ptr pipeline; + std::shared_ptr pipelineInstance; +public: + + TextureFbCopy( + std::shared_ptr inputTexture, + std::shared_ptr outputTexture, + std::shared_ptr copyShader = nullptr); + + TextureFbCopy( + std::shared_ptr inputTexture, + std::shared_ptr outputFb, + std::shared_ptr copyShader = nullptr); + + void Clear() { + framebuffer->Clear(); + } + + void Copy() { + pipeline->Activate(); + pipelineInstance->Activate(); + pipelineInstance->Render(0, 6); + } +}; + class PostProcess { std::shared_ptr framebuffer; std::shared_ptr fbBuffer; -- cgit v1.2.3