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 | |
| 41 | GR_STATIC_ASSERT(0 == kScreen_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 42 | GR_STATIC_ASSERT(1 == kOverlay_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 43 | GR_STATIC_ASSERT(2 == kDarken_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 44 | GR_STATIC_ASSERT(3 == kLighten_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 45 | GR_STATIC_ASSERT(4 == kColorDodge_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 46 | GR_STATIC_ASSERT(5 == kColorBurn_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 47 | GR_STATIC_ASSERT(6 == kHardLight_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 48 | GR_STATIC_ASSERT(7 == kSoftLight_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 49 | GR_STATIC_ASSERT(8 == kDifference_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 50 | GR_STATIC_ASSERT(9 == kExclusion_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 51 | GR_STATIC_ASSERT(10 == kMultiply_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 52 | GR_STATIC_ASSERT(11 == kHSLHue_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 53 | GR_STATIC_ASSERT(12 == kHSLSaturation_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 54 | GR_STATIC_ASSERT(13 == kHSLColor_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
| 55 | GR_STATIC_ASSERT(14 == kHSLLuminosity_GrBlendEquation - kFirstAdvancedGrBlendEquation); |
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. |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 57 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayoutQualifierNames) == |
Mike Klein | 3674336 | 2018-11-06 08:23:30 -0500 | [diff] [blame] | 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 | |
| 65 | GR_STATIC_ASSERT(0 == kTopLeft_GrSurfaceOrigin); |
| 66 | GR_STATIC_ASSERT(1 == kBottomLeft_GrSurfaceOrigin); |
| 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 | |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 74 | SkString GrGLSLFragmentShaderBuilder::ensureCoords2D(const GrShaderVar& coords) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 75 | if (kFloat3_GrSLType != coords.getType() && kHalf3_GrSLType != coords.getType()) { |
| 76 | SkASSERT(kFloat2_GrSLType == coords.getType() || kHalf2_GrSLType == coords.getType()); |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 77 | return coords.getName(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 78 | } |
| 79 | |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 80 | SkString coords2D; |
| 81 | coords2D.printf("%s_ensure2D", coords.c_str()); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 82 | this->codeAppendf("\tfloat2 %s = %s.xy / %s.z;", coords2D.c_str(), coords.c_str(), |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 83 | coords.c_str()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 84 | return coords2D; |
| 85 | } |
| 86 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 87 | const char* GrGLSLFragmentShaderBuilder::sampleOffsets() { |
| 88 | SkASSERT(CustomFeatures::kSampleLocations & fProgramBuilder->header().processorFeatures()); |
| 89 | SkDEBUGCODE(fUsedProcessorFeaturesThisStage_DebugOnly |= CustomFeatures::kSampleLocations); |
| 90 | SkDEBUGCODE(fUsedProcessorFeaturesAllStages_DebugOnly |= CustomFeatures::kSampleLocations); |
| 91 | return "_sampleOffsets"; |
| 92 | } |
| 93 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 94 | void GrGLSLFragmentShaderBuilder::maskOffMultisampleCoverage( |
| 95 | const char* mask, ScopeFlags scopeFlags) { |
Chris Dalton | d31b5e7 | 2019-02-26 18:02:16 -0700 | [diff] [blame] | 96 | const GrShaderCaps& shaderCaps = *fProgramBuilder->shaderCaps(); |
Chris Dalton | a7ad120 | 2019-07-19 15:31:59 -0600 | [diff] [blame] | 97 | if (!shaderCaps.sampleVariablesSupport() && !shaderCaps.sampleVariablesStencilSupport()) { |
Chris Dalton | d31b5e7 | 2019-02-26 18:02:16 -0700 | [diff] [blame] | 98 | SkDEBUGFAIL("Attempted to mask sample coverage without support."); |
| 99 | return; |
| 100 | } |
| 101 | if (const char* extension = shaderCaps.sampleVariablesExtensionString()) { |
| 102 | this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension); |
| 103 | } |
| 104 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 105 | if (!fHasModifiedSampleMask) { |
| 106 | fHasModifiedSampleMask = true; |
| 107 | if (ScopeFlags::kTopLevel != scopeFlags) { |
| 108 | this->codePrependf("gl_SampleMask[0] = ~0;"); |
| 109 | } |
| 110 | if (!(ScopeFlags::kInsideLoop & scopeFlags)) { |
| 111 | this->codeAppendf("gl_SampleMask[0] = (%s);", mask); |
| 112 | return; |
| 113 | } |
Chris Dalton | d31b5e7 | 2019-02-26 18:02:16 -0700 | [diff] [blame] | 114 | } |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 115 | |
Chris Dalton | d31b5e7 | 2019-02-26 18:02:16 -0700 | [diff] [blame] | 116 | this->codeAppendf("gl_SampleMask[0] &= (%s);", mask); |
| 117 | } |
| 118 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 119 | void GrGLSLFragmentShaderBuilder::applyFnToMultisampleMask( |
| 120 | const char* fn, const char* grad, ScopeFlags scopeFlags) { |
| 121 | SkASSERT(CustomFeatures::kSampleLocations & fProgramBuilder->header().processorFeatures()); |
| 122 | SkDEBUGCODE(fUsedProcessorFeaturesThisStage_DebugOnly |= CustomFeatures::kSampleLocations); |
| 123 | SkDEBUGCODE(fUsedProcessorFeaturesAllStages_DebugOnly |= CustomFeatures::kSampleLocations); |
| 124 | |
| 125 | int sampleCnt = fProgramBuilder->effectiveSampleCnt(); |
| 126 | SkASSERT(sampleCnt > 1); |
| 127 | |
| 128 | this->codeAppendf("{"); |
| 129 | |
| 130 | if (!grad) { |
| 131 | SkASSERT(fProgramBuilder->shaderCaps()->shaderDerivativeSupport()); |
| 132 | // In order to use HW derivatives, our neighbors within the same primitive must also be |
| 133 | // executing the same code. A per-pixel branch makes this pre-condition impossible to |
| 134 | // fulfill. |
| 135 | SkASSERT(!(ScopeFlags::kInsidePerPixelBranch & scopeFlags)); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 136 | this->codeAppendf("float2 grad = float2(dFdx(%s), dFdy(%s));", fn, fn); |
| 137 | this->codeAppendf("float fnwidth = fwidth(%s);", fn); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 138 | grad = "grad"; |
| 139 | } else { |
| 140 | this->codeAppendf("float fnwidth = abs(%s.x) + abs(%s.y);", grad, grad); |
| 141 | } |
| 142 | |
| 143 | this->codeAppendf("int mask = 0;"); |
| 144 | this->codeAppendf("if (%s*2 < fnwidth) {", fn); // Are ANY samples inside the implicit fn? |
| 145 | this->codeAppendf( "if (%s*-2 >= fnwidth) {", fn); // Are ALL samples inside the implicit? |
| 146 | this->codeAppendf( "mask = ~0;"); |
| 147 | this->codeAppendf( "} else for (int i = 0; i < %i; ++i) {", sampleCnt); |
| 148 | this->codeAppendf( "float fnsample = dot(%s, _sampleOffsets[i]) + %s;", grad, fn); |
| 149 | this->codeAppendf( "if (fnsample < 0) {"); |
| 150 | this->codeAppendf( "mask |= (1 << i);"); |
| 151 | this->codeAppendf( "}"); |
| 152 | this->codeAppendf( "}"); |
| 153 | this->codeAppendf("}"); |
| 154 | this->maskOffMultisampleCoverage("mask", scopeFlags); |
| 155 | |
| 156 | this->codeAppendf("}"); |
| 157 | } |
| 158 | |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 159 | SkString GrGLSLFPFragmentBuilder::writeProcessorFunction(GrGLSLFragmentProcessor* fp, |
| 160 | GrGLSLFragmentProcessor::EmitArgs& args) { |
| 161 | this->onBeforeChildProcEmitCode(); |
| 162 | this->nextStage(); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 163 | if (!args.fFp.computeLocalCoordsInVertexShader() && args.fTransformedCoords.count() > 0) { |
| 164 | // we currently only support overriding a single coordinate pair |
| 165 | SkASSERT(args.fTransformedCoords.count() == 1); |
| 166 | const GrGLSLProgramDataManager::UniformHandle& mat = |
| 167 | args.fTransformedCoords[0].fUniformMatrix; |
| 168 | if (mat.isValid()) { |
| 169 | args.fUniformHandler->updateUniformVisibility(mat, kFragment_GrShaderFlag); |
| 170 | this->codeAppendf("_coords = (float3(_coords, 1) * %s).xy;\n", |
| 171 | args.fTransformedCoords[0].fMatrixCode.c_str()); |
| 172 | } |
| 173 | } |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 174 | this->codeAppendf("half4 %s;\n", args.fOutputColor); |
| 175 | fp->emitCode(args); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 176 | this->codeAppendf("return %s;\n", args.fOutputColor); |
| 177 | GrShaderVar params[] = { GrShaderVar(args.fInputColor, kHalf4_GrSLType), |
| 178 | GrShaderVar("_coords", kFloat2_GrSLType) }; |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 179 | SkString result; |
| 180 | this->emitFunction(kHalf4_GrSLType, |
| 181 | "stage", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 182 | args.fFp.computeLocalCoordsInVertexShader() ? 1 : 2, |
| 183 | params, |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 184 | this->code().c_str(), |
| 185 | &result); |
| 186 | this->deleteStage(); |
| 187 | this->onAfterChildProcEmitCode(); |
| 188 | return result; |
| 189 | } |
| 190 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 191 | const char* GrGLSLFragmentShaderBuilder::dstColor() { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 192 | SkDEBUGCODE(fHasReadDstColorThisStage_DebugOnly = true;) |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 193 | |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 194 | const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps(); |
| 195 | if (shaderCaps->fbFetchSupport()) { |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 196 | this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 197 | shaderCaps->fbFetchExtensionString()); |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 198 | |
joshualitt | 3c1096f | 2015-01-13 13:13:59 -0800 | [diff] [blame] | 199 | // 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] | 200 | const char* fbFetchColorName = "sk_LastFragColor"; |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 201 | if (shaderCaps->fbFetchNeedsCustomOutput()) { |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 202 | this->enableCustomOutput(); |
| 203 | fOutputs[fCustomColorOutputIndex].setTypeModifier(GrShaderVar::kInOut_TypeModifier); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 204 | fbFetchColorName = DeclaredColorOutputName(); |
egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 205 | // 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] | 206 | this->codeAppendf("half4 %s = %s;", kDstColorName, fbFetchColorName); |
egdaniel | bd4121a | 2016-08-17 12:55:30 -0700 | [diff] [blame] | 207 | } else { |
| 208 | return fbFetchColorName; |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 209 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 210 | } |
egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 211 | return kDstColorName; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 212 | } |
| 213 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 214 | void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 215 | SkASSERT(GrBlendEquationIsAdvanced(equation)); |
| 216 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 217 | const GrShaderCaps& caps = *fProgramBuilder->shaderCaps(); |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 218 | if (!caps.mustEnableAdvBlendEqs()) { |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature, |
| 223 | "GL_KHR_blend_equation_advanced"); |
| 224 | if (caps.mustEnableSpecificAdvBlendEqs()) { |
| 225 | this->addLayoutQualifier(specific_layout_qualifier_name(equation), kOut_InterfaceQualifier); |
| 226 | } else { |
| 227 | this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier); |
| 228 | } |
| 229 | } |
| 230 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 231 | void GrGLSLFragmentShaderBuilder::enableCustomOutput() { |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 232 | if (!fHasCustomColorOutput) { |
| 233 | fHasCustomColorOutput = true; |
| 234 | fCustomColorOutputIndex = fOutputs.count(); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 235 | fOutputs.push_back().set(kHalf4_GrSLType, DeclaredColorOutputName(), |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 236 | GrShaderVar::kOut_TypeModifier); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 237 | fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back()); |
joshualitt | b4384b9 | 2014-10-21 12:53:15 -0700 | [diff] [blame] | 238 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 239 | } |
| 240 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 241 | void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() { |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 242 | SkASSERT(!fHasSecondaryOutput); |
| 243 | fHasSecondaryOutput = true; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 244 | const GrShaderCaps& caps = *fProgramBuilder->shaderCaps(); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 245 | if (const char* extension = caps.secondaryOutputExtensionString()) { |
| 246 | this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension); |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // If the primary output is declared, we must declare also the secondary output |
| 250 | // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom |
| 251 | // output. The condition also co-incides with the condition in whici GLES SL 2.0 |
| 252 | // 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] | 253 | if (caps.mustDeclareFragmentShaderOutput()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 254 | fOutputs.push_back().set(kHalf4_GrSLType, DeclaredSecondaryColorOutputName(), |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 255 | GrShaderVar::kOut_TypeModifier); |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 256 | fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back()); |
kkinnunen | d94708e | 2015-07-30 22:47:04 -0700 | [diff] [blame] | 257 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 258 | } |
| 259 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 260 | const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 261 | return fHasCustomColorOutput ? DeclaredColorOutputName() : "sk_FragColor"; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 264 | bool GrGLSLFragmentShaderBuilder::primaryColorOutputIsInOut() const { |
| 265 | return fHasCustomColorOutput && |
| 266 | fOutputs[fCustomColorOutputIndex].getTypeModifier() == GrShaderVar::kInOut_TypeModifier; |
| 267 | } |
| 268 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 269 | void GrGLSLFragmentBuilder::declAppendf(const char* fmt, ...) { |
| 270 | va_list argp; |
| 271 | va_start(argp, fmt); |
| 272 | inputs().appendVAList(fmt, argp); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 273 | va_end(argp); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 274 | } |
| 275 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 276 | const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const { |
Chris Dalton | 6b76df0 | 2019-04-08 11:01:34 -0600 | [diff] [blame] | 277 | if (this->hasSecondaryOutput()) { |
| 278 | return (fProgramBuilder->shaderCaps()->mustDeclareFragmentShaderOutput()) |
| 279 | ? DeclaredSecondaryColorOutputName() |
| 280 | : "gl_SecondaryFragColorEXT"; |
| 281 | } |
| 282 | return nullptr; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 283 | } |
| 284 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 285 | GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 286 | SkASSERT(fProgramBuilder->header().hasSurfaceOriginKey()); |
Robert Phillips | fb4a20c | 2017-08-29 14:46:43 -0400 | [diff] [blame] | 287 | return static_cast<GrSurfaceOrigin>(fProgramBuilder->header().fSurfaceOriginKey-1); |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 288 | |
Robert Phillips | fb4a20c | 2017-08-29 14:46:43 -0400 | [diff] [blame] | 289 | GR_STATIC_ASSERT(0 == kTopLeft_GrSurfaceOrigin); |
| 290 | GR_STATIC_ASSERT(1 == kBottomLeft_GrSurfaceOrigin); |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 291 | } |
| 292 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 293 | void GrGLSLFragmentShaderBuilder::onFinalize() { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 294 | SkASSERT(fProgramBuilder->header().processorFeatures() |
| 295 | == fUsedProcessorFeaturesAllStages_DebugOnly); |
| 296 | |
| 297 | if (CustomFeatures::kSampleLocations & fProgramBuilder->header().processorFeatures()) { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 298 | const SkTArray<SkPoint>& sampleLocations = |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 299 | fProgramBuilder->renderTarget()->renderTargetPriv().getSampleLocations(); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 300 | this->definitions().append("const float2 _sampleOffsets[] = float2[]("); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 301 | for (int i = 0; i < sampleLocations.count(); ++i) { |
| 302 | SkPoint offset = sampleLocations[i] - SkPoint::Make(.5f, .5f); |
| 303 | if (kBottomLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) { |
| 304 | offset.fY = -offset.fY; |
| 305 | } |
| 306 | this->definitions().appendf("float2(%f, %f)", offset.x(), offset.y()); |
| 307 | this->definitions().append((i + 1 != sampleLocations.count()) ? ", " : ");"); |
| 308 | } |
| 309 | } |
| 310 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 311 | fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 312 | } |
| 313 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 314 | void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() { |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 315 | SkASSERT(fSubstageIndices.count() >= 1); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 316 | fSubstageIndices.push_back(0); |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 317 | // second-to-last value in the fSubstageIndices stack is the index of the child proc |
| 318 | // at that level which is currently emitting code. |
| 319 | fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 320 | } |
| 321 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 322 | void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() { |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 323 | SkASSERT(fSubstageIndices.count() >= 2); |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 324 | fSubstageIndices.pop_back(); |
wangyix | 54a6b1a | 2015-09-08 08:41:51 -0700 | [diff] [blame] | 325 | fSubstageIndices.back()++; |
wangyix | 7ef45a1 | 2015-08-13 06:51:35 -0700 | [diff] [blame] | 326 | int removeAt = fMangleString.findLastOf('_'); |
| 327 | fMangleString.remove(removeAt, fMangleString.size() - removeAt); |
| 328 | } |