WebGPUBindings.js 8.4 KB

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