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