Flutter Engine
The Flutter Engine
AtlasInstancedHelper.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 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
9
11#include "src/gpu/KeyBuilder.h"
15
16using namespace skia_private;
17
18namespace skgpu::ganesh {
19
21 b->addBits(kNumShaderFlags, (int)fShaderFlags, "atlasFlags");
22}
23
25 TArray<GrGeometryProcessor::Attribute>* instanceAttribs) const {
26 instanceAttribs->emplace_back("locations", kFloat4_GrVertexAttribType, SkSLType::kFloat4);
27 if (fShaderFlags & ShaderFlags::kCheckBounds) {
28 instanceAttribs->emplace_back("sizeInAtlas", kFloat2_GrVertexAttribType, SkSLType::kFloat2);
29 }
30}
31
33 const Instance* i) const {
34 SkASSERT(i->fLocationInAtlas.x() >= 0);
35 SkASSERT(i->fLocationInAtlas.y() >= 0);
36 *instanceWriter <<
37 // A negative x coordinate in the atlas indicates that the path is transposed.
38 // Also add 1 since we can't negate zero.
39 ((float)(i->fTransposedInAtlas ? -i->fLocationInAtlas.x() - 1
40 : i->fLocationInAtlas.x() + 1)) <<
41 (float)i->fLocationInAtlas.y() <<
42 (float)i->fPathDevIBounds.left() <<
43 (float)i->fPathDevIBounds.top() <<
45 SkSize::Make(i->fPathDevIBounds.size()));
46}
47
50 const GrShaderVar& devCoord,
51 GrGLSLUniformHandler::UniformHandle* atlasAdjustUniformHandle) const {
53 args.fVaryingHandler->addVarying("atlasCoord", &atlasCoord);
54
55 const char* atlasAdjustName;
56 *atlasAdjustUniformHandle = args.fUniformHandler->addUniform(
57 nullptr, kVertex_GrShaderFlag, SkSLType::kFloat2, "atlas_adjust", &atlasAdjustName);
58
59 args.fVertBuilder->codeAppendf(
60 // A negative x coordinate in the atlas indicates that the path is transposed.
61 // We also added 1 since we can't negate zero.
62 "float2 atlasTopLeft = float2(abs(locations.x) - 1, locations.y);"
63 "float2 devTopLeft = locations.zw;"
64 "bool transposed = locations.x < 0;"
65 "float2 atlasCoord = %s - devTopLeft;"
66 "if (transposed) {"
67 "atlasCoord = atlasCoord.yx;"
68 "}"
69 "atlasCoord += atlasTopLeft;"
70 "%s = atlasCoord * %s;", devCoord.c_str(), atlasCoord.vsOut(), atlasAdjustName);
71
72 if (fShaderFlags & ShaderFlags::kCheckBounds) {
74 args.fVaryingHandler->addVarying("atlasbounds", &atlasBounds,
76 args.fVertBuilder->codeAppendf(R"(
77 float4 atlasBounds = atlasTopLeft.xyxy + (transposed ? sizeInAtlas.00yx
78 : sizeInAtlas.00xy);
79 %s = atlasBounds * %s.xyxy;)", atlasBounds.vsOut(), atlasAdjustName);
80
81 args.fFragBuilder->codeAppendf(
82 "half atlasCoverage = 0;"
83 "float2 atlasCoord = %s;"
84 "float4 atlasBounds = %s;"
85 "if (all(greaterThan(atlasCoord, atlasBounds.xy)) &&"
86 "all(lessThan(atlasCoord, atlasBounds.zw))) {"
87 "atlasCoverage = ", atlasCoord.fsIn(), atlasBounds.fsIn());
88 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[0], "atlasCoord");
89 args.fFragBuilder->codeAppendf(R"(.a;
90 })");
91 } else {
92 args.fFragBuilder->codeAppendf("half atlasCoverage = ");
93 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[0], atlasCoord.fsIn());
94 args.fFragBuilder->codeAppendf(".a;");
95 }
96
97 if (fShaderFlags & ShaderFlags::kInvertCoverage) {
98 args.fFragBuilder->codeAppendf("%s *= (1 - atlasCoverage);", args.fOutputCoverage);
99 } else {
100 args.fFragBuilder->codeAppendf("%s *= atlasCoverage;", args.fOutputCoverage);
101 }
102}
103
105 const GrGLSLProgramDataManager& pdman,
106 const GrGLSLUniformHandler::UniformHandle& atlasAdjustUniformHandle) const {
107 SkASSERT(fAtlasProxy->isInstantiated());
108 SkISize dimensions = fAtlasProxy->backingStoreDimensions();
109 pdman.set2f(atlasAdjustUniformHandle, 1.f / dimensions.width(), 1.f / dimensions.height());
110}
111
112} // namespace skgpu::ganesh
@ kVertex_GrShaderFlag
Definition: GrTypesPriv.h:286
@ kFloat2_GrVertexAttribType
Definition: GrTypesPriv.h:314
@ kFloat4_GrVertexAttribType
Definition: GrTypesPriv.h:316
#define SkASSERT(cond)
Definition: SkAssert.h:116
virtual void set2f(UniformHandle, float, float) const =0
GrGLSLProgramDataManager::UniformHandle UniformHandle
const char * vsOut() const
Definition: GrGLSLVarying.h:67
const char * fsIn() const
Definition: GrGLSLVarying.h:68
const char * c_str() const
Definition: GrShaderVar.h:94
SkISize backingStoreDimensions() const
bool isInstantiated() const
void writeInstanceData(VertexWriter *instanceWriter, const Instance *) const
void appendInstanceAttribs(skia_private::TArray< GrGeometryProcessor::Attribute > *instanceAttribs) const
void injectShaderCode(const GrGeometryProcessor::ProgramImpl::EmitArgs &, const GrShaderVar &devCoord, GrGLSLUniformHandler::UniformHandle *atlasAdjustUniformHandle) const
void setUniformData(const GrGLSLProgramDataManager &, const GrGLSLUniformHandler::UniformHandle &atlasAdjustUniformHandle) const
T & emplace_back(Args &&... args)
Definition: SkTArray.h:248
static bool b
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
Definition: SkSize.h:16
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition: SkSize.h:56
static Conditional< T > If(bool condition, const T &value)
Definition: BufferWriter.h:153