StorageBufferNode.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.bufferCount = bufferCount;
  13. this._attribute = null;
  14. this._varying = null;
  15. this.global = true;
  16. if ( value.isStorageBufferAttribute !== true && value.isStorageInstancedBufferAttribute !== true ) {
  17. // TOOD: Improve it, possibly adding a new property to the BufferAttribute to identify it as a storage buffer read-only attribute in Renderer
  18. if ( value.isInstancedBufferAttribute ) value.isStorageInstancedBufferAttribute = true;
  19. else value.isStorageBufferAttribute = true;
  20. }
  21. }
  22. getHash( builder ) {
  23. if ( this.bufferCount === 0 ) {
  24. let bufferData = builder.globalCache.getData( this.value );
  25. if ( bufferData === undefined ) {
  26. bufferData = {
  27. node: this
  28. };
  29. builder.globalCache.setData( this.value, bufferData );
  30. }
  31. return bufferData.node.uuid;
  32. }
  33. return this.uuid;
  34. }
  35. getInputType( /*builder*/ ) {
  36. return 'storageBuffer';
  37. }
  38. element( indexNode ) {
  39. return storageElement( this, indexNode );
  40. }
  41. setBufferObject( value ) {
  42. this.bufferObject = value;
  43. return this;
  44. }
  45. generate( builder ) {
  46. if ( builder.isAvailable( 'storageBuffer' ) ) return super.generate( builder );
  47. const nodeType = this.getNodeType( builder );
  48. if ( this._attribute === null ) {
  49. this._attribute = bufferAttribute( this.value );
  50. this._varying = varying( this._attribute );
  51. }
  52. const output = this._varying.build( builder, nodeType );
  53. builder.registerTransform( output, this._attribute );
  54. return output;
  55. }
  56. }
  57. export default StorageBufferNode;
  58. export const storage = ( value, type, count ) => nodeObject( new StorageBufferNode( value, type, count ) );
  59. export const storageObject = ( value, type, count ) => nodeObject( new StorageBufferNode( value, type, count ).setBufferObject( true ) );
  60. addNodeClass( 'StorageBufferNode', StorageBufferNode );