WebGPUBindings.js 7.3 KB

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