OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2012 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 GrGLEffect_DEFINED | |
9 #define GrGLEffect_DEFINED | |
10 | |
11 #include "GrBackendEffectFactory.h" | |
12 #include "GrGLProgramEffects.h" | |
13 #include "GrGLShaderVar.h" | |
14 #include "GrGLSL.h" | |
15 | |
16 class GrGLShaderBuilder; | |
17 | |
18 /** @file | |
19 This file contains specializations for OpenGL of the shader stages declared
in | |
20 include/gpu/GrEffect.h. Objects of type GrGLEffect are responsible for emitt
ing the | |
21 GLSL code that implements a GrEffect and for uploading uniforms at draw time
. If they don't | |
22 always emit the same GLSL code, they must have a function: | |
23 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyB
uilder*) | |
24 that is used to implement a program cache. When two GrEffects produce the sa
me key this means | |
25 that their GrGLEffects would emit the same GLSL code. | |
26 | |
27 The GrGLEffect subclass must also have a constructor of the form: | |
28 EffectSubclass::EffectSubclass(const GrBackendEffectFactory&, const GrEf
fect&) | |
29 | |
30 These objects are created by the factory object returned by the GrEffect::ge
tFactory(). | |
31 */ | |
32 | |
33 class GrGLTexture; | |
34 class GrGLGeometryProcessor; | |
35 | |
36 class GrGLEffect { | |
37 public: | |
38 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | |
39 | |
40 /** | |
41 * Passed to GrGLEffects so they can add transformed coordinates to their sh
ader code. | |
42 */ | |
43 typedef GrShaderVar TransformedCoords; | |
44 typedef SkTArray<GrShaderVar> TransformedCoordsArray; | |
45 | |
46 /** | |
47 * Passed to GrGLEffects so they can add texture reads to their shader code. | |
48 */ | |
49 class TextureSampler { | |
50 public: | |
51 TextureSampler(UniformHandle uniform, const GrTextureAccess& access) | |
52 : fSamplerUniform(uniform) | |
53 , fConfigComponentMask(GrPixelConfigComponentMask(access.getTexture(
)->config())) { | |
54 SkASSERT(0 != fConfigComponentMask); | |
55 memcpy(fSwizzle, access.getSwizzle(), 5); | |
56 } | |
57 | |
58 // bitfield of GrColorComponentFlags present in the texture's config. | |
59 uint32_t configComponentMask() const { return fConfigComponentMask; } | |
60 // this is .abcd | |
61 const char* swizzle() const { return fSwizzle; } | |
62 | |
63 private: | |
64 UniformHandle fSamplerUniform; | |
65 uint32_t fConfigComponentMask; | |
66 char fSwizzle[5]; | |
67 | |
68 friend class GrGLShaderBuilder; | |
69 }; | |
70 | |
71 typedef SkTArray<TextureSampler> TextureSamplerArray; | |
72 | |
73 GrGLEffect(const GrBackendEffectFactory& factory) | |
74 : fFactory(factory) | |
75 , fIsVertexEffect(false) { | |
76 } | |
77 | |
78 virtual ~GrGLEffect() {} | |
79 | |
80 /** Called when the program stage should insert its code into the shaders. T
he code in each | |
81 shader will be in its own block ({}) and so locally scoped names will no
t collide across | |
82 stages. | |
83 | |
84 @param builder Interface used to emit code in the shaders. | |
85 @param effect The effect that generated this program stage. | |
86 @param key The key that was computed by GenKey() from the gener
ating GrEffect. | |
87 @param outputColor A predefined vec4 in the FS in which the stage shoul
d place its output | |
88 color (or coverage). | |
89 @param inputColor A vec4 that holds the input color to the stage in th
e FS. This may be | |
90 NULL in which case the implied input is solid white
(all ones). | |
91 TODO: Better system for communicating optimization i
nfo (e.g. input | |
92 color is solid white, trans black, known to be opaqu
e, etc.) that allows | |
93 the effect to communicate back similar known info ab
out its output. | |
94 @param samplers One entry for each GrTextureAccess of the GrEffect t
hat generated the | |
95 GrGLEffect. These can be passed to the builder to em
it texture | |
96 reads in the generated code. | |
97 */ | |
98 virtual void emitCode(GrGLProgramBuilder* builder, | |
99 const GrEffect& effect, | |
100 const GrEffectKey& key, | |
101 const char* outputColor, | |
102 const char* inputColor, | |
103 const TransformedCoordsArray& coords, | |
104 const TextureSamplerArray& samplers) = 0; | |
105 | |
106 /** A GrGLEffect instance can be reused with any GrEffect that produces the
same stage | |
107 key; this function reads data from a GrEffect and uploads any uniform va
riables required | |
108 by the shaders created in emitCode(). The GrEffect is | |
109 guaranteed to be of the same type that created this GrGLEffect and to ha
ve an identical | |
110 effect key as the one that created this GrGLEffect. Effects that use loc
al coords have | |
111 to consider whether the GrEffectStage's coord change matrix should be us
ed. When explicit | |
112 local coordinates are used it can be ignored. */ | |
113 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) {} | |
114 | |
115 const char* name() const { return fFactory.name(); } | |
116 | |
117 static void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuilder*) {} | |
118 | |
119 /** Used by the system when generating shader code, to see if this effect ca
n be downcasted to | |
120 the internal GrGLGeometryProcessor type */ | |
121 bool isVertexEffect() const { return fIsVertexEffect; } | |
122 | |
123 protected: | |
124 const GrBackendEffectFactory& fFactory; | |
125 | |
126 private: | |
127 friend class GrGLGeometryProcessor; // to set fIsVertexEffect | |
128 | |
129 bool fIsVertexEffect; | |
130 }; | |
131 | |
132 #endif | |
OLD | NEW |