WebGPUBindings.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. class WebGPUBindings {
  2. constructor( device, info, properties, textures, renderPipelines, computePipelines, attributes, nodes ) {
  3. this.device = device;
  4. this.info = info;
  5. this.properties = properties;
  6. this.textures = textures;
  7. this.renderPipelines = renderPipelines;
  8. this.computePipelines = computePipelines;
  9. this.attributes = attributes;
  10. this.nodes = nodes;
  11. this.uniformsData = new WeakMap();
  12. this.updateMap = new WeakMap();
  13. }
  14. get( object ) {
  15. let data = this.uniformsData.get( object );
  16. if ( data === undefined ) {
  17. // each object defines an array of bindings (ubos, textures, samplers etc.)
  18. const nodeBuilder = this.nodes.get( object );
  19. const bindings = nodeBuilder.getBindings();
  20. // setup (static) binding layout and (dynamic) binding group
  21. const renderPipeline = this.renderPipelines.get( object );
  22. const bindLayout = renderPipeline.pipeline.getBindGroupLayout( 0 );
  23. const bindGroup = this._createBindGroup( bindings, bindLayout );
  24. data = {
  25. layout: bindLayout,
  26. group: bindGroup,
  27. bindings: bindings
  28. };
  29. this.uniformsData.set( object, data );
  30. }
  31. return data;
  32. }
  33. getForCompute( param ) {
  34. let data = this.uniformsData.get( param );
  35. if ( data === undefined ) {
  36. // bindings are not yet retrieved via node material
  37. const bindings = param.bindings !== undefined ? param.bindings.slice() : [];
  38. const computePipeline = this.computePipelines.get( param );
  39. const bindLayout = computePipeline.getBindGroupLayout( 0 );
  40. const bindGroup = this._createBindGroup( bindings, bindLayout );
  41. data = {
  42. layout: bindLayout,
  43. group: bindGroup,
  44. bindings: bindings
  45. };
  46. this.uniformsData.set( param, data );
  47. }
  48. return data;
  49. }
  50. update( object, camera ) {
  51. const textures = this.textures;
  52. const data = this.get( object );
  53. const bindings = data.bindings;
  54. const updateMap = this.updateMap;
  55. const frame = this.info.render.frame;
  56. let needsBindGroupRefresh = false;
  57. // iterate over all bindings and check if buffer updates or a new binding group is required
  58. for ( const binding of bindings ) {
  59. const isShared = binding.isShared;
  60. const isUpdated = updateMap.get( binding ) === frame;
  61. if ( isShared && isUpdated ) continue;
  62. if ( binding.isUniformsGroup ) {
  63. const array = binding.array;
  64. const bufferGPU = binding.bufferGPU;
  65. binding.onBeforeUpdate( object, camera );
  66. const needsBufferWrite = binding.update();
  67. if ( needsBufferWrite === true ) {
  68. this.device.queue.writeBuffer(
  69. bufferGPU,
  70. 0,
  71. array,
  72. 0
  73. );
  74. }
  75. } else if ( binding.isStorageBuffer ) {
  76. const attribute = binding.attribute;
  77. this.attributes.update( attribute, false, binding.usage );
  78. } else if ( binding.isSampler ) {
  79. const texture = binding.getTexture();
  80. textures.updateSampler( texture );
  81. const samplerGPU = textures.getSampler( texture );
  82. if ( binding.samplerGPU !== samplerGPU ) {
  83. binding.samplerGPU = samplerGPU;
  84. needsBindGroupRefresh = true;
  85. }
  86. } else if ( binding.isSampledTexture ) {
  87. const texture = binding.getTexture();
  88. const forceUpdate = textures.updateTexture( texture );
  89. const textureGPU = textures.getTextureGPU( texture );
  90. if ( binding.textureGPU !== textureGPU || forceUpdate === true ) {
  91. binding.textureGPU = textureGPU;
  92. needsBindGroupRefresh = true;
  93. }
  94. }
  95. updateMap.set( binding, frame );
  96. }
  97. if ( needsBindGroupRefresh === true ) {
  98. data.group = this._createBindGroup( bindings, data.layout );
  99. }
  100. }
  101. dispose() {
  102. this.uniformsData = new WeakMap();
  103. this.updateMap = new WeakMap();
  104. }
  105. _createBindGroup( bindings, layout ) {
  106. let bindingPoint = 0;
  107. const entries = [];
  108. for ( const binding of bindings ) {
  109. if ( binding.isUniformsGroup ) {
  110. if ( binding.bufferGPU === null ) {
  111. const byteLength = binding.getByteLength();
  112. binding.array = new Float32Array( new ArrayBuffer( byteLength ) );
  113. binding.bufferGPU = this.device.createBuffer( {
  114. size: byteLength,
  115. usage: binding.usage,
  116. } );
  117. }
  118. entries.push( { binding: bindingPoint, resource: { buffer: binding.bufferGPU } } );
  119. } else if ( binding.isStorageBuffer ) {
  120. if ( binding.bufferGPU === null ) {
  121. const attribute = binding.attribute;
  122. this.attributes.update( attribute, false, binding.usage );
  123. binding.bufferGPU = this.attributes.get( attribute ).buffer;
  124. }
  125. entries.push( { binding: bindingPoint, resource: { buffer: binding.bufferGPU } } );
  126. } else if ( binding.isSampler ) {
  127. if ( binding.samplerGPU === null ) {
  128. binding.samplerGPU = this.textures.getDefaultSampler();
  129. }
  130. entries.push( { binding: bindingPoint, resource: binding.samplerGPU } );
  131. } else if ( binding.isSampledTexture ) {
  132. if ( binding.textureGPU === null ) {
  133. if ( binding.isSampledCubeTexture ) {
  134. binding.textureGPU = this.textures.getDefaultCubeTexture();
  135. } else {
  136. binding.textureGPU = this.textures.getDefaultTexture();
  137. }
  138. }
  139. entries.push( { binding: bindingPoint, resource: binding.textureGPU.createView( { dimension: binding.dimension } ) } );
  140. }
  141. bindingPoint ++;
  142. }
  143. return this.device.createBindGroup( {
  144. layout: layout,
  145. entries: entries
  146. } );
  147. }
  148. }
  149. export default WebGPUBindings;