summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/Texture.cpp
blob: e80710647ee5aae905338d1d95800dca5f867c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Texture.h"

#include "ipc/WebGPUChild.h"
#include "mozilla/webgpu/ffi/wgpu.h"
#include "mozilla/webgpu/CanvasContext.h"
#include "mozilla/dom/WebGPUBinding.h"
#include "mozilla/webgpu/WebGPUTypes.h"
#include "TextureView.h"
#include "Utility.h"

namespace mozilla::webgpu {

GPU_IMPL_CYCLE_COLLECTION(Texture, mParent)
GPU_IMPL_JS_WRAP(Texture)

static Maybe<uint8_t> GetBytesPerBlock(dom::GPUTextureFormat format) {
  switch (format) {
    case dom::GPUTextureFormat::R8unorm:
    case dom::GPUTextureFormat::R8snorm:
    case dom::GPUTextureFormat::R8uint:
    case dom::GPUTextureFormat::R8sint:
      return Some<uint8_t>(1u);
    case dom::GPUTextureFormat::R16uint:
    case dom::GPUTextureFormat::R16sint:
    case dom::GPUTextureFormat::R16float:
    case dom::GPUTextureFormat::Rg8unorm:
    case dom::GPUTextureFormat::Rg8snorm:
    case dom::GPUTextureFormat::Rg8uint:
    case dom::GPUTextureFormat::Rg8sint:
      return Some<uint8_t>(2u);
    case dom::GPUTextureFormat::R32uint:
    case dom::GPUTextureFormat::R32sint:
    case dom::GPUTextureFormat::R32float:
    case dom::GPUTextureFormat::Rg16uint:
    case dom::GPUTextureFormat::Rg16sint:
    case dom::GPUTextureFormat::Rg16float:
    case dom::GPUTextureFormat::Rgba8unorm:
    case dom::GPUTextureFormat::Rgba8unorm_srgb:
    case dom::GPUTextureFormat::Rgba8snorm:
    case dom::GPUTextureFormat::Rgba8uint:
    case dom::GPUTextureFormat::Rgba8sint:
    case dom::GPUTextureFormat::Bgra8unorm:
    case dom::GPUTextureFormat::Bgra8unorm_srgb:
    case dom::GPUTextureFormat::Rgb9e5ufloat:
    case dom::GPUTextureFormat::Rgb10a2unorm:
    case dom::GPUTextureFormat::Rg11b10float:
      return Some<uint8_t>(4u);
    case dom::GPUTextureFormat::Rg32uint:
    case dom::GPUTextureFormat::Rg32sint:
    case dom::GPUTextureFormat::Rg32float:
    case dom::GPUTextureFormat::Rgba16uint:
    case dom::GPUTextureFormat::Rgba16sint:
    case dom::GPUTextureFormat::Rgba16float:
      return Some<uint8_t>(8u);
    case dom::GPUTextureFormat::Rgba32uint:
    case dom::GPUTextureFormat::Rgba32sint:
    case dom::GPUTextureFormat::Rgba32float:
      return Some<uint8_t>(16u);
    case dom::GPUTextureFormat::Depth32float:
      return Some<uint8_t>(4u);
    case dom::GPUTextureFormat::Bc1_rgba_unorm:
    case dom::GPUTextureFormat::Bc1_rgba_unorm_srgb:
    case dom::GPUTextureFormat::Bc4_r_unorm:
    case dom::GPUTextureFormat::Bc4_r_snorm:
      return Some<uint8_t>(8u);
    case dom::GPUTextureFormat::Bc2_rgba_unorm:
    case dom::GPUTextureFormat::Bc2_rgba_unorm_srgb:
    case dom::GPUTextureFormat::Bc3_rgba_unorm:
    case dom::GPUTextureFormat::Bc3_rgba_unorm_srgb:
    case dom::GPUTextureFormat::Bc5_rg_unorm:
    case dom::GPUTextureFormat::Bc5_rg_snorm:
    case dom::GPUTextureFormat::Bc6h_rgb_ufloat:
    case dom::GPUTextureFormat::Bc6h_rgb_float:
    case dom::GPUTextureFormat::Bc7_rgba_unorm:
    case dom::GPUTextureFormat::Bc7_rgba_unorm_srgb:
      return Some<uint8_t>(16u);
    case dom::GPUTextureFormat::Depth24plus:
    case dom::GPUTextureFormat::Depth24plus_stencil8:
    case dom::GPUTextureFormat::Depth32float_stencil8:
    case dom::GPUTextureFormat::EndGuard_:
      return Nothing();
  }
  return Nothing();
}

Texture::Texture(Device* const aParent, RawId aId,
                 const dom::GPUTextureDescriptor& aDesc)
    : ChildOf(aParent),
      mId(aId),
      mFormat(aDesc.mFormat),
      mBytesPerBlock(GetBytesPerBlock(aDesc.mFormat)),
      mSize(ConvertExtent(aDesc.mSize)),
      mMipLevelCount(aDesc.mMipLevelCount),
      mSampleCount(aDesc.mSampleCount),
      mDimension(aDesc.mDimension),
      mUsage(aDesc.mUsage) {}

Texture::~Texture() { Cleanup(); }

void Texture::Cleanup() {
  if (mValid && mParent) {
    mValid = false;
    auto bridge = mParent->GetBridge();
    if (bridge && bridge->IsOpen()) {
      bridge->SendTextureDestroy(mId);
    }
  }
}

already_AddRefed<TextureView> Texture::CreateView(
    const dom::GPUTextureViewDescriptor& aDesc) {
  auto bridge = mParent->GetBridge();
  RawId id = 0;
  if (bridge->IsOpen()) {
    id = bridge->TextureCreateView(mId, mParent->mId, aDesc);
  }

  RefPtr<TextureView> view = new TextureView(this, id);
  return view.forget();
}

void Texture::Destroy() {
  // TODO: we don't have to implement it right now, but it's used by the
  // examples
}

}  // namespace mozilla::webgpu