juj / wasm_webgpu

System headers for interfacing WebGPU from C programs compiled via Emscripten to WebAssembly
BSD 3-Clause "New" or "Revised" License
331 stars 26 forks source link

Add support for data offset on wgpu_queue_write_buffer #19

Closed alienself closed 2 years ago

alienself commented 2 years ago

Hi,

Looks like the write buffer call is missing an argument. The JS API has an extra argument for data offset:

writeBuffer(buffer: GPUBuffer, bufferOffset: GPUSize64, data: BufferSource, dataOffset: GPUSize64, size: GPUSize64): void

this call is missing from wgpu_queue_write_buffer:

void wgpu_queue_write_buffer(WGpuQueue queue, WGpuBuffer buffer, double_int53_t bufferOffset, const void *data __attribute__((nonnull)), double_int53_t size);

juj commented 2 years ago

The reason that dataOffset is missing is that it is redundant in Wasm, as it can be solved by pointer arithmetic in C side. For example, if you have an array arr in C side, and wanted to upload byte range [100, 200[ of that array (i.e. dataOffset==100), then you can call

uint8_t arr[200] = { ... };
wgpu_queue_write_buffer(queue, buffer, bufferOffset, arr + 100, 100);
alienself commented 2 years ago

Oh I see! Totally makes sense thanks for this example.