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:
parent
10a1ecaf7b
commit
cce75f1859
287 changed files with 11263 additions and 0 deletions
115
data/hlsl/computecullandlod/cull.comp
Normal file
115
data/hlsl/computecullandlod/cull.comp
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
#define MAX_LOD_LEVEL_COUNT 6
|
||||
[[vk::constant_id(0)]] const int MAX_LOD_LEVEL = 5;
|
||||
|
||||
struct InstanceData
|
||||
{
|
||||
float3 pos;
|
||||
float scale;
|
||||
};
|
||||
|
||||
StructuredBuffer<InstanceData> instances : register(t0);
|
||||
|
||||
// Same layout as VkDrawIndexedIndirectCommand
|
||||
struct IndexedIndirectCommand
|
||||
{
|
||||
uint indexCount;
|
||||
uint instanceCount;
|
||||
uint firstIndex;
|
||||
uint vertexOffset;
|
||||
uint firstInstance;
|
||||
};
|
||||
|
||||
RWStructuredBuffer<IndexedIndirectCommand> indirectDraws : register(u1);
|
||||
|
||||
// Binding 2: Uniform block object with matrices
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 modelview;
|
||||
float4 cameraPos;
|
||||
float4 frustumPlanes[6];
|
||||
};
|
||||
|
||||
cbuffer ubo : register(b2) { UBO ubo; }
|
||||
|
||||
// Binding 3: Indirect draw stats
|
||||
struct UBOOut
|
||||
{
|
||||
uint drawCount;
|
||||
uint lodCount[MAX_LOD_LEVEL_COUNT];
|
||||
};
|
||||
RWStructuredBuffer<UBOOut> uboOut : register(u3);
|
||||
|
||||
// Binding 4: level-of-detail information
|
||||
struct LOD
|
||||
{
|
||||
uint firstIndex;
|
||||
uint indexCount;
|
||||
float distance;
|
||||
float _pad0;
|
||||
};
|
||||
|
||||
StructuredBuffer<LOD> lods : register(t4);
|
||||
|
||||
[numthreads(16, 1, 1)]
|
||||
bool frustumCheck(float4 pos, float radius)
|
||||
{
|
||||
// Check sphere against frustum planes
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
if (dot(pos, ubo.frustumPlanes[i]) + radius < 0.0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
[numthreads(16, 1, 1)]
|
||||
void main(uint3 GlobalInvocationID : SV_DispatchThreadID )
|
||||
{
|
||||
uint idx = GlobalInvocationID.x;
|
||||
uint temp;
|
||||
|
||||
// Clear stats on first invocation
|
||||
if (idx == 0)
|
||||
{
|
||||
InterlockedExchange(uboOut[0].drawCount, 0, temp);
|
||||
for (uint i = 0; i < MAX_LOD_LEVEL + 1; i++)
|
||||
{
|
||||
InterlockedExchange(uboOut[0].lodCount[i], 0, temp);
|
||||
}
|
||||
}
|
||||
|
||||
float4 pos = float4(instances[idx].pos.xyz, 1.0);
|
||||
|
||||
// Check if object is within current viewing frustum
|
||||
if (frustumCheck(pos, 1.0))
|
||||
{
|
||||
indirectDraws[idx].instanceCount = 1;
|
||||
|
||||
// Increase number of indirect draw counts
|
||||
InterlockedAdd(uboOut[0].drawCount, 1, temp);
|
||||
|
||||
// Select appropriate LOD level based on distance to camera
|
||||
uint lodLevel = MAX_LOD_LEVEL;
|
||||
for (uint i = 0; i < MAX_LOD_LEVEL; i++)
|
||||
{
|
||||
if (distance(instances[idx].pos.xyz, ubo.cameraPos.xyz) < lods[i].distance)
|
||||
{
|
||||
lodLevel = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
indirectDraws[idx].firstIndex = lods[lodLevel].firstIndex;
|
||||
indirectDraws[idx].indexCount = lods[lodLevel].indexCount;
|
||||
// Update stats
|
||||
InterlockedAdd(uboOut[0].lodCount[lodLevel], 1, temp);
|
||||
}
|
||||
else
|
||||
{
|
||||
indirectDraws[idx].instanceCount = 0;
|
||||
}
|
||||
}
|
||||
18
data/hlsl/computecullandlod/indirectdraw.frag
Normal file
18
data/hlsl/computecullandlod/indirectdraw.frag
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
float4 main(VSOutput input) : SV_TARGET
|
||||
{
|
||||
float3 N = normalize(input.Normal);
|
||||
float3 L = normalize(input.LightVec);
|
||||
float3 ambient = float3(0.25, 0.25, 0.25);
|
||||
float3 diffuse = max(dot(N, L), 0.0).xxx;
|
||||
return float4((ambient + diffuse) * input.Color, 1.0);
|
||||
}
|
||||
46
data/hlsl/computecullandlod/indirectdraw.vert
Normal file
46
data/hlsl/computecullandlod/indirectdraw.vert
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
struct VSInput
|
||||
{
|
||||
[[vk::location(0)]] float4 Pos : POSITION0;
|
||||
[[vk::location(1)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(2)]] float3 Color : COLOR0;
|
||||
// Instanced attributes
|
||||
[[vk::location(4)]] float3 instancePos : TEXCOORD0;
|
||||
[[vk::location(5)]] float instanceScale : TEXCOORD1;
|
||||
};
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 modelview;
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
VSOutput main(VSInput input)
|
||||
{
|
||||
VSOutput output = (VSOutput)0;
|
||||
output.Color = input.Color;
|
||||
|
||||
output.Normal = input.Normal;
|
||||
|
||||
float4 pos = float4((input.Pos.xyz * input.instanceScale) + input.instancePos, 1.0);
|
||||
|
||||
output.Pos = mul(ubo.projection, mul(ubo.modelview, pos));
|
||||
|
||||
float4 wPos = mul(ubo.modelview, float4(pos.xyz, 1.0));
|
||||
float4 lPos = float4(0.0, 10.0, 50.0, 1.0);
|
||||
output.LightVec = lPos.xyz - pos.xyz;
|
||||
output.ViewVec = -pos.xyz;
|
||||
return output;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue