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 "GrGLShaderBuilder.h" 13 #include "GrGLShaderVar.h" 14 #include "GrGLSL.h" 15 16 class GrGLTexture; 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 emitting the 21 GLSL code that implements a GrEffect and for uploading uniforms at draw time. They also 22 must have a function: 23 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) 24 that is used to implement a program cache. When two GrEffects produce the same 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 GrDrawEffect&) 29 The effect held by the GrDrawEffect is guaranteed to be of the type that generated the 30 GrGLEffect subclass instance. 31 32 These objects are created by the factory object returned by the GrEffect::getFactory(). 33 */ 34 35 class GrDrawEffect; 36 37 class GrGLEffect { 38 39 public: 40 typedef GrBackendEffectFactory::EffectKey EffectKey; 41 42 enum { 43 kNoEffectKey = GrBackendEffectFactory::kNoEffectKey, 44 // the number of bits in EffectKey available to GenKey 45 kEffectKeyBits = GrBackendEffectFactory::kEffectKeyBits, 46 }; 47 48 typedef GrGLShaderBuilder::TextureSamplerArray TextureSamplerArray; 49 50 GrGLEffect(const GrBackendEffectFactory&); 51 52 virtual ~GrGLEffect(); 53 54 /** Called when the program stage should insert its code into the shaders. The code in each 55 shader will be in its own block ({}) and so locally scoped names will not collide across 56 stages. 57 58 @param builder Interface used to emit code in the shaders. 59 @param drawEffect A wrapper on the effect that generated this program stage. 60 @param key The key that was computed by GenKey() from the generating GrEffect. 61 Only the bits indicated by GrBackendEffectFactory::kEffectKeyBits are 62 guaranteed to match the value produced by GenKey(); 63 @param outputColor A predefined vec4 in the FS in which the stage should place its output 64 color (or coverage). 65 @param inputColor A vec4 that holds the input color to the stage in the FS. This may be 66 NULL in which case the implied input is solid white (all ones). 67 TODO: Better system for communicating optimization info (e.g. input 68 color is solid white, trans black, known to be opaque, etc.) that allows 69 the effect to communicate back similar known info about its output. 70 @param samplers One entry for each GrTextureAccess of the GrEffect that generated the 71 GrGLEffect. These can be passed to the builder to emit texture 72 reads in the generated code. 73 */ 74 virtual void emitCode(GrGLShaderBuilder* builder, 75 const GrDrawEffect& drawEffect, 76 EffectKey key, 77 const char* outputColor, 78 const char* inputColor, 79 const TextureSamplerArray& samplers) = 0; 80 81 /** A GrGLEffect instance can be reused with any GrEffect that produces the same stage 82 key; this function reads data from a stage and uploads any uniform variables required 83 by the shaders created in emitCode(). The GrEffect installed in the GrEffectStage is 84 guaranteed to be of the same type that created this GrGLEffect and to have an identical 85 EffectKey as the one that created this GrGLEffect. Effects that use local coords have 86 to consider whether the GrEffectStage's coord change matrix should be used. When explicit 87 local coordinates are used it can be ignored. */ 88 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&); 89 90 const char* name() const { return fFactory.name(); } 91 92 static EffectKey GenTextureKey(const GrDrawEffect&, const GrGLCaps&); 93 static EffectKey GenAttribKey(const GrDrawEffect& stage); 94 95 protected: 96 const GrBackendEffectFactory& fFactory; 97 }; 98 99 #endif 100