Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: src/gpu/gl/builders/GrGLProgramBuilder.h

Issue 637003003: Opt state takes a GP instead of a GeometryStage (Closed) Base URL: https://skia.googlesource.com/skia.git@builder_cleanup
Patch Set: clean clean clean Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 GrGLProgramBuilder_DEFINED 8 #ifndef GrGLProgramBuilder_DEFINED
9 #define GrGLProgramBuilder_DEFINED 9 #define GrGLProgramBuilder_DEFINED
10 10
11 #include "GrGLFragmentShaderBuilder.h" 11 #include "GrGLFragmentShaderBuilder.h"
12 #include "GrGLGeometryShaderBuilder.h" 12 #include "GrGLGeometryShaderBuilder.h"
13 #include "GrGLVertexShaderBuilder.h" 13 #include "GrGLVertexShaderBuilder.h"
14 #include "../GrGLProgramDataManager.h" 14 #include "../GrGLProgramDataManager.h"
15 #include "../GrGLUniformHandle.h" 15 #include "../GrGLUniformHandle.h"
16 16
17 class GrGLInstalledProcessors;
18
19 /* 17 /*
20 * This is the base class for a series of interfaces. This base class *MUST* re main abstract with 18 * This is the base class for a series of interfaces. This base class *MUST* re main abstract with
21 * NO data members because it is used in multiple interface inheritance. 19 * NO data members because it is used in multiple interface inheritance.
22 * Heirarchy: 20 * Heirarchy:
23 * GrGLUniformBuilder 21 * GrGLUniformBuilder
24 * / \ 22 * / \
25 * GrGLFPBuilder GrGLGPBuilder 23 * GrGLFPBuilder GrGLGPBuilder
26 * \ / 24 * \ /
27 * GrGLProgramBuilder(internal use only) 25 * GrGLProgramBuilder(internal use only)
28 */ 26 */
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 /* a specializations for FPs. Lets the user add uniforms and FS code */ 89 /* a specializations for FPs. Lets the user add uniforms and FS code */
92 class GrGLFPBuilder : public virtual GrGLUniformBuilder { 90 class GrGLFPBuilder : public virtual GrGLUniformBuilder {
93 public: 91 public:
94 virtual GrGLFPFragmentBuilder* getFragmentShaderBuilder() = 0; 92 virtual GrGLFPFragmentBuilder* getFragmentShaderBuilder() = 0;
95 93
96 /* 94 /*
97 * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE 95 * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
98 */ 96 */
99 }; 97 };
100 98
99 class GrGLInstalledProc;
100 class GrGLInstalledGeoProc;
101 class GrGLInstalledFragProc;
102 class GrGLInstalledFragProcs;
103
101 /* 104 /*
102 * Please note - no diamond problems because of virtual inheritance. Also, both base classes 105 * Please note - no diamond problems because of virtual inheritance. Also, both base classes
103 * are pure virtual with no data members. This is the base class for program bu ilding. 106 * are pure virtual with no data members. This is the base class for program bu ilding.
104 * Subclasses are nearly identical but each has their own way of emitting transf orms. State for 107 * Subclasses are nearly identical but each has their own way of emitting transf orms. State for
105 * each of the elements of the shader pipeline, ie vertex, fragment, geometry, e tc, lives in those 108 * each of the elements of the shader pipeline, ie vertex, fragment, geometry, e tc, lives in those
106 * respective builders 109 * respective builders
107 */ 110 */
108 class GrGLProgramBuilder : public GrGLGPBuilder, 111 class GrGLProgramBuilder : public GrGLGPBuilder,
109 public GrGLFPBuilder { 112 public GrGLFPBuilder {
110 public: 113 public:
111 /** Generates a shader program. 114 /** Generates a shader program.
112 * 115 *
113 * The program implements what is specified in the stages given as input. 116 * The program implements what is specified in the stages given as input.
114 * After successful generation, the builder result objects are available 117 * After successful generation, the builder result objects are available
115 * to be used. 118 * to be used.
116 * @return true if generation was successful. 119 * @return true if generation was successful.
117 */ 120 */
118 static GrGLProgram* CreateProgram(const GrOptDrawState&, 121 static GrGLProgram* CreateProgram(const GrOptDrawState&,
119 const GrGLProgramDesc&, 122 const GrGLProgramDesc&,
120 GrGpu::DrawType, 123 GrGpu::DrawType,
121 const GrGeometryStage* inGeometryProcessor ,
122 const GrFragmentStage* inColorStages[],
123 const GrFragmentStage* inCoverageStages[],
124 GrGpuGL* gpu); 124 GrGpuGL* gpu);
125 125
126 virtual UniformHandle addUniform(uint32_t visibility, 126 virtual UniformHandle addUniform(uint32_t visibility,
127 GrSLType type, 127 GrSLType type,
128 const char* name, 128 const char* name,
129 const char** outName = NULL) SK_OVERRIDE { 129 const char** outName = NULL) SK_OVERRIDE {
130 return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNon Array, outName); 130 return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNon Array, outName);
131 } 131 }
132 virtual UniformHandle addUniformArray(uint32_t visibility, 132 virtual UniformHandle addUniformArray(uint32_t visibility,
133 GrSLType type, 133 GrSLType type,
134 const char* name, 134 const char* name,
135 int arrayCount, 135 int arrayCount,
136 const char** outName = NULL) SK_OVERRI DE; 136 const char** outName = NULL) SK_OVERRI DE;
137 137
138 virtual const GrGLShaderVar& getUniformVariable(UniformHandle u) const SK_OV ERRIDE { 138 virtual const GrGLShaderVar& getUniformVariable(UniformHandle u) const SK_OV ERRIDE {
139 return fUniforms[u.toShaderBuilderIndex()].fVariable; 139 return fUniforms[u.toShaderBuilderIndex()].fVariable;
140 } 140 }
141 141
142 virtual const char* getUniformCStr(UniformHandle u) const SK_OVERRIDE { 142 virtual const char* getUniformCStr(UniformHandle u) const SK_OVERRIDE {
143 return this->getUniformVariable(u).c_str(); 143 return this->getUniformVariable(u).c_str();
144 } 144 }
145 145
146 virtual const GrGLContextInfo& ctxInfo() const SK_OVERRIDE; 146 virtual const GrGLContextInfo& ctxInfo() const SK_OVERRIDE;
147 147
148 virtual GrGpuGL* gpu() const SK_OVERRIDE { return fGpu; } 148 virtual GrGpuGL* gpu() const SK_OVERRIDE { return fGpu; }
149 149
150 virtual GrGLFPFragmentBuilder* getFragmentShaderBuilder() SK_OVERRIDE { retu rn &fFS; } 150 virtual GrGLFPFragmentBuilder* getFragmentShaderBuilder() SK_OVERRIDE { retu rn &fFS; }
151 virtual GrGLVertexBuilder* getVertexShaderBuilder() SK_OVERRIDE { return &fV S; } 151 virtual GrGLVertexBuilder* getVertexShaderBuilder() SK_OVERRIDE { return &fV S; }
152 152
153 virtual void addVarying(GrSLType type, 153 virtual void addVarying(
154 const char* name, 154 GrSLType type,
155 const char** vsOutName = NULL, 155 const char* name,
156 const char** fsInName = NULL, 156 const char** vsOutName = NULL,
157 GrGLShaderVar::Precision fsPrecision=GrGLShaderVar:: kDefault_Precision); 157 const char** fsInName = NULL,
158 GrGLShaderVar::Precision fsPrecision=GrGLShaderVar::kDefault_Precisi on) SK_OVERRIDE;
158 159
159 // Handles for program uniforms (other than per-effect uniforms) 160 // Handles for program uniforms (other than per-effect uniforms)
160 struct BuiltinUniformHandles { 161 struct BuiltinUniformHandles {
161 UniformHandle fViewMatrixUni; 162 UniformHandle fViewMatrixUni;
162 UniformHandle fRTAdjustmentUni; 163 UniformHandle fRTAdjustmentUni;
163 UniformHandle fColorUni; 164 UniformHandle fColorUni;
164 UniformHandle fCoverageUni; 165 UniformHandle fCoverageUni;
165 166
166 // We use the render target height to provide a y-down frag coord when s pecifying 167 // We use the render target height to provide a y-down frag coord when s pecifying
167 // origin_upper_left is not supported. 168 // origin_upper_left is not supported.
168 UniformHandle fRTHeightUni; 169 UniformHandle fRTHeightUni;
169 170
170 // Uniforms for computing texture coords to do the dst-copy lookup 171 // Uniforms for computing texture coords to do the dst-copy lookup
171 UniformHandle fDstCopyTopLeftUni; 172 UniformHandle fDstCopyTopLeftUni;
172 UniformHandle fDstCopyScaleUni; 173 UniformHandle fDstCopyScaleUni;
173 UniformHandle fDstCopySamplerUni; 174 UniformHandle fDstCopySamplerUni;
174 }; 175 };
175 176
176 protected: 177 protected:
178 typedef GrGLProgramDesc::EffectKeyProvider EffectKeyProvider;
179 typedef GrGLProgramDataManager::UniformInfo UniformInfo;
180 typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray;
181
177 static GrGLProgramBuilder* CreateProgramBuilder(const GrGLProgramDesc&, 182 static GrGLProgramBuilder* CreateProgramBuilder(const GrGLProgramDesc&,
178 const GrOptDrawState&, 183 const GrOptDrawState&,
179 GrGpu::DrawType, 184 GrGpu::DrawType,
180 bool hasGeometryProcessor, 185 bool hasGeometryProcessor,
181 GrGpuGL*); 186 GrGpuGL*);
182 187
183 GrGLProgramBuilder(GrGpuGL*, const GrOptDrawState&, const GrGLProgramDesc&); 188 GrGLProgramBuilder(GrGpuGL*, const GrOptDrawState&, const GrGLProgramDesc&);
184 189
185 const GrOptDrawState& optState() const { return fOptState; } 190 const GrOptDrawState& optState() const { return fOptState; }
186 const GrGLProgramDesc& desc() const { return fDesc; } 191 const GrGLProgramDesc& desc() const { return fDesc; }
187 const GrGLProgramDesc::KeyHeader& header() const { return fDesc.getHeader(); } 192 const GrGLProgramDesc::KeyHeader& header() const { return fDesc.getHeader(); }
188 193
189 // Generates a name for a variable. The generated string will be name prefix ed by the prefix 194 // Generates a name for a variable. The generated string will be name prefix ed by the prefix
190 // char (unless the prefix is '\0'). It also mangles the name to be stage-sp ecific if we're 195 // char (unless the prefix is '\0'). It also mangles the name to be stage-sp ecific if we're
191 // generating stage code. 196 // generating stage code.
192 void nameVariable(SkString* out, char prefix, const char* name); 197 void nameVariable(SkString* out, char prefix, const char* name);
193 void setupUniformColorAndCoverageIfNeeded(GrGLSLExpr4* inputColor, GrGLSLExp r4* inputCoverage); 198 void setupUniformColorAndCoverageIfNeeded(GrGLSLExpr4* inputColor, GrGLSLExp r4* inputCoverage);
194 void createAndEmitProcessors(const GrGeometryStage* geometryProcessor, 199 void createAndEmitProcessors(const GrOptDrawState& optState,
195 const GrFragmentStage* colorStages[],
196 const GrFragmentStage* coverageStages[],
197 GrGLSLExpr4* inputColor, 200 GrGLSLExpr4* inputColor,
198 GrGLSLExpr4* inputCoverage); 201 GrGLSLExpr4* inputCoverage);
199 template <class ProcessorStage> 202 void emitFragmentProcessors(int procOffset, int numProcs, GrGLSLExpr4* inOut );
200 void createAndEmitProcessors(const ProcessorStage*[], 203 template <class Proc>
201 int effectCnt, 204 void emit(const Proc&,
202 const GrGLProgramDesc::EffectKeyProvider&, 205 int index,
203 GrGLSLExpr4* fsInOutColor, 206 const EffectKeyProvider,
204 GrGLInstalledProcessors*); 207 const GrGLSLExpr4& input,
208 GrGLSLExpr4* output);
209
210 // these emit functions help to keep the createAndEmitProcessors template ge neral
211 void emit(const GrFragmentStage&,
212 const GrProcessorKey&,
213 const char* outColor,
214 const char* inColor);
215 void emit(const GrGeometryProcessor&,
216 const GrProcessorKey&,
217 const char* outColor,
218 const char* inColor);
205 void verify(const GrGeometryProcessor&); 219 void verify(const GrGeometryProcessor&);
206 void verify(const GrFragmentProcessor&); 220 void verify(const GrFragmentProcessor&);
207 void emitSamplers(const GrProcessor&, 221 void emitSamplers(const GrProcessor&,
208 GrGLProcessor::TextureSamplerArray* outSamplers, 222 GrGLProcessor::TextureSamplerArray* outSamplers,
209 GrGLInstalledProcessors*); 223 GrGLInstalledProc*);
210 224
211 // each specific program builder has a distinct transform and must override this function 225 // each specific program builder has a distinct transform and must override this function
212 virtual void emitTransforms(const GrProcessorStage&, 226 virtual void emitTransforms(const GrFragmentStage&,
213 GrGLProcessor::TransformedCoordsArray* outCoords , 227 GrGLProcessor::TransformedCoordsArray* outCoords ,
214 GrGLInstalledProcessors*); 228 GrGLInstalledFragProc*);
215 GrGLProgram* finalize(); 229 GrGLProgram* finalize();
216 void bindUniformLocations(GrGLuint programID); 230 void bindUniformLocations(GrGLuint programID);
217 bool checkLinkStatus(GrGLuint programID); 231 bool checkLinkStatus(GrGLuint programID);
218 void resolveUniformLocations(GrGLuint programID); 232 void resolveUniformLocations(GrGLuint programID);
219
220 void cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs ); 233 void cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs );
221 void cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs); 234 void cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs);
222 235
223 // Subclasses create different programs 236 // Subclasses create different programs
224 virtual GrGLProgram* createProgram(GrGLuint programID); 237 virtual GrGLProgram* createProgram(GrGLuint programID);
225 238
226 void appendUniformDecls(ShaderVisibility, SkString*) const; 239 void appendUniformDecls(ShaderVisibility, SkString*) const;
227 240
228 // reset is called by program creator between each processor's emit code. I t increments the 241 // reset is called by program creator between each processor's emit code. I t increments the
229 // stage offset for variable name mangling, and also ensures verfication var iables in the 242 // stage offset for variable name mangling, and also ensures verfication var iables in the
(...skipping 19 matching lines...) Expand all
249 public: 262 public:
250 AutoStageAdvance(GrGLProgramBuilder* pb) : fPB(pb) { fPB->reset(); } 263 AutoStageAdvance(GrGLProgramBuilder* pb) : fPB(pb) { fPB->reset(); }
251 ~AutoStageAdvance() { fPB->exitStage(); } 264 ~AutoStageAdvance() { fPB->exitStage(); }
252 private: 265 private:
253 GrGLProgramBuilder* fPB; 266 GrGLProgramBuilder* fPB;
254 }; 267 };
255 void exitStage() { fOutOfStage = true; } 268 void exitStage() { fOutOfStage = true; }
256 void enterStage() { fOutOfStage = false; } 269 void enterStage() { fOutOfStage = false; }
257 int stageIndex() const { return fStageIndex; } 270 int stageIndex() const { return fStageIndex; }
258 271
259 typedef GrGLProgramDesc::EffectKeyProvider EffectKeyProvider;
260 typedef GrGLProgramDataManager::UniformInfo UniformInfo;
261 typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray;
262
263 // number of each input/output type in a single allocation block, used by ma ny builders 272 // number of each input/output type in a single allocation block, used by ma ny builders
264 static const int kVarsPerBlock; 273 static const int kVarsPerBlock;
265 274
266 BuiltinUniformHandles fUniformHandles; 275 BuiltinUniformHandles fUniformHandles;
267 GrGLVertexBuilder fVS; 276 GrGLVertexBuilder fVS;
268 GrGLGeometryBuilder fGS; 277 GrGLGeometryBuilder fGS;
269 GrGLFragmentShaderBuilder fFS; 278 GrGLFragmentShaderBuilder fFS;
270 bool fOutOfStage; 279 bool fOutOfStage;
271 int fStageIndex; 280 int fStageIndex;
272 281
273 SkAutoTUnref<GrGLInstalledProcessors> fGeometryProcessor; 282 GrGLInstalledGeoProc* fGeometryProcessor;
274 SkAutoTUnref<GrGLInstalledProcessors> fColorEffects; 283 SkAutoTUnref<GrGLInstalledFragProcs> fFragmentProcessors;
275 SkAutoTUnref<GrGLInstalledProcessors> fCoverageEffects;
276 284
277 const GrOptDrawState& fOptState; 285 const GrOptDrawState& fOptState;
278 const GrGLProgramDesc& fDesc; 286 const GrGLProgramDesc& fDesc;
279 GrGpuGL* fGpu; 287 GrGpuGL* fGpu;
280 UniformInfoArray fUniforms; 288 UniformInfoArray fUniforms;
281 289
282 friend class GrGLShaderBuilder; 290 friend class GrGLShaderBuilder;
283 friend class GrGLVertexBuilder; 291 friend class GrGLVertexBuilder;
284 friend class GrGLFragmentShaderBuilder; 292 friend class GrGLFragmentShaderBuilder;
285 friend class GrGLGeometryBuilder; 293 friend class GrGLGeometryBuilder;
286 }; 294 };
287 295
288 /** 296 /**
289 * This class encapsulates an array of GrGLProcessors and their supporting data (coord transforms 297 * The below structs represent processors installed in programs. All processors can have texture
290 * and textures). It is built by GrGLProgramBuilder, then used to manage the nec essary GL 298 * samplers, but only frag processors have coord transforms, hence the need for different structs
291 * state and shader uniforms in GLPrograms. Its just Plain old data, and as suc h is entirely public
292 *
293 * TODO We really don't need this class to have an array of processors. It make s sense for it
294 * to just have one, also break out the transforms
295 */ 299 */
296 class GrGLInstalledProcessors : public SkRefCnt { 300 struct GrGLInstalledProc {
297 public: 301 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
298 GrGLInstalledProcessors(int reserveCount, bool hasExplicitLocalCoords = fals e)
299 : fGLProcessors(reserveCount)
300 , fSamplers(reserveCount)
301 , fTransforms(reserveCount)
302 , fHasExplicitLocalCoords(hasExplicitLocalCoords) {
303 }
304 302
305 virtual ~GrGLInstalledProcessors(); 303 struct Sampler {
304 SkDEBUGCODE(Sampler() : fTextureUnit(-1) {})
305 UniformHandle fUniform;
306 int fTextureUnit;
307 };
308 SkSTArray<4, Sampler, true> fSamplers;
309 };
306 310
307 typedef GrGLProgramDataManager::UniformHandle UniformHandle; 311 struct GrGLInstalledGeoProc : public GrGLInstalledProc {
312 GrGLGeometryProcessor* fGLProc;
313 };
308 314
309 struct Sampler { 315 struct GrGLInstalledFragProc : public GrGLInstalledProc {
310 SkDEBUGCODE(Sampler() : fTextureUnit(-1) {}) 316 GrGLInstalledFragProc(bool useLocalCoords) : fGLProc(NULL), fUseLocalCoords( useLocalCoords) {}
311 UniformHandle fUniform;
312 int fTextureUnit;
313 };
314
315 class ShaderVarHandle { 317 class ShaderVarHandle {
316 public: 318 public:
317 bool isValid() const { return fHandle > -1; } 319 bool isValid() const { return fHandle > -1; }
318 ShaderVarHandle() : fHandle(-1) {} 320 ShaderVarHandle() : fHandle(-1) {}
319 ShaderVarHandle(int value) : fHandle(value) { SkASSERT(this->isValid()); } 321 ShaderVarHandle(int value) : fHandle(value) { SkASSERT(this->isValid()); }
320 int handle() const { SkASSERT(this->isValid()); return fHandle; } 322 int handle() const { SkASSERT(this->isValid()); return fHandle; }
321 UniformHandle convertToUniformHandle() { 323 UniformHandle convertToUniformHandle() {
322 SkASSERT(this->isValid()); 324 SkASSERT(this->isValid());
323 return GrGLProgramDataManager::UniformHandle::CreateFromUniformIndex (fHandle); 325 return GrGLProgramDataManager::UniformHandle::CreateFromUniformIndex (fHandle);
324 } 326 }
325 327
326 private: 328 private:
327 int fHandle; 329 int fHandle;
328 }; 330 };
329 331
330 struct Transform { 332 struct Transform {
331 Transform() : fType(kVoid_GrSLType) { fCurrentValue = SkMatrix::InvalidM atrix(); } 333 Transform() : fType(kVoid_GrSLType) { fCurrentValue = SkMatrix::InvalidM atrix(); }
332 ShaderVarHandle fHandle; 334 ShaderVarHandle fHandle;
333 SkMatrix fCurrentValue; 335 SkMatrix fCurrentValue;
334 GrSLType fType; 336 GrSLType fType;
335 }; 337 };
336 338
337 void addEffect(GrGLProcessor* effect) { fGLProcessors.push_back(effect); } 339 GrGLFragmentProcessor* fGLProc;
338 SkTArray<Sampler, true>& addSamplers() { return fSamplers.push_back(); } 340 SkSTArray<2, Transform, true> fTransforms;
339 SkTArray<Transform, true>& addTransforms() { return fTransforms.push_back(); } 341 bool fUseLocalCoords;
342 };
340 343
341 SkTArray<GrGLProcessor*> fGLProcessors; 344 struct GrGLInstalledFragProcs : public SkRefCnt {
342 SkTArray<SkSTArray<4, Sampler, true> > fSamplers; 345 ~GrGLInstalledFragProcs();
343 SkTArray<SkSTArray<2, Transform, true> > fTransforms; 346 SkSTArray<8, GrGLInstalledFragProc*, true> fProcs;
344 bool fHasExplicitLocalCoords;
345
346 friend class GrGLShaderBuilder;
347 friend class GrGLVertexShaderBuilder;
348 friend class GrGLFragmentShaderBuilder;
349 friend class GrGLGeometryShaderBuilder;
350 }; 347 };
351 348
352 #endif 349 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698