18072caa80384292858d31ae34b7e19768875866bjoshualitt/*
28072caa80384292858d31ae34b7e19768875866bjoshualitt * Copyright 2013 Google Inc.
38072caa80384292858d31ae34b7e19768875866bjoshualitt *
48072caa80384292858d31ae34b7e19768875866bjoshualitt * Use of this source code is governed by a BSD-style license that can be
58072caa80384292858d31ae34b7e19768875866bjoshualitt * found in the LICENSE file.
68072caa80384292858d31ae34b7e19768875866bjoshualitt */
78072caa80384292858d31ae34b7e19768875866bjoshualitt
88072caa80384292858d31ae34b7e19768875866bjoshualitt#ifndef GrGLPrimitiveProcessor_DEFINED
98072caa80384292858d31ae34b7e19768875866bjoshualitt#define GrGLPrimitiveProcessor_DEFINED
108072caa80384292858d31ae34b7e19768875866bjoshualitt
118072caa80384292858d31ae34b7e19768875866bjoshualitt#include "GrPrimitiveProcessor.h"
128072caa80384292858d31ae34b7e19768875866bjoshualitt#include "GrGLProcessor.h"
138072caa80384292858d31ae34b7e19768875866bjoshualitt
148072caa80384292858d31ae34b7e19768875866bjoshualittclass GrBatchTracker;
158072caa80384292858d31ae34b7e19768875866bjoshualittclass GrPrimitiveProcessor;
168072caa80384292858d31ae34b7e19768875866bjoshualittclass GrGLGPBuilder;
178072caa80384292858d31ae34b7e19768875866bjoshualitt
188072caa80384292858d31ae34b7e19768875866bjoshualittclass GrGLPrimitiveProcessor {
198072caa80384292858d31ae34b7e19768875866bjoshualittpublic:
208072caa80384292858d31ae34b7e19768875866bjoshualitt    GrGLPrimitiveProcessor() : fViewMatrixName(NULL) { fViewMatrix = SkMatrix::InvalidMatrix(); }
218072caa80384292858d31ae34b7e19768875866bjoshualitt    virtual ~GrGLPrimitiveProcessor() {}
228072caa80384292858d31ae34b7e19768875866bjoshualitt
238072caa80384292858d31ae34b7e19768875866bjoshualitt    typedef GrGLProgramDataManager::UniformHandle UniformHandle;
248072caa80384292858d31ae34b7e19768875866bjoshualitt    typedef GrGLProcessor::TextureSamplerArray TextureSamplerArray;
258072caa80384292858d31ae34b7e19768875866bjoshualitt
268072caa80384292858d31ae34b7e19768875866bjoshualitt    typedef SkSTArray<2, const GrCoordTransform*, true> ProcCoords;
278072caa80384292858d31ae34b7e19768875866bjoshualitt    typedef SkSTArray<8, ProcCoords> TransformsIn;
288072caa80384292858d31ae34b7e19768875866bjoshualitt    typedef SkSTArray<8, GrGLProcessor::TransformedCoordsArray> TransformsOut;
298072caa80384292858d31ae34b7e19768875866bjoshualitt
308072caa80384292858d31ae34b7e19768875866bjoshualitt    struct EmitArgs {
318072caa80384292858d31ae34b7e19768875866bjoshualitt        EmitArgs(GrGLGPBuilder* pb,
328072caa80384292858d31ae34b7e19768875866bjoshualitt                 const GrPrimitiveProcessor& gp,
338072caa80384292858d31ae34b7e19768875866bjoshualitt                 const GrBatchTracker& bt,
348072caa80384292858d31ae34b7e19768875866bjoshualitt                 const char* outputColor,
358072caa80384292858d31ae34b7e19768875866bjoshualitt                 const char* outputCoverage,
368072caa80384292858d31ae34b7e19768875866bjoshualitt                 const TextureSamplerArray& samplers,
378072caa80384292858d31ae34b7e19768875866bjoshualitt                 const TransformsIn& transformsIn,
388072caa80384292858d31ae34b7e19768875866bjoshualitt                 TransformsOut* transformsOut)
398072caa80384292858d31ae34b7e19768875866bjoshualitt            : fPB(pb)
408072caa80384292858d31ae34b7e19768875866bjoshualitt            , fGP(gp)
418072caa80384292858d31ae34b7e19768875866bjoshualitt            , fBT(bt)
428072caa80384292858d31ae34b7e19768875866bjoshualitt            , fOutputColor(outputColor)
438072caa80384292858d31ae34b7e19768875866bjoshualitt            , fOutputCoverage(outputCoverage)
448072caa80384292858d31ae34b7e19768875866bjoshualitt            , fSamplers(samplers)
458072caa80384292858d31ae34b7e19768875866bjoshualitt            , fTransformsIn(transformsIn)
468072caa80384292858d31ae34b7e19768875866bjoshualitt            , fTransformsOut(transformsOut) {}
478072caa80384292858d31ae34b7e19768875866bjoshualitt        GrGLGPBuilder* fPB;
488072caa80384292858d31ae34b7e19768875866bjoshualitt        const GrPrimitiveProcessor& fGP;
498072caa80384292858d31ae34b7e19768875866bjoshualitt        const GrBatchTracker& fBT;
508072caa80384292858d31ae34b7e19768875866bjoshualitt        const char* fOutputColor;
518072caa80384292858d31ae34b7e19768875866bjoshualitt        const char* fOutputCoverage;
528072caa80384292858d31ae34b7e19768875866bjoshualitt        const TextureSamplerArray& fSamplers;
538072caa80384292858d31ae34b7e19768875866bjoshualitt        const TransformsIn& fTransformsIn;
548072caa80384292858d31ae34b7e19768875866bjoshualitt        TransformsOut* fTransformsOut;
558072caa80384292858d31ae34b7e19768875866bjoshualitt    };
568072caa80384292858d31ae34b7e19768875866bjoshualitt
578072caa80384292858d31ae34b7e19768875866bjoshualitt    /**
588072caa80384292858d31ae34b7e19768875866bjoshualitt     * This is similar to emitCode() in the base class, except it takes a full shader builder.
598072caa80384292858d31ae34b7e19768875866bjoshualitt     * This allows the effect subclass to emit vertex code.
608072caa80384292858d31ae34b7e19768875866bjoshualitt     */
618072caa80384292858d31ae34b7e19768875866bjoshualitt    virtual void emitCode(EmitArgs&) = 0;
628072caa80384292858d31ae34b7e19768875866bjoshualitt
638072caa80384292858d31ae34b7e19768875866bjoshualitt
648072caa80384292858d31ae34b7e19768875866bjoshualitt    /** A GrGLPrimitiveProcessor instance can be reused with any GrGLPrimitiveProcessor that
658072caa80384292858d31ae34b7e19768875866bjoshualitt        produces the same stage key; this function reads data from a GrGLPrimitiveProcessor and
668072caa80384292858d31ae34b7e19768875866bjoshualitt        uploads any uniform variables required  by the shaders created in emitCode(). The
678072caa80384292858d31ae34b7e19768875866bjoshualitt        GrPrimitiveProcessor parameter is guaranteed to be of the same type that created this
688072caa80384292858d31ae34b7e19768875866bjoshualitt        GrGLPrimitiveProcessor and to have an identical processor key as the one that created this
698072caa80384292858d31ae34b7e19768875866bjoshualitt        GrGLPrimitiveProcessor.  */
708072caa80384292858d31ae34b7e19768875866bjoshualitt    virtual void setData(const GrGLProgramDataManager&,
718072caa80384292858d31ae34b7e19768875866bjoshualitt                         const GrPrimitiveProcessor&,
728072caa80384292858d31ae34b7e19768875866bjoshualitt                         const GrBatchTracker&) = 0;
738072caa80384292858d31ae34b7e19768875866bjoshualitt
748072caa80384292858d31ae34b7e19768875866bjoshualitt    static SkMatrix GetTransformMatrix(const SkMatrix& localMatrix, const GrCoordTransform&);
758072caa80384292858d31ae34b7e19768875866bjoshualitt
768072caa80384292858d31ae34b7e19768875866bjoshualittprotected:
778072caa80384292858d31ae34b7e19768875866bjoshualitt    /** a helper which can setup vertex, constant, or uniform color depending on inputType.
788072caa80384292858d31ae34b7e19768875866bjoshualitt     *  This function will only do the minimum required to emit the correct shader code.  If
798072caa80384292858d31ae34b7e19768875866bjoshualitt     *  inputType == attribute, then colorAttr must not be NULL.  Likewise, if inputType == Uniform
808072caa80384292858d31ae34b7e19768875866bjoshualitt     *  then colorUniform must not be NULL.
818072caa80384292858d31ae34b7e19768875866bjoshualitt     */
828072caa80384292858d31ae34b7e19768875866bjoshualitt    void setupColorPassThrough(GrGLGPBuilder* pb,
838072caa80384292858d31ae34b7e19768875866bjoshualitt                               GrGPInput inputType,
848072caa80384292858d31ae34b7e19768875866bjoshualitt                               const char* inputName,
858072caa80384292858d31ae34b7e19768875866bjoshualitt                               const GrPrimitiveProcessor::Attribute* colorAttr,
868072caa80384292858d31ae34b7e19768875866bjoshualitt                               UniformHandle* colorUniform);
878072caa80384292858d31ae34b7e19768875866bjoshualitt
888072caa80384292858d31ae34b7e19768875866bjoshualitt    const char* uViewM() const { return fViewMatrixName; }
898072caa80384292858d31ae34b7e19768875866bjoshualitt
908072caa80384292858d31ae34b7e19768875866bjoshualitt    /** a helper function to setup the uniform handle for the uniform view matrix */
918072caa80384292858d31ae34b7e19768875866bjoshualitt    void addUniformViewMatrix(GrGLGPBuilder*);
928072caa80384292858d31ae34b7e19768875866bjoshualitt
938072caa80384292858d31ae34b7e19768875866bjoshualitt
948072caa80384292858d31ae34b7e19768875866bjoshualitt    /** a helper function to upload a uniform viewmatrix.
958072caa80384292858d31ae34b7e19768875866bjoshualitt     * TODO we can remove this function when we have deferred geometry in place
968072caa80384292858d31ae34b7e19768875866bjoshualitt     */
978072caa80384292858d31ae34b7e19768875866bjoshualitt    void setUniformViewMatrix(const GrGLProgramDataManager&,
988072caa80384292858d31ae34b7e19768875866bjoshualitt                              const SkMatrix& viewMatrix);
998072caa80384292858d31ae34b7e19768875866bjoshualitt
1008072caa80384292858d31ae34b7e19768875866bjoshualitt    class ShaderVarHandle {
1018072caa80384292858d31ae34b7e19768875866bjoshualitt    public:
1028072caa80384292858d31ae34b7e19768875866bjoshualitt        bool isValid() const { return fHandle > -1; }
1038072caa80384292858d31ae34b7e19768875866bjoshualitt        ShaderVarHandle() : fHandle(-1) {}
1048072caa80384292858d31ae34b7e19768875866bjoshualitt        ShaderVarHandle(int value) : fHandle(value) { SkASSERT(this->isValid()); }
1058072caa80384292858d31ae34b7e19768875866bjoshualitt        int handle() const { SkASSERT(this->isValid()); return fHandle; }
1068072caa80384292858d31ae34b7e19768875866bjoshualitt        UniformHandle convertToUniformHandle() {
1078072caa80384292858d31ae34b7e19768875866bjoshualitt            SkASSERT(this->isValid());
1088072caa80384292858d31ae34b7e19768875866bjoshualitt            return GrGLProgramDataManager::UniformHandle::CreateFromUniformIndex(fHandle);
1098072caa80384292858d31ae34b7e19768875866bjoshualitt        }
1108072caa80384292858d31ae34b7e19768875866bjoshualitt
1118072caa80384292858d31ae34b7e19768875866bjoshualitt    private:
1128072caa80384292858d31ae34b7e19768875866bjoshualitt        int fHandle;
1138072caa80384292858d31ae34b7e19768875866bjoshualitt    };
1148072caa80384292858d31ae34b7e19768875866bjoshualitt
1158072caa80384292858d31ae34b7e19768875866bjoshualitt    struct Transform {
1168072caa80384292858d31ae34b7e19768875866bjoshualitt        Transform() : fType(kVoid_GrSLType) { fCurrentValue = SkMatrix::InvalidMatrix(); }
1178072caa80384292858d31ae34b7e19768875866bjoshualitt        ShaderVarHandle fHandle;
1188072caa80384292858d31ae34b7e19768875866bjoshualitt        SkMatrix       fCurrentValue;
1198072caa80384292858d31ae34b7e19768875866bjoshualitt        GrSLType       fType;
1208072caa80384292858d31ae34b7e19768875866bjoshualitt    };
1218072caa80384292858d31ae34b7e19768875866bjoshualitt
1228072caa80384292858d31ae34b7e19768875866bjoshualitt    SkSTArray<8, SkSTArray<2, Transform, true> > fInstalledTransforms;
1238072caa80384292858d31ae34b7e19768875866bjoshualitt
1248072caa80384292858d31ae34b7e19768875866bjoshualittprivate:
1258072caa80384292858d31ae34b7e19768875866bjoshualitt    UniformHandle fViewMatrixUniform;
1268072caa80384292858d31ae34b7e19768875866bjoshualitt    SkMatrix fViewMatrix;
1278072caa80384292858d31ae34b7e19768875866bjoshualitt    const char* fViewMatrixName;
1288072caa80384292858d31ae34b7e19768875866bjoshualitt};
1298072caa80384292858d31ae34b7e19768875866bjoshualitt
1308072caa80384292858d31ae34b7e19768875866bjoshualitt#endif
131