WebGPUBindingUtils.js 5.1 KB

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