WebGPUBindings.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.sharedUniformsGroups = new Map();
  15. this.updateMap = new WeakMap();
  16. this._setupSharedUniformsGroups();
  17. }
  18. get( object ) {
  19. let data = this.uniformsData.get( object );
  20. if ( data === undefined ) {
  21. const pipeline = this.pipelines.get( object );
  22. const material = object.material;
  23. const nodeBuilder = this.nodes.get( material );
  24. // each material defines an array of bindings (ubos, textures, samplers etc.)
  25. const bindings = this.composeBindings( object, nodeBuilder.getBindings( 'fragment' ) );
  26. // setup (static) binding layout and (dynamic) binding group
  27. const bindLayout = pipeline.getBindGroupLayout( 0 );
  28. const bindGroup = this._createBindGroup( bindings, bindLayout );
  29. data = {
  30. layout: bindLayout,
  31. group: bindGroup,
  32. bindings: bindings
  33. };
  34. this.uniformsData.set( object, data );
  35. }
  36. return data;
  37. }
  38. getForCompute( param ) {
  39. let data = this.uniformsData.get( param );
  40. if ( data === undefined ) {
  41. const pipeline = this.computePipelines.get( param );
  42. const bindings = param.bindings !== undefined ? param.bindings.slice() : [];
  43. const bindLayout = pipeline.getBindGroupLayout( 0 );
  44. const bindGroup = this._createBindGroup( bindings, bindLayout );
  45. data = {
  46. layout: bindLayout,
  47. group: bindGroup,
  48. bindings: bindings
  49. };
  50. this.uniformsData.set( param, data );
  51. }
  52. return data;
  53. }
  54. update( object, camera ) {
  55. const textures = this.textures;
  56. const data = this.get( object );
  57. const bindings = data.bindings;
  58. const updateMap = this.updateMap;
  59. const frame = this.info.render.frame;
  60. const sharedUniformsGroups = this.sharedUniformsGroups;
  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. if ( binding.isUniformsGroup ) {
  65. const isShared = sharedUniformsGroups.has( binding.name );
  66. const isUpdated = updateMap.get( binding ) === frame;
  67. if ( isShared && isUpdated ) continue;
  68. const array = binding.array;
  69. const bufferGPU = binding.bufferGPU;
  70. binding.onBeforeUpdate( object, camera );
  71. const needsBufferWrite = binding.update();
  72. if ( needsBufferWrite === true ) {
  73. this.device.defaultQueue.writeBuffer(
  74. bufferGPU,
  75. 0,
  76. array,
  77. 0
  78. );
  79. }
  80. updateMap.set( binding, frame );
  81. } else if ( binding.isStorageBuffer ) {
  82. const attribute = binding.attribute;
  83. this.attributes.update( attribute, false, binding.usage );
  84. } else if ( binding.isSampler ) {
  85. const texture = binding.texture;
  86. textures.updateSampler( texture );
  87. const samplerGPU = textures.getSampler( texture );
  88. if ( binding.samplerGPU !== samplerGPU ) {
  89. binding.samplerGPU = samplerGPU;
  90. needsBindGroupRefresh = true;
  91. }
  92. } else if ( binding.isSampledTexture ) {
  93. const texture = binding.texture;
  94. const forceUpdate = textures.updateTexture( texture );
  95. const textureGPU = textures.getTextureGPU( texture );
  96. if ( binding.textureGPU !== textureGPU || forceUpdate === true ) {
  97. binding.textureGPU = textureGPU;
  98. needsBindGroupRefresh = true;
  99. }
  100. }
  101. }
  102. if ( needsBindGroupRefresh === true ) {
  103. data.group = this._createBindGroup( bindings, data.layout );
  104. }
  105. }
  106. dispose() {
  107. this.uniformsData = new WeakMap();
  108. this.updateMap = new WeakMap();
  109. }
  110. _createBindGroup( bindings, layout ) {
  111. let bindingPoint = 0;
  112. const entries = [];
  113. for ( const binding of bindings ) {
  114. if ( binding.isUniformsGroup ) {
  115. if ( binding.bufferGPU === null ) {
  116. const byteLength = binding.getByteLength();
  117. binding.array = new Float32Array( new ArrayBuffer( byteLength ) );
  118. binding.bufferGPU = this.device.createBuffer( {
  119. size: byteLength,
  120. usage: binding.usage,
  121. } );
  122. }
  123. entries.push( { binding: bindingPoint, resource: { buffer: binding.bufferGPU } } );
  124. } else if ( binding.isStorageBuffer ) {
  125. if ( binding.bufferGPU === null ) {
  126. const attribute = binding.attribute;
  127. this.attributes.update( attribute, false, binding.usage );
  128. binding.bufferGPU = this.attributes.get( attribute ).buffer;
  129. }
  130. entries.push( { binding: bindingPoint, resource: { buffer: binding.bufferGPU } } );
  131. } else if ( binding.isSampler ) {
  132. if ( binding.samplerGPU === null ) {
  133. binding.samplerGPU = this.textures.getDefaultSampler();
  134. }
  135. entries.push( { binding: bindingPoint, resource: binding.samplerGPU } );
  136. } else if ( binding.isSampledTexture ) {
  137. if ( binding.textureGPU === null ) {
  138. if ( binding.isSampledCubeTexture ) {
  139. binding.textureGPU = this.textures.getDefaultCubeTexture();
  140. } else {
  141. binding.textureGPU = this.textures.getDefaultTexture();
  142. }
  143. }
  144. entries.push( { binding: bindingPoint, resource: binding.textureGPU.createView( { dimension: binding.dimension } ) } );
  145. }
  146. bindingPoint ++;
  147. }
  148. return this.device.createBindGroup( {
  149. layout: layout,
  150. entries: entries
  151. } );
  152. }
  153. composeBindings( object, uniforms ) {
  154. const bindings = [];
  155. // UBOs
  156. // model
  157. const modelViewUniform = new Matrix4Uniform( 'modelMatrix' );
  158. const modelViewMatrixUniform = new Matrix4Uniform( 'modelViewMatrix' );
  159. const normalMatrixUniform = new Matrix3Uniform( 'normalMatrix' );
  160. const modelGroup = new WebGPUUniformsGroup( 'modelUniforms' );
  161. modelGroup.addUniform( modelViewUniform );
  162. modelGroup.addUniform( modelViewMatrixUniform );
  163. modelGroup.addUniform( normalMatrixUniform );
  164. modelGroup.setOnBeforeUpdate( function ( object/*, camera */ ) {
  165. modelViewUniform.setValue( object.matrixWorld );
  166. modelViewMatrixUniform.setValue( object.modelViewMatrix );
  167. normalMatrixUniform.setValue( object.normalMatrix );
  168. } );
  169. // camera
  170. const cameraGroup = this.sharedUniformsGroups.get( 'cameraUniforms' );
  171. // the order of WebGPUBinding objects must match the binding order in the shader
  172. bindings.push( modelGroup );
  173. bindings.push( cameraGroup );
  174. bindings.push( ... uniforms );
  175. return bindings;
  176. }
  177. _setupSharedUniformsGroups() {
  178. const projectionMatrixUniform = new Matrix4Uniform( 'projectionMatrix' );
  179. const viewMatrixUniform = new Matrix4Uniform( 'viewMatrix' );
  180. const cameraGroup = new WebGPUUniformsGroup( 'cameraUniforms' );
  181. cameraGroup.addUniform( projectionMatrixUniform );
  182. cameraGroup.addUniform( viewMatrixUniform );
  183. cameraGroup.setOnBeforeUpdate( function ( object, camera ) {
  184. projectionMatrixUniform.setValue( camera.projectionMatrix );
  185. viewMatrixUniform.setValue( camera.matrixWorldInverse );
  186. } );
  187. this.sharedUniformsGroups.set( cameraGroup.name, cameraGroup );
  188. }
  189. }
  190. export default WebGPUBindings;