1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include "Texture.h"
7
8 #include "ipc/WebGPUChild.h"
9 #include "mozilla/webgpu/ffi/wgpu.h"
10 #include "mozilla/dom/HTMLCanvasElement.h"
11 #include "mozilla/dom/WebGPUBinding.h"
12 #include "TextureView.h"
13
14 namespace mozilla {
15 namespace webgpu {
16
GPU_IMPL_CYCLE_COLLECTION(Texture,mParent)17 GPU_IMPL_CYCLE_COLLECTION(Texture, mParent)
18 GPU_IMPL_JS_WRAP(Texture)
19
20 static Maybe<uint8_t> GetBytesPerBlock(dom::GPUTextureFormat format) {
21 switch (format) {
22 case dom::GPUTextureFormat::R8unorm:
23 case dom::GPUTextureFormat::R8snorm:
24 case dom::GPUTextureFormat::R8uint:
25 case dom::GPUTextureFormat::R8sint:
26 return Some<uint8_t>(1u);
27 case dom::GPUTextureFormat::R16uint:
28 case dom::GPUTextureFormat::R16sint:
29 case dom::GPUTextureFormat::R16float:
30 case dom::GPUTextureFormat::Rg8unorm:
31 case dom::GPUTextureFormat::Rg8snorm:
32 case dom::GPUTextureFormat::Rg8uint:
33 case dom::GPUTextureFormat::Rg8sint:
34 return Some<uint8_t>(2u);
35 case dom::GPUTextureFormat::R32uint:
36 case dom::GPUTextureFormat::R32sint:
37 case dom::GPUTextureFormat::R32float:
38 case dom::GPUTextureFormat::Rg16uint:
39 case dom::GPUTextureFormat::Rg16sint:
40 case dom::GPUTextureFormat::Rg16float:
41 case dom::GPUTextureFormat::Rgba8unorm:
42 case dom::GPUTextureFormat::Rgba8unorm_srgb:
43 case dom::GPUTextureFormat::Rgba8snorm:
44 case dom::GPUTextureFormat::Rgba8uint:
45 case dom::GPUTextureFormat::Rgba8sint:
46 case dom::GPUTextureFormat::Bgra8unorm:
47 case dom::GPUTextureFormat::Bgra8unorm_srgb:
48 case dom::GPUTextureFormat::Rgb10a2unorm:
49 case dom::GPUTextureFormat::Rg11b10float:
50 return Some<uint8_t>(4u);
51 case dom::GPUTextureFormat::Rg32uint:
52 case dom::GPUTextureFormat::Rg32sint:
53 case dom::GPUTextureFormat::Rg32float:
54 case dom::GPUTextureFormat::Rgba16uint:
55 case dom::GPUTextureFormat::Rgba16sint:
56 case dom::GPUTextureFormat::Rgba16float:
57 return Some<uint8_t>(8u);
58 case dom::GPUTextureFormat::Rgba32uint:
59 case dom::GPUTextureFormat::Rgba32sint:
60 case dom::GPUTextureFormat::Rgba32float:
61 return Some<uint8_t>(16u);
62 case dom::GPUTextureFormat::Depth32float:
63 return Some<uint8_t>(4u);
64 case dom::GPUTextureFormat::Bc1_rgba_unorm:
65 case dom::GPUTextureFormat::Bc1_rgba_unorm_srgb:
66 case dom::GPUTextureFormat::Bc4_r_unorm:
67 case dom::GPUTextureFormat::Bc4_r_snorm:
68 return Some<uint8_t>(8u);
69 case dom::GPUTextureFormat::Bc2_rgba_unorm:
70 case dom::GPUTextureFormat::Bc2_rgba_unorm_srgb:
71 case dom::GPUTextureFormat::Bc3_rgba_unorm:
72 case dom::GPUTextureFormat::Bc3_rgba_unorm_srgb:
73 case dom::GPUTextureFormat::Bc5_rg_unorm:
74 case dom::GPUTextureFormat::Bc5_rg_snorm:
75 case dom::GPUTextureFormat::Bc6h_rgb_ufloat:
76 case dom::GPUTextureFormat::Bc6h_rgb_float:
77 case dom::GPUTextureFormat::Bc7_rgba_unorm:
78 case dom::GPUTextureFormat::Bc7_rgba_unorm_srgb:
79 return Some<uint8_t>(16u);
80 case dom::GPUTextureFormat::Depth24plus:
81 case dom::GPUTextureFormat::Depth24plus_stencil8:
82 case dom::GPUTextureFormat::EndGuard_:
83 return Nothing();
84 }
85 return Nothing();
86 }
87
Texture(Device * const aParent,RawId aId,const dom::GPUTextureDescriptor & aDesc)88 Texture::Texture(Device* const aParent, RawId aId,
89 const dom::GPUTextureDescriptor& aDesc)
90 : ChildOf(aParent),
91 mId(aId),
92 mBytesPerBlock(GetBytesPerBlock(aDesc.mFormat)) {}
93
~Texture()94 Texture::~Texture() { Cleanup(); }
95
Cleanup()96 void Texture::Cleanup() {
97 if (mValid && mParent) {
98 mValid = false;
99 auto bridge = mParent->GetBridge();
100 if (bridge && bridge->IsOpen()) {
101 bridge->SendTextureDestroy(mId);
102 }
103 }
104 }
105
CreateView(const dom::GPUTextureViewDescriptor & aDesc)106 already_AddRefed<TextureView> Texture::CreateView(
107 const dom::GPUTextureViewDescriptor& aDesc) {
108 RawId id = mParent->GetBridge()->TextureCreateView(mId, mParent->mId, aDesc);
109 RefPtr<TextureView> view = new TextureView(this, id);
110 return view.forget();
111 }
112
Destroy()113 void Texture::Destroy() {
114 // TODO: we don't have to implement it right now, but it's used by the
115 // examples
116 }
117
118 } // namespace webgpu
119 } // namespace mozilla
120