Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RenderContexts: Introduce getForClear(). #30256

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions src/renderers/common/RenderContexts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ChainMap from './ChainMap.js';
import RenderContext from './RenderContext.js';
import { Scene } from '../../scenes/Scene.js';
import { Camera } from '../../cameras/Camera.js';

const _chainKeys = [];
const _defaultScene = /*@__PURE__*/ new Scene();
const _defaultCamera = /*@__PURE__*/ new Camera();

/**
* This module manages the render contexts of the renderer.
Expand All @@ -28,22 +32,15 @@ class RenderContexts {
/**
* Returns a render context for the given scene, camera and render target.
*
* @param {Scene?} [scene=null] - The scene. The parameter can become `null` e.g. when the renderer clears a render target.
* @param {Camera?} [camera=null] - The camera that is used to render the scene. The parameter can become `null` e.g. when the renderer clears a render target.
* @param {Scene} scene - The scene.
* @param {Camera} camera - The camera that is used to render the scene.
* @param {RenderTarget?} [renderTarget=null] - The active render target.
* @return {RenderContext} The render context.
*/
get( scene = null, camera = null, renderTarget = null ) {

if ( scene !== null ) _chainKeys.push( scene );
if ( camera !== null ) _chainKeys.push( camera );

if ( _chainKeys.length === 0 ) {

_chainKeys.push( { id: 'default' } );
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By not creating objects inline, we also improve performance.


}
get( scene, camera, renderTarget = null ) {

_chainKeys[ 0 ] = scene;
_chainKeys[ 1 ] = camera;

let attachmentState;

Expand All @@ -60,7 +57,7 @@ class RenderContexts {

}

const chainMap = this.getChainMap( attachmentState );
const chainMap = this._getChainMap( attachmentState );

let renderState = chainMap.get( _chainKeys );

Expand All @@ -80,13 +77,26 @@ class RenderContexts {

}

/**
* Returns a render context intended for clear operations.
*
* @param {RenderTarget?} [renderTarget=null] - The active render target.
* @return {RenderContext} The render context.
*/
getForClear( renderTarget = null ) {

return this.get( _defaultScene, _defaultCamera, renderTarget );

}

/**
* Returns a chain map for the given attachment state.
*
* @private
* @param {String} attachmentState - The attachment state.
* @return {ChainMap} The chain map.
*/
getChainMap( attachmentState ) {
_getChainMap( attachmentState ) {

return this.chainMaps[ attachmentState ] || ( this.chainMaps[ attachmentState ] = new ChainMap() );

Expand Down
2 changes: 1 addition & 1 deletion src/renderers/common/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,7 @@ class Renderer {

const renderTargetData = this._textures.get( renderTarget );

renderContext = this._renderContexts.get( null, null, renderTarget );
renderContext = this._renderContexts.getForClear( renderTarget );
renderContext.textures = renderTargetData.textures;
renderContext.depthTexture = renderTargetData.depthTexture;
renderContext.width = renderTargetData.width;
Expand Down
Loading