Skip to content

Commit 09c38ab

Browse files
authored
WebGPURenderer: copyFramebufferToTexture - support for post-rendering usage (#29729)
* support for post-rendering use * cleanup * Update Textures.js * improve rectangle parameter values and types * cleanup
1 parent 184cc6b commit 09c38ab

File tree

6 files changed

+95
-23
lines changed

6 files changed

+95
-23
lines changed

src/renderers/common/Renderer.js

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,11 +1284,56 @@ class Renderer {
12841284

12851285
copyFramebufferToTexture( framebufferTexture, rectangle = null ) {
12861286

1287-
const renderContext = this._currentRenderContext;
1287+
if ( rectangle !== null ) {
1288+
1289+
if ( rectangle.isVector2 ) {
1290+
1291+
rectangle = _vector4.set( rectangle.x, rectangle.y, framebufferTexture.image.width, framebufferTexture.image.height ).floor();
1292+
1293+
} else if ( rectangle.isVector4 ) {
1294+
1295+
rectangle = _vector4.copy( rectangle ).floor();
1296+
1297+
} else {
1298+
1299+
console.error( 'THREE.Renderer.copyFramebufferToTexture: Invalid rectangle.' );
1300+
1301+
return;
1302+
1303+
}
1304+
1305+
} else {
1306+
1307+
rectangle = _vector4.set( 0, 0, framebufferTexture.image.width, framebufferTexture.image.height );
1308+
1309+
}
1310+
1311+
//
1312+
1313+
let renderContext = this._currentRenderContext;
1314+
let renderTarget;
1315+
1316+
if ( renderContext !== null ) {
1317+
1318+
renderTarget = renderContext.renderTarget;
1319+
1320+
} else {
1321+
1322+
renderTarget = this._renderTarget || this._getFrameBufferTarget();
1323+
1324+
if ( renderTarget !== null ) {
12881325

1289-
this._textures.updateTexture( framebufferTexture );
1326+
this._textures.updateRenderTarget( renderTarget );
12901327

1291-
rectangle = rectangle === null ? _vector4.set( 0, 0, framebufferTexture.image.width, framebufferTexture.image.height ) : rectangle;
1328+
renderContext = this._textures.get( renderTarget );
1329+
1330+
}
1331+
1332+
}
1333+
1334+
//
1335+
1336+
this._textures.updateTexture( framebufferTexture, { renderTarget } );
12921337

12931338
this.backend.copyFramebufferToTexture( framebufferTexture, renderContext, rectangle );
12941339

@@ -1303,7 +1348,6 @@ class Renderer {
13031348

13041349
}
13051350

1306-
13071351
readRenderTargetPixelsAsync( renderTarget, x, y, width, height, index = 0, faceIndex = 0 ) {
13081352

13091353
return this.backend.copyTextureToBuffer( renderTarget.textures[ index ], x, y, width, height, faceIndex );

src/renderers/common/Textures.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ class Textures extends DataMap {
160160

161161
if ( texture.isFramebufferTexture ) {
162162

163-
const renderer = this.renderer;
164-
const renderTarget = renderer.getRenderTarget();
163+
const renderTarget = this.renderer.getRenderTarget();
165164

166165
if ( renderTarget ) {
167166

src/renderers/webgl-fallback/utils/WebGLTextureUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,9 +606,9 @@ class WebGLTextureUtils {
606606

607607
const { textureGPU: dstTextureGPU, glTextureType, glType, glFormat } = backend.get( dstTexture );
608608

609-
610609
let width, height, minX, minY;
611610
let dstX, dstY;
611+
612612
if ( srcRegion !== null ) {
613613

614614
width = srcRegion.max.x - srcRegion.min.x;

src/renderers/webgpu/WebGPUBackend.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,8 +1469,6 @@ class WebGPUBackend extends Backend {
14691469

14701470
const renderContextData = this.get( renderContext );
14711471

1472-
const { encoder, descriptor } = renderContextData;
1473-
14741472
let sourceGPU = null;
14751473

14761474
if ( renderContext.renderTarget ) {
@@ -1509,7 +1507,19 @@ class WebGPUBackend extends Backend {
15091507

15101508
}
15111509

1512-
renderContextData.currentPass.end();
1510+
let encoder;
1511+
1512+
if ( renderContextData.currentPass ) {
1513+
1514+
renderContextData.currentPass.end();
1515+
1516+
encoder = renderContextData.encoder;
1517+
1518+
} else {
1519+
1520+
encoder = this.device.createCommandEncoder( { label: 'copyFramebufferToTexture_' + texture.id } );
1521+
1522+
}
15131523

15141524
encoder.copyTextureToTexture(
15151525
{
@@ -1527,17 +1537,27 @@ class WebGPUBackend extends Backend {
15271537

15281538
if ( texture.generateMipmaps ) this.textureUtils.generateMipmaps( texture );
15291539

1530-
for ( let i = 0; i < descriptor.colorAttachments.length; i ++ ) {
1540+
if ( renderContextData.currentPass ) {
15311541

1532-
descriptor.colorAttachments[ i ].loadOp = GPULoadOp.Load;
1542+
const { descriptor } = renderContextData;
15331543

1534-
}
1544+
for ( let i = 0; i < descriptor.colorAttachments.length; i ++ ) {
15351545

1536-
if ( renderContext.depth ) descriptor.depthStencilAttachment.depthLoadOp = GPULoadOp.Load;
1537-
if ( renderContext.stencil ) descriptor.depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;
1546+
descriptor.colorAttachments[ i ].loadOp = GPULoadOp.Load;
15381547

1539-
renderContextData.currentPass = encoder.beginRenderPass( descriptor );
1540-
renderContextData.currentSets = { attributes: {}, bindingGroups: [], pipeline: null, index: null };
1548+
}
1549+
1550+
if ( renderContext.depth ) descriptor.depthStencilAttachment.depthLoadOp = GPULoadOp.Load;
1551+
if ( renderContext.stencil ) descriptor.depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;
1552+
1553+
renderContextData.currentPass = encoder.beginRenderPass( descriptor );
1554+
renderContextData.currentSets = { attributes: {}, bindingGroups: [], pipeline: null, index: null };
1555+
1556+
} else {
1557+
1558+
this.device.queue.submit( [ encoder.finish() ] );
1559+
1560+
}
15411561

15421562
}
15431563

src/renderers/webgpu/utils/WebGPUTextureUtils.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ class WebGPUTextureUtils {
127127

128128
const { width, height, depth, levels } = options;
129129

130+
if ( texture.isFramebufferTexture ) {
131+
132+
if ( options.renderTarget ) {
133+
134+
options.format = this.backend.utils.getCurrentColorFormat( options.renderTarget );
135+
136+
} else {
137+
138+
options.format = this.backend.utils.getPreferredCanvasFormat();
139+
140+
}
141+
142+
}
143+
130144
const dimension = this._getDimension( texture );
131145
const format = texture.internalFormat || options.format || getFormat( texture, backend.device );
132146

@@ -858,11 +872,7 @@ export function getFormat( texture, device = null ) {
858872

859873
let formatGPU;
860874

861-
if ( texture.isFramebufferTexture === true && texture.type === UnsignedByteType ) {
862-
863-
formatGPU = GPUTextureFormat.BGRA8Unorm;
864-
865-
} else if ( texture.isCompressedTexture === true || texture.isCompressedArrayTexture === true ) {
875+
if ( texture.isCompressedTexture === true || texture.isCompressedArrayTexture === true ) {
866876

867877
switch ( format ) {
868878

src/renderers/webgpu/utils/WebGPUUtils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class WebGPUUtils {
4444

4545
format = this.getTextureFormatGPU( renderContext.textures[ 0 ] );
4646

47-
4847
} else {
4948

5049
format = this.getPreferredCanvasFormat(); // default context format

0 commit comments

Comments
 (0)