Skip to content

Commit

Permalink
switch .readOnly to .access in StorageBufferNode and NodeStorageBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
cmhhelgeson committed May 21, 2024
1 parent 793d1d4 commit 3571c1b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 32 deletions.
2 changes: 1 addition & 1 deletion examples/jsm/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export { default as ReferenceNode, reference, referenceBuffer } from './accessor
export { default as ReflectVectorNode, reflectVector } from './accessors/ReflectVectorNode.js';
export { default as SkinningNode, skinning } from './accessors/SkinningNode.js';
export { default as SceneNode, backgroundBlurriness, backgroundIntensity } from './accessors/SceneNode.js';
export { default as StorageBufferNode, storage, storageImmutable, storageObject } from './accessors/StorageBufferNode.js';
export { default as StorageBufferNode, storage, storageReadOnly, storageObject } from './accessors/StorageBufferNode.js';
export * from './accessors/TangentNode.js';
export { default as TextureNode, texture, textureLoad, /*textureLevel,*/ sampler } from './accessors/TextureNode.js';
export { default as TextureStoreNode, textureStore } from './accessors/TextureStoreNode.js';
Expand Down
26 changes: 14 additions & 12 deletions examples/jsm/nodes/accessors/StorageBufferNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import { nodeObject } from '../shadernode/ShaderNode.js';
import { varying } from '../core/VaryingNode.js';
import { storageElement } from '../utils/StorageArrayElementNode.js';

export const StorageBufferAccessType = {
Read: 'read',
ReadWrite: 'read_write',
};

class StorageBufferNode extends BufferNode {

constructor( value, bufferType, bufferCount = 0 ) {
Expand All @@ -13,7 +18,7 @@ class StorageBufferNode extends BufferNode {

this.isStorageBufferNode = true;

this.readOnly = false;
this.access = StorageBufferAccessType.ReadWrite;

this.bufferObject = false;

Expand All @@ -33,13 +38,7 @@ class StorageBufferNode extends BufferNode {

getInputType( /*builder*/ ) {

if ( this.readOnly ) {

return 'storageReadOnlyBuffer';

}

return 'storageBuffer';
return this.access === StorageBufferAccessType.ReadWrite ? 'storageBuffer' : 'storageReadOnlyBuffer';

}

Expand All @@ -57,17 +56,17 @@ class StorageBufferNode extends BufferNode {

}

setReadOnly( value ) {
setAccess( value ) {

this.readOnly = value;
this.access = value;

return this;

}

generate( builder ) {

if ( builder.isAvailable( 'storageBuffer' ) && ! this.readOnly || builder.isAvailable( 'storageReadOnlyBuffer' ) && this.readOnly ) {
if ( builder.isAvailable( 'storageBuffer' ) ) {

return super.generate( builder );

Expand Down Expand Up @@ -95,8 +94,11 @@ class StorageBufferNode extends BufferNode {

export default StorageBufferNode;

// Read-Write Storage
export const storage = ( value, type, count ) => nodeObject( new StorageBufferNode( value, type, count ) );
export const storageImmutable = ( value, type, count ) => nodeObject( new StorageBufferNode( value, type, count ).setReadOnly( true ) );
export const storageObject = ( value, type, count ) => nodeObject( new StorageBufferNode( value, type, count ).setBufferObject( true ) );

// Read-Only Storage
export const storageReadOnly = ( value, type, count ) => nodeObject( new StorageBufferNode( value, type, count ).setAccess( StorageBufferAccessType.Read ) );

addNodeClass( 'StorageBufferNode', StorageBufferNode );
9 changes: 5 additions & 4 deletions examples/jsm/renderers/common/nodes/NodeStorageBuffer.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import StorageBuffer from '../StorageBuffer.js';
import { StorageBufferAccessType } from '../../../nodes/accessors/StorageBufferNode.js';

let _id = 0;

class NodeStorageBuffer extends StorageBuffer {

constructor( nodeUniform, readOnly ) {
constructor( nodeUniform ) {

readOnly = readOnly ? readOnly : false;
const namePrefix = readOnly ? 'StorageReadOnlyBuffer_' : 'StorageBuffer_';
const access = nodeUniform ? nodeUniform.access : StorageBufferAccessType.ReadWrite;
const namePrefix = access === StorageBufferAccessType.Read ? 'StorageReadOnlyBuffer_' : 'StorageBuffer_';

super( namePrefix + _id ++, nodeUniform ? nodeUniform.value : null );

this.nodeUniform = nodeUniform;
this.readOnly = readOnly;
this.access = access;

}

Expand Down
11 changes: 2 additions & 9 deletions examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const gpuShaderStageLib = {
const supports = {
instance: true,
storageBuffer: true,
storageReadOnlyBuffer: true,
};

const wgslFnOpLib = {
Expand Down Expand Up @@ -407,8 +406,7 @@ class WGSLNodeBuilder extends NodeBuilder {
} else if ( type === 'buffer' || type === 'storageBuffer' || type === 'storageReadOnlyBuffer' ) {

const bufferClass = type !== 'buffer' ? NodeStorageBuffer : NodeUniformBuffer;
const readOnly = type === 'storageReadOnlyBuffer';
const buffer = new bufferClass( node, readOnly );
const buffer = new bufferClass( node );
buffer.setVisibility( gpuShaderStageLib[ shaderStage ] );

bindings.push( buffer );
Expand Down Expand Up @@ -797,12 +795,7 @@ ${ flowData.code }

const bufferCountSnippet = bufferCount > 0 ? ', ' + bufferCount : '';
const bufferSnippet = `\t${uniform.name} : array< ${bufferType}${bufferCountSnippet} >\n`;
let bufferAccessMode = bufferNode.isStorageBufferNode ? 'storage,read' : 'uniform';
if ( ! bufferNode.readOnly ) {

bufferAccessMode += '_write';

}
let bufferAccessMode = bufferNode.isStorageBufferNode ? `storage, ${bufferNode.access}` : 'uniform';

bufferSnippets.push( this._getWGSLStructBinding( 'NodeBuffer_' + bufferNode.id, bufferSnippet, bufferAccessMode, index ++ ) );

Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/renderers/webgpu/utils/WebGPUBindingUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class WebGPUBindingUtils {

if ( binding.isStorageBuffer ) {

buffer.type = binding.readOnly ? GPUBufferBindingType.ReadOnlyStorage : GPUBufferBindingType.Storage;
buffer.type = binding.access === 'read' ? GPUBufferBindingType.ReadOnlyStorage : GPUBufferBindingType.Storage;

}

Expand Down
4 changes: 2 additions & 2 deletions examples/webgpu_compute_audio.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<script type="module">

import * as THREE from 'three';
import { tslFn, uniform, storage, storageObject, instanceIndex, float, texture, viewportTopLeft, color } from 'three/nodes';
import { tslFn, uniform, storage, storageReadOnly, instanceIndex, float, texture, viewportTopLeft, color } from 'three/nodes';

import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

Expand Down Expand Up @@ -115,7 +115,7 @@

// read-only buffer

const waveNode = storageObject( new StorageInstancedBufferAttribute( waveBuffer, 1 ), 'float', waveBuffer.length );
const waveNode = storageReadOnly( new StorageInstancedBufferAttribute( waveBuffer, 1 ), 'float', waveBuffer.length );


// params
Expand Down
6 changes: 3 additions & 3 deletions examples/webgpu_compute_geometry.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<script type="module">

import * as THREE from 'three';
import { vec3, cos, sin, mat3, storage, storageImmutable, tslFn, instanceIndex, timerLocal } from 'three/nodes';
import { vec3, cos, sin, mat3, storage, storageReadOnly, tslFn, instanceIndex, timerLocal } from 'three/nodes';

import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

Expand Down Expand Up @@ -72,8 +72,8 @@

const computeFn = tslFn( () => {

const positionAttribute = storageImmutable( positionBaseAttribute, 'vec3', positionBaseAttribute.count );
const normalAttribute = storageImmutable( normalBaseAttribute, 'vec3', normalBaseAttribute.count );
const positionAttribute = storageReadOnly( positionBaseAttribute, 'vec3', positionBaseAttribute.count );
const normalAttribute = storageReadOnly( normalBaseAttribute, 'vec3', normalBaseAttribute.count );

const positionStorageAttribute = storage( positionStorageBufferAttribute, 'vec4', positionStorageBufferAttribute.count );
const normalStorageAttribute = storage( normalStorageBufferAttribute, 'vec4', normalStorageBufferAttribute.count );
Expand Down

0 comments on commit 3571c1b

Please sign in to comment.