WebGPUUtils.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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( renderContext ) {
  42. if ( renderContext.textures !== null ) {
  43. return renderContext.sampleCount;
  44. }
  45. return this.backend.parameters.sampleCount;
  46. }
  47. }
  48. export default WebGPUUtils;