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 all GrProcessors are all
owed to use in their |
16 * fragment shader | 15 * fragment shader |
17 */ | 16 */ |
18 class GrGLProcessorFragmentShaderBuilder : public GrGLShaderBuilder { | 17 class GrGLProcessorFragmentShaderBuilder : public GrGLShaderBuilder { |
19 public: | 18 public: |
20 GrGLProcessorFragmentShaderBuilder(GrGLProgramBuilder* program) : INHERITED(
program) {} | 19 GrGLProcessorFragmentShaderBuilder(GrGLProgramBuilder* program) : INHERITED(
program) {} |
21 virtual ~GrGLProcessorFragmentShaderBuilder() {} | 20 virtual ~GrGLProcessorFragmentShaderBuilder() {} |
22 /** | 21 /** |
23 * Use of these features may require a GLSL extension to be enabled. Shaders
may not compile | 22 * Use of these features may require a GLSL extension to be enabled. Shaders
may not compile |
(...skipping 24 matching lines...) Expand all Loading... |
48 virtual const char* fragmentPosition() = 0; | 47 virtual const char* fragmentPosition() = 0; |
49 | 48 |
50 private: | 49 private: |
51 typedef GrGLShaderBuilder INHERITED; | 50 typedef GrGLShaderBuilder INHERITED; |
52 }; | 51 }; |
53 | 52 |
54 /* | 53 /* |
55 * Fragment processor's, in addition to all of the above, may need to use dst co
lor so they use | 54 * 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 | 55 * this builder to create their shader |
57 */ | 56 */ |
58 class GrGLFragmentProcessorShaderBuilder : public GrGLProcessorFragmentShaderBui
lder { | 57 class GrGLFragmentProcessorFragmentShaderBuilder : public GrGLProcessorFragmentS
haderBuilder { |
59 public: | 58 public: |
60 GrGLFragmentProcessorShaderBuilder(GrGLProgramBuilder* program) : INHERITED(
program) {} | 59 GrGLFragmentProcessorFragmentShaderBuilder(GrGLProgramBuilder* program) : IN
HERITED(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 GrGLProcessorFragmentShaderBuilder INHERITED; |
67 }; | 67 }; |
68 | 68 |
69 class GrGLFragmentShaderBuilder : public GrGLFragmentProcessorShaderBuilder { | 69 class GrGLFragmentShaderBuilder : public GrGLFragmentProcessorFragmentShaderBuil
der { |
70 public: | 70 public: |
71 typedef uint8_t DstReadKey; | 71 typedef uint8_t DstReadKey; |
72 typedef uint8_t FragPosKey; | 72 typedef uint8_t FragPosKey; |
73 | 73 |
74 /** Returns a key for adding code to read the copy-of-dst color in service
of effects that | 74 /** 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 | 75 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). */ | 76 copy read at all (in which case this function should not be called). */ |
77 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); | 77 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); |
78 | 78 |
79 /** Returns a key for reading the fragment location. This should only be cal
led if there is an | 79 /** 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, | 80 effect that will requires the fragment position. If the fragment position
is not required, |
81 the key is 0. */ | 81 the key is 0. */ |
82 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const Gr
GLCaps&); | 82 static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const Gr
GLCaps&); |
83 | 83 |
84 GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, const GrGLProgramDesc
& desc); | 84 GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, const GrGLProgramDesc
& desc); |
85 | 85 |
| 86 // true public interface, defined explicitly in the abstract interfaces abov
e |
| 87 virtual bool enableFeature(GLSLFeature) SK_OVERRIDE; |
| 88 virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArra
y& coords, |
| 89 int index) SK_OVERRIDE; |
| 90 virtual const char* fragmentPosition() SK_OVERRIDE; |
86 virtual const char* dstColor() SK_OVERRIDE; | 91 virtual const char* dstColor() SK_OVERRIDE; |
87 | 92 |
88 virtual bool enableFeature(GLSLFeature) SK_OVERRIDE; | 93 // Private public interface, used by GrGLProgramBuilder to build a fragment
shader |
| 94 void emitCodeToReadDstTexture(); |
| 95 void enableCustomOutput(); |
| 96 void enableSecondaryOutput(); |
| 97 const char* getPrimaryColorOutputName() const; |
| 98 const char* getSecondaryColorOutputName() const; |
| 99 bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shader
Ids) const; |
| 100 void bindFragmentShaderLocations(GrGLuint programID); |
89 | 101 |
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 /* | 102 /* |
97 * An internal call for GrGLFullProgramBuilder to use to add varyings to the
vertex shader | 103 * An internal call for GrGLProgramBuilder to use to add varyings to the ver
tex shader |
98 */ | 104 */ |
99 void addVarying(GrSLType type, | 105 void addVarying(GrSLType type, |
100 const char* name, | 106 const char* name, |
101 const char** fsInName, | 107 const char** fsInName, |
102 GrGLShaderVar::Precision fsPrecision = GrGLShaderVar::kDefaul
t_Precision); | 108 GrGLShaderVar::Precision fsPrecision = GrGLShaderVar::kDefaul
t_Precision); |
103 | 109 |
104 /* | 110 // As GLProcessors emit code, there are some conditions we need to verify.
We use the below |
105 * Private functions used by GrGLProgramBuilder for compilation | 111 // state to track this. The reset call is called per processor emitted. |
106 */ | 112 bool hasReadDstColor() const { return fHasReadDstColor; } |
107 void bindProgramLocations(GrGLuint programId); | 113 bool hasReadFragmentPosition() const { return fHasReadFragmentPosition; } |
108 bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shader
Ids) const; | 114 void reset() { |
109 void emitCodeBeforeEffects(); | 115 fHasReadDstColor = false; |
110 void emitCodeAfterEffects(const GrGLSLExpr4& inputColor, const GrGLSLExpr4&
inputCoverage); | 116 fHasReadFragmentPosition = false; |
| 117 } |
111 | 118 |
112 /** Enables using the secondary color output and returns the name of the var
in which it is | 119 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 /** | 120 /** |
120 * Features that should only be enabled by GrGLFragmentShaderBuilder itself. | 121 * Features that should only be enabled by GrGLFragmentShaderBuilder itself. |
121 */ | 122 */ |
122 enum GLSLPrivateFeature { | 123 enum GLSLPrivateFeature { |
123 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, | 124 kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, |
124 kLastGLSLPrivateFeature = kFragCoordConventions_GLSLPrivateFeature | 125 kLastGLSLPrivateFeature = kFragCoordConventions_GLSLPrivateFeature |
125 }; | 126 }; |
126 | 127 |
127 // Interpretation of DstReadKey when generating code | 128 // Interpretation of DstReadKey when generating code |
128 enum { | 129 enum { |
129 kNoDstRead_DstReadKey = 0, | 130 kNoDstRead_DstReadKey = 0, |
130 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. | 131 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. |
131 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alph
a only. | 132 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alph
a only. |
132 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-
left. | 133 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-
left. |
133 }; | 134 }; |
134 | 135 |
| 136 // Interpretation of FragPosKey when generating code |
135 enum { | 137 enum { |
136 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil
l not be needed. | 138 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil
l not be needed. |
137 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t
op-left. | 139 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t
op-left. |
138 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b
ottom-left. | 140 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b
ottom-left. |
139 }; | 141 }; |
140 | 142 |
| 143 static const char* kDstCopyColorName; |
| 144 |
141 bool fHasCustomColorOutput; | 145 bool fHasCustomColorOutput; |
142 bool fHasSecondaryOutput; | 146 bool fHasSecondaryOutput; |
143 bool fSetupFragPosition; | 147 bool fSetupFragPosition; |
144 bool fTopLeftFragPosRead; | 148 bool fTopLeftFragPosRead; |
145 | 149 |
| 150 // some state to verify shaders and effects are consistent, this is reset be
tween effects by |
| 151 // the program creator |
| 152 bool fHasReadDstColor; |
| 153 bool fHasReadFragmentPosition; |
| 154 |
| 155 friend class GrGLESNvprProgramBuilder; |
146 friend class GrGLProgramBuilder; | 156 friend class GrGLProgramBuilder; |
147 friend class GrGLFullProgramBuilder; | |
148 | 157 |
149 typedef GrGLFragmentProcessorShaderBuilder INHERITED; | 158 typedef GrGLFragmentProcessorFragmentShaderBuilder INHERITED; |
150 }; | 159 }; |
151 | 160 |
152 #endif | 161 #endif |
OLD | NEW |