blob: 511df10b2916ed266f06dc8e310b4282c6dbcc23 [file] [log] [blame]
joshualitt30ba4362014-08-21 20:18:45 -07001/*
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
egdaniel2d721d32015-11-11 13:06:05 -08008#include "GrGLSLFragmentShaderBuilder.h"
egdaniel574a4c12015-11-02 06:22:44 -08009#include "GrRenderTarget.h"
cdalton28f45b92016-03-07 13:58:26 -080010#include "GrRenderTargetPriv.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050011#include "GrShaderCaps.h"
ethannicholas22793252016-01-30 09:59:10 -080012#include "gl/GrGLGpu.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080013#include "glsl/GrGLSLProgramBuilder.h"
egdaniel7ea439b2015-12-03 09:20:44 -080014#include "glsl/GrGLSLUniformHandler.h"
egdaniel0eafe792015-11-20 14:01:22 -080015#include "glsl/GrGLSLVarying.h"
joshualitt47bb3822014-10-07 16:43:25 -070016
egdaniel138c2632016-08-17 10:59:00 -070017const char* GrGLSLFragmentShaderBuilder::kDstColorName = "_dstColor";
joshualitt30ba4362014-08-21 20:18:45 -070018
cdalton8917d622015-05-06 13:40:21 -070019static const char* specific_layout_qualifier_name(GrBlendEquation equation) {
20 SkASSERT(GrBlendEquationIsAdvanced(equation));
21
22 static const char* kLayoutQualifierNames[] = {
23 "blend_support_screen",
24 "blend_support_overlay",
25 "blend_support_darken",
26 "blend_support_lighten",
27 "blend_support_colordodge",
28 "blend_support_colorburn",
29 "blend_support_hardlight",
30 "blend_support_softlight",
31 "blend_support_difference",
32 "blend_support_exclusion",
33 "blend_support_multiply",
34 "blend_support_hsl_hue",
35 "blend_support_hsl_saturation",
36 "blend_support_hsl_color",
37 "blend_support_hsl_luminosity"
38 };
39 return kLayoutQualifierNames[equation - kFirstAdvancedGrBlendEquation];
40
41 GR_STATIC_ASSERT(0 == kScreen_GrBlendEquation - kFirstAdvancedGrBlendEquation);
42 GR_STATIC_ASSERT(1 == kOverlay_GrBlendEquation - kFirstAdvancedGrBlendEquation);
43 GR_STATIC_ASSERT(2 == kDarken_GrBlendEquation - kFirstAdvancedGrBlendEquation);
44 GR_STATIC_ASSERT(3 == kLighten_GrBlendEquation - kFirstAdvancedGrBlendEquation);
45 GR_STATIC_ASSERT(4 == kColorDodge_GrBlendEquation - kFirstAdvancedGrBlendEquation);
46 GR_STATIC_ASSERT(5 == kColorBurn_GrBlendEquation - kFirstAdvancedGrBlendEquation);
47 GR_STATIC_ASSERT(6 == kHardLight_GrBlendEquation - kFirstAdvancedGrBlendEquation);
48 GR_STATIC_ASSERT(7 == kSoftLight_GrBlendEquation - kFirstAdvancedGrBlendEquation);
49 GR_STATIC_ASSERT(8 == kDifference_GrBlendEquation - kFirstAdvancedGrBlendEquation);
50 GR_STATIC_ASSERT(9 == kExclusion_GrBlendEquation - kFirstAdvancedGrBlendEquation);
51 GR_STATIC_ASSERT(10 == kMultiply_GrBlendEquation - kFirstAdvancedGrBlendEquation);
52 GR_STATIC_ASSERT(11 == kHSLHue_GrBlendEquation - kFirstAdvancedGrBlendEquation);
53 GR_STATIC_ASSERT(12 == kHSLSaturation_GrBlendEquation - kFirstAdvancedGrBlendEquation);
54 GR_STATIC_ASSERT(13 == kHSLColor_GrBlendEquation - kFirstAdvancedGrBlendEquation);
55 GR_STATIC_ASSERT(14 == kHSLLuminosity_GrBlendEquation - kFirstAdvancedGrBlendEquation);
Mike Klein36743362018-11-06 08:23:30 -050056 // There's an illegal GrBlendEquation at the end there, hence the -1.
cdalton8917d622015-05-06 13:40:21 -070057 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayoutQualifierNames) ==
Mike Klein36743362018-11-06 08:23:30 -050058 kGrBlendEquationCnt - kFirstAdvancedGrBlendEquation - 1);
cdalton8917d622015-05-06 13:40:21 -070059}
60
Robert Phillips7f861922018-01-30 13:13:42 +000061uint8_t GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(GrSurfaceOrigin origin) {
62 SkASSERT(kTopLeft_GrSurfaceOrigin == origin || kBottomLeft_GrSurfaceOrigin == origin);
63 return origin + 1;
64
65 GR_STATIC_ASSERT(0 == kTopLeft_GrSurfaceOrigin);
66 GR_STATIC_ASSERT(1 == kBottomLeft_GrSurfaceOrigin);
67}
68
cdalton28f45b92016-03-07 13:58:26 -080069GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program)
cdalton85285412016-02-18 12:37:07 -080070 : GrGLSLFragmentBuilder(program)
joshualitt30ba4362014-08-21 20:18:45 -070071 , fSetupFragPosition(false)
cdalton85285412016-02-18 12:37:07 -080072 , fHasCustomColorOutput(false)
joshualittb4384b92014-10-21 12:53:15 -070073 , fCustomColorOutputIndex(-1)
cdalton85285412016-02-18 12:37:07 -080074 , fHasSecondaryOutput(false)
Ethan Nicholasf7b88202017-09-18 14:10:39 -040075 , fForceHighPrecision(false) {
cdalton85285412016-02-18 12:37:07 -080076 fSubstageIndices.push_back(0);
cdalton87332102016-02-26 12:22:02 -080077#ifdef SK_DEBUG
cdalton87332102016-02-26 12:22:02 -080078 fHasReadDstColor = false;
79#endif
80}
81
bsalomon1a1aa932016-09-12 09:30:36 -070082SkString GrGLSLFragmentShaderBuilder::ensureCoords2D(const GrShaderVar& coords) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -040083 if (kFloat3_GrSLType != coords.getType() && kHalf3_GrSLType != coords.getType()) {
84 SkASSERT(kFloat2_GrSLType == coords.getType() || kHalf2_GrSLType == coords.getType());
bsalomon1a1aa932016-09-12 09:30:36 -070085 return coords.getName();
joshualitt30ba4362014-08-21 20:18:45 -070086 }
87
bsalomon1a1aa932016-09-12 09:30:36 -070088 SkString coords2D;
89 coords2D.printf("%s_ensure2D", coords.c_str());
Ethan Nicholas8aa45692017-09-20 11:24:15 -040090 this->codeAppendf("\tfloat2 %s = %s.xy / %s.z;", coords2D.c_str(), coords.c_str(),
bsalomon1a1aa932016-09-12 09:30:36 -070091 coords.c_str());
joshualitt30ba4362014-08-21 20:18:45 -070092 return coords2D;
93}
94
egdaniel2d721d32015-11-11 13:06:05 -080095const char* GrGLSLFragmentShaderBuilder::dstColor() {
cdalton87332102016-02-26 12:22:02 -080096 SkDEBUGCODE(fHasReadDstColor = true;)
joshualitt47bb3822014-10-07 16:43:25 -070097
Brian Salomon1edc5b92016-11-29 13:43:46 -050098 const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps();
99 if (shaderCaps->fbFetchSupport()) {
cdaltonc08f1962016-02-12 12:14:06 -0800100 this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500101 shaderCaps->fbFetchExtensionString());
joshualittb4384b92014-10-21 12:53:15 -0700102
joshualitt3c1096f2015-01-13 13:13:59 -0800103 // Some versions of this extension string require declaring custom color output on ES 3.0+
Ethan Nicholaseab2baa2018-04-13 15:16:27 -0400104 const char* fbFetchColorName = "sk_LastFragColor";
Brian Salomon1edc5b92016-11-29 13:43:46 -0500105 if (shaderCaps->fbFetchNeedsCustomOutput()) {
joshualittb4384b92014-10-21 12:53:15 -0700106 this->enableCustomOutput();
107 fOutputs[fCustomColorOutputIndex].setTypeModifier(GrShaderVar::kInOut_TypeModifier);
egdaniel8dcdedc2015-11-11 06:27:20 -0800108 fbFetchColorName = DeclaredColorOutputName();
egdaniel138c2632016-08-17 10:59:00 -0700109 // Set the dstColor to an intermediate variable so we don't override it with the output
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400110 this->codeAppendf("half4 %s = %s;", kDstColorName, fbFetchColorName);
egdanielbd4121a2016-08-17 12:55:30 -0700111 } else {
112 return fbFetchColorName;
joshualittb4384b92014-10-21 12:53:15 -0700113 }
halcanary9d524f22016-03-29 09:03:52 -0700114 }
egdaniel138c2632016-08-17 10:59:00 -0700115 return kDstColorName;
joshualitt47bb3822014-10-07 16:43:25 -0700116}
117
egdaniel2d721d32015-11-11 13:06:05 -0800118void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) {
cdalton8917d622015-05-06 13:40:21 -0700119 SkASSERT(GrBlendEquationIsAdvanced(equation));
120
Brian Salomon94efbf52016-11-29 13:43:05 -0500121 const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
cdalton8917d622015-05-06 13:40:21 -0700122 if (!caps.mustEnableAdvBlendEqs()) {
123 return;
124 }
125
126 this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature,
127 "GL_KHR_blend_equation_advanced");
128 if (caps.mustEnableSpecificAdvBlendEqs()) {
129 this->addLayoutQualifier(specific_layout_qualifier_name(equation), kOut_InterfaceQualifier);
130 } else {
131 this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier);
132 }
133}
134
egdaniel2d721d32015-11-11 13:06:05 -0800135void GrGLSLFragmentShaderBuilder::enableCustomOutput() {
joshualittb4384b92014-10-21 12:53:15 -0700136 if (!fHasCustomColorOutput) {
137 fHasCustomColorOutput = true;
138 fCustomColorOutputIndex = fOutputs.count();
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400139 fOutputs.push_back().set(kHalf4_GrSLType, DeclaredColorOutputName(),
Brian Salomon99938a82016-11-21 13:41:08 -0500140 GrShaderVar::kOut_TypeModifier);
Ben Wagner63fd7602017-10-09 15:45:33 -0400141 fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back());
joshualittb4384b92014-10-21 12:53:15 -0700142 }
joshualitt47bb3822014-10-07 16:43:25 -0700143}
144
egdaniel2d721d32015-11-11 13:06:05 -0800145void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() {
joshualitt47bb3822014-10-07 16:43:25 -0700146 SkASSERT(!fHasSecondaryOutput);
147 fHasSecondaryOutput = true;
Brian Salomon94efbf52016-11-29 13:43:05 -0500148 const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
egdaniel8dcdedc2015-11-11 06:27:20 -0800149 if (const char* extension = caps.secondaryOutputExtensionString()) {
150 this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension);
kkinnunend94708e2015-07-30 22:47:04 -0700151 }
152
153 // If the primary output is declared, we must declare also the secondary output
154 // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom
155 // output. The condition also co-incides with the condition in whici GLES SL 2.0
156 // requires the built-in gl_SecondaryFragColorEXT, where as 3.0 requires a custom output.
kkinnunend94708e2015-07-30 22:47:04 -0700157 if (caps.mustDeclareFragmentShaderOutput()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400158 fOutputs.push_back().set(kHalf4_GrSLType, DeclaredSecondaryColorOutputName(),
Brian Salomon99938a82016-11-21 13:41:08 -0500159 GrShaderVar::kOut_TypeModifier);
egdanielb80ec8b2016-02-09 09:54:43 -0800160 fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back());
kkinnunend94708e2015-07-30 22:47:04 -0700161 }
joshualitt47bb3822014-10-07 16:43:25 -0700162}
163
egdaniel2d721d32015-11-11 13:06:05 -0800164const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const {
ethannicholas5961bc92016-10-12 06:39:56 -0700165 return fHasCustomColorOutput ? DeclaredColorOutputName() : "sk_FragColor";
joshualitt47bb3822014-10-07 16:43:25 -0700166}
167
Brian Salomondc092132018-04-04 10:14:16 -0400168bool GrGLSLFragmentShaderBuilder::primaryColorOutputIsInOut() const {
169 return fHasCustomColorOutput &&
170 fOutputs[fCustomColorOutputIndex].getTypeModifier() == GrShaderVar::kInOut_TypeModifier;
171}
172
ethannicholas22793252016-01-30 09:59:10 -0800173void GrGLSLFragmentBuilder::declAppendf(const char* fmt, ...) {
174 va_list argp;
175 va_start(argp, fmt);
176 inputs().appendVAList(fmt, argp);
halcanary9d524f22016-03-29 09:03:52 -0700177 va_end(argp);
ethannicholas22793252016-01-30 09:59:10 -0800178}
179
egdaniel2d721d32015-11-11 13:06:05 -0800180const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const {
Brian Salomon94efbf52016-11-29 13:43:05 -0500181 const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
egdaniel8dcdedc2015-11-11 06:27:20 -0800182 return caps.mustDeclareFragmentShaderOutput() ? DeclaredSecondaryColorOutputName()
kkinnunend94708e2015-07-30 22:47:04 -0700183 : "gl_SecondaryFragColorEXT";
joshualitt47bb3822014-10-07 16:43:25 -0700184}
185
cdalton28f45b92016-03-07 13:58:26 -0800186GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const {
187 SkASSERT(fProgramBuilder->header().fSurfaceOriginKey);
Robert Phillipsfb4a20c2017-08-29 14:46:43 -0400188 return static_cast<GrSurfaceOrigin>(fProgramBuilder->header().fSurfaceOriginKey-1);
cdalton28f45b92016-03-07 13:58:26 -0800189
Robert Phillipsfb4a20c2017-08-29 14:46:43 -0400190 GR_STATIC_ASSERT(0 == kTopLeft_GrSurfaceOrigin);
191 GR_STATIC_ASSERT(1 == kBottomLeft_GrSurfaceOrigin);
cdalton28f45b92016-03-07 13:58:26 -0800192}
193
egdaniel2d721d32015-11-11 13:06:05 -0800194void GrGLSLFragmentShaderBuilder::onFinalize() {
egdaniel0eafe792015-11-20 14:01:22 -0800195 fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs());
joshualitt30ba4362014-08-21 20:18:45 -0700196}
197
cdalton85285412016-02-18 12:37:07 -0800198void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() {
wangyix54a6b1a2015-09-08 08:41:51 -0700199 SkASSERT(fSubstageIndices.count() >= 1);
wangyix7ef45a12015-08-13 06:51:35 -0700200 fSubstageIndices.push_back(0);
wangyix54a6b1a2015-09-08 08:41:51 -0700201 // second-to-last value in the fSubstageIndices stack is the index of the child proc
202 // at that level which is currently emitting code.
203 fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]);
wangyix7ef45a12015-08-13 06:51:35 -0700204}
205
cdalton85285412016-02-18 12:37:07 -0800206void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() {
wangyix54a6b1a2015-09-08 08:41:51 -0700207 SkASSERT(fSubstageIndices.count() >= 2);
wangyix7ef45a12015-08-13 06:51:35 -0700208 fSubstageIndices.pop_back();
wangyix54a6b1a2015-09-08 08:41:51 -0700209 fSubstageIndices.back()++;
wangyix7ef45a12015-08-13 06:51:35 -0700210 int removeAt = fMangleString.findLastOf('_');
211 fMangleString.remove(removeAt, fMangleString.size() - removeAt);
212}