/ proof-of-concept / cymbalsim
Sign in
proof-of-concept / cymbalsim
cymbalsim / src / sim / shaders / common.wgsl
80 lines · 3.0 KBBlameHistoryRaw
1// Shared declarations, prepended to every kernel of the full simulation.
3struct Params {
4 nx: u32,
5 ny: u32,
6 n: u32,
7 histLen: u32,
8 hx: f32,
9 hy: f32,
10 dt: f32,
11 nu: f32,
12 Db: f32, // E / (12 (1 - nu^2)): bending stiffness per h^3 [Pa]
13 Cm: f32, // E / (1 - nu^2): membrane stiffness per h [Pa]
14 Gm: f32, // E / (2 (1 + nu)): membrane shear stiffness per h [Pa]
15 rho: f32, // density [kg/m^3]
16 mass: f32, // total mass [kg]
17 Ix: f32, // sum m y^2
18 Iy: f32, // sum m x^2
19 fs: f32, // audio sample rate [Hz]
20 strikeX: f32, // drumstick contact point [m]
21 strikeY: f32,
22 strikeR2: f32, // squared contact radius [m^2]
23 patchMass: f32, // mass of the contact patch [kg]
24 strikeNode: u32, // plate node nearest the contact point
25 K: f32, // Hertz contact stiffness [N/m^alpha]
26 alpha: f32, // Hertz exponent
27 stickMass: f32, // [kg]
28 patchI0: u32, // index box enclosing the contact patch (inclusive)
29 patchI1: u32,
30 patchJ0: u32,
31 patchJ1: u32,
32 patchCX: f32, // mass-weighted center of the contact patch [m]
33 patchCY: f32,
34 standOn: u32, // 1 when the plate sits on a stand, 0 when it is free
35 liftoff: u32, // 1 when the pads push only while compressed
36 preloadTop: f32, // compression of the top pad at rest [m] (0 without a top pad)
37 preloadBottom: f32, // compression of the bottom pad at rest [m]
38 padDamping: f32, // dashpot rate of the pads per unit of pad stiffness [s]
39 hasTop: u32, // 1 when there is a top pad
40 pad0: u32,
41 pad1: u32,
42 pad2: u32,
43 pad3: u32,
44};
46// Per-step parameters, addressed with a dynamic uniform offset.
47struct StepParams {
48 step: u32,
49 flags: u32, // bit 0: drumstick active
50 audioSlot: i32, // ring-buffer slot to record the velocity into (-1: none)
51 audioOut: i32, // index of this sample within the batch's output
52 sigma0: f32,
53 sigma1: f32,
54 sigmaIn: f32, // additional frequency-independent loss of the in-plane motion
55 parity: u32, // which of the two stick-state slots holds the current state
56 pad0: u32,
57 pad1: u32,
58 pad2: u32,
59 pad3: u32,
60};
62fn nodeX(i: i32) -> f32 { return (f32(i) - 0.5 * f32(P.nx - 1u)) * P.hx; }
63fn nodeY(j: i32) -> f32 { return (f32(j) - 0.5 * f32(P.ny - 1u)) * P.hy; }
65// Static per-node data, written once by the host: x = stress-free shape w0 [m],
66// y = quadrature and lumped-mass weight (the fraction of the node's four cells
67// that belong to the plate; zero outside), z = thickness [m], w = bottom-pad
68// stiffness at the node [N/m] (zero away from the pads; the top pad, when
69// present, has the same rate).
70@group(0) @binding(8) var<storage, read> nd: array<vec4f>;
71fn nodeWeight(i: i32, j: i32) -> f32 {
72 if (i < 0 || i >= i32(P.nx) || j < 0 || j >= i32(P.ny)) { return 0.0; }
73 return nd[u32(j) * P.nx + u32(i)].y;
75// Lumped mass of a node [kg].
76fn nodeMass(i: i32, j: i32) -> f32 {
77 if (i < 0 || i >= i32(P.nx) || j < 0 || j >= i32(P.ny)) { return 0.0; }
78 let k = u32(j) * P.nx + u32(i);
79 return P.rho * nd[k].z * P.hx * P.hy * nd[k].y;
↑↓ move↵openescclose