| 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 |
|
| 11 |
#include "include/core/SkSamplingOptions.h" |
| 12 |
#include "include/core/SkSpan.h" |
| 13 |
#include "include/core/SkTileMode.h" |
| 14 |
#include "include/gpu/graphite/GraphiteTypes.h" |
| 15 |
#include "include/private/base/SkTo.h" |
| 16 |
#include "src/base/SkEnumBitMask.h" |
| 17 |
#include "src/base/SkMathPriv.h" |
| 18 |
|
| 19 |
namespace skgpu::graphite { |
| 20 |
|
| 21 |
class Buffer; |
| 22 |
|
| 23 |
// This declaration of the DepthStencilFlags' SkEnumBitMask ops is here bc, internally, we use |
| 24 |
// DepthStencilFlags as bit fields but, externally (i.e., from the GraphiteTypes view), we want |
| 25 |
// it to appear as just an enum class. |
| 26 |
SK_MAKE_BITMASK_OPS(DepthStencilFlags) |
| 27 |
// The same goes for SampleCount |
| 28 |
SK_MAKE_BITMASK_OPS(SampleCount) |
| 29 |
|
| 30 |
/** |
| 31 |
* There are only a few possible valid sample counts (1, 2, 4, 8, 16). So we can key on those 5 |
| 32 |
* options instead of the actual sample value. The resulting key value only requires 3 bits of space |
| 33 |
*/ |
| 34 |
static constexpr uint32_t SamplesToKey(SampleCount numSamples) { |
| 35 |
switch (numSamples) { |
| 36 |
case SampleCount::k1: |
| 37 |
return 0; |
| 38 |
case SampleCount::k2: |
| 39 |
return 1; |
| 40 |
case SampleCount::k4: |
| 41 |
return 2; |
| 42 |
case SampleCount::k8: |
| 43 |
return 3; |
| 44 |
case SampleCount::k16: |
| 45 |
return 4; |
| 46 |
} |
| 47 |
SkUNREACHABLE; |
| 48 |
} |
| 49 |
static constexpr SampleCount KeyToSamples(uint32_t keyBits) { |
| 50 |
SkASSERT(keyBits <= 4); |
| 51 |
return static_cast<SampleCount>(1 << keyBits); |
| 52 |
} |
| 53 |
static constexpr int kNumSampleKeyBits = 3; |
| 54 |
|
| 55 |
/** |
| 56 |
* The strategy that a renderpass and/or pipeline use to access the current dst pixel when blending. |
| 57 |
*/ |
| 58 |
enum class DstReadStrategy : uint8_t { |
| 59 |
kNoneRequired, |
| 60 |
kTextureCopy, |
| 61 |
kTextureSample, // TODO(b/238756862): To be used once direct texture sampling is implemented |
| 62 |
kReadFromInput, |
| 63 |
kFramebufferFetch, |
| 64 |
|
| 65 |
kLast = kFramebufferFetch |
| 66 |
}; |
| 67 |
inline static constexpr int kDstReadStrategyCount = (int)(DstReadStrategy::kLast) + 1; |
| 68 |
|
| 69 |
/** |
| 70 |
* This enum is used to specify the load operation to be used when a RenderPass begins execution. |
| 71 |
*/ |
| 72 |
enum class LoadOp : uint8_t { |
| 73 |
kLoad, |
| 74 |
kClear, |
| 75 |
kDiscard, |
| 76 |
|
| 77 |
kLast = kDiscard |
| 78 |
}; |
| 79 |
inline static constexpr int kLoadOpCount = (int)(LoadOp::kLast) + 1; |
| 80 |
|
| 81 |
/** |
| 82 |
* This enum is used to specify the store operation to be used when a RenderPass ends execution. |
| 83 |
*/ |
| 84 |
enum class StoreOp : uint8_t { |
| 85 |
kStore, |
| 86 |
kDiscard, |
| 87 |
|
| 88 |
kLast = kDiscard |
| 89 |
}; |
| 90 |
inline static constexpr int kStoreOpCount = (int)(StoreOp::kLast) + 1; |
| 91 |
|
| 92 |
/** |
| 93 |
* What a GPU buffer will be used for |
| 94 |
*/ |
| 95 |
enum class BufferType : int { |
| 96 |
kVertex, |
| 97 |
kIndex, |
| 98 |
kXferCpuToGpu, |
| 99 |
kXferGpuToCpu, |
| 100 |
kUniform, |
| 101 |
kStorage, |
| 102 |
kQuery, |
| 103 |
|
| 104 |
// GPU-only buffer types |
| 105 |
kIndirect, |
| 106 |
kVertexStorage, |
| 107 |
kIndexStorage, |
| 108 |
|
| 109 |
kLast = kIndexStorage, |
| 110 |
}; |
| 111 |
static const int kBufferTypeCount = static_cast<int>(BufferType::kLast) + 1; |
| 112 |
|
| 113 |
/** |
| 114 |
* Data layout requirements on host-shareable buffer contents. |
| 115 |
*/ |
| 116 |
enum class Layout : uint8_t { |
| 117 |
kInvalid = 0, |
| 118 |
kStd140, |
| 119 |
kStd140_F16, |
| 120 |
kStd430, |
| 121 |
kStd430_F16, |
| 122 |
kMetal, |
| 123 |
}; |
| 124 |
|
| 125 |
static constexpr const char* LayoutString(Layout layout) { |
| 126 |
switch(layout) { |
| 127 |
case Layout::kStd140: return "std140"; |
| 128 |
case Layout::kStd140_F16: return "std140-f16"; |
| 129 |
case Layout::kStd430: return "std430"; |
| 130 |
case Layout::kStd430_F16: return "std430-f16"; |
| 131 |
case Layout::kMetal: return "metal"; |
| 132 |
case Layout::kInvalid: return "invalid"; |
| 133 |
} |
| 134 |
SkUNREACHABLE; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Indicates the intended access pattern over resource memory. This is used to select the most |
| 139 |
* efficient memory type during resource creation based on the capabilities of the platform. |
| 140 |
* |
| 141 |
* This is only a hint and the actual memory type will be determined based on the resource type and |
| 142 |
* backend capabilities. |
| 143 |
*/ |
| 144 |
enum class AccessPattern : uint8_t { |
| 145 |
// GPU-only memory does not need to support reads/writes from the CPU. GPU-private memory will |
| 146 |
// be preferred if the backend supports an efficient private memory type. |
| 147 |
kGpuOnly, |
| 148 |
|
| 149 |
// The resource needs to be CPU visible, e.g. for read-back or as a copy/upload source. |
| 150 |
kHostVisible, |
| 151 |
|
| 152 |
// Use to debug GPU only buffers. |
| 153 |
kGpuOnlyCopySrc, |
| 154 |
}; |
| 155 |
|
| 156 |
/** |
| 157 |
* Determines whether the contents of a GPU buffer sub-allocation gets cleared to 0 before being |
| 158 |
* used in a GPU command submission. |
| 159 |
*/ |
| 160 |
enum class ClearBuffer : bool { |
| 161 |
kNo = false, |
| 162 |
kYes = true, |
| 163 |
}; |
| 164 |
|
| 165 |
/** |
| 166 |
* Must the contents of the Resource be preserved af a render pass or can a more efficient |
| 167 |
* representation be chosen when supported by hardware. |
| 168 |
*/ |
| 169 |
enum class Discardable : bool { |
| 170 |
kNo = false, |
| 171 |
kYes = true |
| 172 |
}; |
| 173 |
|
| 174 |
enum class Ownership : uint8_t { |
| 175 |
kOwned, |
| 176 |
kWrapped, |
| 177 |
}; |
| 178 |
|
| 179 |
/** Uniquely identifies the type of resource that is cached with a GraphiteResourceKey. */ |
| 180 |
using ResourceType = uint32_t; |
| 181 |
|
| 182 |
/** |
| 183 |
* Can the resource be held by multiple users at the same time? |
| 184 |
* For example, stencil buffers, pipelines, etc. |
| 185 |
*/ |
| 186 |
enum class Shareable : uint8_t { |
| 187 |
kNo, // The resource is visible in the ResourceCache once all its usage refs are dropped |
| 188 |
kScratch, // The resource is visible to other Recorders, but acts like kNo within a Recording |
| 189 |
kYes, // The resource is always visible in the ResourceCache |
| 190 |
}; |
| 191 |
|
| 192 |
/* |
| 193 |
* Struct that can be passed into bind buffer calls on the CommandBuffer. The ownership of the |
| 194 |
* buffer and its usage in command submission must be tracked by the caller (e.g. as with |
| 195 |
* buffers created by DrawBufferManager). |
| 196 |
*/ |
| 197 |
struct BindBufferInfo { |
| 198 |
const Buffer* fBuffer = nullptr; |
| 199 |
uint32_t fOffset = 0; |
| 200 |
uint32_t fSize = 0; |
| 201 |
|
| 202 |
explicit operator bool() const { return SkToBool(fBuffer); } |
| 203 |
|
| 204 |
bool operator==(const BindBufferInfo& o) const { |
| 205 |
return fBuffer == o.fBuffer && (!fBuffer || (fOffset == o.fOffset && fSize == o.fSize)); |
| 206 |
} |
| 207 |
bool operator!=(const BindBufferInfo& o) const { return !(*this == o); } |
| 208 |
}; |
| 209 |
|
| 210 |
struct ImmutableSamplerInfo { |
| 211 |
// If the sampler requires YCbCr conversion, backends can place that information here. |
| 212 |
// In order to fit within SamplerDesc's uint32 desc field, backends can only utilize up to |
| 213 |
// kMaxNumConversionInfoBits bits. |
| 214 |
uint32_t fNonFormatYcbcrConversionInfo = 0; |
| 215 |
// fFormat represents known OR external format numerical representation. |
| 216 |
uint64_t fFormat = 0; |
| 217 |
}; |
| 218 |
|
| 219 |
/** |
| 220 |
* Struct used to describe how a Texture/TextureProxy/TextureProxyView is sampled. |
| 221 |
*/ |
| 222 |
struct SamplerDesc { |
| 223 |
static_assert(kSkTileModeCount <= 4 && kSkFilterModeCount <= 2 && kSkMipmapModeCount <= 4); |
| 224 |
|
| 225 |
constexpr SamplerDesc(const SkSamplingOptions& samplingOptions, SkTileMode tileMode) |
| 226 |
: SamplerDesc(samplingOptions, {tileMode, tileMode}) {} |
| 227 |
|
| 228 |
constexpr SamplerDesc(const SkSamplingOptions& samplingOptions, |
| 229 |
const std::pair<SkTileMode, SkTileMode> tileModes, |
| 230 |
const ImmutableSamplerInfo info = {}) |
| 231 |
: fDesc((static_cast<int>(tileModes.first) << kTileModeXShift ) | |
| 232 |
(static_cast<int>(tileModes.second) << kTileModeYShift ) | |
| 233 |
(static_cast<int>(samplingOptions.filter) << kFilterModeShift ) | |
| 234 |
(static_cast<int>(samplingOptions.mipmap) << kMipmapModeShift ) | |
| 235 |
(info.fNonFormatYcbcrConversionInfo << kImmutableSamplerInfoShift) ) |
| 236 |
, fFormat(info.fFormat) |
| 237 |
, fExternalFormatMostSignificantBits(info.fFormat >> 32) { |
| 238 |
|
| 239 |
// Cubic sampling is handled in a shader, with the actual texture sampled by with NN, |
| 240 |
// but that is what a cubic SkSamplingOptions is set to if you ignore 'cubic', which let's |
| 241 |
// us simplify how we construct SamplerDec's from the options passed to high-level draws. |
| 242 |
SkASSERT(!samplingOptions.useCubic || (samplingOptions.filter == SkFilterMode::kNearest && |
| 243 |
samplingOptions.mipmap == SkMipmapMode::kNone)); |
| 244 |
|
| 245 |
// TODO: Add aniso value when used. |
| 246 |
|
| 247 |
// Assert that fYcbcrConversionInfo does not exceed kMaxNumConversionInfoBits such that |
| 248 |
// the conversion information can fit within an uint32. |
| 249 |
SkASSERT(info.fNonFormatYcbcrConversionInfo >> kMaxNumConversionInfoBits == 0); |
| 250 |
} |
| 251 |
constexpr SamplerDesc() = default; |
| 252 |
constexpr SamplerDesc(const SamplerDesc&) = default; |
| 253 |
constexpr SamplerDesc& operator=(const SamplerDesc&) = default; |
| 254 |
|
| 255 |
constexpr SamplerDesc(uint32_t desc, uint32_t format, uint32_t extFormatMSB) |
| 256 |
: fDesc(desc) |
| 257 |
, fFormat(format) |
| 258 |
, fExternalFormatMostSignificantBits(extFormatMSB) {} |
| 259 |
|
| 260 |
bool operator==(const SamplerDesc& o) const { |
| 261 |
return o.fDesc == fDesc && o.fFormat == fFormat && |
| 262 |
o.fExternalFormatMostSignificantBits == fExternalFormatMostSignificantBits; |
| 263 |
} |
| 264 |
|
| 265 |
bool operator!=(const SamplerDesc& o) const { return !(*this == o); } |
| 266 |
|
| 267 |
SkTileMode tileModeX() const { |
| 268 |
return static_cast<SkTileMode>((fDesc >> kTileModeXShift) & 0b11); |
| 269 |
} |
| 270 |
SkTileMode tileModeY() const { |
| 271 |
return static_cast<SkTileMode>((fDesc >> kTileModeYShift) & 0b11); |
| 272 |
} |
| 273 |
SkFilterMode filterMode() const { |
| 274 |
return static_cast<SkFilterMode>((fDesc >> kFilterModeShift) & 0b01); |
| 275 |
} |
| 276 |
SkMipmapMode mipmap() const { |
| 277 |
return static_cast<SkMipmapMode>((fDesc >> kMipmapModeShift) & 0b11); |
| 278 |
} |
| 279 |
uint32_t desc() const { return fDesc; } |
| 280 |
uint32_t format() const { return fFormat; } |
| 281 |
uint32_t externalFormatMSBs() const { return fExternalFormatMostSignificantBits; } |
| 282 |
bool isImmutable() const { return (fDesc >> kImmutableSamplerInfoShift) != 0; } |
| 283 |
bool usesExternalFormat() const { return (fDesc >> kImmutableSamplerInfoShift) & 0b1; } |
| 284 |
|
| 285 |
// NOTE: returns the HW sampling options to use, so a bicubic SkSamplingOptions will become |
| 286 |
// nearest-neighbor sampling in HW. |
| 287 |
SkSamplingOptions samplingOptions() const { |
| 288 |
// TODO: Add support for anisotropic filtering |
| 289 |
SkFilterMode filter = static_cast<SkFilterMode>((fDesc >> kFilterModeShift) & 0b01); |
| 290 |
SkMipmapMode mipmap = static_cast<SkMipmapMode>((fDesc >> kMipmapModeShift) & 0b11); |
| 291 |
return SkSamplingOptions(filter, mipmap); |
| 292 |
} |
| 293 |
|
| 294 |
ImmutableSamplerInfo immutableSamplerInfo() const { |
| 295 |
return {this->desc() >> kImmutableSamplerInfoShift, |
| 296 |
((uint64_t) this->externalFormatMSBs() << 32) | (uint64_t) this->format()}; |
| 297 |
} |
| 298 |
|
| 299 |
SkSpan<const uint32_t> asSpan() const { |
| 300 |
// Span length depends upon whether the sampler is immutable and if it uses a known format |
| 301 |
return {&fDesc, 1u + this->isImmutable() + this->usesExternalFormat()}; |
| 302 |
} |
| 303 |
|
| 304 |
// These are public such that backends can bitshift data in order to determine whatever |
| 305 |
// sampler qualities they need from fDesc. |
| 306 |
static constexpr int kNumTileModeBits = SkNextLog2_portable(int(SkTileMode::kLastTileMode)+1); |
| 307 |
static constexpr int kNumFilterModeBits = SkNextLog2_portable(int(SkFilterMode::kLast)+1); |
| 308 |
static constexpr int kNumMipmapModeBits = SkNextLog2_portable(int(SkMipmapMode::kLast)+1); |
| 309 |
static constexpr int kMaxNumConversionInfoBits = |
| 310 |
32 - kNumFilterModeBits - kNumMipmapModeBits - kNumTileModeBits; |
| 311 |
|
| 312 |
static constexpr int kTileModeXShift = 0; |
| 313 |
static constexpr int kTileModeYShift = kTileModeXShift + kNumTileModeBits; |
| 314 |
static constexpr int kFilterModeShift = kTileModeYShift + kNumTileModeBits; |
| 315 |
static constexpr int kMipmapModeShift = kFilterModeShift + kNumFilterModeBits; |
| 316 |
static constexpr int kImmutableSamplerInfoShift = kMipmapModeShift + kNumMipmapModeBits; |
| 317 |
|
| 318 |
// Only relevant when using immutable samplers. Otherwise, can be ignored. The number of uint32s |
| 319 |
// required to represent all relevant sampler desc information depends upon whether we are using |
| 320 |
// a known or external format. |
| 321 |
static constexpr int kInt32sNeededKnownFormat = 2; |
| 322 |
static constexpr int kInt32sNeededExternalFormat = 3; |
| 323 |
|
| 324 |
private: |
| 325 |
// Note: The order of these member attributes matters to keep unique object representation |
| 326 |
// such that SkGoodHash can be used to hash SamplerDesc objects. |
| 327 |
uint32_t fDesc = 0; |
| 328 |
|
| 329 |
// Data fields populated by backend Caps which store texture format information (needed for |
| 330 |
// YCbCr sampling). Only relevant when using immutable samplers. Otherwise, can be ignored. |
| 331 |
// Known formats only require a uint32, but external formats can be up to a uint64. We store |
| 332 |
// this as two separate uint32s such that has_unique_object_representation can be true, allowing |
| 333 |
// this structure to be easily hashed using SkGoodHash. So, external formats can be represented |
| 334 |
// with (fExternalFormatMostSignificantBits << 32) | fFormat. |
| 335 |
uint32_t fFormat = 0; |
| 336 |
uint32_t fExternalFormatMostSignificantBits = 0; |
| 337 |
}; |
| 338 |
|
| 339 |
} // namespace skgpu::graphite |
| 340 |
|
| 341 |
#endif // skgpu_graphite_ResourceTypes_DEFINED |