WebGPUBindings.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import WebGPUUniformsGroup from './WebGPUUniformsGroup.js';
  2. import WebGPUSampler from './WebGPUSampler.js';
  3. import WebGPUSampledTexture from './WebGPUSampledTexture.js';
  4. import { Matrix4 } from '../../../../build/three.module.js';
  5. class WebGPUBindings {
  6. constructor( device, info, properties, textures ) {
  7. this.device = device;
  8. this.info = info;
  9. this.properties = properties;
  10. this.textures = textures;
  11. this.uniformsData = new WeakMap();
  12. this.sharedUniformsGroups = new Map();
  13. this.updateMap = new WeakMap();
  14. this._setupSharedUniformsGroups();
  15. }
  16. get( object ) {
  17. let data = this.uniformsData.get( object );
  18. if ( data === undefined ) {
  19. const material = object.material;
  20. let bindings;
  21. // each material defines an array of bindings (ubos, textures, samplers etc.)
  22. if ( material.isMeshBasicMaterial ) {
  23. bindings = this._getMeshBasicBindings();
  24. } else if ( material.isPointsMaterial ) {
  25. bindings = this._getPointsBasicBindings();
  26. } else if ( material.isLineBasicMaterial ) {
  27. bindings = this._getLinesBasicBindings();
  28. } else {
  29. console.error( 'WebGPURenderer: Unknwon shader type' );
  30. }
  31. // setup (static) binding layout and (dynamic) binding group
  32. const bindLayout = this._createBindLayout( bindings );
  33. const bindGroup = this._createBindGroup( bindings, bindLayout );
  34. data = {
  35. layout: bindLayout,
  36. group: bindGroup,
  37. bindings: bindings
  38. };
  39. this.uniformsData.set( object, data );
  40. }
  41. return data;
  42. }
  43. update( object, camera ) {
  44. const textures = this.textures;
  45. const data = this.get( object );
  46. const bindings = data.bindings;
  47. const updateMap = this.updateMap;
  48. const frame = this.info.render.frame;
  49. const sharedUniformsGroups = this.sharedUniformsGroups;
  50. let needsBindGroupRefresh = false;
  51. // iterate over all bindings and check if buffer updates or a new binding group is required
  52. for ( const binding of bindings ) {
  53. if ( binding.isUniformsGroup ) {
  54. const isShared = sharedUniformsGroups.has( binding.name );
  55. const isUpdated = updateMap.get( binding ) === frame;
  56. if ( isShared && isUpdated ) continue;
  57. const array = binding.array;
  58. const bufferGPU = binding.bufferGPU;
  59. const needsBufferWrite = binding.update( array, object, camera );
  60. if ( needsBufferWrite === true ) {
  61. this.device.defaultQueue.writeBuffer(
  62. bufferGPU,
  63. 0,
  64. array,
  65. 0
  66. );
  67. }
  68. updateMap.set( binding, frame );
  69. } else if ( binding.isSampler ) {
  70. const material = object.material;
  71. const texture = material[ binding.name ];
  72. textures.updateSampler( texture );
  73. const samplerGPU = textures.getSampler( texture );
  74. if ( binding.samplerGPU !== samplerGPU ) {
  75. binding.samplerGPU = samplerGPU;
  76. needsBindGroupRefresh = true;
  77. }
  78. } else if ( binding.isSampledTexture ) {
  79. const material = object.material;
  80. const texture = material[ binding.name ];
  81. const forceUpdate = textures.updateTexture( texture );
  82. const textureGPU = textures.getTextureGPU( texture );
  83. if ( binding.textureGPU !== textureGPU || forceUpdate === true ) {
  84. binding.textureGPU = textureGPU;
  85. needsBindGroupRefresh = true;
  86. }
  87. }
  88. }
  89. if ( needsBindGroupRefresh === true ) {
  90. data.group = this._createBindGroup( bindings, data.layout );
  91. }
  92. }
  93. dispose() {
  94. this.uniformsData = new WeakMap();
  95. this.updateMap = new WeakMap();
  96. }
  97. _createBindLayout( bindings ) {
  98. let bindingPoint = 0;
  99. const entries = [];
  100. for ( const binding of bindings ) {
  101. entries.push( { binding: bindingPoint, visibility: binding.visibility, type: binding.type } );
  102. bindingPoint ++;
  103. }
  104. return this.device.createBindGroupLayout( { entries: entries } );
  105. }
  106. _createBindGroup( bindings, layout ) {
  107. let bindingPoint = 0;
  108. const entries = [];
  109. for ( const binding of bindings ) {
  110. if ( binding.isUniformsGroup ) {
  111. if ( binding.bufferGPU === null ) {
  112. const byteLength = binding.getByteLength();
  113. binding.array = new Float32Array( new ArrayBuffer( byteLength ) );
  114. binding.bufferGPU = this.device.createBuffer( {
  115. size: byteLength,
  116. usage: binding.usage,
  117. } );
  118. }
  119. entries.push( { binding: bindingPoint, resource: { buffer: binding.bufferGPU } } );
  120. } else if ( binding.isSampler ) {
  121. if ( binding.samplerGPU === null ) {
  122. binding.samplerGPU = this.textures.getDefaultSampler();
  123. }
  124. entries.push( { binding: bindingPoint, resource: binding.samplerGPU } );
  125. } else if ( binding.isSampledTexture ) {
  126. if ( binding.textureGPU === null ) {
  127. binding.textureGPU = this.textures.getDefaultTexture();
  128. }
  129. entries.push( { binding: bindingPoint, resource: binding.textureGPU.createView() } );
  130. }
  131. bindingPoint ++;
  132. }
  133. return this.device.createBindGroup( {
  134. layout: layout,
  135. entries: entries
  136. } );
  137. }
  138. _getMeshBasicBindings() {
  139. const bindings = [];
  140. // ubos
  141. const modelGroup = new WebGPUUniformsGroup();
  142. modelGroup.setName( 'modelUniforms' );
  143. modelGroup.setUniform( 'modelMatrix', new Matrix4() );
  144. modelGroup.setUniform( 'modelViewMatrix', new Matrix4() );
  145. modelGroup.setUpdateCallback( function ( array, object/*, camera */ ) {
  146. array.set( object.matrixWorld.elements, 0 );
  147. array.set( object.modelViewMatrix.elements, 16 );
  148. return true; // TODO: implement caching (return false when cache hits occurs)
  149. } );
  150. const cameraGroup = this.sharedUniformsGroups.get( 'cameraUniforms' );
  151. // samplers
  152. const diffuseSampler = new WebGPUSampler();
  153. diffuseSampler.setName( 'map' );
  154. // textures
  155. const diffuseTexture = new WebGPUSampledTexture();
  156. diffuseTexture.setName( 'map' );
  157. //
  158. bindings.push( modelGroup );
  159. bindings.push( cameraGroup );
  160. bindings.push( diffuseSampler );
  161. bindings.push( diffuseTexture );
  162. return bindings;
  163. }
  164. _getPointsBasicBindings() {
  165. const bindings = [];
  166. // ubos
  167. const modelGroup = new WebGPUUniformsGroup();
  168. modelGroup.setName( 'modelUniforms' );
  169. modelGroup.setUniform( 'modelMatrix', new Matrix4() );
  170. modelGroup.setUniform( 'modelViewMatrix', new Matrix4() );
  171. modelGroup.setUpdateCallback( function ( array, object/*, camera */ ) {
  172. array.set( object.matrixWorld.elements, 0 );
  173. array.set( object.modelViewMatrix.elements, 16 );
  174. return true; // TODO: implement caching (return false when cache hits occurs)
  175. } );
  176. const cameraGroup = this.sharedUniformsGroups.get( 'cameraUniforms' );
  177. //
  178. bindings.push( modelGroup );
  179. bindings.push( cameraGroup );
  180. return bindings;
  181. }
  182. _getLinesBasicBindings() {
  183. const bindings = [];
  184. // ubos
  185. const modelGroup = new WebGPUUniformsGroup();
  186. modelGroup.setName( 'modelUniforms' );
  187. modelGroup.setUniform( 'modelMatrix', new Matrix4() );
  188. modelGroup.setUniform( 'modelViewMatrix', new Matrix4() );
  189. modelGroup.setUpdateCallback( function ( array, object/*, camera */ ) {
  190. array.set( object.matrixWorld.elements, 0 );
  191. array.set( object.modelViewMatrix.elements, 16 );
  192. return true; // TODO: implement caching (return false when cache hits occurs)
  193. } );
  194. const cameraGroup = this.sharedUniformsGroups.get( 'cameraUniforms' );
  195. //
  196. bindings.push( modelGroup );
  197. bindings.push( cameraGroup );
  198. return bindings;
  199. }
  200. _setupSharedUniformsGroups() {
  201. const cameraGroup = new WebGPUUniformsGroup();
  202. cameraGroup.setName( 'cameraUniforms' );
  203. cameraGroup.setUniform( 'projectionMatrix', new Matrix4() );
  204. cameraGroup.setUniform( 'viewMatrix', new Matrix4() );
  205. cameraGroup.setUpdateCallback( function ( array, object, camera ) {
  206. array.set( camera.projectionMatrix.elements, 0 );
  207. array.set( camera.matrixWorldInverse.elements, 16 );
  208. return true; // TODO: implement caching (return false when cache hits occurs)
  209. } );
  210. this.sharedUniformsGroups.set( cameraGroup.name, cameraGroup );
  211. }
  212. }
  213. export default WebGPUBindings;