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: |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 43 | struct Attribute { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 44 | enum class InputRate : bool { |
| 45 | kPerVertex, |
| 46 | kPerInstance |
| 47 | }; |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 48 | GrShaderVar asShaderVar() const { |
| 49 | return GrShaderVar(fName, GrVertexAttribTypeToSLType(fType), |
| 50 | GrShaderVar::kIn_TypeModifier); |
| 51 | } |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 52 | bool isInitialized() const { return SkToBool(fName); } |
| 53 | Attribute() = default; |
| 54 | Attribute(const char* name, GrVertexAttribType type, int offset, InputRate rate) |
| 55 | : fName(name), fType(type), fOffsetInRecord(offset), fInputRate(rate) {} |
| 56 | const char* fName = nullptr; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 57 | GrVertexAttribType fType; |
| 58 | int fOffsetInRecord; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 59 | InputRate fInputRate; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 62 | GrPrimitiveProcessor(ClassID classID) |
| 63 | : GrResourceIOProcessor(classID) {} |
| 64 | |
bsalomon | 7dbd45d | 2016-03-23 10:40:53 -0700 | [diff] [blame] | 65 | int numAttribs() const { return fAttribs.count(); } |
| 66 | const Attribute& getAttrib(int index) const { return fAttribs[index]; } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 67 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 68 | bool hasVertexAttribs() const { return SkToBool(fVertexStride); } |
| 69 | bool hasInstanceAttribs() const { return SkToBool(fInstanceStride); } |
| 70 | |
| 71 | /** |
| 72 | * These return the strides of the vertex and instance buffers. Attributes are expected to be |
| 73 | * laid out interleaved in their corresponding buffer (vertex or instance). fOffsetInRecord |
| 74 | * indicates an attribute's location in bytes relative to the first attribute. (These are padded |
| 75 | * to the nearest 4 bytes for performance reasons.) |
| 76 | * |
| 77 | * A common practice is to populate the buffer's memory using an implicit array of structs. In |
| 78 | * this case, it is best to assert: |
| 79 | * |
| 80 | * stride == sizeof(struct) and |
| 81 | * offsetof(struct, field[i]) == attrib[i].fOffsetInRecord |
| 82 | * |
| 83 | * NOTE: for instanced draws the vertex buffer has a single record that each instance reuses. |
| 84 | */ |
| 85 | int getVertexStride() const { return fVertexStride; } |
| 86 | int getInstanceStride() const { return fInstanceStride; } |
| 87 | |
| 88 | // Only the GrGeometryProcessor subclass actually has a geo shader or vertex attributes, but |
| 89 | // we put these calls on the base class to prevent having to cast |
| 90 | virtual bool willUseGeoShader() const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 91 | |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 92 | bool willUsePrimitiveRestart() const { return fWillUsePrimitiveRestart; } |
| 93 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 94 | /** |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 95 | * Computes a transformKey from an array of coord transforms. Will only look at the first |
| 96 | * <numCoords> transforms in the array. |
| 97 | * |
| 98 | * TODO: A better name for this function would be "compute" instead of "get". |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 99 | */ |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 100 | uint32_t getTransformKey(const SkTArray<const GrCoordTransform*, true>& coords, |
| 101 | int numCoords) const; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this geometry |
| 105 | * processor's GL backend implementation. |
wangyix | a7f4c43 | 2015-08-20 07:25:02 -0700 | [diff] [blame] | 106 | * |
| 107 | * TODO: A better name for this function would be "compute" instead of "get". |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 108 | */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 109 | virtual void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 110 | |
| 111 | |
| 112 | /** Returns a new instance of the appropriate *GL* implementation class |
| 113 | for the given GrProcessor; caller is responsible for deleting |
| 114 | the object. */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 115 | virtual GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const = 0; |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 116 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 117 | virtual bool isPathRendering() const { return false; } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 118 | |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 119 | /** |
| 120 | * If non-null, overrides the dest color returned by GrGLSLFragmentShaderBuilder::dstColor(). |
| 121 | */ |
| 122 | virtual const char* getDestColorOverride() const { return nullptr; } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 123 | |
ethannicholas | 28ef445 | 2016-03-25 09:26:03 -0700 | [diff] [blame] | 124 | virtual float getSampleShading() const { |
| 125 | return 0.0; |
| 126 | } |
| 127 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 128 | protected: |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 129 | /** |
| 130 | * Subclasses call these from their constructor to register vertex and instance attributes. |
| 131 | */ |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 132 | const Attribute& addVertexAttrib(const char* name, GrVertexAttribType type) { |
| 133 | fAttribs.push_back() = {name, type, fVertexStride, Attribute::InputRate::kPerVertex}; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 134 | fVertexStride += static_cast<int>(SkAlign4(GrVertexAttribTypeSize(type))); |
| 135 | return fAttribs.back(); |
| 136 | } |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 137 | const Attribute& addInstanceAttrib(const char* name, GrVertexAttribType type) { |
| 138 | fAttribs.push_back() = {name, type, fInstanceStride, Attribute::InputRate::kPerInstance}; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 139 | fInstanceStride += static_cast<int>(SkAlign4(GrVertexAttribTypeSize(type))); |
| 140 | return fAttribs.back(); |
| 141 | } |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 142 | |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 143 | void setWillUsePrimitiveRestart() { fWillUsePrimitiveRestart = true; } |
| 144 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 145 | private: |
Brian Salomon | d61c9d9 | 2017-04-10 10:54:25 -0400 | [diff] [blame] | 146 | void addPendingIOs() const override { GrResourceIOProcessor::addPendingIOs(); } |
| 147 | void removeRefs() const override { GrResourceIOProcessor::removeRefs(); } |
| 148 | void pendingIOComplete() const override { GrResourceIOProcessor::pendingIOComplete(); } |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 149 | void notifyRefCntIsZero() const final {} |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 150 | virtual bool hasExplicitLocalCoords() const = 0; |
| 151 | |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 152 | SkSTArray<8, Attribute> fAttribs; |
| 153 | int fVertexStride = 0; |
| 154 | int fInstanceStride = 0; |
| 155 | bool fWillUsePrimitiveRestart = false; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 156 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 157 | typedef GrProcessor INHERITED; |
| 158 | }; |
| 159 | |
| 160 | #endif |