From be808181b2c84358b232e4a32d1a6dd9dd6659ed Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sat, 11 Dec 2021 20:44:40 +0500 Subject: Optimized GBuffer size --- cwd/assets/altcraft/shaders/frag/entity.fs | 8 ++------ cwd/assets/altcraft/shaders/frag/face.fs | 7 ++----- cwd/assets/altcraft/shaders/frag/fwd_face.fs | 1 + cwd/assets/altcraft/shaders/frag/fwd_sky.fs | 1 + cwd/assets/altcraft/shaders/frag/light.fs | 13 +++++++++---- cwd/assets/altcraft/shaders/frag/sky.fs | 8 ++++---- cwd/assets/altcraft/shaders/frag/ssao.fs | 12 +++++++++--- cwd/assets/altcraft/shaders/vert/entity.vs | 4 +--- cwd/assets/altcraft/shaders/vert/face.vs | 4 +--- cwd/assets/altcraft/shaders/vert/light.vs | 22 ---------------------- cwd/assets/altcraft/shaders/vert/quad.vs | 1 + cwd/assets/altcraft/shaders/vert/rml.vs | 1 + cwd/assets/altcraft/shaders/vert/sky.vs | 1 + src/RenderConfigs.cpp | 13 +++---------- src/RenderConfigs.hpp | 2 +- src/RendererWorld.cpp | 2 ++ 16 files changed, 39 insertions(+), 61 deletions(-) delete mode 100644 cwd/assets/altcraft/shaders/vert/light.vs diff --git a/cwd/assets/altcraft/shaders/frag/entity.fs b/cwd/assets/altcraft/shaders/frag/entity.fs index 31e1a7c..813a9af 100644 --- a/cwd/assets/altcraft/shaders/frag/entity.fs +++ b/cwd/assets/altcraft/shaders/frag/entity.fs @@ -1,19 +1,15 @@ #version 330 core -in vec4 entityWorldPos; - layout (location = 0) out vec4 color; layout (location = 1) out vec4 normal; -layout (location = 2) out vec4 worldPos; -layout (location = 3) out vec4 addColor; -layout (location = 4) out vec4 light; +layout (location = 2) out vec4 addColor; +layout (location = 3) out vec4 light; uniform vec3 entityColor; void main() { color = vec4(entityColor, 1.0f); normal = vec4(0.0f, 0.0f, 0.0f, 1.0f); - worldPos = entityWorldPos; addColor = vec4(1.0f); light = vec4(1.0f, 1.0f, 0.0f, 1.0f); } diff --git a/cwd/assets/altcraft/shaders/frag/face.fs b/cwd/assets/altcraft/shaders/frag/face.fs index 65b7d15..ebda304 100644 --- a/cwd/assets/altcraft/shaders/frag/face.fs +++ b/cwd/assets/altcraft/shaders/frag/face.fs @@ -1,6 +1,5 @@ #version 330 core -in vec4 faceWorldPos; in vec3 faceTextureUv; in vec3 faceAddColor; in vec3 faceNormal; @@ -8,9 +7,8 @@ in vec2 faceLight; layout (location = 0) out vec4 color; layout (location = 1) out vec4 normal; -layout (location = 2) out vec4 worldPos; -layout (location = 3) out vec4 addColor; -layout (location = 4) out vec4 light; +layout (location = 2) out vec4 addColor; +layout (location = 3) out vec4 light; uniform sampler2DArray textureAtlas; @@ -21,7 +19,6 @@ void main() { color = vec4(col.rgb, 1.0f); normal = vec4(faceNormal, 1.0f); - worldPos = faceWorldPos; addColor = vec4(faceAddColor, 1.0f); light = vec4(faceLight / 15.0f, 0.0f, 1.0f); } diff --git a/cwd/assets/altcraft/shaders/frag/fwd_face.fs b/cwd/assets/altcraft/shaders/frag/fwd_face.fs index deb8341..12deab8 100644 --- a/cwd/assets/altcraft/shaders/frag/fwd_face.fs +++ b/cwd/assets/altcraft/shaders/frag/fwd_face.fs @@ -13,6 +13,7 @@ uniform sampler2DArray textureAtlas; layout (std140) uniform Globals { mat4 projView; mat4 proj; + mat4 invProj; mat4 view; uvec2 viewportSize; vec4 ssaoKernels[64]; diff --git a/cwd/assets/altcraft/shaders/frag/fwd_sky.fs b/cwd/assets/altcraft/shaders/frag/fwd_sky.fs index b6185fd..9f278b2 100644 --- a/cwd/assets/altcraft/shaders/frag/fwd_sky.fs +++ b/cwd/assets/altcraft/shaders/frag/fwd_sky.fs @@ -7,6 +7,7 @@ out vec4 fragColor; layout (std140) uniform Globals { mat4 projView; mat4 proj; + mat4 invProj; mat4 view; uvec2 viewportSize; vec4 ssaoKernels[64]; diff --git a/cwd/assets/altcraft/shaders/frag/light.fs b/cwd/assets/altcraft/shaders/frag/light.fs index c7e7254..3876388 100644 --- a/cwd/assets/altcraft/shaders/frag/light.fs +++ b/cwd/assets/altcraft/shaders/frag/light.fs @@ -7,7 +7,6 @@ in vec2 uv; uniform sampler2D depthStencil; uniform sampler2D color; uniform sampler2D normal; -uniform sampler2D worldPos; uniform sampler2D addColor; uniform sampler2D light; uniform sampler2D ssao; @@ -18,6 +17,7 @@ uniform bool applySsao; layout (std140) uniform Globals { mat4 projView; mat4 proj; + mat4 invProj; mat4 view; uvec2 viewportSize; vec4 ssaoKernels[64]; @@ -26,16 +26,21 @@ layout (std140) uniform Globals { float gamma; }; +vec3 RecoverViewWorldPos(vec2 screenPos, float depth) { + vec4 viewPos = invProj * vec4(screenPos * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0); + return viewPos.xyz / viewPos.w; +} + void main() { vec4 c = texture(color, uv); vec4 n = texture(normal, uv); n += 1.0f; n /= 2.0f; - vec4 wp = texture(worldPos, uv); vec4 ac = texture(addColor, uv); vec4 l = texture(light, uv); - float d = (1.0f - texture(depthStencil, uv).r) * 16.0f; + float depth = texture(depthStencil, uv).r; + float d = (1.0f - depth) * 16.0f; vec4 s = texture(ssao, uv); float faceLight = l.r; @@ -62,7 +67,7 @@ void main() { fragColor = n; break; case 3: - fragColor = wp; + fragColor = vec4(RecoverViewWorldPos(uv, depth), 1.0f); break; case 4: fragColor = ac; diff --git a/cwd/assets/altcraft/shaders/frag/sky.fs b/cwd/assets/altcraft/shaders/frag/sky.fs index dc97440..f3830b8 100644 --- a/cwd/assets/altcraft/shaders/frag/sky.fs +++ b/cwd/assets/altcraft/shaders/frag/sky.fs @@ -4,13 +4,13 @@ in vec3 facePos; layout (location = 0) out vec4 color; layout (location = 1) out vec4 normal; -layout (location = 2) out vec4 worldPos; -layout (location = 3) out vec4 addColor; -layout (location = 4) out vec4 light; +layout (location = 2) out vec4 addColor; +layout (location = 3) out vec4 light; layout (std140) uniform Globals { mat4 projView; mat4 proj; + mat4 invProj; mat4 view; uvec2 viewportSize; vec4 ssaoKernels[64]; @@ -65,7 +65,7 @@ void main() { color += vec4(Sun().rgb, 1.0f); color += vec4(Moon().rgb, 1.0f); normal = vec4(0.0f, 0.0f, 0.0f, 1.0f); - worldPos = vec4(0.0f, 0.0f, 0.0f, 1.0f); addColor = vec4(1.0f); light = vec4(1.0f, 1.0f, 0.0f, 1.0f); + gl_FragDepth = 1.0f; } diff --git a/cwd/assets/altcraft/shaders/frag/ssao.fs b/cwd/assets/altcraft/shaders/frag/ssao.fs index 4ed93fd..662852f 100644 --- a/cwd/assets/altcraft/shaders/frag/ssao.fs +++ b/cwd/assets/altcraft/shaders/frag/ssao.fs @@ -5,7 +5,7 @@ out vec4 fragColor; in vec2 uv; uniform sampler2D normal; -uniform sampler2D worldPos; +uniform sampler2D depthStencil; uniform sampler2D ssaoNoise; uniform int ssaoSamples; @@ -13,6 +13,7 @@ uniform int ssaoSamples; layout (std140) uniform Globals { mat4 projView; mat4 proj; + mat4 invProj; mat4 view; uvec2 viewportSize; vec4 ssaoKernels[64]; @@ -26,9 +27,14 @@ const int kernelSize = 64; const float radius = 0.5f; const float bias = 0.025f; +vec3 RecoverViewWorldPos(vec2 screenPos, float depth) { + vec4 viewPos = invProj * vec4(screenPos * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0); + return viewPos.xyz / viewPos.w; +} + void main() { vec3 normal = texture(normal, uv).xyz; - vec3 fragPos = texture(worldPos, uv).xyz; + vec3 fragPos = RecoverViewWorldPos(uv, texture(depthStencil, uv).r); vec2 noiseUv = uv * viewportSize / noiseScale; vec3 randomVec = texture(ssaoNoise, noiseUv).xyz; @@ -49,7 +55,7 @@ void main() { offset.xyz /= offset.w; offset.xyz = offset.xyz * 0.5 + 0.5; - float sampleDepth = texture(worldPos, offset.xy).z; + float sampleDepth = RecoverViewWorldPos(offset.xy, texture(depthStencil, offset.xy).r).z; float rangeCheck = smoothstep(0.0, 1.0, radius / abs(fragPos.z - sampleDepth)); occlusion += (sampleDepth >= samplePos.z + bias ? 1.0 : 0.0) * rangeCheck; } diff --git a/cwd/assets/altcraft/shaders/vert/entity.vs b/cwd/assets/altcraft/shaders/vert/entity.vs index f90fbc1..8cc7f69 100644 --- a/cwd/assets/altcraft/shaders/vert/entity.vs +++ b/cwd/assets/altcraft/shaders/vert/entity.vs @@ -2,13 +2,12 @@ in vec3 pos; -out vec4 entityWorldPos; - uniform mat4 model; layout (std140) uniform Globals { mat4 projView; mat4 proj; + mat4 invProj; mat4 view; uvec2 viewportSize; vec4 ssaoKernels[64]; @@ -19,5 +18,4 @@ layout (std140) uniform Globals { void main() { gl_Position = projView * model * vec4(pos, 1); - entityWorldPos = view * model * vec4(pos, 1.0f); } diff --git a/cwd/assets/altcraft/shaders/vert/face.vs b/cwd/assets/altcraft/shaders/vert/face.vs index a85b2c6..3e6adf7 100644 --- a/cwd/assets/altcraft/shaders/vert/face.vs +++ b/cwd/assets/altcraft/shaders/vert/face.vs @@ -8,7 +8,6 @@ in float animation; in vec3 color; in vec2 light; -out vec4 faceWorldPos; out vec3 faceTextureUv; out vec3 faceNormal; out vec3 faceAddColor; @@ -17,6 +16,7 @@ out vec2 faceLight; layout (std140) uniform Globals { mat4 projView; mat4 proj; + mat4 invProj; mat4 view; uvec2 viewportSize; vec4 ssaoKernels[64]; @@ -28,8 +28,6 @@ layout (std140) uniform Globals { void main() { gl_Position = projView * vec4(pos[gl_VertexID], 1.0f); - faceWorldPos = view * vec4(pos[gl_VertexID], 1.0f); - faceTextureUv = vec3(uv[gl_VertexID], uvLayer); faceTextureUv.y -= (uv[2].y - uv[0].y) * trunc(mod(globalTime * 4.0f, animation)); diff --git a/cwd/assets/altcraft/shaders/vert/light.vs b/cwd/assets/altcraft/shaders/vert/light.vs deleted file mode 100644 index 118ea5b..0000000 --- a/cwd/assets/altcraft/shaders/vert/light.vs +++ /dev/null @@ -1,22 +0,0 @@ -#version 330 core - -in vec2 pos; -in vec2 uvPos; - -out vec2 uv; - -layout (std140) uniform Globals { - mat4 projView; - mat4 proj; - mat4 view; - uvec2 viewportSize; - vec4 ssaoKernels[64]; - float globalTime; - float dayTime; - float gamma; -}; - -void main() { - gl_Position = vec4(pos.x, pos.y, 0.0, 1.0); - uv = uvPos; -} diff --git a/cwd/assets/altcraft/shaders/vert/quad.vs b/cwd/assets/altcraft/shaders/vert/quad.vs index 118ea5b..9add76d 100644 --- a/cwd/assets/altcraft/shaders/vert/quad.vs +++ b/cwd/assets/altcraft/shaders/vert/quad.vs @@ -8,6 +8,7 @@ out vec2 uv; layout (std140) uniform Globals { mat4 projView; mat4 proj; + mat4 invProj; mat4 view; uvec2 viewportSize; vec4 ssaoKernels[64]; diff --git a/cwd/assets/altcraft/shaders/vert/rml.vs b/cwd/assets/altcraft/shaders/vert/rml.vs index c61a4a0..897f49d 100644 --- a/cwd/assets/altcraft/shaders/vert/rml.vs +++ b/cwd/assets/altcraft/shaders/vert/rml.vs @@ -12,6 +12,7 @@ uniform vec2 translation; layout (std140) uniform Globals { mat4 projView; mat4 proj; + mat4 invProj; mat4 view; uvec2 viewportSize; vec4 ssaoKernels[64]; diff --git a/cwd/assets/altcraft/shaders/vert/sky.vs b/cwd/assets/altcraft/shaders/vert/sky.vs index 7043584..c618852 100644 --- a/cwd/assets/altcraft/shaders/vert/sky.vs +++ b/cwd/assets/altcraft/shaders/vert/sky.vs @@ -9,6 +9,7 @@ uniform mat4 model; layout (std140) uniform Globals { mat4 projView; mat4 proj; + mat4 invProj; mat4 view; uvec2 viewportSize; vec4 ssaoKernels[64]; 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