| 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 |
|
| 50 |
SDFTextLCDRenderStep::SDFTextLCDRenderStep(Layout layout) |
| 51 |
: RenderStep(layout, |
| 52 |
RenderStepID::kSDFTextLCD, |
| 53 |
Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage | |
| 54 |
Flags::kLCDCoverage | Flags::kAppendInstances, |
| 55 |
/*uniforms=*/{{"subRunDeviceMatrix", SkSLType::kFloat4x4}, |
| 56 |
{"deviceToLocal", SkSLType::kFloat4x4}, |
| 57 |
{"atlasSizeInv", SkSLType::kFloat2}, |
| 58 |
{"pixelGeometryDelta", SkSLType::kHalf2}, |
| 59 |
{"gammaParams", SkSLType::kHalf4}}, |
| 60 |
PrimitiveType::kTriangleStrip, |
| 61 |
kDirectDepthLEqualPass, |
| 62 |
/*staticAttrs=*/ {}, |
| 63 |
/*appendAttrs=*/ |
| 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 |
{"ssboIndex", VertexAttribType::kUInt, SkSLType::kUInt}}}, |
| 71 |
/*varyings=*/ |
| 72 |
{{{"unormTexCoords", SkSLType::kFloat2}, |
| 73 |
{"textureCoords", SkSLType::kFloat2}, |
| 74 |
{"texIndex", SkSLType::kFloat}}}) {} |
| 75 |
|
| 76 |
SDFTextLCDRenderStep::~SDFTextLCDRenderStep() {} |
| 77 |
|
| 78 |
std::string SDFTextLCDRenderStep::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 |
|
| 96 |
std::string SDFTextLCDRenderStep::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 |
|
| 108 |
const char* SDFTextLCDRenderStep::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_lcd_coverage_fn(textureCoords, " |
| 119 |
"pixelGeometryDelta, " |
| 120 |
"gammaParams, " |
| 121 |
"unormTexCoords, " |
| 122 |
"texIndex, " |
| 123 |
"sdf_atlas_0, " |
| 124 |
"sdf_atlas_1, " |
| 125 |
"sdf_atlas_2, " |
| 126 |
"sdf_atlas_3);"; |
| 127 |
} |
| 128 |
|
| 129 |
void SDFTextLCDRenderStep::writeVertices(DrawWriter* dw, |
| 130 |
const DrawParams& params, |
| 131 |
uint32_t ssboIndex) const { |
| 132 |
const SubRunData& subRunData = params.geometry().subRunData(); |
| 133 |
subRunData.subRun()->vertexFiller().fillInstanceData(dw, |
| 134 |
subRunData.startGlyphIndex(), |
| 135 |
subRunData.glyphCount(), |
| 136 |
subRunData.subRun()->instanceFlags(), |
| 137 |
ssboIndex, |
| 138 |
subRunData.subRun()->glyphs(), |
| 139 |
params.order().depthAsFloat()); |
| 140 |
} |
| 141 |
|
| 142 |
void SDFTextLCDRenderStep::writeUniformsAndTextures(const DrawParams& params, |
| 143 |
PipelineDataGatherer* gatherer) const { |
| 144 |
SkDEBUGCODE(gatherer->checkRewind()); |
| 145 |
SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());) |
| 146 |
|
| 147 |
const SubRunData& subRunData = params.geometry().subRunData(); |
| 148 |
unsigned int numProxies; |
| 149 |
Recorder* recorder = subRunData.recorder(); |
| 150 |
const sk_sp<TextureProxy>* proxies = |
| 151 |
recorder->priv().atlasProvider()->textAtlasManager()->getProxies( |
| 152 |
subRunData.subRun()->maskFormat(), &numProxies); |
| 153 |
SkASSERT(proxies && numProxies > 0); |
| 154 |
|
| 155 |
// write uniforms |
| 156 |
gatherer->write(params.transform().matrix()); // subRunDeviceMatrix |
| 157 |
gatherer->write(subRunData.deviceToLocal()); |
| 158 |
SkV2 atlasDimensionsInverse = {1.f/proxies[0]->dimensions().width(), |
| 159 |
1.f/proxies[0]->dimensions().height()}; |
| 160 |
gatherer->write(atlasDimensionsInverse); |
| 161 |
|
| 162 |
// compute and write pixelGeometry vector |
| 163 |
SkV2 pixelGeometryDelta = {0, 0}; |
| 164 |
if (SkPixelGeometryIsH(subRunData.pixelGeometry())) { |
| 165 |
pixelGeometryDelta = {1.f/(3*proxies[0]->dimensions().width()), 0}; |
| 166 |
} else if (SkPixelGeometryIsV(subRunData.pixelGeometry())) { |
| 167 |
pixelGeometryDelta = {0, 1.f/(3*proxies[0]->dimensions().height())}; |
| 168 |
} |
| 169 |
if (SkPixelGeometryIsBGR(subRunData.pixelGeometry())) { |
| 170 |
pixelGeometryDelta = -pixelGeometryDelta; |
| 171 |
} |
| 172 |
gatherer->writeHalf(pixelGeometryDelta); |
| 173 |
|
| 174 |
// compute and write gamma adjustment |
| 175 |
auto dfAdjustTable = sktext::gpu::DistanceFieldAdjustTable::Get(); |
| 176 |
float redCorrection = dfAdjustTable->getAdjustment(SkColorGetR(subRunData.luminanceColor()), |
| 177 |
subRunData.useGammaCorrectDistanceTable()); |
| 178 |
float greenCorrection = dfAdjustTable->getAdjustment(SkColorGetG(subRunData.luminanceColor()), |
| 179 |
subRunData.useGammaCorrectDistanceTable()); |
| 180 |
float blueCorrection = dfAdjustTable->getAdjustment(SkColorGetB(subRunData.luminanceColor()), |
| 181 |
subRunData.useGammaCorrectDistanceTable()); |
| 182 |
SkV4 gammaParams = {redCorrection, greenCorrection, blueCorrection, |
| 183 |
subRunData.useGammaCorrectDistanceTable() ? 1.f : 0.f}; |
| 184 |
gatherer->writeHalf(gammaParams); |
| 185 |
|
| 186 |
// write textures and samplers |
| 187 |
for (unsigned int i = 0; i < numProxies; ++i) { |
| 188 |
gatherer->add(proxies[i], {SkFilterMode::kLinear, SkTileMode::kClamp}); |
| 189 |
} |
| 190 |
// If the atlas has less than 4 active proxies we still need to set up samplers for the shader. |
| 191 |
for (unsigned int i = numProxies; i < kNumSDFAtlasTextures; ++i) { |
| 192 |
gatherer->add(proxies[0], {SkFilterMode::kLinear, SkTileMode::kClamp}); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
} // namespace skgpu::graphite |