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