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

24
data/hlsl/ssao/blur.frag Normal file
View file

@ -0,0 +1,24 @@
// Copyright 2020 Google LLC
Texture2D textureSSAO : register(t0);
SamplerState samplerSSAO : register(s0);
float4 main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
{
const int blurRange = 2;
int n = 0;
int2 texDim;
textureSSAO.GetDimensions(texDim.x, texDim.y);
float2 texelSize = 1.0 / (float2)texDim;
float result = 0.0;
for (int x = -blurRange; x < blurRange; x++)
{
for (int y = -blurRange; y < blurRange; y++)
{
float2 offset = float2(float(x), float(y)) * texelSize;
result += textureSSAO.Sample(samplerSSAO, inUV + offset).r;
n++;
}
}
return result / (float(n));
}

View file

@ -0,0 +1,56 @@
// Copyright 2020 Google LLC
Texture2D textureposition : register(t0);
SamplerState samplerposition : register(s0);
Texture2D textureNormal : register(t1);
SamplerState samplerNormal : register(s1);
Texture2D textureAlbedo : register(t2);
SamplerState samplerAlbedo : register(s2);
Texture2D textureSSAO : register(t3);
SamplerState samplerSSAO : register(s3);
Texture2D textureSSAOBlur : register(t4);
SamplerState samplerSSAOBlur : register(s4);
struct UBO
{
float4x4 _dummy;
int ssao;
int ssaoOnly;
int ssaoBlur;
};
cbuffer uboParams : register(b5) { UBO uboParams; };
float4 main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
{
float3 fragPos = textureposition.Sample(samplerposition, inUV).rgb;
float3 normal = normalize(textureNormal.Sample(samplerNormal, inUV).rgb * 2.0 - 1.0);
float4 albedo = textureAlbedo.Sample(samplerAlbedo, inUV);
float ssao = (uboParams.ssaoBlur == 1) ? textureSSAOBlur.Sample(samplerSSAOBlur, inUV).r : textureSSAO.Sample(samplerSSAO, inUV).r;
float3 lightPos = float3(0.0, 0.0, 0.0);
float3 L = normalize(lightPos - fragPos);
float NdotL = max(0.5, dot(normal, L));
float4 outFragColor;
if (uboParams.ssaoOnly == 1)
{
outFragColor.rgb = ssao.rrr;
}
else
{
float3 baseColor = albedo.rgb * NdotL;
if (uboParams.ssao == 1)
{
outFragColor.rgb = ssao.rrr;
if (uboParams.ssaoOnly != 1)
outFragColor.rgb *= baseColor;
}
else
{
outFragColor.rgb = baseColor;
}
}
return outFragColor;
}

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 * 2.0f - 1.0f, 0.0f, 1.0f);
return output;
}

View file

@ -0,0 +1,34 @@
// Copyright 2020 Google LLC
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;
};
struct FSOutput
{
float4 Position : SV_TARGET0;
float4 Normal : SV_TARGET1;
float4 Albedo : SV_TARGET2;
};
static const float NEAR_PLANE = 0.1f; //todo: specialization const
static const float FAR_PLANE = 64.0f; //todo: specialization const
float linearDepth(float depth)
{
float z = depth * 2.0f - 1.0f;
return (2.0f * NEAR_PLANE * FAR_PLANE) / (FAR_PLANE + NEAR_PLANE - z * (FAR_PLANE - NEAR_PLANE));
}
FSOutput main(VSOutput input)
{
FSOutput output = (FSOutput)0;
output.Position = float4(input.WorldPos, linearDepth(input.Pos.z));
output.Normal = float4(normalize(input.Normal) * 0.5 + 0.5, 1.0);
output.Albedo = float4(input.Color * 2.0, 1.0);
return output;
}

View file

@ -0,0 +1,45 @@
// 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;
};
struct UBO
{
float4x4 projection;
float4x4 model;
float4x4 view;
};
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;
};
VSOutput main(VSInput input)
{
VSOutput output = (VSOutput)0;
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, input.Pos)));
output.UV = input.UV;
// Vertex position in view space
output.WorldPos = mul(ubo.view, mul(ubo.model, input.Pos)).xyz;
// Normal in view space
float3x3 normalMatrix = (float3x3)mul(ubo.view, ubo.model);
output.Normal = mul(normalMatrix, input.Normal);
output.Color = input.Color;
return output;
}

73
data/hlsl/ssao/ssao.frag Normal file
View file

@ -0,0 +1,73 @@
// Copyright 2020 Google LLC
Texture2D texturePositionDepth : register(t0);
SamplerState samplerPositionDepth : register(s0);
Texture2D textureNormal : register(t1);
SamplerState samplerNormal : register(s1);
Texture2D ssaoNoiseTexture : register(t2);
SamplerState ssaoNoiseSampler : register(s2);
#define SSAO_KERNEL_ARRAY_SIZE 64
[[vk::constant_id(0)]] const int SSAO_KERNEL_SIZE = 64;
[[vk::constant_id(1)]] const float SSAO_RADIUS = 0.5;
struct UBOSSAOKernel
{
float4 samples[SSAO_KERNEL_ARRAY_SIZE];
};
cbuffer uboSSAOKernel : register(b3) { UBOSSAOKernel uboSSAOKernel; };
struct UBO
{
float4x4 projection;
};
cbuffer ubo : register(b4) { UBO ubo; };
float main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
{
// Get G-Buffer values
float3 fragPos = texturePositionDepth.Sample(samplerPositionDepth, inUV).rgb;
float3 normal = normalize(textureNormal.Sample(samplerNormal, inUV).rgb * 2.0 - 1.0);
// Get a random vector using a noise lookup
int2 texDim;
texturePositionDepth.GetDimensions(texDim.x, texDim.y);
int2 noiseDim;
ssaoNoiseTexture.GetDimensions(noiseDim.x, noiseDim.y);
const float2 noiseUV = float2(float(texDim.x)/float(noiseDim.x), float(texDim.y)/(noiseDim.y)) * inUV;
float3 randomVec = ssaoNoiseTexture.Sample(ssaoNoiseSampler, noiseUV).xyz * 2.0 - 1.0;
// Create TBN matrix
float3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
float3 bitangent = cross(tangent, normal);
float3x3 TBN = transpose(float3x3(tangent, bitangent, normal));
// Calculate occlusion value
float occlusion = 0.0f;
for(int i = 0; i < SSAO_KERNEL_SIZE; i++)
{
float3 samplePos = mul(TBN, uboSSAOKernel.samples[i].xyz);
samplePos = fragPos + samplePos * SSAO_RADIUS;
// project
float4 offset = float4(samplePos, 1.0f);
offset = mul(ubo.projection, offset);
offset.xyz /= offset.w;
offset.xyz = offset.xyz * 0.5f + 0.5f;
float sampleDepth = -texturePositionDepth.Sample(samplerPositionDepth, offset.xy).w;
#define RANGE_CHECK 1
#ifdef RANGE_CHECK
// Range check
float rangeCheck = smoothstep(0.0f, 1.0f, SSAO_RADIUS / abs(fragPos.z - sampleDepth));
occlusion += (sampleDepth >= samplePos.z ? 1.0f : 0.0f) * rangeCheck;
#else
occlusion += (sampleDepth >= samplePos.z ? 1.0f : 0.0f);
#endif
}
occlusion = 1.0 - (occlusion / float(SSAO_KERNEL_SIZE));
return occlusion;
}