blob: ddccd99496139c5d84471746e84f6be1db6e95ea [file] [log] [blame]
joshualitt8072caa2015-02-12 14:20:52 -08001/*
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 GrPrimitiveProcessor_DEFINED
9#define GrPrimitiveProcessor_DEFINED
10
11#include "GrColor.h"
12#include "GrProcessor.h"
13#include "GrShaderVar.h"
14
15/*
16 * The GrPrimitiveProcessor represents some kind of geometric primitive. This includes the shape
17 * of the primitive and the inherent color of the primitive. The GrPrimitiveProcessor is
18 * responsible for providing a color and coverage input into the Ganesh rendering pipeline. Through
19 * optimization, Ganesh may decide a different color, no color, and / or no coverage are required
20 * from the GrPrimitiveProcessor, so the GrPrimitiveProcessor must be able to support this
Brian Salomon09d994e2016-12-21 11:14:46 -050021 * functionality.
joshualitt8072caa2015-02-12 14:20:52 -080022 *
23 * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the
Brian Salomoncb30bb22017-02-12 09:28:54 -050024 * GrPrimitiveProcessor. These loops run on the CPU and to determine known properties of the final
25 * color and coverage inputs to the GrXferProcessor in order to perform optimizations that preserve
26 * correctness. The GrDrawOp seeds these loops with initial color and coverage, in its
Brian Salomona811b122017-03-30 08:21:32 -040027 * getProcessorAnalysisInputs implementation. These seed values are processed by the
Brian Salomon5298dc82017-02-22 11:52:03 -050028 * subsequent
Brian Salomon92aee3d2016-12-21 09:20:25 -050029 * stages of the rendering pipeline and the output is then fed back into the GrDrawOp in
30 * the applyPipelineOptimizations call, where the op can use the information to inform decisions
31 * about GrPrimitiveProcessor creation.
joshualitt8072caa2015-02-12 14:20:52 -080032 */
33
egdaniele659a582015-11-13 09:55:43 -080034class GrGLSLPrimitiveProcessor;
joshualitt8072caa2015-02-12 14:20:52 -080035
joshualitt8072caa2015-02-12 14:20:52 -080036/*
joshualitt8072caa2015-02-12 14:20:52 -080037 * GrPrimitiveProcessor defines an interface which all subclasses must implement. All
38 * GrPrimitiveProcessors must proivide seed color and coverage for the Ganesh color / coverage
39 * pipelines, and they must provide some notion of equality
40 */
41class GrPrimitiveProcessor : public GrProcessor {
42public:
joshualitt8072caa2015-02-12 14:20:52 -080043 // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but
44 // we put these calls on the base class to prevent having to cast
45 virtual bool willUseGeoShader() const = 0;
46
joshualitt8072caa2015-02-12 14:20:52 -080047 struct Attribute {
48 Attribute()
halcanary96fcdcc2015-08-27 07:41:13 -070049 : fName(nullptr)
joshualitt8072caa2015-02-12 14:20:52 -080050 , fType(kFloat_GrVertexAttribType)
51 , fOffset(0) {}
bsalomon6cb807b2016-08-17 11:33:39 -070052 Attribute(const char* name, GrVertexAttribType type, GrSLPrecision precision)
joshualitt8072caa2015-02-12 14:20:52 -080053 : fName(name)
54 , fType(type)
senorblancof2539d52015-05-20 14:03:42 -070055 , fOffset(SkAlign4(GrVertexAttribTypeSize(type)))
56 , fPrecision(precision) {}
joshualitt8072caa2015-02-12 14:20:52 -080057 const char* fName;
58 GrVertexAttribType fType;
59 size_t fOffset;
senorblancof2539d52015-05-20 14:03:42 -070060 GrSLPrecision fPrecision;
joshualitt8072caa2015-02-12 14:20:52 -080061 };
62
bsalomon7dbd45d2016-03-23 10:40:53 -070063 int numAttribs() const { return fAttribs.count(); }
64 const Attribute& getAttrib(int index) const { return fAttribs[index]; }
joshualitt8072caa2015-02-12 14:20:52 -080065
66 // Returns the vertex stride of the GP. A common use case is to request geometry from a
Robert Phillipsf2361d22016-10-25 14:20:06 -040067 // GrOpList based off of the stride, and to populate this memory using an implicit array of
joshualitt8072caa2015-02-12 14:20:52 -080068 // structs. In this case, it is best to assert the vertexstride == sizeof(VertexStruct).
69 size_t getVertexStride() const { return fVertexStride; }
70
71 /**
wangyixa7f4c432015-08-20 07:25:02 -070072 * Computes a transformKey from an array of coord transforms. Will only look at the first
73 * <numCoords> transforms in the array.
74 *
75 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -080076 */
wangyixa7f4c432015-08-20 07:25:02 -070077 uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords,
78 int numCoords) const;
joshualitt8072caa2015-02-12 14:20:52 -080079
80 /**
81 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry
82 * processor's GL backend implementation.
wangyixa7f4c432015-08-20 07:25:02 -070083 *
84 * TODO: A better name for this function would be "compute" instead of "get".
joshualitt8072caa2015-02-12 14:20:52 -080085 */
Brian Salomon94efbf52016-11-29 13:43:05 -050086 virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -080087
88
89 /** Returns a new instance of the appropriate *GL* implementation class
90 for the given GrProcessor; caller is responsible for deleting
91 the object. */
Brian Salomon94efbf52016-11-29 13:43:05 -050092 virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0;
joshualitt8072caa2015-02-12 14:20:52 -080093
ethannicholas22793252016-01-30 09:59:10 -080094 virtual bool isPathRendering() const { return false; }
joshualitt8072caa2015-02-12 14:20:52 -080095
ethannicholas22793252016-01-30 09:59:10 -080096 /**
97 * If non-null, overrides the dest color returned by GrGLSLFragmentShaderBuilder::dstColor().
98 */
99 virtual const char* getDestColorOverride() const { return nullptr; }
halcanary9d524f22016-03-29 09:03:52 -0700100
ethannicholas28ef4452016-03-25 09:26:03 -0700101 virtual float getSampleShading() const {
102 return 0.0;
103 }
104
dvonbeck9b03e7b2016-08-01 11:01:56 -0700105 /* Sub-class should override and return true if this primitive processor implements the distance
106 * vector field, a field of vectors to the nearest point in the edge of the shape. */
107 virtual bool implementsDistanceVector() const { return false; }
108
joshualitt8072caa2015-02-12 14:20:52 -0800109protected:
bsalomon7dbd45d2016-03-23 10:40:53 -0700110 GrPrimitiveProcessor() : fVertexStride(0) {}
joshualitt8072caa2015-02-12 14:20:52 -0800111
bsalomon7dbd45d2016-03-23 10:40:53 -0700112 enum { kPreallocAttribCnt = 8 };
113 SkSTArray<kPreallocAttribCnt, Attribute> fAttribs;
joshualitt8072caa2015-02-12 14:20:52 -0800114 size_t fVertexStride;
115
116private:
Mike Kleinfc6c37b2016-09-27 09:34:10 -0400117 void notifyRefCntIsZero() const final {}
joshualitt8072caa2015-02-12 14:20:52 -0800118 virtual bool hasExplicitLocalCoords() const = 0;
119
joshualitt8072caa2015-02-12 14:20:52 -0800120 typedef GrProcessor INHERITED;
121};
122
123#endif