Skip to content
Open
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
Binary file modified examples/screenshots/physics_jolt_instancing.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/screenshots/webgl_postprocessing_godrays.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/Three.TSL.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ export const workgroupBarrier = TSL.workgroupBarrier;
export const workgroupId = TSL.workgroupId;
export const workingToColorSpace = TSL.workingToColorSpace;
export const xor = TSL.xor;
export const invocationIndex = TSL.invocationIndex;

/*
// Use this code to generate the export statements dynamically
Expand Down
9 changes: 9 additions & 0 deletions src/nodes/core/IndexNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ export default IndexNode;
*/
export const vertexIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.VERTEX );

/**
* TSL object that represents the global index of a compute shader invocation.
* This is the linearized index across all workgroups, equivalent to the global invocation ID.
*
* @tsl
* @type {IndexNode}
*/
export const invocationIndex = /*@__PURE__*/ nodeImmutable( IndexNode, IndexNode.INVOCATION_GLOBAL );

/**
* TSL object that represents the index of either a mesh instance or an invocation of a compute shader.
*
Expand Down
12 changes: 12 additions & 0 deletions src/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,18 @@ class NodeBuilder {

}

/**
* Returns the global invocation index for compute shaders.
* This is the linearized index of a compute invocation within a grid of workgroups.
*
* @abstract
* @return {string} The invocation index shader string.
*/
getInvocationIndex() {

warn( 'Abstract function.' );

}
/**
* Contextually returns either the vertex stage instance index builtin
* or the linearized index of an compute invocation within a grid of workgroups.
Expand Down
18 changes: 15 additions & 3 deletions src/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,18 @@ ${ flowData.code }
*
* @return {string} The instance index.
*/
getInvocationIndex() {

if ( this.shaderStage === 'compute' ) {

return 'invocationIndex';

}

throw new Error( 'getInvocationIndex() is only valid in compute shaders' );

}

getInstanceIndex() {

if ( this.shaderStage === 'vertex' ) {
Expand All @@ -1214,7 +1226,7 @@ ${ flowData.code }

}

return 'instanceIndex';
throw new Error( 'instanceIndex is only valid in vertex shaders. Use invocationIndex for compute shaders.' );

}

Expand Down Expand Up @@ -2319,7 +2331,7 @@ fn main( ${shaderData.varyings} ) -> ${shaderData.returnType} {
${ shaderData.directives }

// system
var<private> instanceIndex : u32;
var<private> invocationIndex : u32;

// locals
${ shaderData.scopedArrays }
Expand All @@ -2337,7 +2349,7 @@ ${ shaderData.codes }
fn main( ${ shaderData.attributes } ) {

// system
instanceIndex = globalId.x
invocationIndex = globalId.x
+ globalId.y * ( ${ workgroupSizeX } * numWorkgroups.x )
+ globalId.z * ( ${ workgroupSizeX } * numWorkgroups.x ) * ( ${ workgroupSizeY } * numWorkgroups.y );

Expand Down
Loading