WebGPUBindingUtils.js 4.9 KB

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