StorageBufferNode.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import BufferNode from './BufferNode.js';
  2. import { bufferAttribute } from './BufferAttributeNode.js';
  3. import { addNodeClass } from '../core/Node.js';
  4. import { nodeObject } from '../shadernode/ShaderNode.js';
  5. import { varying } from '../core/VaryingNode.js';
  6. import { storageElement } from '../utils/StorageArrayElementNode.js';
  7. class StorageBufferNode extends BufferNode {
  8. constructor( value, bufferType, bufferCount = 0 ) {
  9. super( value, bufferType, bufferCount );
  10. this.isStorageBufferNode = true;
  11. this.bufferObject = false;
  12. this._attribute = null;
  13. this._varying = null;
  14. }
  15. getInputType( /*builder*/ ) {
  16. return 'storageBuffer';
  17. }
  18. element( indexNode ) {
  19. return storageElement( this, indexNode );
  20. }
  21. setBufferObject( value ) {
  22. this.bufferObject = value;
  23. return this;
  24. }
  25. generate( builder ) {
  26. if ( builder.isAvailable( 'storageBuffer' ) ) return super.generate( builder );
  27. const nodeType = this.getNodeType( builder );
  28. if ( this._attribute === null ) {
  29. this._attribute = bufferAttribute( this.value );
  30. this._varying = varying( this._attribute );
  31. }
  32. const output = this._varying.build( builder, nodeType );
  33. builder.registerTransform( output, this._attribute );
  34. return output;
  35. }
  36. }
  37. export default StorageBufferNode;
  38. export const storage = ( value, type, count ) => nodeObject( new StorageBufferNode( value, type, count ) );
  39. export const storageObject = ( value, type, count ) => nodeObject( new StorageBufferNode( value, type, count ).setBufferObject( true ) );
  40. addNodeClass( 'StorageBufferNode', StorageBufferNode );