webgpuHardwareTexture.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { HardwareTextureWrapper } from '../../Materials/Textures/hardwareTextureWrapper';
  2. import { InternalTextureSource } from '../../Materials/Textures/internalTexture';
  3. import { Nullable } from '../../types';
  4. import * as WebGPUConstants from './webgpuConstants';
  5. import { WebGPUTextureHelper } from './webgpuTextureHelper';
  6. /** @hidden */
  7. export class WebGPUHardwareTexture implements HardwareTextureWrapper {
  8. private _webgpuTexture: Nullable<GPUTexture>;
  9. private _webgpuMSAATexture: Nullable<GPUTexture>;
  10. public get underlyingResource(): Nullable<GPUTexture> {
  11. return this._webgpuTexture;
  12. }
  13. public get msaaTexture(): Nullable<GPUTexture> {
  14. return this._webgpuMSAATexture;
  15. }
  16. public set msaaTexture(texture: Nullable<GPUTexture>) {
  17. this._webgpuMSAATexture = texture;
  18. }
  19. public view: Nullable<GPUTextureView>;
  20. public format: GPUTextureFormat = WebGPUConstants.TextureFormat.RGBA8Unorm;
  21. public textureUsages = 0;
  22. constructor(existingTexture: Nullable<GPUTexture> = null) {
  23. this._webgpuTexture = existingTexture;
  24. this._webgpuMSAATexture = null;
  25. this.view = null;
  26. }
  27. public set(hardwareTexture: GPUTexture): void {
  28. this._webgpuTexture = hardwareTexture;
  29. }
  30. public setMSAATexture(hardwareTexture: GPUTexture): void {
  31. this._webgpuMSAATexture = hardwareTexture;
  32. }
  33. public setUsage(textureSource: number, generateMipMaps: boolean, isCube: boolean, width: number, height: number): void {
  34. generateMipMaps = textureSource === InternalTextureSource.RenderTarget ? false : generateMipMaps;
  35. this.createView({
  36. dimension: isCube ? WebGPUConstants.TextureViewDimension.Cube : WebGPUConstants.TextureViewDimension.E2d,
  37. mipLevelCount: generateMipMaps ? WebGPUTextureHelper.computeNumMipmapLevels(width, height) : 1,
  38. baseArrayLayer: 0,
  39. baseMipLevel: 0,
  40. aspect: WebGPUConstants.TextureAspect.All
  41. });
  42. }
  43. public createView(descriptor?: GPUTextureViewDescriptor): void {
  44. this.view = this._webgpuTexture!.createView(descriptor);
  45. }
  46. public reset(): void {
  47. this._webgpuTexture = null;
  48. this._webgpuMSAATexture = null;
  49. this.view = null;
  50. }
  51. public release(): void {
  52. this._webgpuTexture?.destroy();
  53. this._webgpuMSAATexture?.destroy();
  54. this.reset();
  55. }
  56. }