WebGPUBindings.js 9.9 KB

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