| 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_DrawTypes_DEFINED |
| 9 |
#define skgpu_graphite_DrawTypes_DEFINED |
| 10 |
|
| 11 |
#include "include/private/base/SkAssert.h" |
| 12 |
#include "src/base/SkEnumBitMask.h" |
| 13 |
|
| 14 |
#include <cstddef> |
| 15 |
#include <cstdint> |
| 16 |
|
| 17 |
namespace skgpu::graphite { |
| 18 |
|
| 19 |
/** |
| 20 |
* Geometric primitives used for drawing. |
| 21 |
*/ |
| 22 |
enum class PrimitiveType : uint8_t { |
| 23 |
kTriangles, |
| 24 |
kTriangleStrip, |
| 25 |
kPoints, |
| 26 |
}; |
| 27 |
|
| 28 |
/** |
| 29 |
* Types used to describe format of vertices in buffers. |
| 30 |
*/ |
| 31 |
enum class VertexAttribType : uint8_t { |
| 32 |
kFloat = 0, |
| 33 |
kFloat2, |
| 34 |
kFloat3, |
| 35 |
kFloat4, |
| 36 |
kHalf, |
| 37 |
kHalf2, |
| 38 |
kHalf4, |
| 39 |
|
| 40 |
kInt2, // vector of 2 32-bit ints |
| 41 |
kInt3, // vector of 3 32-bit ints |
| 42 |
kInt4, // vector of 4 32-bit ints |
| 43 |
kUInt2, // vector of 2 32-bit unsigned ints |
| 44 |
|
| 45 |
kByte, // signed byte |
| 46 |
kByte2, // vector of 2 8-bit signed bytes |
| 47 |
kByte4, // vector of 4 8-bit signed bytes |
| 48 |
kUByte, // unsigned byte |
| 49 |
kUByte2, // vector of 2 8-bit unsigned bytes |
| 50 |
kUByte4, // vector of 4 8-bit unsigned bytes |
| 51 |
|
| 52 |
kUByte_norm, // unsigned byte, e.g. coverage, 0 -> 0.0f, 255 -> 1.0f. |
| 53 |
kUByte4_norm, // vector of 4 unsigned bytes, e.g. colors, 0 -> 0.0f, 255 -> 1.0f. |
| 54 |
|
| 55 |
kShort2, // vector of 2 16-bit shorts. |
| 56 |
kShort4, // vector of 4 16-bit shorts. |
| 57 |
|
| 58 |
kUShort2, // vector of 2 unsigned shorts. 0 -> 0, 65535 -> 65535. |
| 59 |
kUShort2_norm, // vector of 2 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f. |
| 60 |
|
| 61 |
kInt, |
| 62 |
kUInt, |
| 63 |
|
| 64 |
kUShort_norm, // unsigned short, e.g. depth, 0 -> 0.0f, 65535 -> 1.0f. |
| 65 |
|
| 66 |
kUShort4_norm, // vector of 4 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f. |
| 67 |
|
| 68 |
kLast = kUShort4_norm |
| 69 |
}; |
| 70 |
static const int kVertexAttribTypeCount = (int)(VertexAttribType::kLast) + 1; |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the size of the attrib type in bytes. |
| 74 |
*/ |
| 75 |
static constexpr inline size_t VertexAttribTypeSize(VertexAttribType type) { |
| 76 |
switch (type) { |
| 77 |
case VertexAttribType::kFloat: |
| 78 |
return sizeof(float); |
| 79 |
case VertexAttribType::kFloat2: |
| 80 |
return 2 * sizeof(float); |
| 81 |
case VertexAttribType::kFloat3: |
| 82 |
return 3 * sizeof(float); |
| 83 |
case VertexAttribType::kFloat4: |
| 84 |
return 4 * sizeof(float); |
| 85 |
case VertexAttribType::kHalf: |
| 86 |
return sizeof(uint16_t); |
| 87 |
case VertexAttribType::kHalf2: |
| 88 |
return 2 * sizeof(uint16_t); |
| 89 |
case VertexAttribType::kHalf4: |
| 90 |
return 4 * sizeof(uint16_t); |
| 91 |
case VertexAttribType::kInt2: |
| 92 |
return 2 * sizeof(int32_t); |
| 93 |
case VertexAttribType::kInt3: |
| 94 |
return 3 * sizeof(int32_t); |
| 95 |
case VertexAttribType::kInt4: |
| 96 |
return 4 * sizeof(int32_t); |
| 97 |
case VertexAttribType::kUInt2: |
| 98 |
return 2 * sizeof(uint32_t); |
| 99 |
case VertexAttribType::kByte: |
| 100 |
return 1 * sizeof(char); |
| 101 |
case VertexAttribType::kByte2: |
| 102 |
return 2 * sizeof(char); |
| 103 |
case VertexAttribType::kByte4: |
| 104 |
return 4 * sizeof(char); |
| 105 |
case VertexAttribType::kUByte: |
| 106 |
return 1 * sizeof(char); |
| 107 |
case VertexAttribType::kUByte2: |
| 108 |
return 2 * sizeof(char); |
| 109 |
case VertexAttribType::kUByte4: |
| 110 |
return 4 * sizeof(char); |
| 111 |
case VertexAttribType::kUByte_norm: |
| 112 |
return 1 * sizeof(char); |
| 113 |
case VertexAttribType::kUByte4_norm: |
| 114 |
return 4 * sizeof(char); |
| 115 |
case VertexAttribType::kShort2: |
| 116 |
return 2 * sizeof(int16_t); |
| 117 |
case VertexAttribType::kShort4: |
| 118 |
return 4 * sizeof(int16_t); |
| 119 |
case VertexAttribType::kUShort2: [[fallthrough]]; |
| 120 |
case VertexAttribType::kUShort2_norm: |
| 121 |
return 2 * sizeof(uint16_t); |
| 122 |
case VertexAttribType::kInt: |
| 123 |
return sizeof(int32_t); |
| 124 |
case VertexAttribType::kUInt: |
| 125 |
return sizeof(uint32_t); |
| 126 |
case VertexAttribType::kUShort_norm: |
| 127 |
return sizeof(uint16_t); |
| 128 |
case VertexAttribType::kUShort4_norm: |
| 129 |
return 4 * sizeof(uint16_t); |
| 130 |
} |
| 131 |
SkUNREACHABLE; |
| 132 |
} |
| 133 |
|
| 134 |
enum class UniformSlot { |
| 135 |
// Slot for paints and render step uniforms |
| 136 |
kCombinedUniforms, |
| 137 |
// Meant for gradient storage buffer. |
| 138 |
kGradient |
| 139 |
}; |
| 140 |
|
| 141 |
/* |
| 142 |
* Depth and stencil settings |
| 143 |
*/ |
| 144 |
enum class CompareOp : uint8_t { |
| 145 |
kAlways, |
| 146 |
kNever, |
| 147 |
kGreater, |
| 148 |
kGEqual, |
| 149 |
kLess, |
| 150 |
kLEqual, |
| 151 |
kEqual, |
| 152 |
kNotEqual |
| 153 |
}; |
| 154 |
static constexpr int kCompareOpCount = 1 + (int)CompareOp::kNotEqual; |
| 155 |
|
| 156 |
enum class StencilOp : uint8_t { |
| 157 |
kKeep, |
| 158 |
kZero, |
| 159 |
kReplace, // Replace stencil value with reference (only the bits enabled in fWriteMask). |
| 160 |
kInvert, |
| 161 |
kIncWrap, |
| 162 |
kDecWrap, |
| 163 |
// NOTE: clamping occurs before the write mask. So if the MSB is zero and masked out, stencil |
| 164 |
// values will still wrap when using clamping ops. |
| 165 |
kIncClamp, |
| 166 |
kDecClamp |
| 167 |
}; |
| 168 |
static constexpr int kStencilOpCount = 1 + (int)StencilOp::kDecClamp; |
| 169 |
|
| 170 |
// These barrier types are not utilized by all backends, but we define them at this level anyhow |
| 171 |
// since it impacts the logic used to group & sort draws. |
| 172 |
enum class BarrierType : uint8_t { |
| 173 |
kNone, |
| 174 |
kAdvancedNoncoherentBlend, |
| 175 |
kReadDstFromInput, |
| 176 |
}; |
| 177 |
|
| 178 |
enum class DstUsage : uint8_t { |
| 179 |
// Prior values of dst pixels will have no effect on final written color |
| 180 |
kNone = 0, |
| 181 |
// Prior values of dst pixels can have an effect on the final written color |
| 182 |
kDependsOnDst = 0b0001, |
| 183 |
// The prior values of dst pixels must be available in the fragment shader |
| 184 |
kDstReadRequired = 0b0010, |
| 185 |
// The final written color uses an advanced blend function, which may require barriers for HW |
| 186 |
kAdvancedBlend = 0b0100, |
| 187 |
// The only reason for kDependsOnDst is because the Renderer has analytic coverage. Switching |
| 188 |
// to a Coverage::kNone Renderer would result in DstUsage::kNone for the same paint. |
| 189 |
kDstOnlyUsedByRenderer = 0b1000, |
| 190 |
}; |
| 191 |
SK_MAKE_BITMASK_OPS(DstUsage) |
| 192 |
|
| 193 |
enum class RenderStateFlags : uint8_t { |
| 194 |
kNone = 0b0000, |
| 195 |
kFixed = 0b0001, // Uses explicit DrawWriter::draw functions |
| 196 |
kAppendVertices = 0b0010, // Appends vertices |
| 197 |
kAppendInstances = 0b0100, // Appends instances with static vertex count |
| 198 |
kAppendDynamicInstances = 0b1000, // Appends instances with a flexible vertex count |
| 199 |
}; |
| 200 |
SK_MAKE_BITMASK_OPS(RenderStateFlags) |
| 201 |
|
| 202 |
struct DepthStencilSettings { |
| 203 |
// Per-face settings for stencil |
| 204 |
struct Face { |
| 205 |
constexpr Face() = default; |
| 206 |
constexpr Face(StencilOp stencilFail, |
| 207 |
StencilOp depthFail, |
| 208 |
StencilOp dsPass, |
| 209 |
CompareOp compare, |
| 210 |
uint32_t readMask, |
| 211 |
uint32_t writeMask) |
| 212 |
: fStencilFailOp(stencilFail) |
| 213 |
, fDepthFailOp(depthFail) |
| 214 |
, fDepthStencilPassOp(dsPass) |
| 215 |
, fCompareOp(compare) |
| 216 |
, fReadMask(readMask) |
| 217 |
, fWriteMask(writeMask) {} |
| 218 |
|
| 219 |
StencilOp fStencilFailOp = StencilOp::kKeep; |
| 220 |
StencilOp fDepthFailOp = StencilOp::kKeep; |
| 221 |
StencilOp fDepthStencilPassOp = StencilOp::kKeep; |
| 222 |
CompareOp fCompareOp = CompareOp::kAlways; |
| 223 |
uint32_t fReadMask = 0xffffffff; |
| 224 |
uint32_t fWriteMask = 0xffffffff; |
| 225 |
|
| 226 |
constexpr bool operator==(const Face& that) const { |
| 227 |
return this->fStencilFailOp == that.fStencilFailOp && |
| 228 |
this->fDepthFailOp == that.fDepthFailOp && |
| 229 |
this->fDepthStencilPassOp == that.fDepthStencilPassOp && |
| 230 |
this->fCompareOp == that.fCompareOp && |
| 231 |
this->fReadMask == that.fReadMask && |
| 232 |
this->fWriteMask == that.fWriteMask; |
| 233 |
} |
| 234 |
}; |
| 235 |
|
| 236 |
constexpr DepthStencilSettings() = default; |
| 237 |
constexpr DepthStencilSettings(Face front, |
| 238 |
Face back, |
| 239 |
uint32_t stencilRef, |
| 240 |
bool stencilTest, |
| 241 |
CompareOp depthCompare, |
| 242 |
bool depthTest, |
| 243 |
bool depthWrite) |
| 244 |
: fFrontStencil(front) |
| 245 |
, fBackStencil(back) |
| 246 |
, fStencilReferenceValue(stencilRef) |
| 247 |
, fDepthCompareOp(depthCompare) |
| 248 |
, fStencilTestEnabled(stencilTest) |
| 249 |
, fDepthTestEnabled(depthTest) |
| 250 |
, fDepthWriteEnabled(depthWrite) {} |
| 251 |
|
| 252 |
constexpr bool operator==(const DepthStencilSettings& that) const { |
| 253 |
return this->fFrontStencil == that.fFrontStencil && |
| 254 |
this->fBackStencil == that.fBackStencil && |
| 255 |
this->fStencilReferenceValue == that.fStencilReferenceValue && |
| 256 |
this->fDepthCompareOp == that.fDepthCompareOp && |
| 257 |
this->fStencilTestEnabled == that.fStencilTestEnabled && |
| 258 |
this->fDepthTestEnabled == that.fDepthTestEnabled && |
| 259 |
this->fDepthWriteEnabled == that.fDepthWriteEnabled; |
| 260 |
} |
| 261 |
|
| 262 |
Face fFrontStencil; |
| 263 |
Face fBackStencil; |
| 264 |
uint32_t fStencilReferenceValue = 0; |
| 265 |
CompareOp fDepthCompareOp = CompareOp::kAlways; |
| 266 |
bool fStencilTestEnabled = false; |
| 267 |
bool fDepthTestEnabled = false; |
| 268 |
bool fDepthWriteEnabled = false; |
| 269 |
}; |
| 270 |
|
| 271 |
} // namespace skgpu::graphite |
| 272 |
|
| 273 |
#endif // skgpu_graphite_DrawTypes_DEFINED |