| 1 |
/* |
| 2 |
* Copyright 2014 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 |
#include "src/gpu/ganesh/GrDefaultGeoProcFactory.h" |
| 9 |
|
| 10 |
#include "include/core/SkMatrix.h" |
| 11 |
#include "include/private/base/SkTo.h" |
| 12 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 13 |
#include "src/base/SkArenaAlloc.h" |
| 14 |
#include "src/base/SkRandom.h" |
| 15 |
#include "src/core/SkSLTypeShared.h" |
| 16 |
#include "src/gpu/KeyBuilder.h" |
| 17 |
#include "src/gpu/ganesh/GrColor.h" |
| 18 |
#include "src/gpu/ganesh/GrGeometryProcessor.h" |
| 19 |
#include "src/gpu/ganesh/GrProcessorUnitTest.h" |
| 20 |
#include "src/gpu/ganesh/GrShaderVar.h" |
| 21 |
#include "src/gpu/ganesh/GrTestUtils.h" |
| 22 |
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h" |
| 23 |
#include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h" |
| 24 |
#include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h" |
| 25 |
#include "src/gpu/ganesh/glsl/GrGLSLVarying.h" |
| 26 |
#include "src/gpu/ganesh/glsl/GrGLSLVertexGeoBuilder.h" |
| 27 |
|
| 28 |
#include <memory> |
| 29 |
|
| 30 |
struct GrShaderCaps; |
| 31 |
|
| 32 |
/* |
| 33 |
* The default Geometry Processor simply takes position and multiplies it by the uniform view |
| 34 |
* matrix. It also leaves coverage untouched. Behind the scenes, we may add per vertex color or |
| 35 |
* local coords. |
| 36 |
*/ |
| 37 |
|
| 38 |
enum GPFlag { |
| 39 |
kColorAttribute_GPFlag = 0x1, |
| 40 |
kColorAttributeIsWide_GPFlag = 0x2, |
| 41 |
kLocalCoordAttribute_GPFlag = 0x4, |
| 42 |
kCoverageAttribute_GPFlag = 0x8, |
| 43 |
kCoverageAttributeTweak_GPFlag = 0x10, |
| 44 |
kCoverageAttributeUnclamped_GPFlag = 0x20, |
| 45 |
}; |
| 46 |
|
| 47 |
class DefaultGeoProc : public GrGeometryProcessor { |
| 48 |
public: |
| 49 |
static GrGeometryProcessor* Make(SkArenaAlloc* arena, |
| 50 |
uint32_t gpTypeFlags, |
| 51 |
const SkPMColor4f& color, |
| 52 |
const SkMatrix& viewMatrix, |
| 53 |
const SkMatrix& localMatrix, |
| 54 |
bool localCoordsWillBeRead, |
| 55 |
uint8_t coverage) { |
| 56 |
return arena->make([&](void* ptr) { |
| 57 |
return new (ptr) DefaultGeoProc(gpTypeFlags, color, viewMatrix, localMatrix, coverage, |
| 58 |
localCoordsWillBeRead); |
| 59 |
}); |
| 60 |
} |
| 61 |
|
| 62 |
const char* name() const override { return "DefaultGeometryProcessor"; } |
| 63 |
|
| 64 |
void addToKey(const GrShaderCaps& caps, skgpu::KeyBuilder* b) const override { |
| 65 |
uint32_t key = fFlags; |
| 66 |
key |= fCoverage == 0xff ? 0x80 : 0; |
| 67 |
key |= fLocalCoordsWillBeRead ? 0x100 : 0; |
| 68 |
|
| 69 |
bool usesLocalMatrix = fLocalCoordsWillBeRead && !fInLocalCoords.isInitialized(); |
| 70 |
key = ProgramImpl::AddMatrixKeys(caps, |
| 71 |
key, |
| 72 |
fViewMatrix, |
| 73 |
usesLocalMatrix ? fLocalMatrix : SkMatrix::I()); |
| 74 |
b->add32(key); |
| 75 |
} |
| 76 |
|
| 77 |
std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override { |
| 78 |
return std::make_unique<Impl>(); |
| 79 |
} |
| 80 |
|
| 81 |
private: |
| 82 |
class Impl : public ProgramImpl { |
| 83 |
public: |
| 84 |
void setData(const GrGLSLProgramDataManager& pdman, |
| 85 |
const GrShaderCaps& shaderCaps, |
| 86 |
const GrGeometryProcessor& geomProc) override { |
| 87 |
const DefaultGeoProc& dgp = geomProc.cast<DefaultGeoProc>(); |
| 88 |
|
| 89 |
SetTransform(pdman, shaderCaps, fViewMatrixUniform, dgp.fViewMatrix, &fViewMatrixPrev); |
| 90 |
SetTransform(pdman, |
| 91 |
shaderCaps, |
| 92 |
fLocalMatrixUniform, |
| 93 |
dgp.fLocalMatrix, |
| 94 |
&fLocalMatrixPrev); |
| 95 |
|
| 96 |
if (!dgp.hasVertexColor() && dgp.fColor != fColor) { |
| 97 |
pdman.set4fv(fColorUniform, 1, dgp.fColor.vec()); |
| 98 |
fColor = dgp.fColor; |
| 99 |
} |
| 100 |
|
| 101 |
if (dgp.fCoverage != fCoverage && !dgp.hasVertexCoverage()) { |
| 102 |
pdman.set1f(fCoverageUniform, GrNormalizeByteToFloat(dgp.fCoverage)); |
| 103 |
fCoverage = dgp.fCoverage; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
private: |
| 108 |
void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 109 |
const DefaultGeoProc& gp = args.fGeomProc.cast<DefaultGeoProc>(); |
| 110 |
GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
| 111 |
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 112 |
GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 113 |
GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 114 |
|
| 115 |
// emit attributes |
| 116 |
varyingHandler->emitAttributes(gp); |
| 117 |
|
| 118 |
bool tweakAlpha = SkToBool(gp.fFlags & kCoverageAttributeTweak_GPFlag); |
| 119 |
bool coverageNeedsSaturate = SkToBool(gp.fFlags & kCoverageAttributeUnclamped_GPFlag); |
| 120 |
SkASSERT(!tweakAlpha || gp.hasVertexCoverage()); |
| 121 |
SkASSERT(!tweakAlpha || !coverageNeedsSaturate); |
| 122 |
|
| 123 |
// Setup pass through color |
| 124 |
fragBuilder->codeAppendf("half4 %s;", args.fOutputColor); |
| 125 |
if (gp.hasVertexColor() || tweakAlpha) { |
| 126 |
GrGLSLVarying varying(SkSLType::kHalf4); |
| 127 |
varyingHandler->addVarying("color", &varying); |
| 128 |
|
| 129 |
// Start with the attribute or with uniform color |
| 130 |
if (gp.hasVertexColor()) { |
| 131 |
vertBuilder->codeAppendf("half4 color = %s;", gp.fInColor.name()); |
| 132 |
} else { |
| 133 |
const char* colorUniformName; |
| 134 |
fColorUniform = uniformHandler->addUniform(nullptr, |
| 135 |
kVertex_GrShaderFlag, |
| 136 |
SkSLType::kHalf4, |
| 137 |
"Color", |
| 138 |
&colorUniformName); |
| 139 |
vertBuilder->codeAppendf("half4 color = %s;", colorUniformName); |
| 140 |
} |
| 141 |
|
| 142 |
// Optionally fold coverage into alpha (color). |
| 143 |
if (tweakAlpha) { |
| 144 |
vertBuilder->codeAppendf("color = color * %s;", gp.fInCoverage.name()); |
| 145 |
} |
| 146 |
vertBuilder->codeAppendf("%s = color;\n", varying.vsOut()); |
| 147 |
fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, varying.fsIn()); |
| 148 |
} else { |
| 149 |
this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, |
| 150 |
&fColorUniform); |
| 151 |
} |
| 152 |
|
| 153 |
// Setup position |
| 154 |
WriteOutputPosition(vertBuilder, |
| 155 |
uniformHandler, |
| 156 |
*args.fShaderCaps, |
| 157 |
gpArgs, |
| 158 |
gp.fInPosition.name(), |
| 159 |
gp.fViewMatrix, |
| 160 |
&fViewMatrixUniform); |
| 161 |
|
| 162 |
// emit transforms using either explicit local coords or positions |
| 163 |
if (gp.fInLocalCoords.isInitialized()) { |
| 164 |
SkASSERT(gp.fLocalMatrix.isIdentity()); |
| 165 |
gpArgs->fLocalCoordVar = gp.fInLocalCoords.asShaderVar(); |
| 166 |
} else if (gp.fLocalCoordsWillBeRead) { |
| 167 |
WriteLocalCoord(vertBuilder, |
| 168 |
uniformHandler, |
| 169 |
*args.fShaderCaps, |
| 170 |
gpArgs, |
| 171 |
gp.fInPosition.asShaderVar(), |
| 172 |
gp.fLocalMatrix, |
| 173 |
&fLocalMatrixUniform); |
| 174 |
} |
| 175 |
|
| 176 |
// Setup coverage as pass through |
| 177 |
if (gp.hasVertexCoverage() && !tweakAlpha) { |
| 178 |
fragBuilder->codeAppendf("half alpha = 1.0;"); |
| 179 |
varyingHandler->addPassThroughAttribute(gp.fInCoverage.asShaderVar(), "alpha"); |
| 180 |
if (coverageNeedsSaturate) { |
| 181 |
fragBuilder->codeAppendf("half4 %s = half4(saturate(alpha));", |
| 182 |
args.fOutputCoverage); |
| 183 |
} else { |
| 184 |
fragBuilder->codeAppendf("half4 %s = half4(alpha);", args.fOutputCoverage); |
| 185 |
} |
| 186 |
} else if (gp.fCoverage == 0xff) { |
| 187 |
fragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage); |
| 188 |
} else { |
| 189 |
const char* fragCoverage; |
| 190 |
fCoverageUniform = uniformHandler->addUniform(nullptr, |
| 191 |
kFragment_GrShaderFlag, |
| 192 |
SkSLType::kHalf, |
| 193 |
"Coverage", |
| 194 |
&fragCoverage); |
| 195 |
fragBuilder->codeAppendf("half4 %s = half4(%s);", |
| 196 |
args.fOutputCoverage, fragCoverage); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
SkMatrix fViewMatrixPrev = SkMatrix::InvalidMatrix(); |
| 201 |
SkMatrix fLocalMatrixPrev = SkMatrix::InvalidMatrix(); |
| 202 |
SkPMColor4f fColor = SK_PMColor4fILLEGAL; |
| 203 |
uint8_t fCoverage = 0xFF; |
| 204 |
|
| 205 |
UniformHandle fViewMatrixUniform; |
| 206 |
UniformHandle fLocalMatrixUniform; |
| 207 |
UniformHandle fColorUniform; |
| 208 |
UniformHandle fCoverageUniform; |
| 209 |
}; |
| 210 |
|
| 211 |
bool hasVertexColor() const { return fInColor.isInitialized(); } |
| 212 |
bool hasVertexCoverage() const { return fInCoverage.isInitialized(); } |
| 213 |
|
| 214 |
DefaultGeoProc(uint32_t gpTypeFlags, |
| 215 |
const SkPMColor4f& color, |
| 216 |
const SkMatrix& viewMatrix, |
| 217 |
const SkMatrix& localMatrix, |
| 218 |
uint8_t coverage, |
| 219 |
bool localCoordsWillBeRead) |
| 220 |
: INHERITED(kDefaultGeoProc_ClassID) |
| 221 |
, fColor(color) |
| 222 |
, fViewMatrix(viewMatrix) |
| 223 |
, fLocalMatrix(localMatrix) |
| 224 |
, fCoverage(coverage) |
| 225 |
, fFlags(gpTypeFlags) |
| 226 |
, fLocalCoordsWillBeRead(localCoordsWillBeRead) { |
| 227 |
fInPosition = {"inPosition", kFloat2_GrVertexAttribType, SkSLType::kFloat2}; |
| 228 |
if (fFlags & kColorAttribute_GPFlag) { |
| 229 |
fInColor = MakeColorAttribute("inColor", |
| 230 |
SkToBool(fFlags & kColorAttributeIsWide_GPFlag)); |
| 231 |
} |
| 232 |
if (fFlags & kLocalCoordAttribute_GPFlag) { |
| 233 |
fInLocalCoords = {"inLocalCoord", kFloat2_GrVertexAttribType, |
| 234 |
SkSLType::kFloat2}; |
| 235 |
} |
| 236 |
if (fFlags & kCoverageAttribute_GPFlag) { |
| 237 |
fInCoverage = {"inCoverage", kFloat_GrVertexAttribType, SkSLType::kHalf}; |
| 238 |
} |
| 239 |
this->setVertexAttributesWithImplicitOffsets(&fInPosition, 4); |
| 240 |
} |
| 241 |
|
| 242 |
Attribute fInPosition; |
| 243 |
Attribute fInColor; |
| 244 |
Attribute fInLocalCoords; |
| 245 |
Attribute fInCoverage; |
| 246 |
SkPMColor4f fColor; |
| 247 |
SkMatrix fViewMatrix; |
| 248 |
SkMatrix fLocalMatrix; |
| 249 |
uint8_t fCoverage; |
| 250 |
uint32_t fFlags; |
| 251 |
bool fLocalCoordsWillBeRead; |
| 252 |
|
| 253 |
GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
| 254 |
|
| 255 |
using INHERITED = GrGeometryProcessor; |
| 256 |
}; |
| 257 |
|
| 258 |
GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DefaultGeoProc) |
| 259 |
|
| 260 |
#if defined(GPU_TEST_UTILS) |
| 261 |
GrGeometryProcessor* DefaultGeoProc::TestCreate(GrProcessorTestData* d) { |
| 262 |
uint32_t flags = 0; |
| 263 |
if (d->fRandom->nextBool()) { |
| 264 |
flags |= kColorAttribute_GPFlag; |
| 265 |
} |
| 266 |
if (d->fRandom->nextBool()) { |
| 267 |
flags |= kColorAttributeIsWide_GPFlag; |
| 268 |
} |
| 269 |
if (d->fRandom->nextBool()) { |
| 270 |
flags |= kCoverageAttribute_GPFlag; |
| 271 |
if (d->fRandom->nextBool()) { |
| 272 |
flags |= (d->fRandom->nextBool()) ? kCoverageAttributeTweak_GPFlag |
| 273 |
: kCoverageAttributeUnclamped_GPFlag; |
| 274 |
} |
| 275 |
} |
| 276 |
if (d->fRandom->nextBool()) { |
| 277 |
flags |= kLocalCoordAttribute_GPFlag; |
| 278 |
} |
| 279 |
|
| 280 |
GrColor color = GrTest::RandomColor(d->fRandom); |
| 281 |
SkMatrix viewMtx = GrTest::TestMatrix(d->fRandom); |
| 282 |
SkMatrix localMtx = GrTest::TestMatrix(d->fRandom); |
| 283 |
bool readsLocalCoords = d->fRandom->nextBool(); |
| 284 |
uint8_t coverage = GrTest::RandomCoverage(d->fRandom); |
| 285 |
return DefaultGeoProc::Make(d->allocator(), |
| 286 |
flags, |
| 287 |
SkPMColor4f::FromBytes_RGBA(color), |
| 288 |
viewMtx, |
| 289 |
localMtx, |
| 290 |
readsLocalCoords, |
| 291 |
coverage); |
| 292 |
} |
| 293 |
#endif |
| 294 |
|
| 295 |
GrGeometryProcessor* GrDefaultGeoProcFactory::Make(SkArenaAlloc* arena, |
| 296 |
const Color& color, |
| 297 |
const Coverage& coverage, |
| 298 |
const LocalCoords& localCoords, |
| 299 |
const SkMatrix& viewMatrix) { |
| 300 |
uint32_t flags = 0; |
| 301 |
if (Color::kPremulGrColorAttribute_Type == color.fType) { |
| 302 |
flags |= kColorAttribute_GPFlag; |
| 303 |
} else if (Color::kPremulWideColorAttribute_Type == color.fType) { |
| 304 |
flags |= kColorAttribute_GPFlag | kColorAttributeIsWide_GPFlag; |
| 305 |
} |
| 306 |
if (Coverage::kAttribute_Type == coverage.fType) { |
| 307 |
flags |= kCoverageAttribute_GPFlag; |
| 308 |
} else if (Coverage::kAttributeTweakAlpha_Type == coverage.fType) { |
| 309 |
flags |= kCoverageAttribute_GPFlag | kCoverageAttributeTweak_GPFlag; |
| 310 |
} else if (Coverage::kAttributeUnclamped_Type == coverage.fType) { |
| 311 |
flags |= kCoverageAttribute_GPFlag | kCoverageAttributeUnclamped_GPFlag; |
| 312 |
} |
| 313 |
flags |= localCoords.fType == LocalCoords::kHasExplicit_Type ? kLocalCoordAttribute_GPFlag : 0; |
| 314 |
|
| 315 |
uint8_t inCoverage = coverage.fCoverage; |
| 316 |
bool localCoordsWillBeRead = localCoords.fType != LocalCoords::kUnused_Type; |
| 317 |
|
| 318 |
return DefaultGeoProc::Make(arena, |
| 319 |
flags, |
| 320 |
color.fColor, |
| 321 |
viewMatrix, |
| 322 |
localCoords.fMatrix ? *localCoords.fMatrix : SkMatrix::I(), |
| 323 |
localCoordsWillBeRead, |
| 324 |
inCoverage); |
| 325 |
} |
| 326 |
|
| 327 |
GrGeometryProcessor* GrDefaultGeoProcFactory::MakeForDeviceSpace(SkArenaAlloc* arena, |
| 328 |
const Color& color, |
| 329 |
const Coverage& coverage, |
| 330 |
const LocalCoords& localCoords, |
| 331 |
const SkMatrix& viewMatrix) { |
| 332 |
SkMatrix inverse = SkMatrix::I(); |
| 333 |
if (LocalCoords::kUnused_Type != localCoords.fType) { |
| 334 |
SkASSERT(LocalCoords::kUsePosition_Type == localCoords.fType); |
| 335 |
if (auto inv = viewMatrix.invert()) { |
| 336 |
inverse = *inv; |
| 337 |
} else { |
| 338 |
return nullptr; |
| 339 |
} |
| 340 |
|
| 341 |
if (localCoords.hasLocalMatrix()) { |
| 342 |
inverse.postConcat(*localCoords.fMatrix); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
LocalCoords inverted(LocalCoords::kUsePosition_Type, &inverse); |
| 347 |
return Make(arena, color, coverage, inverted, SkMatrix::I()); |
| 348 |
} |