Home | History | Annotate | Download | only in gl
      1 /*
      2  * Copyright 2013 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 GrGLPrimitiveProcessor_DEFINED
      9 #define GrGLPrimitiveProcessor_DEFINED
     10 
     11 #include "GrPrimitiveProcessor.h"
     12 #include "GrGLProcessor.h"
     13 
     14 class GrBatchTracker;
     15 class GrPrimitiveProcessor;
     16 class GrGLGPBuilder;
     17 
     18 class GrGLPrimitiveProcessor {
     19 public:
     20     GrGLPrimitiveProcessor() : fViewMatrixName(NULL) { fViewMatrix = SkMatrix::InvalidMatrix(); }
     21     virtual ~GrGLPrimitiveProcessor() {}
     22 
     23     typedef GrGLProgramDataManager::UniformHandle UniformHandle;
     24     typedef GrGLProcessor::TextureSamplerArray TextureSamplerArray;
     25 
     26     typedef SkSTArray<2, const GrCoordTransform*, true> ProcCoords;
     27     typedef SkSTArray<8, ProcCoords> TransformsIn;
     28     typedef SkSTArray<8, GrGLProcessor::TransformedCoordsArray> TransformsOut;
     29 
     30     struct EmitArgs {
     31         EmitArgs(GrGLGPBuilder* pb,
     32                  const GrPrimitiveProcessor& gp,
     33                  const GrBatchTracker& bt,
     34                  const char* outputColor,
     35                  const char* outputCoverage,
     36                  const TextureSamplerArray& samplers,
     37                  const TransformsIn& transformsIn,
     38                  TransformsOut* transformsOut)
     39             : fPB(pb)
     40             , fGP(gp)
     41             , fBT(bt)
     42             , fOutputColor(outputColor)
     43             , fOutputCoverage(outputCoverage)
     44             , fSamplers(samplers)
     45             , fTransformsIn(transformsIn)
     46             , fTransformsOut(transformsOut) {}
     47         GrGLGPBuilder* fPB;
     48         const GrPrimitiveProcessor& fGP;
     49         const GrBatchTracker& fBT;
     50         const char* fOutputColor;
     51         const char* fOutputCoverage;
     52         const TextureSamplerArray& fSamplers;
     53         const TransformsIn& fTransformsIn;
     54         TransformsOut* fTransformsOut;
     55     };
     56 
     57     /**
     58      * This is similar to emitCode() in the base class, except it takes a full shader builder.
     59      * This allows the effect subclass to emit vertex code.
     60      */
     61     virtual void emitCode(EmitArgs&) = 0;
     62 
     63 
     64     /** A GrGLPrimitiveProcessor instance can be reused with any GrGLPrimitiveProcessor that
     65         produces the same stage key; this function reads data from a GrGLPrimitiveProcessor and
     66         uploads any uniform variables required  by the shaders created in emitCode(). The
     67         GrPrimitiveProcessor parameter is guaranteed to be of the same type that created this
     68         GrGLPrimitiveProcessor and to have an identical processor key as the one that created this
     69         GrGLPrimitiveProcessor.  */
     70     virtual void setData(const GrGLProgramDataManager&,
     71                          const GrPrimitiveProcessor&,
     72                          const GrBatchTracker&) = 0;
     73 
     74     static SkMatrix GetTransformMatrix(const SkMatrix& localMatrix, const GrCoordTransform&);
     75 
     76 protected:
     77     /** a helper which can setup vertex, constant, or uniform color depending on inputType.
     78      *  This function will only do the minimum required to emit the correct shader code.  If
     79      *  inputType == attribute, then colorAttr must not be NULL.  Likewise, if inputType == Uniform
     80      *  then colorUniform must not be NULL.
     81      */
     82     void setupColorPassThrough(GrGLGPBuilder* pb,
     83                                GrGPInput inputType,
     84                                const char* inputName,
     85                                const GrPrimitiveProcessor::Attribute* colorAttr,
     86                                UniformHandle* colorUniform);
     87 
     88     const char* uViewM() const { return fViewMatrixName; }
     89 
     90     /** a helper function to setup the uniform handle for the uniform view matrix */
     91     void addUniformViewMatrix(GrGLGPBuilder*);
     92 
     93 
     94     /** a helper function to upload a uniform viewmatrix.
     95      * TODO we can remove this function when we have deferred geometry in place
     96      */
     97     void setUniformViewMatrix(const GrGLProgramDataManager&,
     98                               const SkMatrix& viewMatrix);
     99 
    100     class ShaderVarHandle {
    101     public:
    102         bool isValid() const { return fHandle > -1; }
    103         ShaderVarHandle() : fHandle(-1) {}
    104         ShaderVarHandle(int value) : fHandle(value) { SkASSERT(this->isValid()); }
    105         int handle() const { SkASSERT(this->isValid()); return fHandle; }
    106         UniformHandle convertToUniformHandle() {
    107             SkASSERT(this->isValid());
    108             return GrGLProgramDataManager::UniformHandle::CreateFromUniformIndex(fHandle);
    109         }
    110 
    111     private:
    112         int fHandle;
    113     };
    114 
    115     struct Transform {
    116         Transform() : fType(kVoid_GrSLType) { fCurrentValue = SkMatrix::InvalidMatrix(); }
    117         ShaderVarHandle fHandle;
    118         SkMatrix       fCurrentValue;
    119         GrSLType       fType;
    120     };
    121 
    122     SkSTArray<8, SkSTArray<2, Transform, true> > fInstalledTransforms;
    123 
    124 private:
    125     UniformHandle fViewMatrixUniform;
    126     SkMatrix fViewMatrix;
    127     const char* fViewMatrixName;
    128 };
    129 
    130 #endif
    131