OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2013 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #ifndef GrGLEffectArray_DEFINED | |
9 #define GrGLEffectArray_DEFINED | |
10 | |
11 #include "GrBackendEffectFactory.h" | |
12 #include "GrTexture.h" | |
13 #include "GrTextureAccess.h" | |
14 #include "GrGLUniformManager.h" | |
15 | |
16 class GrEffectStage; | |
17 class GrGLEffectArrayBuilder; | |
18 class GrGLShaderBuilder; | |
19 | |
20 /** | |
21 * This class encapsulates an array of GrGLEffects and their supporting data (co ord transforms | |
22 * and textures). It is built with GrGLEffectArrayBuilder, then used to manage t he necessary GL | |
23 * state and shader uniforms. | |
24 */ | |
25 class GrGLEffectArray { | |
bsalomon
2013/10/02 20:17:06
I feel like the name should indicate that this is
Chris Dalton
2013/10/02 21:25:43
Done.
| |
26 public: | |
27 typedef GrBackendEffectFactory::EffectKey EffectKey; | |
28 typedef GrGLUniformManager::UniformHandle UniformHandle; | |
29 | |
30 /** | |
31 * These methods generate different portions of an effect's final key. | |
32 */ | |
33 static EffectKey GenAttribKey(const GrDrawEffect&); | |
34 static EffectKey GenTransformKey(const GrDrawEffect&); | |
35 static EffectKey GenTextureKey(const GrDrawEffect&, const GrGLCaps&); | |
36 | |
37 ~GrGLEffectArray() { fGLEffects.deleteAll(); } | |
38 | |
39 /** | |
40 * Assigns a texture unit to each sampler. It starts on *texUnitIdx and writ es the next | |
41 * available unit to *texUnitIdx when it returns. | |
42 */ | |
43 void initSamplers(const GrGLUniformManager&, int* texUnitIdx); | |
44 | |
45 /** | |
46 * Calls setData() on each effect, and sets their transformation matrices an d texture bindings. | |
47 */ | |
48 void setData(GrGpuGL*, | |
49 const GrGLUniformManager&, | |
50 const GrEffectStage* effectStages[]); | |
51 | |
52 /** | |
53 * Passed to GrGLEffects so they can add transformed coordinates to their sh ader code. | |
54 */ | |
55 class TransformedCoords { | |
56 public: | |
57 TransformedCoords(const char* name, GrSLType type, const char* vsName) | |
58 : fName(name), fType(type), fVSName(vsName) { | |
59 } | |
60 | |
61 const char* c_str() const { return fName.c_str(); } | |
62 GrSLType type() const { return fType; } | |
63 const SkString& getName() const { return fName; } | |
64 // TODO: Remove the VS name when we have vertexless shaders, and gradien ts are reworked. | |
65 const SkString& getVSName() const { return fVSName; } | |
66 | |
67 private: | |
68 SkString fName; | |
69 GrSLType fType; | |
70 SkString fVSName; | |
71 }; | |
72 | |
73 typedef SkTArray<TransformedCoords> TransformedCoordsArray; | |
74 | |
75 /** | |
76 * Passed to GrGLEffects so they can add texture reads to their shader code. | |
77 */ | |
78 class TextureSampler { | |
79 public: | |
80 TextureSampler(UniformHandle uniform, const GrTextureAccess& access) | |
81 : fSamplerUniform(uniform) | |
82 , fConfigComponentMask(GrPixelConfigComponentMask(access.getTexture( )->config())) { | |
83 SkASSERT(0 != fConfigComponentMask); | |
84 memcpy(fSwizzle, access.getSwizzle(), 5); | |
85 } | |
86 | |
87 UniformHandle samplerUniform() const { return fSamplerUniform; } | |
88 // bitfield of GrColorComponentFlags present in the texture's config. | |
89 uint32_t configComponentMask() const { return fConfigComponentMask; } | |
90 const char* swizzle() const { return fSwizzle; } | |
91 | |
92 private: | |
93 UniformHandle fSamplerUniform; | |
94 uint32_t fConfigComponentMask; | |
95 char fSwizzle[5]; | |
96 }; | |
97 | |
98 typedef SkTArray<TextureSampler> TextureSamplerArray; | |
99 | |
100 private: | |
101 friend class GrGLEffectArrayBuilder; | |
102 | |
103 GrGLEffectArray(int reserveCount, bool explicitLocalCoords) | |
104 : fTransforms(reserveCount) | |
105 , fSamplers(reserveCount) | |
106 , fHasExplicitLocalCoords(explicitLocalCoords) { | |
107 } | |
108 | |
109 /** | |
110 * Helper for setData(). Sets all the transform matrices for an effect. | |
111 */ | |
112 void setTransformData(const GrGLUniformManager&, const GrDrawEffect&, int ef fectIdx); | |
113 | |
114 /** | |
115 * Helper for setData(). Binds all the textures for an effect. | |
116 */ | |
117 void bindTextures(GrGpuGL*, const GrEffectRef&, int effectIdx); | |
118 | |
119 struct Transform { | |
120 Transform() { fCurrentValue = SkMatrix::InvalidMatrix(); } | |
121 UniformHandle fHandle; | |
122 GrSLType fType; | |
123 SkMatrix fCurrentValue; | |
124 }; | |
125 | |
126 struct Sampler { | |
127 SkDEBUGCODE(Sampler() : fTextureUnit(-1) {}) | |
128 UniformHandle fUniform; | |
129 int fTextureUnit; | |
130 }; | |
131 | |
132 SkTDArray<GrGLEffect*> fGLEffects; | |
133 SkTArray<SkSTArray<2, Transform, true> > fTransforms; | |
134 SkTArray<SkSTArray<4, Sampler, true> > fSamplers; | |
135 bool fHasExplicitLocalCoords; | |
136 }; | |
137 | |
138 //////////////////////////////////////////////////////////////////////////////// | |
139 | |
140 /** | |
141 * This class is used to construct a GrGLEffectArray. | |
142 */ | |
143 class GrGLEffectArrayBuilder { | |
144 public: | |
145 GrGLEffectArrayBuilder(GrGLShaderBuilder* builder, int reserveCount); | |
146 | |
147 /** | |
148 * Emits the effect's shader code, and stores the necessary uniforms interna lly. | |
149 */ | |
150 void emitEffect(const GrEffectStage&, | |
151 GrGLEffectArray::EffectKey, | |
152 const char* outColor, | |
153 const char* inColor, | |
154 int stageIndex); | |
155 | |
156 /** | |
157 * Finalizes the building process and returns the effect array. After this c all, the builder | |
158 * becomes invalid. | |
159 */ | |
160 GrGLEffectArray* finish() { return fEffectArray.detach(); } | |
161 | |
162 private: | |
163 /** | |
164 * Helper for emitEffect(). Emits any attributes an effect might have. | |
165 */ | |
166 void emitAttributes(const GrEffectStage&); | |
167 | |
168 /** | |
169 * Helper for emitEffect(). Emits code to implement an effect's coord transf orms in the VS. | |
170 * Varyings are added as an outputs of the VS and inputs to the FS. The vary ings may be either a | |
171 * vec2f or vec3f depending upon whether perspective interpolation is requir ed or not. The names | |
172 * of the varyings in the VS and FS as well their types are appended to the | |
173 * TransformedCoordsArray* object, which is in turn passed to the effect's e mitCode() function. | |
174 */ | |
175 void emitTransforms(const GrEffectRef&, | |
176 GrGLEffectArray::EffectKey, | |
177 GrGLEffectArray::TransformedCoordsArray*); | |
178 | |
179 /** | |
180 * Helper for emitEffect(). Emits uniforms for an effect's texture accesses. The uniform info | |
181 * as well as texture access parameters are appended to the TextureSamplerAr ray* object, which | |
182 * is in turn passed to the effect's emitCode() function. | |
183 */ | |
184 void emitSamplers(const GrEffectRef&, | |
185 GrGLEffectArray::TextureSamplerArray*); | |
186 | |
187 GrGLShaderBuilder* fBuilder; | |
188 SkAutoTDelete<GrGLEffectArray> fEffectArray; | |
189 }; | |
190 | |
191 #endif | |
OLD | NEW |