webgpu-utils icon indicating copy to clipboard operation
webgpu-utils copied to clipboard

setting a value in a uniform that's an array of structures

Open jowens opened this issue 4 months ago • 3 comments

Respectfully requesting enhancing the makeStructuredView with an example of setting a structure (or a partial structure) that's part of a member of structures. "Views" are complicated, and I don't have my mind wrapped around them yet, but I did this wrong many many ways before doing it by hand, and I bet there's a much cleaner way to do it.

const uniformsCode = /* wgsl */ `
  const MAX_LEVEL = 10;
  struct Level {
    f: u32, e: u32, v: u32, t: u32,
  };
  struct MyUniforms {
    uni1: f32,
    uni2: f32,
    @align(16) levelCount: array<Level, MAX_LEVEL>,
    @align(16) levelBasePtr: array<Level, MAX_LEVEL>,
  };
  @group(0) @binding(0) var<uniform> myUniforms: MyUniforms;`;

I'd then like to set levelCount, which I'm (sadly) doing as:

for (let j = 0; j <= mesh.maxLevel; j++) {
  uni.views.levelCount[j].f[0] = mesh.levelCount[j].f;
  uni.views.levelCount[j].e[0] = mesh.levelCount[j].e;
  uni.views.levelCount[j].v[0] = mesh.levelCount[j].v;
  uni.views.levelCount[j].t[0] = mesh.levelCount[j].t;
}

Surely this can be done with less code.

An example of how to set a structure (or a piece of structure) when that structure is within an array would be helpful.

jowens avatar Sep 26 '24 20:09 jowens