Home | History | Annotate | Download | only in gl
      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 #include "GrEffectStage.h"
     16 
     17 class GrGLTexture;
     18 
     19 /** @file
     20     This file contains specializations for OpenGL of the shader stages declared in
     21     include/gpu/GrEffect.h. Objects of type GrGLEffect are responsible for emitting the
     22     GLSL code that implements a GrEffect and for uploading uniforms at draw time. They also
     23     must have a function:
     24         static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&)
     25     that is used to implement a program cache. When two GrEffects produce the same key this means
     26     that their GrGLEffects would emit the same GLSL code.
     27 
     28     These objects are created by the factory object returned by the GrEffect::getFactory().
     29 */
     30 
     31 class GrGLEffect {
     32 
     33 public:
     34     typedef GrBackendEffectFactory::EffectKey EffectKey;
     35 
     36     enum {
     37         kNoEffectKey = GrBackendEffectFactory::kNoEffectKey,
     38         // the number of bits in EffectKey available to GenKey
     39         kEffectKeyBits = GrBackendEffectFactory::kEffectKeyBits,
     40     };
     41 
     42     typedef GrGLShaderBuilder::TextureSamplerArray TextureSamplerArray;
     43 
     44     GrGLEffect(const GrBackendEffectFactory&);
     45 
     46     virtual ~GrGLEffect();
     47 
     48     /** Called when the program stage should insert its code into the shaders. The code in each
     49         shader will be in its own block ({}) and so locally scoped names will not collide across
     50         stages.
     51 
     52         @param builder      Interface used to emit code in the shaders.
     53         @param stage        The effect stage that generated this program stage.
     54         @param key          The key that was computed by GenKey() from the generating GrEffect.
     55                             Only the bits indicated by GrBackendEffectFactory::kEffectKeyBits are
     56                             guaranteed to match the value produced by GenKey();
     57         @param vertexCoords A vec2 in the VS that holds the position in local coords. This is either
     58                             the pre-view-matrix vertex position or if explicit per-vertex texture
     59                             coords are used with a stage then it is those coordinates. See
     60                             GrVertexLayout.
     61         @param outputColor  A predefined vec4 in the FS in which the stage should place its output
     62                             color (or coverage).
     63         @param inputColor   A vec4 that holds the input color to the stage in the FS. This may be
     64                             NULL in which case the implied input is solid white (all ones).
     65                             TODO: Better system for communicating optimization info (e.g. input
     66                             color is solid white, trans black, known to be opaque, etc.) that allows
     67                             the effect to communicate back similar known info about its output.
     68         @param samplers     One entry for each GrTextureAccess of the GrEffect that generated the
     69                             GrGLEffect. These can be passed to the builder to emit texture
     70                             reads in the generated code.
     71         */
     72     virtual void emitCode(GrGLShaderBuilder* builder,
     73                           const GrEffectStage& stage,
     74                           EffectKey key,
     75                           const char* vertexCoords,
     76                           const char* outputColor,
     77                           const char* inputColor,
     78                           const TextureSamplerArray& samplers) = 0;
     79 
     80     /** 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 variables required
     82         by the shaders created in emitCode(). The GrEffect installed in the GrEffectStage is
     83         guaranteed to be of the same type that created this GrGLEffect and to have an identical
     84         EffectKey as the one that created this GrGLEffect. */
     85     virtual void setData(const GrGLUniformManager&, const GrEffectStage&);
     86 
     87     const char* name() const { return fFactory.name(); }
     88 
     89     static EffectKey GenTextureKey(const GrEffectRef*, const GrGLCaps&);
     90 
     91    /**
     92     * GrGLEffect subclasses get passed a GrEffectStage in their emitCode and setData functions.
     93     * The GrGLEffect usually needs to cast the stage's effect to the GrEffect subclass that
     94     * generated the GrGLEffect. This helper does just that.
     95     */
     96     template <typename T>
     97     static const T& GetEffectFromStage(const GrEffectStage& effectStage) {
     98         GrAssert(NULL != effectStage.getEffect());
     99         return CastEffect<T>(*effectStage.getEffect());
    100     }
    101 
    102    /**
    103     * Extracts the GrEffect from a GrEffectRef and down-casts to a GrEffect subclass. Usually used
    104     * in a GrGLEffect subclass's constructor (which takes const GrEffectRef&).
    105     */
    106     template <typename T>
    107     static const T& CastEffect(const GrEffectRef& effectRef) {
    108         GrAssert(NULL != effectRef.get());
    109         return *static_cast<const T*>(effectRef.get());
    110     }
    111 
    112 protected:
    113     const GrBackendEffectFactory& fFactory;
    114 };
    115 
    116 #endif
    117