OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 GrGLSLFragmentProcessor_DEFINED | 8 #ifndef GrGLSLFragmentProcessor_DEFINED |
9 #define GrGLSLFragmentProcessor_DEFINED | 9 #define GrGLSLFragmentProcessor_DEFINED |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 | 26 |
27 virtual ~GrGLSLFragmentProcessor() { | 27 virtual ~GrGLSLFragmentProcessor() { |
28 for (int i = 0; i < fChildProcessors.count(); ++i) { | 28 for (int i = 0; i < fChildProcessors.count(); ++i) { |
29 delete fChildProcessors[i]; | 29 delete fChildProcessors[i]; |
30 } | 30 } |
31 } | 31 } |
32 | 32 |
33 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; | 33 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
34 typedef GrGLSLProgramDataManager::UniformHandle SamplerHandle; | 34 typedef GrGLSLProgramDataManager::UniformHandle SamplerHandle; |
35 | 35 |
| 36 private: |
36 /** | 37 /** |
37 * When building a program from a GrPipeline this is used to provide the GrS
haderVars that | 38 * This class allows the shader builder to provide each GrGLSLFragmentProces
or with an array of |
38 * contain the resulting transformed coords from each of a GrFragmentProcess
or's | 39 * generated variables where each generated variable corresponds to an eleme
nt of an array on |
39 * GrCoordTransforms. This allows the GrFragmentProcessor subclasses to refe
r to the transformed | 40 * the GrFragmentProcessor that generated the GLSLFP. For example, this is u
sed to provide a |
40 * coords in fragment code. | 41 * variable holding transformed coords for each GrCoordTransform owned by th
e FP. |
41 */ | 42 */ |
42 class TransformedCoordVars { | 43 template <typename T, typename FPBASE, int (FPBASE::*COUNT)() const> |
| 44 class BuilderInputProvider { |
43 public: | 45 public: |
44 TransformedCoordVars(const GrFragmentProcessor* fp, const GrShaderVar* v
ars) | 46 BuilderInputProvider(const GrFragmentProcessor* fp, const T* ts) : fFP(f
p) , fTs(ts) {} |
45 : fFP(fp) | |
46 , fTransformedVars(vars) {} | |
47 | 47 |
48 const GrShaderVar& operator[] (int i) const { | 48 const T& operator[] (int i) const { |
49 SkASSERT(i >= 0 && i < fFP->numCoordTransforms()); | 49 SkASSERT(i >= 0 && i < (fFP->*COUNT)()); |
50 return fTransformedVars[i]; | 50 return fTs[i]; |
51 } | 51 } |
52 | 52 |
53 TransformedCoordVars childTransforms(int childIdx) const; | 53 BuilderInputProvider childInputs(int childIdx) const { |
| 54 const GrFragmentProcessor* child = &fFP->childProcessor(childIdx); |
| 55 GrFragmentProcessor::Iter iter(fFP); |
| 56 int numToSkip = 0; |
| 57 while (true) { |
| 58 const GrFragmentProcessor* fp = iter.next(); |
| 59 if (fp == child) { |
| 60 return BuilderInputProvider(child, fTs + numToSkip); |
| 61 } |
| 62 numToSkip += (fp->*COUNT)(); |
| 63 } |
| 64 } |
54 | 65 |
55 private: | 66 private: |
56 const GrFragmentProcessor* fFP; | 67 const GrFragmentProcessor* fFP; |
57 const GrShaderVar* fTransformedVars; | 68 const T* fTs; |
58 }; | 69 }; |
59 | 70 |
| 71 public: |
| 72 using TransformedCoordVars = BuilderInputProvider<GrShaderVar, GrFragmentPro
cessor, |
| 73 &GrFragmentProcessor::numC
oordTransforms>; |
| 74 using TextureSamplers = BuilderInputProvider<SamplerHandle, GrProcessor, |
| 75 &GrProcessor::numTextures>; |
| 76 using BufferSamplers = BuilderInputProvider<SamplerHandle, GrProcessor, |
| 77 &GrProcessor::numBuffers>; |
| 78 |
60 /** Called when the program stage should insert its code into the shaders. T
he code in each | 79 /** Called when the program stage should insert its code into the shaders. T
he code in each |
61 shader will be in its own block ({}) and so locally scoped names will no
t collide across | 80 shader will be in its own block ({}) and so locally scoped names will no
t collide across |
62 stages. | 81 stages. |
63 | 82 |
64 @param fragBuilder Interface used to emit code in the shaders. | 83 @param fragBuilder Interface used to emit code in the shaders. |
65 @param fp The processor that generated this program stage
. | 84 @param fp The processor that generated this program stage
. |
66 @param key The key that was computed by GenKey() from the
generating | 85 @param key The key that was computed by GenKey() from the
generating |
67 GrProcessor. | 86 GrProcessor. |
68 @param outputColor A predefined vec4 in the FS in which the stage
should place its | 87 @param outputColor A predefined vec4 in the FS in which the stage
should place its |
69 output color (or coverage). | 88 output color (or coverage). |
(...skipping 13 matching lines...) Expand all Loading... |
83 generated code. | 102 generated code. |
84 */ | 103 */ |
85 struct EmitArgs { | 104 struct EmitArgs { |
86 EmitArgs(GrGLSLFPFragmentBuilder* fragBuilder, | 105 EmitArgs(GrGLSLFPFragmentBuilder* fragBuilder, |
87 GrGLSLUniformHandler* uniformHandler, | 106 GrGLSLUniformHandler* uniformHandler, |
88 const GrGLSLCaps* caps, | 107 const GrGLSLCaps* caps, |
89 const GrFragmentProcessor& fp, | 108 const GrFragmentProcessor& fp, |
90 const char* outputColor, | 109 const char* outputColor, |
91 const char* inputColor, | 110 const char* inputColor, |
92 const TransformedCoordVars& transformedCoordVars, | 111 const TransformedCoordVars& transformedCoordVars, |
93 const SamplerHandle* texSamplers, | 112 const TextureSamplers& textureSamplers, |
94 const SamplerHandle* bufferSamplers, | 113 const BufferSamplers& bufferSamplers, |
95 bool gpImplementsDistanceVector) | 114 bool gpImplementsDistanceVector) |
96 : fFragBuilder(fragBuilder) | 115 : fFragBuilder(fragBuilder) |
97 , fUniformHandler(uniformHandler) | 116 , fUniformHandler(uniformHandler) |
98 , fGLSLCaps(caps) | 117 , fGLSLCaps(caps) |
99 , fFp(fp) | 118 , fFp(fp) |
100 , fOutputColor(outputColor) | 119 , fOutputColor(outputColor) |
101 , fInputColor(inputColor) | 120 , fInputColor(inputColor) |
102 , fTransformedCoords(transformedCoordVars) | 121 , fTransformedCoords(transformedCoordVars) |
103 , fTexSamplers(texSamplers) | 122 , fTexSamplers(textureSamplers) |
104 , fBufferSamplers(bufferSamplers) | 123 , fBufferSamplers(bufferSamplers) |
105 , fGpImplementsDistanceVector(gpImplementsDistanceVector) {} | 124 , fGpImplementsDistanceVector(gpImplementsDistanceVector) {} |
106 GrGLSLFPFragmentBuilder* fFragBuilder; | 125 GrGLSLFPFragmentBuilder* fFragBuilder; |
107 GrGLSLUniformHandler* fUniformHandler; | 126 GrGLSLUniformHandler* fUniformHandler; |
108 const GrGLSLCaps* fGLSLCaps; | 127 const GrGLSLCaps* fGLSLCaps; |
109 const GrFragmentProcessor& fFp; | 128 const GrFragmentProcessor& fFp; |
110 const char* fOutputColor; | 129 const char* fOutputColor; |
111 const char* fInputColor; | 130 const char* fInputColor; |
112 const TransformedCoordVars& fTransformedCoords; | 131 const TransformedCoordVars& fTransformedCoords; |
113 const SamplerHandle* fTexSamplers; | 132 const TextureSamplers& fTexSamplers; |
114 const SamplerHandle* fBufferSamplers; | 133 const BufferSamplers& fBufferSamplers; |
115 bool fGpImplementsDistanceVector; | 134 bool fGpImplementsDistanceVector; |
116 }; | 135 }; |
117 | 136 |
118 virtual void emitCode(EmitArgs&) = 0; | 137 virtual void emitCode(EmitArgs&) = 0; |
119 | 138 |
120 void setData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcesso
r& processor); | 139 void setData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcesso
r& processor); |
121 | 140 |
122 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuil
der*) {} | 141 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuil
der*) {} |
123 | 142 |
124 int numChildProcessors() const { return fChildProcessors.count(); } | 143 int numChildProcessors() const { return fChildProcessors.count(); } |
125 | 144 |
126 GrGLSLFragmentProcessor* childProcessor(int index) const { | 145 GrGLSLFragmentProcessor* childProcessor(int index) { |
127 return fChildProcessors[index]; | 146 return fChildProcessors[index]; |
128 } | 147 } |
129 | 148 |
130 /** Will emit the code of a child proc in its own scope. Pass in the parent'
s EmitArgs and | 149 /** Will emit the code of a child proc in its own scope. Pass in the parent'
s EmitArgs and |
131 * emitChild will automatically extract the coords and samplers of that chi
ld and pass them | 150 * emitChild will automatically extract the coords and samplers of that chi
ld and pass them |
132 * on to the child's emitCode(). Also, any uniforms or functions emitted by
the child will | 151 * on to the child's emitCode(). Also, any uniforms or functions emitted by
the child will |
133 * have their names mangled to prevent redefinitions. The output color name
is also mangled | 152 * have their names mangled to prevent redefinitions. The output color name
is also mangled |
134 * therefore in an in/out param. It will be declared in mangled form by emi
tChild(). It is | 153 * therefore in an in/out param. It will be declared in mangled form by emi
tChild(). It is |
135 * legal to pass nullptr as inputColor, since all fragment processors are r
equired to work | 154 * legal to pass nullptr as inputColor, since all fragment processors are r
equired to work |
136 * without an input color. | 155 * without an input color. |
137 */ | 156 */ |
138 void emitChild(int childIndex, const char* inputColor, SkString* outputColor
, | 157 void emitChild(int childIndex, const char* inputColor, SkString* outputColor
, |
139 EmitArgs& parentArgs); | 158 EmitArgs& parentArgs); |
140 | 159 |
141 /** Variation that uses the parent's output color variable to hold the child
's output.*/ | 160 /** Variation that uses the parent's output color variable to hold the child
's output.*/ |
142 void emitChild(int childIndex, const char* inputColor, EmitArgs& parentArgs)
; | 161 void emitChild(int childIndex, const char* inputColor, EmitArgs& parentArgs)
; |
143 | 162 |
| 163 /** |
| 164 * Pre-order traversal of a GLSLFP hierarchy, or of multiple trees with root
s in an array of |
| 165 * GLSLFPS. This agrees with the traversal order of GrFragmentProcessor::Ite
r |
| 166 */ |
| 167 class Iter : public SkNoncopyable { |
| 168 public: |
| 169 explicit Iter(GrGLSLFragmentProcessor* fp) { fFPStack.push_back(fp); } |
| 170 explicit Iter(GrGLSLFragmentProcessor* fps[], int cnt) { |
| 171 for (int i = cnt - 1; i >= 0; --i) { |
| 172 fFPStack.push_back(fps[i]); |
| 173 } |
| 174 } |
| 175 GrGLSLFragmentProcessor* next(); |
| 176 |
| 177 private: |
| 178 SkSTArray<4, GrGLSLFragmentProcessor*, true> fFPStack; |
| 179 }; |
| 180 |
144 protected: | 181 protected: |
145 /** A GrGLSLFragmentProcessor instance can be reused with any GrFragmentProc
essor that produces | 182 /** A GrGLSLFragmentProcessor instance can be reused with any GrFragmentProc
essor that produces |
146 the same stage key; this function reads data from a GrFragmentProcessor and
uploads any | 183 the same stage key; this function reads data from a GrFragmentProcessor and
uploads any |
147 uniform variables required by the shaders created in emitCode(). The GrFragm
entProcessor | 184 uniform variables required by the shaders created in emitCode(). The GrFragm
entProcessor |
148 parameter is guaranteed to be of the same type that created this GrGLSLFragm
entProcessor and | 185 parameter is guaranteed to be of the same type that created this GrGLSLFragm
entProcessor and |
149 to have an identical processor key as the one that created this GrGLSLFragme
ntProcessor. */ | 186 to have an identical processor key as the one that created this GrGLSLFragme
ntProcessor. */ |
150 // TODO update this to pass in GrFragmentProcessor | 187 // TODO update this to pass in GrFragmentProcessor |
151 virtual void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&)
{} | 188 virtual void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&)
{} |
152 | 189 |
153 private: | 190 private: |
154 void internalEmitChild(int, const char*, const char*, EmitArgs&); | 191 void internalEmitChild(int, const char*, const char*, EmitArgs&); |
155 | 192 |
156 SkTArray<GrGLSLFragmentProcessor*, true> fChildProcessors; | 193 SkTArray<GrGLSLFragmentProcessor*, true> fChildProcessors; |
157 | 194 |
158 friend class GrFragmentProcessor; | 195 friend class GrFragmentProcessor; |
159 }; | 196 }; |
160 | 197 |
161 #endif | 198 #endif |
OLD | NEW |