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 | |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 8 | #include "src/gpu/GrRenderTarget.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/gpu/GrRenderTargetPriv.h" |
| 10 | #include "src/gpu/GrShaderCaps.h" |
| 11 | #include "src/gpu/gl/GrGLGpu.h" |
| 12 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 13 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 14 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
| 15 | #include "src/gpu/glsl/GrGLSLVarying.h" |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 16 | |
egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 17 | const char* GrGLSLFragmentShaderBuilder::kDstColorName = "_dstColor"; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 18 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 19 | static const char* specific_layout_qualifier_name(GrBlendEquation equation) { |
| 20 | SkASSERT(GrBlendEquationIsAdvanced(equation)); |
| 21 | |
| 22 | static const char* kLayoutQualifierNames[] = { |
| 23 | "blend_support_screen", |
| 24 | "blend_support_overlay", |
| 25 | "blend_support_darken", |
| 26 | "blend_support_lighten", |
| 27 | "blend_support_colordodge", |
| 28 | "blend_support_colorburn", |
| 29 | "blend_support_hardlight", |
| 30 | "blend_support_softlight", |
| 31 | "blend_support_difference", |
| 32 | "blend_support_exclusion", |
| 33 | "blend_support_multiply", |
| 34 | "blend_support_hsl_hue", |
| 35 | "blend_support_hsl_saturation", |
| 36 | "blend_support_hsl_color", |
| 37 | "blend_support_hsl_luminosity" |
| 38 | }; |
| 39 | return kLayoutQualifierNames[equation - kFirstAdvancedGrBlendEquation]; |
| 40 | |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 41 | static_assert(0 == kScreen_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 42 | static_assert(1 == kOverlay_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 43 | static_assert(2 == kDarken_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 44 | static_assert(3 == kLighten_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 45 | static_assert(4 == kColorDodge_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 46 | static_assert(5 == kColorBurn_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 47 | static_assert(6 == kHardLight_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 48 | static_assert(7 == kSoftLight_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 49 | static_assert(8 == kDifference_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 50 | static_assert(9 == kExclusion_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 51 | static_assert(10 == kMultiply_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 52 | static_assert(11 == kHSLHue_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 53 | static_assert(12 == kHSLSaturation_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 54 | static_assert(13 == kHSLColor_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 55 | static_assert(14 == kHSLLuminosity_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
Mike Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 56 | // There's an illegal GrBlendEquation at the end there, hence the -1. |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 57 | static_assert(SK_ARRAY_COUNT(kLayoutQualifierNames) == |
| 58 | kGrBlendEquationCnt - kFirstAdvancedGrBlendEquation - 1); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 61 | uint8_t GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(GrSurfaceOrigin origin) { |
| 62 | SkASSERT(kTopLeft_GrSurfaceOrigin == origin || kBottomLeft_GrSurfaceOrigin == origin); |
| 63 | return origin + 1; |
| 64 | |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 65 | static_assert(0 == kTopLeft_GrSurfaceOrigin); |
| 66 | static_assert(1 == kBottomLeft_GrSurfaceOrigin); |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 67 | } |
| 68 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 69 | GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program) |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 70 | : GrGLSLFragmentBuilder(program) { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 71 | fSubstageIndices.push_back(0); |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 74 | const char* GrGLSLFragmentShaderBuilder::sampleOffsets() { |
Robert Phillips | 7de1333 | 2019-10-09 15:44:54 -0400 | [diff] [blame] | 75 | SkASSERT(CustomFeatures::kSampleLocations & fProgramBuilder->processorFeatures()); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 76 | SkDEBUGCODE(fUsedProcessorFeaturesThisStage_DebugOnly |= CustomFeatures::kSampleLocations); |
| 77 | SkDEBUGCODE(fUsedProcessorFeaturesAllStages_DebugOnly |= CustomFeatures::kSampleLocations); |
| 78 | return "_sampleOffsets"; |
| 79 | } |
| 80 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 81 | void GrGLSLFragmentShaderBuilder::maskOffMultisampleCoverage( |
| 82 | const char* mask, ScopeFlags scopeFlags) { |
Chris Dalton | d31b5e7 | 2019-02-26 18:02:16 -0700 | [diff] [blame] | 83 | const GrShaderCaps& shaderCaps = *fProgramBuilder->shaderCaps(); |
Chris Dalton | 8a64a44 | 2019-10-29 18:54:58 -0600 | [diff] [blame] | 84 | if (!shaderCaps.sampleMaskSupport()) { |
Chris Dalton | d31b5e7 | 2019-02-26 18:02:16 -0700 | [diff] [blame] | 85 | SkDEBUGFAIL("Attempted to mask sample coverage without support."); |
| 86 | return; |
| 87 | } |
| 88 | if (const char* extension = shaderCaps.sampleVariablesExtensionString()) { |
| 89 | this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension); |
| 90 | } |
| 91 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 92 | if (!fHasModifiedSampleMask) { |
| 93 | fHasModifiedSampleMask = true; |
| 94 | if (ScopeFlags::kTopLevel != scopeFlags) { |
Chris Dalton | b0fd4b1 | 2019-10-29 13:41:22 -0600 | [diff] [blame] | 95 | this->codePrependf("sk_SampleMask[0] = ~0;"); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 96 | } |
| 97 | if (!(ScopeFlags::kInsideLoop & scopeFlags)) { |
Chris Dalton | b0fd4b1 | 2019-10-29 13:41:22 -0600 | [diff] [blame] | 98 | this->codeAppendf("sk_SampleMask[0] = (%s);", mask); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 99 | return; |
| 100 | } |
Chris Dalton | d31b5e7 | 2019-02-26 18:02:16 -0700 | [diff] [blame] | 101 | } |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 102 | |
Chris Dalton | b0fd4b1 | 2019-10-29 13:41:22 -0600 | [diff] [blame] | 103 | this->codeAppendf("sk_SampleMask[0] &= (%s);", mask); |
Chris Dalton | d31b5e7 | 2019-02-26 18:02:16 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 106 | void GrGLSLFragmentShaderBuilder::applyFnToMultisampleMask( |
| 107 | const char* fn, const char* grad, ScopeFlags scopeFlags) { |
Robert Phillips | 7de1333 | 2019-10-09 15:44:54 -0400 | [diff] [blame] | 108 | SkASSERT(CustomFeatures::kSampleLocations & fProgramBuilder->processorFeatures()); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 109 | SkDEBUGCODE(fUsedProcessorFeaturesThisStage_DebugOnly |= CustomFeatures::kSampleLocations); |
| 110 | SkDEBUGCODE(fUsedProcessorFeaturesAllStages_DebugOnly |= CustomFeatures::kSampleLocations); |
| 111 | |
| 112 | int sampleCnt = fProgramBuilder->effectiveSampleCnt(); |
| 113 | SkASSERT(sampleCnt > 1); |
| 114 | |
| 115 | this->codeAppendf("{"); |
| 116 | |
| 117 | if (!grad) { |
| 118 | SkASSERT(fProgramBuilder->shaderCaps()->shaderDerivativeSupport()); |
| 119 | // In order to use HW derivatives, our neighbors within the same primitive must also be |
| 120 | // executing the same code. A per-pixel branch makes this pre-condition impossible to |
| 121 | // fulfill. |
| 122 | SkASSERT(!(ScopeFlags::kInsidePerPixelBranch & scopeFlags)); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 123 | this->codeAppendf("float2 grad = float2(dFdx(%s), dFdy(%s));", fn, fn); |
| 124 | this->codeAppendf("float fnwidth = fwidth(%s);", fn); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 125 | grad = "grad"; |
| 126 | } else { |
| 127 | this->codeAppendf("float fnwidth = abs(%s.x) + abs(%s.y);", grad, grad); |
| 128 | } |
| 129 | |
| 130 | this->codeAppendf("int mask = 0;"); |
| 131 | this->codeAppendf("if (%s*2 < fnwidth) {", fn); // Are ANY samples inside the implicit fn? |
| 132 | this->codeAppendf( "if (%s*-2 >= fnwidth) {", fn); // Are ALL samples inside the implicit? |
| 133 | this->codeAppendf( "mask = ~0;"); |
| 134 | this->codeAppendf( "} else for (int i = 0; i < %i; ++i) {", sampleCnt); |
| 135 | this->codeAppendf( "float fnsample = dot(%s, _sampleOffsets[i]) + %s;", grad, fn); |
| 136 | this->codeAppendf( "if (fnsample < 0) {"); |
| 137 | this->codeAppendf( "mask |= (1 << i);"); |
| 138 | this->codeAppendf( "}"); |
| 139 | this->codeAppendf( "}"); |
| 140 | this->codeAppendf("}"); |
| 141 | this->maskOffMultisampleCoverage("mask", scopeFlags); |
| 142 | |
| 143 | this->codeAppendf("}"); |
| 144 | } |
| 145 | |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 146 | SkString GrGLSLFPFragmentBuilder::writeProcessorFunction(GrGLSLFragmentProcessor* fp, |
| 147 | GrGLSLFragmentProcessor::EmitArgs& args) { |
| 148 | this->onBeforeChildProcEmitCode(); |
| 149 | this->nextStage(); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 150 | |
| 151 | // An FP's function signature is theoretically always main(half4 color, float2 _coords). |
| 152 | // However, if it is only sampled by a chain of const/uniform matrices (or legacy coord |
| 153 | // transforms), the value that would have been passed to _coords is lifted to the vertex shader |
| 154 | // and stored in a unique varying. In that case it uses that variable and does not have a |
| 155 | // second actual argument for _coords. |
| 156 | // FIXME: Once GrCoordTransforms are gone, and we can more easily associated this varying with |
| 157 | // the sample call site, then invokeChild() can pass the varying in, instead of requiring this |
| 158 | // dynamic signature. |
| 159 | int paramCount; |
| 160 | GrShaderVar params[] = { GrShaderVar(args.fInputColor, kHalf4_GrSLType), |
| 161 | GrShaderVar(args.fSampleCoord, kFloat2_GrSLType) }; |
| 162 | |
| 163 | if (args.fFp.isSampledWithExplicitCoords()) { |
| 164 | // All invokeChild() that point to 'fp' will evaluate these expressions and pass the float2 |
| 165 | // in, so we need the 2nd argument. |
| 166 | paramCount = 2; |
| 167 | |
| 168 | // FIXME: This is only needed for the short term until FPs no longer put transformation |
| 169 | // data in a GrCoordTransform (and we can then mark the parameter as read-only) |
| 170 | if (args.fTransformedCoords.count() > 0) { |
| 171 | SkASSERT(args.fTransformedCoords.count() == 1); |
| 172 | |
| 173 | const GrShaderVar& transform = args.fTransformedCoords[0].fTransform; |
| 174 | switch (transform.getType()) { |
| 175 | case kFloat4_GrSLType: |
| 176 | // This is a scale+translate, so there's no perspective division needed |
| 177 | this->codeAppendf("%s = %s * %s.xz + %s.yw;\n", args.fSampleCoord, |
| 178 | args.fSampleCoord, |
| 179 | transform.c_str(), |
| 180 | transform.c_str()); |
| 181 | break; |
| 182 | case kFloat3x3_GrSLType: |
| 183 | this->codeAppend("{\n"); |
| 184 | this->codeAppendf("float3 _coords3 = (%s * %s.xy1);\n", |
| 185 | transform.c_str(), args.fSampleCoord); |
| 186 | this->codeAppendf("%s = _coords3.xy / _coords3.z;\n", args.fSampleCoord); |
| 187 | this->codeAppend("}\n"); |
| 188 | break; |
| 189 | default: |
| 190 | SkASSERT(transform.getType() == kVoid_GrSLType); |
| 191 | break; |
| 192 | } |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 193 | } |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 194 | } else { |
| 195 | // Sampled with a const/uniform matrix and/or a legacy coord transform. The actual |
| 196 | // transformation code is emitted in the vertex shader, so this only has to access it. |
| 197 | // Add a float2 _coords variable that maps to the associated varying and replaces the |
| 198 | // absent 2nd argument to the fp's function. |
| 199 | paramCount = 1; |
| 200 | |
| 201 | if (args.fFp.referencesSampleCoords()) { |
| 202 | const GrShaderVar& varying = args.fTransformedCoords[0].fVaryingPoint; |
| 203 | switch(varying.getType()) { |
| 204 | case kFloat2_GrSLType: |
| 205 | // Just point the local coords to the varying |
| 206 | args.fSampleCoord = varying.getName().c_str(); |
| 207 | break; |
| 208 | case kFloat3_GrSLType: |
| 209 | // Must perform the perspective divide in the frag shader based on the varying, |
| 210 | // and since we won't actually have a function parameter for local coords, add |
| 211 | // it as a local variable. |
| 212 | this->codeAppendf("float2 %s = %s.xy / %s.z;\n", args.fSampleCoord, |
| 213 | varying.getName().c_str(), varying.getName().c_str()); |
| 214 | break; |
| 215 | default: |
| 216 | SkDEBUGFAILF("Unexpected varying type for coord: %s %d\n", |
| 217 | varying.getName().c_str(), (int) varying.getType()); |
| 218 | break; |
| 219 | } |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 220 | } |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 221 | } |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 222 | |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 223 | this->codeAppendf("half4 %s;\n", args.fOutputColor); |
| 224 | fp->emitCode(args); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 225 | this->codeAppendf("return %s;\n", args.fOutputColor); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 226 | |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 227 | SkString result; |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 228 | this->emitFunction(kHalf4_GrSLType, args.fFp.name(), paramCount, params, |
| 229 | this->code().c_str(), &result); |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 230 | this->deleteStage(); |
| 231 | this->onAfterChildProcEmitCode(); |
| 232 | return result; |
| 233 | } |
| 234 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 235 | const char* GrGLSLFragmentShaderBuilder::dstColor() { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 236 | SkDEBUGCODE(fHasReadDstColorThisStage_DebugOnly = true;) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 237 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 238 | const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps(); |
| 239 | if (shaderCaps->fbFetchSupport()) { |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 240 | this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 241 | shaderCaps->fbFetchExtensionString()); |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 242 | |
joshualitt | 3c1096f | 2015-01-13 13:13:59 -0800 | [diff] [blame] | 243 | // Some versions of this extension string require declaring custom color output on ES 3.0+ |
Ethan Nicholas | eab2baa | 2018-04-13 15:16:27 -0400 | [diff] [blame] | 244 | const char* fbFetchColorName = "sk_LastFragColor"; |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 245 | if (shaderCaps->fbFetchNeedsCustomOutput()) { |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 246 | this->enableCustomOutput(); |
Ben Wagner | dabd98c | 2020-03-25 15:17:18 -0400 | [diff] [blame] | 247 | fCustomColorOutput->setTypeModifier(GrShaderVar::TypeModifier::InOut); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 248 | fbFetchColorName = DeclaredColorOutputName(); |
egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 249 | // Set the dstColor to an intermediate variable so we don't override it with the output |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 250 | this->codeAppendf("half4 %s = %s;", kDstColorName, fbFetchColorName); |
egdaniel | bd4121a | 2016-08-17 12:55:30 -0700 | [diff] [blame] | 251 | } else { |
| 252 | return fbFetchColorName; |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 253 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 254 | } |
egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 255 | return kDstColorName; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 256 | } |
| 257 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 258 | void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 259 | SkASSERT(GrBlendEquationIsAdvanced(equation)); |
| 260 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 261 | const GrShaderCaps& caps = *fProgramBuilder->shaderCaps(); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 262 | if (!caps.mustEnableAdvBlendEqs()) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature, |
| 267 | "GL_KHR_blend_equation_advanced"); |
| 268 | if (caps.mustEnableSpecificAdvBlendEqs()) { |
| 269 | this->addLayoutQualifier(specific_layout_qualifier_name(equation), kOut_InterfaceQualifier); |
| 270 | } else { |
| 271 | this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier); |
| 272 | } |
| 273 | } |
| 274 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 275 | void GrGLSLFragmentShaderBuilder::enableCustomOutput() { |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 276 | if (!fCustomColorOutput) { |
Ben Wagner | dabd98c | 2020-03-25 15:17:18 -0400 | [diff] [blame] | 277 | fCustomColorOutput = &fOutputs.emplace_back(DeclaredColorOutputName(), kHalf4_GrSLType, |
| 278 | GrShaderVar::TypeModifier::Out); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 279 | fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back()); |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 280 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 281 | } |
| 282 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 283 | void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 284 | SkASSERT(!fHasSecondaryOutput); |
| 285 | fHasSecondaryOutput = true; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 286 | const GrShaderCaps& caps = *fProgramBuilder->shaderCaps(); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 287 | if (const char* extension = caps.secondaryOutputExtensionString()) { |
| 288 | this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension); |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // If the primary output is declared, we must declare also the secondary output |
| 292 | // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom |
| 293 | // output. The condition also co-incides with the condition in whici GLES SL 2.0 |
| 294 | // 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] | 295 | if (caps.mustDeclareFragmentShaderOutput()) { |
Ben Wagner | dabd98c | 2020-03-25 15:17:18 -0400 | [diff] [blame] | 296 | fOutputs.emplace_back(DeclaredSecondaryColorOutputName(), kHalf4_GrSLType, |
| 297 | GrShaderVar::TypeModifier::Out); |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 298 | fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back()); |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 299 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 300 | } |
| 301 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 302 | const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const { |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 303 | return this->hasCustomColorOutput() ? DeclaredColorOutputName() : "sk_FragColor"; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 306 | bool GrGLSLFragmentShaderBuilder::primaryColorOutputIsInOut() const { |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 307 | return fCustomColorOutput && |
Ben Wagner | dabd98c | 2020-03-25 15:17:18 -0400 | [diff] [blame] | 308 | fCustomColorOutput->getTypeModifier() == GrShaderVar::TypeModifier::InOut; |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 309 | } |
| 310 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 311 | void GrGLSLFragmentBuilder::declAppendf(const char* fmt, ...) { |
| 312 | va_list argp; |
| 313 | va_start(argp, fmt); |
| 314 | inputs().appendVAList(fmt, argp); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 315 | va_end(argp); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 316 | } |
| 317 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 318 | const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const { |
Chris Dalton | 6b76df0 | 2019-04-08 11:01:34 -0600 | [diff] [blame] | 319 | if (this->hasSecondaryOutput()) { |
| 320 | return (fProgramBuilder->shaderCaps()->mustDeclareFragmentShaderOutput()) |
| 321 | ? DeclaredSecondaryColorOutputName() |
| 322 | : "gl_SecondaryFragColorEXT"; |
| 323 | } |
| 324 | return nullptr; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 325 | } |
| 326 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 327 | GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const { |
Robert Phillips | 7de1333 | 2019-10-09 15:44:54 -0400 | [diff] [blame] | 328 | return fProgramBuilder->origin(); |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 329 | } |
| 330 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 331 | void GrGLSLFragmentShaderBuilder::onFinalize() { |
Robert Phillips | 7de1333 | 2019-10-09 15:44:54 -0400 | [diff] [blame] | 332 | SkASSERT(fProgramBuilder->processorFeatures() == fUsedProcessorFeaturesAllStages_DebugOnly); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 333 | |
Robert Phillips | 7de1333 | 2019-10-09 15:44:54 -0400 | [diff] [blame] | 334 | if (CustomFeatures::kSampleLocations & fProgramBuilder->processorFeatures()) { |
Robert Phillips | 65a7775 | 2019-10-02 15:22:24 -0400 | [diff] [blame] | 335 | const SkTArray<SkPoint>& sampleLocations = fProgramBuilder->getSampleLocations(); |
Chris Dalton | 2284aab | 2019-11-15 11:02:24 -0700 | [diff] [blame] | 336 | this->definitions().appendf("const float2 _sampleOffsets[%i] = float2[%i](", |
| 337 | sampleLocations.count(), sampleLocations.count()); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 338 | for (int i = 0; i < sampleLocations.count(); ++i) { |
| 339 | SkPoint offset = sampleLocations[i] - SkPoint::Make(.5f, .5f); |
| 340 | if (kBottomLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) { |
| 341 | offset.fY = -offset.fY; |
| 342 | } |
| 343 | this->definitions().appendf("float2(%f, %f)", offset.x(), offset.y()); |
| 344 | this->definitions().append((i + 1 != sampleLocations.count()) ? ", " : ");"); |
| 345 | } |
| 346 | } |
| 347 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 348 | fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 349 | } |
| 350 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 351 | void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() { |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 352 | SkASSERT(fSubstageIndices.count() >= 1); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 353 | fSubstageIndices.push_back(0); |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 354 | // second-to-last value in the fSubstageIndices stack is the index of the child proc |
| 355 | // at that level which is currently emitting code. |
| 356 | fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 357 | } |
| 358 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 359 | void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() { |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 360 | SkASSERT(fSubstageIndices.count() >= 2); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 361 | fSubstageIndices.pop_back(); |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 362 | fSubstageIndices.back()++; |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 363 | int removeAt = fMangleString.findLastOf('_'); |
| 364 | fMangleString.remove(removeAt, fMangleString.size() - removeAt); |
| 365 | } |