WebGPUBindings.js 5.2 KB

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