blob: aed5fea9fe65050553deb35533decf9e266e6d68 [file] [log] [blame]
joshualitt30ba4362014-08-21 20:18:45 -07001/*
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 Salomon201cdbb2019-08-14 17:00:30 -04008#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrShaderCaps.h"
10#include "src/gpu/gl/GrGLGpu.h"
11#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
12#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
13#include "src/gpu/glsl/GrGLSLUniformHandler.h"
14#include "src/gpu/glsl/GrGLSLVarying.h"
joshualitt47bb3822014-10-07 16:43:25 -070015
egdaniel138c2632016-08-17 10:59:00 -070016const char* GrGLSLFragmentShaderBuilder::kDstColorName = "_dstColor";
joshualitt30ba4362014-08-21 20:18:45 -070017
Robert Phillips7f861922018-01-30 13:13:42 +000018uint8_t GrGLSLFragmentShaderBuilder::KeyForSurfaceOrigin(GrSurfaceOrigin origin) {
19 SkASSERT(kTopLeft_GrSurfaceOrigin == origin || kBottomLeft_GrSurfaceOrigin == origin);
20 return origin + 1;
21
Brian Salomon4dea72a2019-12-18 10:43:10 -050022 static_assert(0 == kTopLeft_GrSurfaceOrigin);
23 static_assert(1 == kBottomLeft_GrSurfaceOrigin);
Robert Phillips7f861922018-01-30 13:13:42 +000024}
25
cdalton28f45b92016-03-07 13:58:26 -080026GrGLSLFragmentShaderBuilder::GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program)
Brian Salomonffd15ea2020-07-01 16:48:20 -040027 : GrGLSLShaderBuilder(program) {
cdalton85285412016-02-18 12:37:07 -080028 fSubstageIndices.push_back(0);
cdalton87332102016-02-26 12:22:02 -080029}
30
Chris Daltond7291ba2019-03-07 14:17:03 -070031const char* GrGLSLFragmentShaderBuilder::sampleOffsets() {
Robert Phillips7de13332019-10-09 15:44:54 -040032 SkASSERT(CustomFeatures::kSampleLocations & fProgramBuilder->processorFeatures());
Chris Daltond7291ba2019-03-07 14:17:03 -070033 SkDEBUGCODE(fUsedProcessorFeaturesThisStage_DebugOnly |= CustomFeatures::kSampleLocations);
34 SkDEBUGCODE(fUsedProcessorFeaturesAllStages_DebugOnly |= CustomFeatures::kSampleLocations);
35 return "_sampleOffsets";
36}
37
Chris Dalton0dffbab2019-03-27 13:08:50 -060038void GrGLSLFragmentShaderBuilder::maskOffMultisampleCoverage(
39 const char* mask, ScopeFlags scopeFlags) {
Chris Daltond31b5e72019-02-26 18:02:16 -070040 const GrShaderCaps& shaderCaps = *fProgramBuilder->shaderCaps();
Chris Dalton8a64a442019-10-29 18:54:58 -060041 if (!shaderCaps.sampleMaskSupport()) {
Chris Daltond31b5e72019-02-26 18:02:16 -070042 SkDEBUGFAIL("Attempted to mask sample coverage without support.");
43 return;
44 }
45 if (const char* extension = shaderCaps.sampleVariablesExtensionString()) {
46 this->addFeature(1 << kSampleVariables_GLSLPrivateFeature, extension);
47 }
48
Chris Dalton0dffbab2019-03-27 13:08:50 -060049 if (!fHasModifiedSampleMask) {
50 fHasModifiedSampleMask = true;
51 if (ScopeFlags::kTopLevel != scopeFlags) {
Chris Daltonb0fd4b12019-10-29 13:41:22 -060052 this->codePrependf("sk_SampleMask[0] = ~0;");
Chris Dalton0dffbab2019-03-27 13:08:50 -060053 }
54 if (!(ScopeFlags::kInsideLoop & scopeFlags)) {
Chris Daltonb0fd4b12019-10-29 13:41:22 -060055 this->codeAppendf("sk_SampleMask[0] = (%s);", mask);
Chris Dalton0dffbab2019-03-27 13:08:50 -060056 return;
57 }
Chris Daltond31b5e72019-02-26 18:02:16 -070058 }
Chris Dalton0dffbab2019-03-27 13:08:50 -060059
Chris Daltonb0fd4b12019-10-29 13:41:22 -060060 this->codeAppendf("sk_SampleMask[0] &= (%s);", mask);
Chris Daltond31b5e72019-02-26 18:02:16 -070061}
62
Chris Dalton0dffbab2019-03-27 13:08:50 -060063void GrGLSLFragmentShaderBuilder::applyFnToMultisampleMask(
64 const char* fn, const char* grad, ScopeFlags scopeFlags) {
Robert Phillips7de13332019-10-09 15:44:54 -040065 SkASSERT(CustomFeatures::kSampleLocations & fProgramBuilder->processorFeatures());
Chris Dalton0dffbab2019-03-27 13:08:50 -060066 SkDEBUGCODE(fUsedProcessorFeaturesThisStage_DebugOnly |= CustomFeatures::kSampleLocations);
67 SkDEBUGCODE(fUsedProcessorFeaturesAllStages_DebugOnly |= CustomFeatures::kSampleLocations);
68
69 int sampleCnt = fProgramBuilder->effectiveSampleCnt();
70 SkASSERT(sampleCnt > 1);
71
72 this->codeAppendf("{");
73
74 if (!grad) {
75 SkASSERT(fProgramBuilder->shaderCaps()->shaderDerivativeSupport());
76 // In order to use HW derivatives, our neighbors within the same primitive must also be
77 // executing the same code. A per-pixel branch makes this pre-condition impossible to
78 // fulfill.
79 SkASSERT(!(ScopeFlags::kInsidePerPixelBranch & scopeFlags));
Chris Daltonc3318f02019-07-19 14:20:53 -060080 this->codeAppendf("float2 grad = float2(dFdx(%s), dFdy(%s));", fn, fn);
81 this->codeAppendf("float fnwidth = fwidth(%s);", fn);
Chris Dalton0dffbab2019-03-27 13:08:50 -060082 grad = "grad";
83 } else {
84 this->codeAppendf("float fnwidth = abs(%s.x) + abs(%s.y);", grad, grad);
85 }
86
87 this->codeAppendf("int mask = 0;");
88 this->codeAppendf("if (%s*2 < fnwidth) {", fn); // Are ANY samples inside the implicit fn?
89 this->codeAppendf( "if (%s*-2 >= fnwidth) {", fn); // Are ALL samples inside the implicit?
90 this->codeAppendf( "mask = ~0;");
91 this->codeAppendf( "} else for (int i = 0; i < %i; ++i) {", sampleCnt);
92 this->codeAppendf( "float fnsample = dot(%s, _sampleOffsets[i]) + %s;", grad, fn);
93 this->codeAppendf( "if (fnsample < 0) {");
94 this->codeAppendf( "mask |= (1 << i);");
95 this->codeAppendf( "}");
96 this->codeAppendf( "}");
97 this->codeAppendf("}");
98 this->maskOffMultisampleCoverage("mask", scopeFlags);
99
100 this->codeAppendf("}");
101}
102
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400103SkString GrGLSLFPFragmentBuilder::writeProcessorFunction(GrGLSLFragmentProcessor* fp,
104 GrGLSLFragmentProcessor::EmitArgs& args) {
105 this->onBeforeChildProcEmitCode();
106 this->nextStage();
Michael Ludwige88320b2020-06-24 09:04:56 -0400107
108 // An FP's function signature is theoretically always main(half4 color, float2 _coords).
Brian Osman1298bc42020-06-30 13:39:35 -0400109 // However, if it is only sampled by a chain of uniform matrix expressions (or legacy coord
Michael Ludwige88320b2020-06-24 09:04:56 -0400110 // transforms), the value that would have been passed to _coords is lifted to the vertex shader
111 // and stored in a unique varying. In that case it uses that variable and does not have a
112 // second actual argument for _coords.
Michael Ludwige7e25ac2020-06-26 12:53:03 -0400113 // FIXME: An alternative would be to have all FP functions have a float2 argument, and the
114 // parent FP invokes it with the varying reference when it's been lifted to the vertex shader.
John Stilesfdf61482020-10-27 09:45:40 -0400115 size_t paramCount = 2;
Michael Ludwige88320b2020-06-24 09:04:56 -0400116 GrShaderVar params[] = { GrShaderVar(args.fInputColor, kHalf4_GrSLType),
117 GrShaderVar(args.fSampleCoord, kFloat2_GrSLType) };
118
Michael Ludwige7e25ac2020-06-26 12:53:03 -0400119 if (!args.fFp.isSampledWithExplicitCoords()) {
Brian Osman1298bc42020-06-30 13:39:35 -0400120 // Sampled with a uniform matrix expression and/or a legacy coord transform. The actual
Michael Ludwige88320b2020-06-24 09:04:56 -0400121 // transformation code is emitted in the vertex shader, so this only has to access it.
122 // Add a float2 _coords variable that maps to the associated varying and replaces the
123 // absent 2nd argument to the fp's function.
124 paramCount = 1;
125
126 if (args.fFp.referencesSampleCoords()) {
Brian Osman9cf98dc2020-07-01 17:21:27 -0400127 const GrShaderVar& varying = args.fTransformedCoords[0];
Michael Ludwige88320b2020-06-24 09:04:56 -0400128 switch(varying.getType()) {
129 case kFloat2_GrSLType:
130 // Just point the local coords to the varying
131 args.fSampleCoord = varying.getName().c_str();
132 break;
133 case kFloat3_GrSLType:
134 // Must perform the perspective divide in the frag shader based on the varying,
135 // and since we won't actually have a function parameter for local coords, add
136 // it as a local variable.
137 this->codeAppendf("float2 %s = %s.xy / %s.z;\n", args.fSampleCoord,
Michael Ludwige7e25ac2020-06-26 12:53:03 -0400138 varying.getName().c_str(), varying.getName().c_str());
Michael Ludwige88320b2020-06-24 09:04:56 -0400139 break;
140 default:
141 SkDEBUGFAILF("Unexpected varying type for coord: %s %d\n",
142 varying.getName().c_str(), (int) varying.getType());
143 break;
144 }
Ethan Nicholas58430122020-04-14 09:54:02 -0400145 }
Michael Ludwige7e25ac2020-06-26 12:53:03 -0400146 } // else the function keeps its two arguments
Ethan Nicholas58430122020-04-14 09:54:02 -0400147
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400148 fp->emitCode(args);
Michael Ludwige88320b2020-06-24 09:04:56 -0400149
John Stiles6b58a332020-10-26 17:53:06 -0400150 SkString funcName = this->getMangledFunctionName(args.fFp.name());
John Stilesfdf61482020-10-27 09:45:40 -0400151 this->emitFunction(kHalf4_GrSLType, funcName.c_str(), {params, paramCount},
John Stilesad223002021-03-12 17:33:17 -0500152 this->code().c_str());
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400153 this->deleteStage();
154 this->onAfterChildProcEmitCode();
John Stiles6b58a332020-10-26 17:53:06 -0400155 return funcName;
Ethan Nicholasc6dce5a2019-07-24 16:51:36 -0400156}
157
egdaniel2d721d32015-11-11 13:06:05 -0800158const char* GrGLSLFragmentShaderBuilder::dstColor() {
Chris Daltond7291ba2019-03-07 14:17:03 -0700159 SkDEBUGCODE(fHasReadDstColorThisStage_DebugOnly = true;)
joshualitt47bb3822014-10-07 16:43:25 -0700160
Brian Salomon1edc5b92016-11-29 13:43:46 -0500161 const GrShaderCaps* shaderCaps = fProgramBuilder->shaderCaps();
162 if (shaderCaps->fbFetchSupport()) {
cdaltonc08f1962016-02-12 12:14:06 -0800163 this->addFeature(1 << kFramebufferFetch_GLSLPrivateFeature,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500164 shaderCaps->fbFetchExtensionString());
joshualittb4384b92014-10-21 12:53:15 -0700165
joshualitt3c1096f2015-01-13 13:13:59 -0800166 // Some versions of this extension string require declaring custom color output on ES 3.0+
Ethan Nicholaseab2baa2018-04-13 15:16:27 -0400167 const char* fbFetchColorName = "sk_LastFragColor";
Brian Salomon1edc5b92016-11-29 13:43:46 -0500168 if (shaderCaps->fbFetchNeedsCustomOutput()) {
joshualittb4384b92014-10-21 12:53:15 -0700169 this->enableCustomOutput();
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400170 fCustomColorOutput->setTypeModifier(GrShaderVar::TypeModifier::InOut);
egdaniel8dcdedc2015-11-11 06:27:20 -0800171 fbFetchColorName = DeclaredColorOutputName();
egdaniel138c2632016-08-17 10:59:00 -0700172 // Set the dstColor to an intermediate variable so we don't override it with the output
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400173 this->codeAppendf("half4 %s = %s;", kDstColorName, fbFetchColorName);
egdanielbd4121a2016-08-17 12:55:30 -0700174 } else {
175 return fbFetchColorName;
joshualittb4384b92014-10-21 12:53:15 -0700176 }
halcanary9d524f22016-03-29 09:03:52 -0700177 }
egdaniel138c2632016-08-17 10:59:00 -0700178 return kDstColorName;
joshualitt47bb3822014-10-07 16:43:25 -0700179}
180
egdaniel2d721d32015-11-11 13:06:05 -0800181void GrGLSLFragmentShaderBuilder::enableAdvancedBlendEquationIfNeeded(GrBlendEquation equation) {
cdalton8917d622015-05-06 13:40:21 -0700182 SkASSERT(GrBlendEquationIsAdvanced(equation));
183
Brian Osman4882e9a2021-02-22 17:17:46 -0500184 if (fProgramBuilder->shaderCaps()->mustEnableAdvBlendEqs()) {
185 this->addFeature(1 << kBlendEquationAdvanced_GLSLPrivateFeature,
186 "GL_KHR_blend_equation_advanced");
cdalton8917d622015-05-06 13:40:21 -0700187 this->addLayoutQualifier("blend_support_all_equations", kOut_InterfaceQualifier);
188 }
189}
190
egdaniel2d721d32015-11-11 13:06:05 -0800191void GrGLSLFragmentShaderBuilder::enableCustomOutput() {
Michael Ludwig45191342020-03-24 12:29:39 -0400192 if (!fCustomColorOutput) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400193 fCustomColorOutput = &fOutputs.emplace_back(DeclaredColorOutputName(), kHalf4_GrSLType,
194 GrShaderVar::TypeModifier::Out);
Ben Wagner63fd7602017-10-09 15:45:33 -0400195 fProgramBuilder->finalizeFragmentOutputColor(fOutputs.back());
joshualittb4384b92014-10-21 12:53:15 -0700196 }
joshualitt47bb3822014-10-07 16:43:25 -0700197}
198
egdaniel2d721d32015-11-11 13:06:05 -0800199void GrGLSLFragmentShaderBuilder::enableSecondaryOutput() {
joshualitt47bb3822014-10-07 16:43:25 -0700200 SkASSERT(!fHasSecondaryOutput);
201 fHasSecondaryOutput = true;
Brian Salomon94efbf52016-11-29 13:43:05 -0500202 const GrShaderCaps& caps = *fProgramBuilder->shaderCaps();
egdaniel8dcdedc2015-11-11 06:27:20 -0800203 if (const char* extension = caps.secondaryOutputExtensionString()) {
204 this->addFeature(1 << kBlendFuncExtended_GLSLPrivateFeature, extension);
kkinnunend94708e2015-07-30 22:47:04 -0700205 }
206
207 // If the primary output is declared, we must declare also the secondary output
208 // and vice versa, since it is not allowed to use a built-in gl_FragColor and a custom
John Stiles72664be2020-09-16 17:43:11 -0400209 // output. The condition also co-incides with the condition in which GLES SL 2.0
kkinnunend94708e2015-07-30 22:47:04 -0700210 // requires the built-in gl_SecondaryFragColorEXT, where as 3.0 requires a custom output.
kkinnunend94708e2015-07-30 22:47:04 -0700211 if (caps.mustDeclareFragmentShaderOutput()) {
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400212 fOutputs.emplace_back(DeclaredSecondaryColorOutputName(), kHalf4_GrSLType,
213 GrShaderVar::TypeModifier::Out);
egdanielb80ec8b2016-02-09 09:54:43 -0800214 fProgramBuilder->finalizeFragmentSecondaryColor(fOutputs.back());
kkinnunend94708e2015-07-30 22:47:04 -0700215 }
joshualitt47bb3822014-10-07 16:43:25 -0700216}
217
egdaniel2d721d32015-11-11 13:06:05 -0800218const char* GrGLSLFragmentShaderBuilder::getPrimaryColorOutputName() const {
Michael Ludwig45191342020-03-24 12:29:39 -0400219 return this->hasCustomColorOutput() ? DeclaredColorOutputName() : "sk_FragColor";
joshualitt47bb3822014-10-07 16:43:25 -0700220}
221
Brian Salomondc092132018-04-04 10:14:16 -0400222bool GrGLSLFragmentShaderBuilder::primaryColorOutputIsInOut() const {
Michael Ludwig45191342020-03-24 12:29:39 -0400223 return fCustomColorOutput &&
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400224 fCustomColorOutput->getTypeModifier() == GrShaderVar::TypeModifier::InOut;
Brian Salomondc092132018-04-04 10:14:16 -0400225}
226
egdaniel2d721d32015-11-11 13:06:05 -0800227const char* GrGLSLFragmentShaderBuilder::getSecondaryColorOutputName() const {
Chris Dalton6b76df02019-04-08 11:01:34 -0600228 if (this->hasSecondaryOutput()) {
229 return (fProgramBuilder->shaderCaps()->mustDeclareFragmentShaderOutput())
230 ? DeclaredSecondaryColorOutputName()
231 : "gl_SecondaryFragColorEXT";
232 }
233 return nullptr;
joshualitt47bb3822014-10-07 16:43:25 -0700234}
235
cdalton28f45b92016-03-07 13:58:26 -0800236GrSurfaceOrigin GrGLSLFragmentShaderBuilder::getSurfaceOrigin() const {
Robert Phillips7de13332019-10-09 15:44:54 -0400237 return fProgramBuilder->origin();
cdalton28f45b92016-03-07 13:58:26 -0800238}
239
egdaniel2d721d32015-11-11 13:06:05 -0800240void GrGLSLFragmentShaderBuilder::onFinalize() {
Robert Phillips7de13332019-10-09 15:44:54 -0400241 SkASSERT(fProgramBuilder->processorFeatures() == fUsedProcessorFeaturesAllStages_DebugOnly);
Chris Daltond7291ba2019-03-07 14:17:03 -0700242
Robert Phillips7de13332019-10-09 15:44:54 -0400243 if (CustomFeatures::kSampleLocations & fProgramBuilder->processorFeatures()) {
Robert Phillips65a77752019-10-02 15:22:24 -0400244 const SkTArray<SkPoint>& sampleLocations = fProgramBuilder->getSampleLocations();
Chris Dalton2284aab2019-11-15 11:02:24 -0700245 this->definitions().appendf("const float2 _sampleOffsets[%i] = float2[%i](",
246 sampleLocations.count(), sampleLocations.count());
Chris Daltond7291ba2019-03-07 14:17:03 -0700247 for (int i = 0; i < sampleLocations.count(); ++i) {
248 SkPoint offset = sampleLocations[i] - SkPoint::Make(.5f, .5f);
249 if (kBottomLeft_GrSurfaceOrigin == this->getSurfaceOrigin()) {
250 offset.fY = -offset.fY;
251 }
252 this->definitions().appendf("float2(%f, %f)", offset.x(), offset.y());
253 this->definitions().append((i + 1 != sampleLocations.count()) ? ", " : ");");
254 }
255 }
256
egdaniel0eafe792015-11-20 14:01:22 -0800257 fProgramBuilder->varyingHandler()->getFragDecls(&this->inputs(), &this->outputs());
joshualitt30ba4362014-08-21 20:18:45 -0700258}
259
cdalton85285412016-02-18 12:37:07 -0800260void GrGLSLFragmentShaderBuilder::onBeforeChildProcEmitCode() {
wangyix54a6b1a2015-09-08 08:41:51 -0700261 SkASSERT(fSubstageIndices.count() >= 1);
wangyix7ef45a12015-08-13 06:51:35 -0700262 fSubstageIndices.push_back(0);
wangyix54a6b1a2015-09-08 08:41:51 -0700263 // second-to-last value in the fSubstageIndices stack is the index of the child proc
264 // at that level which is currently emitting code.
265 fMangleString.appendf("_c%d", fSubstageIndices[fSubstageIndices.count() - 2]);
wangyix7ef45a12015-08-13 06:51:35 -0700266}
267
cdalton85285412016-02-18 12:37:07 -0800268void GrGLSLFragmentShaderBuilder::onAfterChildProcEmitCode() {
wangyix54a6b1a2015-09-08 08:41:51 -0700269 SkASSERT(fSubstageIndices.count() >= 2);
wangyix7ef45a12015-08-13 06:51:35 -0700270 fSubstageIndices.pop_back();
wangyix54a6b1a2015-09-08 08:41:51 -0700271 fSubstageIndices.back()++;
wangyix7ef45a12015-08-13 06:51:35 -0700272 int removeAt = fMangleString.findLastOf('_');
273 fMangleString.remove(removeAt, fMangleString.size() - removeAt);
274}