webgpuHardwareTexture.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { Scalar } from "../../Maths/math.scalar.js";
  2. // eslint-disable-next-line @typescript-eslint/naming-convention
  3. import * as WebGPUConstants from "./webgpuConstants.js";
  4. import { WebGPUTextureHelper } from "./webgpuTextureHelper.js";
  5. /** @internal */
  6. export class WebGPUHardwareTexture {
  7. get underlyingResource() {
  8. return this._webgpuTexture;
  9. }
  10. getMSAATexture(index = 0) {
  11. return this._webgpuMSAATexture?.[index] ?? null;
  12. }
  13. setMSAATexture(texture, index = -1) {
  14. if (!this._webgpuMSAATexture) {
  15. this._webgpuMSAATexture = [];
  16. }
  17. if (index === -1) {
  18. index = this._webgpuMSAATexture.length;
  19. }
  20. this._webgpuMSAATexture[index] = texture;
  21. }
  22. releaseMSAATexture() {
  23. if (this._webgpuMSAATexture) {
  24. for (const texture of this._webgpuMSAATexture) {
  25. texture?.destroy();
  26. }
  27. this._webgpuMSAATexture = null;
  28. }
  29. }
  30. constructor(existingTexture = null) {
  31. this.format = WebGPUConstants.TextureFormat.RGBA8Unorm;
  32. this.textureUsages = 0;
  33. this.textureAdditionalUsages = 0;
  34. this._webgpuTexture = existingTexture;
  35. this._webgpuMSAATexture = null;
  36. this.view = null;
  37. this.viewForWriting = null;
  38. }
  39. set(hardwareTexture) {
  40. this._webgpuTexture = hardwareTexture;
  41. }
  42. setUsage(_textureSource, generateMipMaps, is2DArray, isCube, is3D, width, height, depth) {
  43. let viewDimension = WebGPUConstants.TextureViewDimension.E2d;
  44. let arrayLayerCount = 1;
  45. if (isCube) {
  46. viewDimension = is2DArray ? WebGPUConstants.TextureViewDimension.CubeArray : WebGPUConstants.TextureViewDimension.Cube;
  47. arrayLayerCount = 6 * (depth || 1);
  48. }
  49. else if (is3D) {
  50. viewDimension = WebGPUConstants.TextureViewDimension.E3d;
  51. arrayLayerCount = 1;
  52. }
  53. else if (is2DArray) {
  54. viewDimension = WebGPUConstants.TextureViewDimension.E2dArray;
  55. arrayLayerCount = depth;
  56. }
  57. const format = WebGPUTextureHelper.GetDepthFormatOnly(this.format);
  58. const aspect = WebGPUTextureHelper.HasDepthAndStencilAspects(this.format) ? WebGPUConstants.TextureAspect.DepthOnly : WebGPUConstants.TextureAspect.All;
  59. this.createView({
  60. label: `TextureView${is3D ? "3D" : isCube ? "Cube" : "2D"}${is2DArray ? "_Array" + arrayLayerCount : ""}_${width}x${height}_${generateMipMaps ? "wmips" : "womips"}_${this.format}_${viewDimension}`,
  61. format,
  62. dimension: viewDimension,
  63. mipLevelCount: generateMipMaps ? Scalar.ILog2(Math.max(width, height)) + 1 : 1,
  64. baseArrayLayer: 0,
  65. baseMipLevel: 0,
  66. arrayLayerCount,
  67. aspect,
  68. });
  69. }
  70. createView(descriptor, createViewForWriting = false) {
  71. this.view = this._webgpuTexture.createView(descriptor);
  72. if (createViewForWriting && descriptor) {
  73. const saveNumMipMaps = descriptor.mipLevelCount;
  74. descriptor.mipLevelCount = 1;
  75. this.viewForWriting = this._webgpuTexture.createView(descriptor);
  76. descriptor.mipLevelCount = saveNumMipMaps;
  77. }
  78. }
  79. reset() {
  80. this._webgpuTexture = null;
  81. this._webgpuMSAATexture = null;
  82. this.view = null;
  83. this.viewForWriting = null;
  84. }
  85. release() {
  86. this._webgpuTexture?.destroy();
  87. this.releaseMSAATexture();
  88. this._copyInvertYTempTexture?.destroy();
  89. this.reset();
  90. }
  91. }
  92. //# sourceMappingURL=webgpuHardwareTexture.js.map