Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2016 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 gr_instanced_InstancedRenderingTypes_DEFINED | |
| 9 #define gr_instanced_InstancedRenderingTypes_DEFINED | |
| 10 | |
| 11 #include "GrTypes.h" | |
| 12 #include "SkRRect.h" | |
| 13 | |
| 14 namespace gr_instanced { | |
| 15 | |
| 16 enum class AntialiasMode : uint8_t { | |
| 17 kNone, | |
| 18 kCoverage, | |
| 19 kMSAA, | |
| 20 kMixedSamples | |
| 21 }; | |
| 22 constexpr int kNumAntialiasModes = 1 + (int)AntialiasMode::kMixedSamples; | |
| 23 | |
| 24 enum class ShapeType : uint8_t { | |
| 25 kRect, | |
| 26 kOval, | |
| 27 kSimpleRRect, | |
| 28 kNinePatch, | |
| 29 kComplexRRect | |
| 30 }; | |
| 31 constexpr int kNumShapeTypes = 1 + (int)ShapeType::kComplexRRect; | |
| 32 | |
| 33 inline static ShapeType GetRRectShapeType(const SkRRect& rrect) { | |
| 34 SkASSERT(rrect.getType() >= SkRRect::kRect_Type && | |
| 35 rrect.getType() <= SkRRect::kComplex_Type); | |
| 36 return static_cast<ShapeType>(rrect.getType() - 1); | |
| 37 | |
| 38 GR_STATIC_ASSERT((int)ShapeType::kRect == SkRRect::kRect_Type - 1); | |
| 39 GR_STATIC_ASSERT((int)ShapeType::kOval == SkRRect::kOval_Type - 1); | |
| 40 GR_STATIC_ASSERT((int)ShapeType::kSimpleRRect == SkRRect::kSimple_Type - 1); | |
| 41 GR_STATIC_ASSERT((int)ShapeType::kNinePatch == SkRRect::kNinePatch_Type - 1) ; | |
| 42 GR_STATIC_ASSERT((int)ShapeType::kComplexRRect == SkRRect::kComplex_Type - 1 ); | |
| 43 GR_STATIC_ASSERT(kNumShapeTypes == SkRRect::kComplex_Type); | |
| 44 } | |
| 45 | |
| 46 /** | |
| 47 * Per-vertex data. These values get fed into normal vertex attribs. | |
| 48 */ | |
| 49 struct ShapeVertex { | |
| 50 float fX, fY; //!< Shape coordinates. | |
| 51 int32_t fAttrs; //!< Shape-specific vertex attributes, if needed. | |
| 52 }; | |
| 53 | |
| 54 template<typename T> struct TRange { | |
| 55 bool operator ==(const TRange& that) const { | |
| 56 return fStart == that.fStart && fCount == that.fCount; | |
| 57 } | |
| 58 bool operator !=(const TRange& that) const { return !(*this == that); } | |
| 59 | |
| 60 bool isEmpty() const { return fCount <= 0; } | |
| 61 T end() { return fStart + fCount; } | |
| 62 | |
| 63 T fStart; | |
| 64 T fCount; | |
| 65 }; | |
| 66 | |
| 67 typedef TRange<int16_t> IndexRange; | |
| 68 typedef TRange<int> InstanceRange; | |
| 69 | |
| 70 /** | |
| 71 * Per-instance data. These values get fed into instanced vertex attribs. | |
| 72 */ | |
| 73 struct Instance { | |
| 74 uint32_t fInfo; //!< Packed info about the instance. See Inf oBits. | |
| 75 float fShapeMatrix2x3[6]; //!< Maps canonical shape coords -> device s pace coords. | |
| 76 uint32_t fColor; //!< Color to be written out by the primitiv e processor. | |
| 77 float fLocalRect[4]; //!< Local coords rect that spans [-1, +1] i n shape coords. | |
| 78 }; | |
| 79 | |
| 80 /** | |
| 81 * Additional parameters required by some instances (e.g. round rect radii, pers pective column, | |
| 82 * local matrix). These are accessed via texel buffer. | |
| 83 */ | |
| 84 struct ParamsTexel { | |
| 85 float fX, fY, fZ, fW; | |
| 86 }; | |
| 87 | |
| 88 GR_STATIC_ASSERT(0 == offsetof(ParamsTexel, fX)); | |
| 89 GR_STATIC_ASSERT(4 * 4 == sizeof(ParamsTexel)); | |
| 90 | |
| 91 enum AttribIdx { | |
|
bsalomon
2016/06/20 14:49:54
You could make these enum classes if you wanted. I
csmartdalton
2016/06/20 15:33:47
Yeah AttribIdx should be made into an enum class w
| |
| 92 kShapeCoords_AttribIdx, | |
| 93 kVertexAttrs_AttribIdx, | |
| 94 kInstanceInfo_AttribIdx, | |
| 95 kShapeMatrixX_AttribIdx, | |
| 96 kShapeMatrixY_AttribIdx, | |
| 97 kColor_AttribIdx, | |
| 98 kLocalRect_AttribIdx, | |
| 99 | |
| 100 kNumVertexAttribs | |
| 101 }; | |
| 102 | |
| 103 enum ShapeFlag { | |
| 104 kRect_ShapeFlag = (1 << (int)ShapeType::kRect), | |
| 105 kOval_ShapeFlag = (1 << (int)ShapeType::kOval), | |
| 106 kSimpleRRect_ShapeFlag = (1 << (int)ShapeType::kSimpleRRect), | |
| 107 kNinePatch_ShapeFlag = (1 << (int)ShapeType::kNinePatch), | |
| 108 kComplexRRect_ShapeFlag = (1 << (int)ShapeType::kComplexRRect), | |
| 109 | |
| 110 kRRect_ShapesMask = kSimpleRRect_ShapeFlag | kNinePatch_ShapeFlag | kComplex RRect_ShapeFlag | |
| 111 }; | |
| 112 | |
| 113 constexpr uint8_t GetShapeFlag(ShapeType type) { return 1 << (int)type; } | |
| 114 | |
| 115 /** | |
| 116 * Defines what data is stored at which bits in the fInfo field of the instanced data. | |
| 117 */ | |
| 118 enum InfoBits { | |
| 119 kShapeType_InfoBit = 29, | |
| 120 kInnerShapeType_InfoBit = 27, | |
| 121 kPerspective_InfoBit = 26, | |
| 122 kLocalMatrix_InfoBit = 25, | |
| 123 kParamsIdx_InfoBit = 0 | |
| 124 }; | |
| 125 | |
| 126 enum InfoMasks { | |
| 127 kShapeType_InfoMask = 0u - (1 << kShapeType_InfoBit), | |
| 128 kInnerShapeType_InfoMask = (1 << kShapeType_InfoBit) - (1 << kInnerShapeTyp e_InfoBit), | |
| 129 kPerspective_InfoFlag = (1 << kPerspective_InfoBit), | |
| 130 kLocalMatrix_InfoFlag = (1 << kLocalMatrix_InfoBit), | |
| 131 kParamsIdx_InfoMask = (1 << kLocalMatrix_InfoBit) - 1 | |
| 132 }; | |
| 133 | |
| 134 GR_STATIC_ASSERT((kNumShapeTypes - 1) <= (uint32_t)kShapeType_InfoMask >> kShape Type_InfoBit); | |
| 135 GR_STATIC_ASSERT((int)ShapeType::kSimpleRRect <= | |
| 136 kInnerShapeType_InfoMask >> kInnerShapeType_InfoBit); | |
| 137 | |
| 138 /** | |
| 139 * Tracks all information needed in order to draw a batch of instances. This str uct also serves | |
| 140 * as an all-in-one shader key for the batch. | |
| 141 */ | |
| 142 struct BatchInfo { | |
| 143 BatchInfo() : fData(0) {} | |
| 144 | |
| 145 bool isSimpleRects() const { | |
| 146 return !((fShapeTypes & ~kRect_ShapeFlag) | fInnerShapeTypes); | |
| 147 } | |
| 148 | |
| 149 bool canJoin(BatchInfo that) const { | |
| 150 if (fAntialiasMode != that.fAntialiasMode) { | |
| 151 return false; | |
| 152 } | |
| 153 if (SkToBool(fInnerShapeTypes) != SkToBool(that.fInnerShapeTypes)) { | |
| 154 // GrInstanceProcessor can't currently combine draws with and withou t inner shapes. | |
| 155 return false; | |
| 156 } | |
| 157 if (fCannotDiscard != that.fCannotDiscard) { | |
| 158 // For stencil draws, the use of discard can be a requirement. | |
| 159 return false; | |
| 160 } | |
| 161 return true; | |
| 162 } | |
| 163 | |
| 164 void join(BatchInfo that) { | |
| 165 SkASSERT(this->canJoin(that)); | |
| 166 fData |= that.fData; | |
| 167 } | |
| 168 | |
| 169 union { | |
| 170 struct { | |
| 171 AntialiasMode fAntialiasMode; | |
| 172 uint8_t fShapeTypes; | |
| 173 uint8_t fInnerShapeTypes; | |
| 174 bool fHasPerspective : 1; | |
| 175 bool fHasLocalMatrix : 1; | |
| 176 bool fHasParams : 1; | |
| 177 bool fNonSquare : 1; | |
| 178 bool fUsesLocalCoords : 1; | |
| 179 bool fCannotTweakAlphaForCoverage : 1; | |
| 180 bool fCannotDiscard : 1; | |
| 181 }; | |
| 182 uint32_t fData; | |
| 183 }; | |
| 184 }; | |
| 185 | |
| 186 // This is required since all the data must fit into 32 bits of a shader key. | |
| 187 GR_STATIC_ASSERT(sizeof(uint32_t) == sizeof(BatchInfo)); | |
| 188 GR_STATIC_ASSERT(kNumShapeTypes <= 8); | |
| 189 | |
| 190 } | |
| 191 | |
| 192 #endif | |
| OLD | NEW |