WebGPUBindings.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. const opacityGroup = new WebGPUUniformsGroup();
  152. opacityGroup.setName( 'opacityUniforms' );
  153. opacityGroup.setUniform( 'opacity', 1.0 );
  154. opacityGroup.visibility = GPUShaderStage.FRAGMENT;
  155. opacityGroup.setUpdateCallback( function ( array, object ) {
  156. const material = object.material;
  157. const opacity = material.transparent ? material.opacity : 1.0;
  158. let updated = false;
  159. if ( array[ 0 ] !== opacity ) {
  160. array[ 0 ] = opacity;
  161. updated = true;
  162. }
  163. return updated;
  164. } );
  165. // samplers
  166. const diffuseSampler = new WebGPUSampler();
  167. diffuseSampler.setName( 'map' );
  168. // textures
  169. const diffuseTexture = new WebGPUSampledTexture();
  170. diffuseTexture.setName( 'map' );
  171. //
  172. bindings.push( modelGroup );
  173. bindings.push( cameraGroup );
  174. bindings.push( opacityGroup );
  175. bindings.push( diffuseSampler );
  176. bindings.push( diffuseTexture );
  177. return bindings;
  178. }
  179. _getPointsBasicBindings() {
  180. const bindings = [];
  181. // ubos
  182. const modelGroup = new WebGPUUniformsGroup();
  183. modelGroup.setName( 'modelUniforms' );
  184. modelGroup.setUniform( 'modelMatrix', new Matrix4() );
  185. modelGroup.setUniform( 'modelViewMatrix', new Matrix4() );
  186. modelGroup.setUpdateCallback( function ( array, object/*, camera */ ) {
  187. array.set( object.matrixWorld.elements, 0 );
  188. array.set( object.modelViewMatrix.elements, 16 );
  189. return true; // @TODO: Implement caching (return false when cache hits occurs)
  190. } );
  191. const cameraGroup = this.sharedUniformsGroups.get( 'cameraUniforms' );
  192. //
  193. bindings.push( modelGroup );
  194. bindings.push( cameraGroup );
  195. return bindings;
  196. }
  197. _getLinesBasicBindings() {
  198. const bindings = [];
  199. // ubos
  200. const modelGroup = new WebGPUUniformsGroup();
  201. modelGroup.setName( 'modelUniforms' );
  202. modelGroup.setUniform( 'modelMatrix', new Matrix4() );
  203. modelGroup.setUniform( 'modelViewMatrix', new Matrix4() );
  204. modelGroup.setUpdateCallback( function ( array, object/*, camera */ ) {
  205. array.set( object.matrixWorld.elements, 0 );
  206. array.set( object.modelViewMatrix.elements, 16 );
  207. return true; // @TODO: Implement caching (return false when cache hits occurs)
  208. } );
  209. const cameraGroup = this.sharedUniformsGroups.get( 'cameraUniforms' );
  210. //
  211. bindings.push( modelGroup );
  212. bindings.push( cameraGroup );
  213. return bindings;
  214. }
  215. _setupSharedUniformsGroups() {
  216. const cameraGroup = new WebGPUUniformsGroup();
  217. cameraGroup.setName( 'cameraUniforms' );
  218. cameraGroup.setUniform( 'projectionMatrix', new Matrix4() );
  219. cameraGroup.setUniform( 'viewMatrix', new Matrix4() );
  220. cameraGroup.setUpdateCallback( function ( array, object, camera ) {
  221. array.set( camera.projectionMatrix.elements, 0 );
  222. array.set( camera.matrixWorldInverse.elements, 16 );
  223. return true; // @TODO: Implement caching (return false when cache hits occurs)
  224. } );
  225. this.sharedUniformsGroups.set( cameraGroup.name, cameraGroup );
  226. }
  227. }
  228. export default WebGPUBindings;