Browse Source

Move depth downscale to the new system

Panagiotis Christopoulos Charitos 8 years ago
parent
commit
7d95387926

+ 58 - 0
programs/DepthDownscale.ankiprog

@@ -0,0 +1,58 @@
+<!-- 
+Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
+All rights reserved.
+Code licensed under the BSD License.
+http://www.anki3d.org/LICENSE
+-->
+<shaderProgram>
+	<mutators>
+		<mutator name="TO_COLOR_RT" values="0 1"/>
+		<mutator name="SAMPLE_RESOLVE_TYPE" values="0 1 2"/> <!-- 0: average, 1: min, 2: max -->
+	</mutators>
+
+	<shaders>
+		<shader type="vert">
+			<source><![CDATA[
+#include "shaders/Quad.vert.glsl"
+			]]></source>
+		</shader>
+
+		<shader type="frag">
+			<source><![CDATA[
+#include "shaders/Common.glsl"
+
+#define AVG 0
+#define MIN 1
+#define MAX 2
+
+layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_depthRt;
+
+layout(location = 0) in vec2 in_uv;
+
+#if TO_COLOR_RT
+layout(location = 0) out float out_color;
+#	define out_depth out_color
+#else
+#	define out_depth gl_FragDepth
+#endif
+
+void main()
+{
+	vec4 depths = textureGather(u_depthRt, in_uv, 0);
+
+#if SAMPLE_RESOLVE_TYPE == MIN
+	vec2 mind2 = min(depths.xy, depths.zw);
+	out_depth = min(mind2.x, mind2.y);
+#elif SAMPLE_RESOLVE_TYPE == MAX
+	vec2 max2 = max(depths.xy, depths.zw);
+	out_depth = max(max2.x, max2.y);
+#elif SAMPLE_RESOLVE_TYPE == AVG
+	out_depth = dot(depths, vec4(1.0 / 4.0));
+#else
+#	error See file
+#endif
+}
+			]]></source>
+		</shader>
+	</shaders>
+</shaderProgram>

+ 0 - 96
shaders/DepthAwareBlurGeneric.frag.glsl

@@ -1,96 +0,0 @@
-// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#include "shaders/Functions.glsl"
-#include "shaders/GaussianBlurCommon.glsl"
-
-// Preprocessor switches sanity checks
-#if !defined(VPASS) && !defined(HPASS)
-#error See file
-#endif
-
-#if !(defined(COL_RGBA) || defined(COL_RGB) || defined(COL_R))
-#error See file
-#endif
-
-#if !defined(TEXTURE_SIZE)
-#error See file
-#endif
-
-// Determine color type
-#if defined(COL_RGBA)
-#define COL_TYPE vec4
-#elif defined(COL_RGB)
-#define COL_TYPE vec3
-#elif defined(COL_R)
-#define COL_TYPE float
-#endif
-
-// Determine tex fetch
-#if defined(COL_RGBA)
-#define TEX_FETCH rgba
-#elif defined(COL_RGB)
-#define TEX_FETCH rgb
-#elif defined(COL_R)
-#define TEX_FETCH r
-#endif
-
-layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_colorTex;
-layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_depthTex;
-
-layout(location = 0) in vec2 in_uv;
-layout(location = 0) out COL_TYPE out_color;
-
-layout(std140, ANKI_UBO_BINDING(0, 0)) uniform ubo0_
-{
-	vec4 u_linearizeDepthCfDepthThresholdPad1;
-};
-
-#define u_linearizeDepthCf u_linearizeDepthCfDepthThresholdPad1.xy
-#define u_depthThreshold u_linearizeDepthCfDepthThresholdPad1.z
-
-float readLinearDepth(vec2 uv)
-{
-	float d = texture(u_depthTex, uv).r;
-	return linearizeDepthOptimal(d, u_linearizeDepthCf.x, u_linearizeDepthCf.y);
-}
-
-float computeDepthWeight(vec2 uv, float refDepth)
-{
-	float d = readLinearDepth(uv);
-	float diff = abs(refDepth - d);
-	float depthWeight = 1.0 / (EPSILON + diff);
-	return depthWeight;
-}
-
-void main()
-{
-#if defined(VPASS)
-	const vec2 TEXEL_SIZE = vec2(0.0, 1.0 / TEXTURE_SIZE.y);
-#else
-	const vec2 TEXEL_SIZE = vec2(1.0 / TEXTURE_SIZE.x, 0.0);
-#endif
-
-	out_color = COL_TYPE(0.0);
-	float refDepth = readLinearDepth(in_uv);
-	float weight = 0.0;
-
-	for(uint i = 0u; i < STEP_COUNT; ++i)
-	{
-		vec2 texCoordOffset = OFFSETS[i] * TEXEL_SIZE;
-
-		vec2 uv = in_uv + texCoordOffset;
-		float w = WEIGHTS[i] * computeDepthWeight(uv, refDepth);
-		out_color += texture(u_colorTex, uv).TEX_FETCH * w;
-		weight += w;
-
-		uv = in_uv - texCoordOffset;
-		w = WEIGHTS[i] * computeDepthWeight(uv, refDepth);
-		out_color += texture(u_colorTex, uv).TEX_FETCH * w;
-		weight += w;
-	}
-
-	out_color = out_color / weight;
-}

+ 0 - 29
shaders/DepthDownscaleHalf.frag.glsl

@@ -1,29 +0,0 @@
-// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#include "shaders/Common.glsl"
-
-layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_depthRt;
-
-layout(location = 0) in vec2 in_uv;
-
-#define AVG
-
-void main()
-{
-	vec4 depths = textureGather(u_depthRt, in_uv, 0);
-
-#if defined(MIN)
-	vec2 mind2 = min(depths.xy, depths.zw);
-	gl_FragDepth = min(mind2.x, mind2.y);
-#elif defined(MAX)
-	vec2 max2 = max(depths.xy, depths.zw);
-	gl_FragDepth = max(max2.x, max2.y);
-#elif defined(AVG)
-	gl_FragDepth = dot(depths, vec4(1.0 / 4.0));
-#else
-#error See file
-#endif
-}

+ 0 - 30
shaders/DepthDownscaleQuarter.frag.glsl

@@ -1,30 +0,0 @@
-// Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-#include "shaders/Common.glsl"
-
-layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_depthRt;
-
-layout(location = 0) in vec2 in_uv;
-layout(location = 0) out float out_depth;
-
-#define AVG
-
-void main()
-{
-	vec4 depths = textureGather(u_depthRt, in_uv, 0);
-
-#if defined(MIN)
-	vec2 mind2 = min(depths.xy, depths.zw);
-	out_depth = min(mind2.x, mind2.y);
-#elif defined(MAX)
-	vec2 max2 = max(depths.xy, depths.zw);
-	out_depth = max(max2.x, max2.y);
-#elif defined(AVG)
-	out_depth = dot(depths, vec4(1.0 / 4.0));
-#else
-#error See file
-#endif
-}

+ 20 - 6
src/anki/renderer/DepthDownscale.cpp

@@ -36,8 +36,15 @@ Error HalfDepth::init(const ConfigSet&)
 	fbInit.m_depthStencilAttachment.m_aspect = DepthStencilAspectBit::DEPTH;
 	m_fb = gr.newInstance<Framebuffer>(fbInit);
 
-	ANKI_CHECK(getResourceManager().loadResource("shaders/DepthDownscaleHalf.frag.glsl", m_frag));
-	m_r->createDrawQuadShaderProgram(m_frag->getGrShader(), m_prog);
+	// Prog
+	ANKI_CHECK(getResourceManager().loadResource("programs/DepthDownscale.ankiprog", m_prog));
+
+	ShaderProgramResourceMutationInitList<2> mutations(m_prog);
+	mutations.add("TO_COLOR_RT", 0).add("SAMPLE_RESOLVE_TYPE", 0);
+
+	const ShaderProgramResourceVariant* variant;
+	m_prog->getOrCreateVariant(mutations.get(), variant);
+	m_grProg = variant->getProgram();
 
 	return ErrorCode::NONE;
 }
@@ -63,7 +70,7 @@ void HalfDepth::run(RenderingContext& ctx)
 	CommandBufferPtr& cmdb = ctx.m_commandBuffer;
 
 	cmdb->beginRenderPass(m_fb);
-	cmdb->bindShaderProgram(m_prog);
+	cmdb->bindShaderProgram(m_grProg);
 	cmdb->bindTexture(0, 0, m_r->getGBuffer().m_depthRt);
 
 	cmdb->setViewport(0, 0, m_r->getWidth() / 2, m_r->getHeight() / 2);
@@ -101,8 +108,15 @@ Error QuarterDepth::init(const ConfigSet&)
 	fbInit.m_colorAttachmentCount = 1;
 	m_fb = gr.newInstance<Framebuffer>(fbInit);
 
-	ANKI_CHECK(getResourceManager().loadResource("shaders/DepthDownscaleQuarter.frag.glsl", m_frag));
-	m_r->createDrawQuadShaderProgram(m_frag->getGrShader(), m_prog);
+	// Prog
+	ANKI_CHECK(getResourceManager().loadResource("programs/DepthDownscale.ankiprog", m_prog));
+
+	ShaderProgramResourceMutationInitList<2> mutations(m_prog);
+	mutations.add("TO_COLOR_RT", 1).add("SAMPLE_RESOLVE_TYPE", 0);
+
+	const ShaderProgramResourceVariant* variant;
+	m_prog->getOrCreateVariant(mutations.get(), variant);
+	m_grProg = variant->getProgram();
 
 	return ErrorCode::NONE;
 }
@@ -128,7 +142,7 @@ void QuarterDepth::run(RenderingContext& ctx)
 	CommandBufferPtr& cmdb = ctx.m_commandBuffer;
 
 	cmdb->beginRenderPass(m_fb);
-	cmdb->bindShaderProgram(m_prog);
+	cmdb->bindShaderProgram(m_grProg);
 	cmdb->bindTexture(0, 0, m_parent->m_hd.m_depthRt);
 
 	cmdb->setViewport(0, 0, m_r->getWidth() / 4, m_r->getHeight() / 4);

+ 6 - 4
src/anki/renderer/DepthDownscale.h

@@ -43,8 +43,9 @@ private:
 	DepthDownscale* m_parent;
 
 	FramebufferPtr m_fb;
-	ShaderResourcePtr m_frag;
-	ShaderProgramPtr m_prog;
+
+	ShaderProgramResourcePtr m_prog;
+	ShaderProgramPtr m_grProg;
 };
 
 /// Quick pass to downscale the depth buffer.
@@ -71,8 +72,9 @@ private:
 	DepthDownscale* m_parent;
 
 	FramebufferPtr m_fb;
-	ShaderResourcePtr m_frag;
-	ShaderProgramPtr m_prog;
+
+	ShaderProgramResourcePtr m_prog;
+	ShaderProgramPtr m_grProg;
 };
 
 class DepthDownscale : public RenderingPass