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