Skip to content

Commit

Permalink
WebGPURenderer: copyFramebufferToTexture - support for post-renderi…
Browse files Browse the repository at this point in the history
…ng usage (#29729)

* support for post-rendering use

* cleanup

* Update Textures.js

* improve rectangle parameter values and types

* cleanup
  • Loading branch information
sunag authored Oct 30, 2024
1 parent 184cc6b commit 09c38ab
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 23 deletions.
52 changes: 48 additions & 4 deletions src/renderers/common/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1284,11 +1284,56 @@ class Renderer {

copyFramebufferToTexture( framebufferTexture, rectangle = null ) {

const renderContext = this._currentRenderContext;
if ( rectangle !== null ) {

if ( rectangle.isVector2 ) {

rectangle = _vector4.set( rectangle.x, rectangle.y, framebufferTexture.image.width, framebufferTexture.image.height ).floor();

} else if ( rectangle.isVector4 ) {

rectangle = _vector4.copy( rectangle ).floor();

} else {

console.error( 'THREE.Renderer.copyFramebufferToTexture: Invalid rectangle.' );

return;

}

} else {

rectangle = _vector4.set( 0, 0, framebufferTexture.image.width, framebufferTexture.image.height );

}

//

let renderContext = this._currentRenderContext;
let renderTarget;

if ( renderContext !== null ) {

renderTarget = renderContext.renderTarget;

} else {

renderTarget = this._renderTarget || this._getFrameBufferTarget();

if ( renderTarget !== null ) {

this._textures.updateTexture( framebufferTexture );
this._textures.updateRenderTarget( renderTarget );

rectangle = rectangle === null ? _vector4.set( 0, 0, framebufferTexture.image.width, framebufferTexture.image.height ) : rectangle;
renderContext = this._textures.get( renderTarget );

}

}

//

this._textures.updateTexture( framebufferTexture, { renderTarget } );

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

Expand All @@ -1303,7 +1348,6 @@ class Renderer {

}


readRenderTargetPixelsAsync( renderTarget, x, y, width, height, index = 0, faceIndex = 0 ) {

return this.backend.copyTextureToBuffer( renderTarget.textures[ index ], x, y, width, height, faceIndex );
Expand Down
3 changes: 1 addition & 2 deletions src/renderers/common/Textures.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ class Textures extends DataMap {

if ( texture.isFramebufferTexture ) {

const renderer = this.renderer;
const renderTarget = renderer.getRenderTarget();
const renderTarget = this.renderer.getRenderTarget();

if ( renderTarget ) {

Expand Down
2 changes: 1 addition & 1 deletion src/renderers/webgl-fallback/utils/WebGLTextureUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,9 @@ class WebGLTextureUtils {

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


let width, height, minX, minY;
let dstX, dstY;

if ( srcRegion !== null ) {

width = srcRegion.max.x - srcRegion.min.x;
Expand Down
40 changes: 30 additions & 10 deletions src/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1469,8 +1469,6 @@ class WebGPUBackend extends Backend {

const renderContextData = this.get( renderContext );

const { encoder, descriptor } = renderContextData;

let sourceGPU = null;

if ( renderContext.renderTarget ) {
Expand Down Expand Up @@ -1509,7 +1507,19 @@ class WebGPUBackend extends Backend {

}

renderContextData.currentPass.end();
let encoder;

if ( renderContextData.currentPass ) {

renderContextData.currentPass.end();

encoder = renderContextData.encoder;

} else {

encoder = this.device.createCommandEncoder( { label: 'copyFramebufferToTexture_' + texture.id } );

}

encoder.copyTextureToTexture(
{
Expand All @@ -1527,17 +1537,27 @@ class WebGPUBackend extends Backend {

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

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

descriptor.colorAttachments[ i ].loadOp = GPULoadOp.Load;
const { descriptor } = renderContextData;

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

if ( renderContext.depth ) descriptor.depthStencilAttachment.depthLoadOp = GPULoadOp.Load;
if ( renderContext.stencil ) descriptor.depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;
descriptor.colorAttachments[ i ].loadOp = GPULoadOp.Load;

renderContextData.currentPass = encoder.beginRenderPass( descriptor );
renderContextData.currentSets = { attributes: {}, bindingGroups: [], pipeline: null, index: null };
}

if ( renderContext.depth ) descriptor.depthStencilAttachment.depthLoadOp = GPULoadOp.Load;
if ( renderContext.stencil ) descriptor.depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;

renderContextData.currentPass = encoder.beginRenderPass( descriptor );
renderContextData.currentSets = { attributes: {}, bindingGroups: [], pipeline: null, index: null };

} else {

this.device.queue.submit( [ encoder.finish() ] );

}

}

Expand Down
20 changes: 15 additions & 5 deletions src/renderers/webgpu/utils/WebGPUTextureUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ class WebGPUTextureUtils {

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

if ( texture.isFramebufferTexture ) {

if ( options.renderTarget ) {

options.format = this.backend.utils.getCurrentColorFormat( options.renderTarget );

} else {

options.format = this.backend.utils.getPreferredCanvasFormat();

}

}

const dimension = this._getDimension( texture );
const format = texture.internalFormat || options.format || getFormat( texture, backend.device );

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

let formatGPU;

if ( texture.isFramebufferTexture === true && texture.type === UnsignedByteType ) {

formatGPU = GPUTextureFormat.BGRA8Unorm;

} else if ( texture.isCompressedTexture === true || texture.isCompressedArrayTexture === true ) {
if ( texture.isCompressedTexture === true || texture.isCompressedArrayTexture === true ) {

switch ( format ) {

Expand Down
1 change: 0 additions & 1 deletion src/renderers/webgpu/utils/WebGPUUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class WebGPUUtils {

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


} else {

format = this.getPreferredCanvasFormat(); // default context format
Expand Down

0 comments on commit 09c38ab

Please sign in to comment.