WebGPUBindings.js 9.8 KB

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