blob: eaf1aa4050615805910e6719d9baa2f1343527ba [file] [log] [blame]
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +00001/*
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.com054ae992013-04-01 20:06:51 +000011#include "GrTypes.h"
bsalomon@google.com31ec7982013-03-27 18:14:57 +000012#include "SkTArray.h"
bsalomon3e791242014-12-17 13:43:13 -080013#include "SkRect.h"
bsalomon@google.com31ec7982013-03-27 18:14:57 +000014
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000015/**
jvanverth@google.com054ae992013-04-01 20:06:51 +000016 * Types of shader-language-specific boxed variables we can create. (Currently only GrGLShaderVars,
17 * but should be applicable to other shader languages.)
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000018 */
19enum GrSLType {
20 kVoid_GrSLType,
21 kFloat_GrSLType,
22 kVec2f_GrSLType,
23 kVec3f_GrSLType,
24 kVec4f_GrSLType,
25 kMat33f_GrSLType,
26 kMat44f_GrSLType,
jvanverth@google.com054ae992013-04-01 20:06:51 +000027 kSampler2D_GrSLType,
28
29 kLast_GrSLType = kSampler2D_GrSLType
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000030};
jvanverth@google.com054ae992013-04-01 20:06:51 +000031static const int kGrSLTypeCount = kLast_GrSLType + 1;
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000032
bsalomon17168df2014-12-09 09:00:49 -080033enum GrShaderType {
34 kVertex_GrShaderType,
35 kGeometry_GrShaderType,
36 kFragment_GrShaderType,
37
38 kLastkFragment_GrShaderType = kFragment_GrShaderType
39};
40static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1;
41
bsalomon@google.com31ec7982013-03-27 18:14:57 +000042/**
bsalomonc0bd6482014-12-09 10:04:14 -080043 * Precisions of shader language variables. Not all shading languages support precisions or actually
bsalomon422f56f2014-12-09 10:18:12 -080044 * vary the internal precision based on the qualifiers. These currently only apply to float types (
45 * including float vectors and matrices).
bsalomonc0bd6482014-12-09 10:04:14 -080046 */
47enum GrSLPrecision {
48 kLow_GrSLPrecision,
49 kMedium_GrSLPrecision,
50 kHigh_GrSLPrecision,
51
52 // Default precision is medium. This is because on OpenGL ES 2 highp support is not
53 // guaranteed. On (non-ES) OpenGL the specifiers have no effect on precision.
54 kDefault_GrSLPrecision = kMedium_GrSLPrecision,
55
56 kLast_GrSLPrecision = kHigh_GrSLPrecision
57};
58
59static const int kGrSLPrecisionCount = kLast_GrSLPrecision + 1;
60
61/**
jvanverth@google.com054ae992013-04-01 20:06:51 +000062 * Gets the vector size of the SLType. Returns -1 for void, matrices, and samplers.
63 */
64static inline int GrSLTypeVectorCount(GrSLType type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000065 SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
jvanverth@google.com054ae992013-04-01 20:06:51 +000066 static const int kCounts[] = { -1, 1, 2, 3, 4, -1, -1, -1 };
67 return kCounts[type];
68
69 GR_STATIC_ASSERT(0 == kVoid_GrSLType);
70 GR_STATIC_ASSERT(1 == kFloat_GrSLType);
71 GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
72 GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
73 GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
74 GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
75 GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
76 GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000077 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrSLTypeCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +000078}
79
bsalomon@google.com018f1792013-04-18 19:36:09 +000080/** Return the type enum for a vector of floats of length n (1..4),
81 e.g. 1 -> kFloat_GrSLType, 2 -> kVec2_GrSLType, ... */
jvanverth@google.com054ae992013-04-01 20:06:51 +000082static inline GrSLType GrSLFloatVectorType(int count) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000083 SkASSERT(count > 0 && count <= 4);
jvanverth@google.com054ae992013-04-01 20:06:51 +000084 return (GrSLType)(count);
85
86 GR_STATIC_ASSERT(kFloat_GrSLType == 1);
87 GR_STATIC_ASSERT(kVec2f_GrSLType == 2);
88 GR_STATIC_ASSERT(kVec3f_GrSLType == 3);
89 GR_STATIC_ASSERT(kVec4f_GrSLType == 4);
90}
91
bsalomon422f56f2014-12-09 10:18:12 -080092/** Is the shading language type floating point (or vector/matrix of fp)? */
93static inline bool GrSLTypeIsFloatType(GrSLType type) {
94 SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
95 return type >= 1 && type <= 6;
96
97 GR_STATIC_ASSERT(0 == kVoid_GrSLType);
98 GR_STATIC_ASSERT(1 == kFloat_GrSLType);
99 GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
100 GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
101 GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
102 GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
103 GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
104 GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
105 GR_STATIC_ASSERT(8 == kGrSLTypeCount);
106}
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000107//////////////////////////////////////////////////////////////////////////////
108
jvanverth@google.com054ae992013-04-01 20:06:51 +0000109/**
110 * Types used to describe format of vertices in arrays.
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000111 */
112enum GrVertexAttribType {
113 kFloat_GrVertexAttribType = 0,
114 kVec2f_GrVertexAttribType,
115 kVec3f_GrVertexAttribType,
116 kVec4f_GrVertexAttribType,
egdaniel37b4d862014-11-03 10:07:07 -0800117
118 kUByte_GrVertexAttribType, // unsigned byte, e.g. coverage
jvanverth5a105ff2015-02-18 11:36:35 -0800119 kVec4ub_GrVertexAttribType, // vector of 4 unsigned bytes, e.g. colors
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000120
jvanverth5a105ff2015-02-18 11:36:35 -0800121 kVec2s_GrVertexAttribType, // vector of 2 shorts, e.g. texture coordinates
122
123 kLast_GrVertexAttribType = kVec2s_GrVertexAttribType
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000124};
125static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
126
jvanverth@google.com054ae992013-04-01 20:06:51 +0000127/**
128 * Returns the vector size of the type.
129 */
130static inline int GrVertexAttribTypeVectorCount(GrVertexAttribType type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000131 SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
jvanverth5a105ff2015-02-18 11:36:35 -0800132 static const int kCounts[] = { 1, 2, 3, 4, 1, 4, 2 };
jvanverth@google.com054ae992013-04-01 20:06:51 +0000133 return kCounts[type];
134
135 GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
136 GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
137 GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
138 GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
egdaniel37b4d862014-11-03 10:07:07 -0800139 GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
140 GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
jvanverth5a105ff2015-02-18 11:36:35 -0800141 GR_STATIC_ASSERT(6 == kVec2s_GrVertexAttribType);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000142 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrVertexAttribTypeCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000143}
144
145/**
146 * Returns the size of the attrib type in bytes.
147 */
148static inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000149 SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000150 static const size_t kSizes[] = {
151 sizeof(float), // kFloat_GrVertexAttribType
152 2*sizeof(float), // kVec2f_GrVertexAttribType
153 3*sizeof(float), // kVec3f_GrVertexAttribType
154 4*sizeof(float), // kVec4f_GrVertexAttribType
egdaniel37b4d862014-11-03 10:07:07 -0800155 1*sizeof(char), // kUByte_GrVertexAttribType
jvanverth5a105ff2015-02-18 11:36:35 -0800156 4*sizeof(char), // kVec4ub_GrVertexAttribType
157 2*sizeof(int16_t) // kVec2s_GrVertexAttribType
jvanverth@google.com054ae992013-04-01 20:06:51 +0000158 };
159 return kSizes[type];
160
161 GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
162 GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
163 GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
164 GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
egdaniel37b4d862014-11-03 10:07:07 -0800165 GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
166 GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
jvanverth5a105ff2015-02-18 11:36:35 -0800167 GR_STATIC_ASSERT(6 == kVec2s_GrVertexAttribType);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000168 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kSizes) == kGrVertexAttribTypeCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000169}
170
171/**
joshualitt2dd1ae02014-12-03 06:24:10 -0800172 * converts a GrVertexAttribType to a GrSLType
jvanverth@google.com054ae992013-04-01 20:06:51 +0000173 */
joshualitt2dd1ae02014-12-03 06:24:10 -0800174static inline GrSLType GrVertexAttribTypeToSLType(GrVertexAttribType type) {
175 switch (type) {
176 default:
177 SkFAIL("Unsupported type conversion");
178 case kUByte_GrVertexAttribType:
179 case kFloat_GrVertexAttribType:
180 return kFloat_GrSLType;
jvanverth5a105ff2015-02-18 11:36:35 -0800181 case kVec2s_GrVertexAttribType:
joshualitt2dd1ae02014-12-03 06:24:10 -0800182 case kVec2f_GrVertexAttribType:
183 return kVec2f_GrSLType;
184 case kVec3f_GrVertexAttribType:
185 return kVec3f_GrSLType;
186 case kVec4ub_GrVertexAttribType:
187 case kVec4f_GrVertexAttribType:
188 return kVec4f_GrSLType;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000189 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800190}
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000191
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000192//////////////////////////////////////////////////////////////////////////////
193
194/**
195* We have coverage effects that clip rendering to the edge of some geometric primitive.
skia.committer@gmail.com06acb582014-03-06 03:02:32 +0000196* This enum specifies how that clipping is performed. Not all factories that take a
joshualittb0a8a372014-09-23 09:50:21 -0700197* GrProcessorEdgeType will succeed with all values and it is up to the caller to check for
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000198* a NULL return.
199*/
joshualittb0a8a372014-09-23 09:50:21 -0700200enum GrPrimitiveEdgeType {
201 kFillBW_GrProcessorEdgeType,
202 kFillAA_GrProcessorEdgeType,
203 kInverseFillBW_GrProcessorEdgeType,
204 kInverseFillAA_GrProcessorEdgeType,
205 kHairlineAA_GrProcessorEdgeType,
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000206
joshualittb0a8a372014-09-23 09:50:21 -0700207 kLast_GrProcessorEdgeType = kHairlineAA_GrProcessorEdgeType
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000208};
209
joshualittb0a8a372014-09-23 09:50:21 -0700210static const int kGrProcessorEdgeTypeCnt = kLast_GrProcessorEdgeType + 1;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000211
joshualittb0a8a372014-09-23 09:50:21 -0700212static inline bool GrProcessorEdgeTypeIsFill(const GrPrimitiveEdgeType edgeType) {
213 return (kFillAA_GrProcessorEdgeType == edgeType || kFillBW_GrProcessorEdgeType == edgeType);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000214}
215
joshualittb0a8a372014-09-23 09:50:21 -0700216static inline bool GrProcessorEdgeTypeIsInverseFill(const GrPrimitiveEdgeType edgeType) {
217 return (kInverseFillAA_GrProcessorEdgeType == edgeType ||
218 kInverseFillBW_GrProcessorEdgeType == edgeType);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000219}
220
joshualittb0a8a372014-09-23 09:50:21 -0700221static inline bool GrProcessorEdgeTypeIsAA(const GrPrimitiveEdgeType edgeType) {
222 return (kFillBW_GrProcessorEdgeType != edgeType && kInverseFillBW_GrProcessorEdgeType != edgeType);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000223}
224
joshualittb0a8a372014-09-23 09:50:21 -0700225static inline GrPrimitiveEdgeType GrInvertProcessorEdgeType(const GrPrimitiveEdgeType edgeType) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000226 switch (edgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700227 case kFillBW_GrProcessorEdgeType:
228 return kInverseFillBW_GrProcessorEdgeType;
229 case kFillAA_GrProcessorEdgeType:
230 return kInverseFillAA_GrProcessorEdgeType;
231 case kInverseFillBW_GrProcessorEdgeType:
232 return kFillBW_GrProcessorEdgeType;
233 case kInverseFillAA_GrProcessorEdgeType:
234 return kFillAA_GrProcessorEdgeType;
235 case kHairlineAA_GrProcessorEdgeType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000236 SkFAIL("Hairline fill isn't invertible.");
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000237 }
joshualittb0a8a372014-09-23 09:50:21 -0700238 return kFillAA_GrProcessorEdgeType; // suppress warning.
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000239}
240
bsalomonbcf0a522014-10-08 08:40:09 -0700241/**
242 * Indicates the type of pending IO operations that can be recorded for gpu resources.
243 */
244enum GrIOType {
245 kRead_GrIOType,
246 kWrite_GrIOType,
247 kRW_GrIOType
248};
249
bsalomon3e791242014-12-17 13:43:13 -0800250struct GrScissorState {
251 GrScissorState() : fEnabled(false) {}
252 void set(const SkIRect& rect) { fRect = rect; fEnabled = true; }
253 bool operator==(const GrScissorState& other) const {
254 return fEnabled == other.fEnabled &&
255 (false == fEnabled || fRect == other.fRect);
256 }
257 bool operator!=(const GrScissorState& other) const { return !(*this == other); }
robertphillipse85a32d2015-02-10 08:16:55 -0800258
259 bool enabled() const { return fEnabled; }
260 const SkIRect& rect() const { return fRect; }
261
262private:
bsalomon3e791242014-12-17 13:43:13 -0800263 bool fEnabled;
264 SkIRect fRect;
265};
266
joshualitt7d022d62015-05-12 12:03:50 -0700267#ifdef SK_DEBUG
268// Takes a pointer to a GrContext, and will suppress prints if required
269#define GrContextDebugf(context, ...) \
270 if (!context->suppressPrints()) { \
271 SkDebugf(__VA_ARGS__); \
272 }
273#else
274#define GrContextDebugf(context, ...)
275#endif
276
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000277#endif