Selaa lähdekoodia

Make specular color a single float

Panagiotis Christopoulos Charitos 7 vuotta sitten
vanhempi
sitoutus
e16cfc6875

+ 7 - 2
programs/GBufferPost.ankiprog

@@ -44,12 +44,13 @@ void main()
 #define LIGHT_SET 0
 #define LIGHT_SS_BINDING 0
 #define LIGHT_UBO_BINDING 0
-#define LIGHT_TEX_BINDING 1
+#define LIGHT_TEX_BINDING 2
 #define LIGHT_DECALS
 #define LIGHT_COMMON_UNIS
 #include "shaders/ClusterLightCommon.glsl"
 
 layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_msDepthRt;
+layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_ssaoTex;
 
 layout(location = 0) in vec2 in_uv;
 layout(location = 1) in vec2 in_clusterIJ;
@@ -69,6 +70,10 @@ void main()
 	vec4 outDiffuse = vec4(0.0, 0.0, 0.0, 1.0);
 	vec4 outSpecular = vec4(0.0, 0.0, 0.0, 1.0);
 
+	// Sample SSAO
+	float ssao = textureLod(u_ssaoTex, in_uv, 0.0).r;
+	outDiffuse.a *= ssao;
+
 	// Get worldPos
 	float depth = textureLod(u_msDepthRt, in_uv, 0.0).r;
 	vec2 ndc = UV_TO_NDC(in_uv);
@@ -87,7 +92,7 @@ void main()
 
 	// Process decals
 	uint count = u_lightIndices[idxOffset++];
-	if(count == 0)
+	if(count == 0 && ssao >= (1.0 - EPSILON))
 	{
 		discard;
 	}

+ 2 - 7
programs/Reflections.ankiprog

@@ -38,7 +38,7 @@ http://www.anki3d.org/LICENSE
 #define LIGHT_SET 0
 #define LIGHT_SS_BINDING 0
 #define LIGHT_UBO_BINDING 0
-#define LIGHT_TEX_BINDING 7
+#define LIGHT_TEX_BINDING 6
 #define LIGHT_INDIRECT
 #define LIGHT_COMMON_UNIS
 #include "shaders/ClusterLightCommon.glsl"
@@ -53,7 +53,6 @@ layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_gbufferRt2;
 layout(ANKI_TEX_BINDING(0, 3)) uniform sampler2D u_depthRt;
 layout(ANKI_TEX_BINDING(0, 4)) uniform sampler2D u_hizRt;
 layout(ANKI_TEX_BINDING(0, 5)) uniform sampler2D u_lightBufferRt;
-layout(ANKI_TEX_BINDING(0, 6)) uniform sampler2D u_ssaoTex;
 
 layout(ANKI_IMAGE_BINDING(0, 0)) writeonly uniform image2D out_reflAndIndirect;
 
@@ -296,11 +295,7 @@ void main()
 	vec3 finalRefl = mix(probeCol, sslrCol, sslrFactor);
 
 	// Compute the final color
-	vec3 outColor;
-	{
-		float ssao = textureLod(u_ssaoTex, uv, 0.0).r;
-		outColor = (indirectCol * gbuffer.diffuse + finalRefl * env) * ssao;
-	}
+	vec3 outColor = indirectCol * gbuffer.diffuse + finalRefl * env;
 
 	// Store the color for the resolve
 	s_pixels[gl_LocalInvocationID.y][gl_LocalInvocationID.x] = outColor;

+ 7 - 7
samples/sponza/assets/scene.lua

@@ -2692,11 +2692,11 @@ lcomp = node:getSceneNodeBase():getLightComponent()
 lcomp:setDiffuseColor(Vec4.new(15, 15, 15, 1))
 lcomp:setInnerAngle(0.737402)
 lcomp:setOuterAngle(1.4748)
-lcomp:setDistance(79.5799)
+lcomp:setDistance(89.9999)
 trf = Transform.new()
-trf:setOrigin(Vec4.new(9.66932, 40.2052, -0.372856, 0))
+trf:setOrigin(Vec4.new(9.66932, 40.2052, -9.96416, 0))
 rot = Mat3x4.new()
-rot:setAll(-0.175432, -0.931125, 0.319735, 0, 0.0818194, 0.309859, 0.947256, 0, -0.981086, 0.19234, 0.0218249, 0)
+rot:setAll(-0.175432, -0.931125, 0.319735, 0, -0.392148, 0.363986, 0.844828, 0, -0.903019, 0.0228264, -0.428994, 0)
 trf:setRotation(rot)
 trf:setScale(1)
 node:getSceneNodeBase():getMoveComponent():setLocalTransform(trf)
@@ -2704,7 +2704,7 @@ lcomp:setShadowEnabled(1)
 
 node = scene:newPointLightNode("Point")
 lcomp = node:getSceneNodeBase():getLightComponent()
-lcomp:setDiffuseColor(Vec4.new(5.00188, 2.05202, 1.35254, 1))
+lcomp:setDiffuseColor(Vec4.new(10.0038, 4.10403, 2.70507, 1))
 lcomp:setRadius(4)
 trf = Transform.new()
 trf:setOrigin(Vec4.new(8.54617, 2.49423, -3.8305, 0))
@@ -2720,7 +2720,7 @@ event:setFrequency(2, 0.02)
 
 node = scene:newPointLightNode("Point_001")
 lcomp = node:getSceneNodeBase():getLightComponent()
-lcomp:setDiffuseColor(Vec4.new(5.00188, 2.05202, 1.35254, 1))
+lcomp:setDiffuseColor(Vec4.new(10.0038, 4.10403, 2.70507, 1))
 lcomp:setRadius(4)
 trf = Transform.new()
 trf:setOrigin(Vec4.new(8.54617, 2.49423, 2.5181, 0))
@@ -2736,7 +2736,7 @@ event:setFrequency(2, 0.02)
 
 node = scene:newPointLightNode("Point_002")
 lcomp = node:getSceneNodeBase():getLightComponent()
-lcomp:setDiffuseColor(Vec4.new(5.00188, 2.05202, 1.35254, 1))
+lcomp:setDiffuseColor(Vec4.new(10.0038, 4.10403, 2.70507, 1))
 lcomp:setRadius(4)
 trf = Transform.new()
 trf:setOrigin(Vec4.new(-10.7834, 2.49423, 2.5181, 0))
@@ -2752,7 +2752,7 @@ event:setFrequency(2, 0.02)
 
 node = scene:newPointLightNode("Point_003")
 lcomp = node:getSceneNodeBase():getLightComponent()
-lcomp:setDiffuseColor(Vec4.new(5.00188, 2.05202, 1.35254, 1))
+lcomp:setDiffuseColor(Vec4.new(10.0038, 4.10403, 2.70507, 1))
 lcomp:setRadius(4)
 trf = Transform.new()
 trf:setOrigin(Vec4.new(-10.7834, 2.49423, -3.84153, 0))

+ 7 - 22
shaders/Pack.glsl

@@ -145,9 +145,8 @@ struct GbufferInfo
 // Populate the G buffer
 void writeGBuffer(in GbufferInfo g, out vec4 rt0, out vec4 rt1, out vec4 rt2)
 {
-	float comp = packUnorm2ToUnorm1(vec2(g.subsurface, g.metallic));
-	rt0 = vec4(g.diffuse, comp);
-	rt1 = vec4(g.specular, g.roughness);
+	rt0 = vec4(g.diffuse, g.subsurface);
+	rt1 = vec4(g.roughness, g.metallic, g.specular.x, 0.0);
 
 	vec3 encNorm = signedOctEncode(g.normal);
 	rt2 = vec4(encNorm.xy, g.emission / MAX_EMISSION, encNorm.z);
@@ -159,30 +158,17 @@ void readNormalFromGBuffer(in sampler2D rt2, in vec2 uv, out vec3 normal)
 	normal = signedOctDecode(texture(rt2, uv).rga);
 }
 
-// Read the roughness from the G-buffer
-void readRoughnessSpecularFromGBuffer(in sampler2D rt1, in vec2 uv, out float roughness, out vec3 specular)
-{
-	vec4 comp = textureLod(rt1, uv, 0.0);
-	specular = comp.xyz;
-	roughness = comp.w;
-
-	// Fix roughness
-	const float MIN_ROUGHNESS = 0.5;
-	roughness = roughness * (1.0 - MIN_ROUGHNESS) + MIN_ROUGHNESS;
-}
-
 // Read from the G buffer
 void readGBuffer(in sampler2D rt0, in sampler2D rt1, in sampler2D rt2, in vec2 uv, in float lod, out GbufferInfo g)
 {
 	vec4 comp = textureLod(rt0, uv, 0.0);
 	g.diffuse = comp.xyz;
-	vec2 comp2 = unpackUnorm1ToUnorm2(comp.w);
-	g.subsurface = comp2.x;
-	g.metallic = comp2.y;
+	g.subsurface = comp.w;
 
 	comp = textureLod(rt1, uv, 0.0);
-	g.specular = comp.xyz;
-	g.roughness = comp.w;
+	g.roughness = comp.x;
+	g.metallic = comp.y;
+	g.specular = vec3(comp.z);
 
 	comp = textureLod(rt2, uv, 0.0);
 	g.normal = signedOctDecode(comp.xyw);
@@ -193,8 +179,7 @@ void readGBuffer(in sampler2D rt0, in sampler2D rt1, in sampler2D rt2, in vec2 u
 	g.roughness = g.roughness * (1.0 - MIN_ROUGHNESS) + MIN_ROUGHNESS;
 
 	// Compute reflectance
-	vec3 F0 = mix(g.specular, g.diffuse, g.metallic);
-	g.specular = F0;
+	g.specular = mix(g.specular, g.diffuse, g.metallic);
 
 	// Compute diffuse
 	g.diffuse = g.diffuse - g.diffuse * g.metallic;

+ 6 - 2
src/anki/renderer/GBufferPost.cpp

@@ -6,6 +6,7 @@
 #include <anki/renderer/GBufferPost.h>
 #include <anki/renderer/Renderer.h>
 #include <anki/renderer/GBuffer.h>
+#include <anki/renderer/Ssao.h>
 #include <anki/renderer/LightShading.h>
 #include <anki/misc/ConfigSet.h>
 
@@ -68,6 +69,8 @@ void GBufferPost::populateRenderGraph(RenderingContext& ctx)
 		TextureUsageBit::SAMPLED_FRAGMENT,
 		TextureSubresourceInfo(DepthStencilAspectBit::DEPTH)});
 
+	rpass.newConsumer({m_r->getSsao().getRt(), TextureUsageBit::SAMPLED_FRAGMENT});
+
 	rpass.newProducer({m_r->getGBuffer().getColorRt(0), TextureUsageBit::FRAMEBUFFER_ATTACHMENT_READ_WRITE});
 	rpass.newProducer({m_r->getGBuffer().getColorRt(1), TextureUsageBit::FRAMEBUFFER_ATTACHMENT_READ_WRITE});
 }
@@ -89,13 +92,14 @@ void GBufferPost::run(RenderPassWorkContext& rgraphCtx)
 		m_r->getGBuffer().getDepthRt(),
 		TextureSubresourceInfo(DepthStencilAspectBit::DEPTH),
 		m_r->getNearestSampler());
+	rgraphCtx.bindColorTextureAndSampler(0, 1, m_r->getSsao().getRt(), m_r->getLinearSampler());
 	cmdb->bindTextureAndSampler(0,
-		1,
+		2,
 		(rsrc.m_diffDecalTexView) ? rsrc.m_diffDecalTexView : m_r->getDummyTextureView(),
 		m_r->getTrilinearRepeatSampler(),
 		TextureUsageBit::SAMPLED_FRAGMENT);
 	cmdb->bindTextureAndSampler(0,
-		2,
+		3,
 		(rsrc.m_specularRoughnessDecalTexView) ? rsrc.m_specularRoughnessDecalTexView : m_r->getDummyTextureView(),
 		m_r->getTrilinearRepeatSampler(),
 		TextureUsageBit::SAMPLED_FRAGMENT);

+ 3 - 7
src/anki/renderer/Reflections.cpp

@@ -7,7 +7,6 @@
 #include <anki/renderer/Renderer.h>
 #include <anki/renderer/GBuffer.h>
 #include <anki/renderer/Indirect.h>
-#include <anki/renderer/Ssao.h>
 #include <anki/renderer/DepthDownscale.h>
 #include <anki/renderer/DownscaleBlur.h>
 #include <anki/renderer/RenderQueue.h>
@@ -92,7 +91,6 @@ void Reflections::populateRenderGraph(RenderingContext& ctx)
 	rpass.newConsumer({m_r->getGBuffer().getDepthRt(), TextureUsageBit::SAMPLED_COMPUTE});
 	rpass.newConsumer({m_r->getDepthDownscale().getHiZRt(), TextureUsageBit::SAMPLED_COMPUTE});
 	rpass.newConsumer({m_r->getDownscaleBlur().getRt(), TextureUsageBit::SAMPLED_COMPUTE});
-	rpass.newConsumer({m_r->getSsao().getRt(), TextureUsageBit::SAMPLED_FRAGMENT});
 
 	rpass.newConsumer({m_r->getIndirect().getReflectionRt(), TextureUsageBit::SAMPLED_COMPUTE});
 	rpass.newConsumer({m_r->getIndirect().getIrradianceRt(), TextureUsageBit::SAMPLED_COMPUTE});
@@ -117,12 +115,10 @@ void Reflections::run(RenderPassWorkContext& rgraphCtx)
 	rgraphCtx.bindColorTextureAndSampler(0, 4, m_r->getDepthDownscale().getHiZRt(), m_r->getNearestNearestSampler());
 	rgraphCtx.bindColorTextureAndSampler(0, 5, m_r->getDownscaleBlur().getRt(), m_r->getTrilinearRepeatSampler());
 
-	rgraphCtx.bindColorTextureAndSampler(0, 6, m_r->getSsao().getRt(), m_r->getLinearSampler());
-
-	rgraphCtx.bindColorTextureAndSampler(0, 7, m_r->getIndirect().getReflectionRt(), m_r->getTrilinearRepeatSampler());
-	rgraphCtx.bindColorTextureAndSampler(0, 8, m_r->getIndirect().getIrradianceRt(), m_r->getTrilinearRepeatSampler());
+	rgraphCtx.bindColorTextureAndSampler(0, 6, m_r->getIndirect().getReflectionRt(), m_r->getTrilinearRepeatSampler());
+	rgraphCtx.bindColorTextureAndSampler(0, 7, m_r->getIndirect().getIrradianceRt(), m_r->getTrilinearRepeatSampler());
 	cmdb->bindTextureAndSampler(0,
-		9,
+		8,
 		m_r->getIndirect().getIntegrationLut(),
 		m_r->getIndirect().getIntegrationLutSampler(),
 		TextureUsageBit::SAMPLED_COMPUTE);