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 | |
| 8 | #include "GrGLFragmentShaderBuilder.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 9 | #include "GrGLProgramBuilder.h" |
jvanverth | cba99b8 | 2015-06-24 06:59:57 -0700 | [diff] [blame] | 10 | #include "gl/GrGLGpu.h" |
| 11 | #include "gl/GrGLGLSL.h" |
| 12 | #include "glsl/GrGLSLCaps.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 13 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 14 | #define GL_CALL(X) GR_GL_CALL(fProgramBuilder->gpu()->glInterface(), X) |
| 15 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fProgramBuilder->gpu()->glInterface(), R, X) |
| 16 | |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 17 | const char* GrGLFragmentShaderBuilder::kDstTextureColorName = "_dstColor"; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 18 | static const char* declared_color_output_name() { return "fsColorOut"; } |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 19 | static const char* declared_secondary_color_output_name() { return "fsSecondaryColorOut"; } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 20 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 21 | static const char* specific_layout_qualifier_name(GrBlendEquation equation) { |
| 22 | SkASSERT(GrBlendEquationIsAdvanced(equation)); |
| 23 | |
| 24 | static const char* kLayoutQualifierNames[] = { |
| 25 | "blend_support_screen", |
| 26 | "blend_support_overlay", |
| 27 | "blend_support_darken", |
| 28 | "blend_support_lighten", |
| 29 | "blend_support_colordodge", |
| 30 | "blend_support_colorburn", |
| 31 | "blend_support_hardlight", |
| 32 | "blend_support_softlight", |
| 33 | "blend_support_difference", |
| 34 | "blend_support_exclusion", |
| 35 | "blend_support_multiply", |
| 36 | "blend_support_hsl_hue", |
| 37 | "blend_support_hsl_saturation", |
| 38 | "blend_support_hsl_color", |
| 39 | "blend_support_hsl_luminosity" |
| 40 | }; |
| 41 | return kLayoutQualifierNames[equation - kFirstAdvancedGrBlendEquation]; |
| 42 | |
| 43 | GR_STATIC_ASSERT(0 == kScreen_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 44 | GR_STATIC_ASSERT(1 == kOverlay_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 45 | GR_STATIC_ASSERT(2 == kDarken_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 46 | GR_STATIC_ASSERT(3 == kLighten_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 47 | GR_STATIC_ASSERT(4 == kColorDodge_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 48 | GR_STATIC_ASSERT(5 == kColorBurn_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 49 | GR_STATIC_ASSERT(6 == kHardLight_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 50 | GR_STATIC_ASSERT(7 == kSoftLight_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 51 | GR_STATIC_ASSERT(8 == kDifference_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 52 | GR_STATIC_ASSERT(9 == kExclusion_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 53 | GR_STATIC_ASSERT(10 == kMultiply_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 54 | GR_STATIC_ASSERT(11 == kHSLHue_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 55 | GR_STATIC_ASSERT(12 == kHSLSaturation_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 56 | GR_STATIC_ASSERT(13 == kHSLColor_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 57 | GR_STATIC_ASSERT(14 == kHSLLuminosity_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 58 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayoutQualifierNames) == |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 59 | kGrBlendEquationCnt - kFirstAdvancedGrBlendEquation); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 60 | } |
| 61 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 62 | GrGLFragmentShaderBuilder::DstReadKey |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 63 | GrGLFragmentShaderBuilder::KeyForDstRead(const GrTexture* dstTexture, const GrGLCaps& caps) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 64 | uint32_t key = kYesDstRead_DstReadKeyBit; |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 65 | if (caps.glslCaps()->fbFetchSupport()) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 66 | return key; |
| 67 | } |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 68 | SkASSERT(dstTexture); |
| 69 | if (!caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(dstTexture->config())) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 70 | // The fact that the config is alpha-only must be considered when generating code. |
| 71 | key |= kUseAlphaConfig_DstReadKeyBit; |
| 72 | } |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 73 | if (kTopLeft_GrSurfaceOrigin == dstTexture->origin()) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 74 | key |= kTopLeftOrigin_DstReadKeyBit; |
| 75 | } |
| 76 | SkASSERT(static_cast<DstReadKey>(key) == key); |
| 77 | return static_cast<DstReadKey>(key); |
| 78 | } |
| 79 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 80 | GrGLFragmentShaderBuilder::FragPosKey |
rmistry | 63a9f84 | 2014-10-17 06:07:08 -0700 | [diff] [blame] | 81 | GrGLFragmentShaderBuilder::KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 82 | if (kTopLeft_GrSurfaceOrigin == dst->origin()) { |
| 83 | return kTopLeftFragPosRead_FragPosKey; |
| 84 | } else { |
| 85 | return kBottomLeftFragPosRead_FragPosKey; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | GrGLFragmentShaderBuilder::GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 90 | uint8_t fragPosKey) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 91 | : INHERITED(program) |
| 92 | , fHasCustomColorOutput(false) |
| 93 | , fHasSecondaryOutput(false) |
| 94 | , fSetupFragPosition(false) |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 95 | , fTopLeftFragPosRead(kTopLeftFragPosRead_FragPosKey == fragPosKey) |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 96 | , fCustomColorOutputIndex(-1) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 97 | , fHasReadDstColor(false) |
| 98 | , fHasReadFragmentPosition(false) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | bool GrGLFragmentShaderBuilder::enableFeature(GLSLFeature feature) { |
| 102 | switch (feature) { |
| 103 | case kStandardDerivatives_GLSLFeature: { |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 104 | GrGLGpu* gpu = fProgramBuilder->gpu(); |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 105 | if (!gpu->glCaps().shaderCaps()->shaderDerivativeSupport()) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 106 | return false; |
| 107 | } |
rmistry | 63a9f84 | 2014-10-17 06:07:08 -0700 | [diff] [blame] | 108 | if (kGLES_GrGLStandard == gpu->glStandard() && |
| 109 | k110_GrGLSLGeneration == gpu->glslGeneration()) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 110 | this->addFeature(1 << kStandardDerivatives_GLSLFeature, |
| 111 | "GL_OES_standard_derivatives"); |
| 112 | } |
| 113 | return true; |
| 114 | } |
| 115 | default: |
| 116 | SkFAIL("Unexpected GLSLFeature requested."); |
| 117 | return false; |
| 118 | } |
| 119 | } |
| 120 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 121 | SkString GrGLFragmentShaderBuilder::ensureFSCoords2D( |
| 122 | const GrGLProcessor::TransformedCoordsArray& coords, int index) { |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 123 | if (kVec3f_GrSLType != coords[index].getType()) { |
| 124 | SkASSERT(kVec2f_GrSLType == coords[index].getType()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 125 | return coords[index].getName(); |
| 126 | } |
| 127 | |
| 128 | SkString coords2D("coords2D"); |
| 129 | if (0 != index) { |
| 130 | coords2D.appendf("_%i", index); |
| 131 | } |
| 132 | this->codeAppendf("\tvec2 %s = %s.xy / %s.z;", |
| 133 | coords2D.c_str(), coords[index].c_str(), coords[index].c_str()); |
| 134 | return coords2D; |
| 135 | } |
| 136 | |
| 137 | const char* GrGLFragmentShaderBuilder::fragmentPosition() { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 138 | fHasReadFragmentPosition = true; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 139 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 140 | GrGLGpu* gpu = fProgramBuilder->gpu(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 141 | // We only declare "gl_FragCoord" when we're in the case where we want to use layout qualifiers |
| 142 | // to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the |
| 143 | // declaration varies in earlier GLSL specs. So it is simpler to omit it. |
| 144 | if (fTopLeftFragPosRead) { |
| 145 | fSetupFragPosition = true; |
| 146 | return "gl_FragCoord"; |
| 147 | } else if (gpu->glCaps().fragCoordConventionsSupport()) { |
| 148 | if (!fSetupFragPosition) { |
| 149 | if (gpu->glslGeneration() < k150_GrGLSLGeneration) { |
| 150 | this->addFeature(1 << kFragCoordConventions_GLSLPrivateFeature, |
| 151 | "GL_ARB_fragment_coord_conventions"); |
| 152 | } |
| 153 | fInputs.push_back().set(kVec4f_GrSLType, |
| 154 | GrGLShaderVar::kIn_TypeModifier, |
| 155 | "gl_FragCoord", |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 156 | kDefault_GrSLPrecision, |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 157 | GrGLShaderVar::kUpperLeft_Origin); |
| 158 | fSetupFragPosition = true; |
| 159 | } |
| 160 | return "gl_FragCoord"; |
| 161 | } else { |
robertphillips | 18c58c7 | 2015-07-21 12:06:52 -0700 | [diff] [blame] | 162 | static const char* kTempName = "tmpXYFragCoord"; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 163 | static const char* kCoordName = "fragCoordYDown"; |
| 164 | if (!fSetupFragPosition) { |
| 165 | // temporarily change the stage index because we're inserting non-stage code. |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 166 | GrGLProgramBuilder::AutoStageRestore asr(fProgramBuilder); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 167 | SkASSERT(!fProgramBuilder->fUniformHandles.fRTHeightUni.isValid()); |
| 168 | const char* rtHeightName; |
| 169 | |
| 170 | fProgramBuilder->fUniformHandles.fRTHeightUni = |
| 171 | fProgramBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 172 | kFloat_GrSLType, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 173 | kDefault_GrSLPrecision, |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 174 | "RTHeight", |
| 175 | &rtHeightName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 176 | |
robertphillips | 18c58c7 | 2015-07-21 12:06:52 -0700 | [diff] [blame] | 177 | // The Adreno compiler seems to be very touchy about access to "gl_FragCoord". |
| 178 | // Accessing glFragCoord.zw can cause a program to fail to link. Additionally, |
| 179 | // depending on the surrounding code, accessing .xy with a uniform involved can |
| 180 | // do the same thing. Copying gl_FragCoord.xy into a temp vec2 beforehand |
| 181 | // (and only accessing .xy) seems to "fix" things. |
| 182 | this->codePrependf("\tvec4 %s = vec4(%s.x, %s - %s.y, 1.0, 1.0);\n", |
| 183 | kCoordName, kTempName, rtHeightName, kTempName); |
| 184 | this->codePrependf("vec2 %s = gl_FragCoord.xy;", kTempName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 185 | fSetupFragPosition = true; |
| 186 | } |
| 187 | SkASSERT(fProgramBuilder->fUniformHandles.fRTHeightUni.isValid()); |
| 188 | return kCoordName; |
| 189 | } |
| 190 | } |
| 191 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 192 | const char* GrGLFragmentShaderBuilder::dstColor() { |
| 193 | fHasReadDstColor = true; |
| 194 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 195 | GrGLGpu* gpu = fProgramBuilder->gpu(); |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 196 | if (gpu->glCaps().glslCaps()->fbFetchSupport()) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 197 | this->addFeature(1 << (GrGLFragmentShaderBuilder::kLastGLSLPrivateFeature + 1), |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 198 | gpu->glCaps().glslCaps()->fbFetchExtensionString()); |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 199 | |
joshualitt | 3c1096f | 2015-01-13 13:13:59 -0800 | [diff] [blame] | 200 | // Some versions of this extension string require declaring custom color output on ES 3.0+ |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 201 | const char* fbFetchColorName = gpu->glCaps().glslCaps()->fbFetchColorName(); |
| 202 | if (gpu->glCaps().glslCaps()->fbFetchNeedsCustomOutput()) { |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 203 | this->enableCustomOutput(); |
| 204 | fOutputs[fCustomColorOutputIndex].setTypeModifier(GrShaderVar::kInOut_TypeModifier); |
| 205 | fbFetchColorName = declared_color_output_name(); |
| 206 | } |
| 207 | return fbFetchColorName; |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 208 | } else { |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 209 | return kDstTextureColorName; |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 210 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 211 | } |
| 212 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 213 | void GrGLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) { |
| 214 | SkASSERT(GrBlendEquationIsAdvanced(equation)); |
| 215 | |
| 216 | const GrGLSLCaps& caps = *fProgramBuilder->gpu()->glCaps().glslCaps(); |
| 217 | if (!caps.mustEnableAdvBlendEqs()) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature, |
| 222 | "GL_KHR_blend_equation_advanced"); |
| 223 | if (caps.mustEnableSpecificAdvBlendEqs()) { |
| 224 | this->addLayoutQualifier(specific_layout_qualifier_name(equation), kOut_InterfaceQualifier); |
| 225 | } else { |
| 226 | this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier); |
| 227 | } |
| 228 | } |
| 229 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 230 | void GrGLFragmentShaderBuilder::enableCustomOutput() { |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 231 | if (!fHasCustomColorOutput) { |
| 232 | fHasCustomColorOutput = true; |
| 233 | fCustomColorOutputIndex = fOutputs.count(); |
| 234 | fOutputs.push_back().set(kVec4f_GrSLType, |
| 235 | GrGLShaderVar::kOut_TypeModifier, |
| 236 | declared_color_output_name()); |
| 237 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void GrGLFragmentShaderBuilder::enableSecondaryOutput() { |
| 241 | SkASSERT(!fHasSecondaryOutput); |
| 242 | fHasSecondaryOutput = true; |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 243 | if (kGLES_GrGLStandard == fProgramBuilder->gpu()->ctxInfo().standard()) { |
| 244 | this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, "GL_EXT_blend_func_extended"); |
| 245 | } |
| 246 | |
| 247 | // If the primary output is declared, we must declare also the secondary output |
| 248 | // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom |
| 249 | // output. The condition also co-incides with the condition in whici GLES SL 2.0 |
| 250 | // requires the built-in gl_SecondaryFragColorEXT, where as 3.0 requires a custom output. |
| 251 | const GrGLSLCaps& caps = *fProgramBuilder->gpu()->glCaps().glslCaps(); |
| 252 | if (caps.mustDeclareFragmentShaderOutput()) { |
| 253 | fOutputs.push_back().set(kVec4f_GrSLType, GrGLShaderVar::kOut_TypeModifier, |
| 254 | declared_secondary_color_output_name()); |
| 255 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | const char* GrGLFragmentShaderBuilder::getPrimaryColorOutputName() const { |
| 259 | return fHasCustomColorOutput ? declared_color_output_name() : "gl_FragColor"; |
| 260 | } |
| 261 | |
| 262 | const char* GrGLFragmentShaderBuilder::getSecondaryColorOutputName() const { |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 263 | const GrGLSLCaps& caps = *fProgramBuilder->gpu()->glCaps().glslCaps(); |
| 264 | return caps.mustDeclareFragmentShaderOutput() ? declared_secondary_color_output_name() |
| 265 | : "gl_SecondaryFragColorEXT"; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 266 | } |
| 267 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 268 | bool GrGLFragmentShaderBuilder::compileAndAttachShaders(GrGLuint programId, |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 269 | SkTDArray<GrGLuint>* shaderIds) { |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 270 | GrGLGpu* gpu = fProgramBuilder->gpu(); |
jvanverth | cba99b8 | 2015-06-24 06:59:57 -0700 | [diff] [blame] | 271 | this->versionDecl() = GrGLGetGLSLVersionDecl(gpu->ctxInfo()); |
| 272 | GrGLAppendGLSLDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, |
| 273 | gpu->glStandard(), |
| 274 | &this->precisionQualifier()); |
cdalton | e4017d8 | 2015-05-06 11:48:56 -0700 | [diff] [blame] | 275 | this->compileAndAppendLayoutQualifiers(); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 276 | fProgramBuilder->appendUniformDecls(GrGLProgramBuilder::kFragment_Visibility, |
| 277 | &this->uniforms()); |
| 278 | this->appendDecls(fInputs, &this->inputs()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 279 | // We shouldn't have declared outputs on 1.10 |
| 280 | SkASSERT(k110_GrGLSLGeneration != gpu->glslGeneration() || fOutputs.empty()); |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 281 | this->appendDecls(fOutputs, &this->outputs()); |
| 282 | return this->finalize(programId, GR_GL_FRAGMENT_SHADER, shaderIds); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 283 | } |
| 284 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 285 | void GrGLFragmentShaderBuilder::bindFragmentShaderLocations(GrGLuint programID) { |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 286 | const GrGLCaps& caps = fProgramBuilder->gpu()->glCaps(); |
| 287 | if (fHasCustomColorOutput && caps.bindFragDataLocationSupport()) { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 288 | GL_CALL(BindFragDataLocation(programID, 0, declared_color_output_name())); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 289 | } |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 290 | if (fHasSecondaryOutput && caps.glslCaps()->mustDeclareFragmentShaderOutput()) { |
| 291 | GL_CALL(BindFragDataLocationIndexed(programID, 0, 1, |
| 292 | declared_secondary_color_output_name())); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 296 | void GrGLFragmentShaderBuilder::addVarying(GrGLVarying* v, GrSLPrecision fsPrec) { |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 297 | v->fFsIn = v->fVsOut; |
| 298 | if (v->fGsOut) { |
| 299 | v->fFsIn = v->fGsOut; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 300 | } |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 301 | fInputs.push_back().set(v->fType, GrGLShaderVar::kVaryingIn_TypeModifier, v->fFsIn, fsPrec); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 302 | } |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame^] | 303 | |
| 304 | void GrGLFragmentBuilder::onBeforeChildProcEmitCode() { |
| 305 | fSubstageIndices.back()++; |
| 306 | fSubstageIndices.push_back(0); |
| 307 | fMangleString.append(this->getMangleStringThisLevel()); |
| 308 | } |
| 309 | |
| 310 | void GrGLFragmentBuilder::onAfterChildProcEmitCode() { |
| 311 | fSubstageIndices.pop_back(); |
| 312 | int removeAt = fMangleString.findLastOf('_'); |
| 313 | fMangleString.remove(removeAt, fMangleString.size() - removeAt); |
| 314 | } |