1 /*
2 * Copyright 2024 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/SDFTextLCDRenderStep.h"
9
10 #include "include/core/SkColor.h"
11 #include "include/core/SkM44.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkSamplingOptions.h"
14 #include "include/core/SkSize.h"
15 #include "include/core/SkSurfaceProps.h"
16 #include "include/core/SkTileMode.h"
17 #include "include/gpu/graphite/Recorder.h"
18 #include "include/private/base/SkAssert.h"
19 #include "include/private/base/SkDebug.h"
20 #include "src/base/SkEnumBitMask.h"
21 #include "src/core/SkSLTypeShared.h"
22 #include "src/gpu/graphite/AtlasProvider.h"
23 #include "src/gpu/graphite/Attribute.h"
24 #include "src/gpu/graphite/ContextUtils.h"
25 #include "src/gpu/graphite/DrawOrder.h"
26 #include "src/gpu/graphite/DrawParams.h"
27 #include "src/gpu/graphite/DrawTypes.h"
28 #include "src/gpu/graphite/PipelineData.h"
29 #include "src/gpu/graphite/RecorderPriv.h"
30 #include "src/gpu/graphite/TextureProxy.h"
31 #include "src/gpu/graphite/geom/Geometry.h"
32 #include "src/gpu/graphite/geom/SubRunData.h"
33 #include "src/gpu/graphite/geom/Transform_graphite.h"
34 #include "src/gpu/graphite/render/CommonDepthStencilSettings.h"
35 #include "src/gpu/graphite/text/TextAtlasManager.h"
36 #include "src/sksl/SkSLString.h"
37 #include "src/text/gpu/DistanceFieldAdjustTable.h"
38 #include "src/text/gpu/SubRunContainer.h"
39 #include "src/text/gpu/VertexFiller.h"
40
41 #include <string_view>
42
43 namespace skgpu::graphite {
44
45 namespace {
46
47 // We are expecting to sample from up to 4 textures
48 constexpr int kNumSDFAtlasTextures = 4;
49
50 } // namespace
51
SDFTextLCDRenderStep()52 SDFTextLCDRenderStep::SDFTextLCDRenderStep()
53 : RenderStep("SDFTextLCDRenderStep",
54 "",
55 Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage |
56 Flags::kLCDCoverage,
57 /*uniforms=*/{{"subRunDeviceMatrix", SkSLType::kFloat4x4},
58 {"deviceToLocal", SkSLType::kFloat4x4},
59 {"atlasSizeInv", SkSLType::kFloat2},
60 {"pixelGeometryDelta", SkSLType::kHalf2},
61 {"gammaParams", SkSLType::kHalf4}},
62 PrimitiveType::kTriangleStrip,
63 kDirectDepthGEqualPass,
64 /*vertexAttrs=*/ {},
65 /*instanceAttrs=*/
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 {"ssboIndices", VertexAttribType::kUInt2, SkSLType::kUInt2}},
73 /*varyings=*/
74 {{"unormTexCoords", SkSLType::kFloat2},
75 {"textureCoords", SkSLType::kFloat2},
76 {"texIndex", SkSLType::kFloat}}) {}
77
~SDFTextLCDRenderStep()78 SDFTextLCDRenderStep::~SDFTextLCDRenderStep() {}
79
vertexSkSL() const80 std::string SDFTextLCDRenderStep::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
texturesAndSamplersSkSL(const ResourceBindingRequirements & bindingReqs,int * nextBindingIndex) const98 std::string SDFTextLCDRenderStep::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
fragmentCoverageSkSL() const110 const char* SDFTextLCDRenderStep::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_lcd_coverage_fn(textureCoords, "
121 "pixelGeometryDelta, "
122 "gammaParams, "
123 "unormTexCoords, "
124 "texIndex, "
125 "sdf_atlas_0, "
126 "sdf_atlas_1, "
127 "sdf_atlas_2, "
128 "sdf_atlas_3);";
129 }
130
writeVertices(DrawWriter * dw,const DrawParams & params,skvx::uint2 ssboIndices) const131 void SDFTextLCDRenderStep::writeVertices(DrawWriter* dw,
132 const DrawParams& params,
133 skvx::uint2 ssboIndices) const {
134 const SubRunData& subRunData = params.geometry().subRunData();
135 subRunData.subRun()->vertexFiller().fillInstanceData(dw,
136 subRunData.startGlyphIndex(),
137 subRunData.glyphCount(),
138 subRunData.subRun()->instanceFlags(),
139 ssboIndices,
140 subRunData.subRun()->glyphs(),
141 params.order().depthAsFloat());
142 }
143
writeUniformsAndTextures(const DrawParams & params,PipelineDataGatherer * gatherer) const144 void SDFTextLCDRenderStep::writeUniformsAndTextures(const DrawParams& params,
145 PipelineDataGatherer* gatherer) const {
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 // compute and write pixelGeometry vector
164 SkV2 pixelGeometryDelta = {0, 0};
165 if (SkPixelGeometryIsH(subRunData.pixelGeometry())) {
166 pixelGeometryDelta = {1.f/(3*proxies[0]->dimensions().width()), 0};
167 } else if (SkPixelGeometryIsV(subRunData.pixelGeometry())) {
168 pixelGeometryDelta = {0, 1.f/(3*proxies[0]->dimensions().height())};
169 }
170 if (SkPixelGeometryIsBGR(subRunData.pixelGeometry())) {
171 pixelGeometryDelta = -pixelGeometryDelta;
172 }
173 gatherer->writeHalf(pixelGeometryDelta);
174
175 // compute and write gamma adjustment
176 auto dfAdjustTable = sktext::gpu::DistanceFieldAdjustTable::Get();
177 float redCorrection = dfAdjustTable->getAdjustment(SkColorGetR(subRunData.luminanceColor()),
178 subRunData.useGammaCorrectDistanceTable());
179 float greenCorrection = dfAdjustTable->getAdjustment(SkColorGetG(subRunData.luminanceColor()),
180 subRunData.useGammaCorrectDistanceTable());
181 float blueCorrection = dfAdjustTable->getAdjustment(SkColorGetB(subRunData.luminanceColor()),
182 subRunData.useGammaCorrectDistanceTable());
183 SkV4 gammaParams = {redCorrection, greenCorrection, blueCorrection,
184 subRunData.useGammaCorrectDistanceTable() ? 1.f : 0.f};
185 gatherer->writeHalf(gammaParams);
186
187 // write textures and samplers
188 for (unsigned int i = 0; i < numProxies; ++i) {
189 gatherer->add(proxies[i], {SkFilterMode::kLinear, SkTileMode::kClamp});
190 }
191 // If the atlas has less than 4 active proxies we still need to set up samplers for the shader.
192 for (unsigned int i = numProxies; i < kNumSDFAtlasTextures; ++i) {
193 gatherer->add(proxies[0], {SkFilterMode::kLinear, SkTileMode::kClamp});
194 }
195 }
196
197 } // namespace skgpu::graphite
198