Skip to content

Commit eeffdf4

Browse files
dakerfloryst
authored andcommitted
fix(WebGPU): ensure buffer size is aligned to 4 bytes
fixes #3175
1 parent d087b23 commit eeffdf4

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

Sources/Rendering/WebGPU/Buffer/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ function vtkWebGPUBuffer(publicAPI, model) {
4848
};
4949

5050
publicAPI.createAndWrite = (data, usage) => {
51+
const paddedSize = Math.ceil(data.byteLength / 4) * 4;
5152
model.handle = model.device.getHandle().createBuffer({
52-
size: data.byteLength,
53+
size: paddedSize,
5354
usage,
5455
mappedAtCreation: true,
5556
label: model.label,
5657
});
57-
model.sizeInBytes = data.byteLength;
58+
model.sizeInBytes = paddedSize;
5859
model.usage = usage;
5960
new Uint8Array(model.handle.getMappedRange()).set(
6061
new Uint8Array(data.buffer)

Sources/Rendering/WebGPU/README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ instead calling renderWindow.newAPISpecificView('WebGPU') should be all that
2424
is needed.
2525

2626
# ToDo
27+
2728
- add device.lost handler
2829
- create background class to encapsulate, background clear,
2930
gradient background, texture background, skybox etc
@@ -66,7 +67,7 @@ Here are some quick notes on the WebGPU classes and how they work together. Clas
6667

6768
- Device - one instance, represents a GPU, holds objects that can be shared including the buffer manager, texture manager, shader cache, and pipelines
6869

69-
- BufferManager - one instance, manages and caches buffers (chunks of memeory), owned by the Device
70+
- BufferManager - one instance, manages and caches buffers (chunks of memory), owned by the Device
7071

7172
- Buffer - many instances, represents a chunk of memory, often a vtkDataArray, managed by the buffer manager so that multiple mappers can share a common buffer. Textures can also be buffer backed. Owned by the buffermanager.
7273

@@ -115,6 +116,7 @@ encoders in the WebGPU spec.
115116

116117
There is a notion of bindable things in this implementation. BingGroups keep an array of
117118
bindable things that it uses/manages. Right now these unclude UBOs, SSBOs, and TextureViews and Smaplers. A bindable thing must answer to the following interface
119+
118120
```
119121
set/getName
120122
getBindGroupLayoutEntry()
@@ -170,6 +172,12 @@ The simple mapper is a viewnode subclass so it can handle render passes. The Ful
170172
## IndexBuffers
171173

172174
The WebGPU backend supports the standard IndexBuffer and VertexBuffer combination to render primatives. Due to vtk's use of celldata which doesn;t always map well to graphics primitives, the IndexBuffer class has methods to create a index buffer where each primitive has a unique provoking point. That way cell data can be rendered as point data using a flat interpolation. The IndexBuffer class holds array to map between the flat indexbuffer and original vtk data.
175+
176+
> :warning: WebGPU spec requires buffer sizes to be aligned to 4 bytes. If your
177+
data size is not a multiple of 4, you need to pad the buffer size to the next
178+
multiple of 4. For Float32Array data, this is automatically satisfied since each
179+
float is 4 bytes.
180+
173181
## Private API
174182

175183
Note that none of the classes in the WebGPU directory are meant to be accessed directly by application code. These classes implement one view of the data (WebGPU as opposed to WebGL). Typical applicaiton code will interface with the RenderWindowViewNode superclass (in the SceneGraph) directory as the main entry point for a view such as WebGL or WebGPU. As such, changes to the API of the WebGPU classes are considered private changes, internal to the implementation of this view.
@@ -225,7 +233,7 @@ You can offset geometry by a factor cF ranging from 0.0 to 1.0 using the followi
225233

226234
The physically based rendering implementation is relatively basic in its current state. It uses a metallic roughness workflow,
227235
supporting diffuse, roughness, metallic, normal, emission (ambient occlusion is yet to be supported since there is no ambient
228-
lighting). The specular component is computed with the Cook-Torrance BRDF which utilizes the Trowbridge-Reitz GGX normal distribution,
236+
lighting). The specular component is computed with the Cook-Torrance BRDF which utilizes the Trowbridge-Reitz GGX normal distribution,
229237
Shlick fresnel approximation, and Smith's method with Schlick GGX. The diffuse is computed using Yasuhiro Fujii's improvement on the
230238
Oren–Nayar reflectance model. As of right now, there are a few limitations to take note of:
231239

@@ -235,4 +243,4 @@ Oren–Nayar reflectance model. As of right now, there are a few limitations to
235243

236244
- Normal maps are applied with an approximation of tangent values, causing them to be off at high strengths
237245

238-
- For certain meshes (depending on the normals and topology), dark zones can appear around the edges
246+
- For certain meshes (depending on the normals and topology), dark zones can appear around the edges

0 commit comments

Comments
 (0)