commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [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 GrTypesPriv_DEFINED |
| 9 | #define GrTypesPriv_DEFINED |
| 10 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame^] | 11 | #include "GrTypes.h" |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 12 | #include "SkTArray.h" |
| 13 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 14 | /** |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame^] | 15 | * Types of shader-language-specific boxed variables we can create. (Currently only GrGLShaderVars, |
| 16 | * but should be applicable to other shader languages.) |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 17 | */ |
| 18 | enum GrSLType { |
| 19 | kVoid_GrSLType, |
| 20 | kFloat_GrSLType, |
| 21 | kVec2f_GrSLType, |
| 22 | kVec3f_GrSLType, |
| 23 | kVec4f_GrSLType, |
| 24 | kMat33f_GrSLType, |
| 25 | kMat44f_GrSLType, |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame^] | 26 | kSampler2D_GrSLType, |
| 27 | |
| 28 | kLast_GrSLType = kSampler2D_GrSLType |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 29 | }; |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame^] | 30 | static const int kGrSLTypeCount = kLast_GrSLType + 1; |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 31 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 32 | /** |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame^] | 33 | * Gets the vector size of the SLType. Returns -1 for void, matrices, and samplers. |
| 34 | */ |
| 35 | static inline int GrSLTypeVectorCount(GrSLType type) { |
| 36 | GrAssert(type >= 0 && type < kGrSLTypeCount); |
| 37 | static const int kCounts[] = { -1, 1, 2, 3, 4, -1, -1, -1 }; |
| 38 | return kCounts[type]; |
| 39 | |
| 40 | GR_STATIC_ASSERT(0 == kVoid_GrSLType); |
| 41 | GR_STATIC_ASSERT(1 == kFloat_GrSLType); |
| 42 | GR_STATIC_ASSERT(2 == kVec2f_GrSLType); |
| 43 | GR_STATIC_ASSERT(3 == kVec3f_GrSLType); |
| 44 | GR_STATIC_ASSERT(4 == kVec4f_GrSLType); |
| 45 | GR_STATIC_ASSERT(5 == kMat33f_GrSLType); |
| 46 | GR_STATIC_ASSERT(6 == kMat44f_GrSLType); |
| 47 | GR_STATIC_ASSERT(7 == kSampler2D_GrSLType); |
| 48 | GR_STATIC_ASSERT(GR_ARRAY_COUNT(kCounts) == kGrSLTypeCount); |
| 49 | } |
| 50 | |
| 51 | static inline GrSLType GrSLFloatVectorType(int count) { |
| 52 | GrAssert(count > 0 && count <= 4); |
| 53 | return (GrSLType)(count); |
| 54 | |
| 55 | GR_STATIC_ASSERT(kFloat_GrSLType == 1); |
| 56 | GR_STATIC_ASSERT(kVec2f_GrSLType == 2); |
| 57 | GR_STATIC_ASSERT(kVec3f_GrSLType == 3); |
| 58 | GR_STATIC_ASSERT(kVec4f_GrSLType == 4); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Types used to describe format of vertices in arrays. |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 63 | */ |
| 64 | enum GrVertexAttribType { |
| 65 | kFloat_GrVertexAttribType = 0, |
| 66 | kVec2f_GrVertexAttribType, |
| 67 | kVec3f_GrVertexAttribType, |
| 68 | kVec4f_GrVertexAttribType, |
| 69 | kVec4ub_GrVertexAttribType, // vector of 4 unsigned bytes, e.g. colors |
| 70 | |
| 71 | kLast_GrVertexAttribType = kVec4ub_GrVertexAttribType |
| 72 | }; |
| 73 | static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1; |
| 74 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame^] | 75 | /** |
| 76 | * Returns the vector size of the type. |
| 77 | */ |
| 78 | static inline int GrVertexAttribTypeVectorCount(GrVertexAttribType type) { |
| 79 | GrAssert(type >= 0 && type < kGrVertexAttribTypeCount); |
| 80 | static const int kCounts[] = { 1, 2, 3, 4, 4 }; |
| 81 | return kCounts[type]; |
| 82 | |
| 83 | GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType); |
| 84 | GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType); |
| 85 | GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType); |
| 86 | GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType); |
| 87 | GR_STATIC_ASSERT(4 == kVec4ub_GrVertexAttribType); |
| 88 | GR_STATIC_ASSERT(GR_ARRAY_COUNT(kCounts) == kGrVertexAttribTypeCount); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the size of the attrib type in bytes. |
| 93 | */ |
| 94 | static inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) { |
| 95 | GrAssert(type >= 0 && type < kGrVertexAttribTypeCount); |
| 96 | static const size_t kSizes[] = { |
| 97 | sizeof(float), // kFloat_GrVertexAttribType |
| 98 | 2*sizeof(float), // kVec2f_GrVertexAttribType |
| 99 | 3*sizeof(float), // kVec3f_GrVertexAttribType |
| 100 | 4*sizeof(float), // kVec4f_GrVertexAttribType |
| 101 | 4*sizeof(char) // kVec4ub_GrVertexAttribType |
| 102 | }; |
| 103 | return kSizes[type]; |
| 104 | |
| 105 | GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType); |
| 106 | GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType); |
| 107 | GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType); |
| 108 | GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType); |
| 109 | GR_STATIC_ASSERT(4 == kVec4ub_GrVertexAttribType); |
| 110 | GR_STATIC_ASSERT(GR_ARRAY_COUNT(kSizes) == kGrVertexAttribTypeCount); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Semantic bindings for vertex attributes. kEffect means that the attribute is input to a GrEffect. |
| 115 | * Each binding other than kEffect may not appear more than once in the current set of attributes. |
| 116 | * kPosition must be appear for exactly one attribute. |
| 117 | */ |
| 118 | enum GrVertexAttribBinding { |
| 119 | kPosition_GrVertexAttribBinding, // required, must have vector count of 2 |
| 120 | kLocalCoord_GrVertexAttribBinding, // must have vector count of 2 |
| 121 | kColor_GrVertexAttribBinding, // must have vector count of 4 |
| 122 | kCoverage_GrVertexAttribBinding, // must have vector count of 4 |
| 123 | |
| 124 | kLastFixedFunction_GrVertexAttribBinding = kCoverage_GrVertexAttribBinding, |
| 125 | |
| 126 | kEffect_GrVertexAttribBinding, // vector length must agree with |
| 127 | // GrEffect::vertexAttribType() for each effect input to |
| 128 | // which the attribute is mapped by GrDrawState::setEffect() |
| 129 | kLast_GrVertexAttribBinding = kEffect_GrVertexAttribBinding |
| 130 | }; |
| 131 | |
| 132 | static const int kGrVertexAttribBindingCnt = kLast_GrVertexAttribBinding + 1; |
| 133 | static const int kGrFixedFunctionVertexAttribBindingCnt = |
| 134 | kLastFixedFunction_GrVertexAttribBinding + 1; |
| 135 | |
| 136 | static inline const int GrFixedFunctionVertexAttribVectorCount(GrVertexAttribBinding binding) { |
| 137 | GrAssert(binding >= 0 && binding < kGrFixedFunctionVertexAttribBindingCnt); |
| 138 | static const int kVecCounts[] = { 2, 2, 4, 4 }; |
| 139 | |
| 140 | return kVecCounts[binding]; |
| 141 | |
| 142 | GR_STATIC_ASSERT(0 == kPosition_GrVertexAttribBinding); |
| 143 | GR_STATIC_ASSERT(1 == kLocalCoord_GrVertexAttribBinding); |
| 144 | GR_STATIC_ASSERT(2 == kColor_GrVertexAttribBinding); |
| 145 | GR_STATIC_ASSERT(3 == kCoverage_GrVertexAttribBinding); |
| 146 | GR_STATIC_ASSERT(kGrFixedFunctionVertexAttribBindingCnt == SK_ARRAY_COUNT(kVecCounts)); |
| 147 | } |
| 148 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 149 | struct GrVertexAttrib { |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame^] | 150 | inline void set(GrVertexAttribType type, size_t offset, GrVertexAttribBinding binding) { |
| 151 | fType = type; |
| 152 | fOffset = offset; |
| 153 | fBinding = binding; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 154 | } |
| 155 | bool operator==(const GrVertexAttrib& other) const { |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame^] | 156 | return fType == other.fType && fOffset == other.fOffset && fBinding == other.fBinding; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 157 | }; |
| 158 | bool operator!=(const GrVertexAttrib& other) const { return !(*this == other); } |
| 159 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame^] | 160 | GrVertexAttribType fType; |
| 161 | size_t fOffset; |
| 162 | GrVertexAttribBinding fBinding; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame^] | 165 | template <int N> class GrVertexAttribArray : public SkSTArray<N, GrVertexAttrib, true> {}; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 166 | |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 167 | #endif |