xref: /aosp_15_r20/external/skia/src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2014 Google Inc.
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 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
8 
9 #include "include/private/base/SkAssert.h"
10 #include "include/private/base/SkDebug.h"
11 #include "src/core/SkSLTypeShared.h"
12 #include "src/gpu/Blend.h"
13 #include "src/gpu/ganesh/GrShaderCaps.h"
14 #include "src/gpu/ganesh/GrShaderVar.h"
15 #include "src/gpu/ganesh/glsl/GrGLSLProgramBuilder.h"
16 #include "src/gpu/ganesh/glsl/GrGLSLVarying.h"
17 
GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder * program)18 GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program)
19         : GrGLSLShaderBuilder(program) {}
20 
dstColor()21 const char* GrGLSLFragmentShaderBuilder::dstColor() {
22     SkDEBUGCODE(fHasReadDstColorThisStage_DebugOnly = true;)
23 
24     const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps();
25     if (shaderCaps->fFBFetchSupport) {
26         this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature,
27                          shaderCaps->fFBFetchExtensionString);
28 
29         // Some versions of this extension string require declaring custom color output on ES 3.0+
30         const char* fbFetchColorName = "sk_LastFragColor";
31         if (shaderCaps->fFBFetchNeedsCustomOutput) {
32             fPrimaryColorIsInOut = true;
33             fbFetchColorName = DeclaredColorOutputName();
34             // Set the dstColor to an intermediate variable so we don't override it with the output
35             this->codeAppendf("half4 %s = %s;", kDstColorName, fbFetchColorName);
36         } else {
37             return fbFetchColorName;
38         }
39     }
40     return kDstColorName;
41 }
42 
enableAdvancedBlendEquationIfNeeded(skgpu::BlendEquation equation)43 void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(
44         skgpu::BlendEquation equation) {
45     SkASSERT(skgpu::BlendEquationIsAdvanced(equation));
46 
47     if (fProgramBuilder->shaderCaps()->mustEnableAdvBlendEqs()) {
48         this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature,
49                          "GL_KHR_blend_equation_advanced");
50         this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier);
51     }
52 }
53 
enableSecondaryOutput()54 void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() {
55     SkASSERT(!fHasSecondaryOutput);
56     fHasSecondaryOutput = true;
57     const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
58     if (const char* extension = caps.fSecondaryOutputExtensionString) {
59         this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension);
60     }
61 
62     // If the primary output is declared, we must declare also the secondary output
63     // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom
64     // output. The condition also co-incides with the condition in which GLSL ES 2.0
65     // requires the built-in gl_SecondaryFragColorEXT, whereas 3.0 requires a custom output.
66     if (caps.mustDeclareFragmentShaderOutput()) {
67         fOutputs.emplace_back(DeclaredSecondaryColorOutputName(), SkSLType::kHalf4,
68                               GrShaderVar::TypeModifier::Out);
69         fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back());
70     }
71 }
72 
getPrimaryColorOutputName() const73 const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const {
74     return DeclaredColorOutputName();
75 }
76 
primaryColorOutputIsInOut() const77 bool GrGLSLFragmentShaderBuilder::primaryColorOutputIsInOut() const {
78     return fPrimaryColorIsInOut;
79 }
80 
getSecondaryColorOutputName() const81 const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const {
82     if (this->hasSecondaryOutput()) {
83         return (fProgramBuilder->shaderCaps()->mustDeclareFragmentShaderOutput())
84                 ? DeclaredSecondaryColorOutputName()
85                 : "sk_SecondaryFragColor";
86     }
87     return nullptr;
88 }
89 
getSurfaceOrigin() const90 GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const {
91     return fProgramBuilder->origin();
92 }
93 
onFinalize()94 void GrGLSLFragmentShaderBuilder::onFinalize() {
95     fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs());
96 }
97