WebGPUBindingUtils.js 5.7 KB

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