Skip to content

Commit 4144826

Browse files
WebGPURenderer: Fix storage buffer binding update and 4 bytes alignment (#30529)
* WebGPURenderer: Fix storage buffer binding update and 4 bytes alignment * remove unused import
1 parent d264b7c commit 4144826

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/renderers/common/Bindings.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ class Bindings extends DataMap {
224224

225225
}
226226

227+
if ( binding.isStorageBuffer ) {
228+
229+
const attribute = binding.attribute;
230+
const attributeType = attribute.isIndirectStorageBufferAttribute ? AttributeType.INDIRECT : AttributeType.STORAGE;
231+
232+
this.attributes.update( attribute, attributeType );
233+
234+
235+
}
236+
227237
if ( binding.isUniformBuffer ) {
228238

229239
const updated = binding.update();

src/renderers/webgpu/utils/WebGPUAttributeUtils.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class WebGPUAttributeUtils {
109109
bufferAttribute.itemSize = 4;
110110
bufferAttribute.array = array;
111111

112+
bufferData._force3to4BytesAlignment = true;
113+
112114
}
113115

114116
const size = array.byteLength + ( ( 4 - ( array.byteLength % 4 ) ) % 4 ); // ensure 4 byte alignment, see #20441
@@ -142,9 +144,27 @@ class WebGPUAttributeUtils {
142144
const backend = this.backend;
143145
const device = backend.device;
144146

147+
const bufferData = backend.get( bufferAttribute );
145148
const buffer = backend.get( bufferAttribute ).buffer;
146149

147-
const array = bufferAttribute.array;
150+
let array = bufferAttribute.array;
151+
152+
// if storage buffer ensure 4 byte alignment
153+
if ( bufferData._force3to4BytesAlignment === true ) {
154+
155+
array = new array.constructor( bufferAttribute.count * 4 );
156+
157+
for ( let i = 0; i < bufferAttribute.count; i ++ ) {
158+
159+
array.set( bufferAttribute.array.subarray( i * 3, i * 3 + 3 ), i * 4 );
160+
161+
}
162+
163+
bufferAttribute.array = array;
164+
165+
}
166+
167+
148168
const isTypedArray = this._isTypedArray( array );
149169
const updateRanges = bufferAttribute.updateRanges;
150170

0 commit comments

Comments
 (0)