Browse Source

Convert GBufferPost to HLSL

Panagiotis Christopoulos Charitos 3 years ago
parent
commit
a32866d419

+ 1 - 1
AnKi/ShaderCompiler/ShaderProgramReflection.cpp

@@ -795,7 +795,7 @@ Error SpirvReflector::constsReflection(DynamicArrayRaii<Const>& consts) const
 			const Bool err1 = idSame && (!nameSame || !typeSame);
 			if(err0 || err1)
 			{
-				ANKI_SHADER_COMPILER_LOGE("Linking error");
+				ANKI_SHADER_COMPILER_LOGE("Linking error: %s", newConst.m_name.cstr());
 				return Error::kUserData;
 			}
 

+ 0 - 139
AnKi/Shaders/GBufferCommon.glsl

@@ -1,139 +0,0 @@
-// Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
-// All rights reserved.
-// Code licensed under the BSD License.
-// http://www.anki3d.org/LICENSE
-
-// Defines the interfaces for the programs that write the GBuffer
-
-#pragma once
-
-#include <AnKi/Shaders/PackFunctions.glsl>
-#include <AnKi/Shaders/Include/ModelTypes.h>
-#include <AnKi/Shaders/Include/MaterialTypes.h>
-#include <AnKi/Shaders/Include/GpuSceneTypes.h>
-#include <AnKi/Shaders/Include/MeshTypes.h>
-#include <AnKi/Shaders/Common.glsl>
-
-ANKI_BINDLESS_SET(kMaterialSetBindless)
-
-#define REALLY_USING_PARALLAX \
-	(PARALLAX == 1 && ANKI_TECHNIQUE == RENDERING_TECHNIQUE_GBUFFER && ANKI_LOD == 0 && ALPHA_TEST == 0)
-
-// For some reason nVidia doesn't like mediump precision on bitangents
-#if ANKI_PLATFORM_MOBILE
-#	define BITANGENT_PRECISION ANKI_RP
-#else
-#	define BITANGENT_PRECISION
-#endif
-
-//
-// Vert input
-//
-#if defined(ANKI_VERTEX_SHADER)
-
-layout(location = kVertexStreamIdPosition) in Vec3 in_position;
-
-#	if ANKI_TECHNIQUE == RENDERING_TECHNIQUE_GBUFFER
-layout(location = kVertexStreamIdNormal) in ANKI_RP Vec3 in_normal;
-layout(location = kVertexStreamIdTangent) in ANKI_RP Vec4 in_tangent;
-#	endif
-
-#	if ANKI_TECHNIQUE == RENDERING_TECHNIQUE_GBUFFER || ALPHA_TEST
-layout(location = kVertexStreamIdUv) in Vec2 in_uv;
-#	endif
-
-#	if ANKI_BONES
-layout(location = kVertexStreamIdBoneIds) in UVec4 in_boneIndices;
-layout(location = kVertexStreamIdBoneWeights) in Vec4 in_boneWeights;
-#	endif
-
-#endif // defined(ANKI_VERTEX_SHADER)
-
-//
-// Vert out
-//
-#if defined(ANKI_VERTEX_SHADER)
-
-#	if ANKI_TECHNIQUE == RENDERING_TECHNIQUE_GBUFFER || ALPHA_TEST
-layout(location = 0) out Vec2 out_uv;
-#	endif
-
-#	if ANKI_TECHNIQUE == RENDERING_TECHNIQUE_GBUFFER
-layout(location = 1) out ANKI_RP Vec3 out_normal;
-layout(location = 2) out ANKI_RP Vec3 out_tangent;
-layout(location = 3) out BITANGENT_PRECISION Vec3 out_bitangent;
-
-#		if REALLY_USING_PARALLAX
-layout(location = 4) out F32 out_distFromTheCamera;
-layout(location = 5) out Vec3 out_eyeTangentSpace;
-layout(location = 6) out Vec3 out_normalTangentSpace;
-#		endif
-
-#		if ANKI_VELOCITY || ANKI_BONES
-layout(location = 7) out Vec3 out_prevClipXyw;
-layout(location = 8) out Vec3 out_crntClipXyw;
-#		endif
-#	endif // ANKI_TECHNIQUE == RENDERING_TECHNIQUE_GBUFFER
-
-#endif // defined(ANKI_VERTEX_SHADER)
-
-//
-// Frag input
-//
-#if defined(ANKI_FRAGMENT_SHADER)
-
-#	if ANKI_TECHNIQUE == RENDERING_TECHNIQUE_GBUFFER || ALPHA_TEST
-layout(location = 0) in Vec2 in_uv;
-#	endif
-
-#	if ANKI_TECHNIQUE == RENDERING_TECHNIQUE_GBUFFER
-layout(location = 1) in ANKI_RP Vec3 in_normal;
-layout(location = 2) in ANKI_RP Vec3 in_tangent;
-layout(location = 3) in BITANGENT_PRECISION Vec3 in_bitangent;
-
-#		if REALLY_USING_PARALLAX
-layout(location = 4) in F32 in_distFromTheCamera;
-layout(location = 5) in Vec3 in_eyeTangentSpace;
-layout(location = 6) in Vec3 in_normalTangentSpace;
-#		endif
-
-#		if ANKI_VELOCITY || ANKI_BONES
-layout(location = 7) in Vec3 in_prevClipXyw;
-layout(location = 8) in Vec3 in_crntClipXyw;
-#		endif
-#	endif // ANKI_TECHNIQUE == RENDERING_TECHNIQUE_GBUFFER
-
-#endif // defined(ANKI_FRAGMENT_SHADER)
-
-//
-// Frag out
-//
-#if defined(ANKI_FRAGMENT_SHADER) \
-	&& (ANKI_TECHNIQUE == RENDERING_TECHNIQUE_GBUFFER || ANKI_TECHNIQUE == RENDERING_TECHNIQUE_GBUFFER_EZ)
-layout(location = 0) out Vec4 out_gbuffer0;
-layout(location = 1) out Vec4 out_gbuffer1;
-layout(location = 2) out Vec4 out_gbuffer2;
-layout(location = 3) out Vec2 out_gbuffer3;
-#endif
-
-//
-// Functions
-//
-
-// Write the data to RTs
-#if defined(ANKI_FRAGMENT_SHADER) && ANKI_TECHNIQUE == RENDERING_TECHNIQUE_GBUFFER
-void packGBuffer(ANKI_RP Vec3 diffColor, ANKI_RP Vec3 normal, ANKI_RP Vec3 specularColor, ANKI_RP F32 roughness,
-				 ANKI_RP F32 subsurface, ANKI_RP Vec3 emission, ANKI_RP F32 metallic, Vec2 velocity)
-{
-	GbufferInfo g;
-	g.m_diffuse = diffColor;
-	g.m_normal = normal;
-	g.m_f0 = specularColor;
-	g.m_roughness = roughness;
-	g.m_subsurface = subsurface;
-	g.m_emission = emission;
-	g.m_metallic = metallic;
-	g.m_velocity = velocity;
-	packGBuffer(g, out_gbuffer0, out_gbuffer1, out_gbuffer2, out_gbuffer3);
-}
-#endif

+ 36 - 30
AnKi/Shaders/GBufferPost.ankiprog

@@ -3,34 +3,37 @@
 // Code licensed under the BSD License.
 // http://www.anki3d.org/LICENSE
 
+#pragma anki hlsl
+
 #pragma anki start vert
-#include <AnKi/Shaders/QuadVert.glsl>
+#include <AnKi/Shaders/QuadVert.hlsl>
 #pragma anki end
 
 #pragma anki start frag
-#include <AnKi/Shaders/PackFunctions.glsl>
-#include <AnKi/Shaders/Functions.glsl>
+#include <AnKi/Shaders/PackFunctions.hlsl>
+#include <AnKi/Shaders/Functions.hlsl>
 
 ANKI_SPECIALIZATION_CONSTANT_UVEC2(kTileCount, 0u);
 ANKI_SPECIALIZATION_CONSTANT_U32(kZSplitCount, 2u);
 ANKI_SPECIALIZATION_CONSTANT_U32(kTileSize, 3u);
 
-layout(set = 0, binding = 0) uniform sampler u_nearestAnyClampSampler;
-layout(set = 0, binding = 1) uniform texture2D u_depthTex;
-layout(set = 0, binding = 2) uniform sampler u_trilinearRepeatSampler;
+[[vk::binding(0)]] SamplerState g_nearestAnyClampSampler;
+[[vk::binding(1)]] Texture2D g_depthTex;
+[[vk::binding(2)]] SamplerState g_trilinearRepeatSampler;
 
 #define CLUSTERED_SHADING_SET 0u
 #define CLUSTERED_SHADING_UNIFORMS_BINDING 3u
 #define CLUSTERED_SHADING_DECALS_BINDING 4u
 #define CLUSTERED_SHADING_CLUSTERS_BINDING 7u
-#include <AnKi/Shaders/ClusteredShadingCommon.glsl>
-
-layout(location = 0) in Vec2 in_uv;
+#include <AnKi/Shaders/ClusteredShadingCommon.hlsl>
 
-layout(location = 0) out ANKI_RP Vec4 out_diffuse;
-layout(location = 1) out ANKI_RP Vec4 out_roughnessMetallicF0;
+struct FragOut
+{
+	RVec4 m_diffuse : SV_TARGET0;
+	RVec4 m_roughnessMetallicF0 : SV_TARGET1;
+};
 
-void main()
+FragOut main([[vk::location(0)]] Vec2 uv : TEXCOORD, Vec4 svPosition : SV_POSITION)
 {
 	// This code blends the diffuse and the specular+rougness of the decals with GBuffer render targets.
 	// Normaly the blending is being done ('D' is the decal diffuse and 'f' is decal blend factor):
@@ -39,33 +42,33 @@ void main()
 	// 2nd decal: d''=d'*(1-f')+D'*f' <=> d''=d*(1-f)*(1-f')+D*f*(1-f')+D'*f'
 	// By looking at the trend we will have to multiply the gbuffer.diff with: (1-f)*(1-f') ... (1-f'''')
 
-	ANKI_RP Vec4 diffuse = Vec4(0.0, 0.0, 0.0, 1.0);
-	ANKI_RP Vec4 roughnessMetallicF0 = Vec4(0.0, 0.0, 0.0, 1.0);
+	RVec4 diffuse = Vec4(0.0, 0.0, 0.0, 1.0);
+	RVec4 roughnessMetallicF0 = Vec4(0.0, 0.0, 0.0, 1.0);
 
 	// Get worldPos
-	const F32 depth = textureLod(u_depthTex, u_nearestAnyClampSampler, in_uv, 0.0).r;
-	const Vec2 ndc = UV_TO_NDC(in_uv);
-	const Vec4 worldPos4 = u_clusteredShading.m_matrices.m_invertedViewProjectionJitter * Vec4(ndc, depth, 1.0);
+	const F32 depth = g_depthTex.SampleLevel(g_nearestAnyClampSampler, uv, 0.0).r;
+	const Vec2 ndc = uvToNdc(uv);
+	const Vec4 worldPos4 = mul(g_clusteredShading.m_matrices.m_invertedViewProjectionJitter, Vec4(ndc, depth, 1.0));
 	const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
 
 	// Get the cluster
-	Cluster cluster = getClusterFragCoord(Vec3(gl_FragCoord.xy, depth), kTileSize, kTileCount, kZSplitCount,
-										  u_clusteredShading.m_zSplitMagic.x, u_clusteredShading.m_zSplitMagic.y);
+	Cluster cluster = getClusterFragCoord(Vec3(svPosition.xy, depth), kTileSize, kTileCount, kZSplitCount,
+										  g_clusteredShading.m_zSplitMagic.x, g_clusteredShading.m_zSplitMagic.y);
 
 	// Process decals
-	if(cluster.m_decalsMask == ExtendedClusterObjectMask(0))
+	if(cluster.m_decalsMask == 0)
 	{
 		discard;
 	}
 
-	[[dont_unroll]] while(cluster.m_decalsMask != ExtendedClusterObjectMask(0))
+	[[loop]] while(cluster.m_decalsMask != 0)
 	{
-		const I32 idx = findLSB2(cluster.m_decalsMask);
-		cluster.m_decalsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
-		const Decal decal = u_decals2[idx];
+		const I32 idx = firstbitlow2(cluster.m_decalsMask);
+		cluster.m_decalsMask &= ~((ExtendedClusterObjectMask)1 << (ExtendedClusterObjectMask)idx);
+		const Decal decal = g_decals[idx];
 
 		// Project pos to decal space
-		const Vec4 texCoords4 = decal.m_textureMatrix * Vec4(worldPos, 1.0);
+		const Vec4 texCoords4 = mul(decal.m_textureMatrix, Vec4(worldPos, 1.0));
 		if(texCoords4.w <= 0.7)
 		{
 			// Behind the decal, skip
@@ -79,14 +82,14 @@ void main()
 
 		// Read diffuse
 		const Vec2 diffUv = mad(texCoords2, decal.m_diffuseUv.zw, decal.m_diffuseUv.xy);
-		const ANKI_RP Vec4 decalDiff = texture(u_diffuseDecalTex, u_trilinearRepeatSampler, diffUv);
+		const RVec4 decalDiff = g_diffuseDecalTex.Sample(g_trilinearRepeatSampler, diffUv);
 
 		// Read roughness
 		const Vec2 specUv = mad(texCoords2, decal.m_normRoughnessUv.zw, decal.m_normRoughnessUv.xy);
-		const ANKI_RP Vec3 spec = texture(u_specularRoughnessDecalTex, u_trilinearRepeatSampler, specUv).rgb;
+		const RVec3 spec = g_specularRoughnessDecalTex.Sample(g_trilinearRepeatSampler, specUv).rgb;
 
 		// Update diffuse
-		ANKI_RP F32 f = decalDiff.a * decal.m_blendFactors[0];
+		RF32 f = decalDiff.a * decal.m_blendFactors[0];
 		diffuse.rgb = diffuse.rgb * (1.0 - f) + decalDiff.rgb * f;
 		diffuse.a *= (1.0 - f);
 
@@ -96,7 +99,10 @@ void main()
 		roughnessMetallicF0.a *= (1.0 - f);
 	}
 
-	out_diffuse = diffuse;
-	out_roughnessMetallicF0 = roughnessMetallicF0;
+	FragOut output;
+	output.m_diffuse = diffuse;
+	output.m_roughnessMetallicF0 = roughnessMetallicF0;
+
+	return output;
 }
 #pragma anki end