StorageBufferNode.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. if ( value.isStorageBufferAttribute !== true && value.isStorageInstancedBufferAttribute !== true ) {
  15. // TOOD: Improve it, possibly adding a new property to the BufferAttribute to identify it as a storage buffer read-only attribute in Renderer
  16. if ( value.isInstancedBufferAttribute ) value.isStorageInstancedBufferAttribute = true;
  17. else value.isStorageBufferAttribute = true;
  18. }
  19. }
  20. getInputType( /*builder*/ ) {
  21. return 'storageBuffer';
  22. }
  23. element( indexNode ) {
  24. return storageElement( this, indexNode );
  25. }
  26. setBufferObject( value ) {
  27. this.bufferObject = value;
  28. return this;
  29. }
  30. generate( builder ) {
  31. if ( builder.isAvailable( 'storageBuffer' ) ) return super.generate( builder );
  32. const nodeType = this.getNodeType( builder );
  33. if ( this._attribute === null ) {
  34. this._attribute = bufferAttribute( this.value );
  35. this._varying = varying( this._attribute );
  36. }
  37. const output = this._varying.build( builder, nodeType );
  38. builder.registerTransform( output, this._attribute );
  39. return output;
  40. }
  41. }
  42. export default StorageBufferNode;
  43. export const storage = ( value, type, count ) => nodeObject( new StorageBufferNode( value, type, count ) );
  44. export const storageObject = ( value, type, count ) => nodeObject( new StorageBufferNode( value, type, count ).setBufferObject( true ) );
  45. addNodeClass( 'StorageBufferNode', StorageBufferNode );