forked from LukasBanana/LLGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.metal
263 lines (221 loc) · 8.37 KB
/
Example.metal
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
/*
* Metal cloth physics shader
*/
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct SceneState
{
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;
};
// Returns the particle index for the specified grid
uint GridPosToIndex(uint2 gridPos, uint gridWidth)
{
return (gridPos.y * gridWidth + 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(
thread const ParticleView& par,
device float4* parCurrPos,
device float4* parBase,
int2 neighborGridPos,
uint2 gridSize,
thread float3& dCorrection)
{
if (neighborGridPos.x < 0 || (uint)neighborGridPos.x >= gridSize.x ||
neighborGridPos.y < 0 || (uint)neighborGridPos.y >= gridSize.y)
{
return;
}
// Read neighbor particle
uint idx = GridPosToIndex((uint2)neighborGridPos, gridSize.x);
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(
device float4* parCurrPos,
uint2 gridPos,
uint gridWidth)
{
return parCurrPos[GridPosToIndex(gridPos, gridWidth)].xyz;
}
void AccumulateSurfaceNormal(
float4 pos,
device float4* parCurrPos,
int2 gridPos0,
int2 gridPos1,
uint2 gridSize,
thread 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(parCurrPos, (uint2)gridPos0, gridSize.x) - pos.xyz;
float3 v1 = ReadParticlePos(parCurrPos, (uint2)gridPos1, gridSize.x) - pos.xyz;
normal.xyz += cross(v0, v1);
}
void ApplyStretchConstraints(
thread ParticleView& par,
device float4* parCurrPos,
device float4* parBase,
constant SceneState& sceneState,
int2 gridPos)
{
if (par.invMass == 0.0)
{
return;
}
// Apply stretch constraints
float3 dPos = (float3)0;
AccumulateStretchConstraints(par, parCurrPos, parBase, gridPos + int2( 0, -1), sceneState.gridSize, dPos);
AccumulateStretchConstraints(par, parCurrPos, parBase, gridPos + int2( 0, +1), sceneState.gridSize, dPos);
AccumulateStretchConstraints(par, parCurrPos, parBase, gridPos + int2(-1, 0), sceneState.gridSize, dPos);
AccumulateStretchConstraints(par, parCurrPos, parBase, gridPos + int2(+1, 0), sceneState.gridSize, dPos);
AccumulateStretchConstraints(par, parCurrPos, parBase, gridPos + int2(-1, -1), sceneState.gridSize, dPos);
AccumulateStretchConstraints(par, parCurrPos, parBase, gridPos + int2(+1, -1), sceneState.gridSize, dPos);
AccumulateStretchConstraints(par, parCurrPos, parBase, gridPos + int2(-1, +1), sceneState.gridSize, dPos);
AccumulateStretchConstraints(par, parCurrPos, parBase, gridPos + int2(+1, +1), sceneState.gridSize, dPos);
dPos /= 8.0;
// Compute normal
float4 normal = (float4)0;
AccumulateSurfaceNormal(par.currPos, parCurrPos, gridPos + int2( 0, +1), gridPos + int2(+1, 0), sceneState.gridSize, normal);
AccumulateSurfaceNormal(par.currPos, parCurrPos, gridPos + int2(+1, 0), gridPos + int2( 0, -1), sceneState.gridSize, normal);
AccumulateSurfaceNormal(par.currPos, parCurrPos, gridPos + int2( 0, -1), gridPos + int2(-1, 0), sceneState.gridSize, normal);
AccumulateSurfaceNormal(par.currPos, parCurrPos, gridPos + int2(-1, 0), gridPos + int2( 0, +1), sceneState.gridSize, normal);
par.normal = normal / 4.0;
// Adjust position by correction vector
par.nextPos.xyz += dPos * sceneState.dStiffness;
}
kernel void CSForces(
uint2 threadID [[thread_position_in_grid]],
constant SceneState& sceneState [[buffer(0)]],
device float4* parBase [[buffer(1)]], // UV (.xy) and inverse mass (.z)
device float4* parCurrPos [[buffer(2)]],
device float4* parVelocity [[buffer(5)]])
{
uint idx = GridPosToIndex(threadID, sceneState.gridSize.x);
// Accumulate force and multiply by inverse mass
float invMass = parBase[idx].z;
float4 force = sceneState.gravity;
force *= invMass;
// Apply velocity and damping
parVelocity[idx] += force * sceneState.dTime * sceneState.damping;
// Apply position based physics simulation
parCurrPos[idx] += float4(parVelocity[idx].xyz, 0.0) * sceneState.dTime;
}
kernel void CSStretchConstraints(
uint2 threadID [[thread_position_in_grid]],
constant SceneState& sceneState [[buffer(0)]],
device float4* parBase [[buffer(1)]], // UV (.xy) and inverse mass (.z)
device float4* parCurrPos [[buffer(2)]],
device float4* parNextPos [[buffer(3)]],
device float4* parNormal [[buffer(6)]])
{
uint idx = GridPosToIndex(threadID, sceneState.gridSize.x);
// 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, parCurrPos, parBase, sceneState, (int2)threadID);
// Write next position back to swap-buffer
parNextPos[idx] = par.nextPos;
parNormal[idx] = par.normal;
}
kernel void CSRelaxation(
uint2 threadID [[thread_position_in_grid]],
constant SceneState& sceneState [[buffer(0)]],
device float4* parCurrPos [[buffer(2)]],
device float4* parNextPos [[buffer(3)]],
device float4* parPrevPos [[buffer(4)]],
device float4* parVelocity [[buffer(5)]])
{
uint idx = GridPosToIndex(threadID, sceneState.gridSize.x);
// Adjust velocity and store current and previous position
parCurrPos[idx] = parNextPos[idx];
parVelocity[idx] = (parCurrPos[idx] - parPrevPos[idx]) / sceneState.dTime;
parPrevPos[idx] = parCurrPos[idx];
}
/*
* Metal vertex shader
*/
struct VIn
{
float4 position [[attribute(0)]];
float4 normal [[attribute(1)]];
float2 texCoord [[attribute(2)]];
};
struct VOut
{
float4 position [[position]];
float4 normal;
float2 texCoord;
};
vertex VOut VS(
VIn inp [[stage_in]],
constant SceneState& sceneState [[buffer(3)]])
{
VOut outp;
outp.position = sceneState.wvpMatrix * inp.position;
outp.normal = sceneState.wMatrix * inp.normal;
outp.texCoord = inp.texCoord;
return outp;
}
/*
* Metal fragment shader
*/
fragment float4 PS(
VOut inp [[stage_in]],
constant SceneState& sceneState [[buffer(3)]],
texture2d<float> colorMap [[texture(4)]],
sampler linearSampler [[sampler(5)]],
bool frontFace [[front_facing]])
{
// Compute lighting
float3 normal = normalize(inp.normal.xyz);
normal *= select(1.0, -1.0, frontFace);
float NdotL = mix(0.2, 1.0, max(0.0, dot(normal, -sceneState.lightVec.xyz)));
// Sample color texture
float4 color = colorMap.sample(linearSampler, inp.texCoord);
color.rgb = mix(color.rgb, float3(inp.texCoord, 1.0), 0.5);
return float4(color.rgb * NdotL, color.a);
}