joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 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 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 8 | #include "GrGLSLFragmentShaderBuilder.h" |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 9 | #include "GrRenderTarget.h" |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 10 | #include "GrRenderTargetPriv.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 11 | #include "GrShaderCaps.h" |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 12 | #include "gl/GrGLGpu.h" |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLProgramBuilder.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLVarying.h" |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 16 | |
egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 17 | const char* GrGLSLFragmentShaderBuilder::kDstColorName = "_dstColor"; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 18 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 19 | static 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 Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 56 | // There's an illegal GrBlendEquation at the end there, hence the -1. |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 57 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayoutQualifierNames) == |
Mike Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 58 | kGrBlendEquationCnt - kFirstAdvancedGrBlendEquation - 1); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 61 | uint8_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 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 69 | GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program) |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 70 | : GrGLSLFragmentBuilder(program) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 71 | , fSetupFragPosition(false) |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 72 | , fHasCustomColorOutput(false) |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 73 | , fCustomColorOutputIndex(-1) |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 74 | , fHasSecondaryOutput(false) |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 75 | , fForceHighPrecision(false) { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 76 | fSubstageIndices.push_back(0); |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 77 | #ifdef SK_DEBUG |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 78 | fHasReadDstColor = false; |
| 79 | #endif |
| 80 | } |
| 81 | |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 82 | SkString GrGLSLFragmentShaderBuilder::ensureCoords2D(const GrShaderVar& coords) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 83 | if (kFloat3_GrSLType != coords.getType() && kHalf3_GrSLType != coords.getType()) { |
| 84 | SkASSERT(kFloat2_GrSLType == coords.getType() || kHalf2_GrSLType == coords.getType()); |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 85 | return coords.getName(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 86 | } |
| 87 | |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 88 | SkString coords2D; |
| 89 | coords2D.printf("%s_ensure2D", coords.c_str()); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 90 | this->codeAppendf("\tfloat2 %s = %s.xy / %s.z;", coords2D.c_str(), coords.c_str(), |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 91 | coords.c_str()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 92 | return coords2D; |
| 93 | } |
| 94 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 95 | const char* GrGLSLFragmentShaderBuilder::dstColor() { |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 96 | SkDEBUGCODE(fHasReadDstColor = true;) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 97 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 98 | const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps(); |
| 99 | if (shaderCaps->fbFetchSupport()) { |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 100 | this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 101 | shaderCaps->fbFetchExtensionString()); |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 102 | |
joshualitt | 3c1096f | 2015-01-13 13:13:59 -0800 | [diff] [blame] | 103 | // Some versions of this extension string require declaring custom color output on ES 3.0+ |
Ethan Nicholas | eab2baa | 2018-04-13 15:16:27 -0400 | [diff] [blame] | 104 | const char* fbFetchColorName = "sk_LastFragColor"; |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 105 | if (shaderCaps->fbFetchNeedsCustomOutput()) { |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 106 | this->enableCustomOutput(); |
| 107 | fOutputs[fCustomColorOutputIndex].setTypeModifier(GrShaderVar::kInOut_TypeModifier); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 108 | fbFetchColorName = DeclaredColorOutputName(); |
egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 109 | // Set the dstColor to an intermediate variable so we don't override it with the output |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 110 | this->codeAppendf("half4 %s = %s;", kDstColorName, fbFetchColorName); |
egdaniel | bd4121a | 2016-08-17 12:55:30 -0700 | [diff] [blame] | 111 | } else { |
| 112 | return fbFetchColorName; |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 113 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 114 | } |
egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 115 | return kDstColorName; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 116 | } |
| 117 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 118 | void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 119 | SkASSERT(GrBlendEquationIsAdvanced(equation)); |
| 120 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 121 | const GrShaderCaps& caps = *fProgramBuilder->shaderCaps(); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 122 | 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 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 135 | void GrGLSLFragmentShaderBuilder::enableCustomOutput() { |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 136 | if (!fHasCustomColorOutput) { |
| 137 | fHasCustomColorOutput = true; |
| 138 | fCustomColorOutputIndex = fOutputs.count(); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 139 | fOutputs.push_back().set(kHalf4_GrSLType, DeclaredColorOutputName(), |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 140 | GrShaderVar::kOut_TypeModifier); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 141 | fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back()); |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 142 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 143 | } |
| 144 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 145 | void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 146 | SkASSERT(!fHasSecondaryOutput); |
| 147 | fHasSecondaryOutput = true; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 148 | const GrShaderCaps& caps = *fProgramBuilder->shaderCaps(); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 149 | if (const char* extension = caps.secondaryOutputExtensionString()) { |
| 150 | this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension); |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 151 | } |
| 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. |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 157 | if (caps.mustDeclareFragmentShaderOutput()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 158 | fOutputs.push_back().set(kHalf4_GrSLType, DeclaredSecondaryColorOutputName(), |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 159 | GrShaderVar::kOut_TypeModifier); |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 160 | fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back()); |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 161 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 162 | } |
| 163 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 164 | const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 165 | return fHasCustomColorOutput ? DeclaredColorOutputName() : "sk_FragColor"; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 168 | bool GrGLSLFragmentShaderBuilder::primaryColorOutputIsInOut() const { |
| 169 | return fHasCustomColorOutput && |
| 170 | fOutputs[fCustomColorOutputIndex].getTypeModifier() == GrShaderVar::kInOut_TypeModifier; |
| 171 | } |
| 172 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 173 | void GrGLSLFragmentBuilder::declAppendf(const char* fmt, ...) { |
| 174 | va_list argp; |
| 175 | va_start(argp, fmt); |
| 176 | inputs().appendVAList(fmt, argp); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 177 | va_end(argp); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 178 | } |
| 179 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 180 | const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const { |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 181 | const GrShaderCaps& caps = *fProgramBuilder->shaderCaps(); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 182 | return caps.mustDeclareFragmentShaderOutput() ? DeclaredSecondaryColorOutputName() |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 183 | : "gl_SecondaryFragColorEXT"; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 184 | } |
| 185 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 186 | GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const { |
| 187 | SkASSERT(fProgramBuilder->header().fSurfaceOriginKey); |
Robert Phillips | fb4a20c | 2017-08-29 14:46:43 -0400 | [diff] [blame] | 188 | return static_cast<GrSurfaceOrigin>(fProgramBuilder->header().fSurfaceOriginKey-1); |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 189 | |
Robert Phillips | fb4a20c | 2017-08-29 14:46:43 -0400 | [diff] [blame] | 190 | GR_STATIC_ASSERT(0 == kTopLeft_GrSurfaceOrigin); |
| 191 | GR_STATIC_ASSERT(1 == kBottomLeft_GrSurfaceOrigin); |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 192 | } |
| 193 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 194 | void GrGLSLFragmentShaderBuilder::onFinalize() { |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 195 | fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 196 | } |
| 197 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 198 | void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() { |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 199 | SkASSERT(fSubstageIndices.count() >= 1); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 200 | fSubstageIndices.push_back(0); |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 201 | // 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]); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 204 | } |
| 205 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 206 | void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() { |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 207 | SkASSERT(fSubstageIndices.count() >= 2); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 208 | fSubstageIndices.pop_back(); |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 209 | fSubstageIndices.back()++; |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 210 | int removeAt = fMangleString.findLastOf('_'); |
| 211 | fMangleString.remove(removeAt, fMangleString.size() - removeAt); |
| 212 | } |