Browse Source

Minor fixes

Panagiotis Christopoulos Charitos 5 years ago
parent
commit
5ff346049d

+ 1 - 1
samples/common/Framework.cpp

@@ -73,7 +73,7 @@ Error SampleApp::userMainLoop(Bool& quit)
 	{
 	{
 		if(pressed)
 		if(pressed)
 		{
 		{
-			renderer.setCurrentDebugRenderTarget("SSGI");
+			renderer.setCurrentDebugRenderTarget("SSR");
 		}
 		}
 		else
 		else
 		{
 		{

+ 22 - 0
sandbox/Main.cpp

@@ -216,6 +216,28 @@ Error MyApp::userMainLoop(Bool& quit)
 	}
 	}
 #endif
 #endif
 
 
+	{
+		static Bool pressed = false;
+		Bool somethingPressed = false;
+		if(in.getKey(KeyCode::U) == 1)
+		{
+			pressed = !pressed;
+			somethingPressed = true;
+		}
+
+		if(somethingPressed)
+		{
+			if(pressed)
+			{
+				renderer.getOffscreenRenderer().setCurrentDebugRenderTarget("SSGI");
+			}
+			else
+			{
+				renderer.getOffscreenRenderer().setCurrentDebugRenderTarget("");
+			}
+		}
+	}
+
 	if(in.getEvent(InputEvent::WINDOW_CLOSED))
 	if(in.getEvent(InputEvent::WINDOW_CLOSED))
 	{
 	{
 		quit = true;
 		quit = true;

+ 7 - 2
shaders/SsRaymarching.glsl

@@ -178,7 +178,7 @@ void raymarchGroundTruth(Vec3 rayOrigin, // Ray origin in view space
 	const Vec3 start = Vec3(uv, depthRef);
 	const Vec3 start = Vec3(uv, depthRef);
 
 
 	// Project end point
 	// Project end point
-	const Vec3 p1 = rayOrigin + rayDir * 10.0;
+	const Vec3 p1 = rayOrigin + rayDir * 0.1;
 	const Vec4 end4 = projMat * Vec4(p1, 1.0);
 	const Vec4 end4 = projMat * Vec4(p1, 1.0);
 	Vec3 end = end4.xyz / end4.w;
 	Vec3 end = end4.xyz / end4.w;
 	end.xy = NDC_TO_UV(end.xy);
 	end.xy = NDC_TO_UV(end.xy);
@@ -224,7 +224,7 @@ void raymarchGroundTruth(Vec3 rayOrigin, // Ray origin in view space
 			const F32 blackMargin = 0.05 / 4.0;
 			const F32 blackMargin = 0.05 / 4.0;
 			const F32 whiteMargin = 0.1 / 2.0;
 			const F32 whiteMargin = 0.1 / 2.0;
 			const Vec2 marginAttenuation2d = smoothstep(blackMargin, whiteMargin, origin.xy)
 			const Vec2 marginAttenuation2d = smoothstep(blackMargin, whiteMargin, origin.xy)
-											* (1.0 - smoothstep(1.0 - whiteMargin, 1.0 - blackMargin, origin.xy));
+											 * (1.0 - smoothstep(1.0 - whiteMargin, 1.0 - blackMargin, origin.xy));
 			const F32 marginAttenuation = marginAttenuation2d.x * marginAttenuation2d.y;
 			const F32 marginAttenuation = marginAttenuation2d.x * marginAttenuation2d.y;
 			attenuation = marginAttenuation * cameraContribution;
 			attenuation = marginAttenuation * cameraContribution;
 
 
@@ -234,3 +234,8 @@ void raymarchGroundTruth(Vec3 rayOrigin, // Ray origin in view space
 		}
 		}
 	}
 	}
 }
 }
+
+void rejectBackFaces(Vec3 reflection, Vec3 normalAtHitPoint, out F32 attenuation)
+{
+	attenuation = smoothstep(-0.17, 0.0, dot(normalAtHitPoint, -reflection));
+}

+ 11 - 12
shaders/Ssgi.ankiprog

@@ -36,21 +36,22 @@ layout(set = 0, binding = 5) uniform texture2D u_lightBufferRt;
 void main()
 void main()
 {
 {
 	// Compute a global invocation ID that takes the checkerboard pattern into account
 	// Compute a global invocation ID that takes the checkerboard pattern into account
-	IVec2 fixedInvocationId = IVec2(gl_GlobalInvocationID.xy);
-	fixedInvocationId.x *= 2;
+	IVec2 fixedGlobalInvocationId = IVec2(gl_GlobalInvocationID.xy);
+	fixedGlobalInvocationId.x *= 2;
 #if VARIANT == 0
 #if VARIANT == 0
-	fixedInvocationId.x += ((fixedInvocationId.y + 1) & 1);
+	fixedGlobalInvocationId.x += ((fixedGlobalInvocationId.y + 1) & 1);
 #else
 #else
-	fixedInvocationId.x += ((fixedInvocationId.y + 0) & 1);
+	fixedGlobalInvocationId.x += ((fixedGlobalInvocationId.y + 0) & 1);
 #endif
 #endif
 
 
-	if(fixedInvocationId.x >= I32(u_unis.m_framebufferSize.x) || fixedInvocationId.y >= I32(u_unis.m_framebufferSize.y))
+	if(fixedGlobalInvocationId.x >= I32(u_unis.m_framebufferSize.x)
+		|| fixedGlobalInvocationId.y >= I32(u_unis.m_framebufferSize.y))
 	{
 	{
 		// Skip threads outside the writable image
 		// Skip threads outside the writable image
 		return;
 		return;
 	}
 	}
 
 
-	const Vec2 uv = (Vec2(fixedInvocationId.xy) + 0.5) / Vec2(u_unis.m_framebufferSize);
+	const Vec2 uv = (Vec2(fixedGlobalInvocationId.xy) + 0.5) / Vec2(u_unis.m_framebufferSize);
 
 
 	// Get normal
 	// Get normal
 	const Vec3 worldNormal = readNormalFromGBuffer(u_gbufferRt2, u_trilinearClampSampler, uv);
 	const Vec3 worldNormal = readNormalFromGBuffer(u_gbufferRt2, u_trilinearClampSampler, uv);
@@ -64,7 +65,7 @@ void main()
 	const Vec3 viewPos = viewPos4.xyz / viewPos4.w;
 	const Vec3 viewPos = viewPos4.xyz / viewPos4.w;
 
 
 	// Get a random point inside the hemisphere. Use hemisphereSampleCos to avoid perpendicular vecs to viewNormal
 	// Get a random point inside the hemisphere. Use hemisphereSampleCos to avoid perpendicular vecs to viewNormal
-	const UVec2 random = rand3DPCG16(UVec3(fixedInvocationId, u_unis.m_frameCount)).xy;
+	const UVec2 random = rand3DPCG16(UVec3(fixedGlobalInvocationId, u_unis.m_frameCount)).xy;
 	const Vec2 randomCircle = hammersleyRandom16(0, 0xFFFFu, random);
 	const Vec2 randomCircle = hammersleyRandom16(0, 0xFFFFu, random);
 	const Vec3 randomHemisphere = rotationFromDirection(viewNormal) * hemisphereSampleCos(randomCircle);
 	const Vec3 randomHemisphere = rotationFromDirection(viewNormal) * hemisphereSampleCos(randomCircle);
 
 
@@ -72,8 +73,6 @@ void main()
 	Vec3 hitPoint;
 	Vec3 hitPoint;
 	F32 hitAttenuation;
 	F32 hitAttenuation;
 	const U32 lod = 0;
 	const U32 lod = 0;
-	const U32 step = 16u;
-	const F32 stepf = step;
 	const F32 minStepf = 4.0;
 	const F32 minStepf = 4.0;
 	const F32 noise = F32(random.x) * (1.0 / 65536.0);
 	const F32 noise = F32(random.x) * (1.0 / 65536.0);
 	raymarchGroundTruth(viewPos,
 	raymarchGroundTruth(viewPos,
@@ -86,8 +85,8 @@ void main()
 		u_trilinearClampSampler,
 		u_trilinearClampSampler,
 		F32(lod),
 		F32(lod),
 		u_unis.m_depthBufferSize,
 		u_unis.m_depthBufferSize,
-		step,
-		U32((stepf - minStepf) * noise + minStepf),
+		u_unis.m_firstStepPixels,
+		U32(mix(minStepf, F32(u_unis.m_firstStepPixels), noise)),
 		hitPoint,
 		hitPoint,
 		hitAttenuation);
 		hitAttenuation);
 
 
@@ -113,7 +112,7 @@ void main()
 	const F32 NoL = max(0.0, dot(randomHemisphere, viewNormal));
 	const F32 NoL = max(0.0, dot(randomHemisphere, viewNormal));
 	outColor.xyz *= NoL;
 	outColor.xyz *= NoL;
 
 
-	imageStore(out_img, fixedInvocationId, outColor);
+	imageStore(out_img, fixedGlobalInvocationId, outColor);
 }
 }
 
 
 #pragma anki end
 #pragma anki end

+ 39 - 11
shaders/Ssr.ankiprog

@@ -41,21 +41,22 @@ const Vec2 NOISE_TEX_SIZE = Vec2(16.0);
 void main()
 void main()
 {
 {
 	// Compute a global invocation ID that takes the checkerboard pattern into account
 	// Compute a global invocation ID that takes the checkerboard pattern into account
-	IVec2 fixedInvocationId = IVec2(gl_GlobalInvocationID.xy);
-	fixedInvocationId.x *= 2;
+	IVec2 fixedGlobalInvocationId = IVec2(gl_GlobalInvocationID.xy);
+	fixedGlobalInvocationId.x *= 2;
 #if VARIANT == 0
 #if VARIANT == 0
-	fixedInvocationId.x += ((fixedInvocationId.y + 1) & 1);
+	fixedGlobalInvocationId.x += ((fixedGlobalInvocationId.y + 1) & 1);
 #else
 #else
-	fixedInvocationId.x += ((fixedInvocationId.y + 0) & 1);
+	fixedGlobalInvocationId.x += ((fixedGlobalInvocationId.y + 0) & 1);
 #endif
 #endif
 
 
-	if(fixedInvocationId.x >= I32(u_unis.m_framebufferSize.x) || fixedInvocationId.y >= I32(u_unis.m_framebufferSize.y))
+	if(fixedGlobalInvocationId.x >= I32(u_unis.m_framebufferSize.x)
+		|| fixedGlobalInvocationId.y >= I32(u_unis.m_framebufferSize.y))
 	{
 	{
 		// Skip threads outside the writable image
 		// Skip threads outside the writable image
 		return;
 		return;
 	}
 	}
 
 
-	const Vec2 uv = (Vec2(fixedInvocationId.xy) + 0.5) / Vec2(u_unis.m_framebufferSize);
+	const Vec2 uv = (Vec2(fixedGlobalInvocationId.xy) + 0.5) / Vec2(u_unis.m_framebufferSize);
 
 
 	// Read part of the G-buffer
 	// Read part of the G-buffer
 	const F32 roughness = readRoughnessFromGBuffer(u_gbufferRt1, u_trilinearClampSampler, uv);
 	const F32 roughness = readRoughnessFromGBuffer(u_gbufferRt1, u_trilinearClampSampler, uv);
@@ -82,9 +83,9 @@ void main()
 	Vec3 hitPoint;
 	Vec3 hitPoint;
 	F32 hitAttenuation;
 	F32 hitAttenuation;
 	const U32 lod = 0;
 	const U32 lod = 0;
-	const U32 step = 16u;
+	const U32 step = u_unis.m_firstStepPixels;
 	const F32 stepf = step;
 	const F32 stepf = step;
-	const F32 minStepf = 4.0;
+	const F32 minStepf = stepf / 4.0;
 	raymarchGroundTruth(viewPos,
 	raymarchGroundTruth(viewPos,
 		reflVec,
 		reflVec,
 		uv,
 		uv,
@@ -100,12 +101,39 @@ void main()
 		hitPoint,
 		hitPoint,
 		hitAttenuation);
 		hitAttenuation);
 
 
+#if 0
+	// Reject backfacing
+	ANKI_BRANCH if(hitAttenuation > 0.0)
+	{
+		const Vec3 hitNormal =
+			u_unis.m_normalMat * readNormalFromGBuffer(u_gbufferRt2, u_trilinearClampSampler, hitPoint.xy);
+		F32 backFaceAttenuation;
+		rejectBackFaces(reflVec, hitNormal, backFaceAttenuation);
+
+		hitAttenuation *= backFaceAttenuation;
+	}
+
+	// Reject far from hit point
+	ANKI_BRANCH if(hitAttenuation > 0.0)
+	{
+		const F32 depth = textureLod(u_depthRt, u_trilinearClampSampler, hitPoint.xy, 0.0).r;
+		Vec4 viewPos4 = u_unis.m_invProjMat * Vec4(UV_TO_NDC(hitPoint.xy), depth, 1.0);
+		const F32 actualZ = viewPos4.z / viewPos4.w;
+
+		viewPos4 = u_unis.m_invProjMat * Vec4(UV_TO_NDC(hitPoint.xy), hitPoint.z, 1.0);
+		const F32 hitZ = viewPos4.z / viewPos4.w;
+
+		const F32 rejectionMeters = 1.0;
+		const F32 diff = abs(actualZ - hitZ);
+		const F32 distAttenuation = (diff < rejectionMeters) ? 1.0 : 0.0;
+		hitAttenuation *= distAttenuation;
+	}
+#endif
+
 	// Read the reflection
 	// Read the reflection
 	Vec4 outColor;
 	Vec4 outColor;
 	ANKI_BRANCH if(hitAttenuation > 0.0)
 	ANKI_BRANCH if(hitAttenuation > 0.0)
 	{
 	{
-		// Read the refl
-
 		// Reproject the UV because you are reading the previous frame
 		// Reproject the UV because you are reading the previous frame
 		const Vec4 v4 = u_unis.m_prevViewProjMatMulInvViewProjMat * Vec4(UV_TO_NDC(hitPoint.xy), hitPoint.z, 1.0);
 		const Vec4 v4 = u_unis.m_prevViewProjMatMulInvViewProjMat * Vec4(UV_TO_NDC(hitPoint.xy), hitPoint.z, 1.0);
 		hitPoint.xy = NDC_TO_UV(v4.xy / v4.w);
 		hitPoint.xy = NDC_TO_UV(v4.xy / v4.w);
@@ -125,6 +153,6 @@ void main()
 	}
 	}
 
 
 	// Store
 	// Store
-	imageStore(out_img, fixedInvocationId, outColor);
+	imageStore(out_img, fixedGlobalInvocationId, outColor);
 }
 }
 #pragma anki end
 #pragma anki end

+ 2 - 1
shaders/glsl_cpp_common/Ssgi.h

@@ -14,9 +14,10 @@ struct SsgiUniforms
 {
 {
 	UVec2 m_depthBufferSize;
 	UVec2 m_depthBufferSize;
 	UVec2 m_framebufferSize;
 	UVec2 m_framebufferSize;
-	UVec2 m_padding0;
 	U32 m_frameCount;
 	U32 m_frameCount;
 	U32 m_maxSteps;
 	U32 m_maxSteps;
+	U32 m_firstStepPixels;
+	U32 m_padding0;
 	Mat4 m_invProjMat;
 	Mat4 m_invProjMat;
 	Mat4 m_projMat;
 	Mat4 m_projMat;
 	Mat4 m_prevViewProjMatMulInvViewProjMat;
 	Mat4 m_prevViewProjMatMulInvViewProjMat;

+ 2 - 0
shaders/glsl_cpp_common/Ssr.h

@@ -18,6 +18,8 @@ struct SsrUniforms
 	U32 m_depthMipCount;
 	U32 m_depthMipCount;
 	U32 m_maxSteps;
 	U32 m_maxSteps;
 	U32 m_lightBufferMipCount;
 	U32 m_lightBufferMipCount;
+	UVec3 m_padding0;
+	U32 m_firstStepPixels;
 	Mat4 m_prevViewProjMatMulInvViewProjMat;
 	Mat4 m_prevViewProjMatMulInvViewProjMat;
 	Mat4 m_projMat;
 	Mat4 m_projMat;
 	Mat4 m_invProjMat;
 	Mat4 m_invProjMat;

+ 2 - 0
src/anki/renderer/Ssgi.cpp

@@ -35,6 +35,7 @@ Error Ssgi::initInternal(const ConfigSet& cfg)
 	ANKI_R_LOGI("Initializing SSGI pass (%ux%u)", width, height);
 	ANKI_R_LOGI("Initializing SSGI pass (%ux%u)", width, height);
 	m_main.m_maxSteps = cfg.getNumberU32("r_ssgiMaxSteps");
 	m_main.m_maxSteps = cfg.getNumberU32("r_ssgiMaxSteps");
 	m_main.m_depthLod = min(cfg.getNumberU32("r_ssgiDepthLod"), m_r->getDepthDownscale().getMipmapCount() - 1);
 	m_main.m_depthLod = min(cfg.getNumberU32("r_ssgiDepthLod"), m_r->getDepthDownscale().getMipmapCount() - 1);
+	m_main.m_firstStepPixels = 32;
 
 
 	ANKI_CHECK(getResourceManager().loadResource("engine_data/BlueNoiseRgb816x16.png", m_main.m_noiseTex));
 	ANKI_CHECK(getResourceManager().loadResource("engine_data/BlueNoiseRgb816x16.png", m_main.m_noiseTex));
 
 
@@ -115,6 +116,7 @@ void Ssgi::run(RenderPassWorkContext& rgraphCtx)
 	unis->m_normalMat = Mat3x4(ctx.m_matrices.m_view.getRotationPart());
 	unis->m_normalMat = Mat3x4(ctx.m_matrices.m_view.getRotationPart());
 	unis->m_frameCount = m_r->getFrameCount() & MAX_U32;
 	unis->m_frameCount = m_r->getFrameCount() & MAX_U32;
 	unis->m_maxSteps = m_main.m_maxSteps;
 	unis->m_maxSteps = m_main.m_maxSteps;
+	unis->m_firstStepPixels = m_main.m_firstStepPixels;
 
 
 	cmdb->bindSampler(0, 2, m_r->getSamplers().m_trilinearClamp);
 	cmdb->bindSampler(0, 2, m_r->getSamplers().m_trilinearClamp);
 
 

+ 1 - 0
src/anki/renderer/Ssgi.h

@@ -52,6 +52,7 @@ private:
 		TexturePtr m_rt;
 		TexturePtr m_rt;
 		TextureResourcePtr m_noiseTex;
 		TextureResourcePtr m_noiseTex;
 		U32 m_maxSteps = 32;
 		U32 m_maxSteps = 32;
+		U32 m_firstStepPixels = 16;
 		U32 m_depthLod = 0;
 		U32 m_depthLod = 0;
 		Bool m_rtImportedOnce = false;
 		Bool m_rtImportedOnce = false;
 	} m_main;
 	} m_main;

+ 2 - 0
src/anki/renderer/Ssr.cpp

@@ -36,6 +36,7 @@ Error Ssr::initInternal(const ConfigSet& cfg)
 	ANKI_R_LOGI("Initializing SSR pass (%ux%u)", width, height);
 	ANKI_R_LOGI("Initializing SSR pass (%ux%u)", width, height);
 	m_maxSteps = cfg.getNumberU32("r_ssrMaxSteps");
 	m_maxSteps = cfg.getNumberU32("r_ssrMaxSteps");
 	m_depthLod = cfg.getNumberU32("r_ssrDepthLod");
 	m_depthLod = cfg.getNumberU32("r_ssrDepthLod");
+	m_firstStepPixels = 32;
 
 
 	ANKI_CHECK(getResourceManager().loadResource("engine_data/BlueNoiseRgb816x16.png", m_noiseTex));
 	ANKI_CHECK(getResourceManager().loadResource("engine_data/BlueNoiseRgb816x16.png", m_noiseTex));
 
 
@@ -107,6 +108,7 @@ void Ssr::run(RenderPassWorkContext& rgraphCtx)
 	unis->m_depthMipCount = m_r->getDepthDownscale().getMipmapCount();
 	unis->m_depthMipCount = m_r->getDepthDownscale().getMipmapCount();
 	unis->m_maxSteps = m_maxSteps;
 	unis->m_maxSteps = m_maxSteps;
 	unis->m_lightBufferMipCount = m_r->getDownscaleBlur().getMipmapCount();
 	unis->m_lightBufferMipCount = m_r->getDownscaleBlur().getMipmapCount();
+	unis->m_firstStepPixels = m_firstStepPixels;
 	unis->m_prevViewProjMatMulInvViewProjMat =
 	unis->m_prevViewProjMatMulInvViewProjMat =
 		ctx.m_prevMatrices.m_viewProjection * ctx.m_matrices.m_viewProjectionJitter.getInverse();
 		ctx.m_prevMatrices.m_viewProjection * ctx.m_matrices.m_viewProjectionJitter.getInverse();
 	unis->m_projMat = ctx.m_matrices.m_projectionJitter;
 	unis->m_projMat = ctx.m_matrices.m_projectionJitter;

+ 1 - 0
src/anki/renderer/Ssr.h

@@ -45,6 +45,7 @@ private:
 	Array<U32, 2> m_workgroupSize = {};
 	Array<U32, 2> m_workgroupSize = {};
 	U32 m_maxSteps = 32;
 	U32 m_maxSteps = 32;
 	U32 m_depthLod = 0;
 	U32 m_depthLod = 0;
+	U32 m_firstStepPixels = 16;
 
 
 	class
 	class
 	{
 	{