Flutter Engine
The Flutter Engine
ResourceTypes.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef skgpu_graphite_ResourceTypes_DEFINED
9#define skgpu_graphite_ResourceTypes_DEFINED
10
12#include "include/core/SkSpan.h"
17#include "src/base/SkMathPriv.h"
18
19namespace skgpu::graphite {
20
21class Buffer;
22
23enum class DepthStencilFlags : int {
24 kNone = 0b000,
25 kDepth = 0b001,
26 kStencil = 0b010,
28};
30
31/**
32 * This enum is used to specify the load operation to be used when a RenderPass begins execution
33 */
34enum class LoadOp : uint8_t {
35 kLoad,
36 kClear,
38
40};
41inline static constexpr int kLoadOpCount = (int)(LoadOp::kLast) + 1;
42
43/**
44 * This enum is used to specify the store operation to be used when a RenderPass ends execution.
45 */
46enum class StoreOp : uint8_t {
47 kStore,
49
51};
52inline static constexpr int kStoreOpCount = (int)(StoreOp::kLast) + 1;
53
54/**
55 * What a GPU buffer will be used for
56 */
57enum class BufferType : int {
58 kVertex,
59 kIndex,
64
65 // GPU-only buffer types
69
71};
72static const int kBufferTypeCount = static_cast<int>(BufferType::kLast) + 1;
73
74/**
75 * Data layout requirements on host-shareable buffer contents.
76 */
77enum class Layout {
78 kInvalid = 0,
79 kStd140,
80 kStd430,
81 kMetal,
82};
83
84static constexpr const char* LayoutString(Layout layout) {
85 switch(layout) {
86 case Layout::kStd140: return "std140";
87 case Layout::kStd430: return "std430";
88 case Layout::kMetal: return "metal";
89 case Layout::kInvalid: return "invalid";
90 }
92}
93
94/**
95 * Indicates the intended access pattern over resource memory. This is used to select the most
96 * efficient memory type during resource creation based on the capabilities of the platform.
97 *
98 * This is only a hint and the actual memory type will be determined based on the resource type and
99 * backend capabilities.
100 */
101enum class AccessPattern : int {
102 // GPU-only memory does not need to support reads/writes from the CPU. GPU-private memory will
103 // be preferred if the backend supports an efficient private memory type.
104 kGpuOnly,
105
106 // The resource needs to be CPU visible, e.g. for read-back or as a copy/upload source.
107 kHostVisible,
108};
109
110/**
111 * Determines whether the contents of a GPU buffer sub-allocation gets cleared to 0 before being
112 * used in a GPU command submission.
113 */
114enum class ClearBuffer : bool {
115 kNo = false,
116 kYes = true,
117};
118
119/**
120 * Must the contents of the Resource be preserved af a render pass or can a more efficient
121 * representation be chosen when supported by hardware.
122 */
123enum class Discardable : bool {
124 kNo = false,
125 kYes = true
126};
127
128enum class Ownership {
129 kOwned,
130 kWrapped,
131};
132
133/** Uniquely identifies the type of resource that is cached with a GraphiteResourceKey. */
134using ResourceType = uint32_t;
135
136/**
137 * Can the resource be held by multiple users at the same time?
138 * For example, stencil buffers, pipelines, etc.
139 */
140enum class Shareable : bool {
141 kNo = false,
142 kYes = true,
143};
144
145/**
146 * This enum is used to notify the ResourceCache which type of ref just dropped to zero on a
147 * Resource.
148 */
149enum class LastRemovedRef {
150 kUsage,
152 kCache,
153};
154
155/*
156 * Struct that can be passed into bind buffer calls on the CommandBuffer. The ownership of the
157 * buffer and its usage in command submission must be tracked by the caller (e.g. as with
158 * buffers created by DrawBufferManager).
159 */
161 const Buffer* fBuffer = nullptr;
162 size_t fOffset = 0;
163
164 operator bool() const { return SkToBool(fBuffer); }
165
166 bool operator==(const BindBufferInfo& o) const {
167 return fBuffer == o.fBuffer && (!fBuffer || fOffset == o.fOffset);
168 }
169 bool operator!=(const BindBufferInfo& o) const { return !(*this == o); }
170};
171
172/*
173 * Struct that can be passed into bind uniform buffer calls on the CommandBuffer.
174 * It is similar to BindBufferInfo with additional fBindingSize member.
175 */
177 // TODO(b/308933713): Add size to BindBufferInfo instead
178 uint32_t fBindingSize = 0;
179
180 bool operator==(const BindUniformBufferInfo& o) const {
182 }
183 bool operator!=(const BindUniformBufferInfo& o) const { return !(*this == o); }
184};
185
186/**
187 * Represents a buffer region that should be cleared to 0. A ClearBuffersTask does not take an
188 * owning reference to the buffer it clears. A higher layer is responsible for managing the lifetime
189 * and usage refs of the buffer.
190 */
192 const Buffer* fBuffer = nullptr;
193 size_t fOffset = 0;
194 size_t fSize = 0;
195
196 operator bool() const { return SkToBool(fBuffer); }
197};
198
200 // If the sampler requires YCbCr conversion, backends can place that information here.
201 // In order to fit within SamplerDesc's uint32 desc field, backends can only utilize up to
202 // kMaxNumConversionInfoBits bits.
204 // fFormat represents known OR external format numerical representation.
205 uint64_t fFormat = 0;
206};
207
208
209/**
210 * Struct used to describe how a Texture/TextureProxy/TextureProxyView is sampled.
211 */
213 static_assert(kSkTileModeCount <= 4 && kSkFilterModeCount <= 2 && kSkMipmapModeCount <= 4);
214
216 const SkTileMode tileModes[2],
217 const ImmutableSamplerInfo info = {})
218 : fDesc((static_cast<int>(tileModes[0]) << kTileModeXShift ) |
219 (static_cast<int>(tileModes[1]) << kTileModeYShift ) |
220 (static_cast<int>(samplingOptions.filter) << kFilterModeShift ) |
221 (static_cast<int>(samplingOptions.mipmap) << kMipmapModeShift ) |
222 (info.fNonFormatYcbcrConversionInfo << kImmutableSamplerInfoShift) )
223 , fFormat(info.fFormat)
224 , fExternalFormatMostSignificantBits(info.fFormat >> 32) {
225
226 // Cubic sampling is handled in a shader, with the actual texture sampled by with NN,
227 // but that is what a cubic SkSamplingOptions is set to if you ignore 'cubic', which let's
228 // us simplify how we construct SamplerDec's from the options passed to high-level draws.
231
232 // TODO: Add aniso value when used.
233
234 // Assert that fYcbcrConversionInfo does not exceed kMaxNumConversionInfoBits such that
235 // the conversion information can fit within an uint32.
236 SkASSERT(info.fNonFormatYcbcrConversionInfo >> kMaxNumConversionInfoBits == 0);
237 }
238 SamplerDesc() = default;
239 SamplerDesc(const SamplerDesc&) = default;
240
241 bool operator==(const SamplerDesc& o) const {
242 return o.fDesc == fDesc && o.fFormat == fFormat &&
243 o.fExternalFormatMostSignificantBits == fExternalFormatMostSignificantBits;
244 }
245
246 bool operator!=(const SamplerDesc& o) const { return !(*this == o); }
247
248 SkTileMode tileModeX() const { return static_cast<SkTileMode>((fDesc >> 0) & 0b11); }
249 SkTileMode tileModeY() const { return static_cast<SkTileMode>((fDesc >> 2) & 0b11); }
250 uint32_t desc() const { return fDesc; }
251 uint32_t format() const { return fFormat; }
252 uint32_t externalFormatMSBs() const { return fExternalFormatMostSignificantBits; }
253 bool isImmutable() const { return (fDesc >> kImmutableSamplerInfoShift) != 0; }
254 bool usesExternalFormat() const { return (fDesc >> kImmutableSamplerInfoShift) & 0b1; }
255
256 // NOTE: returns the HW sampling options to use, so a bicubic SkSamplingOptions will become
257 // nearest-neighbor sampling in HW.
259 // TODO: Add support for anisotropic filtering
260 SkFilterMode filter = static_cast<SkFilterMode>((fDesc >> 4) & 0b01);
261 SkMipmapMode mipmap = static_cast<SkMipmapMode>((fDesc >> 5) & 0b11);
262 return SkSamplingOptions(filter, mipmap);
263 }
264
266 // Span length depends upon whether the sampler is immutable and if it uses a known format
267 return {&fDesc, 1 + this->isImmutable() + this->usesExternalFormat()};
268 }
269
270 // These are public such that backends can bitshift data in order to determine whatever
271 // sampler qualities they need from fDesc.
275 static constexpr int kMaxNumConversionInfoBits =
276 32 - kNumFilterModeBits - kNumMipmapModeBits - kNumTileModeBits;
277
278 static constexpr int kTileModeXShift = 0;
283
284private:
285 // Note: The order of these member attributes matters to keep unique object representation
286 // such that SkGoodHash can be used to hash SamplerDesc objects.
287 uint32_t fDesc;
288
289 // Data fields populated by backend Caps which store texture format information (needed for
290 // YCbCr sampling). Only relevant when using immutable samplers. Otherwise, can be ignored.
291 // Known formats only require a uint32, but external formats can be up to a uint64. We store
292 // this as two separate uint32s such that has_unique_object_representation can be true, allowing
293 // this structure to be easily hashed using SkGoodHash. So, external formats can be represented
294 // with (fExternalFormatMostSignificantBits << 32) | fFormat.
295 uint32_t fFormat = 0;
296 uint32_t fExternalFormatMostSignificantBits = 0;
297};
298
299}; // namespace skgpu::graphite
300
301#endif // skgpu_graphite_ResourceTypes_DEFINED
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
#define SkUNREACHABLE
Definition: SkAssert.h:135
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kClear
r = 0
@ kInvalid
constexpr int SkNextLog2_portable(uint32_t value)
Definition: SkMathPriv.h:243
@ kYes
Do pre-clip the geometry before applying the (perspective) matrix.
@ kNo
Don't pre-clip the geometry before applying the (perspective) matrix.
SkFilterMode
SkMipmapMode
static constexpr int kSkFilterModeCount
static constexpr int kSkMipmapModeCount
SkTileMode
Definition: SkTileMode.h:13
static constexpr int kSkTileModeCount
Definition: SkTileMode.h:39
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
uint32_t ResourceType
static constexpr const char * LayoutString(Layout layout)
Definition: ResourceTypes.h:84
SK_MAKE_BITMASK_OPS(DawnErrorType)
static constexpr int kLoadOpCount
Definition: ResourceTypes.h:41
static constexpr int kStoreOpCount
Definition: ResourceTypes.h:52
static const int kBufferTypeCount
Definition: ResourceTypes.h:72
SkSamplingOptions(SkFilterMode::kLinear))
const SkFilterMode filter
const SkMipmapMode mipmap
bool operator!=(const BindBufferInfo &o) const
bool operator==(const BindBufferInfo &o) const
bool operator==(const BindUniformBufferInfo &o) const
bool operator!=(const BindUniformBufferInfo &o) const
static constexpr int kTileModeXShift
SkTileMode tileModeX() const
bool operator==(const SamplerDesc &o) const
static constexpr int kTileModeYShift
static constexpr int kMipmapModeShift
SkSamplingOptions samplingOptions() const
static constexpr int kNumTileModeBits
static constexpr int kMaxNumConversionInfoBits
SamplerDesc(const SkSamplingOptions &samplingOptions, const SkTileMode tileModes[2], const ImmutableSamplerInfo info={})
static constexpr int kNumMipmapModeBits
static constexpr int kNumFilterModeBits
static constexpr int kImmutableSamplerInfoShift
SkSpan< const uint32_t > asSpan() const
bool operator!=(const SamplerDesc &o) const
SkTileMode tileModeY() const
static constexpr int kFilterModeShift
uint32_t externalFormatMSBs() const
SamplerDesc(const SamplerDesc &)=default