Skip to content
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
18 changes: 18 additions & 0 deletions src/renderers/common/Backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,24 @@ class Backend {

// utils

/**
* Returns a unique identifier for the given render context that can be used
* to allocate resources like occlusion queries or timestamp queries.
*
* @param {RenderContext|ComputeNode} abstractRenderContext - The render context.
* @return {string} The unique identifier.
*/
getTimestampUID( abstractRenderContext ) {

const contextData = this.get( abstractRenderContext );

let uid = abstractRenderContext.isComputeNode === true ? 'c' : 'r';
uid += ':' + contextData.frameCalls + ':' + abstractRenderContext.id;

return uid;

}

/**
* Returns `true` if the given 3D object is fully occluded by other
* 3D objects in the scene. Backends must implement this method by using
Expand Down
6 changes: 3 additions & 3 deletions src/renderers/common/TimestampQueryPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ class TimestampQueryPool {
}

/**
* Allocate queries for a specific renderContext.
* Allocate queries for a specific uid.
*
* @abstract
* @param {Object} renderContext - The render context to allocate queries for.
* @param {string} uid - A unique identifier for the render context.
* @returns {?number}
*/
allocateQueriesForContext( /* renderContext */ ) {}
allocateQueriesForContext( /* uid */ ) {}

/**
* Resolve all timestamps and return data (or process them).
Expand Down
40 changes: 25 additions & 15 deletions src/renderers/webgl-fallback/WebGLBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { GLFeatureName } from './utils/WebGLConstants.js';
import { WebGLBufferRenderer } from './WebGLBufferRenderer.js';

import { warnOnce } from '../../utils.js';
import { WebGLCoordinateSystem } from '../../constants.js';
import { WebGLCoordinateSystem, TimestampQuery } from '../../constants.js';
import WebGLTimestampQueryPool from './utils/WebGLTimestampQueryPool.js';

/**
Expand Down Expand Up @@ -376,14 +376,13 @@ class WebGLBackend extends Backend {
/**
* Inits a time stamp query for the given render context.
*
* @param {RenderContext} renderContext - The render context.
* @param {string} type - The type of the timestamp query.
* @param {string} uid - A unique identifier for the timestamp query.
*/
initTimestampQuery( renderContext ) {
initTimestampQuery( type, uid ) {

if ( ! this.disjoint || ! this.trackTimestamp ) return;

const type = renderContext.isComputeNode ? 'compute' : 'render';

if ( ! this.timestampQueryPool[ type ] ) {

// TODO: Variable maxQueries?
Expand All @@ -393,11 +392,11 @@ class WebGLBackend extends Backend {

const timestampQueryPool = this.timestampQueryPool[ type ];

const baseOffset = timestampQueryPool.allocateQueriesForContext( renderContext );
const baseOffset = timestampQueryPool.allocateQueriesForContext( uid );

if ( baseOffset !== null ) {

timestampQueryPool.beginQuery( renderContext );
timestampQueryPool.beginQuery( uid );

}

Expand All @@ -408,16 +407,16 @@ class WebGLBackend extends Backend {
/**
* Prepares the timestamp buffer.
*
* @param {RenderContext} renderContext - The render context.
* @param {string} type - The type of the timestamp query.
* @param {string} uid - A unique identifier for the timestamp query.
*/
prepareTimestampBuffer( renderContext ) {
prepareTimestampBuffer( type, uid ) {

if ( ! this.disjoint || ! this.trackTimestamp ) return;

const type = renderContext.isComputeNode ? 'compute' : 'render';
const timestampQueryPool = this.timestampQueryPool[ type ];

timestampQueryPool.endQuery( renderContext );
timestampQueryPool.endQuery( uid );

}

Expand Down Expand Up @@ -446,6 +445,10 @@ class WebGLBackend extends Backend {

//

renderContextData.frameCalls = this.renderer.info.render.frameCalls;

//

if ( renderContext.viewport ) {

this.updateViewport( renderContext );
Expand All @@ -467,7 +470,7 @@ class WebGLBackend extends Backend {

//

this.initTimestampQuery( renderContext );
this.initTimestampQuery( TimestampQuery.RENDER, this.getTimestampUID( renderContext ) );

renderContextData.previousContext = this._currentContext;
this._currentContext = renderContext;
Expand Down Expand Up @@ -560,7 +563,7 @@ class WebGLBackend extends Backend {

}

this.prepareTimestampBuffer( renderContext );
this.prepareTimestampBuffer( TimestampQuery.RENDER, this.getTimestampUID( renderContext ) );

}

Expand Down Expand Up @@ -804,9 +807,16 @@ class WebGLBackend extends Backend {
beginCompute( computeGroup ) {

const { state, gl } = this;
const computeGroupData = this.get( computeGroup );

//

computeGroupData.frameCalls = this.renderer.info.compute.frameCalls;

//

state.bindFramebuffer( gl.FRAMEBUFFER, null );
this.initTimestampQuery( computeGroup );
this.initTimestampQuery( TimestampQuery.COMPUTE, this.getTimestampUID( computeGroup ) );

}

Expand Down Expand Up @@ -912,7 +922,7 @@ class WebGLBackend extends Backend {

gl.disable( gl.RASTERIZER_DISCARD );

this.prepareTimestampBuffer( computeGroup );
this.prepareTimestampBuffer( TimestampQuery.COMPUTE, this.getTimestampUID( computeGroup ) );

if ( this._currentContext ) {

Expand Down
19 changes: 9 additions & 10 deletions src/renderers/webgl-fallback/utils/WebGLTimestampQueryPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ class WebGLTimestampQueryPool extends TimestampQueryPool {
/**
* Allocates a pair of queries for a given render context.
*
* @param {Object} renderContext - The render context to allocate queries for.
* @param {string} uid - A unique identifier for the render context.
* @returns {?number} The base offset for the allocated queries, or null if allocation failed.
*/
allocateQueriesForContext( renderContext ) {
allocateQueriesForContext( uid ) {

if ( ! this.trackTimestamp ) return null;

Expand All @@ -71,7 +71,7 @@ class WebGLTimestampQueryPool extends TimestampQueryPool {

// Initialize query states
this.queryStates.set( baseOffset, 'inactive' );
this.queryOffsets.set( renderContext.id, baseOffset );
this.queryOffsets.set( uid, baseOffset );

return baseOffset;

Expand All @@ -80,17 +80,17 @@ class WebGLTimestampQueryPool extends TimestampQueryPool {
/**
* Begins a timestamp query for the specified render context.
*
* @param {Object} renderContext - The render context to begin timing for.
* @param {string} uid - A unique identifier for the render context.
*/
beginQuery( renderContext ) {
beginQuery( uid ) {

if ( ! this.trackTimestamp || this.isDisposed ) {

return;

}

const baseOffset = this.queryOffsets.get( renderContext.id );
const baseOffset = this.queryOffsets.get( uid );
if ( baseOffset == null ) {

return;
Expand Down Expand Up @@ -135,18 +135,17 @@ class WebGLTimestampQueryPool extends TimestampQueryPool {
/**
* Ends the active timestamp query for the specified render context.
*
* @param {Object} renderContext - The render context to end timing for.
* @param {string} renderContext.id - Unique identifier for the render context.
* @param {string} uid - A unique identifier for the render context.
*/
endQuery( renderContext ) {
endQuery( uid ) {

if ( ! this.trackTimestamp || this.isDisposed ) {

return;

}

const baseOffset = this.queryOffsets.get( renderContext.id );
const baseOffset = this.queryOffsets.get( uid );
if ( baseOffset == null ) {

return;
Expand Down
27 changes: 19 additions & 8 deletions src/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import WebGPUBindingUtils from './utils/WebGPUBindingUtils.js';
import WebGPUPipelineUtils from './utils/WebGPUPipelineUtils.js';
import WebGPUTextureUtils from './utils/WebGPUTextureUtils.js';

import { WebGPUCoordinateSystem } from '../../constants.js';
import { WebGPUCoordinateSystem, TimestampQuery } from '../../constants.js';
import WebGPUTimestampQueryPool from './utils/WebGPUTimestampQueryPool.js';
import { warnOnce } from '../../utils.js';
import { ColorManagement } from '../../math/ColorManagement.js';
Expand Down Expand Up @@ -568,6 +568,12 @@ class WebGPUBackend extends Backend {

const renderContextData = this.get( renderContext );

//

renderContextData.frameCalls = this.renderer.info.render.frameCalls;

//

const device = this.device;
const occlusionQueryCount = renderContext.occlusionQueryCount;

Expand Down Expand Up @@ -608,7 +614,7 @@ class WebGPUBackend extends Backend {

}

this.initTimestampQuery( renderContext, descriptor );
this.initTimestampQuery( TimestampQuery.RENDER, this.getTimestampUID( renderContext ), descriptor );

descriptor.occlusionQuerySet = occlusionQuerySet;

Expand Down Expand Up @@ -1285,11 +1291,17 @@ class WebGPUBackend extends Backend {

const groupGPU = this.get( computeGroup );

//

groupGPU.frameCalls = this.renderer.info.compute.frameCalls;

//

const descriptor = {
label: 'computeGroup_' + computeGroup.id
};

this.initTimestampQuery( computeGroup, descriptor );
this.initTimestampQuery( TimestampQuery.COMPUTE, this.getTimestampUID( computeGroup ), descriptor );

groupGPU.cmdEncoderGPU = this.device.createCommandEncoder( { label: 'computeGroup_' + computeGroup.id } );

Expand Down Expand Up @@ -1916,15 +1928,14 @@ class WebGPUBackend extends Backend {
/**
* Inits a time stamp query for the given render context.
*
* @param {RenderContext} renderContext - The render context.
* @param {string} type - The type of the timestamp query (e.g. 'render', 'compute').
* @param {number} uid - Unique id for the context (e.g. render context id).
* @param {Object} descriptor - The query descriptor.
*/
initTimestampQuery( renderContext, descriptor ) {
initTimestampQuery( type, uid, descriptor ) {

if ( ! this.trackTimestamp ) return;

const type = renderContext.isComputeNode ? 'compute' : 'render';

if ( ! this.timestampQueryPool[ type ] ) {

// TODO: Variable maxQueries?
Expand All @@ -1934,7 +1945,7 @@ class WebGPUBackend extends Backend {

const timestampQueryPool = this.timestampQueryPool[ type ];

const baseOffset = timestampQueryPool.allocateQueriesForContext( renderContext );
const baseOffset = timestampQueryPool.allocateQueriesForContext( uid );

descriptor.timestampWrites = {
querySet: timestampQueryPool.querySet,
Expand Down
6 changes: 3 additions & 3 deletions src/renderers/webgpu/utils/WebGPUTimestampQueryPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ class WebGPUTimestampQueryPool extends TimestampQueryPool {
/**
* Allocates a pair of queries for a given render context.
*
* @param {Object} renderContext - The render context to allocate queries for.
* @param {string} uid - A unique identifier for the render context.
* @returns {?number} The base offset for the allocated queries, or null if allocation failed.
*/
allocateQueriesForContext( renderContext ) {
allocateQueriesForContext( uid ) {

if ( ! this.trackTimestamp || this.isDisposed ) return null;

Expand All @@ -63,7 +63,7 @@ class WebGPUTimestampQueryPool extends TimestampQueryPool {
const baseOffset = this.currentQueryIndex;
this.currentQueryIndex += 2;

this.queryOffsets.set( renderContext.id, baseOffset );
this.queryOffsets.set( uid, baseOffset );
return baseOffset;

}
Expand Down
Loading