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" |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 10 | #include "gl/GrGLGpu.h" |
egdaniel | cb7ba1e | 2015-10-26 08:38:25 -0700 | [diff] [blame] | 11 | #include "glsl/GrGLSL.h" |
jvanverth | cba99b8 | 2015-06-24 06:59:57 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSLCaps.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 | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 17 | const char* GrGLSLFragmentShaderBuilder::kDstTextureColorName = "_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); |
| 56 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayoutQualifierNames) == |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 57 | kGrBlendEquationCnt - kFirstAdvancedGrBlendEquation); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 58 | } |
| 59 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 60 | GrGLSLFragmentShaderBuilder::FragPosKey |
| 61 | GrGLSLFragmentShaderBuilder::KeyForFragmentPosition(const GrRenderTarget* dst) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 62 | if (kTopLeft_GrSurfaceOrigin == dst->origin()) { |
| 63 | return kTopLeftFragPosRead_FragPosKey; |
| 64 | } else { |
| 65 | return kBottomLeftFragPosRead_FragPosKey; |
| 66 | } |
| 67 | } |
| 68 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 69 | GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program, |
| 70 | uint8_t fragPosKey) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 71 | : INHERITED(program) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 72 | , fSetupFragPosition(false) |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 73 | , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == fragPosKey) |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 74 | , fCustomColorOutputIndex(-1) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 75 | , fHasReadDstColor(false) |
| 76 | , fHasReadFragmentPosition(false) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 77 | } |
| 78 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 79 | bool GrGLSLFragmentShaderBuilder::enableFeature(GLSLFeature feature) { |
cdalton | 533cefe | 2016-02-12 13:24:26 -0800 | [diff] [blame] | 80 | const GrGLSLCaps& glslCaps = *fProgramBuilder->glslCaps(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 81 | switch (feature) { |
cdalton | 533cefe | 2016-02-12 13:24:26 -0800 | [diff] [blame] | 82 | case kStandardDerivatives_GLSLFeature: |
| 83 | if (!glslCaps.shaderDerivativeSupport()) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 84 | return false; |
| 85 | } |
cdalton | 533cefe | 2016-02-12 13:24:26 -0800 | [diff] [blame] | 86 | if (const char* extension = glslCaps.shaderDerivativeExtensionString()) { |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 87 | this->addFeature(1 << kStandardDerivatives_GLSLFeature, extension); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 88 | } |
| 89 | return true; |
cdalton | 533cefe | 2016-02-12 13:24:26 -0800 | [diff] [blame] | 90 | case kPixelLocalStorage_GLSLFeature: |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 91 | if (fProgramBuilder->glslCaps()->pixelLocalStorageSize() <= 0) { |
| 92 | return false; |
| 93 | } |
| 94 | this->addFeature(1 << kPixelLocalStorage_GLSLFeature, |
| 95 | "GL_EXT_shader_pixel_local_storage"); |
| 96 | return true; |
cdalton | 533cefe | 2016-02-12 13:24:26 -0800 | [diff] [blame] | 97 | case kSampleVariables_GLSLFeature: |
| 98 | if (!glslCaps.sampleVariablesSupport()) { |
| 99 | return false; |
| 100 | } |
| 101 | if (const char* extension = glslCaps.sampleVariablesExtensionString()) { |
| 102 | this->addFeature(1 << kSampleVariables_GLSLFeature, extension); |
| 103 | } |
| 104 | return true; |
| 105 | case kSampleMaskOverrideCoverage_GLSLFeature: |
| 106 | if (!glslCaps.sampleMaskOverrideCoverageSupport()) { |
| 107 | return false; |
| 108 | } |
| 109 | if (!this->enableFeature(kSampleVariables_GLSLFeature)) { |
| 110 | return false; |
| 111 | } |
| 112 | if (this->addFeature(1 << kSampleMaskOverrideCoverage_GLSLFeature, |
| 113 | "GL_NV_sample_mask_override_coverage")) { |
| 114 | // Redeclare gl_SampleMask with layout(override_coverage) if we haven't already. |
| 115 | fOutputs.push_back().set(kInt_GrSLType, GrShaderVar::kOut_TypeModifier, |
| 116 | "gl_SampleMask", 1, kHigh_GrSLPrecision, |
| 117 | "override_coverage"); |
| 118 | } |
| 119 | return true; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 120 | default: |
| 121 | SkFAIL("Unexpected GLSLFeature requested."); |
| 122 | return false; |
| 123 | } |
| 124 | } |
| 125 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 126 | SkString GrGLSLFragmentShaderBuilder::ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords, |
| 127 | int index) { |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 128 | if (kVec3f_GrSLType != coords[index].getType()) { |
| 129 | SkASSERT(kVec2f_GrSLType == coords[index].getType()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 130 | return coords[index].getName(); |
| 131 | } |
| 132 | |
| 133 | SkString coords2D("coords2D"); |
| 134 | if (0 != index) { |
| 135 | coords2D.appendf("_%i", index); |
| 136 | } |
| 137 | this->codeAppendf("\tvec2 %s = %s.xy / %s.z;", |
| 138 | coords2D.c_str(), coords[index].c_str(), coords[index].c_str()); |
| 139 | return coords2D; |
| 140 | } |
| 141 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 142 | const char* GrGLSLFragmentShaderBuilder::fragmentPosition() { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 143 | fHasReadFragmentPosition = true; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 144 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 145 | const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 146 | // We only declare "gl_FragCoord" when we're in the case where we want to use layout qualifiers |
| 147 | // to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the |
| 148 | // declaration varies in earlier GLSL specs. So it is simpler to omit it. |
| 149 | if (fTopLeftFragPosRead) { |
| 150 | fSetupFragPosition = true; |
| 151 | return "gl_FragCoord"; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 152 | } else if (const char* extension = glslCaps->fragCoordConventionsExtensionString()) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 153 | if (!fSetupFragPosition) { |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 154 | if (glslCaps->generation() < k150_GrGLSLGeneration) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 155 | this->addFeature(1 << kFragCoordConventions_GLSLPrivateFeature, |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 156 | extension); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 157 | } |
| 158 | fInputs.push_back().set(kVec4f_GrSLType, |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 159 | GrGLSLShaderVar::kIn_TypeModifier, |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 160 | "gl_FragCoord", |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 161 | kDefault_GrSLPrecision, |
egdaniel | ae47418 | 2016-01-21 11:19:52 -0800 | [diff] [blame] | 162 | "origin_upper_left"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 163 | fSetupFragPosition = true; |
| 164 | } |
| 165 | return "gl_FragCoord"; |
| 166 | } else { |
robertphillips | 18c58c7 | 2015-07-21 12:06:52 -0700 | [diff] [blame] | 167 | static const char* kTempName = "tmpXYFragCoord"; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 168 | static const char* kCoordName = "fragCoordYDown"; |
| 169 | if (!fSetupFragPosition) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 170 | const char* rtHeightName; |
| 171 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 172 | fProgramBuilder->addRTHeightUniform("RTHeight", &rtHeightName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 173 | |
robertphillips | 18c58c7 | 2015-07-21 12:06:52 -0700 | [diff] [blame] | 174 | // The Adreno compiler seems to be very touchy about access to "gl_FragCoord". |
| 175 | // Accessing glFragCoord.zw can cause a program to fail to link. Additionally, |
| 176 | // depending on the surrounding code, accessing .xy with a uniform involved can |
| 177 | // do the same thing. Copying gl_FragCoord.xy into a temp vec2 beforehand |
| 178 | // (and only accessing .xy) seems to "fix" things. |
ethannicholas | 1cbb5ea | 2015-12-14 11:37:55 -0800 | [diff] [blame] | 179 | const char* precision = glslCaps->usesPrecisionModifiers() ? "highp " : ""; |
| 180 | this->codePrependf("\t%svec4 %s = vec4(%s.x, %s - %s.y, 1.0, 1.0);\n", |
| 181 | precision, kCoordName, kTempName, rtHeightName, kTempName); |
| 182 | this->codePrependf("%svec2 %s = gl_FragCoord.xy;", precision, kTempName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 183 | fSetupFragPosition = true; |
| 184 | } |
| 185 | SkASSERT(fProgramBuilder->fUniformHandles.fRTHeightUni.isValid()); |
| 186 | return kCoordName; |
| 187 | } |
| 188 | } |
| 189 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 190 | const char* GrGLSLFragmentShaderBuilder::dstColor() { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 191 | fHasReadDstColor = true; |
| 192 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 193 | const char* override = fProgramBuilder->primitiveProcessor().getDestColorOverride(); |
| 194 | if (override != nullptr) { |
| 195 | return override; |
| 196 | } |
| 197 | |
egdaniel | 472d44e | 2015-10-22 08:20:00 -0700 | [diff] [blame] | 198 | const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps(); |
| 199 | if (glslCaps->fbFetchSupport()) { |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 200 | this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature, |
egdaniel | 472d44e | 2015-10-22 08:20:00 -0700 | [diff] [blame] | 201 | glslCaps->fbFetchExtensionString()); |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 202 | |
joshualitt | 3c1096f | 2015-01-13 13:13:59 -0800 | [diff] [blame] | 203 | // Some versions of this extension string require declaring custom color output on ES 3.0+ |
egdaniel | 472d44e | 2015-10-22 08:20:00 -0700 | [diff] [blame] | 204 | const char* fbFetchColorName = glslCaps->fbFetchColorName(); |
| 205 | if (glslCaps->fbFetchNeedsCustomOutput()) { |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 206 | this->enableCustomOutput(); |
| 207 | fOutputs[fCustomColorOutputIndex].setTypeModifier(GrShaderVar::kInOut_TypeModifier); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 208 | fbFetchColorName = DeclaredColorOutputName(); |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 209 | } |
| 210 | return fbFetchColorName; |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 211 | } else { |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 212 | return kDstTextureColorName; |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 213 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 214 | } |
| 215 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 216 | void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 217 | SkASSERT(GrBlendEquationIsAdvanced(equation)); |
| 218 | |
egdaniel | 472d44e | 2015-10-22 08:20:00 -0700 | [diff] [blame] | 219 | const GrGLSLCaps& caps = *fProgramBuilder->glslCaps(); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 220 | if (!caps.mustEnableAdvBlendEqs()) { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature, |
| 225 | "GL_KHR_blend_equation_advanced"); |
| 226 | if (caps.mustEnableSpecificAdvBlendEqs()) { |
| 227 | this->addLayoutQualifier(specific_layout_qualifier_name(equation), kOut_InterfaceQualifier); |
| 228 | } else { |
| 229 | this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier); |
| 230 | } |
| 231 | } |
| 232 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 233 | void GrGLSLFragmentShaderBuilder::enableCustomOutput() { |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 234 | if (!fHasCustomColorOutput) { |
| 235 | fHasCustomColorOutput = true; |
| 236 | fCustomColorOutputIndex = fOutputs.count(); |
| 237 | fOutputs.push_back().set(kVec4f_GrSLType, |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 238 | GrGLSLShaderVar::kOut_TypeModifier, |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 239 | DeclaredColorOutputName()); |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 240 | fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back()); |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 241 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 242 | } |
| 243 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 244 | void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 245 | SkASSERT(!fHasSecondaryOutput); |
| 246 | fHasSecondaryOutput = true; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 247 | const GrGLSLCaps& caps = *fProgramBuilder->glslCaps(); |
| 248 | if (const char* extension = caps.secondaryOutputExtensionString()) { |
| 249 | this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension); |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // If the primary output is declared, we must declare also the secondary output |
| 253 | // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom |
| 254 | // output. The condition also co-incides with the condition in whici GLES SL 2.0 |
| 255 | // 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] | 256 | if (caps.mustDeclareFragmentShaderOutput()) { |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 257 | fOutputs.push_back().set(kVec4f_GrSLType, GrGLSLShaderVar::kOut_TypeModifier, |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 258 | DeclaredSecondaryColorOutputName()); |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 259 | fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back()); |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 260 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 261 | } |
| 262 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 263 | const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const { |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 264 | return fHasCustomColorOutput ? DeclaredColorOutputName() : "gl_FragColor"; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 265 | } |
| 266 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 267 | void GrGLSLFragmentBuilder::declAppendf(const char* fmt, ...) { |
| 268 | va_list argp; |
| 269 | va_start(argp, fmt); |
| 270 | inputs().appendVAList(fmt, argp); |
| 271 | va_end(argp); |
| 272 | } |
| 273 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 274 | const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const { |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 275 | const GrGLSLCaps& caps = *fProgramBuilder->glslCaps(); |
| 276 | return caps.mustDeclareFragmentShaderOutput() ? DeclaredSecondaryColorOutputName() |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 277 | : "gl_SecondaryFragColorEXT"; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 278 | } |
| 279 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 280 | void GrGLSLFragmentShaderBuilder::onFinalize() { |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 281 | fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs()); |
egdaniel | cb7ba1e | 2015-10-26 08:38:25 -0700 | [diff] [blame] | 282 | GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, |
| 283 | *fProgramBuilder->glslCaps(), |
| 284 | &this->precisionQualifier()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 285 | } |
| 286 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 287 | void GrGLSLFragmentBuilder::onBeforeChildProcEmitCode() { |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 288 | SkASSERT(fSubstageIndices.count() >= 1); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 289 | fSubstageIndices.push_back(0); |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 290 | // second-to-last value in the fSubstageIndices stack is the index of the child proc |
| 291 | // at that level which is currently emitting code. |
| 292 | fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 293 | } |
| 294 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 295 | void GrGLSLFragmentBuilder::onAfterChildProcEmitCode() { |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 296 | SkASSERT(fSubstageIndices.count() >= 2); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 297 | fSubstageIndices.pop_back(); |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 298 | fSubstageIndices.back()++; |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 299 | int removeAt = fMangleString.findLastOf('_'); |
| 300 | fMangleString.remove(removeAt, fMangleString.size() - removeAt); |
| 301 | } |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 302 | |