Debian Codegraph @

src/gpu/graphite/render/SDFTextRenderStep.cpp

raw
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
53 SDFTextRenderStep::SDFTextRenderStep(Layout layout)
54 : RenderStep(layout,
55 RenderStepID::kSDFText,
56 Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage |
57 Flags::kAppendInstances,
58 /*uniforms=*/{{"subRunDeviceMatrix", SkSLType::kFloat4x4},
59 {"deviceToLocal", SkSLType::kFloat4x4},
60 {"atlasSizeInv", SkSLType::kFloat2},
61 {"gammaParams", SkSLType::kHalf2}},
62 PrimitiveType::kTriangleStrip,
63 kDirectDepthLEqualPass,
64 /*staticAttrs=*/ {},
65 /*appendAttrs=*/
66 {{{"size", VertexAttribType::kUShort2, SkSLType::kUShort2},
67 {"uvPos", VertexAttribType::kUShort2, SkSLType::kUShort2},
68 {"xyPos", VertexAttribType::kFloat2, SkSLType::kFloat2},
69 {"indexAndFlags", VertexAttribType::kUShort2, SkSLType::kUShort2},
70 {"strikeToSourceScale", VertexAttribType::kFloat, SkSLType::kFloat},
71 {"depth", VertexAttribType::kFloat, SkSLType::kFloat},
72 {"ssboIndex", VertexAttribType::kUInt, SkSLType::kUInt}}},
73 /*varyings=*/
74 {{{"unormTexCoords", SkSLType::kFloat2},
75 {"textureCoords", SkSLType::kFloat2},
76 {"texIndex", SkSLType::kFloat}}}) {}
77
78 SDFTextRenderStep::~SDFTextRenderStep() {}
79
80 std::string SDFTextRenderStep::vertexSkSL() const {
81 // Returns the body of a vertex function, which must define a float4 devPosition variable and
82 // must write to an already-defined float2 stepLocalCoords variable.
83 return "texIndex = half(indexAndFlags.x);"
84 "float4 devPosition = text_vertex_fn(float2(sk_VertexID >> 1, sk_VertexID & 1), "
85 "subRunDeviceMatrix, "
86 "deviceToLocal, "
87 "atlasSizeInv, "
88 "float2(size), "
89 "float2(uvPos), "
90 "xyPos, "
91 "strikeToSourceScale, "
92 "depth, "
93 "textureCoords, "
94 "unormTexCoords, "
95 "stepLocalCoords);";
96 }
97
98 std::string SDFTextRenderStep::texturesAndSamplersSkSL(
99 const ResourceBindingRequirements& bindingReqs, int* nextBindingIndex) const {
100 std::string result;
101
102 for (unsigned int i = 0; i < kNumSDFAtlasTextures; ++i) {
103 result += EmitSamplerLayout(bindingReqs, nextBindingIndex);
104 SkSL::String::appendf(&result, " sampler2D sdf_atlas_%u;\n", i);
105 }
106
107 return result;
108 }
109
110 const char* SDFTextRenderStep::fragmentCoverageSkSL() const {
111 // The returned SkSL must write its coverage into a 'half4 outputCoverage' variable (defined in
112 // the calling code) with the actual coverage splatted out into all four channels.
113
114 // TODO: To minimize the number of shaders generated this is the full affine shader.
115 // For best performance it may be worth creating the uniform scale shader as well,
116 // as that's the most common case.
117 // TODO: Need to add 565 support.
118 // TODO: Need aliased and possibly sRGB support.
119 static_assert(kNumSDFAtlasTextures == 4);
120 return "outputCoverage = sdf_text_coverage_fn(sample_indexed_atlas(textureCoords, "
121 "int(texIndex), "
122 "sdf_atlas_0, "
123 "sdf_atlas_1, "
124 "sdf_atlas_2, "
125 "sdf_atlas_3).r, "
126 "gammaParams, "
127 "unormTexCoords);";
128 }
129
130 void SDFTextRenderStep::writeVertices(DrawWriter* dw,
131 const DrawParams& params,
132 uint32_t ssboIndex) const {
133 const SubRunData& subRunData = params.geometry().subRunData();
134 subRunData.subRun()->vertexFiller().fillInstanceData(dw,
135 subRunData.startGlyphIndex(),
136 subRunData.glyphCount(),
137 subRunData.subRun()->instanceFlags(),
138 ssboIndex,
139 subRunData.subRun()->glyphs(),
140 params.order().depthAsFloat());
141 }
142
143 void SDFTextRenderStep::writeUniformsAndTextures(const DrawParams& params,
144 PipelineDataGatherer* gatherer) const {
145 SkDEBUGCODE(gatherer->checkRewind());
146 SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
147
148 const SubRunData& subRunData = params.geometry().subRunData();
149 unsigned int numProxies;
150 Recorder* recorder = subRunData.recorder();
151 const sk_sp<TextureProxy>* proxies =
152 recorder->priv().atlasProvider()->textAtlasManager()->getProxies(
153 subRunData.subRun()->maskFormat(), &numProxies);
154 SkASSERT(proxies && numProxies > 0);
155
156 // write uniforms
157 gatherer->write(params.transform().matrix()); // subRunDeviceMatrix
158 gatherer->write(subRunData.deviceToLocal());
159 SkV2 atlasDimensionsInverse = {1.f/proxies[0]->dimensions().width(),
160 1.f/proxies[0]->dimensions().height()};
161 gatherer->write(atlasDimensionsInverse);
162
163 float gammaAdjustment = 0;
164 // TODO: generate LCD adjustment
165 #if defined(SK_GAMMA_APPLY_TO_A8)
166 auto dfAdjustTable = sktext::gpu::DistanceFieldAdjustTable::Get();
167 // TODO: don't do this for aliased text
168 U8CPU lum = SkColorSpaceLuminance::computeLuminance(SK_GAMMA_EXPONENT,
169 subRunData.luminanceColor());
170 gammaAdjustment = dfAdjustTable->getAdjustment(lum, subRunData.useGammaCorrectDistanceTable());
171 #endif
172 SkV2 gammaParams = {gammaAdjustment, subRunData.useGammaCorrectDistanceTable() ? 1.f : 0.f};
173 gatherer->writeHalf(gammaParams);
174
175 // write textures and samplers
176 for (unsigned int i = 0; i < numProxies; ++i) {
177 gatherer->add(proxies[i], {SkFilterMode::kLinear, SkTileMode::kClamp});
178 }
179 // If the atlas has less than 4 active proxies we still need to set up samplers for the shader.
180 for (unsigned int i = numProxies; i < kNumSDFAtlasTextures; ++i) {
181 gatherer->add(proxies[0], {SkFilterMode::kLinear, SkTileMode::kClamp});
182 }
183 }
184
185 } // namespace skgpu::graphite