WebGPUBindings.js 5.3 KB

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