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 GrCoordTransformed owned by
the FP. |
41 */ | 42 */ |
42 class TransformedCoordVars { | 43 template <typename T, typename FPBASE, int (FPBASE::*COUNT)() const> |
| 44 class EmitProvider { |
43 public: | 45 public: |
44 TransformedCoordVars(const GrFragmentProcessor* fp, const GrShaderVar* v
ars) | 46 EmitProvider(const GrFragmentProcessor* fp, const T* ts) : fFP(fp) , 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 EmitProvider childValues(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 EmitProvider(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 = EmitProvider<GrShaderVar, GrFragmentProcessor, |
| 73 &GrFragmentProcessor::numCoordTran
sforms>; |
| 74 using TextureSamplers = EmitProvider<SamplerHandle, GrProcessor, &GrProcesso
r::numTextures>; |
| 75 using BufferSamplers = EmitProvider<SamplerHandle, GrProcessor, &GrProcessor
::numBuffers>; |
| 76 |
60 /** Called when the program stage should insert its code into the shaders. T
he code in each | 77 /** 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 | 78 shader will be in its own block ({}) and so locally scoped names will no
t collide across |
62 stages. | 79 stages. |
63 | 80 |
64 @param fragBuilder Interface used to emit code in the shaders. | 81 @param fragBuilder Interface used to emit code in the shaders. |
65 @param fp The processor that generated this program stage
. | 82 @param fp The processor that generated this program stage
. |
66 @param key The key that was computed by GenKey() from the
generating | 83 @param key The key that was computed by GenKey() from the
generating |
67 GrProcessor. | 84 GrProcessor. |
68 @param outputColor A predefined vec4 in the FS in which the stage
should place its | 85 @param outputColor A predefined vec4 in the FS in which the stage
should place its |
69 output color (or coverage). | 86 output color (or coverage). |
(...skipping 13 matching lines...) Expand all Loading... |
83 generated code. | 100 generated code. |
84 */ | 101 */ |
85 struct EmitArgs { | 102 struct EmitArgs { |
86 EmitArgs(GrGLSLFPFragmentBuilder* fragBuilder, | 103 EmitArgs(GrGLSLFPFragmentBuilder* fragBuilder, |
87 GrGLSLUniformHandler* uniformHandler, | 104 GrGLSLUniformHandler* uniformHandler, |
88 const GrGLSLCaps* caps, | 105 const GrGLSLCaps* caps, |
89 const GrFragmentProcessor& fp, | 106 const GrFragmentProcessor& fp, |
90 const char* outputColor, | 107 const char* outputColor, |
91 const char* inputColor, | 108 const char* inputColor, |
92 const TransformedCoordVars& transformedCoordVars, | 109 const TransformedCoordVars& transformedCoordVars, |
93 const SamplerHandle* texSamplers, | 110 const TextureSamplers& textureSamplers, |
94 const SamplerHandle* bufferSamplers, | 111 const BufferSamplers& bufferSamplers, |
95 bool gpImplementsDistanceVector) | 112 bool gpImplementsDistanceVector) |
96 : fFragBuilder(fragBuilder) | 113 : fFragBuilder(fragBuilder) |
97 , fUniformHandler(uniformHandler) | 114 , fUniformHandler(uniformHandler) |
98 , fGLSLCaps(caps) | 115 , fGLSLCaps(caps) |
99 , fFp(fp) | 116 , fFp(fp) |
100 , fOutputColor(outputColor) | 117 , fOutputColor(outputColor) |
101 , fInputColor(inputColor) | 118 , fInputColor(inputColor) |
102 , fTransformedCoords(transformedCoordVars) | 119 , fTransformedCoords(transformedCoordVars) |
103 , fTexSamplers(texSamplers) | 120 , fTexSamplers(textureSamplers) |
104 , fBufferSamplers(bufferSamplers) | 121 , fBufferSamplers(bufferSamplers) |
105 , fGpImplementsDistanceVector(gpImplementsDistanceVector) {} | 122 , fGpImplementsDistanceVector(gpImplementsDistanceVector) {} |
106 GrGLSLFPFragmentBuilder* fFragBuilder; | 123 GrGLSLFPFragmentBuilder* fFragBuilder; |
107 GrGLSLUniformHandler* fUniformHandler; | 124 GrGLSLUniformHandler* fUniformHandler; |
108 const GrGLSLCaps* fGLSLCaps; | 125 const GrGLSLCaps* fGLSLCaps; |
109 const GrFragmentProcessor& fFp; | 126 const GrFragmentProcessor& fFp; |
110 const char* fOutputColor; | 127 const char* fOutputColor; |
111 const char* fInputColor; | 128 const char* fInputColor; |
112 const TransformedCoordVars& fTransformedCoords; | 129 const TransformedCoordVars& fTransformedCoords; |
113 const SamplerHandle* fTexSamplers; | 130 const TextureSamplers& fTexSamplers; |
114 const SamplerHandle* fBufferSamplers; | 131 const BufferSamplers& fBufferSamplers; |
115 bool fGpImplementsDistanceVector; | 132 bool fGpImplementsDistanceVector; |
116 }; | 133 }; |
117 | 134 |
118 virtual void emitCode(EmitArgs&) = 0; | 135 virtual void emitCode(EmitArgs&) = 0; |
119 | 136 |
120 void setData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcesso
r& processor); | 137 void setData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcesso
r& processor); |
121 | 138 |
122 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuil
der*) {} | 139 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuil
der*) {} |
123 | 140 |
124 int numChildProcessors() const { return fChildProcessors.count(); } | 141 int numChildProcessors() const { return fChildProcessors.count(); } |
125 | 142 |
126 GrGLSLFragmentProcessor* childProcessor(int index) const { | 143 GrGLSLFragmentProcessor* childProcessor(int index) { |
127 return fChildProcessors[index]; | 144 return fChildProcessors[index]; |
128 } | 145 } |
129 | 146 |
130 /** Will emit the code of a child proc in its own scope. Pass in the parent'
s EmitArgs and | 147 /** 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 | 148 * 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 | 149 * 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 | 150 * 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 | 151 * 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 | 152 * legal to pass nullptr as inputColor, since all fragment processors are r
equired to work |
136 * without an input color. | 153 * without an input color. |
137 */ | 154 */ |
138 void emitChild(int childIndex, const char* inputColor, SkString* outputColor
, | 155 void emitChild(int childIndex, const char* inputColor, SkString* outputColor
, |
139 EmitArgs& parentArgs); | 156 EmitArgs& parentArgs); |
140 | 157 |
141 /** Variation that uses the parent's output color variable to hold the child
's output.*/ | 158 /** 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)
; | 159 void emitChild(int childIndex, const char* inputColor, EmitArgs& parentArgs)
; |
143 | 160 |
| 161 /** |
| 162 * Pre-order traversal of a GLSLFP hierarchy, or of multiple trees with root
s in an array of |
| 163 * GLSLFPS. This agrees with the traversal order of GrFragmentProcessor::Ite
r |
| 164 */ |
| 165 class Iter : public SkNoncopyable { |
| 166 public: |
| 167 explicit Iter(GrGLSLFragmentProcessor* fp) { fFPStack.push_back(fp); } |
| 168 explicit Iter(GrGLSLFragmentProcessor* fps[], int cnt) { |
| 169 for (int i = cnt - 1; i >= 0; --i) { |
| 170 fFPStack.push_back(fps[i]); |
| 171 } |
| 172 } |
| 173 GrGLSLFragmentProcessor* next(); |
| 174 |
| 175 private: |
| 176 SkSTArray<4, GrGLSLFragmentProcessor*, true> fFPStack; |
| 177 }; |
| 178 |
144 protected: | 179 protected: |
145 /** A GrGLSLFragmentProcessor instance can be reused with any GrFragmentProc
essor that produces | 180 /** 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 | 181 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 | 182 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 | 183 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. */ | 184 to have an identical processor key as the one that created this GrGLSLFragme
ntProcessor. */ |
150 // TODO update this to pass in GrFragmentProcessor | 185 // TODO update this to pass in GrFragmentProcessor |
151 virtual void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&)
{} | 186 virtual void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&)
{} |
152 | 187 |
153 private: | 188 private: |
154 void internalEmitChild(int, const char*, const char*, EmitArgs&); | 189 void internalEmitChild(int, const char*, const char*, EmitArgs&); |
155 | 190 |
156 SkTArray<GrGLSLFragmentProcessor*, true> fChildProcessors; | 191 SkTArray<GrGLSLFragmentProcessor*, true> fChildProcessors; |
157 | 192 |
158 friend class GrFragmentProcessor; | 193 friend class GrFragmentProcessor; |
159 }; | 194 }; |
160 | 195 |
161 #endif | 196 #endif |
OLD | NEW |