WebGPUBindings.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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( renderObject ) {
  15. let data = this.uniformsData.get( renderObject );
  16. if ( data === undefined ) {
  17. // each object defines an array of bindings (ubos, textures, samplers etc.)
  18. const nodeBuilder = this.nodes.get( renderObject );
  19. const bindings = nodeBuilder.getBindings();
  20. // setup (static) binding layout and (dynamic) binding group
  21. const pipeline = this.renderPipelines.get( renderObject ).pipeline;
  22. const bindLayout = 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( renderObject, data );
  30. }
  31. return data;
  32. }
  33. getForCompute( computeNode ) {
  34. let data = this.uniformsData.get( computeNode );
  35. if ( data === undefined ) {
  36. // each object defines an array of bindings (ubos, textures, samplers etc.)
  37. const nodeBuilder = this.nodes.getForCompute( computeNode );
  38. const bindings = nodeBuilder.getBindings();
  39. // setup (static) binding layout and (dynamic) binding group
  40. const pipeline = this.computePipelines.get( computeNode );
  41. const bindLayout = pipeline.getBindGroupLayout( 0 );
  42. const bindGroup = this._createBindGroup( bindings, bindLayout );
  43. data = {
  44. layout: bindLayout,
  45. group: bindGroup,
  46. bindings: bindings
  47. };
  48. this.uniformsData.set( computeNode, data );
  49. }
  50. return data;
  51. }
  52. remove( object ) {
  53. this.uniformsData.delete( object );
  54. }
  55. update( object ) {
  56. const textures = this.textures;
  57. const data = this.get( object );
  58. const bindings = data.bindings;
  59. const updateMap = this.updateMap;
  60. const frame = this.info.render.frame;
  61. let needsBindGroupRefresh = false;
  62. // iterate over all bindings and check if buffer updates or a new binding group is required
  63. for ( const binding of bindings ) {
  64. const isShared = binding.isShared;
  65. const isUpdated = updateMap.get( binding ) === frame;
  66. if ( isShared && isUpdated ) continue;
  67. if ( binding.isUniformBuffer ) {
  68. const buffer = binding.getBuffer();
  69. const needsBufferWrite = binding.update();
  70. if ( needsBufferWrite === true ) {
  71. const bufferGPU = binding.bufferGPU;
  72. this.device.queue.writeBuffer( bufferGPU, 0, buffer, 0 );
  73. }
  74. } else if ( binding.isStorageBuffer ) {
  75. const attribute = binding.attribute;
  76. this.attributes.update( attribute, false, binding.usage );
  77. } else if ( binding.isSampler ) {
  78. const texture = binding.getTexture();
  79. textures.updateSampler( texture );
  80. const samplerGPU = textures.getSampler( texture );
  81. if ( binding.samplerGPU !== samplerGPU ) {
  82. binding.samplerGPU = samplerGPU;
  83. needsBindGroupRefresh = true;
  84. }
  85. } else if ( binding.isSampledTexture ) {
  86. const texture = binding.getTexture();
  87. const needsTextureRefresh = textures.updateTexture( texture );
  88. const textureGPU = textures.getTextureGPU( texture );
  89. if ( textureGPU !== undefined && binding.textureGPU !== textureGPU || needsTextureRefresh === true ) {
  90. binding.textureGPU = textureGPU;
  91. needsBindGroupRefresh = true;
  92. }
  93. }
  94. updateMap.set( binding, frame );
  95. }
  96. if ( needsBindGroupRefresh === true ) {
  97. data.group = this._createBindGroup( bindings, data.layout );
  98. }
  99. }
  100. dispose() {
  101. this.uniformsData = new WeakMap();
  102. this.updateMap = new WeakMap();
  103. }
  104. _createBindGroup( bindings, layout ) {
  105. let bindingPoint = 0;
  106. const entries = [];
  107. for ( const binding of bindings ) {
  108. if ( binding.isUniformBuffer ) {
  109. if ( binding.bufferGPU === null ) {
  110. const byteLength = binding.getByteLength();
  111. binding.bufferGPU = this.device.createBuffer( {
  112. label: 'bindingBuffer',
  113. size: byteLength,
  114. usage: binding.usage
  115. } );
  116. }
  117. entries.push( { binding: bindingPoint, resource: { buffer: binding.bufferGPU } } );
  118. } else if ( binding.isStorageBuffer ) {
  119. if ( binding.bufferGPU === null ) {
  120. const attribute = binding.attribute;
  121. this.attributes.update( attribute, false, binding.usage );
  122. binding.bufferGPU = this.attributes.get( attribute ).buffer;
  123. }
  124. entries.push( { binding: bindingPoint, resource: { buffer: binding.bufferGPU } } );
  125. } else if ( binding.isSampler ) {
  126. if ( binding.samplerGPU === null ) {
  127. binding.samplerGPU = this.textures.getDefaultSampler();
  128. }
  129. entries.push( { binding: bindingPoint, resource: binding.samplerGPU } );
  130. } else if ( binding.isSampledTexture ) {
  131. if ( binding.textureGPU === null ) {
  132. if ( binding.isSampledCubeTexture ) {
  133. binding.textureGPU = this.textures.getDefaultCubeTexture();
  134. } else if ( binding.texture.isVideoTexture ) {
  135. binding.textureGPU = this.textures.getDefaultVideoTexture();
  136. } else if ( binding.texture.isDepthTexture ) {
  137. binding.textureGPU = this.textures.getDefaultDepthTexture();
  138. } else {
  139. binding.textureGPU = this.textures.getDefaultTexture();
  140. }
  141. }
  142. const resource = binding.textureGPU instanceof GPUTexture ? binding.textureGPU.createView( { aspect: binding.aspect, dimension: binding.dimension } ) : binding.textureGPU;
  143. entries.push( { binding: bindingPoint, resource } );
  144. }
  145. bindingPoint ++;
  146. }
  147. return this.device.createBindGroup( {
  148. layout,
  149. entries
  150. } );
  151. }
  152. }
  153. export default WebGPUBindings;