载入中...
搜索中...
未找到
ShallowWaterKernels.h
浏览该文件的文档.
1#pragma once
2
30namespace eve::fluids {
31
37inline const char* kShallowWaterClearSources = R"GLSL(
38#version 450
39layout(local_size_x = 8, local_size_y = 8) in;
40layout(set = 0, binding = 3) buffer Sources { vec4 s[]; } sources;
41layout(push_constant) uniform PC { float d[32]; } pc;
42void main() {
43 uint x = gl_GlobalInvocationID.x;
44 uint z = gl_GlobalInvocationID.y;
45 uint resX = uint(pc.d[0]);
46 uint resZ = uint(pc.d[1]);
47 if (x >= resX || z >= resZ) return;
48 sources.s[z * resX + x] = vec4(0.0);
49}
50)GLSL";
51
57inline const char* kShallowWaterRasterizeColliders = R"GLSL(
58#version 450
59layout(local_size_x = 8, local_size_y = 8) in;
60layout(set = 0, binding = 0) readonly buffer StateSrc { vec4 q[]; } stateSrc;
61layout(set = 0, binding = 2) readonly buffer Bed { float b[]; } bed;
62layout(set = 0, binding = 3) buffer Sources { vec4 s[]; } sources;
63layout(set = 0, binding = 4) readonly buffer Colliders { float c[]; } colliders;
64layout(push_constant) uniform PC { float d[32]; } pc;
65
66float capsuleDistanceXZ(vec2 p, vec3 center, float halfHeight, float radius) {
67 // Vertical capsule: segment along local Y, footprint is a disk of `radius`
68 // around the capsule axis projected onto XZ.
69 vec2 d = p - center.xz;
70 return length(d) - radius;
71}
72
73void main() {
74 uint x = gl_GlobalInvocationID.x;
75 uint z = gl_GlobalInvocationID.y;
76 uint resX = uint(pc.d[0]);
77 uint resZ = uint(pc.d[1]);
78 if (x >= resX || z >= resZ) return;
79
80 float cell = pc.d[2];
81 float dt = max(pc.d[3], 0.0);
82 vec3 origin = vec3(pc.d[10], pc.d[11], pc.d[12]);
83 uint idx = z * resX + x;
84 vec2 world = origin.xz + vec2((float(x) + 0.5) * cell, (float(z) + 0.5) * cell);
85 if (stateSrc.q[idx].x <= pc.d[7]) return;
86 float surfaceY = bed.b[idx] + stateSrc.q[idx].x;
87 vec4 acc = sources.s[idx];
88 int count = int(pc.d[9]);
89
90 for (int i = 0; i < count; ++i) {
91 int base = i * 16;
92 vec3 center = vec3(colliders.c[base + 0], colliders.c[base + 1], colliders.c[base + 2]);
93 float radius = colliders.c[base + 3];
94 vec3 halfExtent = vec3(colliders.c[base + 4], colliders.c[base + 5], colliders.c[base + 6]);
95 int shape = int(colliders.c[base + 7] + 0.5);
96 vec3 velocity = vec3(colliders.c[base + 8], colliders.c[base + 9], colliders.c[base + 10]);
97 float displacementScale = colliders.c[base + 11];
98 float dragScale = colliders.c[base + 12];
99
100 float dist = 1e9;
101 float immersion = 0.0;
102 if (shape == 0) {
103 dist = length(world - center.xz) - radius;
104 immersion = clamp(surfaceY - (center.y - radius), 0.0, 2.0 * radius);
105 } else if (shape == 1) {
106 dist = capsuleDistanceXZ(world, center, halfExtent.y, radius);
107 float top = center.y + halfExtent.y + radius;
108 float bottom = center.y - halfExtent.y - radius;
109 immersion = clamp(surfaceY - bottom, 0.0, top - bottom);
110 } else {
111 vec2 d = abs(world - center.xz) - halfExtent.xz;
112 dist = length(max(d, vec2(0.0))) + min(max(d.x, d.y), 0.0);
113 immersion = clamp(surfaceY - (center.y - halfExtent.y), 0.0, 2.0 * halfExtent.y);
114 }
115
116 if (dist >= cell * 0.75 || immersion <= 1e-4) continue;
117
118 float cover = clamp(1.0 - dist / max(cell * 0.75, 1e-4), 0.0, 1.0);
119 // displacementScale / dragScale are rates; multiply by substep dt so
120 // CFL substepping cannot stack absolute impulses into height spikes.
121 float push = immersion * cover * displacementScale * dt;
122 vec2 outward = world - center.xz;
123 float olen = length(outward);
124 vec2 dir = olen > 1e-5 ? outward / olen : vec2(0.0, 1.0);
125 float radial = push * 1.0;
126 float h = max(stateSrc.q[idx].x, pc.d[7]);
127 acc.x -= push * 0.35;
128 acc.y += (dir.x * radial + velocity.x * dragScale * dt) * h;
129 acc.z += (dir.y * radial + velocity.z * dragScale * dt) * h;
130 // Vertical motion injects a local swell that neighbors redistribute via fluxes.
131 acc.x += max(0.0, -velocity.y) * cover * 0.15 * displacementScale * dt;
132 }
133 // Keep one substep's source within a fraction of the hard depth clamp.
134 float maxSrcH = pc.d[8] * 0.15;
135 acc.x = clamp(acc.x, -maxSrcH, maxSrcH);
136 sources.s[idx] = acc;
137}
138)GLSL";
139
145inline const char* kShallowWaterIntegrate = R"GLSL(
146#version 450
147layout(local_size_x = 8, local_size_y = 8) in;
148layout(set = 0, binding = 0) readonly buffer StateSrc { vec4 q[]; } stateSrc;
149layout(set = 0, binding = 1) writeonly buffer StateDst { vec4 q[]; } stateDst;
150layout(set = 0, binding = 2) readonly buffer Bed { float b[]; } bed;
151layout(set = 0, binding = 3) readonly buffer Sources { vec4 s[]; } sources;
152layout(push_constant) uniform PC { float d[32]; } pc;
153
154vec3 fluxX(vec3 q, float g, float minH) {
155 float h = q.x;
156 if (h <= minH) return vec3(0.0);
157 float u = q.y / h;
158 float v = q.z / h;
159 return vec3(q.y, q.y * u + 0.5 * g * h * h, q.y * v);
160}
161
162vec3 fluxZ(vec3 q, float g, float minH) {
163 float h = q.x;
164 if (h <= minH) return vec3(0.0);
165 float u = q.y / h;
166 float v = q.z / h;
167 return vec3(q.z, q.z * u, q.z * v + 0.5 * g * h * h);
168}
169
170// Same hydrostatic reconstruction and side-specific pressure correction as CPU.
171vec3 bedFlux(vec3 a, vec3 b, float ba, float bb, float g, float minH, bool axisZ, bool receiveA) {
172 float top = max(ba, bb);
173 float ha = max(0.0, a.x + ba - top), hb = max(0.0, b.x + bb - top);
174 vec3 qa = vec3(ha, a.x > minH ? a.yz * ha / a.x : vec2(0.0));
175 vec3 qb = vec3(hb, b.x > minH ? b.yz * hb / b.x : vec2(0.0));
176 int axis = axisZ ? 2 : 1;
177 float va = ha > minH ? abs(qa[axis] / ha) : 0.0;
178 float vb = hb > minH ? abs(qb[axis] / hb) : 0.0;
179 float alpha = max(va + sqrt(g * ha), vb + sqrt(g * hb));
180 vec3 f = 0.5 * ((axisZ ? fluxZ(qa, g, minH) : fluxX(qa, g, minH))
181 + (axisZ ? fluxZ(qb, g, minH) : fluxX(qb, g, minH))) - 0.5 * alpha * (qb - qa);
182 float original = receiveA ? a.x : b.x, reconstructed = receiveA ? ha : hb;
183 f[axis] += 0.5 * g * (original * original - reconstructed * reconstructed);
184 return f;
185}
186
187vec3 sampleQ(int x, int z, int resX, int resZ) {
188 x = clamp(x, 0, resX - 1);
189 z = clamp(z, 0, resZ - 1);
190 vec4 q = stateSrc.q[z * resX + x];
191 vec4 src = sources.s[z * resX + x];
192 return vec3(max(q.x + src.x, 0.0), q.y + src.y, q.z + src.z);
193}
194
195float sampleBed(int x, int z, int resX, int resZ) {
196 x = clamp(x, 0, resX - 1);
197 z = clamp(z, 0, resZ - 1);
198 return bed.b[z * resX + x];
199}
200
201void main() {
202 int x = int(gl_GlobalInvocationID.x);
203 int z = int(gl_GlobalInvocationID.y);
204 int resX = int(pc.d[0]);
205 int resZ = int(pc.d[1]);
206 if (x >= resX || z >= resZ) return;
207
208 float cell = pc.d[2];
209 float dt = pc.d[3];
210 float g = pc.d[4];
211 float viscosity = pc.d[5];
212 float friction = pc.d[6];
213 float minH = pc.d[7];
214 float maxH = pc.d[8];
215
216 vec3 q = sampleQ(x, z, resX, resZ);
217 vec3 qL = sampleQ(x - 1, z, resX, resZ);
218 vec3 qR = sampleQ(x + 1, z, resX, resZ);
219 vec3 qD = sampleQ(x, z - 1, resX, resZ);
220 vec3 qU = sampleQ(x, z + 1, resX, resZ);
221
222 float centerBed = sampleBed(x, z, resX, resZ);
223 vec3 fR = bedFlux(q, qR, centerBed, sampleBed(x + 1, z, resX, resZ), g, minH, false, true);
224 vec3 fL = bedFlux(qL, q, sampleBed(x - 1, z, resX, resZ), centerBed, g, minH, false, false);
225 vec3 fU = bedFlux(q, qU, centerBed, sampleBed(x, z + 1, resX, resZ), g, minH, true, true);
226 vec3 fD = bedFlux(qD, q, sampleBed(x, z - 1, resX, resZ), centerBed, g, minH, true, false);
227 vec3 qn = q - (dt / cell) * (fR - fL + fU - fD);
228 qn.y += viscosity * dt * (qL.y + qR.y + qD.y + qU.y - 4.0 * q.y);
229 qn.z += viscosity * dt * (qL.z + qR.z + qD.z + qU.z - 4.0 * q.z);
230 qn.y *= max(0.0, 1.0 - friction * dt);
231 qn.z *= max(0.0, 1.0 - friction * dt);
232
233 qn.x = clamp(qn.x, 0.0, maxH);
234 if (qn.x <= minH) {
235 qn.y = 0.0;
236 qn.z = 0.0;
237 } else {
238 float speed = length(vec2(qn.y, qn.z)) / max(qn.x, minH);
239 if (speed > 8.0) {
240 float s = 8.0 / speed;
241 qn.y *= s;
242 qn.z *= s;
243 }
244 }
245 // Reflecting walls: kill normal momentum on domain edges.
246 if (x == 0 || x == resX - 1) qn.y = 0.0;
247 if (z == 0 || z == resZ - 1) qn.z = 0.0;
248
249 stateDst.q[z * resX + x] = vec4(qn, 0.0);
250}
251)GLSL";
252
253} // namespace eve::fluids
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12
const char * kShallowWaterClearSources
Zero the per-cell impulse source buffer. @lifetime The returned pointer remains valid for the process...
const char * kShallowWaterIntegrate
Apply source impulses then advance one Lax-Friedrichs shallow-water step. @lifetime The returned poin...
const char * kShallowWaterRasterizeColliders
Rasterize sphere/capsule/box colliders into height/momentum sources. @lifetime The returned pointer r...