diff options
author | LaG1924 <lag1924@gmail.com> | 2021-12-06 01:39:15 +0100 |
---|---|---|
committer | LaG1924 <lag1924@gmail.com> | 2021-12-07 12:37:47 +0100 |
commit | aa591c73c818d7f26caa15bc567929e2d404dcf2 (patch) | |
tree | 2514facee211ef5eb16812658caae91d1ee078d6 /src | |
parent | Minor shader optimization (diff) | |
download | AltCraft-aa591c73c818d7f26caa15bc567929e2d404dcf2.tar AltCraft-aa591c73c818d7f26caa15bc567929e2d404dcf2.tar.gz AltCraft-aa591c73c818d7f26caa15bc567929e2d404dcf2.tar.bz2 AltCraft-aa591c73c818d7f26caa15bc567929e2d404dcf2.tar.lz AltCraft-aa591c73c818d7f26caa15bc567929e2d404dcf2.tar.xz AltCraft-aa591c73c818d7f26caa15bc567929e2d404dcf2.tar.zst AltCraft-aa591c73c818d7f26caa15bc567929e2d404dcf2.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/Render.cpp | 83 | ||||
-rw-r--r-- | src/Render.hpp | 3 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 15 | ||||
-rw-r--r-- | src/RendererWorld.hpp | 4 |
4 files changed, 72 insertions, 33 deletions
diff --git a/src/Render.cpp b/src/Render.cpp index 1e85437..35ee695 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -140,28 +140,53 @@ void Render::PrepareToRendering() { float resolutionScale = Settings::ReadDouble("resolutionScale", 1.0f); size_t scaledW = width * resolutionScale, scaledH = height * resolutionScale; - gbuffer = std::make_unique<Gbuffer>(scaledW, scaledH, scaledW, scaledH, Settings::ReadBool("ssao", false)); - gbuffer->SetRenderBuff(renderBuff); - auto gal = Gal::GetImplementation(); gal->GetDefaultFramebuffer()->SetViewport(0, 0, width, height); - gal->GetGlobalShaderParameters()->Get<GlobalShaderParameters>()->gamma = Settings::ReadDouble("gamma", 2.2); - 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; - for (auto& vec : ssaoKernels) { - vec.x = dis(rng); - vec.y = dis(rng); - vec.z = (dis(rng) + 1.0f) / 2.0f; - vec.w = 0.0f; - vec = glm::normalize(vec); - } - for (size_t i = 0; i < sizeof(ssaoKernels) / sizeof(*ssaoKernels); i++) { - float scale = i / 64.0f; - scale = glm::mix(0.1f, 1.0f, scale * scale); - ssaoKernels[i] *= scale; + gbuffer.reset(); + fbTarget.reset(); + fbTextureColor.reset(); + fbTextureDepthStencil.reset(); + + bool useDeffered = Settings::ReadBool("deffered", false); + + if (useDeffered) { + gbuffer = std::make_unique<Gbuffer>(scaledW, scaledH, scaledW, scaledH, Settings::ReadBool("ssao", false)); + 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; + for (auto& vec : ssaoKernels) { + vec.x = dis(rng); + vec.y = dis(rng); + vec.z = (dis(rng) + 1.0f) / 2.0f; + vec.w = 0.0f; + vec = glm::normalize(vec); + } + for (size_t i = 0; i < sizeof(ssaoKernels) / sizeof(*ssaoKernels); i++) { + float scale = i / 64.0f; + 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; @@ -189,19 +214,19 @@ void Render::PrepareToRendering() { fbPPC->SetTarget(gal->GetDefaultFramebuffer()); fbPPC->SetVertexShader(gal->LoadVertexShader(vertexSource)); fbPPC->SetPixelShader(gal->LoadPixelShader(pixelSource)); - fbPPC->AddStaticTexture("inputTexture", gbuffer->GetFinalTexture()); + 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(gbuffer->GetGeometryTarget()); + world->PrepareRender(useDeffered ? gbuffer->GetGeometryTarget() : fbTarget, useDeffered); } void Render::UpdateKeyboard() { @@ -226,7 +251,10 @@ void Render::RenderFrame() { OPTICK_EVENT(); Gal::GetImplementation()->GetDefaultFramebuffer()->Clear(); - gbuffer->Clear(); + if (gbuffer) + gbuffer->Clear(); + if (fbTarget) + fbTarget->Clear(); if (isWireframe) Gal::GetImplementation()->SetWireframe(true); @@ -236,7 +264,8 @@ void Render::RenderFrame() { if (isWireframe) Gal::GetImplementation()->SetWireframe(false); - gbuffer->Render(); + if (gbuffer) + gbuffer->Render(); fbPipeline->Activate(); fbPipelineInstance->Activate(); @@ -368,7 +397,8 @@ void Render::HandleEvents() { if (renderBuff > gbuffer->GetMaxRenderBuffers()) renderBuff = 0; } - gbuffer->SetRenderBuff(renderBuff); + if (gbuffer) + gbuffer->SetRenderBuff(renderBuff); break; } @@ -512,7 +542,10 @@ void Render::InitEvents() { listener.RegisterHandler("PlayerConnected", [this](const Event&) { stateString = "Loading terrain..."; - world = std::make_unique<RendererWorld>(gbuffer->GetGeometryTarget()); + world = std::make_unique<RendererWorld>( + Settings::ReadBool("deffered", false) ? gbuffer->GetGeometryTarget() : 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 8bdcd63..944a4cb 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -39,6 +39,9 @@ class Render { 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::shared_ptr<Gal::Texture> fbTextureColor; + std::shared_ptr<Gal::Texture> fbTextureDepthStencil; std::unique_ptr<Gbuffer> gbuffer; EventListener listener; std::string stateString; diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 2108ba6..b5120c2 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -152,7 +152,7 @@ void RendererWorld::UpdateAllSections(VectorF playerPos) { } } -RendererWorld::RendererWorld(std::shared_ptr<Gal::Framebuffer> target) { +RendererWorld::RendererWorld(std::shared_ptr<Gal::Framebuffer> target, bool defferedShading) { OPTICK_EVENT(); MaxRenderingDistance = 2; numOfWorkers = _max(1, (signed int) std::thread::hardware_concurrency() - 2); @@ -161,7 +161,7 @@ RendererWorld::RendererWorld(std::shared_ptr<Gal::Framebuffer> target) { globalTimeStart = std::chrono::high_resolution_clock::now(); - PrepareRender(target); + PrepareRender(target, defferedShading); listener->RegisterHandler("DeleteSectionRender", [this](const Event& eventData) { OPTICK_EVENT("EV_DeleteSectionRender"); @@ -407,13 +407,14 @@ void RendererWorld::Render(float screenRatio) { DebugInfo::renderFaces = renderedFaces; } -void RendererWorld::PrepareRender(std::shared_ptr<Gal::Framebuffer> target) { +void RendererWorld::PrepareRender(std::shared_ptr<Gal::Framebuffer> target, bool defferedShading) { std::string sectionVertexSource, sectionPixelSource; { auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/face"); sectionVertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); - auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/face"); + auto pixelAsset = defferedShading ? AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/face") : + AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/fwd_face"); sectionPixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); } @@ -422,7 +423,8 @@ void RendererWorld::PrepareRender(std::shared_ptr<Gal::Framebuffer> target) { auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/entity"); entitiesVertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); - auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/entity"); + auto pixelAsset = defferedShading ? AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/entity") : + AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/fwd_entity"); entitiesPixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); } @@ -431,7 +433,8 @@ void RendererWorld::PrepareRender(std::shared_ptr<Gal::Framebuffer> target) { auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/sky"); skyVertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); - auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/sky"); + auto pixelAsset = defferedShading ? AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/sky") : + AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/fwd_sky"); skyPixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); } diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index a6fca06..184323d 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -55,11 +55,11 @@ class RendererWorld { std::shared_ptr<Gal::PipelineInstance> skyPipelineInstance; std::shared_ptr<Gal::Buffer> skyBuffer; public: - RendererWorld(std::shared_ptr<Gal::Framebuffer> target); + RendererWorld(std::shared_ptr<Gal::Framebuffer> target, bool defferedShading); ~RendererWorld(); void Render(float screenRatio); - void PrepareRender(std::shared_ptr<Gal::Framebuffer> target); + void PrepareRender(std::shared_ptr<Gal::Framebuffer> target, bool defferedShading); double MaxRenderingDistance; |