WebGPUBindings.js 9.8 KB

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