blob: 769c7a133006fa5df49f21383cc9546caab29e96 [file] [log] [blame]
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +00001/*
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 GrGLProgramEffects_DEFINED
9#define GrGLProgramEffects_DEFINED
10
11#include "GrBackendEffectFactory.h"
12#include "GrTexture.h"
13#include "GrTextureAccess.h"
14#include "GrGLUniformManager.h"
15
16class GrEffectStage;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000017class GrGLVertexProgramEffectsBuilder;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000018class GrGLShaderBuilder;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000019class GrGLFullShaderBuilder;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000020class GrGLFragmentOnlyShaderBuilder;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000021
22/**
23 * This class encapsulates an array of GrGLEffects and their supporting data (coord transforms
24 * and textures). It is built with GrGLProgramEffectsBuilder, then used to manage the necessary GL
25 * state and shader uniforms.
26 */
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +000027class GrGLProgramEffects : public SkRefCnt {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000028public:
29 typedef GrBackendEffectFactory::EffectKey EffectKey;
30 typedef GrGLUniformManager::UniformHandle UniformHandle;
31
32 /**
33 * These methods generate different portions of an effect's final key.
34 */
35 static EffectKey GenAttribKey(const GrDrawEffect&);
36 static EffectKey GenTransformKey(const GrDrawEffect&);
37 static EffectKey GenTextureKey(const GrDrawEffect&, const GrGLCaps&);
38
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000039 virtual ~GrGLProgramEffects();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000040
41 /**
42 * Assigns a texture unit to each sampler. It starts on *texUnitIdx and writes the next
43 * available unit to *texUnitIdx when it returns.
44 */
45 void initSamplers(const GrGLUniformManager&, int* texUnitIdx);
46
47 /**
48 * Calls setData() on each effect, and sets their transformation matrices and texture bindings.
49 */
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000050 virtual void setData(GrGpuGL*,
51 const GrGLUniformManager&,
52 const GrEffectStage* effectStages[]) = 0;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000053
54 /**
55 * Passed to GrGLEffects so they can add transformed coordinates to their shader code.
56 */
57 class TransformedCoords {
58 public:
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000059 TransformedCoords(const SkString& name, GrSLType type)
commit-bot@chromium.org5fd7d5c2013-10-04 01:20:09 +000060 : fName(name), fType(type) {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000061 }
62
63 const char* c_str() const { return fName.c_str(); }
64 GrSLType type() const { return fType; }
65 const SkString& getName() const { return fName; }
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000066
67 private:
68 SkString fName;
69 GrSLType fType;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000070 };
71
72 typedef SkTArray<TransformedCoords> TransformedCoordsArray;
73
74 /**
75 * Passed to GrGLEffects so they can add texture reads to their shader code.
76 */
77 class TextureSampler {
78 public:
79 TextureSampler(UniformHandle uniform, const GrTextureAccess& access)
80 : fSamplerUniform(uniform)
81 , fConfigComponentMask(GrPixelConfigComponentMask(access.getTexture()->config())) {
82 SkASSERT(0 != fConfigComponentMask);
83 memcpy(fSwizzle, access.getSwizzle(), 5);
84 }
85
86 UniformHandle samplerUniform() const { return fSamplerUniform; }
87 // bitfield of GrColorComponentFlags present in the texture's config.
88 uint32_t configComponentMask() const { return fConfigComponentMask; }
89 const char* swizzle() const { return fSwizzle; }
90
91 private:
92 UniformHandle fSamplerUniform;
93 uint32_t fConfigComponentMask;
94 char fSwizzle[5];
95 };
96
97 typedef SkTArray<TextureSampler> TextureSamplerArray;
98
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000099protected:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000100 GrGLProgramEffects(int reserveCount)
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000101 : fGLEffects(reserveCount)
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000102 , fSamplers(reserveCount) {
103 }
104
105 /**
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000106 * Helper for emitEffect() in a subclasses. Emits uniforms for an effect's texture accesses and
107 * appends the necessary data to the TextureSamplerArray* object so effects can add texture
108 * lookups to their code. This method is only meant to be called during the construction phase.
109 */
110 void emitSamplers(GrGLShaderBuilder*, const GrEffectRef&, TextureSamplerArray*);
111
112 /**
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000113 * Helper for setData(). Binds all the textures for an effect.
114 */
115 void bindTextures(GrGpuGL*, const GrEffectRef&, int effectIdx);
116
117 struct Sampler {
118 SkDEBUGCODE(Sampler() : fTextureUnit(-1) {})
119 UniformHandle fUniform;
120 int fTextureUnit;
121 };
122
123 SkTArray<GrGLEffect*> fGLEffects;
124 SkTArray<SkSTArray<4, Sampler, true> > fSamplers;
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +0000125
126private:
127 typedef SkRefCnt INHERITED;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000128};
129
130/**
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000131 * This is an abstract base class for constructing different types of GrGLProgramEffects objects.
132 */
133class GrGLProgramEffectsBuilder {
134public:
mtklein@google.comf1077f92013-11-20 15:13:49 +0000135 virtual ~GrGLProgramEffectsBuilder() { }
136
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000137 /**
138 * Emits the effect's shader code, and stores the necessary uniforms internally.
139 */
140 virtual void emitEffect(const GrEffectStage&,
141 GrGLProgramEffects::EffectKey,
142 const char* outColor,
143 const char* inColor,
144 int stageIndex) = 0;
145};
146
147////////////////////////////////////////////////////////////////////////////////
148
149/**
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000150 * This is a GrGLProgramEffects implementation that does coord transforms with the vertex shader.
151 */
152class GrGLVertexProgramEffects : public GrGLProgramEffects {
153public:
154 virtual void setData(GrGpuGL*,
155 const GrGLUniformManager&,
156 const GrEffectStage* effectStages[]) SK_OVERRIDE;
157
158private:
159 friend class GrGLVertexProgramEffectsBuilder;
160
161 GrGLVertexProgramEffects(int reserveCount, bool explicitLocalCoords)
162 : INHERITED(reserveCount)
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000163 , fTransforms(reserveCount)
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000164 , fHasExplicitLocalCoords(explicitLocalCoords) {
165 }
166
167 /**
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000168 * Helper for GrGLProgramEffectsBuilder::emitEfffect(). This method is meant to only be called
169 * during the construction phase.
170 */
171 void emitEffect(GrGLFullShaderBuilder*,
172 const GrEffectStage&,
173 GrGLProgramEffects::EffectKey,
174 const char* outColor,
175 const char* inColor,
176 int stageIndex);
177
178 /**
179 * Helper for emitEffect(). Emits any attributes an effect may have.
180 */
181 void emitAttributes(GrGLFullShaderBuilder*, const GrEffectStage&);
182
183 /**
184 * Helper for emitEffect(). Emits code to implement an effect's coord transforms in the VS.
185 * Varyings are added as an outputs of the VS and inputs to the FS. The varyings may be either a
186 * vec2f or vec3f depending upon whether perspective interpolation is required or not. The names
187 * of the varyings in the VS and FS as well their types are appended to the
188 * TransformedCoordsArray* object, which is in turn passed to the effect's emitCode() function.
189 */
190 void emitTransforms(GrGLFullShaderBuilder*,
191 const GrEffectRef&,
192 EffectKey,
193 TransformedCoordsArray*);
194
195 /**
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000196 * Helper for setData(). Sets all the transform matrices for an effect.
197 */
198 void setTransformData(const GrGLUniformManager&, const GrDrawEffect&, int effectIdx);
199
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000200 struct Transform {
201 Transform() { fCurrentValue = SkMatrix::InvalidMatrix(); }
202 UniformHandle fHandle;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000203 SkMatrix fCurrentValue;
204 };
205
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000206 SkTArray<SkSTArray<2, Transform, true> > fTransforms;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000207 bool fHasExplicitLocalCoords;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000208
209 typedef GrGLProgramEffects INHERITED;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000210};
211
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000212/**
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000213 * This class is used to construct a GrGLVertexProgramEffects* object.
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000214 */
215class GrGLVertexProgramEffectsBuilder : public GrGLProgramEffectsBuilder {
216public:
217 GrGLVertexProgramEffectsBuilder(GrGLFullShaderBuilder*, int reserveCount);
mtklein@google.comf1077f92013-11-20 15:13:49 +0000218 virtual ~GrGLVertexProgramEffectsBuilder() { }
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000219
220 virtual void emitEffect(const GrEffectStage&,
221 GrGLProgramEffects::EffectKey,
222 const char* outColor,
223 const char* inColor,
224 int stageIndex) SK_OVERRIDE;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000225
226 /**
227 * Finalizes the building process and returns the effect array. After this call, the builder
228 * becomes invalid.
229 */
230 GrGLProgramEffects* finish() { return fProgramEffects.detach(); }
231
232private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000233 GrGLFullShaderBuilder* fBuilder;
234 SkAutoTDelete<GrGLVertexProgramEffects> fProgramEffects;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000235
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000236 typedef GrGLProgramEffectsBuilder INHERITED;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000237};
238
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000239////////////////////////////////////////////////////////////////////////////////
240
241/**
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000242 * This is a GrGLProgramEffects implementation that does coord transforms with
243 * the the NV_path_rendering PathTexGen functionality.
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000244 */
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000245class GrGLPathTexGenProgramEffects : public GrGLProgramEffects {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000246public:
247 virtual void setData(GrGpuGL*,
248 const GrGLUniformManager&,
249 const GrEffectStage* effectStages[]) SK_OVERRIDE;
250
251private:
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000252 friend class GrGLPathTexGenProgramEffectsBuilder;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000253
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000254 GrGLPathTexGenProgramEffects(int reserveCount)
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000255 : INHERITED(reserveCount)
256 , fTransforms(reserveCount) {
257 }
258
259 /**
260 * Helper for GrGLProgramEffectsBuilder::emitEfffect(). This method is meant to only be called
261 * during the construction phase.
262 */
263 void emitEffect(GrGLFragmentOnlyShaderBuilder*,
264 const GrEffectStage&,
265 GrGLProgramEffects::EffectKey,
266 const char* outColor,
267 const char* inColor,
268 int stageIndex);
269
270 /**
271 * Helper for emitEffect(). Allocates texture units from the builder for each transform in an
272 * effect. The transforms all use adjacent texture units. They either use two or three of the
273 * coordinates at a given texture unit, depending on if they need perspective interpolation.
274 * The expressions to access the transformed coords (i.e. 'vec2(gl_TexCoord[0])') as well as the
275 * types are appended to the TransformedCoordsArray* object, which is in turn passed to the
276 * effect's emitCode() function.
277 */
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000278 void setupPathTexGen(GrGLFragmentOnlyShaderBuilder*,
279 const GrEffectRef&,
280 EffectKey,
281 TransformedCoordsArray*);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000282
283 /**
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000284 * Helper for setData(). Sets the PathTexGen state for each transform in an effect.
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000285 */
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000286 void setPathTexGenState(GrGpuGL*, const GrDrawEffect&, int effectIdx);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000287
288 struct Transforms {
289 Transforms(EffectKey transformKey, int texCoordIndex)
290 : fTransformKey(transformKey), fTexCoordIndex(texCoordIndex) {}
291 EffectKey fTransformKey;
292 int fTexCoordIndex;
293 };
294
295 SkTArray<Transforms> fTransforms;
296
297 typedef GrGLProgramEffects INHERITED;
298};
299
300/**
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000301 * This class is used to construct a GrGLPathTexGenProgramEffects* object.
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000302 */
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000303class GrGLPathTexGenProgramEffectsBuilder : public GrGLProgramEffectsBuilder {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000304public:
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000305 GrGLPathTexGenProgramEffectsBuilder(GrGLFragmentOnlyShaderBuilder*, int reserveCount);
306 virtual ~GrGLPathTexGenProgramEffectsBuilder() { }
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000307
308 virtual void emitEffect(const GrEffectStage&,
309 GrGLProgramEffects::EffectKey,
310 const char* outColor,
311 const char* inColor,
312 int stageIndex) SK_OVERRIDE;
313
314 /**
315 * Finalizes the building process and returns the effect array. After this call, the builder
316 * becomes invalid.
317 */
318 GrGLProgramEffects* finish() { return fProgramEffects.detach(); }
319
320private:
321 GrGLFragmentOnlyShaderBuilder* fBuilder;
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000322 SkAutoTDelete<GrGLPathTexGenProgramEffects> fProgramEffects;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000323
324 typedef GrGLProgramEffectsBuilder INHERITED;
325};
326
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000327#endif