Skip to content

Examples - Update WebGPU Compute Water #30440

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
77 changes: 60 additions & 17 deletions examples/webgpu_compute_water.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,37 @@
const heightStorage = instancedArray( heightArray ).label( 'Height' );
const prevHeightStorage = instancedArray( prevHeightArray ).label( 'PrevHeight' );

// Get Indices of Neighbor Values of an Index in the Simulation Grid
const getNeighborIndicesTSL = ( index ) => {
const NeighborIndicesStructure = {
northIndex: 'uint',
eastIndex: 'uint',
southIndex: 'uint',
westIndex: 'uint',
};
const NeighborValuesStructure = {
north: 'float',
east: 'float',
south: 'float',
west: 'float'
};

const NeighborIndicesStruct = struct( NeighborIndicesStructure, 'NeighborIndicesStructType' );
const NeighborValuesStruct = struct( NeighborValuesStructure, 'NeighborValuesStructType' );

const destructureStruct = ( structure, structureNode ) => {

const retObj = {};
for ( const property in structure ) {

retObj[ property ] = structureNode.get( property );

}

return retObj;

};


const GetNeighborIndices = Fn( ( [ index ] ) => {

const width = uint( WIDTH );

Expand All @@ -153,34 +182,48 @@
const bottomY = max( 0, y.sub( 1 ) );
const topY = min( y.add( 1 ), width.sub( 1 ) );

const neighborIndices = NeighborIndicesStruct();

const westIndex = y.mul( width ).add( leftX );
const eastIndex = y.mul( width ).add( rightX );

const southIndex = bottomY.mul( width ).add( x );
const northIndex = topY.mul( width ).add( x );

return { northIndex, southIndex, eastIndex, westIndex };
neighborIndices.get( 'northIndex' ).assign( northIndex );
neighborIndices.get( 'southIndex' ).assign( southIndex );
neighborIndices.get( 'eastIndex' ).assign( eastIndex );
neighborIndices.get( 'westIndex' ).assign( westIndex );

};
return neighborIndices;

// Get simulation index neighbor values
const getNeighborValuesTSL = ( index, store ) => {
} ).setLayout( {
name: 'GetNeighborIndices',
type: 'NeighborIndicesStructType',
inputs: [
{ name: 'index', type: 'uint' }
]
} );

const GetNeighborValues = ( index, store ) => {

const { northIndex, southIndex, eastIndex, westIndex } = getNeighborIndicesTSL( index );
const { northIndex, southIndex, eastIndex, westIndex } = destructureStruct( NeighborIndicesStructure, GetNeighborIndices( index ).toVar() );

const north = store.element( northIndex );
const south = store.element( southIndex );
const east = store.element( eastIndex );
const west = store.element( westIndex );
const neighborValues = NeighborValuesStruct();

return { north, south, east, west };
neighborValues.get( 'north' ).assign( store.element( northIndex ) );
neighborValues.get( 'south' ).assign( store.element( southIndex ) );
neighborValues.get( 'east' ).assign( store.element( eastIndex ) );
neighborValues.get( 'west' ).assign( store.element( westIndex ) );

return neighborValues;

};

// Get new normals of simulation area.
const getNormalsFromHeightTSL = ( index, store ) => {

const { north, south, east, west } = getNeighborValuesTSL( index, store );
const { north, south, east, west } = destructureStruct( NeighborValuesStructure, GetNeighborValues( index, store ) );

const normalX = ( west.sub( east ) ).mul( WIDTH / BOUNDS );
const normalY = ( south.sub( north ) ).mul( WIDTH / BOUNDS );
Expand All @@ -196,7 +239,7 @@
const height = heightStorage.element( instanceIndex ).toVar();
const prevHeight = prevHeightStorage.element( instanceIndex ).toVar();

const { north, south, east, west } = getNeighborValuesTSL( instanceIndex, heightStorage );
const { north, south, east, west } = destructureStruct( NeighborValuesStructure, GetNeighborValues( instanceIndex, heightStorage ) );

const neighborHeight = north.add( south ).add( east ).add( west );
neighborHeight.mulAssign( 0.5 );
Expand Down Expand Up @@ -226,10 +269,10 @@
const prevHeight = prevHeightStorage.element( instanceIndex ).toVar();

// Get neighboring height values.
const { north: northH, south: southH, east: eastH, west: westH } = getNeighborValuesTSL( instanceIndex, heightStorage );
const { north: northH, south: southH, east: eastH, west: westH } = destructureStruct( NeighborValuesStructure, GetNeighborValues( instanceIndex, heightStorage ) );

// Get neighboring prev height values.
const { north: northP, south: southP, east: eastP, west: westP } = getNeighborValuesTSL( instanceIndex, prevHeightStorage );
const { north: northP, south: southP, east: eastP, west: westP } = destructureStruct( NeighborValuesStructure, GetNeighborValues( instanceIndex, prevHeightStorage ) );

height.addAssign( northH.add( southH ).add( eastH ).add( westH ) );
prevHeight.addAssign( northP.add( southP ).add( eastP ).add( westP ) );
Expand Down Expand Up @@ -299,7 +342,7 @@
const SphereStruct = struct( {
position: 'vec3',
velocity: 'vec2'
} );
}, 'SphereStructType' );

// Sphere Instance Storage
const sphereVelocityStorage = instancedArray( sphereArray, SphereStruct ).label( 'SphereData' );
Expand Down