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" |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 11 | #include "gl/GrGLGpu.h" |
egdaniel | cb7ba1e | 2015-10-26 08:38:25 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSL.h" |
jvanverth | cba99b8 | 2015-06-24 06:59:57 -0700 | [diff] [blame] | 13 | #include "glsl/GrGLSLCaps.h" |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLProgramBuilder.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLVarying.h" |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 17 | |
egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 18 | const char* GrGLSLFragmentShaderBuilder::kDstColorName = "_dstColor"; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 19 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 20 | static const char* sample_offset_array_name(GrGLSLFPFragmentBuilder::Coordinates coords) { |
| 21 | static const char* kArrayNames[] = { |
| 22 | "deviceSpaceSampleOffsets", |
| 23 | "windowSpaceSampleOffsets" |
| 24 | }; |
| 25 | return kArrayNames[coords]; |
| 26 | |
| 27 | GR_STATIC_ASSERT(0 == GrGLSLFPFragmentBuilder::kSkiaDevice_Coordinates); |
| 28 | GR_STATIC_ASSERT(1 == GrGLSLFPFragmentBuilder::kGLSLWindow_Coordinates); |
| 29 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kArrayNames) == GrGLSLFPFragmentBuilder::kLast_Coordinates + 1); |
| 30 | } |
| 31 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 32 | static const char* specific_layout_qualifier_name(GrBlendEquation equation) { |
| 33 | SkASSERT(GrBlendEquationIsAdvanced(equation)); |
| 34 | |
| 35 | static const char* kLayoutQualifierNames[] = { |
| 36 | "blend_support_screen", |
| 37 | "blend_support_overlay", |
| 38 | "blend_support_darken", |
| 39 | "blend_support_lighten", |
| 40 | "blend_support_colordodge", |
| 41 | "blend_support_colorburn", |
| 42 | "blend_support_hardlight", |
| 43 | "blend_support_softlight", |
| 44 | "blend_support_difference", |
| 45 | "blend_support_exclusion", |
| 46 | "blend_support_multiply", |
| 47 | "blend_support_hsl_hue", |
| 48 | "blend_support_hsl_saturation", |
| 49 | "blend_support_hsl_color", |
| 50 | "blend_support_hsl_luminosity" |
| 51 | }; |
| 52 | return kLayoutQualifierNames[equation - kFirstAdvancedGrBlendEquation]; |
| 53 | |
| 54 | GR_STATIC_ASSERT(0 == kScreen_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 55 | GR_STATIC_ASSERT(1 == kOverlay_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 56 | GR_STATIC_ASSERT(2 == kDarken_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 57 | GR_STATIC_ASSERT(3 == kLighten_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 58 | GR_STATIC_ASSERT(4 == kColorDodge_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 59 | GR_STATIC_ASSERT(5 == kColorBurn_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 60 | GR_STATIC_ASSERT(6 == kHardLight_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 61 | GR_STATIC_ASSERT(7 == kSoftLight_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 62 | GR_STATIC_ASSERT(8 == kDifference_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 63 | GR_STATIC_ASSERT(9 == kExclusion_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 64 | GR_STATIC_ASSERT(10 == kMultiply_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 65 | GR_STATIC_ASSERT(11 == kHSLHue_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 66 | GR_STATIC_ASSERT(12 == kHSLSaturation_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 67 | GR_STATIC_ASSERT(13 == kHSLColor_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 68 | GR_STATIC_ASSERT(14 == kHSLLuminosity_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 69 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayoutQualifierNames) == |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 70 | kGrBlendEquationCnt - kFirstAdvancedGrBlendEquation); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 71 | } |
| 72 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 73 | uint8_t GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(GrSurfaceOrigin origin) { |
| 74 | SkASSERT(kTopLeft_GrSurfaceOrigin == origin || kBottomLeft_GrSurfaceOrigin == origin); |
| 75 | return origin; |
| 76 | |
| 77 | GR_STATIC_ASSERT(1 == kTopLeft_GrSurfaceOrigin); |
| 78 | GR_STATIC_ASSERT(2 == kBottomLeft_GrSurfaceOrigin); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 79 | } |
| 80 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 81 | GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program) |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 82 | : GrGLSLFragmentBuilder(program) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 83 | , fSetupFragPosition(false) |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 84 | , fHasCustomColorOutput(false) |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 85 | , fCustomColorOutputIndex(-1) |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 86 | , fHasSecondaryOutput(false) |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 87 | , fUsedSampleOffsetArrays(0) |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 88 | , fHasInitializedSampleMask(false) { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 89 | fSubstageIndices.push_back(0); |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 90 | #ifdef SK_DEBUG |
| 91 | fUsedProcessorFeatures = GrProcessor::kNone_RequiredFeatures; |
| 92 | fHasReadDstColor = false; |
| 93 | #endif |
| 94 | } |
| 95 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 96 | bool GrGLSLFragmentShaderBuilder::enableFeature(GLSLFeature feature) { |
cdalton | 4a98cdb | 2016-03-01 12:12:20 -0800 | [diff] [blame] | 97 | const GrGLSLCaps& glslCaps = *fProgramBuilder->glslCaps(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 98 | switch (feature) { |
cdalton | 4a98cdb | 2016-03-01 12:12:20 -0800 | [diff] [blame] | 99 | case kStandardDerivatives_GLSLFeature: |
| 100 | if (!glslCaps.shaderDerivativeSupport()) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 101 | return false; |
| 102 | } |
cdalton | 4a98cdb | 2016-03-01 12:12:20 -0800 | [diff] [blame] | 103 | if (const char* extension = glslCaps.shaderDerivativeExtensionString()) { |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 104 | this->addFeature(1 << kStandardDerivatives_GLSLFeature, extension); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 105 | } |
| 106 | return true; |
cdalton | 4a98cdb | 2016-03-01 12:12:20 -0800 | [diff] [blame] | 107 | case kPixelLocalStorage_GLSLFeature: |
| 108 | if (glslCaps.pixelLocalStorageSize() <= 0) { |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 109 | return false; |
| 110 | } |
| 111 | this->addFeature(1 << kPixelLocalStorage_GLSLFeature, |
| 112 | "GL_EXT_shader_pixel_local_storage"); |
| 113 | return true; |
cdalton | 4a98cdb | 2016-03-01 12:12:20 -0800 | [diff] [blame] | 114 | case kMultisampleInterpolation_GLSLFeature: |
| 115 | if (!glslCaps.multisampleInterpolationSupport()) { |
| 116 | return false; |
| 117 | } |
| 118 | if (const char* extension = glslCaps.multisampleInterpolationExtensionString()) { |
| 119 | this->addFeature(1 << kMultisampleInterpolation_GLSLFeature, extension); |
| 120 | } |
| 121 | return true; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 122 | default: |
| 123 | SkFAIL("Unexpected GLSLFeature requested."); |
| 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 128 | SkString GrGLSLFragmentShaderBuilder::ensureCoords2D(const GrShaderVar& coords) { |
| 129 | if (kVec3f_GrSLType != coords.getType()) { |
| 130 | SkASSERT(kVec2f_GrSLType == coords.getType()); |
| 131 | return coords.getName(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 132 | } |
| 133 | |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 134 | SkString coords2D; |
| 135 | coords2D.printf("%s_ensure2D", coords.c_str()); |
| 136 | this->codeAppendf("\tvec2 %s = %s.xy / %s.z;", coords2D.c_str(), coords.c_str(), |
| 137 | coords.c_str()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 138 | return coords2D; |
| 139 | } |
| 140 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 141 | const char* GrGLSLFragmentShaderBuilder::fragmentPosition() { |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 142 | SkDEBUGCODE(fUsedProcessorFeatures |= GrProcessor::kFragmentPosition_RequiredFeature;) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 143 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 144 | const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 145 | // We only declare "gl_FragCoord" when we're in the case where we want to use layout qualifiers |
| 146 | // to reverse y. Otherwise it isn't necessary and whether the "in" qualifier appears in the |
| 147 | // declaration varies in earlier GLSL specs. So it is simpler to omit it. |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 148 | if (kTopLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 149 | fSetupFragPosition = true; |
| 150 | return "gl_FragCoord"; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 151 | } else if (const char* extension = glslCaps->fragCoordConventionsExtensionString()) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 152 | if (!fSetupFragPosition) { |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 153 | if (glslCaps->generation() < k150_GrGLSLGeneration) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 154 | this->addFeature(1 << kFragCoordConventions_GLSLPrivateFeature, |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 155 | extension); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 156 | } |
| 157 | fInputs.push_back().set(kVec4f_GrSLType, |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 158 | GrGLSLShaderVar::kIn_TypeModifier, |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 159 | "gl_FragCoord", |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 160 | kDefault_GrSLPrecision, |
egdaniel | ae47418 | 2016-01-21 11:19:52 -0800 | [diff] [blame] | 161 | "origin_upper_left"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 162 | fSetupFragPosition = true; |
| 163 | } |
| 164 | return "gl_FragCoord"; |
| 165 | } else { |
robertphillips | 18c58c7 | 2015-07-21 12:06:52 -0700 | [diff] [blame] | 166 | static const char* kTempName = "tmpXYFragCoord"; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 167 | static const char* kCoordName = "fragCoordYDown"; |
| 168 | if (!fSetupFragPosition) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 169 | const char* rtHeightName; |
| 170 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 171 | fProgramBuilder->addRTHeightUniform("RTHeight", &rtHeightName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 172 | |
robertphillips | 18c58c7 | 2015-07-21 12:06:52 -0700 | [diff] [blame] | 173 | // The Adreno compiler seems to be very touchy about access to "gl_FragCoord". |
| 174 | // Accessing glFragCoord.zw can cause a program to fail to link. Additionally, |
| 175 | // depending on the surrounding code, accessing .xy with a uniform involved can |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 176 | // do the same thing. Copying gl_FragCoord.xy into a temp vec2 beforehand |
robertphillips | 18c58c7 | 2015-07-21 12:06:52 -0700 | [diff] [blame] | 177 | // (and only accessing .xy) seems to "fix" things. |
ethannicholas | 1cbb5ea | 2015-12-14 11:37:55 -0800 | [diff] [blame] | 178 | const char* precision = glslCaps->usesPrecisionModifiers() ? "highp " : ""; |
| 179 | this->codePrependf("\t%svec4 %s = vec4(%s.x, %s - %s.y, 1.0, 1.0);\n", |
| 180 | precision, kCoordName, kTempName, rtHeightName, kTempName); |
| 181 | this->codePrependf("%svec2 %s = gl_FragCoord.xy;", precision, kTempName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 182 | fSetupFragPosition = true; |
| 183 | } |
| 184 | SkASSERT(fProgramBuilder->fUniformHandles.fRTHeightUni.isValid()); |
| 185 | return kCoordName; |
| 186 | } |
| 187 | } |
| 188 | |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 189 | const char* GrGLSLFragmentShaderBuilder::distanceVectorName() const { |
| 190 | return "fsDistanceVector"; |
| 191 | } |
| 192 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 193 | void GrGLSLFragmentShaderBuilder::appendOffsetToSample(const char* sampleIdx, Coordinates coords) { |
| 194 | SkASSERT(fProgramBuilder->header().fSamplePatternKey); |
| 195 | SkDEBUGCODE(fUsedProcessorFeatures |= GrProcessor::kSampleLocations_RequiredFeature); |
| 196 | if (kTopLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) { |
| 197 | // With a top left origin, device and window space are equal, so we only use device coords. |
| 198 | coords = kSkiaDevice_Coordinates; |
| 199 | } |
| 200 | this->codeAppendf("%s[%s]", sample_offset_array_name(coords), sampleIdx); |
| 201 | fUsedSampleOffsetArrays |= (1 << coords); |
| 202 | } |
| 203 | |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 204 | void GrGLSLFragmentShaderBuilder::maskSampleCoverage(const char* mask, bool invert) { |
| 205 | const GrGLSLCaps& glslCaps = *fProgramBuilder->glslCaps(); |
| 206 | if (!glslCaps.sampleVariablesSupport()) { |
| 207 | SkDEBUGFAIL("Attempted to mask sample coverage without support."); |
| 208 | return; |
| 209 | } |
| 210 | if (const char* extension = glslCaps.sampleVariablesExtensionString()) { |
| 211 | this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension); |
| 212 | } |
| 213 | if (!fHasInitializedSampleMask) { |
| 214 | this->codePrependf("gl_SampleMask[0] = -1;"); |
| 215 | fHasInitializedSampleMask = true; |
| 216 | } |
| 217 | if (invert) { |
| 218 | this->codeAppendf("gl_SampleMask[0] &= ~(%s);", mask); |
| 219 | } else { |
| 220 | this->codeAppendf("gl_SampleMask[0] &= %s;", mask); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void GrGLSLFragmentShaderBuilder::overrideSampleCoverage(const char* mask) { |
| 225 | const GrGLSLCaps& glslCaps = *fProgramBuilder->glslCaps(); |
| 226 | if (!glslCaps.sampleMaskOverrideCoverageSupport()) { |
| 227 | SkDEBUGFAIL("Attempted to override sample coverage without support."); |
| 228 | return; |
| 229 | } |
| 230 | SkASSERT(glslCaps.sampleVariablesSupport()); |
| 231 | if (const char* extension = glslCaps.sampleVariablesExtensionString()) { |
| 232 | this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension); |
| 233 | } |
| 234 | if (this->addFeature(1 << kSampleMaskOverrideCoverage_GLSLPrivateFeature, |
| 235 | "GL_NV_sample_mask_override_coverage")) { |
| 236 | // Redeclare gl_SampleMask with layout(override_coverage) if we haven't already. |
| 237 | fOutputs.push_back().set(kInt_GrSLType, GrShaderVar::kOut_TypeModifier, |
| 238 | "gl_SampleMask", 1, kHigh_GrSLPrecision, |
| 239 | "override_coverage"); |
| 240 | } |
| 241 | this->codeAppendf("gl_SampleMask[0] = %s;", mask); |
| 242 | fHasInitializedSampleMask = true; |
| 243 | } |
| 244 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 245 | const char* GrGLSLFragmentShaderBuilder::dstColor() { |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 246 | SkDEBUGCODE(fHasReadDstColor = true;) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 247 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 248 | const char* override = fProgramBuilder->primitiveProcessor().getDestColorOverride(); |
| 249 | if (override != nullptr) { |
| 250 | return override; |
| 251 | } |
| 252 | |
egdaniel | 472d44e | 2015-10-22 08:20:00 -0700 | [diff] [blame] | 253 | const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps(); |
| 254 | if (glslCaps->fbFetchSupport()) { |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 255 | this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature, |
egdaniel | 472d44e | 2015-10-22 08:20:00 -0700 | [diff] [blame] | 256 | glslCaps->fbFetchExtensionString()); |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 257 | |
joshualitt | 3c1096f | 2015-01-13 13:13:59 -0800 | [diff] [blame] | 258 | // 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] | 259 | const char* fbFetchColorName = glslCaps->fbFetchColorName(); |
| 260 | if (glslCaps->fbFetchNeedsCustomOutput()) { |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 261 | this->enableCustomOutput(); |
| 262 | fOutputs[fCustomColorOutputIndex].setTypeModifier(GrShaderVar::kInOut_TypeModifier); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 263 | fbFetchColorName = DeclaredColorOutputName(); |
egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 264 | // Set the dstColor to an intermediate variable so we don't override it with the output |
| 265 | this->codeAppendf("vec4 %s = %s;", kDstColorName, fbFetchColorName); |
egdaniel | bd4121a | 2016-08-17 12:55:30 -0700 | [diff] [blame] | 266 | } else { |
| 267 | return fbFetchColorName; |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 268 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 269 | } |
egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 270 | return kDstColorName; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 271 | } |
| 272 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 273 | void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 274 | SkASSERT(GrBlendEquationIsAdvanced(equation)); |
| 275 | |
egdaniel | 472d44e | 2015-10-22 08:20:00 -0700 | [diff] [blame] | 276 | const GrGLSLCaps& caps = *fProgramBuilder->glslCaps(); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 277 | if (!caps.mustEnableAdvBlendEqs()) { |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature, |
| 282 | "GL_KHR_blend_equation_advanced"); |
| 283 | if (caps.mustEnableSpecificAdvBlendEqs()) { |
| 284 | this->addLayoutQualifier(specific_layout_qualifier_name(equation), kOut_InterfaceQualifier); |
| 285 | } else { |
| 286 | this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier); |
| 287 | } |
| 288 | } |
| 289 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 290 | void GrGLSLFragmentShaderBuilder::enableCustomOutput() { |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 291 | if (!fHasCustomColorOutput) { |
| 292 | fHasCustomColorOutput = true; |
| 293 | fCustomColorOutputIndex = fOutputs.count(); |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 294 | fOutputs.push_back().set(kVec4f_GrSLType, |
| 295 | GrGLSLShaderVar::kOut_TypeModifier, |
| 296 | DeclaredColorOutputName()); |
| 297 | fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back()); |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 298 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 299 | } |
| 300 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 301 | void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 302 | SkASSERT(!fHasSecondaryOutput); |
| 303 | fHasSecondaryOutput = true; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 304 | const GrGLSLCaps& caps = *fProgramBuilder->glslCaps(); |
| 305 | if (const char* extension = caps.secondaryOutputExtensionString()) { |
| 306 | this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension); |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // If the primary output is declared, we must declare also the secondary output |
| 310 | // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom |
| 311 | // output. The condition also co-incides with the condition in whici GLES SL 2.0 |
| 312 | // 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] | 313 | if (caps.mustDeclareFragmentShaderOutput()) { |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 314 | fOutputs.push_back().set(kVec4f_GrSLType, GrGLSLShaderVar::kOut_TypeModifier, |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 315 | DeclaredSecondaryColorOutputName()); |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 316 | fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back()); |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 317 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 318 | } |
| 319 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 320 | const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const { |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 321 | return fHasCustomColorOutput ? DeclaredColorOutputName() : "gl_FragColor"; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 322 | } |
| 323 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 324 | void GrGLSLFragmentBuilder::declAppendf(const char* fmt, ...) { |
| 325 | va_list argp; |
| 326 | va_start(argp, fmt); |
| 327 | inputs().appendVAList(fmt, argp); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 328 | va_end(argp); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 329 | } |
| 330 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 331 | const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const { |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 332 | const GrGLSLCaps& caps = *fProgramBuilder->glslCaps(); |
| 333 | return caps.mustDeclareFragmentShaderOutput() ? DeclaredSecondaryColorOutputName() |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 334 | : "gl_SecondaryFragColorEXT"; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 335 | } |
| 336 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 337 | GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const { |
| 338 | SkASSERT(fProgramBuilder->header().fSurfaceOriginKey); |
| 339 | return static_cast<GrSurfaceOrigin>(fProgramBuilder->header().fSurfaceOriginKey); |
| 340 | |
| 341 | GR_STATIC_ASSERT(1 == kTopLeft_GrSurfaceOrigin); |
| 342 | GR_STATIC_ASSERT(2 == kBottomLeft_GrSurfaceOrigin); |
| 343 | } |
| 344 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 345 | void GrGLSLFragmentShaderBuilder::onFinalize() { |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 346 | fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs()); |
egdaniel | cb7ba1e | 2015-10-26 08:38:25 -0700 | [diff] [blame] | 347 | GrGLSLAppendDefaultFloatPrecisionDeclaration(kDefault_GrSLPrecision, |
| 348 | *fProgramBuilder->glslCaps(), |
| 349 | &this->precisionQualifier()); |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 350 | if (fUsedSampleOffsetArrays & (1 << kSkiaDevice_Coordinates)) { |
| 351 | this->defineSampleOffsetArray(sample_offset_array_name(kSkiaDevice_Coordinates), |
| 352 | SkMatrix::MakeTrans(-0.5f, -0.5f)); |
| 353 | } |
| 354 | if (fUsedSampleOffsetArrays & (1 << kGLSLWindow_Coordinates)) { |
| 355 | // With a top left origin, device and window space are equal, so we only use device coords. |
| 356 | SkASSERT(kBottomLeft_GrSurfaceOrigin == this->getSurfaceOrigin()); |
| 357 | SkMatrix m; |
| 358 | m.setScale(1, -1); |
| 359 | m.preTranslate(-0.5f, -0.5f); |
| 360 | this->defineSampleOffsetArray(sample_offset_array_name(kGLSLWindow_Coordinates), m); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void GrGLSLFragmentShaderBuilder::defineSampleOffsetArray(const char* name, const SkMatrix& m) { |
| 365 | SkASSERT(fProgramBuilder->caps()->sampleLocationsSupport()); |
| 366 | const GrPipeline& pipeline = fProgramBuilder->pipeline(); |
| 367 | const GrRenderTargetPriv& rtp = pipeline.getRenderTarget()->renderTargetPriv(); |
| 368 | const GrGpu::MultisampleSpecs& specs = rtp.getMultisampleSpecs(pipeline.getStencil()); |
| 369 | SkSTArray<16, SkPoint, true> offsets; |
| 370 | offsets.push_back_n(specs.fEffectiveSampleCnt); |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 371 | m.mapPoints(offsets.begin(), specs.fSampleLocations, specs.fEffectiveSampleCnt); |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 372 | this->definitions().append("const "); |
| 373 | if (fProgramBuilder->glslCaps()->usesPrecisionModifiers()) { |
| 374 | this->definitions().append("highp "); |
| 375 | } |
| 376 | this->definitions().appendf("vec2 %s[] = vec2[](", name); |
| 377 | for (int i = 0; i < specs.fEffectiveSampleCnt; ++i) { |
| 378 | this->definitions().appendf("vec2(%f, %f)", offsets[i].x(), offsets[i].y()); |
| 379 | this->definitions().append(i + 1 != specs.fEffectiveSampleCnt ? ", " : ");\n"); |
| 380 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 381 | } |
| 382 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 383 | void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() { |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 384 | SkASSERT(fSubstageIndices.count() >= 1); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 385 | fSubstageIndices.push_back(0); |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 386 | // second-to-last value in the fSubstageIndices stack is the index of the child proc |
| 387 | // at that level which is currently emitting code. |
| 388 | fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 389 | } |
| 390 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 391 | void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() { |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 392 | SkASSERT(fSubstageIndices.count() >= 2); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 393 | fSubstageIndices.pop_back(); |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 394 | fSubstageIndices.back()++; |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 395 | int removeAt = fMangleString.findLastOf('_'); |
| 396 | fMangleString.remove(removeAt, fMangleString.size() - removeAt); |
| 397 | } |