• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "src/gpu/ganesh/ops/AtlasInstancedHelper.h"
9 
10 #include "src/gpu/BufferWriter.h"
11 #include "src/gpu/KeyBuilder.h"
12 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
13 #include "src/gpu/ganesh/glsl/GrGLSLVarying.h"
14 #include "src/gpu/ganesh/glsl/GrGLSLVertexGeoBuilder.h"
15 
16 using namespace skia_private;
17 
18 namespace skgpu::ganesh {
19 
getKeyBits(KeyBuilder * b) const20 void AtlasInstancedHelper::getKeyBits(KeyBuilder* b) const {
21     b->addBits(kNumShaderFlags, (int)fShaderFlags, "atlasFlags");
22 }
23 
appendInstanceAttribs(TArray<GrGeometryProcessor::Attribute> * instanceAttribs) const24 void AtlasInstancedHelper::appendInstanceAttribs(
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 
writeInstanceData(VertexWriter * instanceWriter,const Instance * i) const32 void AtlasInstancedHelper::writeInstanceData(VertexWriter* instanceWriter,
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() <<
44             VertexWriter::If(fShaderFlags & ShaderFlags::kCheckBounds,
45                              SkSize::Make(i->fPathDevIBounds.size()));
46 }
47 
injectShaderCode(const GrGeometryProcessor::ProgramImpl::EmitArgs & args,const GrShaderVar & devCoord,GrGLSLUniformHandler::UniformHandle * atlasAdjustUniformHandle) const48 void AtlasInstancedHelper::injectShaderCode(
49         const GrGeometryProcessor::ProgramImpl::EmitArgs& args,
50         const GrShaderVar& devCoord,
51         GrGLSLUniformHandler::UniformHandle* atlasAdjustUniformHandle) const {
52     GrGLSLVarying atlasCoord(SkSLType::kFloat2);
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) {
73         GrGLSLVarying atlasBounds(SkSLType::kFloat4);
74         args.fVaryingHandler->addVarying("atlasbounds", &atlasBounds,
75                                          GrGLSLVaryingHandler::Interpolation::kCanBeFlat);
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 
setUniformData(const GrGLSLProgramDataManager & pdman,const GrGLSLUniformHandler::UniformHandle & atlasAdjustUniformHandle) const104 void AtlasInstancedHelper::setUniformData(
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
113