WebGPUUtils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 ).format;
  19. }
  20. getTextureSampleData( texture ) {
  21. let samples;
  22. if ( texture.isFramebufferTexture ) {
  23. samples = 1;
  24. } else if ( texture.isDepthTexture && ! texture.renderTarget ) {
  25. const renderer = this.backend.renderer;
  26. const renderTarget = renderer.getRenderTarget();
  27. samples = renderTarget ? renderTarget.samples : renderer.samples;
  28. } else if ( texture.renderTarget ) {
  29. samples = texture.renderTarget.samples;
  30. }
  31. samples = samples || 1;
  32. const isMSAA = samples > 1 && texture.renderTarget !== null && ( texture.isDepthTexture !== true && texture.isFramebufferTexture !== true );
  33. const primarySamples = isMSAA ? 1 : samples;
  34. return { samples, primarySamples, isMSAA };
  35. }
  36. getCurrentColorFormat( renderContext ) {
  37. let format;
  38. if ( renderContext.textures !== null ) {
  39. format = this.getTextureFormatGPU( renderContext.textures[ 0 ] );
  40. } else {
  41. format = this.getPreferredCanvasFormat(); // default context format
  42. }
  43. return format;
  44. }
  45. getCurrentColorSpace( renderContext ) {
  46. if ( renderContext.textures !== null ) {
  47. return renderContext.textures[ 0 ].colorSpace;
  48. }
  49. return this.backend.renderer.outputColorSpace;
  50. }
  51. getPrimitiveTopology( object, material ) {
  52. if ( object.isPoints ) return GPUPrimitiveTopology.PointList;
  53. else if ( object.isLineSegments || ( object.isMesh && material.wireframe === true ) ) return GPUPrimitiveTopology.LineList;
  54. else if ( object.isLine ) return GPUPrimitiveTopology.LineStrip;
  55. else if ( object.isMesh ) return GPUPrimitiveTopology.TriangleList;
  56. }
  57. getSampleCount( sampleCount ) {
  58. let count = 1;
  59. if ( sampleCount > 1 ) {
  60. // WebGPU only supports power-of-two sample counts and 2 is not a valid value
  61. count = Math.pow( 2, Math.floor( Math.log2( sampleCount ) ) );
  62. if ( count === 2 ) {
  63. count = 4;
  64. }
  65. }
  66. return count;
  67. }
  68. getSampleCountRenderContext( renderContext ) {
  69. if ( renderContext.textures !== null ) {
  70. return this.getSampleCount( renderContext.sampleCount );
  71. }
  72. return this.getSampleCount( this.backend.renderer.samples );
  73. }
  74. getPreferredCanvasFormat() {
  75. // TODO: Remove this check when Quest 34.5 is out
  76. // https://github.com/mrdoob/three.js/pull/29221/files#r1731833949
  77. if ( navigator.userAgent.includes( 'Quest' ) ) {
  78. return GPUTextureFormat.BGRA8Unorm;
  79. } else {
  80. return navigator.gpu.getPreferredCanvasFormat();
  81. }
  82. }
  83. }
  84. export default WebGPUUtils;
粤ICP备19079148号