From e02d3dbd590810df98b08c17fd81e6ce28711af8 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sat, 20 Nov 2021 16:35:20 +0500 Subject: Fixed some broken graphics features --- src/Gal.hpp | 2 ++ src/GalOgl.cpp | 4 ++++ src/Render.cpp | 16 ++++++++-------- src/RendererWorld.cpp | 1 + 4 files changed, 15 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/Gal.hpp b/src/Gal.hpp index f448c89..4757e37 100644 --- a/src/Gal.hpp +++ b/src/Gal.hpp @@ -106,6 +106,8 @@ namespace Gal { virtual void SetScissor(bool enabled) = 0; + virtual void SetWireframe(bool enabled) = 0; + virtual std::shared_ptr CreateBuffer() = 0; diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index 052c68a..29e7fc0 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -1055,6 +1055,10 @@ struct ImplOgl : public Impl { glCheckError(); } + virtual void SetWireframe(bool enabled) override { + glPolygonMode(GL_FRONT_AND_BACK, enabled ? GL_LINE : GL_FILL); + } + virtual std::shared_ptr CreateBuffer() override { auto buff = std::make_shared(); diff --git a/src/Render.cpp b/src/Render.cpp index 6301ff5..02f56ff 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -142,13 +142,13 @@ void Render::PrepareToRendering() { auto dsTexConf = gal->CreateTexture2DConfig(scaledW, scaledH, Gal::Format::D24S8); - dsTexConf->SetMinFilter(Gal::Filtering::Nearest); - dsTexConf->SetMaxFilter(Gal::Filtering::Nearest); + dsTexConf->SetMinFilter(Gal::Filtering::Bilinear); + dsTexConf->SetMaxFilter(Gal::Filtering::Bilinear); fbDepthStencil = gal->BuildTexture(dsTexConf); auto texConf = gal->CreateTexture2DConfig(scaledW, scaledH, Gal::Format::R8G8B8A8); - texConf->SetMinFilter(Gal::Filtering::Nearest); - texConf->SetMaxFilter(Gal::Filtering::Nearest); + texConf->SetMinFilter(Gal::Filtering::Bilinear); + texConf->SetMaxFilter(Gal::Filtering::Bilinear); fbColor = gal->BuildTexture(texConf); auto fbConf = gal->CreateFramebufferConfig(); @@ -223,12 +223,12 @@ void Render::RenderFrame() { Gal::GetImplementation()->GetDefaultFramebuffer()->Clear(); framebuffer->Clear(); - //if (isWireframe) - //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + if (isWireframe) + Gal::GetImplementation()->SetWireframe(true); if (renderWorld) world->Render(static_cast(windowWidth) / static_cast(windowHeight)); - //if (isWireframe) - //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + if (isWireframe) + Gal::GetImplementation()->SetWireframe(false); fbPipeline->Activate(); fbPipelineInstance->Activate(); diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index cb4551f..561578f 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -381,6 +381,7 @@ void RendererWorld::Render(float screenRatio) { sectionsPipeline->Activate(); sectionsPipeline->SetShaderParameter("DayTime", mixLevel); sectionsPipeline->SetShaderParameter("projView", projView); + sectionsPipeline->SetShaderParameter("GlobalTime", globalTime); Frustum frustum(projView); -- cgit v1.2.3 From aafc423a7298b0fc595c0209480474dc50c277f0 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 21 Nov 2021 13:56:28 +0500 Subject: Added Gbuffer --- src/GalOgl.cpp | 7 +++++ src/Render.cpp | 34 +++++++---------------- src/Render.hpp | 5 ++-- src/RenderConfigs.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/RenderConfigs.hpp | 38 ++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 27 deletions(-) create mode 100644 src/RenderConfigs.cpp create mode 100644 src/RenderConfigs.hpp (limited to 'src') diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index 29e7fc0..c250539 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -137,6 +137,7 @@ public: if (activeTexture[textureUnit] != texture) { SetTextureUnit(textureUnit); glBindTexture(type, texture); + activeTexture[textureUnit] = texture; } glCheckError(); } @@ -690,6 +691,7 @@ struct FramebufferOgl : public Framebuffer { size_t vpX = 0, vpY = 0, vpW = 1, vpH = 1; std::shared_ptr depthStencil; std::vector> colors; + std::vector attachments; GlResource fbo; @@ -853,6 +855,8 @@ struct PipelineOgl : public Pipeline { oglState.UseProgram(program); oglState.BindFbo(target->fbo); oglState.SetViewport(target->vpX, target->vpY, target->vpW, target->vpH); + if (target->fbo) + glDrawBuffers(target->attachments.size(), target->attachments.data()); for (size_t i = 0; i < staticTextures.size(); i++) { oglState.BindTexture(staticTextures[i]->type, staticTextures[i]->texture, i); @@ -1226,6 +1230,7 @@ struct ImplOgl : public Impl { glUniform1i(location, usedTextureBlocks); pipeline->staticTextures.push_back(texture); + usedTextureBlocks++; } glCheckError(); @@ -1299,6 +1304,7 @@ struct ImplOgl : public Impl { for (auto&& [location, texture] : conf->colors) { glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + location, texture->type, texture->texture, 0); fb->colors.emplace_back(std::move(texture)); + fb->attachments.push_back(GL_COLOR_ATTACHMENT0 + location); } if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { @@ -1315,6 +1321,7 @@ struct ImplOgl : public Impl { if (!fbDefault) fbDefault = std::make_shared(); fbDefault->fbo = GlResource(0, GlResourceType::None); + fbDefault->attachments.push_back(GL_COLOR_ATTACHMENT0); return std::static_pointer_cast(fbDefault); } diff --git a/src/Render.cpp b/src/Render.cpp index 02f56ff..96c1ed8 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -17,6 +17,7 @@ #include "Plugin.hpp" #include "Rml.hpp" #include "Gal.hpp" +#include "RenderConfigs.hpp" const std::map keyMapping = { {SDLK_BACKSPACE, Rml::Input::KI_BACK}, @@ -77,9 +78,8 @@ Render::~Render() { rmlRender.reset(); rmlSystem.reset(); - PluginSystem::Init(); + PluginSystem::Init(); - framebuffer.reset(); SDL_GL_DeleteContext(glContext); SDL_DestroyWindow(window); SDL_Quit(); @@ -137,28 +137,12 @@ void Render::PrepareToRendering() { float resolutionScale = Settings::ReadDouble("resolutionScale", 1.0f); size_t scaledW = width * resolutionScale, scaledH = height * resolutionScale; + gbuffer = std::make_unique(scaledW, scaledH, scaledW, scaledH); + auto gal = Gal::GetImplementation(); gal->GetDefaultFramebuffer()->SetViewport(0, 0, width, height); - auto dsTexConf = gal->CreateTexture2DConfig(scaledW, scaledH, Gal::Format::D24S8); - dsTexConf->SetMinFilter(Gal::Filtering::Bilinear); - dsTexConf->SetMaxFilter(Gal::Filtering::Bilinear); - fbDepthStencil = gal->BuildTexture(dsTexConf); - - auto texConf = gal->CreateTexture2DConfig(scaledW, scaledH, Gal::Format::R8G8B8A8); - texConf->SetMinFilter(Gal::Filtering::Bilinear); - texConf->SetMaxFilter(Gal::Filtering::Bilinear); - fbColor = gal->BuildTexture(texConf); - - auto fbConf = gal->CreateFramebufferConfig(); - fbConf->SetTexture(0, fbColor); - fbConf->SetDepthStencil(fbDepthStencil); - - framebuffer = gal->BuildFramebuffer(fbConf); - framebuffer->SetViewport(0, 0, scaledW, scaledH); - framebuffer->Clear(); - std::string vertexSource, pixelSource; { auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/fbo"); @@ -184,7 +168,7 @@ void Render::PrepareToRendering() { fbPPC->SetTarget(gal->GetDefaultFramebuffer()); fbPPC->SetVertexShader(gal->LoadVertexShader(vertexSource)); fbPPC->SetPixelShader(gal->LoadPixelShader(pixelSource)); - fbPPC->AddStaticTexture("inputTexture", fbColor); + fbPPC->AddStaticTexture("inputTexture", gbuffer->GetFinalTexture()); auto fbColorBB = fbPPC->BindVertexBuffer({ {"Pos", Gal::Type::Vec2}, {"TextureCoords", Gal::Type::Vec2} @@ -196,7 +180,7 @@ void Render::PrepareToRendering() { }); if (world) - world->PrepareRender(framebuffer); + world->PrepareRender(gbuffer->GetGeometryTarget()); } void Render::UpdateKeyboard() { @@ -221,7 +205,7 @@ void Render::RenderFrame() { OPTICK_EVENT(); Gal::GetImplementation()->GetDefaultFramebuffer()->Clear(); - framebuffer->Clear(); + gbuffer->Clear(); if (isWireframe) Gal::GetImplementation()->SetWireframe(true); @@ -230,6 +214,8 @@ void Render::RenderFrame() { if (isWireframe) Gal::GetImplementation()->SetWireframe(false); + gbuffer->Render(); + fbPipeline->Activate(); fbPipelineInstance->Activate(); fbPipelineInstance->Render(0, 6); @@ -487,7 +473,7 @@ void Render::InitEvents() { listener.RegisterHandler("PlayerConnected", [this](const Event&) { stateString = "Loading terrain..."; - world = std::make_unique(framebuffer); + world = std::make_unique(gbuffer->GetGeometryTarget()); world->MaxRenderingDistance = Settings::ReadDouble("renderDistance", 2.0f); PUSH_EVENT("UpdateSectionsRender", 0); }); diff --git a/src/Render.hpp b/src/Render.hpp index 87494fa..379ca66 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -11,6 +11,7 @@ #include "Event.hpp" #include "Gal.hpp" +class Gbuffer; class RendererWorld; class RmlRenderInterface; class RmlSystemInterface; @@ -35,12 +36,10 @@ class Render { bool HasFocus=true; float sensetivity = 0.1f; bool isWireframe = false; - std::shared_ptr framebuffer; - std::shared_ptr fbDepthStencil; - std::shared_ptr fbColor; std::shared_ptr fbPipeline; std::shared_ptr fbPipelineInstance; std::shared_ptr fbBuffer; + std::unique_ptr gbuffer; EventListener listener; std::string stateString; std::unique_ptr rmlRender; diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp new file mode 100644 index 0000000..ebdbaef --- /dev/null +++ b/src/RenderConfigs.cpp @@ -0,0 +1,75 @@ +#include "RenderConfigs.hpp" + +#include "AssetManager.hpp" + +Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { + auto gal = Gal::GetImplementation(); + + auto colorConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8A8); + color = gal->BuildTexture(colorConf); + + auto normalConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8A8); + normal = gal->BuildTexture(normalConf); + + auto dsConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::D24S8); + depthStencil = gal->BuildTexture(dsConf); + + auto geomFbConf = gal->CreateFramebufferConfig(); + geomFbConf->SetTexture(0, color); + geomFbConf->SetTexture(1, normal); + geomFbConf->SetDepthStencil(depthStencil); + + geomFramebuffer = gal->BuildFramebuffer(geomFbConf); + geomFramebuffer->SetViewport(0, 0, geomW, geomH); + + auto finalColorConf = gal->CreateTexture2DConfig(lightW, lightH, Gal::Format::R8G8B8A8); + finalColor = gal->BuildTexture(finalColorConf); + + auto lightFbConf = gal->CreateFramebufferConfig(); + lightFbConf->SetTexture(0, finalColor); + + lightFramebuffer = gal->BuildFramebuffer(lightFbConf); + lightFramebuffer->SetViewport(0, 0, lightW, lightH); + + std::string vertexSource, pixelSource; + { + auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/light"); + vertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); + + auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/light"); + pixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); + } + + auto lightPPC = gal->CreatePipelineConfig(); + lightPPC->SetTarget(lightFramebuffer); + lightPPC->AddStaticTexture("color", color); + lightPPC->AddStaticTexture("normal", normal); + lightPPC->AddStaticTexture("depthStencil", depthStencil); + + lightPPC->SetVertexShader(gal->LoadVertexShader(vertexSource)); + lightPPC->SetPixelShader(gal->LoadPixelShader(pixelSource)); + + auto lightBB = lightPPC->BindVertexBuffer({ + {"pos", Gal::Type::Vec2}, + {"uvPos", Gal::Type::Vec2} + }); + + lightPipeline = gal->BuildPipeline(lightPPC); + + 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 + }; + lightBuffer = gal->CreateBuffer(); + lightBuffer->SetData({ reinterpret_cast(quadVertices), reinterpret_cast(quadVertices) + sizeof(quadVertices) }); + + lightPipelineInstance = lightPipeline->CreateInstance({ + {lightBB, lightBuffer} + }); +} diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp new file mode 100644 index 0000000..6279169 --- /dev/null +++ b/src/RenderConfigs.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include "Gal.hpp" + +class Gbuffer { + std::shared_ptr lightFramebuffer; + std::shared_ptr lightBuffer; + std::shared_ptr lightPipeline; + std::shared_ptr lightPipelineInstance; + std::shared_ptr color; + std::shared_ptr normal; + std::shared_ptr depthStencil; + std::shared_ptr geomFramebuffer; + + std::shared_ptr finalColor; + +public: + Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH); + + std::shared_ptr GetGeometryTarget() { + return geomFramebuffer; + } + + std::shared_ptr GetFinalTexture() { + return finalColor; + } + + void Render() { + lightPipeline->Activate(); + lightPipelineInstance->Activate(); + lightPipelineInstance->Render(0, 6); + } + + void Clear() { + geomFramebuffer->Clear(); + lightFramebuffer->Clear(); + } +}; -- cgit v1.2.3 From f16c897522b6418c399b5699f8378a25c2e5de4f Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 21 Nov 2021 14:57:57 +0500 Subject: Added normals to faces --- src/RendererSectionData.cpp | 4 ++++ src/RendererSectionData.hpp | 19 ++++++++++--------- src/RendererWorld.cpp | 3 ++- 3 files changed, 16 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 2588fd6..0ac901e 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -26,11 +26,13 @@ glm::vec2 TransformTextureCoord(glm::vec4 TextureAtlasCoords, glm::vec2 UvCoords void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, const glm::mat4 &transform, bool visibility[FaceDirection::none], BlockLightness light, BlockLightness skyLight) { for (const auto &face : model.faces) { + glm::vec3 normal = {}; glm::vec2 lightness; lightness.x = _max(light.face[0], light.face[1], light.face[2], light.face[3], light.face[4], light.face[5]); lightness.y = _max(skyLight.face[0], skyLight.face[1], skyLight.face[2], skyLight.face[3], skyLight.face[4], skyLight.face[5]); if (face.visibility != FaceDirection::none) { FaceDirection direction = face.visibility; + normal = FaceDirectionVector[direction].glm(); Vector directionVec = model.faceDirectionVector[direction]; FaceDirection faceDirection = FaceDirection::none; for (int i = 0; i < FaceDirection::none; i++) { @@ -56,6 +58,8 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co vertexData.positions[2] = transformed * glm::vec4(1, 0, 1, 1); vertexData.positions[3] = transformed * glm::vec4(1, 0, 0, 1); + vertexData.normal = normal; + vertexData.uvs[0] = TransformTextureCoord(face.texture, glm::vec2(0, 0), face.frames); vertexData.uvs[1] = TransformTextureCoord(face.texture, glm::vec2(1, 0), face.frames); vertexData.uvs[2] = TransformTextureCoord(face.texture, glm::vec2(1, 1), face.frames); diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index edd2992..0a80560 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -31,20 +31,21 @@ struct SectionsData { }; struct VertexData { - glm::vec3 positions[4]; - glm::vec2 uvs[4]; - float uvLayers; - float animations; - glm::vec3 colors; - glm::vec2 lights; - uint8_t padding[20]; + glm::vec3 positions[4]; + glm::vec3 normal; + glm::vec2 uvs[4]; + float uvLayers; + float animations; + glm::vec3 colors; + glm::vec2 lights; + uint8_t padding[8]; }; struct RendererSectionData { - std::vector vertices; + std::vector vertices; size_t hash = 0; Vector sectionPos; - bool forced = false; + bool forced = false; }; RendererSectionData ParseSection(const SectionsData §ions); \ No newline at end of file diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 561578f..9055729 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -449,12 +449,13 @@ void RendererWorld::PrepareRender(std::shared_ptr target) { sectionsPLC->SetPrimitive(Gal::Primitive::TriangleFan); sectionsBufferBinding = sectionsPLC->BindVertexBuffer({ {"position", Gal::Type::Vec3, 4, 1}, + {"normal", Gal::Type::Vec3, 1, 1}, {"uv", Gal::Type::Vec2, 4, 1}, {"uvLayer", Gal::Type::Float, 1, 1}, {"animation", Gal::Type::Float, 1, 1}, {"color", Gal::Type::Vec3, 1, 1}, {"light", Gal::Type::Vec2, 1, 1}, - {"", Gal::Type::Uint8, 20, 1} + {"", Gal::Type::Uint8, 8, 1} }); sectionsPipeline = gal->BuildPipeline(sectionsPLC); sectionsPipeline->SetShaderParameter("MinLightLevel", 0.2f); -- cgit v1.2.3 From 0ca11f9bee1cd918acf6ce8247a495442009fec9 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 21 Nov 2021 17:49:23 +0500 Subject: Moved face lighting to lighting pass --- src/GalOgl.cpp | 5 +++++ src/Render.cpp | 4 +++- src/RenderConfigs.cpp | 27 +++++++++++++++++++++++++-- src/RenderConfigs.hpp | 10 ++++++++-- src/RendererWorld.cpp | 1 + src/RendererWorld.hpp | 2 ++ 6 files changed, 44 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index c250539..75369ab 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -1256,6 +1256,11 @@ struct ImplOgl : public Impl { size_t attribSize = GalTypeGetSize(type); for (size_t i = 0; i < count; i++) { + if (location < 0) { + vertexSize += attribSize; + continue; + } + pipeline->vertexBindCmds.push_back({ bufferId, static_cast(location + i), diff --git a/src/Render.cpp b/src/Render.cpp index 96c1ed8..48114b0 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -209,8 +209,10 @@ void Render::RenderFrame() { if (isWireframe) Gal::GetImplementation()->SetWireframe(true); - if (renderWorld) + if (renderWorld) { world->Render(static_cast(windowWidth) / static_cast(windowHeight)); + gbuffer->SetDayTime(world->shaderDayTime); + } if (isWireframe) Gal::GetImplementation()->SetWireframe(false); diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index ebdbaef..86418c2 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -5,24 +5,44 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { auto gal = Gal::GetImplementation(); - auto colorConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8A8); + auto colorConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); + colorConf->SetMinFilter(Gal::Filtering::Bilinear); + colorConf->SetMaxFilter(Gal::Filtering::Bilinear); color = gal->BuildTexture(colorConf); - auto normalConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8A8); + auto normalConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); + normalConf->SetMinFilter(Gal::Filtering::Bilinear); + normalConf->SetMaxFilter(Gal::Filtering::Bilinear); normal = gal->BuildTexture(normalConf); + auto addColorConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); + addColorConf->SetMinFilter(Gal::Filtering::Bilinear); + addColorConf->SetMaxFilter(Gal::Filtering::Bilinear); + addColor = gal->BuildTexture(addColorConf); + + auto lightConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); + lightConf->SetMinFilter(Gal::Filtering::Bilinear); + lightConf->SetMaxFilter(Gal::Filtering::Bilinear); + light = gal->BuildTexture(lightConf); + auto dsConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::D24S8); + dsConf->SetMinFilter(Gal::Filtering::Bilinear); + dsConf->SetMaxFilter(Gal::Filtering::Bilinear); depthStencil = gal->BuildTexture(dsConf); auto geomFbConf = gal->CreateFramebufferConfig(); geomFbConf->SetTexture(0, color); geomFbConf->SetTexture(1, normal); + geomFbConf->SetTexture(2, addColor); + geomFbConf->SetTexture(3, light); geomFbConf->SetDepthStencil(depthStencil); geomFramebuffer = gal->BuildFramebuffer(geomFbConf); geomFramebuffer->SetViewport(0, 0, geomW, geomH); auto finalColorConf = gal->CreateTexture2DConfig(lightW, lightH, Gal::Format::R8G8B8A8); + finalColorConf->SetMinFilter(Gal::Filtering::Bilinear); + finalColorConf->SetMaxFilter(Gal::Filtering::Bilinear); finalColor = gal->BuildTexture(finalColorConf); auto lightFbConf = gal->CreateFramebufferConfig(); @@ -43,8 +63,11 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { auto lightPPC = gal->CreatePipelineConfig(); lightPPC->SetTarget(lightFramebuffer); lightPPC->AddStaticTexture("color", color); + lightPPC->AddStaticTexture("addColor", addColor); lightPPC->AddStaticTexture("normal", normal); + lightPPC->AddStaticTexture("light", light); lightPPC->AddStaticTexture("depthStencil", depthStencil); + lightPPC->AddShaderParameter("dayTime", Gal::Type::Float); lightPPC->SetVertexShader(gal->LoadVertexShader(vertexSource)); lightPPC->SetPixelShader(gal->LoadPixelShader(pixelSource)); diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index 6279169..94e96bb 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -7,8 +7,10 @@ class Gbuffer { std::shared_ptr lightBuffer; std::shared_ptr lightPipeline; std::shared_ptr lightPipelineInstance; - std::shared_ptr color; - std::shared_ptr normal; + std::shared_ptr color; //RGB - color + std::shared_ptr normal; //RGB - normal + std::shared_ptr addColor; //RGB - addColor + std::shared_ptr light; //R - faceLight, G - skyLight, B - unused std::shared_ptr depthStencil; std::shared_ptr geomFramebuffer; @@ -35,4 +37,8 @@ public: geomFramebuffer->Clear(); lightFramebuffer->Clear(); } + + void SetDayTime(float dayTime) { + lightPipeline->SetShaderParameter("dayTime", dayTime); + } }; diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 9055729..d2c69fd 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -366,6 +366,7 @@ void RendererWorld::Render(float screenRatio) { float timePassed = (dayTime - moonriseMin); mixLevel = 1.0 - (timePassed / moonriseLength); } + shaderDayTime = mixLevel; skyPipeline->Activate(); skyPipeline->SetShaderParameter("projView", projView); diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index e645b30..a6fca06 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -63,6 +63,8 @@ public: double MaxRenderingDistance; + float shaderDayTime; + void Update(double timeToUpdate); }; -- cgit v1.2.3 From 3f14653e536c6c4b4bb9e4d0a6b2ec0dfeadc665 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 21 Nov 2021 19:49:04 +0500 Subject: Added gbuffer visualization --- src/Render.cpp | 16 ++++++++++++++++ src/Render.hpp | 1 + src/RenderConfigs.cpp | 1 + src/RenderConfigs.hpp | 8 ++++++++ 4 files changed, 26 insertions(+) (limited to 'src') diff --git a/src/Render.cpp b/src/Render.cpp index 48114b0..148ec27 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -335,6 +335,22 @@ void Render::HandleEvents() { break; } + case SDL_SCANCODE_F9: { + if (sdlKeyMods & KMOD_CTRL) { + renderBuff = 0; + } else if (sdlKeyMods & KMOD_SHIFT) { + renderBuff--; + if (renderBuff < 0) + renderBuff = 0; + } else { + renderBuff++; + if (renderBuff > gbuffer->GetMaxRenderBuffers()) + renderBuff = 0; + } + gbuffer->SetRenderBuff(renderBuff); + break; + } + default: break; } diff --git a/src/Render.hpp b/src/Render.hpp index 379ca66..8bdcd63 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -48,6 +48,7 @@ class Render { Rml::Context* rmlContext; unsigned short sdlKeyMods = 0; bool hideRml = false; + size_t renderBuff = 0; void SetMouseCapture(bool IsCaptured); diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index 86418c2..2867bc6 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -68,6 +68,7 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { lightPPC->AddStaticTexture("light", light); lightPPC->AddStaticTexture("depthStencil", depthStencil); lightPPC->AddShaderParameter("dayTime", Gal::Type::Float); + lightPPC->AddShaderParameter("renderBuff", Gal::Type::Int32); lightPPC->SetVertexShader(gal->LoadVertexShader(vertexSource)); lightPPC->SetPixelShader(gal->LoadPixelShader(pixelSource)); diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index 94e96bb..9b535ca 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -41,4 +41,12 @@ public: void SetDayTime(float dayTime) { lightPipeline->SetShaderParameter("dayTime", dayTime); } + + int GetMaxRenderBuffers() { + return 5; + } + + void SetRenderBuff(int renderBuff) { + lightPipeline->SetShaderParameter("renderBuff", renderBuff); + } }; -- cgit v1.2.3 From 0148fe0915c2c6f0357609c9b3e1c99d713fcd2b Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 21 Nov 2021 21:26:31 +0500 Subject: Added PostProcess class --- src/Render.cpp | 1 + src/RenderConfigs.cpp | 145 ++++++++++++++++++++++++++++++++------------------ src/RenderConfigs.hpp | 60 ++++++++++++++++----- 3 files changed, 140 insertions(+), 66 deletions(-) (limited to 'src') diff --git a/src/Render.cpp b/src/Render.cpp index 148ec27..fadec80 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -138,6 +138,7 @@ void Render::PrepareToRendering() { size_t scaledW = width * resolutionScale, scaledH = height * resolutionScale; gbuffer = std::make_unique(scaledW, scaledH, scaledW, scaledH); + gbuffer->SetRenderBuff(renderBuff); auto gal = Gal::GetImplementation(); gal->GetDefaultFramebuffer()->SetViewport(0, 0, width, height); diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index 2867bc6..d62d204 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -2,6 +2,81 @@ #include "AssetManager.hpp" +std::string LoadShaderCode(std::string_view assetPath) { + auto gal = Gal::GetImplementation(); + auto codeAsset = AssetManager::GetAssetByAssetName(std::string(assetPath)); + auto codePtr = reinterpret_cast(codeAsset->data.data()); + return std::string(codePtr, codePtr + codeAsset->data.size()); +} + +std::shared_ptr LoadVertexShader(std::string_view assetPath) { + auto gal = Gal::GetImplementation(); + return gal->LoadVertexShader(LoadShaderCode(assetPath)); +} + +std::shared_ptr LoadPixelShader(std::string_view assetPath) { + auto gal = Gal::GetImplementation(); + return gal->LoadPixelShader(LoadShaderCode(assetPath)); +} + +PostProcess::PostProcess( + std::shared_ptr pixelShader, + std::vector>> inputTextures, + std::vector> inputParameters, + size_t width, + size_t height, + Gal::Format format, + Gal::Filtering filtering) { + auto gal = Gal::GetImplementation(); + + auto texConf = gal->CreateTexture2DConfig(width, height, format); + texConf->SetMinFilter(filtering); + texConf->SetMaxFilter(filtering); + + result = gal->BuildTexture(texConf); + + auto fbConf = gal->CreateFramebufferConfig(); + fbConf->SetTexture(0, result); + + framebuffer = gal->BuildFramebuffer(fbConf); + framebuffer->SetViewport(0, 0, width, height); + + auto fbPPC = gal->CreatePipelineConfig(); + fbPPC->SetTarget(framebuffer); + for (auto&& [name, texture] : inputTextures) { + fbPPC->AddStaticTexture(name, texture); + } + for (auto&& [name, type] : inputParameters) { + fbPPC->AddShaderParameter(name, type); + } + fbPPC->SetVertexShader(LoadVertexShader("/altcraft/shaders/vert/pp")); + fbPPC->SetPixelShader(pixelShader); + 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} + }); +} + Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { auto gal = Gal::GetImplementation(); @@ -40,60 +115,24 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { geomFramebuffer = gal->BuildFramebuffer(geomFbConf); geomFramebuffer->SetViewport(0, 0, geomW, geomH); - auto finalColorConf = gal->CreateTexture2DConfig(lightW, lightH, Gal::Format::R8G8B8A8); - finalColorConf->SetMinFilter(Gal::Filtering::Bilinear); - finalColorConf->SetMaxFilter(Gal::Filtering::Bilinear); - finalColor = gal->BuildTexture(finalColorConf); - - auto lightFbConf = gal->CreateFramebufferConfig(); - lightFbConf->SetTexture(0, finalColor); - - lightFramebuffer = gal->BuildFramebuffer(lightFbConf); - lightFramebuffer->SetViewport(0, 0, lightW, lightH); - - std::string vertexSource, pixelSource; - { - auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/light"); - vertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); - - auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/light"); - pixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); - } - - auto lightPPC = gal->CreatePipelineConfig(); - lightPPC->SetTarget(lightFramebuffer); - lightPPC->AddStaticTexture("color", color); - lightPPC->AddStaticTexture("addColor", addColor); - lightPPC->AddStaticTexture("normal", normal); - lightPPC->AddStaticTexture("light", light); - lightPPC->AddStaticTexture("depthStencil", depthStencil); - lightPPC->AddShaderParameter("dayTime", Gal::Type::Float); - lightPPC->AddShaderParameter("renderBuff", Gal::Type::Int32); - - lightPPC->SetVertexShader(gal->LoadVertexShader(vertexSource)); - lightPPC->SetPixelShader(gal->LoadPixelShader(pixelSource)); - - auto lightBB = lightPPC->BindVertexBuffer({ - {"pos", Gal::Type::Vec2}, - {"uvPos", Gal::Type::Vec2} - }); - - lightPipeline = gal->BuildPipeline(lightPPC); - - 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, + std::vector>> lightingTexture = { + {"color", color}, + {"addColor", addColor}, + {"normal", normal}, + {"light", light}, + {"depthStencil", depthStencil}, + }; - -1.0f, 1.0f, 0.0f, 1.0f, - 1.0f, -1.0f, 1.0f, 0.0f, - 1.0f, 1.0f, 1.0f, 1.0f + std::vector> lightingParameters = { + {"dayTime", Gal::Type::Float}, + {"renderBuff", Gal::Type::Int32}, }; - lightBuffer = gal->CreateBuffer(); - lightBuffer->SetData({ reinterpret_cast(quadVertices), reinterpret_cast(quadVertices) + sizeof(quadVertices) }); - lightPipelineInstance = lightPipeline->CreateInstance({ - {lightBB, lightBuffer} - }); + lightingPass = std::make_unique(LoadPixelShader("/altcraft/shaders/frag/light"), + lightingTexture, + lightingParameters, + lightW, + lightH, + Gal::Format::R8G8B8A8, + Gal::Filtering::Bilinear); } diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index 9b535ca..793dbb2 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -2,11 +2,49 @@ #include "Gal.hpp" +std::shared_ptr LoadVertexShader(std::string_view assetPath); + +std::shared_ptr LoadPixelShader(std::string_view assetPath); + +class PostProcess { + std::shared_ptr framebuffer; + std::shared_ptr fbBuffer; + std::shared_ptr pipeline; + std::shared_ptr pipelineInstance; + std::shared_ptr result; +public: + + PostProcess( + std::shared_ptr pixelShader, + std::vector>> inputTextures, + std::vector> inputParameters, + size_t width, + size_t height, + Gal::Format format, + Gal::Filtering filtering); + + void Clear() { + framebuffer->Clear(); + } + + void Render() { + pipeline->Activate(); + pipelineInstance->Activate(); + pipelineInstance->Render(0, 6); + } + + template + void SetShaderParameter(std::string_view name, T value) { + pipeline->SetShaderParameter(name, value); + } + + std::shared_ptr GetResultTexture() { + return result; + } +}; + class Gbuffer { - std::shared_ptr lightFramebuffer; - std::shared_ptr lightBuffer; - std::shared_ptr lightPipeline; - std::shared_ptr lightPipelineInstance; + std::unique_ptr lightingPass; std::shared_ptr color; //RGB - color std::shared_ptr normal; //RGB - normal std::shared_ptr addColor; //RGB - addColor @@ -14,8 +52,6 @@ class Gbuffer { std::shared_ptr depthStencil; std::shared_ptr geomFramebuffer; - std::shared_ptr finalColor; - public: Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH); @@ -24,22 +60,20 @@ public: } std::shared_ptr GetFinalTexture() { - return finalColor; + return lightingPass->GetResultTexture(); } void Render() { - lightPipeline->Activate(); - lightPipelineInstance->Activate(); - lightPipelineInstance->Render(0, 6); + lightingPass->Render(); } void Clear() { geomFramebuffer->Clear(); - lightFramebuffer->Clear(); + lightingPass->Clear(); } void SetDayTime(float dayTime) { - lightPipeline->SetShaderParameter("dayTime", dayTime); + lightingPass->SetShaderParameter("dayTime", dayTime); } int GetMaxRenderBuffers() { @@ -47,6 +81,6 @@ public: } void SetRenderBuff(int renderBuff) { - lightPipeline->SetShaderParameter("renderBuff", renderBuff); + lightingPass->SetShaderParameter("renderBuff", renderBuff); } }; -- cgit v1.2.3 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/Gal.hpp | 40 +++++++++++-------------------- src/GalOgl.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++------- src/Render.cpp | 1 + src/RenderConfigs.hpp | 4 ++++ src/RendererWorld.cpp | 3 +++ 5 files changed, 78 insertions(+), 35 deletions(-) (limited to 'src') 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 GetDefaultFramebuffer() = 0; - virtual std::shared_ptr GetGlobalShaderParameters() = 0; + virtual std::shared_ptr GetGlobalShaderParameters() = 0; virtual std::shared_ptr LoadVertexShader(std::string_view code) = 0; @@ -250,34 +250,22 @@ namespace Gal { virtual void SetTexture(size_t location, std::shared_ptr texture) = 0; }; - struct ShaderParameters { - virtual ~ShaderParameters() = default; + struct ShaderParametersBuffer { + virtual ~ShaderParametersBuffer() = default; - virtual void AddGlobalShaderParameter(std::string_view name, Type type) = 0; + template + T* Get() { + return reinterpret_cast(GetDataPtr()); + } - virtual void SetGlobalShaderParameter(std::string_view name, float value) = 0; + template + 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 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 { 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(); 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 LoadVertexShader(std::string_view assetPath); std::shared_ptr 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()->projView = projView; + //Render Entities constexpr size_t entitiesVerticesCount = 240; entitiesPipeline->Activate(); -- cgit v1.2.3 From c905ede556c892d39fd69d3945026ba244567ce9 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sat, 27 Nov 2021 21:08:58 +0500 Subject: Changed shaders to use SPB --- src/GalOgl.cpp | 2 ++ src/Render.cpp | 7 ++++--- src/RenderConfigs.cpp | 1 - src/RenderConfigs.hpp | 7 +++---- src/RendererEntity.cpp | 2 +- src/RendererWorld.cpp | 43 +++++++++++++++---------------------------- src/Rml.cpp | 11 +++-------- 7 files changed, 28 insertions(+), 45 deletions(-) (limited to 'src') diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index 854ab40..961c6ef 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -1049,6 +1049,8 @@ struct ImplOgl : public Impl { throw std::runtime_error("GLEW initialization failed with unknown reason"); } + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + GLint flags; glGetIntegerv(GL_CONTEXT_FLAGS, &flags); if (flags & GL_CONTEXT_FLAG_DEBUG_BIT) diff --git a/src/Render.cpp b/src/Render.cpp index 8a5253c..682b60a 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -172,8 +172,8 @@ void Render::PrepareToRendering() { fbPPC->SetPixelShader(gal->LoadPixelShader(pixelSource)); fbPPC->AddStaticTexture("inputTexture", gbuffer->GetFinalTexture()); auto fbColorBB = fbPPC->BindVertexBuffer({ - {"Pos", Gal::Type::Vec2}, - {"TextureCoords", Gal::Type::Vec2} + {"pos", Gal::Type::Vec2}, + {"uvPos", Gal::Type::Vec2} }); fbPipeline = gal->BuildPipeline(fbPPC); @@ -213,7 +213,6 @@ void Render::RenderFrame() { Gal::GetImplementation()->SetWireframe(true); if (renderWorld) { world->Render(static_cast(windowWidth) / static_cast(windowHeight)); - gbuffer->SetDayTime(world->shaderDayTime); } if (isWireframe) Gal::GetImplementation()->SetWireframe(false); @@ -256,6 +255,7 @@ void Render::HandleEvents() { windowHeight = height; rmlRender->Update(width, height); rmlContext->SetDimensions(Rml::Vector2i(width, height)); + Gal::GetImplementation()->GetGlobalShaderParameters()->Get()->viewportSize = glm::uvec2(width, height); PrepareToRendering(); break; } @@ -613,6 +613,7 @@ void Render::InitRml() { rmlRender = std::make_unique(); Rml::SetRenderInterface(rmlRender.get()); rmlRender->Update(windowWidth, windowHeight); + Gal::GetImplementation()->GetGlobalShaderParameters()->Get()->viewportSize = glm::uvec2(windowWidth, windowHeight); rmlFile = std::make_unique(); Rml::SetFileInterface(rmlFile.get()); diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index d62d204..aad2c59 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -124,7 +124,6 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { }; std::vector> lightingParameters = { - {"dayTime", Gal::Type::Float}, {"renderBuff", Gal::Type::Int32}, }; diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index a9d6c00..da7fd96 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -4,6 +4,9 @@ struct GlobalShaderParameters { glm::mat4 projView; + glm::uvec2 viewportSize; + float globalTime; + float dayTime; }; std::shared_ptr LoadVertexShader(std::string_view assetPath); @@ -76,10 +79,6 @@ public: lightingPass->Clear(); } - void SetDayTime(float dayTime) { - lightingPass->SetShaderParameter("dayTime", dayTime); - } - int GetMaxRenderBuffers() { return 5; } diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp index 02a5f54..d014bdb 100644 --- a/src/RendererEntity.cpp +++ b/src/RendererEntity.cpp @@ -18,5 +18,5 @@ void RendererEntity::Render(std::shared_ptr pipeline, const World model = glm::scale(model, glm::vec3(entity.width, entity.height, entity.width)); pipeline->SetShaderParameter("model", model); - pipeline->SetShaderParameter("color", entity.renderColor); + pipeline->SetShaderParameter("entityColor", entity.renderColor); } diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 2434ed1..c6d490a 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -256,10 +256,9 @@ RendererWorld::RendererWorld(std::shared_ptr target) { sections.erase(it); }); - listener->RegisterHandler("SetMinLightLevel", [this](const Event& eventData) { - auto value = eventData.get(); - sectionsPipeline->SetShaderParameter("MinLightLevel", value); - }); + listener->RegisterHandler("SetMinLightLevel", [this](const Event& eventData) { + + }); for (int i = 0; i < numOfWorkers; i++) workers.push_back(std::thread(&RendererWorld::WorkerFunction, this, i)); @@ -290,13 +289,12 @@ void RendererWorld::Render(float screenRatio) { glm::mat4 view = GetGameState()->GetViewMatrix(); glm::mat4 projView = projection * view; - Gal::GetImplementation()->GetGlobalShaderParameters()->Get()->projView = projView; + auto globalSpb = Gal::GetImplementation()->GetGlobalShaderParameters(); + globalSpb->Get()->projView = projView; //Render Entities constexpr size_t entitiesVerticesCount = 240; entitiesPipeline->Activate(); - entitiesPipeline->SetShaderParameter("projView", projView); - entitiesPipelineInstance->Activate(); for (auto& it : entities) { it.Render(entitiesPipeline, &GetGameState()->GetWorld()); @@ -312,7 +310,7 @@ void RendererWorld::Render(float screenRatio) { model = glm::translate(model,glm::vec3(0.5f,0.5f,0.5f)); model = glm::scale(model,glm::vec3(1.01f,1.01f,1.01f)); entitiesPipeline->SetShaderParameter("model", model); - entitiesPipeline->SetShaderParameter("color", glm::vec3(0, 0, 0)); + entitiesPipeline->SetShaderParameter("entityColor", glm::vec3(0, 0, 0)); entitiesPipelineInstance->Render(0, entitiesVerticesCount); } } @@ -328,9 +326,9 @@ void RendererWorld::Render(float screenRatio) { //entityShader->SetUniform("model", model); entitiesPipeline->SetShaderParameter("model", model); if (selectedBlock == Vector()) - entitiesPipeline->SetShaderParameter("color", glm::vec3(0.7f, 0.0f, 0.0f)); + entitiesPipeline->SetShaderParameter("entityColor", glm::vec3(0.7f, 0.0f, 0.0f)); else - entitiesPipeline->SetShaderParameter("color", glm::vec3(0.0f, 0.0f, 0.7f)); + entitiesPipeline->SetShaderParameter("entityColor", glm::vec3(0.0f, 0.0f, 0.7f)); entitiesPipelineInstance->Render(0, entitiesVerticesCount); } } @@ -369,12 +367,11 @@ void RendererWorld::Render(float screenRatio) { float timePassed = (dayTime - moonriseMin); mixLevel = 1.0 - (timePassed / moonriseLength); } - shaderDayTime = mixLevel; + + globalSpb->Get()->dayTime = mixLevel; skyPipeline->Activate(); - skyPipeline->SetShaderParameter("projView", projView); skyPipeline->SetShaderParameter("model", model); - skyPipeline->SetShaderParameter("DayTime", mixLevel); skyPipelineInstance->Activate(); skyPipelineInstance->Render(0, 36); @@ -382,10 +379,8 @@ void RendererWorld::Render(float screenRatio) { //Render sections auto rawGlobalTime = (std::chrono::high_resolution_clock::now() - globalTimeStart); float globalTime = rawGlobalTime.count() / 1000000000.0f; + globalSpb->Get()->globalTime = globalTime; sectionsPipeline->Activate(); - sectionsPipeline->SetShaderParameter("DayTime", mixLevel); - sectionsPipeline->SetShaderParameter("projView", projView); - sectionsPipeline->SetShaderParameter("GlobalTime", globalTime); Frustum frustum(projView); @@ -443,16 +438,12 @@ void RendererWorld::PrepareRender(std::shared_ptr target) { { auto sectionsPLC = gal->CreatePipelineConfig(); sectionsPLC->SetTarget(target); - sectionsPLC->AddShaderParameter("projView", Gal::Type::Mat4); - sectionsPLC->AddShaderParameter("DayTime", Gal::Type::Float); - sectionsPLC->AddShaderParameter("GlobalTime", Gal::Type::Float); - sectionsPLC->AddShaderParameter("MinLightLevel", Gal::Type::Float); sectionsPLC->AddStaticTexture("textureAtlas", AssetManager::GetTextureAtlas()); sectionsPLC->SetVertexShader(gal->LoadVertexShader(sectionVertexSource)); sectionsPLC->SetPixelShader(gal->LoadPixelShader(sectionPixelSource)); sectionsPLC->SetPrimitive(Gal::Primitive::TriangleFan); sectionsBufferBinding = sectionsPLC->BindVertexBuffer({ - {"position", Gal::Type::Vec3, 4, 1}, + {"pos", Gal::Type::Vec3, 4, 1}, {"normal", Gal::Type::Vec3, 1, 1}, {"uv", Gal::Type::Vec2, 4, 1}, {"uvLayer", Gal::Type::Float, 1, 1}, @@ -462,20 +453,18 @@ void RendererWorld::PrepareRender(std::shared_ptr target) { {"", Gal::Type::Uint8, 8, 1} }); sectionsPipeline = gal->BuildPipeline(sectionsPLC); - sectionsPipeline->SetShaderParameter("MinLightLevel", 0.2f); } { auto entitiesPLC = gal->CreatePipelineConfig(); entitiesPLC->SetTarget(target); - entitiesPLC->AddShaderParameter("projView", Gal::Type::Mat4); entitiesPLC->AddShaderParameter("model", Gal::Type::Mat4); - entitiesPLC->AddShaderParameter("color", Gal::Type::Vec3); + entitiesPLC->AddShaderParameter("entityColor", Gal::Type::Vec3); entitiesPLC->SetVertexShader(gal->LoadVertexShader(entitiesVertexSource)); entitiesPLC->SetPixelShader(gal->LoadPixelShader(entitiesPixelSource)); entitiesPLC->SetPrimitive(Gal::Primitive::Triangle); auto entitiesPosBB = entitiesPLC->BindVertexBuffer({ - {"position", Gal::Type::Vec3}, + {"pos", Gal::Type::Vec3}, }); auto entitiesIndicesBB = entitiesPLC->BindIndexBuffer(); @@ -636,14 +625,12 @@ void RendererWorld::PrepareRender(std::shared_ptr target) { skyPPC->AddShaderParameter("sunTextureLayer", Gal::Type::Float); skyPPC->AddShaderParameter("moonTexture", Gal::Type::Vec4); skyPPC->AddShaderParameter("moonTextureLayer", Gal::Type::Float); - skyPPC->AddShaderParameter("DayTime", Gal::Type::Float); - skyPPC->AddShaderParameter("projView", Gal::Type::Mat4); skyPPC->AddShaderParameter("model", Gal::Type::Mat4); skyPPC->AddStaticTexture("textureAtlas", AssetManager::GetTextureAtlas()); skyPPC->SetVertexShader(gal->LoadVertexShader(skyVertexSource)); skyPPC->SetPixelShader(gal->LoadPixelShader(skyPixelSource)); auto skyPosUvBB = skyPPC->BindVertexBuffer({ - {"position", Gal::Type::Vec3}, + {"pos", Gal::Type::Vec3}, {"", Gal::Type::Vec2}, }); diff --git a/src/Rml.cpp b/src/Rml.cpp index fa2d4e7..746f6a4 100644 --- a/src/Rml.cpp +++ b/src/Rml.cpp @@ -64,7 +64,6 @@ RmlRenderInterface::RmlRenderInterface() { { auto pipelineConfig = gal->CreatePipelineConfig(); - pipelineConfig->AddShaderParameter("viewportSize", Gal::Type::Vec2u32); pipelineConfig->AddShaderParameter("translation", Gal::Type::Vec2); pipelineConfig->SetTarget(gal->GetDefaultFramebuffer()); pipelineConfig->SetVertexShader(gal->LoadVertexShader(vertexSource)); @@ -72,7 +71,7 @@ RmlRenderInterface::RmlRenderInterface() { auto vertBuffBind = pipelineConfig->BindVertexBuffer({ {"pos", Gal::Type::Vec2}, - {"color", Gal::Type::Vec4u8}, + {"col", Gal::Type::Vec4u8}, {"", Gal::Type::Vec2}, //it's not used in shader, so driver optimizes it away }); @@ -88,7 +87,6 @@ RmlRenderInterface::RmlRenderInterface() { { auto texPipelineConfig = gal->CreatePipelineConfig(); - texPipelineConfig->AddShaderParameter("viewportSize", Gal::Type::Vec2u32); texPipelineConfig->AddShaderParameter("translation", Gal::Type::Vec2); texPipelineConfig->AddShaderParameter("fontTexture", Gal::Type::Int32); texPipelineConfig->SetTarget(gal->GetDefaultFramebuffer()); @@ -97,8 +95,8 @@ RmlRenderInterface::RmlRenderInterface() { auto texVertBuffBind = texPipelineConfig->BindVertexBuffer({ {"pos", Gal::Type::Vec2}, - {"color", Gal::Type::Vec4u8}, - {"tex_coord", Gal::Type::Vec2}, + {"col", Gal::Type::Vec4u8}, + {"uvPos", Gal::Type::Vec2}, }); auto texIndexBuffBind = texPipelineConfig->BindIndexBuffer(); @@ -165,9 +163,6 @@ void RmlRenderInterface::ReleaseTexture(Rml::TextureHandle texture) { } void RmlRenderInterface::Update(unsigned int windowWidth, unsigned int windowHeight) { - pipeline->SetShaderParameter("viewportSize", glm::uvec2(windowWidth, windowHeight)); - texPipeline->SetShaderParameter("viewportSize", glm::uvec2(windowWidth, windowHeight)); - vpWidth = windowWidth; vpHeight = windowHeight; } -- cgit v1.2.3 From 8286ddda96a5f2925d342d0ce115aa00ae5d94ec Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 28 Nov 2021 16:16:29 +0500 Subject: Added gamma correction --- src/Gal.hpp | 3 +++ src/GalOgl.cpp | 29 ++++++++++++++++++++++++++--- src/Render.cpp | 6 +++--- src/RenderConfigs.hpp | 8 ++++++-- src/RendererWorld.cpp | 4 ---- src/TextureAtlas.cpp | 1 + 6 files changed, 39 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/Gal.hpp b/src/Gal.hpp index 234e49a..8d7394a 100644 --- a/src/Gal.hpp +++ b/src/Gal.hpp @@ -158,6 +158,8 @@ namespace Gal { virtual void SetWrapping(Wrapping wrapping) = 0; + virtual void SetLinear(bool isLinear) = 0; + }; struct Texture { @@ -261,6 +263,7 @@ namespace Gal { template void Resize() { Resize(sizeof(T)); + *Get() = T{}; } virtual std::byte* GetDataPtr() = 0; diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index 961c6ef..ff43ed8 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -321,7 +321,7 @@ size_t GalFormatGetSize(Format format) { return 0; } -GLenum GalFormatGetGlInternalFormat(Format format) { +GLenum GalFormatGetGlLinearInternalFormat(Format format) { switch (format) { case Format::D24S8: return GL_DEPTH24_STENCIL8; @@ -335,6 +335,20 @@ GLenum GalFormatGetGlInternalFormat(Format format) { return 0; } +GLenum GalFormatGetGlInternalFormat(Format format) { + switch (format) { + case Format::D24S8: + return GL_DEPTH24_STENCIL8; + case Format::R8G8B8: + return GL_SRGB; + case Format::R8G8B8A8: + return GL_SRGB_ALPHA; + default: + return 0; + } + return 0; +} + GLenum GalFormatGetGlFormat(Format format) { switch (format) { case Format::D24S8: @@ -580,6 +594,7 @@ struct TextureConfigOgl : public TextureConfig { Format format; size_t width = 1, height = 1, depth = 1; bool interpolateLayers = false; + bool linear = true; GLenum type; Filtering min = Filtering::Nearest, max = Filtering::Nearest; @@ -597,6 +612,10 @@ struct TextureConfigOgl : public TextureConfig { wrap = wrapping; } + virtual void SetLinear(bool isLinear) override { + linear = isLinear; + } + }; struct TextureOgl : public Texture { @@ -605,12 +624,15 @@ struct TextureOgl : public Texture { GlResource texture; Format format; size_t width, height, depth; + bool linear; 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()) throw std::logic_error("Size of data is not valid for this texture"); + GLenum internalFormat = linear ? GalFormatGetGlLinearInternalFormat(format) : GalFormatGetGlInternalFormat(format); + oglState.BindTexture(type, texture); switch (type) { @@ -630,13 +652,13 @@ struct TextureOgl : public Texture { case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: case GL_PROXY_TEXTURE_CUBE_MAP: - glTexImage2D(type, mipLevel, GalFormatGetGlInternalFormat(format), width, height, 0, GalFormatGetGlFormat(format), GalFormatGetGlType(format), data.empty() ? nullptr : data.data()); + glTexImage2D(type, mipLevel, internalFormat, width, height, 0, GalFormatGetGlFormat(format), GalFormatGetGlType(format), data.empty() ? nullptr : data.data()); break; case GL_TEXTURE_3D: case GL_PROXY_TEXTURE_3D: case GL_TEXTURE_2D_ARRAY: case GL_PROXY_TEXTURE_2D_ARRAY: - glTexImage3D(type, mipLevel, GalFormatGetGlInternalFormat(format), width, height, depth, 0, GalFormatGetGlFormat(format), GalFormatGetGlType(format), data.empty() ? nullptr : data.data()); + glTexImage3D(type, mipLevel, internalFormat, width, height, depth, 0, GalFormatGetGlFormat(format), GalFormatGetGlType(format), data.empty() ? nullptr : data.data()); break; default: throw std::runtime_error("Unknown texture type"); @@ -1136,6 +1158,7 @@ struct ImplOgl : public Impl { texture->width = texConfig->width; texture->height = texConfig->height; texture->depth = texConfig->depth; + texture->linear = texConfig->linear; GLuint newTex; glGenTextures(1, &newTex); diff --git a/src/Render.cpp b/src/Render.cpp index 682b60a..4fc0616 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -144,6 +144,7 @@ void Render::PrepareToRendering() { auto gal = Gal::GetImplementation(); gal->GetDefaultFramebuffer()->SetViewport(0, 0, width, height); + gal->GetGlobalShaderParameters()->Get()->gamma = Settings::ReadDouble("gamma", 2.2); std::string vertexSource, pixelSource; { @@ -503,7 +504,6 @@ void Render::InitEvents() { renderWorld = true; SetState(State::Playing); GetGameState()->GetPlayer()->isFlying = Settings::ReadBool("flight", false); - PUSH_EVENT("SetMinLightLevel", (float)Settings::ReadDouble("brightness", 0.2f)); }); listener.RegisterHandler("ConnectionFailed", [this](const Event& eventData) { @@ -597,8 +597,8 @@ void Render::InitEvents() { else SDL_GL_SetSwapInterval(0); - float brightness = Settings::ReadDouble("brightness", 0.2f); - PUSH_EVENT("SetMinLightLevel", brightness); + + Gal::GetImplementation()->GetGlobalShaderParameters()->Get()->gamma = Settings::ReadDouble("gamma", 2.2); PrepareToRendering(); }); diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index da7fd96..1e6a978 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -5,8 +5,12 @@ struct GlobalShaderParameters { glm::mat4 projView; glm::uvec2 viewportSize; - float globalTime; - float dayTime; + glm::float32 globalTime; + glm::float32 dayTime; + glm::float32 gamma; + glm::uint32 paddingF0 = 0xF0F0F0F0; + glm::uint32 paddingF1 = 0xF1F1F1F1; + glm::uint32 paddingF2 = 0xF2F2F2F2; }; std::shared_ptr LoadVertexShader(std::string_view assetPath); diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index c6d490a..846788b 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -256,10 +256,6 @@ RendererWorld::RendererWorld(std::shared_ptr target) { sections.erase(it); }); - listener->RegisterHandler("SetMinLightLevel", [this](const Event& eventData) { - - }); - for (int i = 0; i < numOfWorkers; i++) workers.push_back(std::thread(&RendererWorld::WorkerFunction, this, i)); diff --git a/src/TextureAtlas.cpp b/src/TextureAtlas.cpp index 7e44a86..9ad018e 100644 --- a/src/TextureAtlas.cpp +++ b/src/TextureAtlas.cpp @@ -89,6 +89,7 @@ TextureAtlas::TextureAtlas(std::vector &textures) { texConfig->SetWrapping(Gal::Wrapping::Clamp); texConfig->SetMinFilter(Gal::Filtering::Nearest); texConfig->SetMaxFilter(Gal::Filtering::Nearest); + texConfig->SetLinear(false); texture = gal->BuildTexture(texConfig); -- cgit v1.2.3 From 3f122e57f118db1229a4bad2c54be624f2f8f19c Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 5 Dec 2021 00:51:39 +0500 Subject: Added SSAO --- src/Gal.hpp | 1 + src/GalOgl.cpp | 14 +++++++++- src/Render.cpp | 18 +++++++++++++ src/RenderConfigs.cpp | 62 +++++++++++++++++++++++++++++++++++++-------- src/RenderConfigs.hpp | 14 ++++++++-- src/RendererSectionData.cpp | 2 +- src/RendererWorld.cpp | 15 +++++++---- 7 files changed, 107 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/Gal.hpp b/src/Gal.hpp index 8d7394a..76db560 100644 --- a/src/Gal.hpp +++ b/src/Gal.hpp @@ -63,6 +63,7 @@ namespace Gal { D24S8, R8G8B8, R8G8B8A8, + R32G32B32A32F, }; enum class Filtering { diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index ff43ed8..c7344fe 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -311,10 +311,14 @@ GLenum GalTypeGetComponentGlType(Gal::Type type) { size_t GalFormatGetSize(Format format) { switch (format) { + case Format::D24S8: + return 4; case Format::R8G8B8: return 3; case Format::R8G8B8A8: return 4; + case Format::R32G32B32A32F: + return 16; default: return 0; } @@ -329,6 +333,8 @@ GLenum GalFormatGetGlLinearInternalFormat(Format format) { return GL_RGB8; case Format::R8G8B8A8: return GL_RGBA8; + case Format::R32G32B32A32F: + return GL_RGBA32F; default: return 0; } @@ -338,11 +344,13 @@ GLenum GalFormatGetGlLinearInternalFormat(Format format) { GLenum GalFormatGetGlInternalFormat(Format format) { switch (format) { case Format::D24S8: - return GL_DEPTH24_STENCIL8; + return 0; case Format::R8G8B8: return GL_SRGB; case Format::R8G8B8A8: return GL_SRGB_ALPHA; + case Format::R32G32B32A32F: + return 0; default: return 0; } @@ -357,6 +365,8 @@ GLenum GalFormatGetGlFormat(Format format) { return GL_RGB; case Format::R8G8B8A8: return GL_RGBA; + case Format::R32G32B32A32F: + return GL_RGBA; default: return 0; } @@ -371,6 +381,8 @@ GLenum GalFormatGetGlType(Format format) { return GL_UNSIGNED_BYTE; case Format::R8G8B8A8: return GL_UNSIGNED_BYTE; + case Format::R32G32B32A32F: + return GL_FLOAT; default: return 0; } diff --git a/src/Render.cpp b/src/Render.cpp index 4fc0616..1095148 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -1,5 +1,7 @@ #include "Render.hpp" +#include + #include #include #include @@ -146,6 +148,22 @@ void Render::PrepareToRendering() { gal->GetGlobalShaderParameters()->Get()->gamma = Settings::ReadDouble("gamma", 2.2); + 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; + 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; + } + std::string vertexSource, pixelSource; { auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/fbo"); diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index aad2c59..d185c79 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -1,5 +1,8 @@ #include "RenderConfigs.hpp" +#include +#include + #include "AssetManager.hpp" std::string LoadShaderCode(std::string_view assetPath) { @@ -85,7 +88,7 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { colorConf->SetMaxFilter(Gal::Filtering::Bilinear); color = gal->BuildTexture(colorConf); - auto normalConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); + auto normalConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R32G32B32A32F); normalConf->SetMinFilter(Gal::Filtering::Bilinear); normalConf->SetMaxFilter(Gal::Filtering::Bilinear); normal = gal->BuildTexture(normalConf); @@ -105,30 +108,69 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { dsConf->SetMaxFilter(Gal::Filtering::Bilinear); depthStencil = gal->BuildTexture(dsConf); + auto worldPosConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R32G32B32A32F); + worldPosConf->SetMinFilter(Gal::Filtering::Bilinear); + worldPosConf->SetMaxFilter(Gal::Filtering::Bilinear); + worldPos = gal->BuildTexture(worldPosConf); + auto geomFbConf = gal->CreateFramebufferConfig(); + geomFbConf->SetDepthStencil(depthStencil); geomFbConf->SetTexture(0, color); geomFbConf->SetTexture(1, normal); - geomFbConf->SetTexture(2, addColor); - geomFbConf->SetTexture(3, light); - geomFbConf->SetDepthStencil(depthStencil); + geomFbConf->SetTexture(2, worldPos); + geomFbConf->SetTexture(3, addColor); + geomFbConf->SetTexture(4, light); geomFramebuffer = gal->BuildFramebuffer(geomFbConf); geomFramebuffer->SetViewport(0, 0, geomW, geomH); - std::vector>> lightingTexture = { - {"color", color}, - {"addColor", addColor}, + auto noiseConf = gal->CreateTexture2DConfig(4, 4, Gal::Format::R32G32B32A32F); + noiseConf->SetWrapping(Gal::Wrapping::Repeat); + noiseConf->SetMinFilter(Gal::Filtering::Bilinear); + noiseConf->SetMaxFilter(Gal::Filtering::Bilinear); + ssaoNoise = gal->BuildTexture(noiseConf); + + std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); + std::uniform_real_distribution<> dis(-1.0f, 1.0f); + std::vector noiseTexData(16); + for (auto& vec : noiseTexData) { + vec.x = dis(rng); + vec.y = dis(rng); + vec.z = 0.0f; + vec.w = 0.0f; + } + ssaoNoise->SetData({ reinterpret_cast(noiseTexData.data()), reinterpret_cast(noiseTexData.data() + noiseTexData.size()) }); + + std::vector>> ssaoTextures = { {"normal", normal}, - {"light", light}, - {"depthStencil", depthStencil}, + {"worldPos", worldPos}, + {"ssaoNoise", ssaoNoise}, }; + ssaoPass = std::make_unique(LoadPixelShader("/altcraft/shaders/frag/ssao"), + ssaoTextures, + std::vector>{}, + lightW, + lightH, + Gal::Format::R8G8B8A8, + Gal::Filtering::Bilinear); + std::vector> lightingParameters = { {"renderBuff", Gal::Type::Int32}, }; + std::vector>> lightingTextures = { + {"depthStencil", depthStencil}, + {"color", color}, + {"normal", normal}, + {"worldPos", worldPos}, + {"addColor", addColor}, + {"light", light}, + {"ssao", ssaoPass->GetResultTexture()}, + }; + lightingPass = std::make_unique(LoadPixelShader("/altcraft/shaders/frag/light"), - lightingTexture, + lightingTextures, lightingParameters, lightW, lightH, diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index 1e6a978..ef4c97d 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -4,7 +4,12 @@ struct GlobalShaderParameters { glm::mat4 projView; + glm::mat4 proj; + glm::mat4 view; glm::uvec2 viewportSize; + glm::uint32 paddingFA = 0xFAAFFAFA; + glm::uint32 paddingFB = 0xFBFBFBFB; + glm::vec4 ssaoKernels[64]; glm::float32 globalTime; glm::float32 dayTime; glm::float32 gamma; @@ -55,12 +60,15 @@ public: }; class Gbuffer { + std::shared_ptr ssaoNoise; + std::unique_ptr ssaoPass; std::unique_ptr lightingPass; + std::shared_ptr depthStencil; std::shared_ptr color; //RGB - color std::shared_ptr normal; //RGB - normal + std::shared_ptr worldPos; //RGB - viewSpaceWorldPos std::shared_ptr addColor; //RGB - addColor std::shared_ptr light; //R - faceLight, G - skyLight, B - unused - std::shared_ptr depthStencil; std::shared_ptr geomFramebuffer; public: @@ -75,16 +83,18 @@ public: } void Render() { + ssaoPass->Render(); lightingPass->Render(); } void Clear() { geomFramebuffer->Clear(); + ssaoPass->Clear(); lightingPass->Clear(); } int GetMaxRenderBuffers() { - return 5; + return 7; } void SetRenderBuff(int renderBuff) { diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 0ac901e..45d242c 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -58,7 +58,7 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co vertexData.positions[2] = transformed * glm::vec4(1, 0, 1, 1); vertexData.positions[3] = transformed * glm::vec4(1, 0, 0, 1); - vertexData.normal = normal; + vertexData.normal = model.transform * face.transform * glm::vec4(normal, 1.0f); vertexData.uvs[0] = TransformTextureCoord(face.texture, glm::vec2(0, 0), face.frames); vertexData.uvs[1] = TransformTextureCoord(face.texture, glm::vec2(1, 0), face.frames); diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 846788b..2108ba6 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -278,15 +278,20 @@ RendererWorld::~RendererWorld() { void RendererWorld::Render(float screenRatio) { OPTICK_EVENT(); //Common - glm::mat4 projection = glm::perspective( + + auto globalSpb = Gal::GetImplementation()->GetGlobalShaderParameters(); + + auto& projection = globalSpb->Get()->proj; + projection = glm::perspective( glm::radians(70.0f), screenRatio, 0.1f, 10000000.0f ); - glm::mat4 view = GetGameState()->GetViewMatrix(); - glm::mat4 projView = projection * view; - auto globalSpb = Gal::GetImplementation()->GetGlobalShaderParameters(); - globalSpb->Get()->projView = projView; + auto& view = globalSpb->Get()->view; + view = GetGameState()->GetViewMatrix(); + + auto& projView = globalSpb->Get()->projView; + projView = projection * view; //Render Entities constexpr size_t entitiesVerticesCount = 240; -- cgit v1.2.3 From 93a5385cb03482d09108edef1433a9f470bf1e4a Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 5 Dec 2021 04:26:50 +0500 Subject: Added SSAO blur --- src/RenderConfigs.cpp | 20 +++++++++++++++++++- src/RenderConfigs.hpp | 3 +++ 2 files changed, 22 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index d185c79..88166a5 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -155,6 +155,24 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { Gal::Format::R8G8B8A8, Gal::Filtering::Bilinear); + std::vector>> ssaoBlurTextures = { + {"blurInput", ssaoPass->GetResultTexture()}, + }; + + std::vector> ssaoBlurParameters = { + {"blurScale", Gal::Type::Int32}, + }; + + ssaoBlurPass = std::make_unique(LoadPixelShader("/altcraft/shaders/frag/blur"), + ssaoBlurTextures, + ssaoBlurParameters, + lightW, + lightH, + Gal::Format::R8G8B8A8, + Gal::Filtering::Bilinear); + + ssaoBlurPass->SetShaderParameter("blurScale", 2); + std::vector> lightingParameters = { {"renderBuff", Gal::Type::Int32}, }; @@ -166,7 +184,7 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { {"worldPos", worldPos}, {"addColor", addColor}, {"light", light}, - {"ssao", ssaoPass->GetResultTexture()}, + {"ssao", ssaoBlurPass->GetResultTexture()}, }; lightingPass = std::make_unique(LoadPixelShader("/altcraft/shaders/frag/light"), diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index ef4c97d..0667bbe 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -62,6 +62,7 @@ public: class Gbuffer { std::shared_ptr ssaoNoise; std::unique_ptr ssaoPass; + std::unique_ptr ssaoBlurPass; std::unique_ptr lightingPass; std::shared_ptr depthStencil; std::shared_ptr color; //RGB - color @@ -84,12 +85,14 @@ public: void Render() { ssaoPass->Render(); + ssaoBlurPass->Render(); lightingPass->Render(); } void Clear() { geomFramebuffer->Clear(); ssaoPass->Clear(); + ssaoBlurPass->Clear(); lightingPass->Clear(); } -- cgit v1.2.3 From f01c1c3704d277a3fe8286f591fef6693d9e089c Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 5 Dec 2021 04:50:13 +0500 Subject: Added Ambient occlusion options parameter --- src/Render.cpp | 2 +- src/RenderConfigs.cpp | 105 +++++++++++++++++++++++++++----------------------- src/RenderConfigs.hpp | 14 ++++--- 3 files changed, 66 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/Render.cpp b/src/Render.cpp index 1095148..1e85437 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -140,7 +140,7 @@ void Render::PrepareToRendering() { float resolutionScale = Settings::ReadDouble("resolutionScale", 1.0f); size_t scaledW = width * resolutionScale, scaledH = height * resolutionScale; - gbuffer = std::make_unique(scaledW, scaledH, scaledW, scaledH); + gbuffer = std::make_unique(scaledW, scaledH, scaledW, scaledH, Settings::ReadBool("ssao", false)); gbuffer->SetRenderBuff(renderBuff); auto gal = Gal::GetImplementation(); diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index 88166a5..0a5450b 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -80,7 +80,7 @@ PostProcess::PostProcess( }); } -Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { +Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, bool applySsao) { auto gal = Gal::GetImplementation(); auto colorConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); @@ -124,57 +124,60 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { geomFramebuffer = gal->BuildFramebuffer(geomFbConf); geomFramebuffer->SetViewport(0, 0, geomW, geomH); - auto noiseConf = gal->CreateTexture2DConfig(4, 4, Gal::Format::R32G32B32A32F); - noiseConf->SetWrapping(Gal::Wrapping::Repeat); - noiseConf->SetMinFilter(Gal::Filtering::Bilinear); - noiseConf->SetMaxFilter(Gal::Filtering::Bilinear); - ssaoNoise = gal->BuildTexture(noiseConf); - - std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); - std::uniform_real_distribution<> dis(-1.0f, 1.0f); - std::vector noiseTexData(16); - for (auto& vec : noiseTexData) { - vec.x = dis(rng); - vec.y = dis(rng); - vec.z = 0.0f; - vec.w = 0.0f; + if (applySsao) { + auto noiseConf = gal->CreateTexture2DConfig(4, 4, Gal::Format::R32G32B32A32F); + noiseConf->SetWrapping(Gal::Wrapping::Repeat); + noiseConf->SetMinFilter(Gal::Filtering::Bilinear); + noiseConf->SetMaxFilter(Gal::Filtering::Bilinear); + ssaoNoise = gal->BuildTexture(noiseConf); + + std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); + std::uniform_real_distribution<> dis(-1.0f, 1.0f); + std::vector noiseTexData(16); + for (auto& vec : noiseTexData) { + vec.x = dis(rng); + vec.y = dis(rng); + vec.z = 0.0f; + vec.w = 0.0f; + } + ssaoNoise->SetData({ reinterpret_cast(noiseTexData.data()), reinterpret_cast(noiseTexData.data() + noiseTexData.size()) }); + + std::vector>> ssaoTextures = { + {"normal", normal}, + {"worldPos", worldPos}, + {"ssaoNoise", ssaoNoise}, + }; + + ssaoPass = std::make_unique(LoadPixelShader("/altcraft/shaders/frag/ssao"), + ssaoTextures, + std::vector>{}, + lightW, + lightH, + Gal::Format::R8G8B8A8, + Gal::Filtering::Bilinear); + + std::vector>> ssaoBlurTextures = { + {"blurInput", ssaoPass->GetResultTexture()}, + }; + + std::vector> ssaoBlurParameters = { + {"blurScale", Gal::Type::Int32}, + }; + + ssaoBlurPass = std::make_unique(LoadPixelShader("/altcraft/shaders/frag/blur"), + ssaoBlurTextures, + ssaoBlurParameters, + lightW, + lightH, + Gal::Format::R8G8B8A8, + Gal::Filtering::Bilinear); + + ssaoBlurPass->SetShaderParameter("blurScale", 2); } - ssaoNoise->SetData({ reinterpret_cast(noiseTexData.data()), reinterpret_cast(noiseTexData.data() + noiseTexData.size()) }); - - std::vector>> ssaoTextures = { - {"normal", normal}, - {"worldPos", worldPos}, - {"ssaoNoise", ssaoNoise}, - }; - - ssaoPass = std::make_unique(LoadPixelShader("/altcraft/shaders/frag/ssao"), - ssaoTextures, - std::vector>{}, - lightW, - lightH, - Gal::Format::R8G8B8A8, - Gal::Filtering::Bilinear); - - std::vector>> ssaoBlurTextures = { - {"blurInput", ssaoPass->GetResultTexture()}, - }; - - std::vector> ssaoBlurParameters = { - {"blurScale", Gal::Type::Int32}, - }; - - ssaoBlurPass = std::make_unique(LoadPixelShader("/altcraft/shaders/frag/blur"), - ssaoBlurTextures, - ssaoBlurParameters, - lightW, - lightH, - Gal::Format::R8G8B8A8, - Gal::Filtering::Bilinear); - - ssaoBlurPass->SetShaderParameter("blurScale", 2); std::vector> lightingParameters = { {"renderBuff", Gal::Type::Int32}, + {"applySsao", Gal::Type::Int32}, }; std::vector>> lightingTextures = { @@ -184,9 +187,11 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { {"worldPos", worldPos}, {"addColor", addColor}, {"light", light}, - {"ssao", ssaoBlurPass->GetResultTexture()}, }; + if (applySsao) + lightingTextures.emplace_back("ssao", ssaoBlurPass->GetResultTexture()); + lightingPass = std::make_unique(LoadPixelShader("/altcraft/shaders/frag/light"), lightingTextures, lightingParameters, @@ -194,4 +199,6 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) { lightH, Gal::Format::R8G8B8A8, Gal::Filtering::Bilinear); + + lightingPass->SetShaderParameter("applySsao", true); } diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index 0667bbe..a9b8d72 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -73,7 +73,7 @@ class Gbuffer { std::shared_ptr geomFramebuffer; public: - Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH); + Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, bool applySsao); std::shared_ptr GetGeometryTarget() { return geomFramebuffer; @@ -84,15 +84,19 @@ public: } void Render() { - ssaoPass->Render(); - ssaoBlurPass->Render(); + if (ssaoPass) { + ssaoPass->Render(); + ssaoBlurPass->Render(); + } lightingPass->Render(); } void Clear() { geomFramebuffer->Clear(); - ssaoPass->Clear(); - ssaoBlurPass->Clear(); + if (ssaoPass) { + ssaoPass->Clear(); + ssaoBlurPass->Clear(); + } lightingPass->Clear(); } -- cgit v1.2.3 From da66c30a110233f7c8b71b5e6aa8dd804879c1b6 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 5 Dec 2021 05:42:15 +0500 Subject: Added blending --- src/Gal.hpp | 7 +++++++ src/GalOgl.cpp | 21 ++++++++++++++++++++- src/Rml.cpp | 2 ++ 3 files changed, 29 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/Gal.hpp b/src/Gal.hpp index 76db560..fe99dc7 100644 --- a/src/Gal.hpp +++ b/src/Gal.hpp @@ -86,6 +86,11 @@ namespace Gal { TriangleFan, }; + enum class Blending { + Opaque, + Additive, + }; + struct VertexAttribute { std::string name; Type type; @@ -186,6 +191,8 @@ namespace Gal { virtual void SetPrimitive(Primitive primitive) = 0; + virtual void SetBlending(Blending blendingMode) = 0; + virtual std::shared_ptr BindVertexBuffer(std::vector &&bufferLayout) = 0; virtual std::shared_ptr BindIndexBuffer() = 0; diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index c7344fe..4ac6c84 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -89,6 +89,7 @@ class OglState { GLuint activeTextureUnit = 0; GLint vpX = 0, vpY = 0; GLsizei vpW = 0, vpH = 0; + bool blending = false; public: @@ -162,6 +163,16 @@ public: glCheckError(); } + void EnableBlending(bool enable) { + if (enable != blending) { + blending = enable; + if (blending) + glEnable(GL_BLEND); + else + glDisable(GL_BLEND); + } + } + } oglState; std::unique_ptr impl; @@ -791,6 +802,7 @@ struct PipelineConfigOgl : public PipelineConfig { std::shared_ptr targetFb; std::vector> vertexBuffers; Primitive vertexPrimitive = Primitive::Triangle; + Blending blending = Blending::Opaque; virtual void SetVertexShader(std::shared_ptr shader) override { vertexShader = std::static_pointer_cast(shader); @@ -818,6 +830,10 @@ struct PipelineConfigOgl : public PipelineConfig { vertexPrimitive = primitive; } + virtual void SetBlending(Blending blendingMode) override { + blending = blendingMode; + } + virtual std::shared_ptr BindVertexBuffer(std::vector &&bufferLayout) override { auto binding = std::make_shared(vertexBuffers.size()); vertexBuffers.push_back(bufferLayout); @@ -901,12 +917,14 @@ struct PipelineOgl : public Pipeline { }; std::vector vertexBindCmds; Primitive primitive; + Blending blending; std::shared_ptr target; virtual void Activate() override { oglState.UseProgram(program); oglState.BindFbo(target->fbo); oglState.SetViewport(target->vpX, target->vpY, target->vpW, target->vpH); + oglState.EnableBlending(blending == Blending::Additive); if (target->fbo) glDrawBuffers(target->attachments.size(), target->attachments.data()); @@ -1076,7 +1094,7 @@ struct ImplOgl : public Impl { glCullFace(GL_BACK); glFrontFace(GL_CCW); - glEnable(GL_BLEND); + oglState.EnableBlending(true); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glCheckError(); if (glActiveTexture == nullptr) { @@ -1201,6 +1219,7 @@ struct ImplOgl : public Impl { auto config = std::static_pointer_cast(pipelineConfig); pipeline->primitive = config->vertexPrimitive; + pipeline->blending = config->blending; pipeline->target = config->targetFb; if (!pipeline->target) diff --git a/src/Rml.cpp b/src/Rml.cpp index 746f6a4..f6fff44 100644 --- a/src/Rml.cpp +++ b/src/Rml.cpp @@ -68,6 +68,7 @@ RmlRenderInterface::RmlRenderInterface() { pipelineConfig->SetTarget(gal->GetDefaultFramebuffer()); pipelineConfig->SetVertexShader(gal->LoadVertexShader(vertexSource)); pipelineConfig->SetPixelShader(gal->LoadPixelShader(pixelSource)); + pipelineConfig->SetBlending(Gal::Blending::Additive); auto vertBuffBind = pipelineConfig->BindVertexBuffer({ {"pos", Gal::Type::Vec2}, @@ -92,6 +93,7 @@ RmlRenderInterface::RmlRenderInterface() { texPipelineConfig->SetTarget(gal->GetDefaultFramebuffer()); texPipelineConfig->SetVertexShader(gal->LoadVertexShader(vertexSource)); texPipelineConfig->SetPixelShader(gal->LoadPixelShader(texPixelSource)); + texPipelineConfig->SetBlending(Gal::Blending::Additive); auto texVertBuffBind = texPipelineConfig->BindVertexBuffer({ {"pos", Gal::Type::Vec2}, -- cgit v1.2.3 From 9ffe5b9c171e01a4319e6329bb21e6c05ac3e2b4 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 5 Dec 2021 06:44:37 +0500 Subject: Minor shader optimization --- src/AssetManager.cpp | 2 +- src/RenderConfigs.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 0976e30..ca68304 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -557,7 +557,7 @@ void ParseBlockModels() { if (face.second.tintIndex) parsedFace.color = glm::vec3(0.486, 0.745, 0.423); else - parsedFace.color = glm::vec3(0, 0, 0); + parsedFace.color = glm::vec3(1.0f); model.parsedFaces.push_back(parsedFace); } diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index 0a5450b..3d91a1d 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -200,5 +200,5 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, bool Gal::Format::R8G8B8A8, Gal::Filtering::Bilinear); - lightingPass->SetShaderParameter("applySsao", true); + lightingPass->SetShaderParameter("applySsao", applySsao); } -- cgit v1.2.3 From aa591c73c818d7f26caa15bc567929e2d404dcf2 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Mon, 6 Dec 2021 05:39:15 +0500 Subject: Added optional forward rendering --- src/Render.cpp | 83 +++++++++++++++++++++++++++++++++++---------------- src/Render.hpp | 3 ++ src/RendererWorld.cpp | 15 ++++++---- src/RendererWorld.hpp | 4 +-- 4 files changed, 72 insertions(+), 33 deletions(-) (limited to 'src') 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(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()->gamma = Settings::ReadDouble("gamma", 2.2); - 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; - 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(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 dis(-1.0f, 1.0f); + auto& ssaoKernels = gal->GetGlobalShaderParameters()->Get()->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(gbuffer->GetGeometryTarget()); + world = std::make_unique( + 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 fbPipeline; std::shared_ptr fbPipelineInstance; std::shared_ptr fbBuffer; + std::shared_ptr fbTarget; + std::shared_ptr fbTextureColor; + std::shared_ptr fbTextureDepthStencil; std::unique_ptr 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 target) { +RendererWorld::RendererWorld(std::shared_ptr 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 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 target) { +void RendererWorld::PrepareRender(std::shared_ptr 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 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 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 skyPipelineInstance; std::shared_ptr skyBuffer; public: - RendererWorld(std::shared_ptr target); + RendererWorld(std::shared_ptr target, bool defferedShading); ~RendererWorld(); void Render(float screenRatio); - void PrepareRender(std::shared_ptr target); + void PrepareRender(std::shared_ptr target, bool defferedShading); double MaxRenderingDistance; -- cgit v1.2.3 From a27bfe76545822eb4ef2798c8200096ec9b6963c Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Tue, 7 Dec 2021 16:38:36 +0500 Subject: Fixed GalOgl OglState dangling resource --- src/GalOgl.cpp | 162 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 98 insertions(+), 64 deletions(-) (limited to 'src') diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index 4ac6c84..6452a44 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -7,70 +7,6 @@ #include "Utility.hpp" -enum class GlResourceType { - Vbo, - Vao, - Texture, - Fbo, - Program, - None, -}; - -class GlResource { - GlResourceType type = GlResourceType::None; - GLuint res = 0; -public: - GlResource() = default; - - GlResource(GLuint resource, GlResourceType resType) noexcept : res(resource), type(resType) {} - - GlResource(const GlResource&) = delete; - - GlResource(GlResource&& rhs) noexcept { - std::swap(this->res, rhs.res); - std::swap(this->type, rhs.type); - } - - GlResource& operator=(const GlResource&) = delete; - - GlResource& operator=(GlResource&& rhs) noexcept { - std::swap(this->res, rhs.res); - std::swap(this->type, rhs.type); - return *this; - } - - ~GlResource() { - switch (type) { - case GlResourceType::Vbo: - glDeleteBuffers(1, &res); - break; - case GlResourceType::Vao: - glDeleteVertexArrays(1, &res); - break; - case GlResourceType::Texture: - glDeleteTextures(1, &res); - break; - case GlResourceType::Fbo: - glDeleteFramebuffers(1, &res); - break; - case GlResourceType::Program: - glDeleteProgram(res); - break; - case GlResourceType::None: - default: - break; - } - } - - operator GLuint() const noexcept { - return res; - } - - GLuint Get() const noexcept { - return res; - } -}; - using namespace Gal; @@ -173,8 +109,106 @@ public: } } + void ReleaseFbo(GLuint vao) { + if (activeVao == vao) + activeVao = 0; + } + + void ReleaseVao(GLuint fbo) { + if (activeFbo == fbo) + activeEbo = 0; + } + + void ReleaseVbo(GLuint vbo) { + if (activeVbo == vbo) + activeVbo = 0; + if (activeEbo == vbo) + activeEbo = 0; + } + + void ReleaseProgram(GLuint program) { + if (activeProgram == program) + activeProgram = 0; + } + + void ReleaseTexture(GLuint texture) { + for (auto& activeTex : activeTexture) { + if (activeTex == texture) + activeTex = 0; + } + } + } oglState; +enum class GlResourceType { + Vbo, + Vao, + Texture, + Fbo, + Program, + None, +}; + +class GlResource { + GlResourceType type = GlResourceType::None; + GLuint res = 0; +public: + GlResource() = default; + + GlResource(GLuint resource, GlResourceType resType) noexcept : res(resource), type(resType) {} + + GlResource(const GlResource&) = delete; + + GlResource(GlResource&& rhs) noexcept { + std::swap(this->res, rhs.res); + std::swap(this->type, rhs.type); + } + + GlResource& operator=(const GlResource&) = delete; + + GlResource& operator=(GlResource&& rhs) noexcept { + std::swap(this->res, rhs.res); + std::swap(this->type, rhs.type); + return *this; + } + + ~GlResource() { + switch (type) { + case GlResourceType::Vbo: + oglState.ReleaseVbo(res); + glDeleteBuffers(1, &res); + break; + case GlResourceType::Vao: + oglState.ReleaseVao(res); + glDeleteVertexArrays(1, &res); + break; + case GlResourceType::Texture: + oglState.ReleaseTexture(res); + glDeleteTextures(1, &res); + break; + case GlResourceType::Fbo: + oglState.ReleaseFbo(res); + glDeleteFramebuffers(1, &res); + break; + case GlResourceType::Program: + oglState.ReleaseProgram(res); + glDeleteProgram(res); + break; + case GlResourceType::None: + default: + break; + } + } + + operator GLuint() const noexcept { + return res; + } + + GLuint Get() const noexcept { + return res; + } +}; + std::unique_ptr impl; std::shared_ptr fbDefault; std::shared_ptr spbDefault; -- cgit v1.2.3 From 91afb55a58c4b27d0c746e64305dd1d9ad816d94 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Tue, 7 Dec 2021 21:53:02 +0500 Subject: Fixed SSAO normals --- src/RendererSectionData.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 45d242c..94c802d 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include "World.hpp" @@ -25,6 +26,8 @@ glm::vec2 TransformTextureCoord(glm::vec4 TextureAtlasCoords, glm::vec2 UvCoords } void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, const glm::mat4 &transform, bool visibility[FaceDirection::none], BlockLightness light, BlockLightness skyLight) { + glm::mat3 normalMat = glm::mat3(glm::inverseTranspose(transform * model.transform)); + for (const auto &face : model.faces) { glm::vec3 normal = {}; glm::vec2 lightness; @@ -58,7 +61,7 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co vertexData.positions[2] = transformed * glm::vec4(1, 0, 1, 1); vertexData.positions[3] = transformed * glm::vec4(1, 0, 0, 1); - vertexData.normal = model.transform * face.transform * glm::vec4(normal, 1.0f); + vertexData.normal = glm::normalize(normalMat * glm::vec4(normal, 1.0f)); vertexData.uvs[0] = TransformTextureCoord(face.texture, glm::vec2(0, 0), face.frames); vertexData.uvs[1] = TransformTextureCoord(face.texture, glm::vec2(1, 0), face.frames); -- cgit v1.2.3 From 039f05be5a548a9b5051b988869f984d7e335a47 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Wed, 8 Dec 2021 01:55:37 +0500 Subject: Added more SSAO settings --- src/Render.cpp | 6 +++++- src/RenderConfigs.cpp | 24 +++++++++++++++--------- src/RenderConfigs.hpp | 2 +- 3 files changed, 21 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/Render.cpp b/src/Render.cpp index 35ee695..be7e2b2 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -152,7 +152,11 @@ void Render::PrepareToRendering() { bool useDeffered = Settings::ReadBool("deffered", false); if (useDeffered) { - gbuffer = std::make_unique(scaledW, scaledH, scaledW, scaledH, Settings::ReadBool("ssao", false)); + 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()); diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index 3d91a1d..56c24bf 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -80,7 +80,7 @@ PostProcess::PostProcess( }); } -Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, bool applySsao) { +Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int ssaoSamples, size_t ssaoW, size_t ssaoH) { auto gal = Gal::GetImplementation(); auto colorConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); @@ -124,7 +124,7 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, bool geomFramebuffer = gal->BuildFramebuffer(geomFbConf); geomFramebuffer->SetViewport(0, 0, geomW, geomH); - if (applySsao) { + if (ssaoSamples > 0) { auto noiseConf = gal->CreateTexture2DConfig(4, 4, Gal::Format::R32G32B32A32F); noiseConf->SetWrapping(Gal::Wrapping::Repeat); noiseConf->SetMinFilter(Gal::Filtering::Bilinear); @@ -148,14 +148,20 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, bool {"ssaoNoise", ssaoNoise}, }; + std::vector> ssaoParameters = { + {"ssaoSamples", Gal::Type::Int32}, + }; + ssaoPass = std::make_unique(LoadPixelShader("/altcraft/shaders/frag/ssao"), ssaoTextures, - std::vector>{}, - lightW, - lightH, + ssaoParameters, + ssaoW, + ssaoH, Gal::Format::R8G8B8A8, Gal::Filtering::Bilinear); + ssaoPass->SetShaderParameter("ssaoSamples", ssaoSamples); + std::vector>> ssaoBlurTextures = { {"blurInput", ssaoPass->GetResultTexture()}, }; @@ -167,8 +173,8 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, bool ssaoBlurPass = std::make_unique(LoadPixelShader("/altcraft/shaders/frag/blur"), ssaoBlurTextures, ssaoBlurParameters, - lightW, - lightH, + ssaoW, + ssaoH, Gal::Format::R8G8B8A8, Gal::Filtering::Bilinear); @@ -189,7 +195,7 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, bool {"light", light}, }; - if (applySsao) + if (ssaoSamples > 0) lightingTextures.emplace_back("ssao", ssaoBlurPass->GetResultTexture()); lightingPass = std::make_unique(LoadPixelShader("/altcraft/shaders/frag/light"), @@ -200,5 +206,5 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, bool Gal::Format::R8G8B8A8, Gal::Filtering::Bilinear); - lightingPass->SetShaderParameter("applySsao", applySsao); + lightingPass->SetShaderParameter("applySsao", ssaoSamples); } diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index a9b8d72..d76647e 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -73,7 +73,7 @@ class Gbuffer { std::shared_ptr geomFramebuffer; public: - Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, bool applySsao); + Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int ssaoSamples, size_t ssaoW, size_t ssaoH); std::shared_ptr GetGeometryTarget() { return geomFramebuffer; -- cgit v1.2.3 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 --- src/Gal.hpp | 2 + src/GalOgl.cpp | 4 ++ src/Render.cpp | 121 ++++++++++++++++++++++---------------------------- src/Render.hpp | 8 ++-- src/RenderConfigs.cpp | 93 +++++++++++++++++++++++++++++++++++++- src/RenderConfigs.hpp | 28 ++++++++++++ 6 files changed, 183 insertions(+), 73 deletions(-) (limited to 'src') 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 From be808181b2c84358b232e4a32d1a6dd9dd6659ed Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sat, 11 Dec 2021 20:44:40 +0500 Subject: Optimized GBuffer size --- src/RenderConfigs.cpp | 13 +++---------- src/RenderConfigs.hpp | 2 +- src/RendererWorld.cpp | 2 ++ 3 files changed, 6 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index af1cde9..d399818 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -198,18 +198,12 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s dsConf->SetMaxFilter(Gal::Filtering::Bilinear); depthStencil = gal->BuildTexture(dsConf); - auto worldPosConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R32G32B32A32F); - worldPosConf->SetMinFilter(Gal::Filtering::Bilinear); - worldPosConf->SetMaxFilter(Gal::Filtering::Bilinear); - worldPos = gal->BuildTexture(worldPosConf); - auto geomFbConf = gal->CreateFramebufferConfig(); geomFbConf->SetDepthStencil(depthStencil); geomFbConf->SetTexture(0, color); geomFbConf->SetTexture(1, normal); - geomFbConf->SetTexture(2, worldPos); - geomFbConf->SetTexture(3, addColor); - geomFbConf->SetTexture(4, light); + geomFbConf->SetTexture(2, addColor); + geomFbConf->SetTexture(3, light); geomFramebuffer = gal->BuildFramebuffer(geomFbConf); geomFramebuffer->SetViewport(0, 0, geomW, geomH); @@ -234,7 +228,7 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s std::vector>> ssaoTextures = { {"normal", normal}, - {"worldPos", worldPos}, + {"depthStencil", depthStencil}, {"ssaoNoise", ssaoNoise}, }; @@ -280,7 +274,6 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s {"depthStencil", depthStencil}, {"color", color}, {"normal", normal}, - {"worldPos", worldPos}, {"addColor", addColor}, {"light", light}, }; diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index 8ca5a9e..92ce90c 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -5,6 +5,7 @@ struct GlobalShaderParameters { glm::mat4 projView; glm::mat4 proj; + glm::mat4 invProj; glm::mat4 view; glm::uvec2 viewportSize; glm::uint32 paddingFA = 0xFAAFFAFA; @@ -95,7 +96,6 @@ class Gbuffer { std::shared_ptr depthStencil; std::shared_ptr color; //RGB - color std::shared_ptr normal; //RGB - normal - std::shared_ptr worldPos; //RGB - viewSpaceWorldPos std::shared_ptr addColor; //RGB - addColor std::shared_ptr light; //R - faceLight, G - skyLight, B - unused std::shared_ptr geomFramebuffer; diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index b5120c2..b6e504d 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -287,6 +287,8 @@ void RendererWorld::Render(float screenRatio) { 0.1f, 10000000.0f ); + globalSpb->Get()->invProj = glm::inverse(projection); + auto& view = globalSpb->Get()->view; view = GetGameState()->GetViewMatrix(); -- cgit v1.2.3 From 5721d60db866373dc5718a8af8e63e5377ffd927 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sat, 11 Dec 2021 20:56:17 +0500 Subject: More optimization to GBuffer size --- src/RenderConfigs.cpp | 9 +-------- src/RenderConfigs.hpp | 1 - 2 files changed, 1 insertion(+), 9 deletions(-) (limited to 'src') diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index d399818..8cc4ee1 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -183,11 +183,6 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s normalConf->SetMaxFilter(Gal::Filtering::Bilinear); normal = gal->BuildTexture(normalConf); - auto addColorConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); - addColorConf->SetMinFilter(Gal::Filtering::Bilinear); - addColorConf->SetMaxFilter(Gal::Filtering::Bilinear); - addColor = gal->BuildTexture(addColorConf); - auto lightConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); lightConf->SetMinFilter(Gal::Filtering::Bilinear); lightConf->SetMaxFilter(Gal::Filtering::Bilinear); @@ -202,8 +197,7 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s geomFbConf->SetDepthStencil(depthStencil); geomFbConf->SetTexture(0, color); geomFbConf->SetTexture(1, normal); - geomFbConf->SetTexture(2, addColor); - geomFbConf->SetTexture(3, light); + geomFbConf->SetTexture(2, light); geomFramebuffer = gal->BuildFramebuffer(geomFbConf); geomFramebuffer->SetViewport(0, 0, geomW, geomH); @@ -274,7 +268,6 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s {"depthStencil", depthStencil}, {"color", color}, {"normal", normal}, - {"addColor", addColor}, {"light", light}, }; diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index 92ce90c..d0adfce 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -96,7 +96,6 @@ class Gbuffer { std::shared_ptr depthStencil; std::shared_ptr color; //RGB - color std::shared_ptr normal; //RGB - normal - std::shared_ptr addColor; //RGB - addColor std::shared_ptr light; //R - faceLight, G - skyLight, B - unused std::shared_ptr geomFramebuffer; -- cgit v1.2.3 From 9202a3cab793a239a084414e4dddc7759f479fc2 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sat, 11 Dec 2021 22:11:31 +0500 Subject: Even more optimization to GBuffer size --- src/Gal.hpp | 3 +++ src/GalOgl.cpp | 30 ++++++++++++++++++++++++++++++ src/RenderConfigs.cpp | 19 +++++++++---------- 3 files changed, 42 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/Gal.hpp b/src/Gal.hpp index 0aa61c7..ed0b559 100644 --- a/src/Gal.hpp +++ b/src/Gal.hpp @@ -61,7 +61,10 @@ namespace Gal { enum class Format { D24S8, + R8, + R8G8, R8G8B8, + R8G8B8SN, R8G8B8A8, R32G32B32A32F, }; diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index 94c2146..b046d4b 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -358,8 +358,14 @@ size_t GalFormatGetSize(Format format) { switch (format) { case Format::D24S8: return 4; + case Format::R8: + return 1; + case Format::R8G8: + return 2; case Format::R8G8B8: return 3; + case Format::R8G8B8SN: + return 3; case Format::R8G8B8A8: return 4; case Format::R32G32B32A32F: @@ -374,8 +380,14 @@ GLenum GalFormatGetGlLinearInternalFormat(Format format) { switch (format) { case Format::D24S8: return GL_DEPTH24_STENCIL8; + case Format::R8: + return GL_R8; + case Format::R8G8: + return GL_RG8; case Format::R8G8B8: return GL_RGB8; + case Format::R8G8B8SN: + return GL_RGB8_SNORM; case Format::R8G8B8A8: return GL_RGBA8; case Format::R32G32B32A32F: @@ -390,8 +402,14 @@ GLenum GalFormatGetGlInternalFormat(Format format) { switch (format) { case Format::D24S8: return 0; + case Format::R8: + return 0; + case Format::R8G8: + return 0; case Format::R8G8B8: return GL_SRGB; + case Format::R8G8B8SN: + return 0; case Format::R8G8B8A8: return GL_SRGB_ALPHA; case Format::R32G32B32A32F: @@ -406,8 +424,14 @@ GLenum GalFormatGetGlFormat(Format format) { switch (format) { case Format::D24S8: return GL_DEPTH_STENCIL; + case Format::R8: + return GL_RED; + case Format::R8G8: + return GL_RG; case Format::R8G8B8: return GL_RGB; + case Format::R8G8B8SN: + return GL_RGB; case Format::R8G8B8A8: return GL_RGBA; case Format::R32G32B32A32F: @@ -422,8 +446,14 @@ GLenum GalFormatGetGlType(Format format) { switch (format) { case Format::D24S8: return GL_UNSIGNED_INT_24_8; + case Format::R8: + return GL_UNSIGNED_BYTE; + case Format::R8G8: + return GL_UNSIGNED_BYTE; case Format::R8G8B8: return GL_UNSIGNED_BYTE; + case Format::R8G8B8SN: + return GL_BYTE; case Format::R8G8B8A8: return GL_UNSIGNED_BYTE; case Format::R32G32B32A32F: diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index 8cc4ee1..43342bb 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -178,12 +178,12 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s colorConf->SetMaxFilter(Gal::Filtering::Bilinear); color = gal->BuildTexture(colorConf); - auto normalConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R32G32B32A32F); + auto normalConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8SN); normalConf->SetMinFilter(Gal::Filtering::Bilinear); normalConf->SetMaxFilter(Gal::Filtering::Bilinear); normal = gal->BuildTexture(normalConf); - auto lightConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); + auto lightConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8); lightConf->SetMinFilter(Gal::Filtering::Bilinear); lightConf->SetMaxFilter(Gal::Filtering::Bilinear); light = gal->BuildTexture(lightConf); @@ -203,7 +203,7 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s geomFramebuffer->SetViewport(0, 0, geomW, geomH); if (ssaoSamples > 0) { - auto noiseConf = gal->CreateTexture2DConfig(4, 4, Gal::Format::R32G32B32A32F); + auto noiseConf = gal->CreateTexture2DConfig(4, 4, Gal::Format::R8G8B8SN); noiseConf->SetWrapping(Gal::Wrapping::Repeat); noiseConf->SetMinFilter(Gal::Filtering::Bilinear); noiseConf->SetMaxFilter(Gal::Filtering::Bilinear); @@ -211,12 +211,11 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); std::uniform_real_distribution<> dis(-1.0f, 1.0f); - std::vector noiseTexData(16); + std::vector noiseTexData(16); for (auto& vec : noiseTexData) { - vec.x = dis(rng); - vec.y = dis(rng); + vec.x = static_cast(dis(rng) * 128.0f); + vec.y = static_cast(dis(rng) * 128.0f); vec.z = 0.0f; - vec.w = 0.0f; } ssaoNoise->SetData({ reinterpret_cast(noiseTexData.data()), reinterpret_cast(noiseTexData.data() + noiseTexData.size()) }); @@ -235,7 +234,7 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s ssaoParameters, ssaoW, ssaoH, - Gal::Format::R8G8B8A8, + Gal::Format::R8, Gal::Filtering::Bilinear); ssaoPass->SetShaderParameter("ssaoSamples", ssaoSamples); @@ -253,7 +252,7 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s ssaoBlurParameters, ssaoW, ssaoH, - Gal::Format::R8G8B8A8, + Gal::Format::R8, Gal::Filtering::Bilinear); ssaoBlurPass->SetShaderParameter("blurScale", 2); @@ -279,7 +278,7 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s lightingParameters, lightW, lightH, - Gal::Format::R8G8B8A8, + Gal::Format::R8G8B8, Gal::Filtering::Bilinear); lightingPass->SetShaderParameter("applySsao", ssaoSamples); -- cgit v1.2.3 From 3bee20454e982423c098ffa17ab705ca56bda92b Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 12 Dec 2021 00:48:35 +0500 Subject: Optimized fragment shader quads overdraw --- src/RendererWorld.cpp | 69 ++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index b6e504d..7cbd45e 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include "DebugInfo.hpp" @@ -336,7 +337,43 @@ void RendererWorld::Render(float screenRatio) { } } - //Render sky + //Render sections + auto rawGlobalTime = (std::chrono::high_resolution_clock::now() - globalTimeStart); + float globalTime = rawGlobalTime.count() / 1000000000.0f; + globalSpb->Get()->globalTime = globalTime; + sectionsPipeline->Activate(); + + Frustum frustum(projView); + renderList.clear(); + size_t culledSections = sections.size(); + unsigned int renderedFaces = 0; + for (auto& section : sections) { + glm::vec3 point{ + section.second.GetPosition().x * 16 + 8, + section.second.GetPosition().y * 16 + 8, + section.second.GetPosition().z * 16 + 8 + }; + + bool isVisible = frustum.TestSphere(point, 16.0f); + + if (!isVisible) { + culledSections--; + continue; + } + renderList.push_back(section.first); + renderedFaces += section.second.numOfFaces; + } + glm::vec3 playerChunk(GetGameState()->GetPlayer()->pos / 16); + std::sort(renderList.begin(), renderList.end(), [playerChunk](const Vector& lhs, const Vector& rhs) { + return glm::distance2(lhs.glm(), playerChunk) < glm::distance2(rhs.glm(), playerChunk); + }); + for (const auto& renderPos : renderList) { + sections.at(renderPos).Render(); + } + DebugInfo::culledSections = culledSections; + DebugInfo::renderFaces = renderedFaces; + + //Render sky glm::mat4 model = glm::mat4(1.0); model = glm::translate(model, GetGameState()->GetPlayer()->pos.glm()); const float scale = 1000000.0f; @@ -377,36 +414,6 @@ void RendererWorld::Render(float screenRatio) { skyPipeline->SetShaderParameter("model", model); skyPipelineInstance->Activate(); skyPipelineInstance->Render(0, 36); - - - //Render sections - auto rawGlobalTime = (std::chrono::high_resolution_clock::now() - globalTimeStart); - float globalTime = rawGlobalTime.count() / 1000000000.0f; - globalSpb->Get()->globalTime = globalTime; - sectionsPipeline->Activate(); - - Frustum frustum(projView); - - size_t culledSections = sections.size(); - unsigned int renderedFaces = 0; - for (auto& section : sections) { - glm::vec3 point{ - section.second.GetPosition().x * 16 + 8, - section.second.GetPosition().y * 16 + 8, - section.second.GetPosition().z * 16 + 8 - }; - - bool isVisible = frustum.TestSphere(point, 16.0f); - - if (!isVisible) { - culledSections--; - continue; - } - section.second.Render(); - renderedFaces += section.second.numOfFaces; - } - DebugInfo::culledSections = culledSections; - DebugInfo::renderFaces = renderedFaces; } void RendererWorld::PrepareRender(std::shared_ptr target, bool defferedShading) { -- cgit v1.2.3 From 5da2c6799c9064278a4c055e1505880699829cfc Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 12 Dec 2021 01:10:47 +0500 Subject: Optimized face's normals calculation --- src/RendererSectionData.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 94c802d..7e3d285 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -26,16 +26,12 @@ glm::vec2 TransformTextureCoord(glm::vec4 TextureAtlasCoords, glm::vec2 UvCoords } void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, const glm::mat4 &transform, bool visibility[FaceDirection::none], BlockLightness light, BlockLightness skyLight) { - glm::mat3 normalMat = glm::mat3(glm::inverseTranspose(transform * model.transform)); - - for (const auto &face : model.faces) { - glm::vec3 normal = {}; + for (const auto &face : model.faces) { glm::vec2 lightness; lightness.x = _max(light.face[0], light.face[1], light.face[2], light.face[3], light.face[4], light.face[5]); lightness.y = _max(skyLight.face[0], skyLight.face[1], skyLight.face[2], skyLight.face[3], skyLight.face[4], skyLight.face[5]); if (face.visibility != FaceDirection::none) { FaceDirection direction = face.visibility; - normal = FaceDirectionVector[direction].glm(); Vector directionVec = model.faceDirectionVector[direction]; FaceDirection faceDirection = FaceDirection::none; for (int i = 0; i < FaceDirection::none; i++) { @@ -61,7 +57,8 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co vertexData.positions[2] = transformed * glm::vec4(1, 0, 1, 1); vertexData.positions[3] = transformed * glm::vec4(1, 0, 0, 1); - vertexData.normal = glm::normalize(normalMat * glm::vec4(normal, 1.0f)); + glm::vec3 normal = glm::cross(vertexData.positions[1] - vertexData.positions[0], vertexData.positions[3] - vertexData.positions[0]); + vertexData.normal = glm::normalize(normal); vertexData.uvs[0] = TransformTextureCoord(face.texture, glm::vec2(0, 0), face.frames); vertexData.uvs[1] = TransformTextureCoord(face.texture, glm::vec2(1, 0), face.frames); -- cgit v1.2.3 From 3dc7e8aba4a07cc3c0d897b82af5a5951bde9991 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sat, 18 Dec 2021 10:59:06 +0500 Subject: Added AO mask --- src/AssetManager.cpp | 1 + src/AssetManager.hpp | 1 + src/RenderConfigs.cpp | 3 ++- src/RenderConfigs.hpp | 2 +- src/RendererSectionData.cpp | 1 + src/RendererSectionData.hpp | 3 ++- src/RendererWorld.cpp | 3 ++- 7 files changed, 10 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index ca68304..3e3f677 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -594,6 +594,7 @@ BlockFaces &AssetManager::GetBlockModelByBlockId(BlockId block) { blockFaces.transform = glm::mat4(1.0); blockFaces.faces = assetModel->blockModel.parsedFaces; blockFaces.isBlock = assetModel->blockModel.IsBlock; + blockFaces.ambientOcclusion = assetModel->blockModel.AmbientOcclusion; glm::mat4 transform = glm::mat4(1.0); if (model.y != 0) { diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index 2bee47c..59fc851 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -48,6 +48,7 @@ struct BlockFaces { glm::mat4 transform; std::vector faces; bool isBlock; + bool ambientOcclusion; Vector faceDirectionVector[FaceDirection::none]; }; diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp index 43342bb..78b442d 100644 --- a/src/RenderConfigs.cpp +++ b/src/RenderConfigs.cpp @@ -183,7 +183,7 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s normalConf->SetMaxFilter(Gal::Filtering::Bilinear); normal = gal->BuildTexture(normalConf); - auto lightConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8); + auto lightConf = gal->CreateTexture2DConfig(geomW, geomH, Gal::Format::R8G8B8); lightConf->SetMinFilter(Gal::Filtering::Bilinear); lightConf->SetMaxFilter(Gal::Filtering::Bilinear); light = gal->BuildTexture(lightConf); @@ -221,6 +221,7 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH, int s std::vector>> ssaoTextures = { {"normal", normal}, + {"light", light}, {"depthStencil", depthStencil}, {"ssaoNoise", ssaoNoise}, }; diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp index d0adfce..9d1ffcd 100644 --- a/src/RenderConfigs.hpp +++ b/src/RenderConfigs.hpp @@ -96,7 +96,7 @@ class Gbuffer { std::shared_ptr depthStencil; std::shared_ptr color; //RGB - color std::shared_ptr normal; //RGB - normal - std::shared_ptr light; //R - faceLight, G - skyLight, B - unused + std::shared_ptr light; //R - faceLight, G - skyLight, B - ssaoDepthMask std::shared_ptr geomFramebuffer; public: diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 7e3d285..c7d922e 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -69,6 +69,7 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co vertexData.animations = face.frames; vertexData.colors = face.color; vertexData.lights = lightness; + vertexData.ambientOcclusion = model.ambientOcclusion ? 1.0f : 0.0f; } } diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index 0a80560..390200c 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -38,7 +38,8 @@ struct VertexData { float animations; glm::vec3 colors; glm::vec2 lights; - uint8_t padding[8]; + float ambientOcclusion; + uint8_t padding[4]; }; struct RendererSectionData { diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 7cbd45e..2ffb57f 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -463,7 +463,8 @@ void RendererWorld::PrepareRender(std::shared_ptr target, bool {"animation", Gal::Type::Float, 1, 1}, {"color", Gal::Type::Vec3, 1, 1}, {"light", Gal::Type::Vec2, 1, 1}, - {"", Gal::Type::Uint8, 8, 1} + {"ambientOcclusion", Gal::Type::Float, 1, 1}, + {"", Gal::Type::Uint8, 4, 1} }); sectionsPipeline = gal->BuildPipeline(sectionsPLC); } -- cgit v1.2.3 From bac80e3514f93055daa4e743fa4f8ba177f77311 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 19 Dec 2021 17:00:13 +0500 Subject: Added per vertex lighting --- src/RendererSectionData.cpp | 11 +++++++---- src/RendererSectionData.hpp | 9 +++------ src/RendererWorld.cpp | 9 +++------ 3 files changed, 13 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index c7d922e..979858e 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -65,11 +65,14 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co vertexData.uvs[2] = TransformTextureCoord(face.texture, glm::vec2(1, 1), face.frames); vertexData.uvs[3] = TransformTextureCoord(face.texture, glm::vec2(0, 1), face.frames); - vertexData.uvLayers = face.layer; - vertexData.animations = face.frames; + vertexData.layerAnimationAo.r = face.layer; + vertexData.layerAnimationAo.g = face.frames; vertexData.colors = face.color; - vertexData.lights = lightness; - vertexData.ambientOcclusion = model.ambientOcclusion ? 1.0f : 0.0f; + vertexData.lights[0] = lightness; + vertexData.lights[1] = lightness; + vertexData.lights[2] = lightness; + vertexData.lights[3] = lightness; + vertexData.layerAnimationAo.b = model.ambientOcclusion ? 1.0f : 0.0f; } } diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index 390200c..efc6cad 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -32,14 +32,11 @@ struct SectionsData { struct VertexData { glm::vec3 positions[4]; - glm::vec3 normal; glm::vec2 uvs[4]; - float uvLayers; - float animations; + glm::vec2 lights[4]; + glm::vec3 normal; glm::vec3 colors; - glm::vec2 lights; - float ambientOcclusion; - uint8_t padding[4]; + glm::vec3 layerAnimationAo; //R - uvLayer, G - animation, B - ambientOcclusion }; struct RendererSectionData { diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 2ffb57f..21f0db2 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -457,14 +457,11 @@ void RendererWorld::PrepareRender(std::shared_ptr target, bool sectionsPLC->SetPrimitive(Gal::Primitive::TriangleFan); sectionsBufferBinding = sectionsPLC->BindVertexBuffer({ {"pos", Gal::Type::Vec3, 4, 1}, - {"normal", Gal::Type::Vec3, 1, 1}, {"uv", Gal::Type::Vec2, 4, 1}, - {"uvLayer", Gal::Type::Float, 1, 1}, - {"animation", Gal::Type::Float, 1, 1}, + {"light", Gal::Type::Vec2, 4, 1}, + {"normal", Gal::Type::Vec3, 1, 1}, {"color", Gal::Type::Vec3, 1, 1}, - {"light", Gal::Type::Vec2, 1, 1}, - {"ambientOcclusion", Gal::Type::Float, 1, 1}, - {"", Gal::Type::Uint8, 4, 1} + {"layerAnimationAo", Gal::Type::Vec3, 1, 1}, }); sectionsPipeline = gal->BuildPipeline(sectionsPLC); } -- cgit v1.2.3 From 749e24c0ca1ea5d1d3166ce52ca98601135e0bcc Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sat, 25 Dec 2021 11:20:36 +0500 Subject: Added smooth lighting --- src/RendererSectionData.cpp | 238 ++++++++++++++++++-------------------------- src/RendererSectionData.hpp | 44 ++++++-- src/RendererWorld.cpp | 16 +-- 3 files changed, 143 insertions(+), 155 deletions(-) (limited to 'src') diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 979858e..b5260f4 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -25,12 +25,44 @@ glm::vec2 TransformTextureCoord(glm::vec4 TextureAtlasCoords, glm::vec2 UvCoords return transformed; } -void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, const glm::mat4 &transform, bool visibility[FaceDirection::none], BlockLightness light, BlockLightness skyLight) { - for (const auto &face : model.faces) { - glm::vec2 lightness; - lightness.x = _max(light.face[0], light.face[1], light.face[2], light.face[3], light.face[4], light.face[5]); - lightness.y = _max(skyLight.face[0], skyLight.face[1], skyLight.face[2], skyLight.face[3], skyLight.face[4], skyLight.face[5]); - if (face.visibility != FaceDirection::none) { +//Maps [0.0, 0.5] range to [0.0, 1.0] +float MapNeg(float x) { + float y = x * 2.0f; + return glm::clamp(y, 0.0f, 1.0f); +} + +//Maps [0.5, 1.0] range to [0.0, 1.0] +float MapPos(float x) { + float y = (x * 2.0f) - 1.0f; + return glm::clamp(y, 0.0f, 1.0f); +} + +float InterpolateBlockLightness(const BlockLightness& light, const glm::vec3 &point) { + float xNeg = MapNeg(point.x); + float xPos = MapPos(point.x); + float xNegLight = glm::mix(light.face[FaceDirection::east], light.self, xNeg); + float xPosLight = glm::mix(light.self, light.face[FaceDirection::west], xPos); + float xLight = (glm::max)(xNegLight, xPosLight); + + float yNeg = MapNeg(point.y); + float yPos = MapPos(point.y); + float yNegLight = glm::mix(light.face[FaceDirection::down], light.self, yNeg); + float yPosLight = glm::mix(light.self, light.face[FaceDirection::up], yPos); + float yLight = (glm::max)(yNegLight, yPosLight); + + float zNeg = MapNeg(point.z); + float zPos = MapPos(point.z); + float zNegLight = glm::mix(light.face[FaceDirection::south], light.self, zNeg); + float zPosLight = glm::mix(light.self, light.face[FaceDirection::north], zPos); + float zLight = (glm::max)(zNegLight, zPosLight); + + return (glm::max)(xLight, (glm::max)(yLight, zLight)); +} + +void AddFacesByBlockModel(RendererSectionData& data, const BlockFaces& model, const glm::mat4& transform, bool visibility[FaceDirection::none], const Vector &pos, const SectionsData §ions) { + glm::vec3 absPos = (sections.data[1][1][1].GetPosition() * 16).glm(); + for (const auto& face : model.faces) { + if (face.visibility != FaceDirection::none) { FaceDirection direction = face.visibility; Vector directionVec = model.faceDirectionVector[direction]; FaceDirection faceDirection = FaceDirection::none; @@ -45,7 +77,6 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co if (visibility[faceDirection]) continue; - lightness = glm::vec2(light.face[faceDirection], skyLight.face[faceDirection]); } data.vertices.emplace_back(); @@ -68,12 +99,37 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co vertexData.layerAnimationAo.r = face.layer; vertexData.layerAnimationAo.g = face.frames; vertexData.colors = face.color; - vertexData.lights[0] = lightness; - vertexData.lights[1] = lightness; - vertexData.lights[2] = lightness; - vertexData.lights[3] = lightness; - vertexData.layerAnimationAo.b = model.ambientOcclusion ? 1.0f : 0.0f; - } + + bool useSmoothLighting = true; + if (useSmoothLighting) { + for (size_t i = 0; i < 4; i++) { + glm::vec3 baseLightPos = vertexData.positions[i] - absPos; + glm::vec3 lightPos = baseLightPos + normal * 0.5f; + glm::ivec3 basePos = glm::trunc(lightPos); + BlockLightness light = sections.GetLight(Vector(basePos.x, basePos.y, basePos.z)); + BlockLightness skyLight = sections.GetSkyLight(Vector(basePos.x, basePos.y, basePos.z)); + vertexData.lights[i].x = InterpolateBlockLightness(light, lightPos - glm::vec3(basePos)); + vertexData.lights[i].y = InterpolateBlockLightness(skyLight, lightPos - glm::vec3(basePos)); + } + } else { + BlockLightness light = sections.GetLight(pos); + BlockLightness skyLight = sections.GetSkyLight(pos); + glm::vec2 lightness; + + lightness.x = face.visibility != FaceDirection::none ? light.face[face.visibility] : light.self; + lightness.x = (glm::max)(lightness.x, static_cast(light.self)); + + lightness.y = face.visibility != FaceDirection::none ? skyLight.face[face.visibility] : skyLight.self; + lightness.y = (glm::max)(lightness.y, static_cast(skyLight.self)); + + vertexData.lights[0] = lightness; + vertexData.lights[1] = lightness; + vertexData.lights[2] = lightness; + vertexData.lights[3] = lightness; + } + + vertexData.layerAnimationAo.b = model.ambientOcclusion ? 1.0f : 0.0f; + } } BlockFaces *GetInternalBlockModel(const BlockId& id, std::vector> &idModels) { @@ -125,7 +181,7 @@ std::array SetBlockIdData(const SectionsData §ions) { for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { for (int x = 0; x < 16; x++) { - blockIdData[y * 256 + z * 16 + x] = sections.section.GetBlockId(Vector(x, y, z)); + blockIdData[y * 256 + z * 16 + x] = sections.data[1][1][1].GetBlockId(Vector(x, y, z)); } } } @@ -141,10 +197,10 @@ RendererSectionData ParseSection(const SectionsData §ions) { std::array blockVisibility = GetBlockVisibilityData(sections, blockIdData, idModels); std::string textureName; - data.hash = sections.section.GetHash(); - data.sectionPos = sections.section.GetPosition(); + data.hash = sections.data[1][1][1].GetHash(); + data.sectionPos = sections.data[1][1][1].GetPosition(); - glm::mat4 baseOffset = glm::translate(glm::mat4(1.0), (sections.section.GetPosition() * 16).glm()), transform; + glm::mat4 baseOffset = glm::translate(glm::mat4(1.0), (sections.data[1][1][1].GetPosition() * 16).glm()), transform; for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { @@ -157,11 +213,8 @@ RendererSectionData ParseSection(const SectionsData §ions) { transform = glm::translate(baseOffset, vec.glm()); - BlockLightness light = sections.GetLight(vec); - BlockLightness skyLight = sections.GetSkyLight(vec); - BlockFaces *model = GetInternalBlockModel(block, idModels); - AddFacesByBlockModel(data, *model, transform, blockVisibility[y * 256 + z * 16 + x], light, skyLight); + AddFacesByBlockModel(data, *model, transform, blockVisibility[y * 256 + z * 16 + x], vec, sections); } } } @@ -171,127 +224,34 @@ RendererSectionData ParseSection(const SectionsData §ions) { } BlockId SectionsData::GetBlockId(const Vector &pos) const { - if (pos.x < 0) - return east.GetBlockId(Vector(15, pos.y, pos.z)); - - if (pos.x > 15) - return west.GetBlockId(Vector(0, pos.y, pos.z)); - - if (pos.y < 0) - return bottom.GetBlockId(Vector(pos.x, 15, pos.z)); - - if (pos.y > 15) - return top.GetBlockId(Vector(pos.x, 0, pos.z)); - - if (pos.z < 0) - return south.GetBlockId(Vector(pos.x, pos.y, 15)); - - if (pos.z > 15) - return north.GetBlockId(Vector(pos.x, pos.y, 0)); - - return section.GetBlockId(pos); + Vector sectionPos = pos; + return GetSection(sectionPos).GetBlockId(sectionPos); } -BlockLightness SectionsData::GetLight(const Vector &pos) const { - BlockLightness lightness; - static const Vector directions[] = { - Vector(1,0,0), - Vector(-1,0,0), - Vector(0,1,0), - Vector(0,-1,0), - Vector(0,0,1), - Vector(0,0,-1), - }; - - unsigned char self = section.GetBlockLight(pos); - - for (const Vector &dir : directions) { - Vector vec = pos + dir; - unsigned char dirValue = 0; - - if (vec.x < 0 || vec.x > 15 || vec.y < 0 || vec.y > 15 || vec.z < 0 || vec.z > 15) { - if (vec.x < 0) - dirValue = east.GetBlockLight(Vector(15, vec.y, vec.z)); - if (vec.x > 15) - dirValue = west.GetBlockLight(Vector(0, vec.y, vec.z)); - if (vec.y < 0) - dirValue = bottom.GetBlockLight(Vector(vec.x, 15, vec.z)); - if (vec.y > 15) - dirValue = top.GetBlockLight(Vector(vec.x, 0, vec.z)); - if (vec.z < 0) - dirValue = south.GetBlockLight(Vector(vec.x, vec.y, 15)); - if (vec.z > 15) - dirValue = north.GetBlockLight(Vector(vec.x, vec.y, 0)); - } - else - dirValue = section.GetBlockLight(vec); - - dirValue = _max(self, dirValue); - - if (dir == directions[0]) - lightness.face[FaceDirection::east] = dirValue; - if (dir == directions[1]) - lightness.face[FaceDirection::west] = dirValue; - if (dir == directions[2]) - lightness.face[FaceDirection::up] = dirValue; - if (dir == directions[3]) - lightness.face[FaceDirection::down] = dirValue; - if (dir == directions[4]) - lightness.face[FaceDirection::south] = dirValue; - if (dir == directions[5]) - lightness.face[FaceDirection::north] = dirValue; - } - return lightness; +BlockLightness SectionsData::GetLight(const Vector& pos) const { + BlockLightness lightness; + for (size_t i = 0; i <= FaceDirection::none; i++) { + Vector vec = pos + FaceDirectionVector[i]; + uint8_t dirValue = GetSection(vec).GetBlockLight(vec); + + if (i == FaceDirection::none) + lightness.self = dirValue; + else + lightness.face[i] = dirValue; + } + return lightness; } BlockLightness SectionsData::GetSkyLight(const Vector &pos) const { - BlockLightness lightness; - static const Vector directions[] = { - Vector(1,0,0), - Vector(-1,0,0), - Vector(0,1,0), - Vector(0,-1,0), - Vector(0,0,1), - Vector(0,0,-1), - }; - - unsigned char self = section.GetBlockSkyLight(pos); - - for (const Vector &dir : directions) { - Vector vec = pos + dir; - unsigned char dirValue = 0; - - if (vec.x < 0 || vec.x > 15 || vec.y < 0 || vec.y > 15 || vec.z < 0 || vec.z > 15) { - if (vec.x < 0) - dirValue = east.GetBlockSkyLight(Vector(15, vec.y, vec.z)); - if (vec.x > 15) - dirValue = west.GetBlockSkyLight(Vector(0, vec.y, vec.z)); - if (vec.y < 0) - dirValue = bottom.GetBlockSkyLight(Vector(vec.x, 15, vec.z)); - if (vec.y > 15) - dirValue = top.GetBlockSkyLight(Vector(vec.x, 0, vec.z)); - if (vec.z < 0) - dirValue = south.GetBlockSkyLight(Vector(vec.x, vec.y, 15)); - if (vec.z > 15) - dirValue = north.GetBlockSkyLight(Vector(vec.x, vec.y, 0)); - } - else - dirValue = section.GetBlockSkyLight(vec); - - dirValue = _max(self, dirValue); - - if (dir == directions[0]) - lightness.face[FaceDirection::east] = dirValue; - if (dir == directions[1]) - lightness.face[FaceDirection::west] = dirValue; - if (dir == directions[2]) - lightness.face[FaceDirection::up] = dirValue; - if (dir == directions[3]) - lightness.face[FaceDirection::down] = dirValue; - if (dir == directions[4]) - lightness.face[FaceDirection::south] = dirValue; - if (dir == directions[5]) - lightness.face[FaceDirection::north] = dirValue; - } - return lightness; + BlockLightness lightness; + for (size_t i = 0; i <= FaceDirection::none; i++) { + Vector vec = pos + FaceDirectionVector[i]; + uint8_t dirValue = GetSection(vec).GetBlockSkyLight(vec); + + if (i == FaceDirection::none) + lightness.self = dirValue; + else + lightness.face[i] = dirValue; + } + return lightness; } diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index efc6cad..9eb99ad 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -11,17 +11,45 @@ class World; struct BlockLightness { - unsigned char face[FaceDirection::none] = { 0,0,0,0,0,0 }; + uint8_t face[FaceDirection::none + 1] = { 0,0,0,0,0,0 }; + uint8_t self = 0; }; struct SectionsData { - Section section; - Section west; - Section east; - Section top; - Section bottom; - Section north; - Section south; + Section data[3][3][3]; + + const Section& GetSection(Vector& pos) const { + size_t x = 1, y = 1, z = 1; + while (true) { + if (pos.x < 0) { + x--; + pos.x += 16; + } + else if (pos.x > 15) { + x++; + pos.x -= 16; + } + else if (pos.y < 0) { + y--; + pos.y += 16; + } + else if (pos.y > 15) { + y++; + pos.y -= 16; + } + else if (pos.z < 0) { + z--; + pos.z += 16; + } + else if (pos.z > 15) { + z++; + pos.z -= 16; + } + else + break; + } + return data[x][y][z]; + } BlockId GetBlockId(const Vector &pos) const; diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 21f0db2..057cf1e 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -57,13 +57,13 @@ void RendererWorld::ParseQueueUpdate() { vec.y -= 4500; } - parsing[id].data.section = GetGameState()->GetWorld().GetSection(vec); - parsing[id].data.north = GetGameState()->GetWorld().GetSection(vec + Vector(0, 0, 1)); - parsing[id].data.south = GetGameState()->GetWorld().GetSection(vec + Vector(0, 0, -1)); - parsing[id].data.west = GetGameState()->GetWorld().GetSection(vec + Vector(1, 0, 0)); - parsing[id].data.east = GetGameState()->GetWorld().GetSection(vec + Vector(-1, 0, 0)); - parsing[id].data.bottom = GetGameState()->GetWorld().GetSection(vec + Vector(0, -1, 0)); - parsing[id].data.top = GetGameState()->GetWorld().GetSection(vec + Vector(0, 1, 0)); + for (int x = -1; x < 2; x++) { + for (int y = -1; y < 2; y++) { + for (int z = -1; z < 2; z++) { + parsing[id].data.data[x + 1][y + 1][z + 1] = GetGameState()->GetWorld().GetSection(vec + Vector(x, y, z)); + } + } + } parsing[id].parsing = true; @@ -97,7 +97,7 @@ void RendererWorld::ParseQeueueRemoveUnnecessary() { bool skip = false; for (int i = 0; i < RendererWorld::parsingBufferSize; i++) { - if (parsing[i].data.section.GetHash() == section.GetHash()) { + if (parsing[i].data.data[1][1][1].GetHash() == section.GetHash()) { skip = true; break; } -- cgit v1.2.3 From 9feb51764077093a95a7b98af4b936a242096087 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sat, 25 Dec 2021 11:37:53 +0500 Subject: Added smooth lighting settings parameter --- src/Render.cpp | 3 ++- src/RendererSectionData.cpp | 9 ++++----- src/RendererSectionData.hpp | 2 +- src/RendererWorld.cpp | 7 ++++--- src/RendererWorld.hpp | 5 ++--- 5 files changed, 13 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/Render.cpp b/src/Render.cpp index 304f25f..fe9dc04 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -534,7 +534,7 @@ void Render::InitEvents() { listener.RegisterHandler("PlayerConnected", [this](const Event&) { stateString = "Loading terrain..."; - world = std::make_unique(fbTarget, Settings::ReadBool("deffered", false)); + world = std::make_unique(fbTarget, Settings::ReadBool("deffered", false), Settings::ReadBool("smoothlight", false)); world->MaxRenderingDistance = Settings::ReadDouble("renderDistance", 2.0f); PUSH_EVENT("UpdateSectionsRender", 0); }); @@ -606,6 +606,7 @@ void Render::InitEvents() { listener.RegisterHandler("SettingsUpdate", [this](const Event& eventData) { if (world) { + world->smoothLighting = Settings::ReadBool("smoothlight", false); float renderDistance = Settings::ReadDouble("renderDistance", 2.0f); if (renderDistance != world->MaxRenderingDistance) { world->MaxRenderingDistance = renderDistance; diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index b5260f4..761dd14 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -59,7 +59,7 @@ float InterpolateBlockLightness(const BlockLightness& light, const glm::vec3 &po return (glm::max)(xLight, (glm::max)(yLight, zLight)); } -void AddFacesByBlockModel(RendererSectionData& data, const BlockFaces& model, const glm::mat4& transform, bool visibility[FaceDirection::none], const Vector &pos, const SectionsData §ions) { +void AddFacesByBlockModel(RendererSectionData& data, const BlockFaces& model, const glm::mat4& transform, bool visibility[FaceDirection::none], const Vector &pos, const SectionsData §ions, bool smoothLighting) { glm::vec3 absPos = (sections.data[1][1][1].GetPosition() * 16).glm(); for (const auto& face : model.faces) { if (face.visibility != FaceDirection::none) { @@ -100,8 +100,7 @@ void AddFacesByBlockModel(RendererSectionData& data, const BlockFaces& model, co vertexData.layerAnimationAo.g = face.frames; vertexData.colors = face.color; - bool useSmoothLighting = true; - if (useSmoothLighting) { + if (smoothLighting) { for (size_t i = 0; i < 4; i++) { glm::vec3 baseLightPos = vertexData.positions[i] - absPos; glm::vec3 lightPos = baseLightPos + normal * 0.5f; @@ -188,7 +187,7 @@ std::array SetBlockIdData(const SectionsData §ions) { return blockIdData; } -RendererSectionData ParseSection(const SectionsData §ions) { +RendererSectionData ParseSection(const SectionsData §ions, bool smoothLighting) { OPTICK_EVENT(); RendererSectionData data; @@ -214,7 +213,7 @@ RendererSectionData ParseSection(const SectionsData §ions) { transform = glm::translate(baseOffset, vec.glm()); BlockFaces *model = GetInternalBlockModel(block, idModels); - AddFacesByBlockModel(data, *model, transform, blockVisibility[y * 256 + z * 16 + x], vec, sections); + AddFacesByBlockModel(data, *model, transform, blockVisibility[y * 256 + z * 16 + x], vec, sections, smoothLighting); } } } diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index 9eb99ad..0f9ade6 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -74,4 +74,4 @@ struct RendererSectionData { bool forced = false; }; -RendererSectionData ParseSection(const SectionsData §ions); \ No newline at end of file +RendererSectionData ParseSection(const SectionsData §ions, bool smoothLighting); diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 057cf1e..af177d7 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -26,7 +26,7 @@ void RendererWorld::WorkerFunction(size_t workerId) { return; size_t id = std::get<1>(data); bool forced = std::get<2>(data); - parsing[id].renderer = ParseSection(parsing[id].data); + parsing[id].renderer = ParseSection(parsing[id].data, smoothLighting); parsing[id].renderer.forced = forced; PUSH_EVENT("SectionParsed", id); }); @@ -153,8 +153,9 @@ void RendererWorld::UpdateAllSections(VectorF playerPos) { } } -RendererWorld::RendererWorld(std::shared_ptr target, bool defferedShading) { - OPTICK_EVENT(); +RendererWorld::RendererWorld(std::shared_ptr target, bool defferedShading, bool smoothLighting) { + OPTICK_EVENT(); + this->smoothLighting = smoothLighting; MaxRenderingDistance = 2; numOfWorkers = _max(1, (signed int) std::thread::hardware_concurrency() - 2); diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index 184323d..438c022 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -55,7 +55,7 @@ class RendererWorld { std::shared_ptr skyPipelineInstance; std::shared_ptr skyBuffer; public: - RendererWorld(std::shared_ptr target, bool defferedShading); + RendererWorld(std::shared_ptr target, bool defferedShading, bool smoothLighting); ~RendererWorld(); void Render(float screenRatio); @@ -63,8 +63,7 @@ public: double MaxRenderingDistance; - float shaderDayTime; - void Update(double timeToUpdate); + bool smoothLighting; }; -- cgit v1.2.3