فهرست منبع

Move 2 more LightShading shaders to HLSL

Panagiotis Christopoulos Charitos 3 سال پیش
والد
کامیت
1e9580f7ed

+ 1 - 1
AnKi/Shaders/LightFunctions.hlsl

@@ -100,7 +100,7 @@ RVec3 specularIsotropicLobe(GbufferInfo gbuffer, Vec3 viewDir, Vec3 frag2Light)
 	return F * (V * D);
 }
 
-Vec3 specularDFG(Vec3 F0, F32 roughness, Texture2D integrationLut, SamplerState integrationLutSampler, F32 NoV)
+Vec3 specularDFG(RVec3 F0, RF32 roughness, Texture2D<RVec4> integrationLut, SamplerState integrationLutSampler, F32 NoV)
 {
 	const Vec2 envBRDF = integrationLut.SampleLevel(integrationLutSampler, Vec2(roughness, NoV), 0.0).xy;
 	return lerp(envBRDF.xxx, envBRDF.yyy, F0);

+ 22 - 21
AnKi/Shaders/LightShadingApplyFog.ankiprog

@@ -3,51 +3,52 @@
 // 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/Functions.glsl>
+#include <AnKi/Shaders/Functions.hlsl>
 
 ANKI_SPECIALIZATION_CONSTANT_U32(kZSplitCount, 0u);
 ANKI_SPECIALIZATION_CONSTANT_U32(kFinalZSplit, 1u);
 
-layout(location = 0) in Vec2 in_uv;
-layout(location = 0) out ANKI_RP Vec4 out_color;
-
-layout(set = 0, binding = 0) uniform sampler u_nearestAnyClampSampler;
-layout(set = 0, binding = 1) uniform sampler u_linearAnyClampSampler;
-layout(set = 0, binding = 2) uniform texture2D u_depthRt;
-layout(set = 0, binding = 3) uniform texture3D u_fogVolume;
+[[vk::binding(0)]] SamplerState g_nearestAnyClampSampler;
+[[vk::binding(1)]] SamplerState g_linearAnyClampSampler;
+[[vk::binding(2)]] Texture2D g_depthRt;
+[[vk::binding(3)]] Texture3D<RVec4> g_fogVolume;
 
-layout(push_constant, std140, row_major) uniform b_pc
+struct Uniforms
 {
-	Vec2 u_padding;
-	F32 u_near;
-	F32 u_far;
+	Vec2 m_padding;
+	F32 m_near;
+	F32 m_far;
 };
 
-void main()
+[[vk::push_constant]] ConstantBuffer<Uniforms> g_uniforms;
+
+RVec4 main([[vk::location(0)]] Vec2 uv : TEXCOORD) : SV_TARGET0
 {
 	Vec3 uvw;
 
 	// Compute W coordinate
-	const F32 depth = textureLod(u_depthRt, u_nearestAnyClampSampler, in_uv, 0.0).r;
-	const F32 linearDepth = linearizeDepth(depth, u_near, u_far);
+	const F32 depth = g_depthRt.SampleLevel(g_nearestAnyClampSampler, uv, 0.0).r;
+	const F32 linearDepth = linearizeDepth(depth, g_uniforms.m_near, g_uniforms.m_far);
 	uvw.z = linearDepth * (F32(kZSplitCount) / F32(kFinalZSplit + 1u));
 
 	// Compute UV coordinates
-	uvw.xy = in_uv;
+	uvw.xy = uv;
 
 	// Read the volume
-	const ANKI_RP Vec4 fogVals = textureLod(u_fogVolume, u_linearAnyClampSampler, uvw, 0.0);
-	const ANKI_RP Vec3 inScattering = fogVals.rgb;
-	const ANKI_RP F32 transmittance = fogVals.a;
+	const RVec4 fogVals = g_fogVolume.SampleLevel(g_linearAnyClampSampler, uvw, 0.0);
+	const RVec3 inScattering = fogVals.rgb;
+	const RF32 transmittance = fogVals.a;
 
 	// Apply the fog
-	out_color = Vec4(inScattering, transmittance);
+	return RVec4(inScattering, transmittance);
 }
 
 #pragma anki end

+ 49 - 53
AnKi/Shaders/LightShadingApplyIndirect.ankiprog

@@ -3,92 +3,87 @@
 // 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/BilateralFilter.glsl>
-#include <AnKi/Shaders/PackFunctions.glsl>
-
-layout(set = 0, binding = 0) uniform sampler u_nearestAnyClampSampler;
-layout(set = 0, binding = 1) uniform sampler u_linearAnyClampSampler;
-layout(set = 0, binding = 2) uniform ANKI_RP texture2D u_quarterDiffuseIndirectTex;
-layout(set = 0, binding = 3) uniform ANKI_RP texture2D u_quarterSpecularIndirectTex;
-layout(set = 0, binding = 4) uniform texture2D u_quarterDepthTex;
-layout(set = 0, binding = 5) uniform texture2D u_fullDepthTex;
-layout(set = 0, binding = 6) uniform ANKI_RP texture2D u_gbuffer0Tex;
-layout(set = 0, binding = 7) uniform ANKI_RP texture2D u_gbuffer1Tex;
-layout(set = 0, binding = 8) uniform ANKI_RP texture2D u_gbuffer2Tex;
-layout(set = 0, binding = 9) uniform ANKI_RP texture2D u_integrationLut;
+#include <AnKi/Shaders/BilateralFilter.hlsl>
+#include <AnKi/Shaders/PackFunctions.hlsl>
+
+[[vk::binding(0)]] SamplerState g_nearestAnyClampSampler;
+[[vk::binding(1)]] SamplerState g_linearAnyClampSampler;
+[[vk::binding(2)]] Texture2D<RVec4> g_quarterDiffuseIndirectTex;
+[[vk::binding(3)]] Texture2D<RVec4> g_quarterSpecularIndirectTex;
+[[vk::binding(4)]] Texture2D g_quarterDepthTex;
+[[vk::binding(5)]] Texture2D g_fullDepthTex;
+[[vk::binding(6)]] Texture2D<RVec4> g_gbuffer0Tex;
+[[vk::binding(7)]] Texture2D<RVec4> g_gbuffer1Tex;
+[[vk::binding(8)]] Texture2D<RVec4> g_gbuffer2Tex;
+[[vk::binding(9)]] Texture2D<RVec4> g_integrationLut;
 
 #define CLUSTERED_SHADING_SET 0u
 #define CLUSTERED_SHADING_UNIFORMS_BINDING 10u
-#include <AnKi/Shaders/ClusteredShadingCommon.glsl>
+#include <AnKi/Shaders/ClusteredShadingCommon.hlsl>
 
-layout(push_constant, std140) uniform b_pc
+struct Uniforms
 {
-	F32 u_near;
-	F32 u_far;
-	F32 u_padding0;
-	F32 u_padding1;
+	F32 m_near;
+	F32 m_far;
+	F32 m_padding0;
+	F32 m_padding1;
 };
 
-layout(location = 0) in Vec2 in_uv;
-layout(location = 0) out ANKI_RP Vec3 out_color;
+[[vk::push_constant]] ConstantBuffer<Uniforms> g_uniforms;
 
-void main()
+RVec3 main([[vk::location(0)]] Vec2 uv : TEXCOORD) : SV_TARGET0
 {
 	// GBuffer
-	GbufferInfo gbuffer;
-	unpackGBufferNoVelocity(textureLod(u_gbuffer0Tex, u_nearestAnyClampSampler, in_uv, 0.0),
-							textureLod(u_gbuffer1Tex, u_nearestAnyClampSampler, in_uv, 0.0),
-							textureLod(u_gbuffer2Tex, u_nearestAnyClampSampler, in_uv, 0.0), gbuffer);
+	GbufferInfo gbuffer = (GbufferInfo)0;
+	unpackGBufferNoVelocity(g_gbuffer0Tex.SampleLevel(g_nearestAnyClampSampler, uv, 0.0),
+							g_gbuffer1Tex.SampleLevel(g_nearestAnyClampSampler, uv, 0.0),
+							g_gbuffer2Tex.SampleLevel(g_nearestAnyClampSampler, uv, 0.0), gbuffer);
 
 	// Reference
-	const F32 depthCenter = textureLod(u_fullDepthTex, u_nearestAnyClampSampler, in_uv, 0.0).x;
+	const F32 depthCenter = g_fullDepthTex.SampleLevel(g_nearestAnyClampSampler, uv, 0.0).x;
 	if(depthCenter == 1.0)
 	{
 		discard;
 	}
 
-	const F32 linearDepthCenter = linearizeDepth(depthCenter, u_near, u_far);
+	const F32 linearDepthCenter = linearizeDepth(depthCenter, g_uniforms.m_near, g_uniforms.m_far);
 
 	// Quad depths
-	Vec4 quarterDepths = textureGather(sampler2D(u_quarterDepthTex, u_nearestAnyClampSampler), in_uv, 0);
-	quarterDepths = linearizeDepth(quarterDepths, u_near, u_far);
+	Vec4 quarterDepths = g_quarterDepthTex.GatherRed(g_nearestAnyClampSampler, uv);
+	quarterDepths = linearizeDepth(quarterDepths, g_uniforms.m_near, g_uniforms.m_far);
 
 	// Diff
 	const Vec4 diffs = abs(quarterDepths - linearDepthCenter);
 	const F32 maxDiff = max(diffs.x, max(diffs.y, max(diffs.z, diffs.w)));
 
-	const F32 depthThreshold = 0.2 / (u_far - u_near);
+	const F32 depthThreshold = 0.2 / (g_uniforms.m_far - g_uniforms.m_near);
 
 	// Do a neareset depth upscale
-	ANKI_RP Vec3 diffuse = Vec3(0.0);
-	ANKI_RP Vec3 specular = Vec3(0.0);
+	RVec3 diffuse = RVec3(0.0, 0.0, 0.0);
+	RVec3 specular = RVec3(0.0, 0.0, 0.0);
 	if(maxDiff <= depthThreshold)
 	{
-		diffuse = textureLod(u_quarterDiffuseIndirectTex, u_linearAnyClampSampler, in_uv, 0.0).xyz;
-		specular = textureLod(u_quarterSpecularIndirectTex, u_linearAnyClampSampler, in_uv, 0.0).xyz;
+		diffuse = g_quarterDiffuseIndirectTex.SampleLevel(g_linearAnyClampSampler, uv, 0.0).xyz;
+		specular = g_quarterSpecularIndirectTex.SampleLevel(g_linearAnyClampSampler, uv, 0.0).xyz;
 	}
 	else
 	{
 		// Some discontinuites, need to pick the one closest to depth reference
 
-		const ANKI_RP Vec4 diffuseR =
-			textureGather(sampler2D(u_quarterDiffuseIndirectTex, u_linearAnyClampSampler), in_uv, 0);
-		const ANKI_RP Vec4 diffuseG =
-			textureGather(sampler2D(u_quarterDiffuseIndirectTex, u_linearAnyClampSampler), in_uv, 1);
-		const ANKI_RP Vec4 diffuseB =
-			textureGather(sampler2D(u_quarterDiffuseIndirectTex, u_linearAnyClampSampler), in_uv, 2);
+		const RVec4 diffuseR = g_quarterDiffuseIndirectTex.GatherRed(g_linearAnyClampSampler, uv);
+		const RVec4 diffuseG = g_quarterDiffuseIndirectTex.GatherGreen(g_linearAnyClampSampler, uv);
+		const RVec4 diffuseB = g_quarterDiffuseIndirectTex.GatherBlue(g_linearAnyClampSampler, uv);
 
-		const ANKI_RP Vec4 specularR =
-			textureGather(sampler2D(u_quarterSpecularIndirectTex, u_linearAnyClampSampler), in_uv, 0);
-		const ANKI_RP Vec4 specularG =
-			textureGather(sampler2D(u_quarterSpecularIndirectTex, u_linearAnyClampSampler), in_uv, 1);
-		const ANKI_RP Vec4 specularB =
-			textureGather(sampler2D(u_quarterSpecularIndirectTex, u_linearAnyClampSampler), in_uv, 2);
+		const RVec4 specularR = g_quarterSpecularIndirectTex.GatherRed(g_linearAnyClampSampler, uv);
+		const RVec4 specularG = g_quarterSpecularIndirectTex.GatherGreen(g_linearAnyClampSampler, uv);
+		const RVec4 specularB = g_quarterSpecularIndirectTex.GatherBlue(g_linearAnyClampSampler, uv);
 
 		F32 minDiff = diffs.x;
 		U32 comp = 0u;
@@ -113,15 +108,16 @@ void main()
 	diffuse *= gbuffer.m_diffuse;
 
 	// Do specular
-	const Vec2 ndc = UV_TO_NDC(in_uv);
-	const Vec4 worldPos4 = u_clusteredShading.m_matrices.m_invertedViewProjectionJitter * Vec4(ndc, depthCenter, 1.0);
+	const Vec2 ndc = uvToNdc(uv);
+	const Vec4 worldPos4 =
+		mul(g_clusteredShading.m_matrices.m_invertedViewProjectionJitter, Vec4(ndc, depthCenter, 1.0));
 	const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
-	const ANKI_RP Vec3 viewDir = normalize(u_clusteredShading.m_cameraPosition - worldPos);
+	const RVec3 viewDir = normalize(g_clusteredShading.m_cameraPosition - worldPos);
 	const F32 NoV = max(0.0, dot(gbuffer.m_normal, viewDir));
-	const Vec3 env = specularDFG(gbuffer.m_f0, gbuffer.m_roughness, u_integrationLut, u_linearAnyClampSampler, NoV);
+	const Vec3 env = specularDFG(gbuffer.m_f0, gbuffer.m_roughness, g_integrationLut, g_linearAnyClampSampler, NoV);
 	specular *= env;
 
 	// Writeout
-	out_color = saturateRp(diffuse + specular);
+	return min(diffuse + specular, RVec3(kMaxRF32, kMaxRF32, kMaxRF32));
 }
 #pragma anki end

+ 37 - 27
AnKi/Shaders/LightShadingSkybox.ankiprog

@@ -3,72 +3,82 @@
 // Code licensed under the BSD License.
 // http://www.anki3d.org/LICENSE
 
+#pragma anki hlsl
+
 #pragma anki mutator METHOD 0 1 // 0: solid colod, 1: 2D image
 
 #pragma anki start vert
 
-#include <AnKi/Shaders/Common.glsl>
+#include <AnKi/Shaders/Common.hlsl>
 
-layout(location = 0) out Vec2 out_uv;
+struct VertOut
+{
+	Vec4 m_position : SV_POSITION;
+	[[vk::location(0)]] Vec2 m_uv : TEXCOORD;
+};
 
-void main()
+VertOut main(U32 vertId : SV_VERTEXID)
 {
-	out_uv = Vec2(gl_VertexIndex & 1, gl_VertexIndex >> 1) * 2.0;
-	const Vec2 pos = out_uv * 2.0 - 1.0;
+	VertOut output;
+	output.m_uv = Vec2(vertId & 1, vertId >> 1) * 2.0;
+
+	output.m_position = Vec4(output.m_uv * 2.0 - 1.0, 1.0, 1.0);
 
-	gl_Position = Vec4(pos, 1.0, 1.0);
+	return output;
 }
 
 #pragma anki end
 
 #pragma anki start frag
 
-#include <AnKi/Shaders/Functions.glsl>
-
-layout(location = 0) in Vec2 in_uv;
-layout(location = 0) out ANKI_RP Vec3 out_color;
+#include <AnKi/Shaders/Functions.hlsl>
 
 #if METHOD == 0
-layout(push_constant) uniform b_pc
+struct Uniforms
 {
-	ANKI_RP Vec3 u_solidColor;
-	F32 u_padding;
+	RVec3 m_solidColor;
+	F32 m_padding;
 };
+
+[[vk::push_constant]] ConstantBuffer<Uniforms> g_uniforms;
 #else
-layout(binding = 0) uniform sampler u_trilinearAnySampler;
-layout(binding = 1) uniform ANKI_RP texture2D u_envMapTex;
+[[vk::binding(0)]] SamplerState g_trilinearAnySampler;
+[[vk::binding(1)]] Texture2D<RVec4> g_envMapTex;
 
-layout(push_constant, row_major) uniform b_pc
+struct Uniforms
 {
-	Mat4 u_invertedViewProjectionJitterMat;
-	Vec3 u_cameraPos;
-	F32 u_padding;
+	Mat4 m_invertedViewProjectionJitterMat;
+	Vec3 m_cameraPos;
+	F32 m_padding;
 };
+
+[[vk::push_constant]] ConstantBuffer<Uniforms> g_uniforms;
 #endif
 
-void main()
+RVec3 main([[vk::location(0)]] Vec2 uv : TEXCOORD) : SV_TARGET0
 {
 #if METHOD == 0
-	out_color = u_solidColor;
+	ANKI_MAYBE_UNUSED(uv);
+	return g_uniforms.m_solidColor;
 #else
 	const F32 depth = 1.0;
-	const Vec2 ndc = UV_TO_NDC(in_uv);
-	const Vec4 worldPos4 = u_invertedViewProjectionJitterMat * Vec4(ndc, depth, 1.0);
+	const Vec2 ndc = uvToNdc(uv);
+	const Vec4 worldPos4 = mul(g_uniforms.m_invertedViewProjectionJitterMat, Vec4(ndc, depth, 1.0));
 	const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
 
-	const Vec3 eyeToFrag = normalize(worldPos - u_cameraPos);
+	const Vec3 eyeToFrag = normalize(worldPos - g_uniforms.m_cameraPos);
 
-	const Vec2 uv = equirectangularMapping(eyeToFrag);
+	const Vec2 uv3 = equirectangularMapping(eyeToFrag);
 
 	// When uv is close to the edge of the texture the other quads might be in the oposit coordinate. Then the
 	// derivatives will be huge causing the texture to use the highest mip and thus create a visible seam. To fix this
 	// find when the derivatives are large and do some manual work to fix it
-	const Vec2 dx = abs(dFdx(uv));
+	const Vec2 dx = abs(ddx_coarse(uv3));
 	const F32 maxD = max(dx.x, dx.y);
 
 	const F32 bias = (maxD > 0.9) ? -100.0f : 0.0f;
 
-	out_color = texture(u_envMapTex, u_trilinearAnySampler, uv, bias).rgb;
+	return g_envMapTex.SampleBias(g_trilinearAnySampler, uv3, bias).rgb;
 #endif
 }