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
154
data/hlsl/computecloth/cloth.comp
Normal file
154
data/hlsl/computecloth/cloth.comp
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
struct Particle {
|
||||
float4 pos;
|
||||
float4 vel;
|
||||
float4 uv;
|
||||
float4 normal;
|
||||
float pinned;
|
||||
};
|
||||
|
||||
[[vk::binding(0)]]
|
||||
StructuredBuffer<Particle> particleIn;
|
||||
[[vk::binding(1)]]
|
||||
RWStructuredBuffer<Particle> particleOut;
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float deltaT;
|
||||
float particleMass;
|
||||
float springStiffness;
|
||||
float damping;
|
||||
float restDistH;
|
||||
float restDistV;
|
||||
float restDistD;
|
||||
float sphereRadius;
|
||||
float4 spherePos;
|
||||
float4 gravity;
|
||||
int2 particleCount;
|
||||
};
|
||||
|
||||
cbuffer ubo : register(b2)
|
||||
{
|
||||
UBO params;
|
||||
};
|
||||
|
||||
struct PushConstants
|
||||
{
|
||||
uint calculateNormals;
|
||||
};
|
||||
|
||||
[[vk::push_constant]]
|
||||
PushConstants pushConstants;
|
||||
|
||||
float3 springForce(float3 p0, float3 p1, float restDist)
|
||||
{
|
||||
float3 dist = p0 - p1;
|
||||
return normalize(dist) * params.springStiffness * (length(dist) - restDist);
|
||||
}
|
||||
|
||||
[numthreads(10, 10, 1)]
|
||||
void main(uint3 id : SV_DispatchThreadID)
|
||||
{
|
||||
uint index = id.y * params.particleCount.x + id.x;
|
||||
if (index > params.particleCount.x * params.particleCount.y)
|
||||
return;
|
||||
|
||||
// Pinned?
|
||||
if (particleIn[index].pinned == 1.0) {
|
||||
particleOut[index].pos = particleOut[index].pos;
|
||||
particleOut[index].vel = float4(0, 0, 0, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Initial force from gravity
|
||||
float3 force = params.gravity.xyz * params.particleMass;
|
||||
|
||||
float3 pos = particleIn[index].pos.xyz;
|
||||
float3 vel = particleIn[index].vel.xyz;
|
||||
|
||||
// Spring forces from neighboring particles
|
||||
// left
|
||||
if (id.x > 0) {
|
||||
force += springForce(particleIn[index-1].pos.xyz, pos, params.restDistH);
|
||||
}
|
||||
// right
|
||||
if (id.x < params.particleCount.x - 1) {
|
||||
force += springForce(particleIn[index + 1].pos.xyz, pos, params.restDistH);
|
||||
}
|
||||
// upper
|
||||
if (id.y < params.particleCount.y - 1) {
|
||||
force += springForce(particleIn[index + params.particleCount.x].pos.xyz, pos, params.restDistV);
|
||||
}
|
||||
// lower
|
||||
if (id.y > 0) {
|
||||
force += springForce(particleIn[index - params.particleCount.x].pos.xyz, pos, params.restDistV);
|
||||
}
|
||||
// upper-left
|
||||
if ((id.x > 0) && (id.y < params.particleCount.y - 1)) {
|
||||
force += springForce(particleIn[index + params.particleCount.x - 1].pos.xyz, pos, params.restDistD);
|
||||
}
|
||||
// lower-left
|
||||
if ((id.x > 0) && (id.y > 0)) {
|
||||
force += springForce(particleIn[index - params.particleCount.x - 1].pos.xyz, pos, params.restDistD);
|
||||
}
|
||||
// upper-right
|
||||
if ((id.x < params.particleCount.x - 1) && (id.y < params.particleCount.y - 1)) {
|
||||
force += springForce(particleIn[index + params.particleCount.x + 1].pos.xyz, pos, params.restDistD);
|
||||
}
|
||||
// lower-right
|
||||
if ((id.x < params.particleCount.x - 1) && (id.y > 0)) {
|
||||
force += springForce(particleIn[index - params.particleCount.x + 1].pos.xyz, pos, params.restDistD);
|
||||
}
|
||||
|
||||
force += (-params.damping * vel);
|
||||
|
||||
// Integrate
|
||||
float3 f = force * (1.0 / params.particleMass);
|
||||
particleOut[index].pos = float4(pos + vel * params.deltaT + 0.5 * f * params.deltaT * params.deltaT, 1.0);
|
||||
particleOut[index].vel = float4(vel + f * params.deltaT, 0.0);
|
||||
|
||||
// Sphere collision
|
||||
float3 sphereDist = particleOut[index].pos.xyz - params.spherePos.xyz;
|
||||
if (length(sphereDist) < params.sphereRadius + 0.01) {
|
||||
// If the particle is inside the sphere, push it to the outer radius
|
||||
particleOut[index].pos.xyz = params.spherePos.xyz + normalize(sphereDist) * (params.sphereRadius + 0.01);
|
||||
// Cancel out velocity
|
||||
particleOut[index].vel = float4(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
// Normals
|
||||
if (pushConstants.calculateNormals == 1) {
|
||||
float3 normal = float3(0, 0, 0);
|
||||
float3 a, b, c;
|
||||
if (id.y > 0) {
|
||||
if (id.x > 0) {
|
||||
a = particleIn[index - 1].pos.xyz - pos;
|
||||
b = particleIn[index - params.particleCount.x - 1].pos.xyz - pos;
|
||||
c = particleIn[index - params.particleCount.x].pos.xyz - pos;
|
||||
normal += cross(a,b) + cross(b,c);
|
||||
}
|
||||
if (id.x < params.particleCount.x - 1) {
|
||||
a = particleIn[index - params.particleCount.x].pos.xyz - pos;
|
||||
b = particleIn[index - params.particleCount.x + 1].pos.xyz - pos;
|
||||
c = particleIn[index + 1].pos.xyz - pos;
|
||||
normal += cross(a,b) + cross(b,c);
|
||||
}
|
||||
}
|
||||
if (id.y < params.particleCount.y - 1) {
|
||||
if (id.x > 0) {
|
||||
a = particleIn[index + params.particleCount.x].pos.xyz - pos;
|
||||
b = particleIn[index + params.particleCount.x - 1].pos.xyz - pos;
|
||||
c = particleIn[index - 1].pos.xyz - pos;
|
||||
normal += cross(a,b) + cross(b,c);
|
||||
}
|
||||
if (id.x < params.particleCount.x - 1) {
|
||||
a = particleIn[index + 1].pos.xyz - pos;
|
||||
b = particleIn[index + params.particleCount.x + 1].pos.xyz - pos;
|
||||
c = particleIn[index + params.particleCount.x].pos.xyz - pos;
|
||||
normal += cross(a,b) + cross(b,c);
|
||||
}
|
||||
}
|
||||
particleOut[index].normal = float4(normalize(normal), 0.0f);
|
||||
}
|
||||
}
|
||||
24
data/hlsl/computecloth/cloth.frag
Normal file
24
data/hlsl/computecloth/cloth.frag
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
Texture2D textureColor : register(t1);
|
||||
SamplerState samplerColor : register(s1);
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
[[vk::location(0)]]float2 UV : TEXCOORD0;
|
||||
[[vk::location(1)]]float3 Normal : NORMAL0;
|
||||
[[vk::location(2)]]float3 ViewVec : TEXCOORD1;
|
||||
[[vk::location(3)]]float3 LightVec : TEXCOORD2;
|
||||
};
|
||||
|
||||
float4 main (VSOutput input) : SV_TARGET
|
||||
{
|
||||
float3 color = textureColor.Sample(samplerColor, input.UV).rgb;
|
||||
float3 N = normalize(input.Normal);
|
||||
float3 L = normalize(input.LightVec);
|
||||
float3 V = normalize(input.ViewVec);
|
||||
float3 R = reflect(-L, N);
|
||||
float3 diffuse = max(dot(N, L), 0.15) * float3(1, 1, 1);
|
||||
float3 specular = pow(max(dot(R, V), 0.0), 8.0) * float3(0.2, 0.2, 0.2);
|
||||
return float4(diffuse * color.rgb + specular, 1.0);
|
||||
}
|
||||
43
data/hlsl/computecloth/cloth.vert
Normal file
43
data/hlsl/computecloth/cloth.vert
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
struct VSInput
|
||||
{
|
||||
[[vk::location(0)]] float3 Pos : POSITION0;
|
||||
[[vk::location(1)]] float2 UV : TEXCOORD0;
|
||||
[[vk::location(2)]] float3 Normal : NORMAL0;
|
||||
};
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float2 UV : TEXCOORD0;
|
||||
[[vk::location(1)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(2)]] float3 ViewVec : TEXCOORD1;
|
||||
[[vk::location(3)]] float3 LightVec : TEXCOORD2;
|
||||
};
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 modelview;
|
||||
float4 lightPos;
|
||||
};
|
||||
|
||||
cbuffer ubo : register(b0)
|
||||
{
|
||||
UBO ubo;
|
||||
};
|
||||
|
||||
VSOutput main (VSInput input)
|
||||
{
|
||||
VSOutput output = (VSOutput)0;
|
||||
output.UV = input.UV;
|
||||
output.Normal = input.Normal.xyz;
|
||||
float4 eyePos = mul(ubo.modelview, float4(input.Pos.x, input.Pos.y, input.Pos.z, 1.0));
|
||||
output.Pos = mul(ubo.projection, eyePos);
|
||||
float4 pos = float4(input.Pos, 1.0);
|
||||
float3 lPos = ubo.lightPos.xyz;
|
||||
output.LightVec = lPos - pos.xyz;
|
||||
output.ViewVec = -pos.xyz;
|
||||
return output;
|
||||
}
|
||||
20
data/hlsl/computecloth/sphere.frag
Normal file
20
data/hlsl/computecloth/sphere.frag
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
[[vk::location(0)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(1)]] float3 ViewVec : TEXCOORD0;
|
||||
[[vk::location(2)]] float3 LightVec : TEXCOORD1;
|
||||
};
|
||||
|
||||
float4 main (VSOutput input) : SV_TARGET
|
||||
{
|
||||
float3 color = float3(0.5, 0.5, 0.5);
|
||||
float3 N = normalize(input.Normal);
|
||||
float3 L = normalize(input.LightVec);
|
||||
float3 V = normalize(input.ViewVec);
|
||||
float3 R = reflect(-L, N);
|
||||
float3 diffuse = max(dot(N, L), 0.15);
|
||||
float3 specular = pow(max(dot(R, V), 0.0), 32.0);
|
||||
return float4(diffuse * color.rgb + specular, 1.0);
|
||||
}
|
||||
41
data/hlsl/computecloth/sphere.vert
Normal file
41
data/hlsl/computecloth/sphere.vert
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// Copyright 2020 Google LLC
|
||||
|
||||
struct VSInput
|
||||
{
|
||||
[[vk::location(0)]]float3 Pos : POSITION0;
|
||||
[[vk::location(2)]]float3 Normal : NORMAL0;
|
||||
};
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(1)]] float3 ViewVec : TEXCOORD0;
|
||||
[[vk::location(2)]] float3 LightVec : TEXCOORD1;
|
||||
};
|
||||
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 modelview;
|
||||
float4 lightPos;
|
||||
};
|
||||
|
||||
cbuffer ubo : register(b0)
|
||||
{
|
||||
UBO ubo;
|
||||
};
|
||||
|
||||
VSOutput main (VSInput input)
|
||||
{
|
||||
VSOutput output = (VSOutput)0;
|
||||
float4 eyePos = mul(ubo.modelview, float4(input.Pos.x, input.Pos.y, input.Pos.z, 1.0));
|
||||
output.Pos = mul(ubo.projection, eyePos);
|
||||
float4 pos = float4(input.Pos, 1.0);
|
||||
float3 lPos = ubo.lightPos.xyz;
|
||||
output.LightVec = lPos - pos.xyz;
|
||||
output.ViewVec = -pos.xyz;
|
||||
output.Normal = input.Normal;
|
||||
return output;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue