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,16 @@
// Copyright 2020 Google LLC
Texture2DArray shadowMapTexture : register(t1);
SamplerState shadowMapSampler : register(s1);
struct VSOutput
{
[[vk::location(0)]] float2 UV : TEXCOORD0;
[[vk::location(1)]] uint CascadeIndex : TEXCOORD1;
};
float4 main(VSOutput input) : SV_TARGET
{
float depth = shadowMapTexture.Sample(shadowMapSampler, float3(input.UV, float(input.CascadeIndex))).r;
return float4(depth.xxx, 1.0);
}

View file

@ -0,0 +1,23 @@
// Copyright 2020 Google LLC
struct PushConsts {
float4 position;
uint cascadeIndex;
};
[[vk::push_constant]] PushConsts pushConsts;
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float2 UV : TEXCOORD0;
[[vk::location(1)]] uint CascadeIndex : TEXCOORD1;
};
VSOutput main(uint VertexIndex : SV_VertexID)
{
VSOutput output = (VSOutput)0;
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
output.CascadeIndex = pushConsts.cascadeIndex;
output.Pos = float4(output.UV * 2.0f - 1.0f, 0.0f, 1.0f);
return output;
}

View file

@ -0,0 +1,12 @@
// Copyright 2020 Google LLC
Texture2D colorMapTexture : register(t0, space1);
SamplerState colorMapSampler : register(s0, space1);
void main([[vk::location(0)]] float2 inUV : TEXCOORD0)
{
float alpha = colorMapTexture.Sample(colorMapSampler, inUV).a;
if (alpha < 0.5) {
clip(-1);
}
}

View file

@ -0,0 +1,36 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float3 Pos : POSITION0;
[[vk::location(1)]] float2 UV : TEXCOORD0;
};
// todo: pass via specialization constant
#define SHADOW_MAP_CASCADE_COUNT 4
struct PushConsts {
float4 position;
uint cascadeIndex;
};
[[vk::push_constant]] PushConsts pushConsts;
struct UBO {
float4x4 cascadeViewProjMat[SHADOW_MAP_CASCADE_COUNT];
};
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;
float3 pos = input.Pos + pushConsts.position.xyz;
output.Pos = mul(ubo.cascadeViewProjMat[pushConsts.cascadeIndex], float4(pos, 1.0));
return output;
}

View file

@ -0,0 +1,131 @@
// Copyright 2020 Google LLC
#define SHADOW_MAP_CASCADE_COUNT 4
Texture2DArray shadowMapTexture : register(t1);
SamplerState shadowMapSampler : register(s1);
Texture2D colorMapTexture : register(t0, space1);
SamplerState colorMapSampler : register(s0, space1);
struct VSOutput
{
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float3 Color : COLOR0;
[[vk::location(2)]] float3 ViewPos : POSITION1;
[[vk::location(3)]] float3 Pos : POSITION0;
[[vk::location(4)]] float2 UV : TEXCOORD0;
};
[[vk::constant_id(0)]] const int enablePCF = 0;
#define ambient 0.3
struct UBO {
float4 cascadeSplits;
float4x4 cascadeViewProjMat[SHADOW_MAP_CASCADE_COUNT];
float4x4 inverseViewMat;
float3 lightDir;
float _pad;
int colorCascades;
};
cbuffer ubo : register(b2) { UBO ubo; };
static const float4x4 biasMat = float4x4(
0.5, 0.0, 0.0, 0.5,
0.0, 0.5, 0.0, 0.5,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
);
float textureProj(float4 shadowCoord, float2 offset, uint cascadeIndex)
{
float shadow = 1.0;
float bias = 0.005;
if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 ) {
float dist = shadowMapTexture.Sample(shadowMapSampler, float3(shadowCoord.xy + offset, cascadeIndex)).r;
if (shadowCoord.w > 0 && dist < shadowCoord.z - bias) {
shadow = ambient;
}
}
return shadow;
}
float filterPCF(float4 sc, uint cascadeIndex)
{
int3 texDim;
shadowMapTexture.GetDimensions(texDim.x, texDim.y, texDim.z);
float scale = 0.75;
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, float2(dx*x, dy*y), cascadeIndex);
count++;
}
}
return shadowFactor / count;
}
float4 main(VSOutput input) : SV_TARGET
{
float4 outFragColor;
float4 color = colorMapTexture.Sample(colorMapSampler, input.UV);
if (color.a < 0.5) {
clip(-1);
}
// Get cascade index for the current fragment's view position
uint cascadeIndex = 0;
for(uint i = 0; i < SHADOW_MAP_CASCADE_COUNT - 1; ++i) {
if(input.ViewPos.z < ubo.cascadeSplits[i]) {
cascadeIndex = i + 1;
}
}
// Depth compare for shadowing
float4 shadowCoord = mul(biasMat, mul(ubo.cascadeViewProjMat[cascadeIndex], float4(input.Pos, 1.0)));
float shadow = 0;
if (enablePCF == 1) {
shadow = filterPCF(shadowCoord / shadowCoord.w, cascadeIndex);
} else {
shadow = textureProj(shadowCoord / shadowCoord.w, float2(0.0, 0.0), cascadeIndex);
}
// Directional light
float3 N = normalize(input.Normal);
float3 L = normalize(-ubo.lightDir);
float3 H = normalize(L + input.ViewPos);
float diffuse = max(dot(N, L), ambient);
float3 lightColor = float3(1.0, 1.0, 1.0);
outFragColor.rgb = max(lightColor * (diffuse * color.rgb), float3(0.0, 0.0, 0.0));
outFragColor.rgb *= shadow;
outFragColor.a = color.a;
// Color cascades (if enabled)
if (ubo.colorCascades == 1) {
switch(cascadeIndex) {
case 0 :
outFragColor.rgb *= float3(1.0f, 0.25f, 0.25f);
break;
case 1 :
outFragColor.rgb *= float3(0.25f, 1.0f, 0.25f);
break;
case 2 :
outFragColor.rgb *= float3(0.25f, 0.25f, 1.0f);
break;
case 3 :
outFragColor.rgb *= float3(1.0f, 1.0f, 0.25f);
break;
}
}
return outFragColor;
}

View file

@ -0,0 +1,47 @@
// Copyright 2020 Google LLC
struct VSInput
{
[[vk::location(0)]] float3 Pos : POSITION0;
[[vk::location(1)]] float2 UV : TEXCOORD0;
[[vk::location(2)]] float3 Color : COLOR0;
[[vk::location(3)]] float3 Normal : NORMAL0;
};
struct UBO {
float4x4 projection;
float4x4 view;
float4x4 model;
};
cbuffer ubo : register(b0) { UBO ubo; }
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float3 Color : COLOR0;
[[vk::location(2)]] float3 ViewPos : POSITION1;
[[vk::location(3)]] float3 WorldPos : POSITION0;
[[vk::location(4)]] float2 UV : TEXCOORD0;
};
struct PushConsts {
float4 position;
uint cascadeIndex;
};
[[vk::push_constant]] PushConsts pushConsts;
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.Color = input.Color;
output.Normal = input.Normal;
output.UV = input.UV;
float3 pos = input.Pos + pushConsts.position.xyz;
output.WorldPos = pos;
output.ViewPos = mul(ubo.view, float4(pos.xyz, 1.0)).xyz;
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(pos.xyz, 1.0))));
return output;
}