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