WebGPUUtils.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { GPUPrimitiveTopology, GPUTextureFormat } from './WebGPUConstants.js';
  2. class WebGPUUtils {
  3. constructor( backend ) {
  4. this.backend = backend;
  5. }
  6. getCurrentDepthStencilFormat( renderContext ) {
  7. let format;
  8. if ( renderContext.depthTexture !== null ) {
  9. format = this.getTextureFormatGPU( renderContext.depthTexture );
  10. } else if ( renderContext.depth && renderContext.stencil ) {
  11. format = GPUTextureFormat.Depth24PlusStencil8;
  12. } else if ( renderContext.depth ) {
  13. format = GPUTextureFormat.Depth24Plus;
  14. }
  15. return format;
  16. }
  17. getTextureFormatGPU( texture ) {
  18. return this.backend.get( texture ).texture.format;
  19. }
  20. getCurrentColorFormat( renderContext ) {
  21. let format;
  22. if ( renderContext.textures !== null ) {
  23. format = this.getTextureFormatGPU( renderContext.textures[ 0 ] );
  24. } else {
  25. format = GPUTextureFormat.BGRA8Unorm; // default context format
  26. }
  27. return format;
  28. }
  29. getCurrentColorSpace( renderContext ) {
  30. if ( renderContext.textures !== null ) {
  31. return renderContext.textures[ 0 ].colorSpace;
  32. }
  33. return this.backend.renderer.outputColorSpace;
  34. }
  35. getPrimitiveTopology( object, material ) {
  36. if ( object.isPoints ) return GPUPrimitiveTopology.PointList;
  37. else if ( object.isLineSegments || ( object.isMesh && material.wireframe === true ) ) return GPUPrimitiveTopology.LineList;
  38. else if ( object.isLine ) return GPUPrimitiveTopology.LineStrip;
  39. else if ( object.isMesh ) return GPUPrimitiveTopology.TriangleList;
  40. }
  41. getSampleCount( sampleCount ) {
  42. let count = 1;
  43. if ( sampleCount > 1 ) {
  44. // WebGPU only supports power-of-two sample counts and 2 is not a valid value
  45. count = Math.pow( 2, Math.floor( Math.log2( sampleCount ) ) );
  46. if ( count === 2 ) {
  47. count = 4;
  48. }
  49. }
  50. return count;
  51. }
  52. getSampleCountRenderContext( renderContext ) {
  53. if ( renderContext.textures !== null ) {
  54. return this.getSampleCount( renderContext.sampleCount );
  55. }
  56. return this.getSampleCount( this.backend.renderer.samples );
  57. }
  58. }
  59. export default WebGPUUtils;