浏览代码

For once SSAO is applied in the correct order

Panagiotis Christopoulos Charitos 9 年之前
父节点
当前提交
fdb0b4221f
共有 6 个文件被更改,包括 44 次插入38 次删除
  1. 15 3
      shaders/NearDepthUpscale.frag.glsl
  2. 3 9
      shaders/Pps.frag.glsl
  3. 1 1
      shaders/Ssao.frag.glsl
  4. 3 11
      src/renderer/Pps.cpp
  5. 11 11
      src/renderer/Renderer.cpp
  6. 11 3
      src/renderer/Upsample.cpp

+ 15 - 3
shaders/NearDepthUpscale.frag.glsl

@@ -8,12 +8,15 @@
 
 layout(location = 0) in vec2 in_uv;
 
-layout(location = 0) out vec3 out_color;
+layout(location = 0) out vec4 out_color;
 
 layout(TEX_BINDING(0, 0)) uniform sampler2D u_depthFullTex;
 layout(TEX_BINDING(0, 1)) uniform sampler2D u_depthHalfTex;
 layout(TEX_BINDING(0, 2)) uniform sampler2D u_colorTexNearest;
 layout(TEX_BINDING(0, 3)) uniform sampler2D u_colorTexLinear;
+#if SSAO_ENABLED
+layout(TEX_BINDING(0, 4)) uniform sampler2D u_ssaoTex;
+#endif
 
 layout(UBO_BINDING(0, 0)) uniform _u0
 {
@@ -75,14 +78,23 @@ void main()
 #else
 	float maxDiffLinear = abs(maxDiff);
 #endif
+	vec3 color;
 	if(maxDiffLinear < DEPTH_THRESHOLD)
 	{
 		// No major discontinuites, sample with bilinear
-		out_color = textureLod(u_colorTexLinear, in_uv, 0.0).rgb;
+		color = textureLod(u_colorTexLinear, in_uv, 0.0).rgb;
 	}
 	else
 	{
 		// Some discontinuites, need to use the newUv
-		out_color = textureLod(u_colorTexNearest, newUv, 0.0).rgb;
+		color = textureLod(u_colorTexNearest, newUv, 0.0).rgb;
 	}
+
+#if SSAO_ENABLED
+	float ssao = texture(u_ssaoTex, in_uv).r;
+	ssao = dither(ssao, 16.0);
+	out_color = vec4(color, ssao);
+#else
+	out_color = vec4(color, 1.0);
+#endif
 }

+ 3 - 9
shaders/Pps.frag.glsl

@@ -8,9 +8,8 @@
 #include "shaders/Functions.glsl"
 
 layout(binding = 0) uniform sampler2D u_isRt;
-layout(binding = 1) uniform sampler2D u_ppsSsaoRt;
-layout(binding = 2) uniform sampler2D u_ppsBloomLfRt;
-layout(binding = 3) uniform sampler3D u_lut;
+layout(binding = 1) uniform sampler2D u_ppsBloomLfRt;
+layout(binding = 2) uniform sampler3D u_lut;
 
 struct Luminance
 {
@@ -118,11 +117,6 @@ void main()
 	out_color = textureLod(u_isRt, in_uv, 0.0).rgb;
 #endif
 
-#if SSAO_ENABLED
-	float ssao = textureLod(u_ppsSsaoRt, in_uv, 0.0).r;
-	out_color *= ssao;
-#endif
-
 	out_color = tonemap(out_color, u_luminance.averageLuminancePad3.x, 0.0);
 
 #if 0
@@ -138,7 +132,7 @@ void main()
 
 #if 0
 	{
-		out_color = textureLod(u_isRt, in_uv, 0.0).rgb;;
+		out_color = textureLod(u_isRt, in_uv, 0.0).rgb;
 	}
 #endif
 }

+ 1 - 1
shaders/Ssao.frag.glsl

@@ -15,7 +15,7 @@ const vec3 KERNEL[KERNEL_SIZE] = KERNEL_ARRAY; // This will be appended in C++
 const float RADIUS = 1.1;
 
 // Initial is 1.0 but the bigger it is the more darker the SSAO factor gets
-const float DARKNESS_MULTIPLIER = 1.5;
+const float DARKNESS_MULTIPLIER = 2.0;
 
 // The algorithm will chose the number of samples depending on the distance
 const float MAX_DISTANCE = 40.0;

+ 3 - 11
src/renderer/Pps.cpp

@@ -7,7 +7,6 @@
 #include <anki/renderer/Renderer.h>
 #include <anki/renderer/Bloom.h>
 #include <anki/renderer/Sslf.h>
-#include <anki/renderer/Ssao.h>
 #include <anki/renderer/Tm.h>
 #include <anki/renderer/Is.h>
 #include <anki/renderer/Ms.h>
@@ -59,13 +58,11 @@ Error Pps::initInternal(const ConfigSet& config)
 	// SProg
 	StringAuto pps(getAllocator());
 
-	pps.sprintf("#define SSAO_ENABLED %u\n"
-				"#define BLOOM_ENABLED %u\n"
+	pps.sprintf("#define BLOOM_ENABLED %u\n"
 				"#define SHARPEN_ENABLED %u\n"
 				"#define GAMMA_CORRECTION_ENABLED %u\n"
 				"#define FBO_WIDTH %u\n"
 				"#define FBO_HEIGHT %u\n",
-		m_r->getSsaoEnabled(),
 		m_r->getBloomEnabled(),
 		U(config.getNumber("pps.sharpen")),
 		U(config.getNumber("pps.gammaCorrection")),
@@ -87,17 +84,12 @@ Error Pps::initInternal(const ConfigSet& config)
 	ResourceGroupInitInfo rcInit;
 	rcInit.m_textures[0].m_texture = m_r->getIs().getRt();
 
-	if(m_r->getSsaoEnabled())
-	{
-		rcInit.m_textures[1].m_texture = m_r->getSsao().getRt();
-	}
-
 	if(m_r->getBloomEnabled())
 	{
-		rcInit.m_textures[2].m_texture = m_r->getBloom().getRt();
+		rcInit.m_textures[1].m_texture = m_r->getBloom().getRt();
 	}
 
-	rcInit.m_textures[3].m_texture = m_lut->getGrTexture();
+	rcInit.m_textures[2].m_texture = m_lut->getGrTexture();
 
 	rcInit.m_storageBuffers[0].m_buffer =
 		m_r->getTm().getAverageLuminanceBuffer();

+ 11 - 11
src/renderer/Renderer.cpp

@@ -167,6 +167,12 @@ Error Renderer::initInternal(const ConfigSet& config)
 	m_lf.reset(m_alloc.newInstance<Lf>(this));
 	ANKI_CHECK(m_lf->init(config));
 
+	if(config.getNumber("ssao.enabled"))
+	{
+		m_ssao.reset(m_alloc.newInstance<Ssao>(this));
+		ANKI_CHECK(m_ssao->init(config));
+	}
+
 	m_upsample.reset(m_alloc.newInstance<Upsample>(this));
 	ANKI_CHECK(m_upsample->init(config));
 
@@ -182,12 +188,6 @@ Error Renderer::initInternal(const ConfigSet& config)
 		ANKI_CHECK(m_downscale->init(config));
 	}
 
-	if(config.getNumber("ssao.enabled") && config.getNumber("pps.enabled"))
-	{
-		m_ssao.reset(m_alloc.newInstance<Ssao>(this));
-		ANKI_CHECK(m_ssao->init(config));
-	}
-
 	if(config.getNumber("bloom.enabled") && config.getNumber("pps.enabled"))
 	{
 		m_bloom.reset(m_alloc.newInstance<Bloom>(this));
@@ -269,6 +269,11 @@ Error Renderer::render(RenderingContext& ctx)
 	m_vol->run(ctx);
 	cmdb->endRenderPass();
 
+	if(m_ssao)
+	{
+		m_ssao->run(ctx);
+	}
+
 	m_upsample->run(ctx);
 
 	if(m_downscale)
@@ -291,11 +296,6 @@ Error Renderer::render(RenderingContext& ctx)
 		m_sslf->run(ctx);
 	}
 
-	if(m_ssao)
-	{
-		m_ssao->run(ctx);
-	}
-
 	if(m_pps)
 	{
 		m_pps->run(ctx);

+ 11 - 3
src/renderer/Upsample.cpp

@@ -8,6 +8,7 @@
 #include <anki/renderer/Ms.h>
 #include <anki/renderer/Is.h>
 #include <anki/renderer/Fs.h>
+#include <anki/renderer/Ssao.h>
 #include <anki/scene/FrustumComponent.h>
 
 namespace anki
@@ -38,6 +39,11 @@ Error Upsample::init(const ConfigSet& config)
 	rcInit.m_textures[3].m_texture = m_r->getFs().getRt();
 	rcInit.m_textures[3].m_sampler = gr.newInstance<Sampler>(sinit);
 
+	if(m_r->getSsaoEnabled())
+	{
+		rcInit.m_textures[4].m_texture = m_r->getSsao().getRt();
+	}
+
 	rcInit.m_uniformBuffers[0].m_dynamic = true;
 
 	m_rcGroup = getGrManager().newInstance<ResourceGroup>(rcInit);
@@ -45,9 +51,11 @@ Error Upsample::init(const ConfigSet& config)
 	// Shader
 	StringAuto pps(getFrameAllocator());
 	pps.sprintf("#define TEXTURE_WIDTH %uu\n"
-				"#define TEXTURE_HEIGHT %uu\n",
+				"#define TEXTURE_HEIGHT %uu\n"
+				"#define SSAO_ENABLED %u\n",
 		m_r->getWidth() / FS_FRACTION,
-		m_r->getHeight() / FS_FRACTION);
+		m_r->getHeight() / FS_FRACTION,
+		m_r->getSsaoEnabled());
 
 	ANKI_CHECK(getResourceManager().loadResourceToCache(
 		m_frag, "shaders/NearDepthUpscale.frag.glsl", pps.toCString(), "r_"));
@@ -64,7 +72,7 @@ Error Upsample::init(const ConfigSet& config)
 	ppinit.m_color.m_attachmentCount = 1;
 	ppinit.m_color.m_attachments[0].m_format = Is::RT_PIXEL_FORMAT;
 	ppinit.m_color.m_attachments[0].m_srcBlendMethod = BlendMethod::ONE;
-	ppinit.m_color.m_attachments[0].m_dstBlendMethod = BlendMethod::ONE;
+	ppinit.m_color.m_attachments[0].m_dstBlendMethod = BlendMethod::SRC_ALPHA;
 
 	ppinit.m_shaders[U(ShaderType::VERTEX)] = m_vert->getGrShader();
 	ppinit.m_shaders[U(ShaderType::FRAGMENT)] = m_frag->getGrShader();