Add shaders re-implemented in HLSL

These were written against the shaders at revision eddd724e7.
There have been changes made since then, which will need to be mirrored.

See `data/hlsl/README.md` for the current status of each sample.
This commit is contained in:
Ben Clayton 2020-05-21 10:20:19 +00:00
parent 10a1ecaf7b
commit cce75f1859
287 changed files with 11263 additions and 0 deletions

View file

@ -0,0 +1,26 @@
// Copyright 2020 Google LLC
Texture2D texturePosition : register(t1);
SamplerState samplerPosition : register(s1);
Texture2D textureNormal : register(t2);
SamplerState samplerNormal : register(s2);
Texture2D textureAlbedo : register(t3);
SamplerState samplerAlbedo : register(s3);
Texture2DArray textureDepth : register(t5);
SamplerState samplerDepth : register(s5);
float LinearizeDepth(float depth)
{
float n = 0.1; // camera z near
float f = 64.0; // camera z far
float z = depth;
return (2.0 * n) / (f + n - z * (f - n));
}
float4 main([[vk::location(0)]] float3 inUV : TEXCOORD0) : SV_TARGET
{
// Display depth from light's point-of-view
// inUV.w = number of light source
float depth = textureDepth.Sample(samplerDepth, float3(inUV)).r;
return float4((1.0 - LinearizeDepth(depth)).xxx, 0.0);
}

View file

@ -0,0 +1,32 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float3 Pos : POSITION0;
[[vk::location(1)]] float2 UV : TEXCOORD0;
};
struct UBO
{
float4x4 projection;
float4x4 modelview;
};
cbuffer ubo : register(b0) { UBO ubo; }
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float3 UV : TEXCOORD0;
};
VSOutput main(VSInput input, uint InstanceIndex : SV_InstanceID)
{
VSOutput output = (VSOutput)0;
output.UV = float3(input.UV.xy, InstanceIndex);
float4 tmpPos = float4(input.Pos, 1.0);
tmpPos.y += InstanceIndex;
tmpPos.xy *= float2(1.0/4.0, 1.0/3.0);
output.Pos = mul(ubo.projection, mul(ubo.modelview, tmpPos));
return output;
}

View file

@ -0,0 +1,146 @@
// Copyright 2020 Google LLC
Texture2D textureposition : register(t1);
SamplerState samplerposition : register(s1);
Texture2D textureNormal : register(t2);
SamplerState samplerNormal : register(s2);
Texture2D textureAlbedo : register(t3);
SamplerState samplerAlbedo : register(s3);
// Depth from the light's point of view
//layout (binding = 5) uniform sampler2DShadow samplerShadowMap;
Texture2DArray textureShadowMap : register(t5);
SamplerState samplerShadowMap : register(s5);
#define LIGHT_COUNT 3
#define SHADOW_FACTOR 0.25
#define AMBIENT_LIGHT 0.1
#define USE_PCF
struct Light
{
float4 position;
float4 target;
float4 color;
float4x4 viewMatrix;
};
struct UBO
{
float4 viewPos;
Light lights[LIGHT_COUNT];
int useShadows;
};
cbuffer ubo : register(b4) { UBO ubo; }
float textureProj(float4 P, float layer, float2 offset)
{
float shadow = 1.0;
float4 shadowCoord = P / P.w;
shadowCoord.xy = shadowCoord.xy * 0.5 + 0.5;
if (shadowCoord.z > -1.0 && shadowCoord.z < 1.0)
{
float dist = textureShadowMap.Sample(samplerShadowMap, float3(shadowCoord.xy + offset, layer)).r;
if (shadowCoord.w > 0.0 && dist < shadowCoord.z)
{
shadow = SHADOW_FACTOR;
}
}
return shadow;
}
float filterPCF(float4 sc, float layer)
{
int2 texDim; int elements; int levels;
textureShadowMap.GetDimensions(0, texDim.x, texDim.y, elements, levels);
float scale = 1.5;
float dx = scale * 1.0 / float(texDim.x);
float dy = scale * 1.0 / float(texDim.y);
float shadowFactor = 0.0;
int count = 0;
int range = 1;
for (int x = -range; x <= range; x++)
{
for (int y = -range; y <= range; y++)
{
shadowFactor += textureProj(sc, layer, float2(dx*x, dy*y));
count++;
}
}
return shadowFactor / count;
}
float4 main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
{
// Get G-Buffer values
float3 fragPos = textureposition.Sample(samplerposition, inUV).rgb;
float3 normal = textureNormal.Sample(samplerNormal, inUV).rgb;
float4 albedo = textureAlbedo.Sample(samplerAlbedo, inUV);
// Ambient part
float3 fragcolor = albedo.rgb * AMBIENT_LIGHT;
float3 N = normalize(normal);
float shadow = 0.0;
for(int i = 0; i < LIGHT_COUNT; ++i)
{
// Vector to light
float3 L = ubo.lights[i].position.xyz - fragPos;
// Distance from light to fragment position
float dist = length(L);
L = normalize(L);
// Viewer to fragment
float3 V = ubo.viewPos.xyz - fragPos;
V = normalize(V);
float lightCosInnerAngle = cos(radians(15.0));
float lightCosOuterAngle = cos(radians(25.0));
float lightRange = 100.0;
// Direction vector from source to target
float3 dir = normalize(ubo.lights[i].position.xyz - ubo.lights[i].target.xyz);
// Dual cone spot light with smooth transition between inner and outer angle
float cosDir = dot(L, dir);
float spotEffect = smoothstep(lightCosOuterAngle, lightCosInnerAngle, cosDir);
float heightAttenuation = smoothstep(lightRange, 0.0f, dist);
// Diffuse lighting
float NdotL = max(0.0, dot(N, L));
float3 diff = NdotL.xxx;
// Specular lighting
float3 R = reflect(-L, N);
float NdotR = max(0.0, dot(R, V));
float3 spec = (pow(NdotR, 16.0) * albedo.a * 2.5).xxx;
fragcolor += float3((diff + spec) * spotEffect * heightAttenuation) * ubo.lights[i].color.rgb * albedo.rgb;
}
// Shadow calculations in a separate pass
if (ubo.useShadows > 0)
{
for(int i = 0; i < LIGHT_COUNT; ++i)
{
float4 shadowClip = mul(ubo.lights[i].viewMatrix, float4(fragPos, 1.0));
float shadowFactor;
#ifdef USE_PCF
shadowFactor= filterPCF(shadowClip, i);
#else
shadowFactor = textureProj(shadowClip, i, float2(0.0, 0.0));
#endif
fragcolor *= shadowFactor;
}
}
return float4(fragcolor, 1);
}

View file

@ -0,0 +1,29 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float3 Pos : POSITION0;
[[vk::location(1)]] float2 UV : TEXCOORD0;
};
struct UBO
{
float4x4 projection;
float4x4 modelview;
};
cbuffer ubo : register(b0) { UBO ubo; }
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float2 UV : TEXCOORD0;
};
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.UV = input.UV;
output.Pos = mul(ubo.projection, mul(ubo.modelview, float4(input.Pos.xyz, 1.0)));
return output;
}

View file

@ -0,0 +1,39 @@
// Copyright 2020 Google LLC
Texture2D textureColor : register(t1);
SamplerState samplerColor : register(s1);
Texture2D textureNormalMap : register(t2);
SamplerState samplerNormalMap : register(s2);
struct VSOutput
{
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float2 UV : TEXCOORD0;
[[vk::location(2)]] float3 Color : COLOR0;
[[vk::location(3)]] float3 WorldPos : POSITION0;
[[vk::location(4)]] float3 Tangent : TEXCOORD1;
};
struct FSOutput
{
float4 Position : SV_TARGET0;
float4 Normal : SV_TARGET1;
float4 Albedo : SV_TARGET2;
};
FSOutput main(VSOutput input)
{
FSOutput output = (FSOutput)0;
output.Position = float4(input.WorldPos, 1.0);
// Calculate normal in tangent space
float3 N = normalize(input.Normal);
float3 T = normalize(input.Tangent);
float3 B = cross(N, T);
float3x3 TBN = float3x3(T, B, N);
float3 tnorm = mul(normalize(textureNormalMap.Sample(samplerNormalMap, input.UV).xyz * 2.0 - float3(1.0, 1.0, 1.0)), TBN);
output.Normal = float4(tnorm, 1.0);
output.Albedo = textureColor.Sample(samplerColor, input.UV);
return output;
}

View file

@ -0,0 +1,52 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float4 Pos : POSITION0;
[[vk::location(1)]] float2 UV : TEXCOORD0;
[[vk::location(2)]] float3 Color : COLOR0;
[[vk::location(3)]] float3 Normal : NORMAL0;
[[vk::location(4)]] float3 Tangent : TEXCOORD1;
};
struct UBO
{
float4x4 projection;
float4x4 model;
float4x4 view;
float4 instancePos[3];
};
cbuffer ubo : register(b0) { UBO ubo; }
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float2 UV : TEXCOORD0;
[[vk::location(2)]] float3 Color : COLOR0;
[[vk::location(3)]] float3 WorldPos : POSITION0;
[[vk::location(4)]] float3 Tangent : TEXCOORD1;
};
VSOutput main(VSInput input, uint InstanceIndex : SV_InstanceID)
{
VSOutput output = (VSOutput)0;
float4 tmpPos = input.Pos + ubo.instancePos[InstanceIndex];
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, tmpPos)));
output.UV = input.UV;
output.UV.y = 1.0 - output.UV.y;
// Vertex position in world space
output.WorldPos = mul(ubo.model, tmpPos).xyz;
// Normal in world space
output.Normal = normalize(input.Normal);
output.Tangent = normalize(input.Tangent);
// Currently just vertex color
output.Color = input.Color;
return output;
}

View file

@ -0,0 +1,39 @@
// Copyright 2020 Google LLC
#define LIGHT_COUNT 3
struct UBO
{
float4x4 mvp[LIGHT_COUNT];
float4 instancePos[3];
};
cbuffer ubo : register(b0) { UBO ubo; }
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] int InstanceIndex : TEXCOORD0;
};
struct GSOutput
{
float4 Pos : SV_POSITION;
int Layer : SV_RenderTargetArrayIndex;
};
[maxvertexcount(3)]
[instance(3)]
void main(triangle VSOutput input[3], uint InvocationID : SV_GSInstanceID, inout TriangleStream<GSOutput> outStream)
{
float4 instancedPos = ubo.instancePos[input[0].InstanceIndex];
for (int i = 0; i < 3; i++)
{
float4 tmpPos = input[i].Pos + instancedPos;
GSOutput output = (GSOutput)0;
output.Pos = mul(ubo.mvp[InvocationID], tmpPos);
output.Layer = InvocationID;
outStream.Append( output );
}
outStream.RestartStrip();
}

View file

@ -0,0 +1,15 @@
// Copyright 2020 Google LLC
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] int InstanceIndex : TEXCOORD0;
};
VSOutput main([[vk::location(0)]] float4 Pos : POSITION0, uint InstanceIndex : SV_InstanceID)
{
VSOutput output = (VSOutput)0;
output.InstanceIndex = InstanceIndex;
output.Pos = Pos;
return output;
}