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