| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 GrGLEffect_DEFINED | 8 #ifndef GrGLEffect_DEFINED |
| 9 #define GrGLEffect_DEFINED | 9 #define GrGLEffect_DEFINED |
| 10 | 10 |
| 11 #include "GrBackendEffectFactory.h" | 11 #include "GrBackendEffectFactory.h" |
| 12 #include "GrGLShaderBuilder.h" | 12 #include "GrGLShaderBuilder.h" |
| 13 #include "GrGLShaderVar.h" | 13 #include "GrGLShaderVar.h" |
| 14 #include "GrGLSL.h" | 14 #include "GrGLSL.h" |
| 15 #include "GrEffectStage.h" | |
| 16 | 15 |
| 17 class GrGLTexture; | 16 class GrGLTexture; |
| 18 | 17 |
| 19 /** @file | 18 /** @file |
| 20 This file contains specializations for OpenGL of the shader stages declared
in | 19 This file contains specializations for OpenGL of the shader stages declared
in |
| 21 include/gpu/GrEffect.h. Objects of type GrGLEffect are responsible for emitt
ing the | 20 include/gpu/GrEffect.h. Objects of type GrGLEffect are responsible for emitt
ing the |
| 22 GLSL code that implements a GrEffect and for uploading uniforms at draw time
. They also | 21 GLSL code that implements a GrEffect and for uploading uniforms at draw time
. They also |
| 23 must have a function: | 22 must have a function: |
| 24 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&) | 23 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) |
| 25 that is used to implement a program cache. When two GrEffects produce the sa
me key this means | 24 that is used to implement a program cache. When two GrEffects produce the sa
me key this means |
| 26 that their GrGLEffects would emit the same GLSL code. | 25 that their GrGLEffects would emit the same GLSL code. |
| 27 | 26 |
| 27 The GrGLEffect subclass must also have a constructor of the form: |
| 28 EffectSubclass::EffectSubclass(const GrBackendEffectFactory&, const GrDr
awEffect&) |
| 29 The effect held by the GrDrawEffect is guaranteed to be of the type that gen
erated the |
| 30 GrGLEffect subclass instance. |
| 31 |
| 28 These objects are created by the factory object returned by the GrEffect::ge
tFactory(). | 32 These objects are created by the factory object returned by the GrEffect::ge
tFactory(). |
| 29 */ | 33 */ |
| 30 | 34 |
| 35 class GrDrawEffect; |
| 36 |
| 31 class GrGLEffect { | 37 class GrGLEffect { |
| 32 | 38 |
| 33 public: | 39 public: |
| 34 typedef GrBackendEffectFactory::EffectKey EffectKey; | 40 typedef GrBackendEffectFactory::EffectKey EffectKey; |
| 35 | 41 |
| 36 enum { | 42 enum { |
| 37 kNoEffectKey = GrBackendEffectFactory::kNoEffectKey, | 43 kNoEffectKey = GrBackendEffectFactory::kNoEffectKey, |
| 38 // the number of bits in EffectKey available to GenKey | 44 // the number of bits in EffectKey available to GenKey |
| 39 kEffectKeyBits = GrBackendEffectFactory::kEffectKeyBits, | 45 kEffectKeyBits = GrBackendEffectFactory::kEffectKeyBits, |
| 40 }; | 46 }; |
| 41 | 47 |
| 42 typedef GrGLShaderBuilder::TextureSamplerArray TextureSamplerArray; | 48 typedef GrGLShaderBuilder::TextureSamplerArray TextureSamplerArray; |
| 43 | 49 |
| 44 GrGLEffect(const GrBackendEffectFactory&); | 50 GrGLEffect(const GrBackendEffectFactory&); |
| 45 | 51 |
| 46 virtual ~GrGLEffect(); | 52 virtual ~GrGLEffect(); |
| 47 | 53 |
| 48 /** Called when the program stage should insert its code into the shaders. T
he code in each | 54 /** Called when the program stage should insert its code into the shaders. T
he code in each |
| 49 shader will be in its own block ({}) and so locally scoped names will no
t collide across | 55 shader will be in its own block ({}) and so locally scoped names will no
t collide across |
| 50 stages. | 56 stages. |
| 51 | 57 |
| 52 @param builder Interface used to emit code in the shaders. | 58 @param builder Interface used to emit code in the shaders. |
| 53 @param stage The effect stage that generated this program stage. | 59 @param drawEffect A wrapper on the effect that generated this program
stage. |
| 54 @param key The key that was computed by GenKey() from the gener
ating GrEffect. | 60 @param key The key that was computed by GenKey() from the gener
ating GrEffect. |
| 55 Only the bits indicated by GrBackendEffectFactory::k
EffectKeyBits are | 61 Only the bits indicated by GrBackendEffectFactory::k
EffectKeyBits are |
| 56 guaranteed to match the value produced by GenKey(); | 62 guaranteed to match the value produced by GenKey(); |
| 57 @param vertexCoords A vec2 in the VS that holds the position in local co
ords. This is either | |
| 58 the pre-view-matrix vertex position or if explicit p
er-vertex texture | |
| 59 coords are used with a stage then it is those coordi
nates. See | |
| 60 GrVertexLayout. | |
| 61 @param outputColor A predefined vec4 in the FS in which the stage shoul
d place its output | 63 @param outputColor A predefined vec4 in the FS in which the stage shoul
d place its output |
| 62 color (or coverage). | 64 color (or coverage). |
| 63 @param inputColor A vec4 that holds the input color to the stage in th
e FS. This may be | 65 @param inputColor A vec4 that holds the input color to the stage in th
e FS. This may be |
| 64 NULL in which case the implied input is solid white
(all ones). | 66 NULL in which case the implied input is solid white
(all ones). |
| 65 TODO: Better system for communicating optimization i
nfo (e.g. input | 67 TODO: Better system for communicating optimization i
nfo (e.g. input |
| 66 color is solid white, trans black, known to be opaqu
e, etc.) that allows | 68 color is solid white, trans black, known to be opaqu
e, etc.) that allows |
| 67 the effect to communicate back similar known info ab
out its output. | 69 the effect to communicate back similar known info ab
out its output. |
| 68 @param samplers One entry for each GrTextureAccess of the GrEffect t
hat generated the | 70 @param samplers One entry for each GrTextureAccess of the GrEffect t
hat generated the |
| 69 GrGLEffect. These can be passed to the builder to em
it texture | 71 GrGLEffect. These can be passed to the builder to em
it texture |
| 70 reads in the generated code. | 72 reads in the generated code. |
| 71 */ | 73 */ |
| 72 virtual void emitCode(GrGLShaderBuilder* builder, | 74 virtual void emitCode(GrGLShaderBuilder* builder, |
| 73 const GrEffectStage& stage, | 75 const GrDrawEffect& drawEffect, |
| 74 EffectKey key, | 76 EffectKey key, |
| 75 const char* vertexCoords, | |
| 76 const char* outputColor, | 77 const char* outputColor, |
| 77 const char* inputColor, | 78 const char* inputColor, |
| 78 const TextureSamplerArray& samplers) = 0; | 79 const TextureSamplerArray& samplers) = 0; |
| 79 | 80 |
| 80 /** A GrGLEffect instance can be reused with any GrEffect that produces the
same stage | 81 /** A GrGLEffect instance can be reused with any GrEffect that produces the
same stage |
| 81 key; this function reads data from a stage and uploads any uniform varia
bles required | 82 key; this function reads data from a stage and uploads any uniform varia
bles required |
| 82 by the shaders created in emitCode(). The GrEffect installed in the GrEf
fectStage is | 83 by the shaders created in emitCode(). The GrEffect installed in the GrEf
fectStage is |
| 83 guaranteed to be of the same type that created this GrGLEffect and to ha
ve an identical | 84 guaranteed to be of the same type that created this GrGLEffect and to ha
ve an identical |
| 84 EffectKey as the one that created this GrGLEffect. */ | 85 EffectKey as the one that created this GrGLEffect. Effects that use loca
l coords have |
| 85 virtual void setData(const GrGLUniformManager&, const GrEffectStage&); | 86 to consider whether the GrEffectStage's coord change matrix should be us
ed. When explicit |
| 87 local coordinates are used it can be ignored. */ |
| 88 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&); |
| 86 | 89 |
| 87 const char* name() const { return fFactory.name(); } | 90 const char* name() const { return fFactory.name(); } |
| 88 | 91 |
| 89 static EffectKey GenTextureKey(const GrEffectRef*, const GrGLCaps&); | 92 static EffectKey GenTextureKey(const GrDrawEffect&, const GrGLCaps&); |
| 90 static EffectKey GenAttribKey(const GrEffectStage& stage); | 93 static EffectKey GenAttribKey(const GrDrawEffect& stage); |
| 91 | |
| 92 /** | |
| 93 * GrGLEffect subclasses get passed a GrEffectStage in their emitCode and set
Data functions. | |
| 94 * The GrGLEffect usually needs to cast the stage's effect to the GrEffect su
bclass that | |
| 95 * generated the GrGLEffect. This helper does just that. | |
| 96 */ | |
| 97 template <typename T> | |
| 98 static const T& GetEffectFromStage(const GrEffectStage& effectStage) { | |
| 99 GrAssert(NULL != effectStage.getEffect()); | |
| 100 return CastEffect<T>(*effectStage.getEffect()); | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * Extracts the GrEffect from a GrEffectRef and down-casts to a GrEffect subc
lass. Usually used | |
| 105 * in a GrGLEffect subclass's constructor (which takes const GrEffectRef&). | |
| 106 */ | |
| 107 template <typename T> | |
| 108 static const T& CastEffect(const GrEffectRef& effectRef) { | |
| 109 GrAssert(NULL != effectRef.get()); | |
| 110 return *static_cast<const T*>(effectRef.get()); | |
| 111 } | |
| 112 | 94 |
| 113 protected: | 95 protected: |
| 114 const GrBackendEffectFactory& fFactory; | 96 const GrBackendEffectFactory& fFactory; |
| 115 }; | 97 }; |
| 116 | 98 |
| 117 #endif | 99 #endif |
| OLD | NEW |