blob: 1ab7af6b1b4e6606fdbd217e79498306f39bdb26 [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
8#ifndef GrGLFragmentShaderBuilder_DEFINED
9#define GrGLFragmentShaderBuilder_DEFINED
joshualitt47bb3822014-10-07 16:43:25 -070010
joshualitt30ba4362014-08-21 20:18:45 -070011#include "GrGLShaderBuilder.h"
12
joshualitt74077b92014-10-24 11:26:03 -070013class GrGLVarying;
14
joshualittb0a8a372014-09-23 09:50:21 -070015/*
joshualitt47bb3822014-10-07 16:43:25 -070016 * This base class encapsulates the functionality which the GP uses to build fragment shaders
joshualittb0a8a372014-09-23 09:50:21 -070017 */
egdaniel29bee0f2015-04-29 11:54:42 -070018class GrGLFragmentBuilder : public GrGLShaderBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070019public:
egdaniel29bee0f2015-04-29 11:54:42 -070020 GrGLFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {}
21 virtual ~GrGLFragmentBuilder() {}
joshualittb0a8a372014-09-23 09:50:21 -070022 /**
23 * Use of these features may require a GLSL extension to be enabled. Shaders may not compile
24 * if code is added that uses one of these features without calling enableFeature()
25 */
26 enum GLSLFeature {
27 kStandardDerivatives_GLSLFeature = 0,
28 kLastGLSLFeature = kStandardDerivatives_GLSLFeature
29 };
30
31 /**
32 * If the feature is supported then true is returned and any necessary #extension declarations
33 * are added to the shaders. If the feature is not supported then false will be returned.
34 */
35 virtual bool enableFeature(GLSLFeature) = 0;
36
37 /**
38 * This returns a variable name to access the 2D, perspective correct version of the coords in
39 * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a
40 * perspective divide into the fragment shader (xy / z) to convert them to 2D.
41 */
42 virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords,
43 int index) = 0;
44
45
46 /** Returns a variable name that represents the position of the fragment in the FS. The position
47 is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */
48 virtual const char* fragmentPosition() = 0;
49
50private:
jvanverth50530632015-04-27 10:36:27 -070051 friend class GrGLPathProcessor;
joshualittabb52a12015-01-13 15:02:10 -080052
joshualittb0a8a372014-09-23 09:50:21 -070053 typedef GrGLShaderBuilder INHERITED;
54};
55
56/*
57 * Fragment processor's, in addition to all of the above, may need to use dst color so they use
joshualitt47bb3822014-10-07 16:43:25 -070058 * this builder to create their shader. Because this is the only shader builder the FP sees, we
59 * just call it FPShaderBuilder
joshualittb0a8a372014-09-23 09:50:21 -070060 */
egdaniel29bee0f2015-04-29 11:54:42 -070061class GrGLXPFragmentBuilder : public GrGLFragmentBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070062public:
egdaniel29bee0f2015-04-29 11:54:42 -070063 GrGLXPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {}
joshualitt47bb3822014-10-07 16:43:25 -070064
joshualittb0a8a372014-09-23 09:50:21 -070065 /** Returns the variable name that holds the color of the destination pixel. This may be NULL if
66 no effect advertised that it will read the destination. */
67 virtual const char* dstColor() = 0;
68
cdalton8917d622015-05-06 13:40:21 -070069 /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
70 this shader. It is only legal to call this method with an advanced blend equation, and only
71 if these equations are supported. */
72 virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
73
joshualittb0a8a372014-09-23 09:50:21 -070074private:
egdaniel29bee0f2015-04-29 11:54:42 -070075 typedef GrGLFragmentBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -070076};
77
joshualitt47bb3822014-10-07 16:43:25 -070078// TODO rename to Fragment Builder
egdaniel29bee0f2015-04-29 11:54:42 -070079class GrGLFragmentShaderBuilder : public GrGLXPFragmentBuilder {
joshualitt30ba4362014-08-21 20:18:45 -070080public:
81 typedef uint8_t DstReadKey;
82 typedef uint8_t FragPosKey;
83
84 /** Returns a key for adding code to read the copy-of-dst color in service of effects that
85 require reading the dst. It must not return 0 because 0 indicates that there is no dst
86 copy read at all (in which case this function should not be called). */
87 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&);
88
89 /** Returns a key for reading the fragment location. This should only be called if there is an
90 effect that will requires the fragment position. If the fragment position is not required,
91 the key is 0. */
92 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&);
93
joshualitt79f8fae2014-10-28 17:59:26 -070094 GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, uint8_t fragPosKey);
joshualitt30ba4362014-08-21 20:18:45 -070095
joshualitt47bb3822014-10-07 16:43:25 -070096 // true public interface, defined explicitly in the abstract interfaces above
mtklein36352bf2015-03-25 18:17:31 -070097 bool enableFeature(GLSLFeature) override;
joshualittdb0d3ca2014-10-07 12:42:26 -070098 virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords,
mtklein36352bf2015-03-25 18:17:31 -070099 int index) override;
100 const char* fragmentPosition() override;
101 const char* dstColor() override;
joshualittdb0d3ca2014-10-07 12:42:26 -0700102
cdalton8917d622015-05-06 13:40:21 -0700103 void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
104
joshualitt74077b92014-10-24 11:26:03 -0700105private:
joshualitt47bb3822014-10-07 16:43:25 -0700106 // Private public interface, used by GrGLProgramBuilder to build a fragment shader
joshualitt47bb3822014-10-07 16:43:25 -0700107 void enableCustomOutput();
108 void enableSecondaryOutput();
109 const char* getPrimaryColorOutputName() const;
110 const char* getSecondaryColorOutputName() const;
joshualitt43466a12015-02-13 17:18:27 -0800111 bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds);
joshualitt47bb3822014-10-07 16:43:25 -0700112 void bindFragmentShaderLocations(GrGLuint programID);
113
joshualitt47bb3822014-10-07 16:43:25 -0700114 // As GLProcessors emit code, there are some conditions we need to verify. We use the below
115 // state to track this. The reset call is called per processor emitted.
116 bool hasReadDstColor() const { return fHasReadDstColor; }
117 bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; }
118 void reset() {
119 fHasReadDstColor = false;
120 fHasReadFragmentPosition = false;
121 }
joshualitt30ba4362014-08-21 20:18:45 -0700122
joshualitt74077b92014-10-24 11:26:03 -0700123 /*
124 * An internal call for GrGLProgramBuilder to use to add varyings to the vertex shader
125 */
bsalomonc0bd6482014-12-09 10:04:14 -0800126 void addVarying(GrGLVarying*, GrSLPrecision);
joshualitt74077b92014-10-24 11:26:03 -0700127
joshualitt30ba4362014-08-21 20:18:45 -0700128 /**
129 * Features that should only be enabled by GrGLFragmentShaderBuilder itself.
130 */
131 enum GLSLPrivateFeature {
132 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1,
cdalton8917d622015-05-06 13:40:21 -0700133 kBlendEquationAdvanced_GLSLPrivateFeature,
134 kLastGLSLPrivateFeature = kBlendEquationAdvanced_GLSLPrivateFeature
joshualitt30ba4362014-08-21 20:18:45 -0700135 };
136
137 // Interpretation of DstReadKey when generating code
138 enum {
139 kNoDstRead_DstReadKey = 0,
140 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read.
141 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only.
142 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left.
143 };
144
joshualitt47bb3822014-10-07 16:43:25 -0700145 // Interpretation of FragPosKey when generating code
joshualitt30ba4362014-08-21 20:18:45 -0700146 enum {
147 kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed.
148 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left.
149 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left.
150 };
151
joshualitt47bb3822014-10-07 16:43:25 -0700152 static const char* kDstCopyColorName;
153
joshualitt30ba4362014-08-21 20:18:45 -0700154 bool fHasCustomColorOutput;
155 bool fHasSecondaryOutput;
156 bool fSetupFragPosition;
157 bool fTopLeftFragPosRead;
joshualittb4384b92014-10-21 12:53:15 -0700158 int fCustomColorOutputIndex;
joshualitt30ba4362014-08-21 20:18:45 -0700159
joshualitt47bb3822014-10-07 16:43:25 -0700160 // some state to verify shaders and effects are consistent, this is reset between effects by
161 // the program creator
162 bool fHasReadDstColor;
163 bool fHasReadFragmentPosition;
joshualittfe1233c2014-10-07 12:16:35 -0700164
joshualitt47bb3822014-10-07 16:43:25 -0700165 friend class GrGLProgramBuilder;
166
egdaniel29bee0f2015-04-29 11:54:42 -0700167 typedef GrGLXPFragmentBuilder INHERITED;
joshualitt30ba4362014-08-21 20:18:45 -0700168};
169
170#endif