| 1 |
/* |
| 2 |
* Copyright 2023 Google Inc. |
| 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 SkRasterPipelineOpContexts_DEFINED |
| 9 |
#define SkRasterPipelineOpContexts_DEFINED |
| 10 |
|
| 11 |
#include <algorithm> |
| 12 |
#include <cstddef> |
| 13 |
#include <cstdint> |
| 14 |
|
| 15 |
namespace SkSL { class TraceHook; } |
| 16 |
// state shared by stack_checkpoint and stack_rewind |
| 17 |
struct SkRasterPipelineStage; |
| 18 |
enum class SkPerlinNoiseShaderType; |
| 19 |
|
| 20 |
namespace SkRasterPipelineContexts { |
| 21 |
|
| 22 |
// The largest number of pixels we handle at a time. We have a separate value for the largest number |
| 23 |
// of pixels we handle in the highp pipeline. Many of the context structs in this file are only used |
| 24 |
// by stages that have no lowp implementation. They can therefore use the (smaller) highp value to |
| 25 |
// save memory in the arena. |
| 26 |
inline static constexpr int kMaxStride = 16; |
| 27 |
inline static constexpr int kMaxStride_highp = 16; |
| 28 |
|
| 29 |
// How much space to allocate for each MemoryCtx scratch buffer, as part of tail-pixel handling. |
| 30 |
inline static constexpr size_t kMaxScratchPerPatch = |
| 31 |
std::max(kMaxStride_highp * 16, // 16 == largest highp bpp (RGBA_F32) |
| 32 |
kMaxStride * 4); // 4 == largest lowp bpp (RGBA_8888) |
| 33 |
|
| 34 |
// These structs hold the context data for many of the Raster Pipeline ops. |
| 35 |
struct MemoryCtx { |
| 36 |
void* pixels; |
| 37 |
int stride; |
| 38 |
}; |
| 39 |
|
| 40 |
// Raster Pipeline typically processes N (4, 8, 16) pixels at a time, in SIMT fashion. If the |
| 41 |
// number of pixels in a row isn't evenly divisible by N, there will be leftover pixels; this is |
| 42 |
// called the "tail". To avoid reading or writing past the end of any source or destination buffers |
| 43 |
// when we reach the tail: |
| 44 |
// |
| 45 |
// 1) Source buffers have their tail contents copied to a scratch buffer that is at least N wide. |
| 46 |
// In practice, each scratch buffer uses kMaxScratchPerPatch bytes. |
| 47 |
// 2) Each MemoryCtx in the pipeline is patched, such that access to them (at the current scanline |
| 48 |
// and x-offset) will land in the scratch buffer. |
| 49 |
// 3) Pipeline is run as normal (with all memory access happening safely in the scratch buffers). |
| 50 |
// 4) Destination buffers have their tail contents copied back from the scratch buffer. |
| 51 |
// 5) Each MemoryCtx is "un-patched". |
| 52 |
// |
| 53 |
// To do all of this, the pipeline creates a MemoryCtxPatch for each unique MemoryCtx referenced by |
| 54 |
// the pipeline. |
| 55 |
struct MemoryCtxInfo { |
| 56 |
MemoryCtx* context; |
| 57 |
|
| 58 |
int bytesPerPixel; |
| 59 |
bool load; |
| 60 |
bool store; |
| 61 |
}; |
| 62 |
|
| 63 |
struct MemoryCtxPatch { |
| 64 |
std::byte scratch[kMaxScratchPerPatch]; |
| 65 |
|
| 66 |
MemoryCtxInfo info; |
| 67 |
void* backup; // Remembers context->pixels so we can restore it |
| 68 |
}; |
| 69 |
|
| 70 |
struct GatherCtx { |
| 71 |
const void* pixels; |
| 72 |
int stride; |
| 73 |
float width; |
| 74 |
float height; |
| 75 |
float weights[16]; // for bicubic and bicubic_clamp_8888 |
| 76 |
// Controls whether pixel i-1 or i is selected when floating point sample position is exactly i. |
| 77 |
bool roundDownAtInteger = false; |
| 78 |
}; |
| 79 |
|
| 80 |
// State shared by save_xy, accumulate, and bilinear_* / bicubic_*. |
| 81 |
struct SamplerCtx { |
| 82 |
float x[kMaxStride_highp]; |
| 83 |
float y[kMaxStride_highp]; |
| 84 |
float fx[kMaxStride_highp]; |
| 85 |
float fy[kMaxStride_highp]; |
| 86 |
float scalex[kMaxStride_highp]; |
| 87 |
float scaley[kMaxStride_highp]; |
| 88 |
|
| 89 |
// for bicubic_[np][13][xy] |
| 90 |
float weights[16]; |
| 91 |
float wx[4][kMaxStride_highp]; |
| 92 |
float wy[4][kMaxStride_highp]; |
| 93 |
}; |
| 94 |
|
| 95 |
struct TileCtx { |
| 96 |
float scale; |
| 97 |
float invScale; // cache of 1/scale |
| 98 |
// When in the reflection portion of mirror tiling we need to snap the opposite direction |
| 99 |
// at integer sample points than when in the forward direction. This controls which way we bias |
| 100 |
// in the reflection. It should be 1 if GatherCtx::roundDownAtInteger is true |
| 101 |
// and otherwise -1. |
| 102 |
int mirrorBiasDir = -1; |
| 103 |
}; |
| 104 |
|
| 105 |
struct DecalTileCtx { |
| 106 |
uint32_t mask[kMaxStride]; |
| 107 |
float limit_x; |
| 108 |
float limit_y; |
| 109 |
// These control which edge of the interval is included (i.e. closed interval at 0 or at limit). |
| 110 |
// They should be set to limit_x and limit_y if GatherCtx::roundDownAtInteger |
| 111 |
// is true and otherwise zero. |
| 112 |
float inclusiveEdge_x = 0; |
| 113 |
float inclusiveEdge_y = 0; |
| 114 |
}; |
| 115 |
|
| 116 |
struct PerlinNoiseCtx { |
| 117 |
SkPerlinNoiseShaderType noiseType; |
| 118 |
float baseFrequencyX, baseFrequencyY; |
| 119 |
float stitchDataInX, stitchDataInY; |
| 120 |
bool stitching; |
| 121 |
int numOctaves; |
| 122 |
const uint8_t* latticeSelector; // [256 values] |
| 123 |
const uint16_t* noiseData; // [4 channels][256 elements][vector of 2] |
| 124 |
}; |
| 125 |
|
| 126 |
// State used by mipmap_linear_* |
| 127 |
struct MipmapCtx { |
| 128 |
// Original coords, saved before the base level logic |
| 129 |
float x[kMaxStride_highp]; |
| 130 |
float y[kMaxStride_highp]; |
| 131 |
|
| 132 |
// Base level color |
| 133 |
float r[kMaxStride_highp]; |
| 134 |
float g[kMaxStride_highp]; |
| 135 |
float b[kMaxStride_highp]; |
| 136 |
float a[kMaxStride_highp]; |
| 137 |
|
| 138 |
// Scale factors to transform base level coords to lower level coords |
| 139 |
float scaleX; |
| 140 |
float scaleY; |
| 141 |
|
| 142 |
float lowerWeight; |
| 143 |
}; |
| 144 |
|
| 145 |
struct CoordClampCtx { |
| 146 |
float min_x, min_y; |
| 147 |
float max_x, max_y; |
| 148 |
}; |
| 149 |
|
| 150 |
struct CallbackCtx { |
| 151 |
void (*fn)(CallbackCtx* self, int active_pixels /*<= kMaxStride_highp*/); |
| 152 |
|
| 153 |
// When called, fn() will have our active pixels available in rgba. |
| 154 |
// When fn() returns, the pipeline will read back those active pixels from read_from. |
| 155 |
float rgba[4 * kMaxStride_highp]; |
| 156 |
float* read_from = rgba; |
| 157 |
}; |
| 158 |
|
| 159 |
struct RewindCtx { |
| 160 |
float r[kMaxStride_highp]; |
| 161 |
float g[kMaxStride_highp]; |
| 162 |
float b[kMaxStride_highp]; |
| 163 |
float a[kMaxStride_highp]; |
| 164 |
float dr[kMaxStride_highp]; |
| 165 |
float dg[kMaxStride_highp]; |
| 166 |
float db[kMaxStride_highp]; |
| 167 |
float da[kMaxStride_highp]; |
| 168 |
std::byte* base; |
| 169 |
SkRasterPipelineStage* stage; |
| 170 |
}; |
| 171 |
|
| 172 |
constexpr size_t kRGBAChannels = 4; |
| 173 |
|
| 174 |
struct GradientCtx { |
| 175 |
size_t stopCount; |
| 176 |
float* factors[kRGBAChannels]; |
| 177 |
float* biases[kRGBAChannels]; |
| 178 |
float* ts; |
| 179 |
}; |
| 180 |
|
| 181 |
struct EvenlySpaced2StopGradientCtx { |
| 182 |
float factor[kRGBAChannels]; |
| 183 |
float bias[kRGBAChannels]; |
| 184 |
}; |
| 185 |
|
| 186 |
struct Conical2PtCtx { |
| 187 |
uint32_t fMask[kMaxStride_highp]; |
| 188 |
float fP0, |
| 189 |
fP1; |
| 190 |
}; |
| 191 |
|
| 192 |
struct UniformColorCtx { |
| 193 |
float r,g,b,a; |
| 194 |
uint16_t rgba[4]; // [0,255] in a 16-bit lane. |
| 195 |
}; |
| 196 |
|
| 197 |
struct EmbossCtx { |
| 198 |
MemoryCtx mul, add; |
| 199 |
}; |
| 200 |
|
| 201 |
struct TablesCtx { |
| 202 |
const uint8_t *r, *g, *b, *a; |
| 203 |
}; |
| 204 |
|
| 205 |
using SkRPOffset = uint32_t; |
| 206 |
|
| 207 |
struct InitLaneMasksCtx { |
| 208 |
uint8_t* tail; |
| 209 |
}; |
| 210 |
|
| 211 |
struct ConstantCtx { |
| 212 |
int32_t value; |
| 213 |
SkRPOffset dst; |
| 214 |
}; |
| 215 |
|
| 216 |
struct UniformCtx { |
| 217 |
int32_t* dst; |
| 218 |
const int32_t* src; |
| 219 |
}; |
| 220 |
|
| 221 |
struct BinaryOpCtx { |
| 222 |
SkRPOffset dst; |
| 223 |
SkRPOffset src; |
| 224 |
}; |
| 225 |
|
| 226 |
struct TernaryOpCtx { |
| 227 |
SkRPOffset dst; |
| 228 |
SkRPOffset delta; |
| 229 |
}; |
| 230 |
|
| 231 |
struct MatrixMultiplyCtx { |
| 232 |
SkRPOffset dst; |
| 233 |
uint8_t leftColumns, leftRows, rightColumns, rightRows; |
| 234 |
}; |
| 235 |
|
| 236 |
struct SwizzleCtx { |
| 237 |
// If we are processing more than 16 pixels at a time, an 8-bit offset won't be sufficient and |
| 238 |
// `offsets` will need to use uint16_t (or dial down the premultiplication). |
| 239 |
static_assert(kMaxStride_highp <= 16); |
| 240 |
|
| 241 |
SkRPOffset dst; |
| 242 |
uint8_t offsets[4]; // values must be byte offsets (4 * highp-stride * component-index) |
| 243 |
}; |
| 244 |
|
| 245 |
struct ShuffleCtx { |
| 246 |
int32_t* ptr; |
| 247 |
int count; |
| 248 |
uint16_t offsets[16]; // values must be byte offsets (4 * highp-stride * component-index) |
| 249 |
}; |
| 250 |
|
| 251 |
struct SwizzleCopyCtx { |
| 252 |
int32_t* dst; |
| 253 |
const int32_t* src; // src values must _not_ overlap dst values |
| 254 |
uint16_t offsets[4]; // values must be byte offsets (4 * highp-stride * component-index) |
| 255 |
}; |
| 256 |
|
| 257 |
struct CopyIndirectCtx { |
| 258 |
int32_t* dst; |
| 259 |
const int32_t* src; |
| 260 |
const uint32_t *indirectOffset; // this applies to `src` or `dst` based on the op |
| 261 |
uint32_t indirectLimit; // the indirect offset is clamped to this upper bound |
| 262 |
uint32_t slots; // the number of slots to copy |
| 263 |
}; |
| 264 |
|
| 265 |
struct SwizzleCopyIndirectCtx : public CopyIndirectCtx { |
| 266 |
uint16_t offsets[4]; // values must be byte offsets (4 * highp-stride * component-index) |
| 267 |
}; |
| 268 |
|
| 269 |
struct BranchCtx { |
| 270 |
int offset; // contains the label ID during compilation, and the program offset when compiled |
| 271 |
}; |
| 272 |
|
| 273 |
struct BranchIfAllLanesActiveCtx : public BranchCtx { |
| 274 |
uint8_t* tail = nullptr; // lanes past the tail are _never_ active, so we need to exclude them |
| 275 |
}; |
| 276 |
|
| 277 |
struct BranchIfEqualCtx : public BranchCtx { |
| 278 |
int value; |
| 279 |
const int* ptr; |
| 280 |
}; |
| 281 |
|
| 282 |
struct CaseOpCtx { |
| 283 |
int expectedValue; |
| 284 |
SkRPOffset offset; // points to a pair of adjacent I32s: {I32 actualValue, I32 defaultMask} |
| 285 |
}; |
| 286 |
|
| 287 |
struct TraceFuncCtx { |
| 288 |
const int* traceMask; |
| 289 |
SkSL::TraceHook* traceHook; |
| 290 |
int funcIdx; |
| 291 |
}; |
| 292 |
|
| 293 |
struct TraceScopeCtx { |
| 294 |
const int* traceMask; |
| 295 |
SkSL::TraceHook* traceHook; |
| 296 |
int delta; |
| 297 |
}; |
| 298 |
|
| 299 |
struct TraceLineCtx { |
| 300 |
const int* traceMask; |
| 301 |
SkSL::TraceHook* traceHook; |
| 302 |
int lineNumber; |
| 303 |
}; |
| 304 |
|
| 305 |
struct TraceVarCtx { |
| 306 |
const int* traceMask; |
| 307 |
SkSL::TraceHook* traceHook; |
| 308 |
int slotIdx, numSlots; |
| 309 |
const int* data; |
| 310 |
const uint32_t *indirectOffset; // can be null; if set, an offset applied to `data` |
| 311 |
uint32_t indirectLimit; // the indirect offset is clamped to this upper bound |
| 312 |
}; |
| 313 |
|
| 314 |
} // namespace SkRasterPipelineContexts |
| 315 |
|
| 316 |
#endif // SkRasterPipelineOpContexts_DEFINED |