diff options
-rw-r--r-- | cwd/assets/altcraft/shaders/frag/copy.fs (renamed from cwd/assets/altcraft/shaders/frag/fbo.fs) | 0 | ||||
-rw-r--r-- | cwd/assets/altcraft/shaders/vert/fbo.vs | 22 | ||||
-rw-r--r-- | cwd/assets/altcraft/shaders/vert/quad.vs (renamed from cwd/assets/altcraft/shaders/vert/pp.vs) | 0 | ||||
-rw-r--r-- | src/Gal.hpp | 2 | ||||
-rw-r--r-- | src/GalOgl.cpp | 4 | ||||
-rw-r--r-- | src/Render.cpp | 121 | ||||
-rw-r--r-- | src/Render.hpp | 8 | ||||
-rw-r--r-- | src/RenderConfigs.cpp | 93 | ||||
-rw-r--r-- | src/RenderConfigs.hpp | 28 |
9 files changed, 183 insertions, 95 deletions
diff --git a/cwd/assets/altcraft/shaders/frag/fbo.fs b/cwd/assets/altcraft/shaders/frag/copy.fs index a5a7a2b..a5a7a2b 100644 --- a/cwd/assets/altcraft/shaders/frag/fbo.fs +++ b/cwd/assets/altcraft/shaders/frag/copy.fs 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/quad.vs index 118ea5b..118ea5b 100644 --- a/cwd/assets/altcraft/shaders/vert/pp.vs +++ b/cwd/assets/altcraft/shaders/vert/quad.vs 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<size_t, size_t, size_t> GetSize() = 0; + virtual void SetData(std::vector<std::byte>&& 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<std::byte> &&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<size_t, size_t, size_t> GetSize() override { + return { width, height, depth }; + } + virtual void SetData(std::vector<std::byte>&& 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<GlobalShaderParameters>()->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<Gbuffer>(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<float> dis(-1.0f, 1.0f); auto& ssaoKernels = gal->GetGlobalShaderParameters()->Get<GlobalShaderParameters>()->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<Gbuffer>(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<TextureFbCopy>(gbuffer->GetFinalTexture(), fbTextureColor); + fbTextureCopy = std::make_unique<TextureFbCopy>(fbTextureColor, gal->GetDefaultFramebuffer()); + } else { + fbTextureCopy = std::make_unique<TextureFbCopy>(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<TextureFbCopy>(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<const std::byte*>(quadVertices), reinterpret_cast<const std::byte*>(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<RendererWorld>( - Settings::ReadBool("deffered", false) ? gbuffer->GetGeometryTarget() : fbTarget, - Settings::ReadBool("deffered", false) - ); + world = std::make_unique<RendererWorld>(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<Gal::Pipeline> fbPipeline; - std::shared_ptr<Gal::PipelineInstance> fbPipelineInstance; - std::shared_ptr<Gal::Buffer> fbBuffer; - std::shared_ptr<Gal::Framebuffer> fbTarget; + std::unique_ptr<TextureFbCopy> resizeTextureCopy; + std::unique_ptr<TextureFbCopy> fbTextureCopy; std::shared_ptr<Gal::Texture> fbTextureColor; std::shared_ptr<Gal::Texture> fbTextureDepthStencil; + std::shared_ptr<Gal::Framebuffer> fbTarget; std::unique_ptr<Gbuffer> 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<Gal::Shader> LoadPixelShader(std::string_view assetPath) { return gal->LoadPixelShader(LoadShaderCode(assetPath)); } +TextureFbCopy::TextureFbCopy( + std::shared_ptr<Gal::Texture> inputTexture, + std::shared_ptr<Gal::Texture> outputTexture, + std::shared_ptr<Gal::Shader> 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<const std::byte*>(quadVertices); + fbBuffer->SetData({ quadPtr, quadPtr + sizeof(quadVertices) }); + + pipelineInstance = pipeline->CreateInstance({ + {fbBufferBB, fbBuffer} + }); +} + +TextureFbCopy::TextureFbCopy( + std::shared_ptr<Gal::Texture> inputTexture, + std::shared_ptr<Gal::Framebuffer> outputFb, + std::shared_ptr<Gal::Shader> 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<const std::byte*>(quadVertices); + fbBuffer->SetData({ quadPtr, quadPtr + sizeof(quadVertices) }); + + pipelineInstance = pipeline->CreateInstance({ + {fbBufferBB, fbBuffer} + }); +} + PostProcess::PostProcess( std::shared_ptr<Gal::Shader> pixelShader, std::vector<std::pair<std::string_view, std::shared_ptr<Gal::Texture>>> 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<Gal::Shader> LoadVertexShader(std::string_view assetPath); std::shared_ptr<Gal::Shader> LoadPixelShader(std::string_view assetPath); +class TextureFbCopy { + std::shared_ptr<Gal::Framebuffer> framebuffer; + std::shared_ptr<Gal::Buffer> fbBuffer; + std::shared_ptr<Gal::Pipeline> pipeline; + std::shared_ptr<Gal::PipelineInstance> pipelineInstance; +public: + + TextureFbCopy( + std::shared_ptr<Gal::Texture> inputTexture, + std::shared_ptr<Gal::Texture> outputTexture, + std::shared_ptr<Gal::Shader> copyShader = nullptr); + + TextureFbCopy( + std::shared_ptr<Gal::Texture> inputTexture, + std::shared_ptr<Gal::Framebuffer> outputFb, + std::shared_ptr<Gal::Shader> copyShader = nullptr); + + void Clear() { + framebuffer->Clear(); + } + + void Copy() { + pipeline->Activate(); + pipelineInstance->Activate(); + pipelineInstance->Render(0, 6); + } +}; + class PostProcess { std::shared_ptr<Gal::Framebuffer> framebuffer; std::shared_ptr<Gal::Buffer> fbBuffer; |