2
0

WebGPUBindingUtils.js 5.4 KB

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