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 GrPrimitiveProcessor_DEFINED
98072caa80384292858d31ae34b7e19768875866bjoshualitt#define GrPrimitiveProcessor_DEFINED
108072caa80384292858d31ae34b7e19768875866bjoshualitt
118072caa80384292858d31ae34b7e19768875866bjoshualitt#include "GrColor.h"
128072caa80384292858d31ae34b7e19768875866bjoshualitt#include "GrProcessor.h"
138072caa80384292858d31ae34b7e19768875866bjoshualitt#include "GrShaderVar.h"
148072caa80384292858d31ae34b7e19768875866bjoshualitt
158072caa80384292858d31ae34b7e19768875866bjoshualitt/*
168072caa80384292858d31ae34b7e19768875866bjoshualitt * The GrPrimitiveProcessor represents some kind of geometric primitive.  This includes the shape
178072caa80384292858d31ae34b7e19768875866bjoshualitt * of the primitive and the inherent color of the primitive.  The GrPrimitiveProcessor is
188072caa80384292858d31ae34b7e19768875866bjoshualitt * responsible for providing a color and coverage input into the Ganesh rendering pipeline.  Through
198072caa80384292858d31ae34b7e19768875866bjoshualitt * optimization, Ganesh may decide a different color, no color, and / or no coverage are required
208072caa80384292858d31ae34b7e19768875866bjoshualitt * from the GrPrimitiveProcessor, so the GrPrimitiveProcessor must be able to support this
218072caa80384292858d31ae34b7e19768875866bjoshualitt * functionality.  We also use the GrPrimitiveProcessor to make batching decisions.
228072caa80384292858d31ae34b7e19768875866bjoshualitt *
238072caa80384292858d31ae34b7e19768875866bjoshualitt * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the
248072caa80384292858d31ae34b7e19768875866bjoshualitt * GrPrimitiveProcessor.  These loops run on the CPU and compute any invariant components which
258072caa80384292858d31ae34b7e19768875866bjoshualitt * might be useful for correctness / optimization decisions.  The GrPrimitiveProcessor seeds these
268072caa80384292858d31ae34b7e19768875866bjoshualitt * loops, one with initial color and one with initial coverage, in its
278072caa80384292858d31ae34b7e19768875866bjoshualitt * onComputeInvariantColor / Coverage calls.  These seed values are processed by the subsequent
288072caa80384292858d31ae34b7e19768875866bjoshualitt * stages of the rendering pipeline and the output is then fed back into the GrPrimitiveProcessor in
298072caa80384292858d31ae34b7e19768875866bjoshualitt * the initBatchTracker call, where the GrPrimitiveProcessor can then initialize the GrBatchTracker
308072caa80384292858d31ae34b7e19768875866bjoshualitt * struct with the appropriate values.
318072caa80384292858d31ae34b7e19768875866bjoshualitt *
328072caa80384292858d31ae34b7e19768875866bjoshualitt * We are evolving this system to move towards generating geometric meshes and their associated
338072caa80384292858d31ae34b7e19768875866bjoshualitt * vertex data after we have batched and reordered draws.  This system, known as 'deferred geometry'
348072caa80384292858d31ae34b7e19768875866bjoshualitt * will allow the GrPrimitiveProcessor much greater control over how data is transmitted to shaders.
358072caa80384292858d31ae34b7e19768875866bjoshualitt *
368072caa80384292858d31ae34b7e19768875866bjoshualitt * In a deferred geometry world, the GrPrimitiveProcessor can always 'batch'  To do this, each
378072caa80384292858d31ae34b7e19768875866bjoshualitt * primitive type is associated with one GrPrimitiveProcessor, who has complete control of how
388072caa80384292858d31ae34b7e19768875866bjoshualitt * it draws.  Each primitive draw will bundle all required data to perform the draw, and these
398072caa80384292858d31ae34b7e19768875866bjoshualitt * bundles of data will be owned by an instance of the associated GrPrimitiveProcessor.  Bundles
408072caa80384292858d31ae34b7e19768875866bjoshualitt * can be updated alongside the GrBatchTracker struct itself, ultimately allowing the
418072caa80384292858d31ae34b7e19768875866bjoshualitt * GrPrimitiveProcessor complete control of how it gets data into the fragment shader as long as
428072caa80384292858d31ae34b7e19768875866bjoshualitt * it emits the appropriate color, or none at all, as directed.
438072caa80384292858d31ae34b7e19768875866bjoshualitt */
448072caa80384292858d31ae34b7e19768875866bjoshualitt
458072caa80384292858d31ae34b7e19768875866bjoshualitt/*
468072caa80384292858d31ae34b7e19768875866bjoshualitt * A struct for tracking batching decisions.  While this lives on GrOptState, it is managed
478072caa80384292858d31ae34b7e19768875866bjoshualitt * entirely by the derived classes of the GP.
488072caa80384292858d31ae34b7e19768875866bjoshualitt * // TODO this was an early attempt at handling out of order batching.  It should be
498072caa80384292858d31ae34b7e19768875866bjoshualitt * used carefully as it is being replaced by GrBatch
508072caa80384292858d31ae34b7e19768875866bjoshualitt */
518072caa80384292858d31ae34b7e19768875866bjoshualittclass GrBatchTracker {
528072caa80384292858d31ae34b7e19768875866bjoshualittpublic:
538072caa80384292858d31ae34b7e19768875866bjoshualitt    template <typename T> const T& cast() const {
548072caa80384292858d31ae34b7e19768875866bjoshualitt        SkASSERT(sizeof(T) <= kMaxSize);
558072caa80384292858d31ae34b7e19768875866bjoshualitt        return *reinterpret_cast<const T*>(fData.get());
568072caa80384292858d31ae34b7e19768875866bjoshualitt    }
578072caa80384292858d31ae34b7e19768875866bjoshualitt
588072caa80384292858d31ae34b7e19768875866bjoshualitt    template <typename T> T* cast() {
598072caa80384292858d31ae34b7e19768875866bjoshualitt        SkASSERT(sizeof(T) <= kMaxSize);
608072caa80384292858d31ae34b7e19768875866bjoshualitt        return reinterpret_cast<T*>(fData.get());
618072caa80384292858d31ae34b7e19768875866bjoshualitt    }
628072caa80384292858d31ae34b7e19768875866bjoshualitt
638072caa80384292858d31ae34b7e19768875866bjoshualitt    static const size_t kMaxSize = 32;
648072caa80384292858d31ae34b7e19768875866bjoshualitt
658072caa80384292858d31ae34b7e19768875866bjoshualittprivate:
668072caa80384292858d31ae34b7e19768875866bjoshualitt    SkAlignedSStorage<kMaxSize> fData;
678072caa80384292858d31ae34b7e19768875866bjoshualitt};
688072caa80384292858d31ae34b7e19768875866bjoshualitt
69e9c0fc616d2a1632c285885b9b656b68ca8d4f24jvanverthclass GrGLSLCaps;
708072caa80384292858d31ae34b7e19768875866bjoshualittclass GrGLPrimitiveProcessor;
718072caa80384292858d31ae34b7e19768875866bjoshualitt
728072caa80384292858d31ae34b7e19768875866bjoshualittstruct GrInitInvariantOutput;
738072caa80384292858d31ae34b7e19768875866bjoshualitt
748072caa80384292858d31ae34b7e19768875866bjoshualitt/*
758072caa80384292858d31ae34b7e19768875866bjoshualitt * This struct allows the GrPipeline to communicate information about the pipeline.  Most of this
768072caa80384292858d31ae34b7e19768875866bjoshualitt * is overrides, but some of it is general information.  Logically it should live in GrPipeline.h,
778072caa80384292858d31ae34b7e19768875866bjoshualitt * but this is problematic due to circular dependencies.
788072caa80384292858d31ae34b7e19768875866bjoshualitt */
798072caa80384292858d31ae34b7e19768875866bjoshualittstruct GrPipelineInfo {
808072caa80384292858d31ae34b7e19768875866bjoshualitt    bool fColorIgnored;
818072caa80384292858d31ae34b7e19768875866bjoshualitt    bool fCoverageIgnored;
828072caa80384292858d31ae34b7e19768875866bjoshualitt    GrColor fOverrideColor;
838072caa80384292858d31ae34b7e19768875866bjoshualitt    bool fUsesLocalCoords;
84f7c2d558726b8d629e1453b7201a0dd6bfda7b05egdaniel    bool fCanTweakAlphaForCoverage;
858072caa80384292858d31ae34b7e19768875866bjoshualitt};
868072caa80384292858d31ae34b7e19768875866bjoshualitt
878072caa80384292858d31ae34b7e19768875866bjoshualitt/*
888072caa80384292858d31ae34b7e19768875866bjoshualitt * This enum is shared by GrPrimitiveProcessors and GrGLPrimitiveProcessors to coordinate shaders
898072caa80384292858d31ae34b7e19768875866bjoshualitt * with vertex attributes / uniforms.
908072caa80384292858d31ae34b7e19768875866bjoshualitt */
918072caa80384292858d31ae34b7e19768875866bjoshualittenum GrGPInput {
928072caa80384292858d31ae34b7e19768875866bjoshualitt    kAllOnes_GrGPInput,
938072caa80384292858d31ae34b7e19768875866bjoshualitt    kAttribute_GrGPInput,
948072caa80384292858d31ae34b7e19768875866bjoshualitt    kUniform_GrGPInput,
958072caa80384292858d31ae34b7e19768875866bjoshualitt    kIgnored_GrGPInput,
968072caa80384292858d31ae34b7e19768875866bjoshualitt};
978072caa80384292858d31ae34b7e19768875866bjoshualitt
988072caa80384292858d31ae34b7e19768875866bjoshualitt/*
998072caa80384292858d31ae34b7e19768875866bjoshualitt * GrPrimitiveProcessor defines an interface which all subclasses must implement.  All
1008072caa80384292858d31ae34b7e19768875866bjoshualitt * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage
1018072caa80384292858d31ae34b7e19768875866bjoshualitt * pipelines, and they must provide some notion of equality
1028072caa80384292858d31ae34b7e19768875866bjoshualitt */
1038072caa80384292858d31ae34b7e19768875866bjoshualittclass GrPrimitiveProcessor : public GrProcessor {
1048072caa80384292858d31ae34b7e19768875866bjoshualittpublic:
1058072caa80384292858d31ae34b7e19768875866bjoshualitt    virtual void initBatchTracker(GrBatchTracker*, const GrPipelineInfo&) const = 0;
1068072caa80384292858d31ae34b7e19768875866bjoshualitt
1078072caa80384292858d31ae34b7e19768875866bjoshualitt    virtual bool canMakeEqual(const GrBatchTracker& mine,
1088072caa80384292858d31ae34b7e19768875866bjoshualitt                              const GrPrimitiveProcessor& that,
1098072caa80384292858d31ae34b7e19768875866bjoshualitt                              const GrBatchTracker& theirs) const = 0;
1108072caa80384292858d31ae34b7e19768875866bjoshualitt
1118072caa80384292858d31ae34b7e19768875866bjoshualitt    virtual void getInvariantOutputColor(GrInitInvariantOutput* out) const = 0;
1128072caa80384292858d31ae34b7e19768875866bjoshualitt    virtual void getInvariantOutputCoverage(GrInitInvariantOutput* out) const = 0;
1138072caa80384292858d31ae34b7e19768875866bjoshualitt
1148072caa80384292858d31ae34b7e19768875866bjoshualitt    // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but
1158072caa80384292858d31ae34b7e19768875866bjoshualitt    // we put these calls on the base class to prevent having to cast
1168072caa80384292858d31ae34b7e19768875866bjoshualitt    virtual bool willUseGeoShader() const = 0;
1178072caa80384292858d31ae34b7e19768875866bjoshualitt
1188072caa80384292858d31ae34b7e19768875866bjoshualitt    /*
1198072caa80384292858d31ae34b7e19768875866bjoshualitt     * This is a safeguard to prevent GrPrimitiveProcessor's from going beyond platform specific
1208072caa80384292858d31ae34b7e19768875866bjoshualitt     * attribute limits. This number can almost certainly be raised if required.
1218072caa80384292858d31ae34b7e19768875866bjoshualitt     */
1228072caa80384292858d31ae34b7e19768875866bjoshualitt    static const int kMaxVertexAttribs = 6;
1238072caa80384292858d31ae34b7e19768875866bjoshualitt
1248072caa80384292858d31ae34b7e19768875866bjoshualitt    struct Attribute {
1258072caa80384292858d31ae34b7e19768875866bjoshualitt        Attribute()
1268072caa80384292858d31ae34b7e19768875866bjoshualitt            : fName(NULL)
1278072caa80384292858d31ae34b7e19768875866bjoshualitt            , fType(kFloat_GrVertexAttribType)
1288072caa80384292858d31ae34b7e19768875866bjoshualitt            , fOffset(0) {}
129c375b0b5c5c03d381111bf7bff12507d5ed24769senorblanco        Attribute(const char* name, GrVertexAttribType type,
130c375b0b5c5c03d381111bf7bff12507d5ed24769senorblanco                  GrSLPrecision precision = kDefault_GrSLPrecision)
1318072caa80384292858d31ae34b7e19768875866bjoshualitt            : fName(name)
1328072caa80384292858d31ae34b7e19768875866bjoshualitt            , fType(type)
133c375b0b5c5c03d381111bf7bff12507d5ed24769senorblanco            , fOffset(SkAlign4(GrVertexAttribTypeSize(type)))
134c375b0b5c5c03d381111bf7bff12507d5ed24769senorblanco            , fPrecision(precision) {}
1358072caa80384292858d31ae34b7e19768875866bjoshualitt        const char* fName;
1368072caa80384292858d31ae34b7e19768875866bjoshualitt        GrVertexAttribType fType;
1378072caa80384292858d31ae34b7e19768875866bjoshualitt        size_t fOffset;
138c375b0b5c5c03d381111bf7bff12507d5ed24769senorblanco        GrSLPrecision fPrecision;
1398072caa80384292858d31ae34b7e19768875866bjoshualitt    };
1408072caa80384292858d31ae34b7e19768875866bjoshualitt
1418072caa80384292858d31ae34b7e19768875866bjoshualitt    int numAttribs() const { return fNumAttribs; }
1428072caa80384292858d31ae34b7e19768875866bjoshualitt    const Attribute& getAttrib(int index) const {
1438072caa80384292858d31ae34b7e19768875866bjoshualitt        SkASSERT(index < fNumAttribs);
1448072caa80384292858d31ae34b7e19768875866bjoshualitt        return fAttribs[index];
1458072caa80384292858d31ae34b7e19768875866bjoshualitt    }
1468072caa80384292858d31ae34b7e19768875866bjoshualitt
1478072caa80384292858d31ae34b7e19768875866bjoshualitt    // Returns the vertex stride of the GP.  A common use case is to request geometry from a
1488072caa80384292858d31ae34b7e19768875866bjoshualitt    // drawtarget based off of the stride, and to populate this memory using an implicit array of
1498072caa80384292858d31ae34b7e19768875866bjoshualitt    // structs.  In this case, it is best to assert the vertexstride == sizeof(VertexStruct).
1508072caa80384292858d31ae34b7e19768875866bjoshualitt    size_t getVertexStride() const { return fVertexStride; }
1518072caa80384292858d31ae34b7e19768875866bjoshualitt
1528072caa80384292858d31ae34b7e19768875866bjoshualitt    /**
1538072caa80384292858d31ae34b7e19768875866bjoshualitt     * Gets a transformKey from an array of coord transforms
1548072caa80384292858d31ae34b7e19768875866bjoshualitt     */
1558072caa80384292858d31ae34b7e19768875866bjoshualitt    uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>&) const;
1568072caa80384292858d31ae34b7e19768875866bjoshualitt
1578072caa80384292858d31ae34b7e19768875866bjoshualitt    /**
1588072caa80384292858d31ae34b7e19768875866bjoshualitt     * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
1598072caa80384292858d31ae34b7e19768875866bjoshualitt     * processor's GL backend implementation.
1608072caa80384292858d31ae34b7e19768875866bjoshualitt     */
1618072caa80384292858d31ae34b7e19768875866bjoshualitt    virtual void getGLProcessorKey(const GrBatchTracker& bt,
162cfc18867d982119d9dc2888bf09f1093012daaddjvanverth                                   const GrGLSLCaps& caps,
1638072caa80384292858d31ae34b7e19768875866bjoshualitt                                   GrProcessorKeyBuilder* b) const = 0;
1648072caa80384292858d31ae34b7e19768875866bjoshualitt
1658072caa80384292858d31ae34b7e19768875866bjoshualitt
1668072caa80384292858d31ae34b7e19768875866bjoshualitt    /** Returns a new instance of the appropriate *GL* implementation class
1678072caa80384292858d31ae34b7e19768875866bjoshualitt        for the given GrProcessor; caller is responsible for deleting
1688072caa80384292858d31ae34b7e19768875866bjoshualitt        the object. */
1698072caa80384292858d31ae34b7e19768875866bjoshualitt    virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
170cfc18867d982119d9dc2888bf09f1093012daaddjvanverth                                                     const GrGLSLCaps& caps) const = 0;
1718072caa80384292858d31ae34b7e19768875866bjoshualitt
1728072caa80384292858d31ae34b7e19768875866bjoshualitt    bool isPathRendering() const { return fIsPathRendering; }
1738072caa80384292858d31ae34b7e19768875866bjoshualitt
1748072caa80384292858d31ae34b7e19768875866bjoshualittprotected:
175e3ababe44315452cd33b96a18ce316ede09ff3c3joshualitt    GrPrimitiveProcessor(bool isPathRendering)
1768072caa80384292858d31ae34b7e19768875866bjoshualitt        : fNumAttribs(0)
1778072caa80384292858d31ae34b7e19768875866bjoshualitt        , fVertexStride(0)
1788072caa80384292858d31ae34b7e19768875866bjoshualitt        , fIsPathRendering(isPathRendering) {}
1798072caa80384292858d31ae34b7e19768875866bjoshualitt
1808072caa80384292858d31ae34b7e19768875866bjoshualitt    Attribute fAttribs[kMaxVertexAttribs];
1818072caa80384292858d31ae34b7e19768875866bjoshualitt    int fNumAttribs;
1828072caa80384292858d31ae34b7e19768875866bjoshualitt    size_t fVertexStride;
1838072caa80384292858d31ae34b7e19768875866bjoshualitt
1848072caa80384292858d31ae34b7e19768875866bjoshualittprivate:
1858072caa80384292858d31ae34b7e19768875866bjoshualitt    virtual bool hasExplicitLocalCoords() const = 0;
1868072caa80384292858d31ae34b7e19768875866bjoshualitt
1878072caa80384292858d31ae34b7e19768875866bjoshualitt    bool fIsPathRendering;
1888072caa80384292858d31ae34b7e19768875866bjoshualitt
1898072caa80384292858d31ae34b7e19768875866bjoshualitt    typedef GrProcessor INHERITED;
1908072caa80384292858d31ae34b7e19768875866bjoshualitt};
1918072caa80384292858d31ae34b7e19768875866bjoshualitt
1928072caa80384292858d31ae34b7e19768875866bjoshualitt#endif
193