OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrGLFragmentShaderBuilder_DEFINED | 8 #ifndef GrGLFragmentShaderBuilder_DEFINED |
9 #define GrGLFragmentShaderBuilder_DEFINED | 9 #define GrGLFragmentShaderBuilder_DEFINED |
| 10 |
10 #include "GrGLShaderBuilder.h" | 11 #include "GrGLShaderBuilder.h" |
11 | 12 |
12 class GrGLProgramBuilder; | |
13 | |
14 /* | 13 /* |
15 * This base class encapsulates the functionality which all GrProcessors are all
owed to use in their | 14 * This base class encapsulates the functionality which the GP uses to build fra
gment shaders |
16 * fragment shader | |
17 */ | 15 */ |
18 class GrGLProcessorFragmentShaderBuilder : public GrGLShaderBuilder { | 16 class GrGLGPFragmentBuilder : public GrGLShaderBuilder { |
19 public: | 17 public: |
20 GrGLProcessorFragmentShaderBuilder(GrGLProgramBuilder* program) : INHERITED(
program) {} | 18 GrGLGPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {} |
21 virtual ~GrGLProcessorFragmentShaderBuilder() {} | 19 virtual ~GrGLGPFragmentBuilder() {} |
22 /** | 20 /** |
23 * Use of these features may require a GLSL extension to be enabled. Shaders
may not compile | 21 * 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 enableFe
ature() | 22 * if code is added that uses one of these features without calling enableFe
ature() |
25 */ | 23 */ |
26 enum GLSLFeature { | 24 enum GLSLFeature { |
27 kStandardDerivatives_GLSLFeature = 0, | 25 kStandardDerivatives_GLSLFeature = 0, |
28 kLastGLSLFeature = kStandardDerivatives_GLSLFeature | 26 kLastGLSLFeature = kStandardDerivatives_GLSLFeature |
29 }; | 27 }; |
30 | 28 |
31 /** | 29 /** |
(...skipping 14 matching lines...) Expand all Loading... |
46 /** Returns a variable name that represents the position of the fragment in
the FS. The position | 44 /** 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 ha
lf-integers). */ | 45 is in device space (e.g. 0,0 is the top left and pixel centers are at ha
lf-integers). */ |
48 virtual const char* fragmentPosition() = 0; | 46 virtual const char* fragmentPosition() = 0; |
49 | 47 |
50 private: | 48 private: |
51 typedef GrGLShaderBuilder INHERITED; | 49 typedef GrGLShaderBuilder INHERITED; |
52 }; | 50 }; |
53 | 51 |
54 /* | 52 /* |
55 * Fragment processor's, in addition to all of the above, may need to use dst co
lor so they use | 53 * Fragment processor's, in addition to all of the above, may need to use dst co
lor so they use |
56 * this builder to create their shader | 54 * this builder to create their shader. Because this is the only shader builder
the FP sees, we |
| 55 * just call it FPShaderBuilder |
57 */ | 56 */ |
58 class GrGLFragmentProcessorShaderBuilder : public GrGLProcessorFragmentShaderBui
lder { | 57 class GrGLFPFragmentBuilder : public GrGLGPFragmentBuilder { |
59 public: | 58 public: |
60 GrGLFragmentProcessorShaderBuilder(GrGLProgramBuilder* program) : INHERITED(
program) {} | 59 GrGLFPFragmentBuilder(GrGLProgramBuilder* program) : INHERITED(program) {} |
| 60 |
61 /** Returns the variable name that holds the color of the destination pixel.
This may be NULL if | 61 /** 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. */ | 62 no effect advertised that it will read the destination. */ |
63 virtual const char* dstColor() = 0; | 63 virtual const char* dstColor() = 0; |
64 | 64 |
65 private: | 65 private: |
66 typedef GrGLProcessorFragmentShaderBuilder INHERITED; | 66 typedef GrGLGPFragmentBuilder INHERITED; |
67 }; | 67 }; |
68 | 68 |
69 class GrGLFragmentShaderBuilder : public GrGLFragmentProcessorShaderBuilder { | 69 // TODO rename to Fragment Builder |
| 70 class GrGLFragmentShaderBuilder : public GrGLFPFragmentBuilder { |
70 public: | 71 public: |
71 typedef uint8_t DstReadKey; | 72 typedef uint8_t DstReadKey; |
72 typedef uint8_t FragPosKey; | 73 typedef uint8_t FragPosKey; |
73 | 74 |
74 /** Returns a key for adding code to read the copy-of-dst color in service
of effects that | 75 /** Returns a key for adding code to read the copy-of-dst color in service
of effects that |
75 require reading the dst. It must not return 0 because 0 indicates that t
here is no dst | 76 require reading the dst. It must not return 0 because 0 indicates that t
here is no dst |
76 copy read at all (in which case this function should not be called). */ | 77 copy read at all (in which case this function should not be called). */ |
77 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); | 78 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); |
78 | 79 |
79 /** Returns a key for reading the fragment location. This should only be cal
led if there is an | 80 /** Returns a key for reading the fragment location. This should only be cal
led if there is an |
80 effect that will requires the fragment position. If the fragment position
is not required, | 81 effect that will requires the fragment position. If the fragment position
is not required, |
81 the key is 0. */ | 82 the key is 0. */ |
82 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const Gr
GLCaps&); | 83 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const Gr
GLCaps&); |
83 | 84 |
84 GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, const GrGLProgramDesc
& desc); | 85 GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, const GrGLProgramDesc
& desc); |
85 | 86 |
| 87 // true public interface, defined explicitly in the abstract interfaces abov
e |
| 88 virtual bool enableFeature(GLSLFeature) SK_OVERRIDE; |
| 89 virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArra
y& coords, |
| 90 int index) SK_OVERRIDE; |
| 91 virtual const char* fragmentPosition() SK_OVERRIDE; |
86 virtual const char* dstColor() SK_OVERRIDE; | 92 virtual const char* dstColor() SK_OVERRIDE; |
87 | 93 |
88 virtual bool enableFeature(GLSLFeature) SK_OVERRIDE; | 94 // 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 GrGLSLExpr
4& inputCoverage); |
| 102 bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shader
Ids) const; |
| 103 void bindFragmentShaderLocations(GrGLuint programID); |
89 | 104 |
90 virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArra
y& coords, | |
91 int index) SK_OVERRIDE; | |
92 | |
93 virtual const char* fragmentPosition() SK_OVERRIDE; | |
94 | |
95 private: | |
96 /* | 105 /* |
97 * An internal call for GrGLFullProgramBuilder to use to add varyings to the
vertex shader | 106 * An internal call for GrGLProgramBuilder to use to add varyings to the ver
tex shader |
98 */ | 107 */ |
99 void addVarying(GrSLType type, | 108 void addVarying(GrSLType type, |
100 const char* name, | 109 const char* name, |
101 const char** fsInName, | 110 const char** fsInName, |
102 GrGLShaderVar::Precision fsPrecision = GrGLShaderVar::kDefaul
t_Precision); | 111 GrGLShaderVar::Precision fsPrecision = GrGLShaderVar::kDefaul
t_Precision); |
103 | 112 |
104 /* | 113 // As GLProcessors emit code, there are some conditions we need to verify.
We use the below |
105 * Private functions used by GrGLProgramBuilder for compilation | 114 // state to track this. The reset call is called per processor emitted. |
106 */ | 115 bool hasReadDstColor() const { return fHasReadDstColor; } |
107 void bindProgramLocations(GrGLuint programId); | 116 bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; } |
108 bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shader
Ids) const; | 117 void reset() { |
109 void emitCodeBeforeEffects(); | 118 fHasReadDstColor = false; |
110 void emitCodeAfterEffects(const GrGLSLExpr4& inputColor, const GrGLSLExpr4&
inputCoverage); | 119 fHasReadFragmentPosition = false; |
| 120 } |
111 | 121 |
112 /** Enables using the secondary color output and returns the name of the var
in which it is | 122 private: |
113 to be stored */ | |
114 const char* enableSecondaryOutput(); | |
115 | |
116 /** Gets the name of the primary color output. */ | |
117 const char* getColorOutputName() const; | |
118 | |
119 /** | 123 /** |
120 * Features that should only be enabled by GrGLFragmentShaderBuilder itself. | 124 * Features that should only be enabled by GrGLFragmentShaderBuilder itself. |
121 */ | 125 */ |
122 enum GLSLPrivateFeature { | 126 enum GLSLPrivateFeature { |
123 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, | 127 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, |
124 kLastGLSLPrivateFeature = kFragCoordConventions_GLSLPrivateFeature | 128 kLastGLSLPrivateFeature = kFragCoordConventions_GLSLPrivateFeature |
125 }; | 129 }; |
126 | 130 |
127 // Interpretation of DstReadKey when generating code | 131 // Interpretation of DstReadKey when generating code |
128 enum { | 132 enum { |
129 kNoDstRead_DstReadKey = 0, | 133 kNoDstRead_DstReadKey = 0, |
130 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. | 134 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. |
131 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alph
a only. | 135 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alph
a only. |
132 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-
left. | 136 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-
left. |
133 }; | 137 }; |
134 | 138 |
| 139 // Interpretation of FragPosKey when generating code |
135 enum { | 140 enum { |
136 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil
l not be needed. | 141 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil
l not be needed. |
137 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t
op-left. | 142 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t
op-left. |
138 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b
ottom-left. | 143 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b
ottom-left. |
139 }; | 144 }; |
140 | 145 |
| 146 static const char* kDstCopyColorName; |
| 147 |
141 bool fHasCustomColorOutput; | 148 bool fHasCustomColorOutput; |
142 bool fHasSecondaryOutput; | 149 bool fHasSecondaryOutput; |
143 bool fSetupFragPosition; | 150 bool fSetupFragPosition; |
144 bool fTopLeftFragPosRead; | 151 bool fTopLeftFragPosRead; |
145 | 152 |
| 153 // some state to verify shaders and effects are consistent, this is reset be
tween effects by |
| 154 // the program creator |
| 155 bool fHasReadDstColor; |
| 156 bool fHasReadFragmentPosition; |
| 157 |
| 158 friend class GrGLNvprProgramBuilder; |
146 friend class GrGLProgramBuilder; | 159 friend class GrGLProgramBuilder; |
147 friend class GrGLFullProgramBuilder; | |
148 | 160 |
149 typedef GrGLFragmentProcessorShaderBuilder INHERITED; | 161 typedef GrGLFPFragmentBuilder INHERITED; |
150 }; | 162 }; |
151 | 163 |
152 #endif | 164 #endif |
OLD | NEW |