blob: fdf685cc09bcc32c28e2eec296979b170861eaad [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
joshualittfe1233c2014-10-07 12:16:35 -070010
joshualitt30ba4362014-08-21 20:18:45 -070011#include "GrGLShaderBuilder.h"
12
joshualittb0a8a372014-09-23 09:50:21 -070013/*
joshualittfe1233c2014-10-07 12:16:35 -070014 * This base class encapsulates the functionality which the GP uses to build fragment shaders
joshualittb0a8a372014-09-23 09:50:21 -070015 */
joshualittfe1233c2014-10-07 12:16:35 -070016class GrGLGPFragmentBuilder : public GrGLShaderBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070017public:
joshualittfe1233c2014-10-07 12:16:35 -070018 GrGLGPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {}
19 virtual ~GrGLGPFragmentBuilder() {}
joshualittb0a8a372014-09-23 09:50:21 -070020 /**
21 * Use of these features may require a GLSL extension to be enabled. Shaders may not compile
22 * if code is added that uses one of these features without calling enableFeature()
23 */
24 enum GLSLFeature {
25 kStandardDerivatives_GLSLFeature = 0,
26 kLastGLSLFeature = kStandardDerivatives_GLSLFeature
27 };
28
29 /**
30 * If the feature is supported then true is returned and any necessary #extension declarations
31 * are added to the shaders. If the feature is not supported then false will be returned.
32 */
33 virtual bool enableFeature(GLSLFeature) = 0;
34
35 /**
36 * This returns a variable name to access the 2D, perspective correct version of the coords in
37 * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a
38 * perspective divide into the fragment shader (xy / z) to convert them to 2D.
39 */
40 virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords,
41 int index) = 0;
42
43
44 /** Returns a variable name that represents the position of the fragment in the FS. The position
45 is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */
46 virtual const char* fragmentPosition() = 0;
47
48private:
49 typedef GrGLShaderBuilder INHERITED;
50};
51
52/*
53 * Fragment processor's, in addition to all of the above, may need to use dst color so they use
joshualittfe1233c2014-10-07 12:16:35 -070054 * this builder to create their shader. Because this is the only shader builder the FP sees, we
55 * just call it FPShaderBuilder
joshualittb0a8a372014-09-23 09:50:21 -070056 */
joshualittfe1233c2014-10-07 12:16:35 -070057class GrGLFPFragmentBuilder : public GrGLGPFragmentBuilder {
joshualittb0a8a372014-09-23 09:50:21 -070058public:
joshualittfe1233c2014-10-07 12:16:35 -070059 GrGLFPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {}
60
joshualittb0a8a372014-09-23 09:50:21 -070061 /** Returns the variable name that holds the color of the destination pixel. This may be NULL if
62 no effect advertised that it will read the destination. */
63 virtual const char* dstColor() = 0;
64
65private:
joshualittfe1233c2014-10-07 12:16:35 -070066 typedef GrGLGPFragmentBuilder INHERITED;
joshualittb0a8a372014-09-23 09:50:21 -070067};
68
joshualittfe1233c2014-10-07 12:16:35 -070069// TODO rename to Fragment Builder
70class GrGLFragmentShaderBuilder : public GrGLFPFragmentBuilder {
joshualitt30ba4362014-08-21 20:18:45 -070071public:
72 typedef uint8_t DstReadKey;
73 typedef uint8_t FragPosKey;
74
75 /** Returns a key for adding code to read the copy-of-dst color in service of effects that
76 require reading the dst. It must not return 0 because 0 indicates that there is no dst
77 copy read at all (in which case this function should not be called). */
78 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&);
79
80 /** Returns a key for reading the fragment location. This should only be called if there is an
81 effect that will requires the fragment position. If the fragment position is not required,
82 the key is 0. */
83 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&);
84
85 GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, const GrGLProgramDesc& desc);
86
joshualittfe1233c2014-10-07 12:16:35 -070087 // true public interface, defined explicitly in the abstract interfaces above
joshualittb0a8a372014-09-23 09:50:21 -070088 virtual bool enableFeature(GLSLFeature) SK_OVERRIDE;
joshualittb0a8a372014-09-23 09:50:21 -070089 virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords,
90 int index) SK_OVERRIDE;
joshualittb0a8a372014-09-23 09:50:21 -070091 virtual const char* fragmentPosition() SK_OVERRIDE;
joshualittfe1233c2014-10-07 12:16:35 -070092 virtual const char* dstColor() SK_OVERRIDE;
joshualitt30ba4362014-08-21 20:18:45 -070093
joshualittfe1233c2014-10-07 12:16:35 -070094 // Private public interface, used by GrGLProgramBuilder to build a fragment shader
95 void emitCodeToReadDstTexture();
96 void enableCustomOutput();
97 void enableSecondaryOutput();
98 const char* getPrimaryColorOutputName() const;
99 const char* getSecondaryColorOutputName() const;
100 void enableSecondaryOutput(const GrGLSLExpr4& inputColor, const GrGLSLExpr4& inputCoverage);
101 void combineColorAndCoverage(const GrGLSLExpr4& inputColor, const GrGLSLExpr4& inputCoverage);
102 bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds) const;
103 void bindFragmentShaderLocations(GrGLuint programID);
104
joshualitt30ba4362014-08-21 20:18:45 -0700105 /*
joshualittfe1233c2014-10-07 12:16:35 -0700106 * An internal call for GrGLProgramBuilder to use to add varyings to the vertex shader
joshualitt30ba4362014-08-21 20:18:45 -0700107 */
108 void addVarying(GrSLType type,
109 const char* name,
egdaniel6db91282014-09-02 08:02:38 -0700110 const char** fsInName,
111 GrGLShaderVar::Precision fsPrecision = GrGLShaderVar::kDefault_Precision);
joshualitt30ba4362014-08-21 20:18:45 -0700112
joshualittfe1233c2014-10-07 12:16:35 -0700113 // As GLProcessors emit code, there are some conditions we need to verify. We use the below
114 // state to track this. The reset call is called per processor emitted.
115 bool hasReadDstColor() const { return fHasReadDstColor; }
116 bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; }
117 void reset() {
118 fHasReadDstColor = false;
119 fHasReadFragmentPosition = false;
120 }
joshualitt30ba4362014-08-21 20:18:45 -0700121
joshualittfe1233c2014-10-07 12:16:35 -0700122private:
joshualitt30ba4362014-08-21 20:18:45 -0700123 /**
124 * Features that should only be enabled by GrGLFragmentShaderBuilder itself.
125 */
126 enum GLSLPrivateFeature {
127 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1,
128 kLastGLSLPrivateFeature = kFragCoordConventions_GLSLPrivateFeature
129 };
130
131 // Interpretation of DstReadKey when generating code
132 enum {
133 kNoDstRead_DstReadKey = 0,
134 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read.
135 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only.
136 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left.
137 };
138
joshualittfe1233c2014-10-07 12:16:35 -0700139 // Interpretation of FragPosKey when generating code
joshualitt30ba4362014-08-21 20:18:45 -0700140 enum {
141 kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed.
142 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left.
143 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left.
144 };
145
joshualittfe1233c2014-10-07 12:16:35 -0700146 static const char* kDstCopyColorName;
147
joshualitt30ba4362014-08-21 20:18:45 -0700148 bool fHasCustomColorOutput;
149 bool fHasSecondaryOutput;
150 bool fSetupFragPosition;
151 bool fTopLeftFragPosRead;
152
joshualittfe1233c2014-10-07 12:16:35 -0700153 // some state to verify shaders and effects are consistent, this is reset between effects by
154 // the program creator
155 bool fHasReadDstColor;
156 bool fHasReadFragmentPosition;
joshualitt30ba4362014-08-21 20:18:45 -0700157
joshualittfe1233c2014-10-07 12:16:35 -0700158 friend class GrGLNvprProgramBuilder;
159 friend class GrGLProgramBuilder;
160
161 typedef GrGLFPFragmentBuilder INHERITED;
joshualitt30ba4362014-08-21 20:18:45 -0700162};
163
164#endif