StorageBufferNode.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. import { GPUBufferBindingType } from '../../renderers/webgpu/utils/WebGPUConstants.js';
  8. class StorageBufferNode extends BufferNode {
  9. constructor( value, bufferType, bufferCount = 0 ) {
  10. super( value, bufferType, bufferCount );
  11. this.isStorageBufferNode = true;
  12. this.access = GPUBufferBindingType.Storage;
  13. this.bufferObject = false;
  14. this.bufferCount = bufferCount;
  15. this._attribute = null;
  16. this._varying = null;
  17. this.global = true;
  18. if ( value.isStorageBufferAttribute !== true && value.isStorageInstancedBufferAttribute !== true ) {
  19. // TOOD: Improve it, possibly adding a new property to the BufferAttribute to identify it as a storage buffer read-only attribute in Renderer
  20. if ( value.isInstancedBufferAttribute ) value.isStorageInstancedBufferAttribute = true;
  21. else value.isStorageBufferAttribute = true;
  22. }
  23. }
  24. getHash( builder ) {
  25. if ( this.bufferCount === 0 ) {
  26. let bufferData = builder.globalCache.getData( this.value );
  27. if ( bufferData === undefined ) {
  28. bufferData = {
  29. node: this
  30. };
  31. builder.globalCache.setData( this.value, bufferData );
  32. }
  33. return bufferData.node.uuid;
  34. }
  35. return this.uuid;
  36. }
  37. getInputType( /*builder*/ ) {
  38. return 'storageBuffer';
  39. }
  40. element( indexNode ) {
  41. return storageElement( this, indexNode );
  42. }
  43. setBufferObject( value ) {
  44. this.bufferObject = value;
  45. return this;
  46. }
  47. setAccess( value ) {
  48. this.access = value;
  49. return this;
  50. }
  51. toReadOnly() {
  52. return this.setAccess( GPUBufferBindingType.ReadOnlyStorage );
  53. }
  54. generate( builder ) {
  55. if ( builder.isAvailable( 'storageBuffer' ) ) {
  56. return super.generate( builder );
  57. }
  58. const nodeType = this.getNodeType( builder );
  59. if ( this._attribute === null ) {
  60. this._attribute = bufferAttribute( this.value );
  61. this._varying = varying( this._attribute );
  62. }
  63. const output = this._varying.build( builder, nodeType );
  64. builder.registerTransform( output, this._attribute );
  65. return output;
  66. }
  67. }
  68. export default StorageBufferNode;
  69. // Read-Write Storage
  70. export const storage = ( value, type, count ) => nodeObject( new StorageBufferNode( value, type, count ) );
  71. export const storageObject = ( value, type, count ) => nodeObject( new StorageBufferNode( value, type, count ).setBufferObject( true ) );
  72. addNodeClass( 'StorageBufferNode', StorageBufferNode );