forked from LukasBanana/LLGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.hlsl
278 lines (226 loc) · 7.69 KB
/
Example.hlsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* HLSL cloth physics shader
*/
cbuffer SceneState : register(b0)
{
float4x4 wvpMatrix;
float4x4 wMatrix;
float4 gravity;
uint2 gridSize;
uint2 _pad0;
float damping;
float dTime;
float dStiffness;
float _pad1;
float4 lightVec;
};
// Sub view of a particle
struct ParticleView
{
float4 currPos;
float4 nextPos;
float4 origPos;
float4 normal;
float invMass;
};
// Particle buffers
#ifdef ENABLE_STORAGE_TEXTURES
Texture2D<float4> parBase : register(t1); // UV (.xy) and inverse mass (.z)
RWTexture2D<float4> parCurrPos : register(u2);
RWTexture2D<float4> parNextPos : register(u3);
RWTexture2D<float4> parPrevPos : register(u4);
RWTexture2D<float4> parVelocity : register(u5);
RWTexture2D<float4> parNormal : register(u6);
#else
Buffer<float4> parBase : register(t1); // UV (.xy) and inverse mass (.z)
RWBuffer<float4> parCurrPos : register(u2);
RWBuffer<float4> parNextPos : register(u3);
RWBuffer<float4> parPrevPos : register(u4);
RWBuffer<float4> parVelocity : register(u5);
RWBuffer<float4> parNormal : register(u6);
#endif // /ENABLE_STORAGE_TEXTURES
// Returns the particle index for the specified grid
uint GridPosToIndex(uint2 gridPos)
{
return (gridPos.y * gridSize.x + gridPos.x);
}
// Converts the specified grid UV coordinates to the original vertex coordinates.
// Only distance between those coordinates are important.
float4 UVToOrigPos(float2 uv)
{
return float4(uv.x * 2.0 - 1.0, 0.0, uv.y * -2.0, 1.0);
}
void AccumulateStretchConstraints(ParticleView par, int2 neighborGridPos, inout float3 dCorrection)
{
if (neighborGridPos.x < 0 || (uint)neighborGridPos.x >= gridSize.x ||
neighborGridPos.y < 0 || (uint)neighborGridPos.y >= gridSize.y)
{
return;
}
// Read neighbor particle
#ifdef ENABLE_STORAGE_TEXTURES
uint2 idx = (uint2)neighborGridPos;
#else
uint idx = GridPosToIndex((uint2)neighborGridPos);
#endif // /ENABLE_STORAGE_TEXTURES
float4 otherCurrPos = parCurrPos[idx];
float4 otherOrigPos = UVToOrigPos(parBase[idx].xy);
float otherInvMass = parBase[idx].z;
// Compute edge distance between particle and its neighbor
float3 dPos = (par.nextPos - otherCurrPos).xyz;
float currDist = length(dPos);
float edgeDist = distance(par.origPos, otherOrigPos);
// Compute stretch constraint
dPos = normalize(dPos) * ((currDist - edgeDist) / (par.invMass + otherInvMass));
// Adjust position
dCorrection += (dPos * -par.invMass);
}
float3 ReadParticlePos(uint2 gridPos)
{
#ifdef ENABLE_STORAGE_TEXTURES
return parCurrPos[gridPos].xyz;
#else
return parCurrPos[GridPosToIndex(gridPos)].xyz;
#endif // /ENABLE_STORAGE_TEXTURES
}
void AccumulateSurfaceNormal(float4 pos, int2 gridPos0, int2 gridPos1, inout float4 normal)
{
if (gridPos0.x < 0 || (uint)gridPos0.x >= gridSize.x ||
gridPos0.y < 0 || (uint)gridPos0.y >= gridSize.y ||
gridPos1.x < 0 || (uint)gridPos1.x >= gridSize.x ||
gridPos1.y < 0 || (uint)gridPos1.y >= gridSize.y)
{
return;
}
float3 v0 = ReadParticlePos((uint2)gridPos0) - pos.xyz;
float3 v1 = ReadParticlePos((uint2)gridPos1) - pos.xyz;
normal.xyz += cross(v0, v1);
}
void ApplyStretchConstraints(inout ParticleView par, int2 gridPos)
{
if (par.invMass == 0.0)
{
return;
}
// Apply stretch constraints
float3 dPos = (float3)0;
AccumulateStretchConstraints(par, gridPos + int2( 0, -1), dPos);
AccumulateStretchConstraints(par, gridPos + int2( 0, +1), dPos);
AccumulateStretchConstraints(par, gridPos + int2(-1, 0), dPos);
AccumulateStretchConstraints(par, gridPos + int2(+1, 0), dPos);
AccumulateStretchConstraints(par, gridPos + int2(-1, -1), dPos);
AccumulateStretchConstraints(par, gridPos + int2(+1, -1), dPos);
AccumulateStretchConstraints(par, gridPos + int2(-1, +1), dPos);
AccumulateStretchConstraints(par, gridPos + int2(+1, +1), dPos);
dPos /= 8.0;
// Compute normal
float4 normal = (float4)0;
AccumulateSurfaceNormal(par.currPos, gridPos + int2( 0, +1), gridPos + int2(+1, 0), normal);
AccumulateSurfaceNormal(par.currPos, gridPos + int2(+1, 0), gridPos + int2( 0, -1), normal);
AccumulateSurfaceNormal(par.currPos, gridPos + int2( 0, -1), gridPos + int2(-1, 0), normal);
AccumulateSurfaceNormal(par.currPos, gridPos + int2(-1, 0), gridPos + int2( 0, +1), normal);
par.normal = normal / 4.0;
// Adjust position by correction vector
par.nextPos.xyz += dPos * dStiffness;
}
[numthreads(1, 1, 1)]
void CSForces(uint2 threadID : SV_DispatchThreadID)
{
#ifdef ENABLE_STORAGE_TEXTURES
uint2 idx = threadID;
#else
uint idx = GridPosToIndex(threadID);
#endif // /ENABLE_STORAGE_TEXTURES
// Accumulate force and multiply by inverse mass
float invMass = parBase[idx].z;
float4 force = gravity;
force *= invMass;
// Apply velocity and damping
parVelocity[idx] += force * dTime * damping;
// Apply position based physics simulation
parCurrPos[idx] += float4(parVelocity[idx].xyz, 0.0) * dTime;
}
[numthreads(1, 1, 1)]
void CSStretchConstraints(uint2 threadID : SV_DispatchThreadID)
{
#ifdef ENABLE_STORAGE_TEXTURES
uint2 idx = threadID;
#else
uint idx = GridPosToIndex(threadID);
#endif // /ENABLE_STORAGE_TEXTURES
// Read particle
ParticleView par;
par.currPos = parCurrPos[idx];
par.nextPos = par.currPos;
par.origPos = UVToOrigPos(parBase[idx].xy);
par.normal = parNormal[idx];
par.invMass = parBase[idx].z;
// Apply stretch constraints
ApplyStretchConstraints(par, (int2)threadID);
// Write next position back to swap-buffer
parNextPos[idx] = par.nextPos;
parNormal[idx] = par.normal;
}
[numthreads(1, 1, 1)]
void CSRelaxation(uint2 threadID : SV_DispatchThreadID)
{
#ifdef ENABLE_STORAGE_TEXTURES
uint2 idx = threadID;
#else
uint idx = GridPosToIndex(threadID);
#endif // /ENABLE_STORAGE_TEXTURES
// Adjust velocity and store current and previous position
parCurrPos[idx] = parNextPos[idx];
parVelocity[idx] = (parCurrPos[idx] - parPrevPos[idx]) / dTime;
parPrevPos[idx] = parCurrPos[idx];
}
/*
* HLSL vertex shader
*/
struct VOut
{
float4 position : SV_Position;
float4 normal : NORMAL;
float2 texCoord : TEXCOORD;
};
#ifdef ENABLE_STORAGE_TEXTURES
Texture2D<float4> vertexBase : register(t1); // UV (.xy) and inverse mass (.z)
Texture2D<float4> vertexPos : register(t2);
Texture2D<float4> vertexNormal : register(t3);
void VS(uint id : SV_VertexID, out VOut outp)
{
uint2 idx = uint2(id % gridSize.x, id / gridSize.x);
outp.position = mul(wvpMatrix, vertexPos[idx]);
outp.normal = mul(wMatrix, vertexNormal[idx]);
outp.texCoord = vertexBase[idx].xy;
}
#else
struct VIn
{
float4 position : POS;
float4 normal : NORMAL;
float2 texCoord : TEXCOORD;
};
void VS(in VIn inp, out VOut outp)
{
outp.position = mul(wvpMatrix, inp.position);
outp.normal = mul(wMatrix, inp.normal);
outp.texCoord = inp.texCoord;
}
#endif // /ENABLE_STORAGE_TEXTURES
/*
* HLSL pixel shader
*/
Texture2D colorMap : register(t0);
SamplerState linearSampler : register(s0);
float4 PS(in VOut inp, bool frontFace : SV_IsFrontFace) : SV_Target0
{
// Compute lighting
float3 normal = normalize(inp.normal.xyz);
normal *= lerp(1.0, -1.0, frontFace);
float NdotL = lerp(0.2, 1.0, max(0.0, dot(normal, -lightVec.xyz)));
// Sample color texture
float4 color = colorMap.Sample(linearSampler, inp.texCoord);
color.rgb = lerp(color.rgb, float3(inp.texCoord, 1.0), 0.5);
return float4(color.rgb * NdotL, color.a);
}