WebGPUBindings.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 data = this.get( object );
  45. const bindings = data.bindings;
  46. const updateMap = this.updateMap;
  47. const frame = this.info.render.frame;
  48. const sharedUniformsGroups = this.sharedUniformsGroups;
  49. let needsBindGroupRefresh = false;
  50. // iterate over all bindings and check if buffer updates or a new binding group is required
  51. for ( const binding of bindings ) {
  52. if ( binding.isUniformsGroup ) {
  53. const isShared = sharedUniformsGroups.has( binding.name );
  54. const isUpdated = updateMap.get( binding ) === frame;
  55. if ( isShared && isUpdated ) continue;
  56. const array = binding.array;
  57. const bufferGPU = binding.bufferGPU;
  58. const needsBufferWrite = binding.update( array, object, camera );
  59. if ( needsBufferWrite === true ) {
  60. this.device.defaultQueue.writeBuffer(
  61. bufferGPU,
  62. 0,
  63. array,
  64. 0
  65. );
  66. }
  67. updateMap.set( binding, frame );
  68. } else if ( binding.isSampler ) {
  69. const material = object.material;
  70. const texture = material[ binding.name ];
  71. const forceUpdate = this.textures.updateSampler( texture );
  72. const samplerGPU = this.textures.getSampler( texture );
  73. if ( binding.samplerGPU !== samplerGPU || forceUpdate ) {
  74. binding.samplerGPU = samplerGPU;
  75. needsBindGroupRefresh = true;
  76. }
  77. } else if ( binding.isSampledTexture ) {
  78. const material = object.material;
  79. const texture = material[ binding.name ];
  80. const forceUpdate = this.textures.updateTexture( texture );
  81. const textureGPU = this.textures.getTextureGPU( texture );
  82. if ( binding.textureGPU !== textureGPU || forceUpdate ) {
  83. binding.textureGPU = textureGPU;
  84. needsBindGroupRefresh = true;
  85. }
  86. }
  87. }
  88. if ( needsBindGroupRefresh === true ) {
  89. data.group = this._createBindGroup( bindings, data.layout );
  90. }
  91. }
  92. dispose() {
  93. this.uniformsData = new WeakMap();
  94. this.updateMap = new WeakMap();
  95. }
  96. _createBindLayout( bindings ) {
  97. let bindingPoint = 0;
  98. const entries = [];
  99. for ( const binding of bindings ) {
  100. entries.push( { binding: bindingPoint, visibility: binding.visibility, type: binding.type } );
  101. bindingPoint ++;
  102. }
  103. return this.device.createBindGroupLayout( { entries: entries } );
  104. }
  105. _createBindGroup( bindings, layout ) {
  106. let bindingPoint = 0;
  107. const entries = [];
  108. for ( const binding of bindings ) {
  109. if ( binding.isUniformsGroup ) {
  110. if ( binding.bufferGPU === null ) {
  111. const byteLength = binding.getByteLength();
  112. binding.array = new Float32Array( new ArrayBuffer( byteLength ) );
  113. binding.bufferGPU = this.device.createBuffer( {
  114. size: byteLength,
  115. usage: binding.usage,
  116. } );
  117. }
  118. entries.push( { binding: bindingPoint, resource: { buffer: binding.bufferGPU } } );
  119. } else if ( binding.isSampler ) {
  120. if ( binding.samplerGPU === null ) {
  121. binding.samplerGPU = this.textures.getDefaultSampler();
  122. }
  123. entries.push( { binding: bindingPoint, resource: binding.samplerGPU } );
  124. } else if ( binding.isSampledTexture ) {
  125. if ( binding.textureGPU === null ) {
  126. binding.textureGPU = this.textures.getDefaultTexture();
  127. }
  128. entries.push( { binding: bindingPoint, resource: binding.textureGPU.createView() } );
  129. }
  130. bindingPoint ++;
  131. }
  132. return this.device.createBindGroup( {
  133. layout: layout,
  134. entries: entries
  135. } );
  136. }
  137. _getMeshBasicBindings() {
  138. const bindings = [];
  139. // ubos
  140. const modelGroup = new WebGPUUniformsGroup();
  141. modelGroup.setName( 'modelUniforms' );
  142. modelGroup.setUniform( 'modelMatrix', new Matrix4() );
  143. modelGroup.setUniform( 'modelViewMatrix', new Matrix4() );
  144. modelGroup.setUpdateCallback( function ( array, object/*, camera */ ) {
  145. array.set( object.matrixWorld.elements, 0 );
  146. array.set( object.modelViewMatrix.elements, 16 );
  147. return true; // TODO: implement caching (return false when cache hits occurs)
  148. } );
  149. const cameraGroup = this.sharedUniformsGroups.get( 'cameraUniforms' );
  150. // samplers
  151. const diffuseSampler = new WebGPUSampler();
  152. diffuseSampler.setName( 'map' );
  153. // textures
  154. const diffuseTexture = new WebGPUSampledTexture();
  155. diffuseTexture.setName( 'map' );
  156. //
  157. bindings.push( modelGroup );
  158. bindings.push( cameraGroup );
  159. bindings.push( diffuseSampler );
  160. bindings.push( diffuseTexture );
  161. return bindings;
  162. }
  163. _getPointsBasicBindings() {
  164. const bindings = [];
  165. // ubos
  166. const modelGroup = new WebGPUUniformsGroup();
  167. modelGroup.setName( 'modelUniforms' );
  168. modelGroup.setUniform( 'modelMatrix', new Matrix4() );
  169. modelGroup.setUniform( 'modelViewMatrix', new Matrix4() );
  170. modelGroup.setUpdateCallback( function ( array, object/*, camera */ ) {
  171. array.set( object.matrixWorld.elements, 0 );
  172. array.set( object.modelViewMatrix.elements, 16 );
  173. return true; // TODO: implement caching (return false when cache hits occurs)
  174. } );
  175. const cameraGroup = this.sharedUniformsGroups.get( 'cameraUniforms' );
  176. //
  177. bindings.push( modelGroup );
  178. bindings.push( cameraGroup );
  179. return bindings;
  180. }
  181. _getLinesBasicBindings() {
  182. const bindings = [];
  183. // ubos
  184. const modelGroup = new WebGPUUniformsGroup();
  185. modelGroup.setName( 'modelUniforms' );
  186. modelGroup.setUniform( 'modelMatrix', new Matrix4() );
  187. modelGroup.setUniform( 'modelViewMatrix', new Matrix4() );
  188. modelGroup.setUpdateCallback( function ( array, object/*, camera */ ) {
  189. array.set( object.matrixWorld.elements, 0 );
  190. array.set( object.modelViewMatrix.elements, 16 );
  191. return true; // TODO: implement caching (return false when cache hits occurs)
  192. } );
  193. const cameraGroup = this.sharedUniformsGroups.get( 'cameraUniforms' );
  194. //
  195. bindings.push( modelGroup );
  196. bindings.push( cameraGroup );
  197. return bindings;
  198. }
  199. _setupSharedUniformsGroups() {
  200. const cameraGroup = new WebGPUUniformsGroup();
  201. cameraGroup.setName( 'cameraUniforms' );
  202. cameraGroup.setUniform( 'projectionMatrix', new Matrix4() );
  203. cameraGroup.setUniform( 'viewMatrix', new Matrix4() );
  204. cameraGroup.setUpdateCallback( function ( array, object, camera ) {
  205. array.set( camera.projectionMatrix.elements, 0 );
  206. array.set( camera.matrixWorldInverse.elements, 16 );
  207. return true; // TODO: implement caching (return false when cache hits occurs)
  208. } );
  209. this.sharedUniformsGroups.set( cameraGroup.name, cameraGroup );
  210. }
  211. }
  212. export default WebGPUBindings;