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,6 @@
// Copyright 2020 Google LLC
float4 main() : SV_TARGET
{
return float4(1.0, 0.0, 0.0, 1.0);
}

View file

@ -0,0 +1,13 @@
// Copyright 2020 Google LLC
struct UBO
{
float4x4 depthMVP;
};
cbuffer ubo : register(b0) { UBO ubo; }
float4 main([[vk::location(0)]] float3 Pos : POSITION0) : SV_POSITION
{
return mul(ubo.depthMVP, float4(Pos, 1.0));
}

View file

@ -0,0 +1,18 @@
// Copyright 2020 Google LLC
Texture2D textureColor : register(t1);
SamplerState samplerColor : register(s1);
float LinearizeDepth(float depth)
{
float n = 1.0; // camera z near
float f = 128.0; // camera z far
float z = depth;
return (2.0 * n) / (f + n - z * (f - n));
}
float4 main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
{
float depth = textureColor.Sample(samplerColor, inUV).r;
return float4((1.0-LinearizeDepth(depth)).xxx, 1.0);
}

View file

@ -0,0 +1,15 @@
// Copyright 2020 Google LLC
struct VSOutput
{
float4 Pos : SV_POSITION;
[[vk::location(0)]] float2 UV : TEXCOORD0;
};
VSOutput main(uint VertexIndex : SV_VertexID)
{
VSOutput output = (VSOutput)0;
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
output.Pos = float4(output.UV, 0.0f, 1.0f);
return output;
}

View file

@ -0,0 +1,68 @@
// Copyright 2020 Google LLC
Texture2D shadowMapTexture : register(t1);
SamplerState shadowMapSampler : register(s1);
struct VSOutput
{
[[vk::location(0)]] float3 Normal : NORMAL0;
[[vk::location(1)]] float3 Color : COLOR0;
[[vk::location(2)]] float3 ViewVec : TEXCOORD1;
[[vk::location(3)]] float3 LightVec : TEXCOORD2;
[[vk::location(4)]] float4 ShadowCoord : TEXCOORD3;
};
[[vk::constant_id(0)]] const int enablePCF = 0;
#define ambient 0.1
float textureProj(float4 shadowCoord, float2 off)
{
float shadow = 1.0;
if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 )
{
float dist = shadowMapTexture.Sample( shadowMapSampler, shadowCoord.xy + off ).r;
if ( shadowCoord.w > 0.0 && dist < shadowCoord.z )
{
shadow = ambient;
}
}
return shadow;
}
float filterPCF(float4 sc)
{
int2 texDim;
shadowMapTexture.GetDimensions(texDim.x, texDim.y);
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, float2(dx*x, dy*y));
count++;
}
}
return shadowFactor / count;
}
float4 main(VSOutput input) : SV_TARGET
{
float shadow = (enablePCF == 1) ? filterPCF(input.ShadowCoord / input.ShadowCoord.w) : textureProj(input.ShadowCoord / input.ShadowCoord.w, float2(0.0, 0.0));
float3 N = normalize(input.Normal);
float3 L = normalize(input.LightVec);
float3 V = normalize(input.ViewVec);
float3 R = normalize(-reflect(L, N));
float3 diffuse = max(dot(N, L), ambient) * input.Color;
return float4(diffuse * shadow, 1.0);
}

View file

@ -0,0 +1,54 @@
// 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;
float4x4 lightSpace;
float3 lightPos;
};
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 ViewVec : TEXCOORD1;
[[vk::location(3)]] float3 LightVec : TEXCOORD2;
[[vk::location(4)]] float4 ShadowCoord : TEXCOORD3;
};
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 );
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.Color = input.Color;
output.Normal = input.Normal;
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(input.Pos.xyz, 1.0))));
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
output.Normal = mul((float3x3)ubo.model, input.Normal);
output.LightVec = normalize(ubo.lightPos - input.Pos);
output.ViewVec = -pos.xyz;
output.ShadowCoord = mul(biasMat, mul(ubo.lightSpace, mul(ubo.model, float4(input.Pos, 1.0))));
return output;
}