WebGPUBindingUtils.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import {
  2. GPUTextureAspect, GPUTextureViewDimension, GPUTextureSampleType
  3. } from './WebGPUConstants.js';
  4. import { FloatType, IntType, UnsignedIntType } from 'three';
  5. class WebGPUBindingUtils {
  6. constructor( backend ) {
  7. this.backend = backend;
  8. }
  9. createBindingsLayout( bindGroup ) {
  10. const backend = this.backend;
  11. const device = backend.device;
  12. const entries = [];
  13. let index = 0;
  14. for ( const binding of bindGroup.bindings ) {
  15. const bindingGPU = {
  16. binding: index ++,
  17. visibility: binding.visibility
  18. };
  19. if ( binding.isUniformBuffer || binding.isStorageBuffer ) {
  20. const buffer = {}; // GPUBufferBindingLayout
  21. if ( binding.isStorageBuffer ) {
  22. buffer.type = binding.access;
  23. }
  24. bindingGPU.buffer = buffer;
  25. } else if ( binding.isSampler ) {
  26. const sampler = {}; // GPUSamplerBindingLayout
  27. if ( binding.texture.isDepthTexture ) {
  28. if ( binding.texture.compareFunction !== null ) {
  29. sampler.type = 'comparison';
  30. }
  31. }
  32. bindingGPU.sampler = sampler;
  33. } else if ( binding.isSampledTexture && binding.texture.isVideoTexture ) {
  34. bindingGPU.externalTexture = {}; // GPUExternalTextureBindingLayout
  35. } else if ( binding.isSampledTexture && binding.store ) {
  36. const format = this.backend.get( binding.texture ).texture.format;
  37. const access = binding.access;
  38. bindingGPU.storageTexture = { format, access }; // GPUStorageTextureBindingLayout
  39. } else if ( binding.isSampledTexture ) {
  40. const texture = {}; // GPUTextureBindingLayout
  41. if ( binding.texture.isDepthTexture ) {
  42. texture.sampleType = GPUTextureSampleType.Depth;
  43. } else if ( binding.texture.isDataTexture ) {
  44. const type = binding.texture.type;
  45. if ( type === IntType ) {
  46. texture.sampleType = GPUTextureSampleType.SInt;
  47. } else if ( type === UnsignedIntType ) {
  48. texture.sampleType = GPUTextureSampleType.UInt;
  49. } else if ( type === FloatType ) {
  50. // @TODO: Add support for this soon: backend.hasFeature( 'float32-filterable' )
  51. texture.sampleType = GPUTextureSampleType.UnfilterableFloat;
  52. }
  53. }
  54. if ( binding.isSampledCubeTexture ) {
  55. texture.viewDimension = GPUTextureViewDimension.Cube;
  56. } else if ( binding.texture.isDataArrayTexture ) {
  57. texture.viewDimension = GPUTextureViewDimension.TwoDArray;
  58. } else if ( binding.isSampledTexture3D ) {
  59. texture.viewDimension = GPUTextureViewDimension.ThreeD;
  60. }
  61. bindingGPU.texture = texture;
  62. } else {
  63. console.error( `WebGPUBindingUtils: Unsupported binding "${ binding }".` );
  64. }
  65. entries.push( bindingGPU );
  66. }
  67. return device.createBindGroupLayout( { entries } );
  68. }
  69. createBindings( bindGroup ) {
  70. const backend = this.backend;
  71. const bindingsData = backend.get( bindGroup );
  72. // setup (static) binding layout and (dynamic) binding group
  73. const bindLayoutGPU = this.createBindingsLayout( bindGroup );
  74. const bindGroupGPU = this.createBindGroup( bindGroup, bindLayoutGPU );
  75. bindingsData.layout = bindLayoutGPU;
  76. bindingsData.group = bindGroupGPU;
  77. }
  78. updateBinding( binding ) {
  79. const backend = this.backend;
  80. const device = backend.device;
  81. const buffer = binding.buffer;
  82. const bufferGPU = backend.get( binding ).buffer;
  83. device.queue.writeBuffer( bufferGPU, 0, buffer, 0 );
  84. }
  85. createBindGroup( bindGroup, layoutGPU ) {
  86. const backend = this.backend;
  87. const device = backend.device;
  88. let bindingPoint = 0;
  89. const entriesGPU = [];
  90. for ( const binding of bindGroup.bindings ) {
  91. if ( binding.isUniformBuffer ) {
  92. const bindingData = backend.get( binding );
  93. if ( bindingData.buffer === undefined ) {
  94. const byteLength = binding.byteLength;
  95. const usage = GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST;
  96. const bufferGPU = device.createBuffer( {
  97. label: 'bindingBuffer_' + binding.name,
  98. size: byteLength,
  99. usage: usage
  100. } );
  101. bindingData.buffer = bufferGPU;
  102. }
  103. entriesGPU.push( { binding: bindingPoint, resource: { buffer: bindingData.buffer } } );
  104. } else if ( binding.isStorageBuffer ) {
  105. const bindingData = backend.get( binding );
  106. if ( bindingData.buffer === undefined ) {
  107. const attribute = binding.attribute;
  108. //const usage = GPUBufferUsage.STORAGE | GPUBufferUsage.VERTEX | /*GPUBufferUsage.COPY_SRC |*/ GPUBufferUsage.COPY_DST;
  109. //backend.attributeUtils.createAttribute( attribute, usage ); // @TODO: Move it to universal renderer
  110. bindingData.buffer = backend.get( attribute ).buffer;
  111. }
  112. entriesGPU.push( { binding: bindingPoint, resource: { buffer: bindingData.buffer } } );
  113. } else if ( binding.isSampler ) {
  114. const textureGPU = backend.get( binding.texture );
  115. entriesGPU.push( { binding: bindingPoint, resource: textureGPU.sampler } );
  116. } else if ( binding.isSampledTexture ) {
  117. const textureData = backend.get( binding.texture );
  118. let dimensionViewGPU;
  119. if ( binding.isSampledCubeTexture ) {
  120. dimensionViewGPU = GPUTextureViewDimension.Cube;
  121. } else if ( binding.isSampledTexture3D ) {
  122. dimensionViewGPU = GPUTextureViewDimension.ThreeD;
  123. } else if ( binding.texture.isDataArrayTexture ) {
  124. dimensionViewGPU = GPUTextureViewDimension.TwoDArray;
  125. } else {
  126. dimensionViewGPU = GPUTextureViewDimension.TwoD;
  127. }
  128. let resourceGPU;
  129. if ( textureData.externalTexture !== undefined ) {
  130. resourceGPU = device.importExternalTexture( { source: textureData.externalTexture } );
  131. } else {
  132. const aspectGPU = GPUTextureAspect.All;
  133. resourceGPU = textureData.texture.createView( { aspect: aspectGPU, dimension: dimensionViewGPU, mipLevelCount: binding.store ? 1 : textureData.mipLevelCount } );
  134. }
  135. entriesGPU.push( { binding: bindingPoint, resource: resourceGPU } );
  136. }
  137. bindingPoint ++;
  138. }
  139. return device.createBindGroup( {
  140. label: 'bindGroup_' + bindGroup.name,
  141. layout: layoutGPU,
  142. entries: entriesGPU
  143. } );
  144. }
  145. }
  146. export default WebGPUBindingUtils;