Skip to content

Commit 4127bb0

Browse files
WebGPURenderer: Honor layerUpdates for compressed array textures (#33417)
1 parent facafaf commit 4127bb0

File tree

1 file changed

+57
-24
lines changed

1 file changed

+57
-24
lines changed

src/renderers/webgpu/utils/WebGPUTextureUtils.js

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ const _compareToWebGPU = {
3535

3636
const _flipMap = [ 0, 1, 3, 2, 4, 5 ];
3737

38+
function writeTextureLayer( device, textureGPU, mipLevel, layerIndex, mipmap, bytesPerImage, bytesPerRow, rowsPerImage, textureWidth, textureHeight ) {
39+
40+
device.queue.writeTexture(
41+
{
42+
texture: textureGPU,
43+
mipLevel,
44+
origin: { x: 0, y: 0, z: layerIndex }
45+
},
46+
mipmap.data,
47+
{
48+
offset: layerIndex * bytesPerImage,
49+
bytesPerRow,
50+
rowsPerImage
51+
},
52+
{
53+
width: textureWidth,
54+
height: textureHeight,
55+
depthOrArrayLayers: 1
56+
}
57+
);
58+
59+
}
60+
3861
/**
3962
* A WebGPU backend utility module for managing textures.
4063
*
@@ -560,7 +583,17 @@ class WebGPUTextureUtils {
560583

561584
} else if ( texture.isCompressedTexture || texture.isCompressedArrayTexture ) {
562585

563-
this._copyCompressedBufferToTexture( texture.mipmaps, textureData.texture, textureDescriptorGPU );
586+
if ( texture.isCompressedArrayTexture && texture.layerUpdates.size > 0 ) {
587+
588+
this._copyCompressedBufferToTexture( texture.mipmaps, textureData.texture, textureDescriptorGPU, texture.layerUpdates );
589+
590+
texture.clearLayerUpdates();
591+
592+
} else {
593+
594+
this._copyCompressedBufferToTexture( texture.mipmaps, textureData.texture, textureDescriptorGPU );
595+
596+
}
564597

565598
} else if ( texture.isCubeTexture ) {
566599

@@ -947,15 +980,17 @@ class WebGPUTextureUtils {
947980
* @param {Array<Object>} mipmaps - An array with mipmap data.
948981
* @param {GPUTexture} textureGPU - The GPU texture.
949982
* @param {Object} textureDescriptorGPU - The GPU texture descriptor.
983+
* @param {?Set<number>} [layerUpdates=null] - The layer indices to update.
950984
*/
951-
_copyCompressedBufferToTexture( mipmaps, textureGPU, textureDescriptorGPU ) {
985+
_copyCompressedBufferToTexture( mipmaps, textureGPU, textureDescriptorGPU, layerUpdates = null ) {
952986

953987
// @TODO: Consider to use GPUCommandEncoder.copyBufferToTexture()
954988

955989
const device = this.backend.device;
956990

957991
const blockData = this._getBlockData( textureDescriptorGPU.format );
958992
const isArrayTexture = textureDescriptorGPU.size.depthOrArrayLayers > 1;
993+
const activeLayerUpdates = layerUpdates && layerUpdates.size > 0 ? layerUpdates : null;
959994

960995
for ( let i = 0; i < mipmaps.length; i ++ ) {
961996

@@ -966,28 +1001,26 @@ class WebGPUTextureUtils {
9661001
const depth = isArrayTexture ? textureDescriptorGPU.size.depthOrArrayLayers : 1;
9671002

9681003
const bytesPerRow = Math.ceil( width / blockData.width ) * blockData.byteLength;
969-
const bytesPerImage = bytesPerRow * Math.ceil( height / blockData.height );
970-
971-
for ( let j = 0; j < depth; j ++ ) {
972-
973-
device.queue.writeTexture(
974-
{
975-
texture: textureGPU,
976-
mipLevel: i,
977-
origin: { x: 0, y: 0, z: j }
978-
},
979-
mipmap.data,
980-
{
981-
offset: j * bytesPerImage,
982-
bytesPerRow,
983-
rowsPerImage: Math.ceil( height / blockData.height )
984-
},
985-
{
986-
width: Math.ceil( width / blockData.width ) * blockData.width,
987-
height: Math.ceil( height / blockData.height ) * blockData.height,
988-
depthOrArrayLayers: 1
989-
}
990-
);
1004+
const rowsPerImage = Math.ceil( height / blockData.height );
1005+
const bytesPerImage = bytesPerRow * rowsPerImage;
1006+
const textureWidth = Math.ceil( width / blockData.width ) * blockData.width;
1007+
const textureHeight = rowsPerImage * blockData.height;
1008+
1009+
if ( activeLayerUpdates !== null ) {
1010+
1011+
for ( const layerIndex of activeLayerUpdates ) {
1012+
1013+
writeTextureLayer( device, textureGPU, i, layerIndex, mipmap, bytesPerImage, bytesPerRow, rowsPerImage, textureWidth, textureHeight );
1014+
1015+
}
1016+
1017+
} else {
1018+
1019+
for ( let layerIndex = 0; layerIndex < depth; layerIndex ++ ) {
1020+
1021+
writeTextureLayer( device, textureGPU, i, layerIndex, mipmap, bytesPerImage, bytesPerRow, rowsPerImage, textureWidth, textureHeight );
1022+
1023+
}
9911024

9921025
}
9931026

0 commit comments

Comments
 (0)