blob: aff0bc5f6e5e5b8f7b18b340505dd63a5da54e8d [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
egdaniel2d721d32015-11-11 13:06:05 -08008#ifndef GrGLSLFragmentShaderBuilder_DEFINED
9#define GrGLSLFragmentShaderBuilder_DEFINED
joshualitt47bb3822014-10-07 16:43:25 -070010
Brian Salomon096b0912019-08-14 16:56:13 -040011#include "src/gpu/GrBlend.h"
Brian Salomon48959462021-08-11 13:01:06 -040012#include "src/gpu/GrFragmentProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrProcessor.h"
14#include "src/gpu/glsl/GrGLSLShaderBuilder.h"
egdaniel7dc4bd02015-10-29 07:57:01 -070015
egdaniel574a4c12015-11-02 06:22:44 -080016class GrRenderTarget;
egdaniel8dcdedc2015-11-11 06:27:20 -080017class GrGLSLVarying;
joshualitt74077b92014-10-24 11:26:03 -070018
joshualittb0a8a372014-09-23 09:50:21 -070019/*
cdalton85285412016-02-18 12:37:07 -080020 * This class is used by fragment processors to build their fragment code.
joshualittb0a8a372014-09-23 09:50:21 -070021 */
Brian Salomonffd15ea2020-07-01 16:48:20 -040022class GrGLSLFPFragmentBuilder : virtual public GrGLSLShaderBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070023public:
Brian Salomonffd15ea2020-07-01 16:48:20 -040024 /** Appease the compiler; the derived class initializes GrGLSLShaderBuilder. */
25 GrGLSLFPFragmentBuilder() : GrGLSLShaderBuilder(nullptr) {
Michael Ludwig45191342020-03-24 12:29:39 -040026 // Suppress unused warning error
Kevin Lubickbe03ef12021-06-16 15:28:00 -040027 (void) fPadding;
Michael Ludwig45191342020-03-24 12:29:39 -040028 }
joshualitt47bb3822014-10-07 16:43:25 -070029
Chris Dalton0dffbab2019-03-27 13:08:50 -060030 enum class ScopeFlags {
31 // Every fragment will always execute this code, and will do it exactly once.
32 kTopLevel = 0,
33 // Either all fragments in a given primitive, or none, will execute this code.
34 kInsidePerPrimitiveBranch = (1 << 0),
35 // Any given fragment may or may not execute this code.
36 kInsidePerPixelBranch = (1 << 1),
37 // This code will be executed more than once.
38 kInsideLoop = (1 << 2)
Robert Phillips7f861922018-01-30 13:13:42 +000039 };
40
Ethan Nicholasf7b88202017-09-18 14:10:39 -040041 virtual void forceHighPrecision() = 0;
Michael Ludwig45191342020-03-24 12:29:39 -040042
John Stilesbb04e3d2021-06-04 12:09:11 -040043 /** Returns the variable name that holds the color of the destination pixel. This may be nullptr
44 * if no effect advertised that it will read the destination. */
45 virtual const char* dstColor() = 0;
46
Michael Ludwig45191342020-03-24 12:29:39 -040047private:
48 // WARNING: LIke GrRenderTargetProxy, changes to this can cause issues in ASAN. This is caused
Michael Ludwigc1ed11d2021-08-24 11:49:55 -040049 // by GrGLSLProgramBuilder's SkTBlockLists requiring 16 byte alignment, but since
Michael Ludwig45191342020-03-24 12:29:39 -040050 // GrGLSLFragmentShaderBuilder has a virtual diamond hierarchy, ASAN requires all this pointers
51 // to start aligned, even though clang is already correctly offsetting the individual fields
52 // that require the larger alignment. In the current world, this extra padding is sufficient to
53 // correctly initialize GrGLSLXPFragmentBuilder second.
Kevin Lubickbe03ef12021-06-16 15:28:00 -040054 char fPadding[4] = {};
cdalton85285412016-02-18 12:37:07 -080055};
56
Brian Osman2421b992021-06-19 10:44:54 -040057GR_MAKE_BITFIELD_CLASS_OPS(GrGLSLFPFragmentBuilder::ScopeFlags)
Chris Dalton0dffbab2019-03-27 13:08:50 -060058
cdalton85285412016-02-18 12:37:07 -080059/*
cdalton85285412016-02-18 12:37:07 -080060 * This class is used by Xfer processors to build their fragment code.
61 */
Brian Salomonffd15ea2020-07-01 16:48:20 -040062class GrGLSLXPFragmentBuilder : virtual public GrGLSLShaderBuilder {
cdalton85285412016-02-18 12:37:07 -080063public:
Brian Salomonffd15ea2020-07-01 16:48:20 -040064 /** Appease the compiler; the derived class initializes GrGLSLShaderBuilder. */
65 GrGLSLXPFragmentBuilder() : GrGLSLShaderBuilder(nullptr) {}
cdalton85285412016-02-18 12:37:07 -080066
67 virtual bool hasCustomColorOutput() const = 0;
68 virtual bool hasSecondaryOutput() const = 0;
69
70 /** Returns the variable name that holds the color of the destination pixel. This may be nullptr
71 * if no effect advertised that it will read the destination. */
joshualittb0a8a372014-09-23 09:50:21 -070072 virtual const char* dstColor() = 0;
73
cdalton8917d622015-05-06 13:40:21 -070074 /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
75 this shader. It is only legal to call this method with an advanced blend equation, and only
76 if these equations are supported. */
77 virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
joshualittb0a8a372014-09-23 09:50:21 -070078};
79
cdalton85285412016-02-18 12:37:07 -080080/*
81 * This class implements the various fragment builder interfaces.
82 */
Chris Dalton60283612018-02-14 13:38:14 -070083class GrGLSLFragmentShaderBuilder : public GrGLSLFPFragmentBuilder, public GrGLSLXPFragmentBuilder {
joshualitt30ba4362014-08-21 20:18:45 -070084public:
cdalton28f45b92016-03-07 13:58:26 -080085 GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program);
joshualitt30ba4362014-08-21 20:18:45 -070086
John Stilesbb04e3d2021-06-04 12:09:11 -040087 // Shared FP/XP interface.
88 const char* dstColor() override;
89
cdalton85285412016-02-18 12:37:07 -080090 // GrGLSLFPFragmentBuilder interface.
Ethan Nicholasf7b88202017-09-18 14:10:39 -040091 void forceHighPrecision() override { fForceHighPrecision = true; }
cdalton85285412016-02-18 12:37:07 -080092
93 // GrGLSLXPFragmentBuilder interface.
Michael Ludwig45191342020-03-24 12:29:39 -040094 bool hasCustomColorOutput() const override { return SkToBool(fCustomColorOutput); }
cdalton85285412016-02-18 12:37:07 -080095 bool hasSecondaryOutput() const override { return fHasSecondaryOutput; }
cdalton8917d622015-05-06 13:40:21 -070096 void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
97
joshualitt74077b92014-10-24 11:26:03 -070098private:
joshualitt47bb3822014-10-07 16:43:25 -070099 // Private public interface, used by GrGLProgramBuilder to build a fragment shader
joshualitt47bb3822014-10-07 16:43:25 -0700100 void enableCustomOutput();
101 void enableSecondaryOutput();
102 const char* getPrimaryColorOutputName() const;
103 const char* getSecondaryColorOutputName() const;
Brian Salomondc092132018-04-04 10:14:16 -0400104 bool primaryColorOutputIsInOut() const;
joshualitt47bb3822014-10-07 16:43:25 -0700105
cdalton87332102016-02-26 12:22:02 -0800106#ifdef SK_DEBUG
egdaniel57d3b032015-11-13 11:57:27 -0800107 // As GLSLProcessors emit code, there are some conditions we need to verify. We use the below
joshualitt47bb3822014-10-07 16:43:25 -0700108 // state to track this. The reset call is called per processor emitted.
Chris Daltond7291ba2019-03-07 14:17:03 -0700109 bool fHasReadDstColorThisStage_DebugOnly = false;
Chris Daltond7291ba2019-03-07 14:17:03 -0700110
111 void debugOnly_resetPerStageVerification() {
112 fHasReadDstColorThisStage_DebugOnly = false;
joshualitt47bb3822014-10-07 16:43:25 -0700113 }
cdalton87332102016-02-26 12:22:02 -0800114#endif
joshualitt30ba4362014-08-21 20:18:45 -0700115
ethannicholas5961bc92016-10-12 06:39:56 -0700116 static const char* DeclaredColorOutputName() { return "sk_FragColor"; }
egdaniel8dcdedc2015-11-11 06:27:20 -0800117 static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; }
118
cdalton28f45b92016-03-07 13:58:26 -0800119 GrSurfaceOrigin getSurfaceOrigin() const;
joshualitt74077b92014-10-24 11:26:03 -0700120
egdaniel574a4c12015-11-02 06:22:44 -0800121 void onFinalize() override;
joshualitt30ba4362014-08-21 20:18:45 -0700122
John Stilesbb04e3d2021-06-04 12:09:11 -0400123 static constexpr const char kDstColorName[] = "_dstColor";
joshualitt47bb3822014-10-07 16:43:25 -0700124
Michael Ludwig45191342020-03-24 12:29:39 -0400125 GrShaderVar* fCustomColorOutput = nullptr;
126
Chris Daltond7291ba2019-03-07 14:17:03 -0700127 bool fSetupFragPosition = false;
Chris Daltond7291ba2019-03-07 14:17:03 -0700128 bool fHasSecondaryOutput = false;
Chris Dalton0dffbab2019-03-27 13:08:50 -0600129 bool fHasModifiedSampleMask = false;
Chris Daltond7291ba2019-03-07 14:17:03 -0700130 bool fForceHighPrecision = false;
joshualittfe1233c2014-10-07 12:16:35 -0700131
egdanielfa896322016-01-13 12:19:30 -0800132 friend class GrGLSLProgramBuilder;
joshualitt47bb3822014-10-07 16:43:25 -0700133 friend class GrGLProgramBuilder;
Ethan Nicholas8f352ce2021-03-17 14:12:20 -0400134 friend class GrVkPipelineStateBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700135};
136
137#endif