• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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/graphite/render/SDFTextRenderStep.h"
9 
10 #include "include/core/SkM44.h"
11 #include "include/gpu/graphite/Recorder.h"
12 #include "src/gpu/graphite/AtlasProvider.h"
13 #include "src/gpu/graphite/ContextUtils.h"
14 #include "src/gpu/graphite/DrawParams.h"
15 #include "src/gpu/graphite/DrawWriter.h"
16 #include "src/gpu/graphite/PipelineData.h"
17 #include "src/gpu/graphite/RecorderPriv.h"
18 #include "src/gpu/graphite/render/CommonDepthStencilSettings.h"
19 #include "src/gpu/graphite/text/TextAtlasManager.h"
20 #include "src/sksl/SkSLString.h"
21 #include "src/text/gpu/DistanceFieldAdjustTable.h"
22 #include "src/text/gpu/SubRunContainer.h"
23 #include "src/text/gpu/VertexFiller.h"
24 
25 #if defined(SK_GAMMA_APPLY_TO_A8)
26 #include "include/private/base/SkCPUTypes.h"
27 #include "src/core/SkMaskGamma.h"
28 #endif
29 
30 namespace skgpu::graphite {
31 
32 namespace {
33 
34 // We are expecting to sample from up to 4 textures
35 constexpr int kNumSDFAtlasTextures = 4;
36 
37 }  // namespace
38 
SDFTextRenderStep()39 SDFTextRenderStep::SDFTextRenderStep()
40         : RenderStep("SDFTextRenderStep",
41                      "",
42                      Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage,
43                      /*uniforms=*/{{"subRunDeviceMatrix", SkSLType::kFloat4x4},
44                                    {"deviceToLocal", SkSLType::kFloat4x4},
45                                    {"atlasSizeInv", SkSLType::kFloat2},
46                                    {"gammaParams", SkSLType::kHalf2}},
47                      PrimitiveType::kTriangleStrip,
48                      kDirectDepthGEqualPass,
49                      /*vertexAttrs=*/ {},
50                      /*instanceAttrs=*/
51                      {{"size", VertexAttribType::kUShort2, SkSLType::kUShort2},
52                       {"uvPos", VertexAttribType::kUShort2, SkSLType::kUShort2},
53                       {"xyPos", VertexAttribType::kFloat2, SkSLType::kFloat2},
54                       {"indexAndFlags", VertexAttribType::kUShort2, SkSLType::kUShort2},
55                       {"strikeToSourceScale", VertexAttribType::kFloat, SkSLType::kFloat},
56                       {"depth", VertexAttribType::kFloat, SkSLType::kFloat},
57                       {"ssboIndices", VertexAttribType::kUShort2, SkSLType::kUShort2}},
58                      /*varyings=*/
59                      {{"unormTexCoords", SkSLType::kFloat2},
60                       {"textureCoords", SkSLType::kFloat2},
61                       {"texIndex", SkSLType::kFloat}}) {}
62 
~SDFTextRenderStep()63 SDFTextRenderStep::~SDFTextRenderStep() {}
64 
vertexSkSL() const65 std::string SDFTextRenderStep::vertexSkSL() const {
66     // Returns the body of a vertex function, which must define a float4 devPosition variable and
67     // must write to an already-defined float2 stepLocalCoords variable.
68     return "texIndex = half(indexAndFlags.x);"
69            "float4 devPosition = text_vertex_fn(float2(sk_VertexID >> 1, sk_VertexID & 1), "
70                                                "subRunDeviceMatrix, "
71                                                "deviceToLocal, "
72                                                "atlasSizeInv, "
73                                                "float2(size), "
74                                                "float2(uvPos), "
75                                                "xyPos, "
76                                                "strikeToSourceScale, "
77                                                "depth, "
78                                                "textureCoords, "
79                                                "unormTexCoords, "
80                                                "stepLocalCoords);";
81 }
82 
texturesAndSamplersSkSL(const ResourceBindingRequirements & bindingReqs,int * nextBindingIndex) const83 std::string SDFTextRenderStep::texturesAndSamplersSkSL(
84         const ResourceBindingRequirements& bindingReqs, int* nextBindingIndex) const {
85     std::string result;
86 
87     for (unsigned int i = 0; i < kNumSDFAtlasTextures; ++i) {
88         result += EmitSamplerLayout(bindingReqs, nextBindingIndex);
89         SkSL::String::appendf(&result, " sampler2D sdf_atlas_%u;\n", i);
90     }
91 
92     return result;
93 }
94 
fragmentCoverageSkSL() const95 const char* SDFTextRenderStep::fragmentCoverageSkSL() const {
96     // The returned SkSL must write its coverage into a 'half4 outputCoverage' variable (defined in
97     // the calling code) with the actual coverage splatted out into all four channels.
98 
99     // TODO: To minimize the number of shaders generated this is the full affine shader.
100     // For best performance it may be worth creating the uniform scale shader as well,
101     // as that's the most common case.
102     // TODO: Need to add 565 support.
103     // TODO: Need aliased and possibly sRGB support.
104     static_assert(kNumSDFAtlasTextures == 4);
105     return "outputCoverage = sdf_text_coverage_fn(sample_indexed_atlas(textureCoords, "
106                                                                       "int(texIndex), "
107                                                                       "sdf_atlas_0, "
108                                                                       "sdf_atlas_1, "
109                                                                       "sdf_atlas_2, "
110                                                                       "sdf_atlas_3).r, "
111                                                  "gammaParams, "
112                                                  "unormTexCoords);";
113 }
114 
writeVertices(DrawWriter * dw,const DrawParams & params,skvx::ushort2 ssboIndices) const115 void SDFTextRenderStep::writeVertices(DrawWriter* dw,
116                                       const DrawParams& params,
117                                       skvx::ushort2 ssboIndices) const {
118     const SubRunData& subRunData = params.geometry().subRunData();
119     subRunData.subRun()->vertexFiller().fillInstanceData(dw,
120                                                          subRunData.startGlyphIndex(),
121                                                          subRunData.glyphCount(),
122                                                          subRunData.subRun()->instanceFlags(),
123                                                          ssboIndices,
124                                                          subRunData.subRun()->glyphs(),
125                                                          params.order().depthAsFloat());
126 }
127 
writeUniformsAndTextures(const DrawParams & params,PipelineDataGatherer * gatherer) const128 void SDFTextRenderStep::writeUniformsAndTextures(const DrawParams& params,
129                                                  PipelineDataGatherer* gatherer) const {
130     SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
131 
132     const SubRunData& subRunData = params.geometry().subRunData();
133     unsigned int numProxies;
134     Recorder* recorder = subRunData.recorder();
135     const sk_sp<TextureProxy>* proxies =
136             recorder->priv().atlasProvider()->textAtlasManager()->getProxies(
137                     subRunData.subRun()->maskFormat(), &numProxies);
138     SkASSERT(proxies && numProxies > 0);
139 
140     // write uniforms
141     gatherer->write(params.transform().matrix());  // subRunDeviceMatrix
142     gatherer->write(subRunData.deviceToLocal());
143     SkV2 atlasDimensionsInverse = {1.f/proxies[0]->dimensions().width(),
144                                    1.f/proxies[0]->dimensions().height()};
145     gatherer->write(atlasDimensionsInverse);
146 
147     float gammaAdjustment = 0;
148     // TODO: generate LCD adjustment
149 #if defined(SK_GAMMA_APPLY_TO_A8)
150     auto dfAdjustTable = sktext::gpu::DistanceFieldAdjustTable::Get();
151     // TODO: don't do this for aliased text
152     U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
153                                                         subRunData.luminanceColor());
154     gammaAdjustment = dfAdjustTable->getAdjustment(lum, subRunData.useGammaCorrectDistanceTable());
155 #endif
156     SkV2 gammaParams = {gammaAdjustment, subRunData.useGammaCorrectDistanceTable() ? 1.f : 0.f};
157     gatherer->writeHalf(gammaParams);
158 
159     // write textures and samplers
160     const SkSamplingOptions kSamplingOptions(SkFilterMode::kLinear);
161     constexpr SkTileMode kTileModes[2] = { SkTileMode::kClamp, SkTileMode::kClamp };
162     for (unsigned int i = 0; i < numProxies; ++i) {
163         gatherer->add(kSamplingOptions, kTileModes, proxies[i]);
164     }
165     // If the atlas has less than 4 active proxies we still need to set up samplers for the shader.
166     for (unsigned int i = numProxies; i < kNumSDFAtlasTextures; ++i) {
167         gatherer->add(kSamplingOptions, kTileModes, proxies[0]);
168     }
169 }
170 
171 }  // namespace skgpu::graphite
172