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.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 namespace skgpu::graphite {
42
43 namespace {
44
45 // We are expecting to sample from up to 4 textures
46 constexpr int kNumSDFAtlasTextures = 4;
47
48 } // namespace
49
SDFTextLCDRenderStep()50 SDFTextLCDRenderStep::SDFTextLCDRenderStep()
51 : RenderStep(RenderStepID::kSDFTextLCD,
52 Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage |
53 Flags::kLCDCoverage,
54 /*uniforms=*/{{"subRunDeviceMatrix", SkSLType::kFloat4x4},
55 {"deviceToLocal", SkSLType::kFloat4x4},
56 {"atlasSizeInv", SkSLType::kFloat2},
57 {"pixelGeometryDelta", SkSLType::kHalf2},
58 {"gammaParams", SkSLType::kHalf4}},
59 PrimitiveType::kTriangleStrip,
60 kDirectDepthGEqualPass,
61 /*vertexAttrs=*/ {},
62 /*instanceAttrs=*/
63 {{"size", VertexAttribType::kUShort2, SkSLType::kUShort2},
64 {"uvPos", VertexAttribType::kUShort2, SkSLType::kUShort2},
65 {"xyPos", VertexAttribType::kFloat2, SkSLType::kFloat2},
66 {"indexAndFlags", VertexAttribType::kUShort2, SkSLType::kUShort2},
67 {"strikeToSourceScale", VertexAttribType::kFloat, SkSLType::kFloat},
68 {"depth", VertexAttribType::kFloat, SkSLType::kFloat},
69 {"ssboIndices", VertexAttribType::kUInt2, SkSLType::kUInt2}},
70 /*varyings=*/
71 {{"unormTexCoords", SkSLType::kFloat2},
72 {"textureCoords", SkSLType::kFloat2},
73 {"texIndex", SkSLType::kFloat}}) {}
74
~SDFTextLCDRenderStep()75 SDFTextLCDRenderStep::~SDFTextLCDRenderStep() {}
76
vertexSkSL() const77 std::string SDFTextLCDRenderStep::vertexSkSL() const {
78 // Returns the body of a vertex function, which must define a float4 devPosition variable and
79 // must write to an already-defined float2 stepLocalCoords variable.
80 return "texIndex = half(indexAndFlags.x);"
81 "float4 devPosition = text_vertex_fn(float2(sk_VertexID >> 1, sk_VertexID & 1), "
82 "subRunDeviceMatrix, "
83 "deviceToLocal, "
84 "atlasSizeInv, "
85 "float2(size), "
86 "float2(uvPos), "
87 "xyPos, "
88 "strikeToSourceScale, "
89 "depth, "
90 "textureCoords, "
91 "unormTexCoords, "
92 "stepLocalCoords);";
93 }
94
texturesAndSamplersSkSL(const ResourceBindingRequirements & bindingReqs,int * nextBindingIndex) const95 std::string SDFTextLCDRenderStep::texturesAndSamplersSkSL(
96 const ResourceBindingRequirements& bindingReqs, int* nextBindingIndex) const {
97 std::string result;
98
99 for (unsigned int i = 0; i < kNumSDFAtlasTextures; ++i) {
100 result += EmitSamplerLayout(bindingReqs, nextBindingIndex);
101 SkSL::String::appendf(&result, " sampler2D sdf_atlas_%u;\n", i);
102 }
103
104 return result;
105 }
106
fragmentCoverageSkSL() const107 const char* SDFTextLCDRenderStep::fragmentCoverageSkSL() const {
108 // The returned SkSL must write its coverage into a 'half4 outputCoverage' variable (defined in
109 // the calling code) with the actual coverage splatted out into all four channels.
110
111 // TODO: To minimize the number of shaders generated this is the full affine shader.
112 // For best performance it may be worth creating the uniform scale shader as well,
113 // as that's the most common case.
114 // TODO: Need to add 565 support.
115 // TODO: Need aliased and possibly sRGB support.
116 static_assert(kNumSDFAtlasTextures == 4);
117 return "outputCoverage = sdf_text_lcd_coverage_fn(textureCoords, "
118 "pixelGeometryDelta, "
119 "gammaParams, "
120 "unormTexCoords, "
121 "texIndex, "
122 "sdf_atlas_0, "
123 "sdf_atlas_1, "
124 "sdf_atlas_2, "
125 "sdf_atlas_3);";
126 }
127
writeVertices(DrawWriter * dw,const DrawParams & params,skvx::uint2 ssboIndices) const128 void SDFTextLCDRenderStep::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 SDFTextLCDRenderStep::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 // compute and write pixelGeometry vector
161 SkV2 pixelGeometryDelta = {0, 0};
162 if (SkPixelGeometryIsH(subRunData.pixelGeometry())) {
163 pixelGeometryDelta = {1.f/(3*proxies[0]->dimensions().width()), 0};
164 } else if (SkPixelGeometryIsV(subRunData.pixelGeometry())) {
165 pixelGeometryDelta = {0, 1.f/(3*proxies[0]->dimensions().height())};
166 }
167 if (SkPixelGeometryIsBGR(subRunData.pixelGeometry())) {
168 pixelGeometryDelta = -pixelGeometryDelta;
169 }
170 gatherer->writeHalf(pixelGeometryDelta);
171
172 // compute and write gamma adjustment
173 auto dfAdjustTable = sktext::gpu::DistanceFieldAdjustTable::Get();
174 float redCorrection = dfAdjustTable->getAdjustment(SkColorGetR(subRunData.luminanceColor()),
175 subRunData.useGammaCorrectDistanceTable());
176 float greenCorrection = dfAdjustTable->getAdjustment(SkColorGetG(subRunData.luminanceColor()),
177 subRunData.useGammaCorrectDistanceTable());
178 float blueCorrection = dfAdjustTable->getAdjustment(SkColorGetB(subRunData.luminanceColor()),
179 subRunData.useGammaCorrectDistanceTable());
180 SkV4 gammaParams = {redCorrection, greenCorrection, blueCorrection,
181 subRunData.useGammaCorrectDistanceTable() ? 1.f : 0.f};
182 gatherer->writeHalf(gammaParams);
183
184 // write textures and samplers
185 for (unsigned int i = 0; i < numProxies; ++i) {
186 gatherer->add(proxies[i], {SkFilterMode::kLinear, SkTileMode::kClamp});
187 }
188 // If the atlas has less than 4 active proxies we still need to set up samplers for the shader.
189 for (unsigned int i = numProxies; i < kNumSDFAtlasTextures; ++i) {
190 gatherer->add(proxies[0], {SkFilterMode::kLinear, SkTileMode::kClamp});
191 }
192 }
193
194 } // namespace skgpu::graphite
195