joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 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 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 Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 21 | * functionality. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 22 | * |
| 23 | * There are two feedback loops between the GrFragmentProcessors, the GrXferProcessor, and the |
Brian Salomon | cb30bb2 | 2017-02-12 09:28:54 -0500 | [diff] [blame] | 24 | * 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 Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 27 | * getProcessorAnalysisInputs implementation. These seed values are processed by the |
Brian Salomon | 5298dc8 | 2017-02-22 11:52:03 -0500 | [diff] [blame] | 28 | * subsequent |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 29 | * 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. |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 32 | */ |
| 33 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 34 | class GrGLSLPrimitiveProcessor; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 35 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 36 | /* |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 37 | * 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 | */ |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 41 | class GrPrimitiveProcessor : public GrResourceIOProcessor, public GrProgramElement { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 42 | public: |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 43 | /** Describes a vertex or instance attribute. */ |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 44 | class Attribute { |
| 45 | public: |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 46 | constexpr Attribute() = default; |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 47 | constexpr Attribute(const char* name, GrVertexAttribType type) : fName(name), fType(type) {} |
| 48 | constexpr Attribute(const Attribute&) = default; |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 49 | |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 50 | Attribute& operator=(const Attribute&) = default; |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 51 | |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 52 | constexpr bool isInitialized() const { return SkToBool(fName); } |
| 53 | |
| 54 | constexpr const char* name() const { return fName; } |
| 55 | constexpr GrVertexAttribType type() const { return fType; } |
| 56 | |
| 57 | constexpr size_t size() const { return GrVertexAttribTypeSize(fType); } |
| 58 | constexpr size_t sizeAlign4() const { return SkAlign4(this->size()); } |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 59 | |
| 60 | GrShaderVar asShaderVar() const { |
| 61 | return {fName, GrVertexAttribTypeToSLType(fType), GrShaderVar::kIn_TypeModifier}; |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | const char* fName = nullptr; |
| 66 | GrVertexAttribType fType = kFloat_GrVertexAttribType; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 69 | GrPrimitiveProcessor(ClassID); |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 70 | |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 71 | int numVertexAttributes() const { return fVertexAttributeCnt; } |
| 72 | const Attribute& vertexAttribute(int i) const; |
| 73 | int numInstanceAttributes() const { return fInstanceAttributeCnt; } |
| 74 | const Attribute& instanceAttribute(int i) const; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 75 | |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 76 | bool hasVertexAttributes() const { return SkToBool(fVertexAttributeCnt); } |
| 77 | bool hasInstanceAttributes() const { return SkToBool(fInstanceAttributeCnt); } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 78 | |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 79 | #ifdef SK_DEBUG |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 80 | /** |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 81 | * A common practice is to populate the the vertex/instance's memory using an implicit array of |
| 82 | * structs. In this case, it is best to assert that: |
| 83 | * debugOnly_stride == sizeof(struct) and |
| 84 | * offsetof(struct, field[i]) == debugOnly_AttributeOffset(i) |
| 85 | * In general having Op subclasses assert that attribute offsets and strides agree with their |
| 86 | * tessellation code's expectations is good practice. |
| 87 | * However, these functions walk the attributes to compute offsets and call virtual functions |
| 88 | * to access the attributes. Thus, they are only available in debug builds. |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 89 | */ |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 90 | size_t debugOnly_vertexStride() const; |
| 91 | size_t debugOnly_instanceStride() const; |
| 92 | size_t debugOnly_vertexAttributeOffset(int) const; |
| 93 | size_t debugOnly_instanceAttributeOffset(int) const; |
| 94 | #endif |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 95 | |
| 96 | // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but |
| 97 | // we put these calls on the base class to prevent having to cast |
| 98 | virtual bool willUseGeoShader() const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 99 | |
| 100 | /** |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 101 | * Computes a transformKey from an array of coord transforms. Will only look at the first |
| 102 | * <numCoords> transforms in the array. |
| 103 | * |
| 104 | * TODO: A better name for this function would be "compute" instead of "get". |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 105 | */ |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 106 | uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords, |
| 107 | int numCoords) const; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry |
| 111 | * processor's GL backend implementation. |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 112 | * |
| 113 | * TODO: A better name for this function would be "compute" instead of "get". |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 114 | */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 115 | virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 116 | |
| 117 | |
| 118 | /** Returns a new instance of the appropriate *GL* implementation class |
| 119 | for the given GrProcessor; caller is responsible for deleting |
| 120 | the object. */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 121 | virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 122 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 123 | virtual bool isPathRendering() const { return false; } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 124 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 125 | /** |
| 126 | * If non-null, overrides the dest color returned by GrGLSLFragmentShaderBuilder::dstColor(). |
| 127 | */ |
| 128 | virtual const char* getDestColorOverride() const { return nullptr; } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 129 | |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 130 | virtual float getSampleShading() const { return 0.0; } |
ethannicholas | 28ef445 | 2016-03-25 09:26:03 -0700 | [diff] [blame] | 131 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 132 | protected: |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 133 | void setVertexAttributeCnt(int cnt) { fVertexAttributeCnt = cnt; } |
| 134 | void setInstanceAttributeCnt(int cnt) { fInstanceAttributeCnt = cnt; } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 135 | |
| 136 | private: |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 137 | void addPendingIOs() const override { GrResourceIOProcessor::addPendingIOs(); } |
| 138 | void removeRefs() const override { GrResourceIOProcessor::removeRefs(); } |
| 139 | void pendingIOComplete() const override { GrResourceIOProcessor::pendingIOComplete(); } |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 140 | void notifyRefCntIsZero() const final {} |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 141 | |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 142 | virtual const Attribute& onVertexAttribute(int) const = 0; |
| 143 | virtual const Attribute& onInstanceAttribute(int) const = 0; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 144 | |
Brian Salomon | 19c1233 | 2018-06-18 12:52:47 -0400 | [diff] [blame] | 145 | int fVertexAttributeCnt = 0; |
| 146 | int fInstanceAttributeCnt = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 147 | typedef GrProcessor INHERITED; |
| 148 | }; |
| 149 | |
| 150 | #endif |