• 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/BitmapTextRenderStep.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/SubRunContainer.h"
22 #include "src/text/gpu/VertexFiller.h"
23 
24 using AtlasSubRun = sktext::gpu::AtlasSubRun;
25 
26 namespace skgpu::graphite {
27 
28 namespace {
29 
30 // We are expecting to sample from up to 4 textures
31 constexpr int kNumTextAtlasTextures = 4;
32 
33 }  // namespace
34 
BitmapTextRenderStep(bool isLCD)35 BitmapTextRenderStep::BitmapTextRenderStep(bool isLCD)
36         : RenderStep("BitmapTextRenderStep",
37                      "",
38                      isLCD ? Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage |
39                              Flags::kLCDCoverage
40                            : Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage,
41                      /*uniforms=*/{{"subRunDeviceMatrix", SkSLType::kFloat4x4},
42                                    {"deviceToLocal"     , SkSLType::kFloat4x4},
43                                    {"atlasSizeInv"      , SkSLType::kFloat2}},
44                      PrimitiveType::kTriangleStrip,
45                      kDirectDepthGEqualPass,
46                      /*vertexAttrs=*/ {},
47                      /*instanceAttrs=*/
48                      {{"size", VertexAttribType::kUShort2, SkSLType::kUShort2},
49                       {"uvPos", VertexAttribType::kUShort2, SkSLType::kUShort2},
50                       {"xyPos", VertexAttribType::kFloat2, SkSLType::kFloat2},
51                       {"indexAndFlags", VertexAttribType::kUShort2, SkSLType::kUShort2},
52                       {"strikeToSourceScale", VertexAttribType::kFloat, SkSLType::kFloat},
53                       {"depth", VertexAttribType::kFloat, SkSLType::kFloat},
54                       {"ssboIndices", VertexAttribType::kUShort2, SkSLType::kUShort2}},
55                      /*varyings=*/
56                      {{"textureCoords", SkSLType::kFloat2},
57                       {"texIndex", SkSLType::kHalf},
58                       {"maskFormat", SkSLType::kHalf}}) {}
59 
~BitmapTextRenderStep()60 BitmapTextRenderStep::~BitmapTextRenderStep() {}
61 
vertexSkSL() const62 std::string BitmapTextRenderStep::vertexSkSL() const {
63     // Returns the body of a vertex function, which must define a float4 devPosition variable and
64     // must write to an already-defined float2 stepLocalCoords variable.
65     return "texIndex = half(indexAndFlags.x);"
66            "maskFormat = half(indexAndFlags.y);"
67            "float2 unormTexCoords;"
68            "float4 devPosition = text_vertex_fn(float2(sk_VertexID >> 1, sk_VertexID & 1), "
69                                                "subRunDeviceMatrix, "
70                                                "deviceToLocal, "
71                                                "atlasSizeInv, "
72                                                "float2(size), "
73                                                "float2(uvPos), "
74                                                "xyPos, "
75                                                "strikeToSourceScale, "
76                                                "depth, "
77                                                "textureCoords, "
78                                                "unormTexCoords, "
79                                                "stepLocalCoords);";
80 }
81 
texturesAndSamplersSkSL(const ResourceBindingRequirements & bindingReqs,int * nextBindingIndex) const82 std::string BitmapTextRenderStep::texturesAndSamplersSkSL(
83         const ResourceBindingRequirements& bindingReqs, int* nextBindingIndex) const {
84     std::string result;
85 
86     for (unsigned int i = 0; i < kNumTextAtlasTextures; ++i) {
87         result += EmitSamplerLayout(bindingReqs, nextBindingIndex);
88         SkSL::String::appendf(&result, " sampler2D text_atlas_%u;\n", i);
89     }
90 
91     return result;
92 }
93 
fragmentCoverageSkSL() const94 const char* BitmapTextRenderStep::fragmentCoverageSkSL() const {
95     // The returned SkSL must write its coverage into a 'half4 outputCoverage' variable (defined in
96     // the calling code) with the actual coverage splatted out into all four channels.
97     static_assert(kNumTextAtlasTextures == 4);
98     return "outputCoverage = bitmap_text_coverage_fn(sample_indexed_atlas(textureCoords, "
99                                                                          "int(texIndex), "
100                                                                          "text_atlas_0, "
101                                                                          "text_atlas_1, "
102                                                                          "text_atlas_2, "
103                                                                          "text_atlas_3), "
104                                                     "int(maskFormat));";
105 }
106 
writeVertices(DrawWriter * dw,const DrawParams & params,skvx::ushort2 ssboIndices) const107 void BitmapTextRenderStep::writeVertices(DrawWriter* dw,
108                                          const DrawParams& params,
109                                          skvx::ushort2 ssboIndices) const {
110     const SubRunData& subRunData = params.geometry().subRunData();
111 
112     subRunData.subRun()->vertexFiller().fillInstanceData(dw,
113                                                          subRunData.startGlyphIndex(),
114                                                          subRunData.glyphCount(),
115                                                          subRunData.subRun()->instanceFlags(),
116                                                          ssboIndices,
117                                                          subRunData.subRun()->glyphs(),
118                                                          params.order().depthAsFloat());
119 }
120 
writeUniformsAndTextures(const DrawParams & params,PipelineDataGatherer * gatherer) const121 void BitmapTextRenderStep::writeUniformsAndTextures(const DrawParams& params,
122                                                     PipelineDataGatherer* gatherer) const {
123     SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
124 
125     const SubRunData& subRunData = params.geometry().subRunData();
126     unsigned int numProxies;
127     Recorder* recorder = subRunData.recorder();
128     const sk_sp<TextureProxy>* proxies =
129             recorder->priv().atlasProvider()->textAtlasManager()->getProxies(
130                     subRunData.subRun()->maskFormat(), &numProxies);
131     SkASSERT(proxies && numProxies > 0);
132 
133     // write uniforms
134     gatherer->write(params.transform().matrix());  // subRunDeviceMatrix
135     gatherer->write(subRunData.deviceToLocal());
136     SkV2 atlasDimensionsInverse = {1.f/proxies[0]->dimensions().width(),
137                                    1.f/proxies[0]->dimensions().height()};
138     gatherer->write(atlasDimensionsInverse);
139 
140     // write textures and samplers
141     const SkSamplingOptions kSamplingOptions(SkFilterMode::kNearest);
142     constexpr SkTileMode kTileModes[2] = { SkTileMode::kClamp, SkTileMode::kClamp };
143     for (unsigned int i = 0; i < numProxies; ++i) {
144         gatherer->add(kSamplingOptions, kTileModes, proxies[i]);
145     }
146     // If the atlas has less than 4 active proxies we still need to set up samplers for the shader.
147     for (unsigned int i = numProxies; i < kNumTextAtlasTextures; ++i) {
148         gatherer->add(kSamplingOptions, kTileModes, proxies[0]);
149     }
150 }
151 
152 }  // namespace skgpu::graphite
153