xref: /aosp_15_r20/external/skia/src/gpu/graphite/ShaderInfo.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/ShaderInfo.h"
9 
10 #include "src/gpu/BlendFormula.h"
11 #include "src/gpu/graphite/ContextUtils.h"
12 #include "src/gpu/graphite/PaintParamsKey.h"
13 #include "src/gpu/graphite/Renderer.h"
14 #include "src/gpu/graphite/ShaderCodeDictionary.h"
15 #include "src/gpu/graphite/UniformManager.h"
16 #include "src/sksl/SkSLString.h"
17 #include "src/sksl/SkSLUtil.h"
18 
19 using namespace skia_private;
20 
21 namespace skgpu::graphite {
22 
23 namespace {
24 
get_uniform_header(int bufferID,const char * name)25 std::string get_uniform_header(int bufferID, const char* name) {
26     std::string result;
27 
28     SkSL::String::appendf(&result, "layout (binding=%d) uniform %sUniforms {\n", bufferID, name);
29 
30     return result;
31 }
32 
get_uniforms(UniformOffsetCalculator * offsetter,SkSpan<const Uniform> uniforms,int manglingSuffix,bool * wrotePaintColor)33 std::string get_uniforms(UniformOffsetCalculator* offsetter,
34                          SkSpan<const Uniform> uniforms,
35                          int manglingSuffix,
36                          bool* wrotePaintColor) {
37     std::string result;
38     std::string uniformName;
39     for (const Uniform& u : uniforms) {
40         uniformName = u.name();
41 
42         if (u.isPaintColor() && wrotePaintColor) {
43             if (*wrotePaintColor) {
44                 SkSL::String::appendf(&result, "    // deduplicated %s\n", u.name());
45                 continue;
46             }
47 
48             *wrotePaintColor = true;
49         } else {
50             if (manglingSuffix >= 0) {
51                 uniformName.append("_");
52                 uniformName.append(std::to_string(manglingSuffix));
53             }
54         }
55 
56         SkSL::String::appendf(&result,
57                               "    layout(offset=%d) %s %s",
58                               offsetter->advanceOffset(u.type(), u.count()),
59                               SkSLTypeString(u.type()),
60                               uniformName.c_str());
61         if (u.count()) {
62             result.append("[");
63             result.append(std::to_string(u.count()));
64             result.append("]");
65         }
66         result.append(";\n");
67     }
68 
69     return result;
70 }
71 
get_node_uniforms(UniformOffsetCalculator * offsetter,const ShaderNode * node,bool * wrotePaintColor)72 std::string get_node_uniforms(UniformOffsetCalculator* offsetter,
73                               const ShaderNode* node,
74                               bool* wrotePaintColor) {
75     std::string result;
76     SkSpan<const Uniform> uniforms = node->entry()->fUniforms;
77 
78     if (!uniforms.empty()) {
79         if (node->entry()->fUniformStructName) {
80             auto substruct = UniformOffsetCalculator::ForStruct(offsetter->layout());
81             for (const Uniform& u : uniforms) {
82                 substruct.advanceOffset(u.type(), u.count());
83             }
84 
85             const int structOffset = offsetter->advanceStruct(substruct);
86             SkSL::String::appendf(&result,
87                                   "layout(offset=%d) %s node_%d;",
88                                   structOffset,
89                                   node->entry()->fUniformStructName,
90                                   node->keyIndex());
91         } else {
92             SkSL::String::appendf(&result, "// %d - %s uniforms\n",
93                                   node->keyIndex(), node->entry()->fName);
94             result += get_uniforms(offsetter, uniforms, node->keyIndex(), wrotePaintColor);
95         }
96     }
97 
98     for (const ShaderNode* child : node->children()) {
99         result += get_node_uniforms(offsetter, child, wrotePaintColor);
100     }
101     return result;
102 }
103 
get_ssbo_fields(SkSpan<const Uniform> uniforms,int manglingSuffix,bool * wrotePaintColor)104 std::string get_ssbo_fields(SkSpan<const Uniform> uniforms,
105                             int manglingSuffix,
106                             bool* wrotePaintColor) {
107     std::string result;
108 
109     std::string uniformName;
110     for (const Uniform& u : uniforms) {
111         uniformName = u.name();
112 
113         if (u.isPaintColor() && wrotePaintColor) {
114             if (*wrotePaintColor) {
115                 SkSL::String::appendf(&result, "    // deduplicated %s\n", u.name());
116                 continue;
117             }
118 
119             *wrotePaintColor = true;
120         } else {
121             if (manglingSuffix >= 0) {
122                 uniformName.append("_");
123                 uniformName.append(std::to_string(manglingSuffix));
124             }
125         }
126 
127         SkSL::String::appendf(&result, "    %s %s", SkSLTypeString(u.type()), uniformName.c_str());
128         if (u.count()) {
129             SkSL::String::appendf(&result, "[%d]", u.count());
130         }
131         result.append(";\n");
132     }
133 
134     return result;
135 }
136 
get_node_ssbo_fields(const ShaderNode * node,bool * wrotePaintColor)137 std::string get_node_ssbo_fields(const ShaderNode* node, bool* wrotePaintColor) {
138     std::string result;
139     SkSpan<const Uniform> uniforms = node->entry()->fUniforms;
140 
141     if (!uniforms.empty()) {
142         if (node->entry()->fUniformStructName) {
143             SkSL::String::appendf(&result, "%s node_%d;",
144                                   node->entry()->fUniformStructName, node->keyIndex());
145         } else {
146             SkSL::String::appendf(&result, "// %d - %s uniforms\n",
147                                   node->keyIndex(), node->entry()->fName);
148 
149             result += get_ssbo_fields(uniforms, node->keyIndex(), wrotePaintColor);
150         }
151     }
152 
153     for (const ShaderNode* child : node->children()) {
154         result += get_node_ssbo_fields(child, wrotePaintColor);
155     }
156     return result;
157 }
158 
emit_intrinsic_uniforms(int bufferID,Layout layout)159 std::string emit_intrinsic_uniforms(int bufferID, Layout layout) {
160     auto offsetter = UniformOffsetCalculator::ForTopLevel(layout);
161 
162     std::string result = get_uniform_header(bufferID, "Intrinsic");
163     result += get_uniforms(&offsetter, kIntrinsicUniforms, -1, /* wrotePaintColor= */ nullptr);
164     result.append("};\n\n");
165 
166     SkASSERTF(result.find('[') == std::string::npos,
167               "Arrays are not supported in intrinsic uniforms");
168 
169     return result;
170 }
171 
emit_paint_params_uniforms(int bufferID,const Layout layout,SkSpan<const ShaderNode * > nodes,bool * hasUniforms,bool * wrotePaintColor)172 std::string emit_paint_params_uniforms(int bufferID,
173                                        const Layout layout,
174                                        SkSpan<const ShaderNode*> nodes,
175                                        bool* hasUniforms,
176                                        bool* wrotePaintColor) {
177     auto offsetter = UniformOffsetCalculator::ForTopLevel(layout);
178 
179     std::string result = get_uniform_header(bufferID, "FS");
180     for (const ShaderNode* n : nodes) {
181         result += get_node_uniforms(&offsetter, n, wrotePaintColor);
182     }
183     result.append("};\n\n");
184 
185     *hasUniforms = offsetter.size() > 0;
186     if (!*hasUniforms) {
187         // No uniforms were added
188         return {};
189     }
190 
191     return result;
192 }
193 
emit_render_step_uniforms(int bufferID,const Layout layout,SkSpan<const Uniform> uniforms)194 std::string emit_render_step_uniforms(int bufferID,
195                                       const Layout layout,
196                                       SkSpan<const Uniform> uniforms) {
197     auto offsetter = UniformOffsetCalculator::ForTopLevel(layout);
198 
199     std::string result = get_uniform_header(bufferID, "Step");
200     result += get_uniforms(&offsetter, uniforms, -1, /* wrotePaintColor= */ nullptr);
201     result.append("};\n\n");
202 
203     return result;
204 }
205 
emit_paint_params_storage_buffer(int bufferID,SkSpan<const ShaderNode * > nodes,bool * hasUniforms,bool * wrotePaintColor)206 std::string emit_paint_params_storage_buffer(int bufferID,
207                                              SkSpan<const ShaderNode*> nodes,
208                                              bool* hasUniforms,
209                                              bool* wrotePaintColor) {
210     *hasUniforms = false;
211 
212     std::string fields;
213     for (const ShaderNode* n : nodes) {
214         fields += get_node_ssbo_fields(n, wrotePaintColor);
215     }
216 
217     if (fields.empty()) {
218         // No uniforms were added
219         *hasUniforms = false;
220         return {};
221     }
222 
223     *hasUniforms = true;
224     return SkSL::String::printf(
225             "struct FSUniformData {\n"
226                 "%s\n"
227             "};\n\n"
228             "layout (binding=%d) readonly buffer FSUniforms {\n"
229                 "FSUniformData fsUniformData[];\n"
230             "};\n",
231             fields.c_str(),
232             bufferID);
233 }
234 
emit_render_step_storage_buffer(int bufferID,SkSpan<const Uniform> uniforms)235 std::string emit_render_step_storage_buffer(int bufferID, SkSpan<const Uniform> uniforms) {
236     SkASSERT(!uniforms.empty());
237     std::string fields = get_ssbo_fields(uniforms, -1, /*wrotePaintColor=*/nullptr);
238     return SkSL::String::printf(
239             "struct StepUniformData {\n"
240             "%s\n"
241             "};\n\n"
242             "layout (binding=%d) readonly buffer StepUniforms {\n"
243             "    StepUniformData stepUniformData[];\n"
244             "};\n",
245             fields.c_str(),
246             bufferID);
247 }
248 
emit_uniforms_from_storage_buffer(const char * bufferNamePrefix,const char * ssboIndex,SkSpan<const Uniform> uniforms)249 std::string emit_uniforms_from_storage_buffer(const char* bufferNamePrefix,
250                                               const char* ssboIndex,
251                                               SkSpan<const Uniform> uniforms) {
252     std::string result;
253 
254     for (const Uniform& u : uniforms) {
255         SkSL::String::appendf(&result, "%s %s", SkSLTypeString(u.type()), u.name());
256         if (u.count()) {
257             SkSL::String::appendf(&result, "[%d]", u.count());
258         }
259         SkSL::String::appendf(
260                 &result, " = %sUniformData[%s].%s;\n", bufferNamePrefix, ssboIndex, u.name());
261     }
262 
263     return result;
264 }
265 
append_sampler_descs(const SkSpan<const uint32_t> samplerData,skia_private::TArray<SamplerDesc> & outDescs)266 void append_sampler_descs(const SkSpan<const uint32_t> samplerData,
267                           skia_private::TArray<SamplerDesc>& outDescs) {
268     // Sampler data consists of variable-length SamplerDesc representations which can differ based
269     // upon a sampler's immutability and format. For this reason, handle incrementing i in the loop.
270     for (size_t i = 0; i < samplerData.size();) {
271         // Create a default-initialized SamplerDesc (which only takes up one uint32). If we are
272         // using a dynamic sampler, this will be directly inserted into outDescs. Otherwise, it will
273         // be populated with actual immutable sampler data and then inserted.
274         SamplerDesc desc{};
275         size_t samplerDescLength = 1;
276         SkASSERT(desc.asSpan().size() == samplerDescLength);
277 
278         // Isolate the ImmutableSamplerInfo portion of the SamplerDesc represented by samplerData.
279         // If immutableSamplerInfo is non-zero, that means we are using an immutable sampler.
280         uint32_t immutableSamplerInfo = samplerData[i] >> SamplerDesc::kImmutableSamplerInfoShift;
281         if (immutableSamplerInfo != 0) {
282             // Consult the first bit of immutableSamplerInfo which tells us whether the sampler uses
283             // a known or external format. With this, update sampler description length.
284             bool usesExternalFormat = immutableSamplerInfo & 0b1;
285             samplerDescLength = usesExternalFormat ? SamplerDesc::kInt32sNeededExternalFormat
286                                                    : SamplerDesc::kInt32sNeededKnownFormat;
287             // Populate a SamplerDesc with samplerDescLength quantity of immutable sampler data
288             memcpy(&desc, samplerData.begin() + i, samplerDescLength * sizeof(uint32_t));
289         }
290         outDescs.push_back(desc);
291         i += samplerDescLength;
292     }
293 }
294 
get_node_texture_samplers(const ResourceBindingRequirements & bindingReqs,const ShaderNode * node,int * binding,skia_private::TArray<SamplerDesc> * outDescs)295 std::string get_node_texture_samplers(const ResourceBindingRequirements& bindingReqs,
296                                       const ShaderNode* node,
297                                       int* binding,
298                                       skia_private::TArray<SamplerDesc>* outDescs) {
299     std::string result;
300     SkSpan<const TextureAndSampler> samplers = node->entry()->fTexturesAndSamplers;
301 
302     if (!samplers.empty()) {
303         SkSL::String::appendf(&result, "// %d - %s samplers\n",
304                               node->keyIndex(), node->entry()->fName);
305 
306         // Determine whether we need to analyze & interpret a ShaderNode's data as immutable
307         // SamplerDescs based upon whether:
308         // 1) A backend passes in a non-nullptr outImmutableSamplers param (may be nullptr in
309         //    backends or circumstances where we know immutable sampler data is never stored)
310         // 2) Any data is stored on the ShaderNode
311         // 3) Whether the ShaderNode snippet's ID matches that of any snippet ID that could store
312         //    immutable sampler data.
313         int32_t snippetId = node->codeSnippetId();
314         if (outDescs) {
315             // TODO(b/369846881): Refactor checking snippet ID to instead having a named
316             // snippet requirement flag that we can check here to decrease fragility.
317             if (!node->data().empty() &&
318                 (snippetId == static_cast<int32_t>(BuiltInCodeSnippetID::kImageShader) ||
319                  snippetId == static_cast<int32_t>(BuiltInCodeSnippetID::kCubicImageShader) ||
320                  snippetId == static_cast<int32_t>(BuiltInCodeSnippetID::kHWImageShader))) {
321                 append_sampler_descs(node->data(), *outDescs);
322             } else {
323                 // Add default SamplerDescs for any dynamic samplers to outDescs.
324                 outDescs->push_back_n(samplers.size());
325             }
326         }
327 
328         for (const TextureAndSampler& t : samplers) {
329             result += EmitSamplerLayout(bindingReqs, binding);
330             SkSL::String::appendf(&result, " sampler2D %s_%d;\n", t.name(), node->keyIndex());
331         }
332     }
333 
334     for (const ShaderNode* child : node->children()) {
335         result += get_node_texture_samplers(bindingReqs, child, binding, outDescs);
336     }
337     return result;
338 }
339 
emit_textures_and_samplers(const ResourceBindingRequirements & bindingReqs,SkSpan<const ShaderNode * > nodes,int * binding,skia_private::TArray<SamplerDesc> * outDescs)340 std::string emit_textures_and_samplers(const ResourceBindingRequirements& bindingReqs,
341                                        SkSpan<const ShaderNode*> nodes,
342                                        int* binding,
343                                        skia_private::TArray<SamplerDesc>* outDescs) {
344     std::string result;
345     for (const ShaderNode* n : nodes) {
346         result += get_node_texture_samplers(bindingReqs, n, binding, outDescs);
347     }
348     return result;
349 }
350 
emit_varyings(const RenderStep * step,const char * direction,bool emitSsboIndicesVarying,bool emitLocalCoordsVarying)351 std::string emit_varyings(const RenderStep* step,
352                           const char* direction,
353                           bool emitSsboIndicesVarying,
354                           bool emitLocalCoordsVarying) {
355     std::string result;
356     int location = 0;
357 
358     auto appendVarying = [&](const Varying& v) {
359         const char* interpolation;
360         switch (v.interpolation()) {
361             case Interpolation::kPerspective: interpolation = ""; break;
362             case Interpolation::kLinear:      interpolation = "noperspective "; break;
363             case Interpolation::kFlat:        interpolation = "flat "; break;
364         }
365         SkSL::String::appendf(&result, "layout(location=%d) %s %s%s %s;\n",
366                               location++,
367                               direction,
368                               interpolation,
369                               SkSLTypeString(v.gpuType()),
370                               v.name());
371     };
372 
373     if (emitSsboIndicesVarying) {
374         appendVarying({RenderStep::ssboIndicesVarying(), SkSLType::kUInt2});
375     }
376 
377     if (emitLocalCoordsVarying) {
378         appendVarying({"localCoordsVar", SkSLType::kFloat2});
379     }
380 
381     for (auto v : step->varyings()) {
382         appendVarying(v);
383     }
384 
385     return result;
386 }
387 
388 // Walk the node tree and generate all preambles, accumulating into 'preamble'.
emit_preambles(const ShaderInfo & shaderInfo,SkSpan<const ShaderNode * > nodes,std::string treeLabel,std::string * preamble)389 void emit_preambles(const ShaderInfo& shaderInfo,
390                     SkSpan<const ShaderNode*> nodes,
391                     std::string treeLabel,
392                     std::string* preamble) {
393     for (int i = 0; i < SkTo<int>(nodes.size()); ++i) {
394         const ShaderNode* node = nodes[i];
395         std::string nodeLabel = std::to_string(i);
396         std::string nextLabel = treeLabel.empty() ? nodeLabel : (treeLabel + "<-" + nodeLabel);
397 
398         if (node->numChildren() > 0) {
399             emit_preambles(shaderInfo, node->children(), nextLabel, preamble);
400         }
401 
402         std::string nodePreamble = node->entry()->fPreambleGenerator
403                                            ? node->entry()->fPreambleGenerator(shaderInfo, node)
404                                            : node->generateDefaultPreamble(shaderInfo);
405         if (!nodePreamble.empty()) {
406             SkSL::String::appendf(preamble,
407                                   "// [%d]   %s: %s\n"
408                                   "%s\n",
409                                   node->keyIndex(),
410                                   nextLabel.c_str(),
411                                   node->entry()->fName,
412                                   nodePreamble.c_str());
413         }
414     }
415 }
416 
emit_color_output(BlendFormula::OutputType outputType,const char * outColor,const char * inColor)417 std::string emit_color_output(BlendFormula::OutputType outputType,
418                               const char* outColor,
419                               const char* inColor) {
420     switch (outputType) {
421         case BlendFormula::kNone_OutputType:
422             return SkSL::String::printf("%s = half4(0.0);", outColor);
423 
424         case BlendFormula::kCoverage_OutputType:
425             return SkSL::String::printf("%s = outputCoverage;", outColor);
426 
427         case BlendFormula::kModulate_OutputType:
428             return SkSL::String::printf("%s = %s * outputCoverage;", outColor, inColor);
429 
430         case BlendFormula::kSAModulate_OutputType:
431             return SkSL::String::printf("%s = %s.a * outputCoverage;", outColor, inColor);
432 
433         case BlendFormula::kISAModulate_OutputType:
434             return SkSL::String::printf("%s = (1.0 - %s.a) * outputCoverage;", outColor, inColor);
435 
436         case BlendFormula::kISCModulate_OutputType:
437             return SkSL::String::printf(
438                     "%s = (half4(1.0) - %s) * outputCoverage;", outColor, inColor);
439 
440         default:
441             SkUNREACHABLE;
442     }
443 }
444 
make_simple_blendInfo(skgpu::BlendCoeff srcCoeff,skgpu::BlendCoeff dstCoeff)445 constexpr skgpu::BlendInfo make_simple_blendInfo(skgpu::BlendCoeff srcCoeff,
446                                                  skgpu::BlendCoeff dstCoeff) {
447     return { skgpu::BlendEquation::kAdd,
448              srcCoeff,
449              dstCoeff,
450              SK_PMColor4fTRANSPARENT,
451              skgpu::BlendModifiesDst(skgpu::BlendEquation::kAdd, srcCoeff, dstCoeff) };
452 }
453 
454 static constexpr int kNumCoeffModes = (int)SkBlendMode::kLastCoeffMode + 1;
455 static constexpr skgpu::BlendInfo gBlendTable[kNumCoeffModes] = {
456         /* clear */      make_simple_blendInfo(skgpu::BlendCoeff::kZero, skgpu::BlendCoeff::kZero),
457         /* src */        make_simple_blendInfo(skgpu::BlendCoeff::kOne,  skgpu::BlendCoeff::kZero),
458         /* dst */        make_simple_blendInfo(skgpu::BlendCoeff::kZero, skgpu::BlendCoeff::kOne),
459         /* src-over */   make_simple_blendInfo(skgpu::BlendCoeff::kOne,  skgpu::BlendCoeff::kISA),
460         /* dst-over */   make_simple_blendInfo(skgpu::BlendCoeff::kIDA,  skgpu::BlendCoeff::kOne),
461         /* src-in */     make_simple_blendInfo(skgpu::BlendCoeff::kDA,   skgpu::BlendCoeff::kZero),
462         /* dst-in */     make_simple_blendInfo(skgpu::BlendCoeff::kZero, skgpu::BlendCoeff::kSA),
463         /* src-out */    make_simple_blendInfo(skgpu::BlendCoeff::kIDA,  skgpu::BlendCoeff::kZero),
464         /* dst-out */    make_simple_blendInfo(skgpu::BlendCoeff::kZero, skgpu::BlendCoeff::kISA),
465         /* src-atop */   make_simple_blendInfo(skgpu::BlendCoeff::kDA,   skgpu::BlendCoeff::kISA),
466         /* dst-atop */   make_simple_blendInfo(skgpu::BlendCoeff::kIDA,  skgpu::BlendCoeff::kSA),
467         /* xor */        make_simple_blendInfo(skgpu::BlendCoeff::kIDA,  skgpu::BlendCoeff::kISA),
468         /* plus */       make_simple_blendInfo(skgpu::BlendCoeff::kOne,  skgpu::BlendCoeff::kOne),
469         /* modulate */   make_simple_blendInfo(skgpu::BlendCoeff::kZero, skgpu::BlendCoeff::kSC),
470         /* screen */     make_simple_blendInfo(skgpu::BlendCoeff::kOne,  skgpu::BlendCoeff::kISC)
471 };
472 
473 }  // anonymous namespace
474 
Make(const Caps * caps,const ShaderCodeDictionary * dict,const RuntimeEffectDictionary * rteDict,const RenderStep * step,UniquePaintParamsID paintID,bool useStorageBuffers,skgpu::Swizzle writeSwizzle,skia_private::TArray<SamplerDesc> * outDescs)475 std::unique_ptr<ShaderInfo> ShaderInfo::Make(const Caps* caps,
476                                              const ShaderCodeDictionary* dict,
477                                              const RuntimeEffectDictionary* rteDict,
478                                              const RenderStep* step,
479                                              UniquePaintParamsID paintID,
480                                              bool useStorageBuffers,
481                                              skgpu::Swizzle writeSwizzle,
482                                              skia_private::TArray<SamplerDesc>* outDescs) {
483     const char* shadingSsboIndex =
484             useStorageBuffers && step->performsShading() ? "shadingSsboIndex" : nullptr;
485     std::unique_ptr<ShaderInfo> result =
486             std::unique_ptr<ShaderInfo>(new ShaderInfo(rteDict, shadingSsboIndex));
487 
488     // The fragment shader must be generated before the vertex shader, because we determine
489     // properties of the entire program while generating the fragment shader.
490 
491     // If paintID is not valid this is a depth-only draw and there's no fragment shader to compile.
492     if (paintID.isValid()) {
493         result->generateFragmentSkSL(caps,
494                                      dict,
495                                      step,
496                                      paintID,
497                                      useStorageBuffers,
498                                      writeSwizzle,
499                                      outDescs);
500     }
501 
502     result->generateVertexSkSL(caps,
503                                step,
504                                useStorageBuffers);
505 
506     return result;
507 }
508 
ShaderInfo(const RuntimeEffectDictionary * rteDict,const char * ssboIndex)509 ShaderInfo::ShaderInfo(const RuntimeEffectDictionary* rteDict, const char* ssboIndex)
510         : fRuntimeEffectDictionary(rteDict), fSsboIndex(ssboIndex) {}
511 
512 // The current, incomplete, model for shader construction is:
513 //   - Static code snippets (which can have an arbitrary signature) live in the Graphite
514 //     pre-compiled modules, which are located at `src/sksl/sksl_graphite_frag.sksl` and
515 //     `src/sksl/sksl_graphite_frag_es2.sksl`.
516 //   - Glue code is generated in a `main` method which calls these static code snippets.
517 //     The glue code is responsible for:
518 //            1) gathering the correct (mangled) uniforms
519 //            2) passing the uniforms and any other parameters to the helper method
520 //   - The result of the final code snippet is then copied into "sk_FragColor".
521 //   Note: each entry's 'fStaticFunctionName' field is expected to match the name of a function
522 //   in the Graphite pre-compiled module, or be null if the preamble and expression generators are
523 //   overridden to not use a static function.
generateFragmentSkSL(const Caps * caps,const ShaderCodeDictionary * dict,const RenderStep * step,UniquePaintParamsID paintID,bool useStorageBuffers,Swizzle writeSwizzle,skia_private::TArray<SamplerDesc> * outDescs)524 void ShaderInfo::generateFragmentSkSL(const Caps* caps,
525                                       const ShaderCodeDictionary* dict,
526                                       const RenderStep* step,
527                                       UniquePaintParamsID paintID,
528                                       bool useStorageBuffers,
529                                       Swizzle writeSwizzle,
530                                       skia_private::TArray<SamplerDesc>* outDescs) {
531     PaintParamsKey key = dict->lookup(paintID);
532     SkASSERT(key.isValid());  // invalid keys should have been caught by invalid paint ID earlier
533 
534     std::string label = key.toString(dict, /*includeData=*/false).c_str();
535     fRootNodes = key.getRootNodes(dict, &fShaderNodeAlloc);
536 
537     // TODO(b/366220690): aggregateSnippetData() goes away entirely once the VulkanGraphicsPipeline
538     // is updated to use the extracted SamplerDescs directly.
539     for (const ShaderNode* root : fRootNodes) {
540         this->aggregateSnippetData(root);
541     }
542 
543 #if defined(SK_DEBUG)
544     // Validate the root node structure of the key.
545     SkASSERT(fRootNodes.size() == 2 || fRootNodes.size() == 3);
546     // First node produces the source color (all snippets return a half4), so we just require that
547     // its signature takes no extra args or just local coords.
548     const ShaderSnippet* srcSnippet = dict->getEntry(fRootNodes[0]->codeSnippetId());
549     // TODO(b/349997190): Once SkEmptyShader doesn't use the passthrough snippet, we can assert
550     // that srcSnippet->needsPriorStageOutput() is false.
551     SkASSERT(!srcSnippet->needsBlenderDstColor());
552     // Second node is the final blender, so it must take both the src color and dst color, and not
553     // any local coordinate.
554     const ShaderSnippet* blendSnippet = dict->getEntry(fRootNodes[1]->codeSnippetId());
555     SkASSERT(blendSnippet->needsPriorStageOutput() && blendSnippet->needsBlenderDstColor());
556     SkASSERT(!blendSnippet->needsLocalCoords());
557 
558     const ShaderSnippet* clipSnippet =
559             fRootNodes.size() > 2 ? dict->getEntry(fRootNodes[2]->codeSnippetId()) : nullptr;
560     SkASSERT(!clipSnippet ||
561              (!clipSnippet->needsPriorStageOutput() && !clipSnippet->needsBlenderDstColor()));
562 #endif
563 
564     // The RenderStep should be performing shading since otherwise there's no need to generate a
565     // fragment shader program at all.
566     SkASSERT(step->performsShading());
567     // TODO(b/372912880): Release assert debugging for illegal instruction occurring in the wild.
568     SkASSERTF_RELEASE(step->performsShading(),
569                       "render step: %s, label: %s",
570                       step->name(),
571                       label.c_str());
572 
573     // Extract the root nodes for clarity
574     // TODO(b/372912880): Release assert debugging for illegal instruction occurring in the wild.
575     SkASSERTF_RELEASE(fRootNodes.size() == 2 || fRootNodes.size() == 3,
576                       "root node size = %zu, label = %s",
577                       fRootNodes.size(),
578                       label.c_str());
579     const ShaderNode* const srcColorRoot = fRootNodes[0];
580     const ShaderNode* const finalBlendRoot = fRootNodes[1];
581     const ShaderNode* const clipRoot = fRootNodes.size() > 2 ? fRootNodes[2] : nullptr;
582 
583     // Determine the algorithm for final blending: direct HW blending, coverage-modified HW
584     // blending (w/ or w/o dual-source blending) or via dst-read requirement.
585     Coverage finalCoverage = step->coverage();
586     if (finalCoverage == Coverage::kNone && SkToBool(clipRoot)) {
587         finalCoverage = Coverage::kSingleChannel;
588     }
589     std::optional<SkBlendMode> finalBlendMode;
590     if (finalBlendRoot->codeSnippetId() < kBuiltInCodeSnippetIDCount &&
591         finalBlendRoot->codeSnippetId() >= kFixedBlendIDOffset) {
592         finalBlendMode =
593                 static_cast<SkBlendMode>(finalBlendRoot->codeSnippetId() - kFixedBlendIDOffset);
594         if (*finalBlendMode > SkBlendMode::kLastCoeffMode) {
595             // TODO(b/239726010): When we support advanced blend modes in HW, these modes could
596             // still be handled by fBlendInfo instead of SkSL
597             finalBlendMode.reset();
598         }
599     }
600     fDstReadRequirement = GetDstReadRequirement(caps, finalBlendMode, finalCoverage);
601     // TODO(b/372912880): Release assert debugging for illegal instruction occurring in the wild.
602     SkASSERTF_RELEASE(finalBlendMode.has_value() ||
603                       fDstReadRequirement != DstReadRequirement::kNone,
604                       "blend mode: %d, dst read: %d, coverage: %d, label = %s",
605                       finalBlendMode.has_value() ? (int)*finalBlendMode : -1,
606                       (int) fDstReadRequirement,
607                       (int) finalCoverage,
608                       label.c_str());
609 
610     const bool hasStepUniforms = step->numUniforms() > 0 && step->coverage() != Coverage::kNone;
611     const bool useStepStorageBuffer = useStorageBuffers && hasStepUniforms;
612     const bool useShadingStorageBuffer = useStorageBuffers && step->performsShading();
613 
614     auto allReqFlags = srcColorRoot->requiredFlags() | finalBlendRoot->requiredFlags();
615     if (clipRoot) {
616         allReqFlags |= clipRoot->requiredFlags();
617     }
618     const bool useGradientStorageBuffer = caps->gradientBufferSupport() &&
619                                           (allReqFlags & SnippetRequirementFlags::kGradientBuffer);
620     const bool useDstSampler = fDstReadRequirement == DstReadRequirement::kTextureCopy ||
621                                fDstReadRequirement == DstReadRequirement::kTextureSample;
622 
623     const bool defineLocalCoordsVarying = this->needsLocalCoords();
624     std::string preamble = emit_varyings(step,
625                                          /*direction=*/"in",
626                                          /*emitSsboIndicesVarying=*/useShadingStorageBuffer,
627                                          defineLocalCoordsVarying);
628 
629     // The uniforms are mangled by having their index in 'fEntries' as a suffix (i.e., "_%d")
630     const ResourceBindingRequirements& bindingReqs = caps->resourceBindingRequirements();
631     preamble += emit_intrinsic_uniforms(bindingReqs.fIntrinsicBufferBinding,
632                                         bindingReqs.fUniformBufferLayout);
633     if (hasStepUniforms) {
634         if (useStepStorageBuffer) {
635             preamble += emit_render_step_storage_buffer(bindingReqs.fRenderStepBufferBinding,
636                                                         step->uniforms());
637         } else {
638             preamble += emit_render_step_uniforms(bindingReqs.fRenderStepBufferBinding,
639                                                   bindingReqs.fUniformBufferLayout,
640                                                   step->uniforms());
641         }
642     }
643 
644     bool wrotePaintColor = false;
645     if (useShadingStorageBuffer) {
646         preamble += emit_paint_params_storage_buffer(bindingReqs.fPaintParamsBufferBinding,
647                                                      fRootNodes,
648                                                      &fHasPaintUniforms,
649                                                      &wrotePaintColor);
650         SkSL::String::appendf(&preamble, "uint %s;\n", this->ssboIndex());
651     } else {
652         preamble += emit_paint_params_uniforms(bindingReqs.fPaintParamsBufferBinding,
653                                                bindingReqs.fUniformBufferLayout,
654                                                fRootNodes,
655                                                &fHasPaintUniforms,
656                                                &wrotePaintColor);
657     }
658 
659     if (useGradientStorageBuffer) {
660         SkSL::String::appendf(&preamble,
661                               "layout (binding=%d) readonly buffer FSGradientBuffer {\n"
662                               "    float %s[];\n"
663                               "};\n",
664                               bindingReqs.fGradientBufferBinding,
665                               ShaderInfo::kGradientBufferName);
666         fHasGradientBuffer = true;
667     }
668 
669     {
670         int binding = 0;
671         preamble += emit_textures_and_samplers(bindingReqs, fRootNodes, &binding, outDescs);
672         int paintTextureCount = binding;
673         if (step->hasTextures()) {
674             preamble += step->texturesAndSamplersSkSL(bindingReqs, &binding);
675             if (outDescs) {
676                 // Determine how many render step samplers were used by comparing the binding value
677                 // against paintTextureCount, taking into account the binding requirements. We
678                 // assume and do not anticipate the render steps to use immutable samplers.
679                 int renderStepSamplerCount = bindingReqs.fSeparateTextureAndSamplerBinding
680                                                      ? (binding - paintTextureCount) / 2
681                                                      : binding - paintTextureCount;
682                 // Add default SamplerDescs for all the dynamic samplers used by the render step so
683                 // the size of outDescs will be equivalent to the total number of samplers.
684                 outDescs->push_back_n(renderStepSamplerCount);
685             }
686         }
687         if (useDstSampler) {
688             preamble += EmitSamplerLayout(bindingReqs, &binding);
689             preamble += " sampler2D dstSampler;";
690             // Add default SamplerDesc for the intrinsic dstSampler to stay consistent with
691             // `fNumFragmentTexturesAndSamplers`.
692             if (outDescs) {
693                 outDescs->push_back({});
694             }
695         }
696 
697         // Record how many textures and samplers are used.
698         fNumFragmentTexturesAndSamplers = binding;
699     }
700 
701     // Emit preamble declarations and helper functions required for snippets. In the default case
702     // this adds functions that bind a node's specific mangled uniforms to the snippet's
703     // implementation in the SkSL modules.
704     emit_preambles(*this, fRootNodes, /*treeLabel=*/"", &preamble);
705 
706     std::string mainBody = "void main() {";
707 
708     if (useShadingStorageBuffer) {
709         SkSL::String::appendf(&mainBody,
710                               "%s = %s.y;\n",
711                               this->ssboIndex(),
712                               RenderStep::ssboIndicesVarying());
713     }
714 
715     if (step->emitsPrimitiveColor()) {
716         mainBody += "half4 primitiveColor;";
717         mainBody += step->fragmentColorSkSL();
718     } else {
719         SkASSERT(!(fRootNodes[0]->requiredFlags() & SnippetRequirementFlags::kPrimitiveColor));
720     }
721 
722     // Using kDefaultArgs as the initial value means it will refer to undefined variables, but the
723     // root nodes should--at most--be depending on the coordinate when "needsLocalCoords" is true.
724     // If the PaintParamsKey violates that structure, this will produce SkSL compile errors.
725     ShaderSnippet::Args args = ShaderSnippet::kDefaultArgs;
726     args.fFragCoord = "localCoordsVar";  // the varying added in emit_varyings()
727     // TODO(b/349997190): The paint root node should not depend on any prior stage's output, but
728     // it can happen with how SkEmptyShader is currently mapped to `sk_passthrough`. In this case
729     // it requires that prior stage color to be transparent black. When SkEmptyShader can instead
730     // cause the draw to be skipped, this can go away.
731     args.fPriorStageOutput = "half4(0)";
732 
733     // Calculate the src color and stash its output variable in `args`
734     args.fPriorStageOutput = srcColorRoot->invokeAndAssign(*this, args, &mainBody);
735 
736     if (fDstReadRequirement != DstReadRequirement::kNone) {
737         // Get the current dst color into a local variable, it may be used later on for coverage
738         // blending as well as the final blend.
739         mainBody += "half4 dstColor;";
740         if (useDstSampler) {
741             // dstCopyBounds is in frag coords and already includes the replay translation. The
742             // reciprocol of the dstCopy dimensions are in ZW.
743             mainBody += "dstColor = sample(dstSampler,"
744                                           "dstCopyBounds.zw*(sk_FragCoord.xy - dstCopyBounds.xy));";
745         } else {
746             SkASSERT(fDstReadRequirement == DstReadRequirement::kFramebufferFetch);
747             mainBody += "dstColor = sk_LastFragColor;";
748         }
749 
750         args.fBlenderDstColor = "dstColor";
751         args.fPriorStageOutput = finalBlendRoot->invokeAndAssign(*this, args, &mainBody);
752         finalBlendMode = SkBlendMode::kSrc;
753     }
754 
755     if (writeSwizzle != Swizzle::RGBA()) {
756         SkSL::String::appendf(&mainBody, "%s = %s.%s;", args.fPriorStageOutput.c_str(),
757                                                         args.fPriorStageOutput.c_str(),
758                                                         writeSwizzle.asString().c_str());
759     }
760 
761     if (finalCoverage == Coverage::kNone) {
762         // Either direct HW blending or a dst-read w/o any extra coverage. In both cases we just
763         // need to assign directly to sk_FragCoord and update the HW blend info to finalBlendMode.
764         SkASSERT(finalBlendMode.has_value());
765         // TODO(b/372912880): Release assert debugging for illegal instruction occurring in the wild
766         SkASSERTF_RELEASE(finalBlendMode.has_value(),
767                           "blend mode: %d, dst read: %d, label = %s",
768                           finalBlendMode.has_value() ? (int)*finalBlendMode : -1,
769                           (int) fDstReadRequirement,
770                           label.c_str());
771 
772         fBlendInfo = gBlendTable[static_cast<int>(*finalBlendMode)];
773         SkSL::String::appendf(&mainBody, "sk_FragColor = %s;", args.fPriorStageOutput.c_str());
774     } else {
775         // Accumulate the output coverage. This will either modify the src color and secondary
776         // outputs for dual-source blending, or be combined directly with the in-shader blended
777         // final color if a dst-readback was required.
778         if (useStepStorageBuffer) {
779             SkSL::String::appendf(&mainBody,
780                                   "uint stepSsboIndex = %s.x;\n",
781                                   RenderStep::ssboIndicesVarying());
782             mainBody +=
783                     emit_uniforms_from_storage_buffer("step", "stepSsboIndex", step->uniforms());
784         }
785 
786         mainBody += "half4 outputCoverage = half4(1);";
787         mainBody += step->fragmentCoverageSkSL();
788 
789         if (clipRoot) {
790             // The clip block node is invoked with device coords, not local coords like the main
791             // shading root node. However sk_FragCoord includes any replay translation and we
792             // need to recover the original device coordinate.
793             mainBody += "float2 devCoord = sk_FragCoord.xy - viewport.xy;";
794             args.fFragCoord = "devCoord";
795             std::string clipBlockOutput = clipRoot->invokeAndAssign(*this, args, &mainBody);
796             SkSL::String::appendf(&mainBody, "outputCoverage *= %s.a;", clipBlockOutput.c_str());
797         }
798 
799         const char* outColor = args.fPriorStageOutput.c_str();
800         if (fDstReadRequirement != DstReadRequirement::kNone) {
801             // If this draw uses a non-coherent dst read, we want to keep the existing dst color (or
802             // whatever has been previously drawn) when there's no coverage. This helps for batching
803             // text draws that need to read from a dst copy for blends. However, this only helps the
804             // case where the outer bounding boxes of each letter overlap and not two actual parts
805             // of the text.
806             if (useDstSampler) {
807                 // We don't think any shaders actually output negative coverage, but just as a
808                 // safety check for floating point precision errors, we compare with <= here. We
809                 // just check the RGB values of the coverage, since the alpha may not have been set
810                 // when using LCD. If we are using single-channel coverage, alpha will be equal to
811                 // RGB anyway.
812                 mainBody +=
813                     "if (all(lessThanEqual(outputCoverage.rgb, half3(0)))) {"
814                         "discard;"
815                     "}";
816             }
817 
818             // Use kSrc HW BlendInfo and do the coverage blend with dst in the shader.
819             fBlendInfo = gBlendTable[static_cast<int>(SkBlendMode::kSrc)];
820             SkSL::String::appendf(
821                     &mainBody,
822                     "sk_FragColor = %s * outputCoverage + dstColor * (1.0 - outputCoverage);",
823                     outColor);
824             if (finalCoverage == Coverage::kLCD) {
825                 SkSL::String::appendf(
826                         &mainBody,
827                         "half3 lerpRGB = mix(dstColor.aaa, %s.aaa, outputCoverage.rgb);"
828                         "sk_FragColor.a = max(max(lerpRGB.r, lerpRGB.g), lerpRGB.b);",
829                         outColor);
830             }
831         } else {
832             // Adjust the shader output(s) to incorporate the coverage so that HW blending produces
833             // the correct output.
834             // TODO: Determine whether draw is opaque and pass that to GetBlendFormula.
835             // TODO(b/372912880): Release assert debugging for illegal instruction
836             SkASSERTF_RELEASE(finalBlendMode.has_value(),
837                               "blend mode: %d, dst read: %d, coverage: %d, label = %s",
838                               finalBlendMode.has_value() ? (int)*finalBlendMode : -1,
839                               (int) fDstReadRequirement,
840                               (int) finalCoverage,
841                               label.c_str());
842             BlendFormula coverageBlendFormula =
843                     finalCoverage == Coverage::kLCD
844                             ? skgpu::GetLCDBlendFormula(*finalBlendMode)
845                             : skgpu::GetBlendFormula(
846                                       /*isOpaque=*/false, /*hasCoverage=*/true, *finalBlendMode);
847             fBlendInfo = {coverageBlendFormula.equation(),
848                           coverageBlendFormula.srcCoeff(),
849                           coverageBlendFormula.dstCoeff(),
850                           SK_PMColor4fTRANSPARENT,
851                           coverageBlendFormula.modifiesDst()};
852 
853             if (finalCoverage == Coverage::kLCD) {
854                 mainBody += "outputCoverage.a = max(max(outputCoverage.r, "
855                                                        "outputCoverage.g), "
856                                                    "outputCoverage.b);";
857             }
858 
859             mainBody += emit_color_output(coverageBlendFormula.primaryOutput(),
860                                           "sk_FragColor",
861                                           outColor);
862             if (coverageBlendFormula.hasSecondaryOutput()) {
863                 SkASSERT(caps->shaderCaps()->fDualSourceBlendingSupport);
864                 mainBody += emit_color_output(coverageBlendFormula.secondaryOutput(),
865                                               "sk_SecondaryFragColor",
866                                               outColor);
867             }
868         }
869     }
870     mainBody += "}\n";
871 
872     fFragmentSkSL = preamble + "\n" + mainBody;
873 
874     fFSLabel = writeSwizzle.asString().c_str();
875     fFSLabel += " + ";
876     fFSLabel = step->name();
877     fFSLabel += " + ";
878     fFSLabel += label;
879 }
880 
generateVertexSkSL(const Caps * caps,const RenderStep * step,bool useStorageBuffers)881 void ShaderInfo::generateVertexSkSL(const Caps* caps,
882                                     const RenderStep* step,
883                                     bool useStorageBuffers) {
884     const bool hasStepUniforms = step->numUniforms() > 0;
885     const bool useStepStorageBuffer = useStorageBuffers && hasStepUniforms;
886     const bool useShadingStorageBuffer = useStorageBuffers && step->performsShading();
887     const bool defineLocalCoordsVarying = this->needsLocalCoords();
888 
889     // Fixed program header (intrinsics are always declared as an uniform interface block)
890     const ResourceBindingRequirements& bindingReqs = caps->resourceBindingRequirements();
891     std::string sksl = emit_intrinsic_uniforms(bindingReqs.fIntrinsicBufferBinding,
892                                                bindingReqs.fUniformBufferLayout);
893 
894     if (step->numVertexAttributes() > 0 || step->numInstanceAttributes() > 0) {
895         int attr = 0;
896         auto add_attrs = [&sksl, &attr](SkSpan<const Attribute> attrs) {
897             for (auto a : attrs) {
898                 SkSL::String::appendf(&sksl, "    layout(location=%d) in ", attr++);
899                 sksl.append(SkSLTypeString(a.gpuType()));
900                 SkSL::String::appendf(&sksl, " %s;\n", a.name());
901             }
902         };
903         if (step->numVertexAttributes() > 0) {
904             sksl.append("// vertex attrs\n");
905             add_attrs(step->vertexAttributes());
906         }
907         if (step->numInstanceAttributes() > 0) {
908             sksl.append("// instance attrs\n");
909             add_attrs(step->instanceAttributes());
910         }
911     }
912 
913     // Uniforms needed by RenderStep
914     // The uniforms are mangled by having their index in 'fEntries' as a suffix (i.e., "_%d")
915     if (hasStepUniforms) {
916         if (useStepStorageBuffer) {
917             sksl += emit_render_step_storage_buffer(bindingReqs.fRenderStepBufferBinding,
918                                                     step->uniforms());
919         } else {
920             sksl += emit_render_step_uniforms(bindingReqs.fRenderStepBufferBinding,
921                                               bindingReqs.fUniformBufferLayout,
922                                               step->uniforms());
923         }
924     }
925 
926     // Varyings needed by RenderStep
927     sksl += emit_varyings(step, "out", useShadingStorageBuffer, defineLocalCoordsVarying);
928 
929     // Vertex shader function declaration
930     sksl += "void main() {";
931     // Create stepLocalCoords which render steps can write to.
932     sksl += "float2 stepLocalCoords = float2(0);";
933     // Vertex shader body
934     if (useStepStorageBuffer) {
935         // Extract out render step uniforms from SSBO, declaring local variables with the expected
936         // uniform names so that RenderStep SkSL is independent of storage choice.
937         SkSL::String::appendf(
938                 &sksl, "uint stepSsboIndex = %s.x;\n", RenderStep::ssboIndicesAttribute());
939         sksl += emit_uniforms_from_storage_buffer("step", "stepSsboIndex", step->uniforms());
940     }
941 
942     sksl += step->vertexSkSL();
943 
944     // We want to map the rectangle of logical device pixels from (0,0) to (viewWidth, viewHeight)
945     // to normalized device coordinates: (-1,-1) to (1,1) (actually -w to w since it's before
946     // homogenous division).
947     //
948     // For efficiency, this assumes viewport.zw holds the reciprocol of twice the viewport width and
949     // height. On some backends the NDC Y axis is flipped relative to the device and
950     // viewport coords (i.e. it points up instead of down). In those cases, it's also assumed that
951     // viewport.w holds a negative value. In that case the sign(viewport.zw) changes from
952     // subtracting w to adding w.
953     sksl += "sk_Position = float4(viewport.zw*devPosition.xy - sign(viewport.zw)*devPosition.ww,"
954             "devPosition.zw);";
955 
956     if (useShadingStorageBuffer) {
957         // Assign SSBO index values to the SSBO index varying.
958         SkSL::String::appendf(&sksl,
959                               "%s = %s;",
960                               RenderStep::ssboIndicesVarying(),
961                               RenderStep::ssboIndicesAttribute());
962     }
963 
964     if (defineLocalCoordsVarying) {
965         // Assign Render Step's stepLocalCoords to the localCoordsVar varying.
966         sksl += "localCoordsVar = stepLocalCoords;";
967     }
968     sksl += "}";
969 
970     fVertexSkSL = std::move(sksl);
971     fVSLabel = step->name();
972     if (defineLocalCoordsVarying) {
973         fVSLabel += " (w/ local coords)";
974     }
975     fHasStepUniforms = hasStepUniforms;
976 }
977 
needsLocalCoords() const978 bool ShaderInfo::needsLocalCoords() const {
979     return !fRootNodes.empty() &&
980            SkToBool(fRootNodes[0]->requiredFlags() & SnippetRequirementFlags::kLocalCoords);
981 }
982 
aggregateSnippetData(const ShaderNode * node)983 void ShaderInfo::aggregateSnippetData(const ShaderNode* node) {
984     if (!node) {
985         return;
986     }
987 
988     // Accumulate data of children first.
989     for (const ShaderNode* child : node->children()) {
990         this->aggregateSnippetData(child);
991     }
992 
993     if (node->requiredFlags() & SnippetRequirementFlags::kStoresData && !node->data().empty()) {
994         fData.push_back_n(node->data().size(), node->data().data());
995     }
996 }
997 
998 }  // namespace skgpu::graphite
999