blob: c46e25bd0d4410bfb11dcf4a4743072a766e84d7 [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
bsalomoncdee0092016-01-08 13:20:12 -080015 /**
16 * Types of shader-language-specific boxed variables we can create. (Currently only GrGLShaderVars,
17 * but should be applicable to other shader languages.)
18 */
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000019enum 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,
bsalomon7ea33f52015-11-22 14:51:00 -080028 kSamplerExternal_GrSLType,
bsalomone5286e02016-01-14 09:24:09 -080029 kSampler2DRect_GrSLType,
jvanverth@google.com054ae992013-04-01 20:06:51 +000030
bsalomone5286e02016-01-14 09:24:09 -080031 kLast_GrSLType = kSampler2DRect_GrSLType
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000032};
jvanverth@google.com054ae992013-04-01 20:06:51 +000033static const int kGrSLTypeCount = kLast_GrSLType + 1;
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000034
bsalomon17168df2014-12-09 09:00:49 -080035enum GrShaderType {
36 kVertex_GrShaderType,
37 kGeometry_GrShaderType,
38 kFragment_GrShaderType,
39
40 kLastkFragment_GrShaderType = kFragment_GrShaderType
41};
42static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1;
43
bsalomon@google.com31ec7982013-03-27 18:14:57 +000044/**
bsalomonc0bd6482014-12-09 10:04:14 -080045 * Precisions of shader language variables. Not all shading languages support precisions or actually
bsalomon422f56f2014-12-09 10:18:12 -080046 * vary the internal precision based on the qualifiers. These currently only apply to float types (
47 * including float vectors and matrices).
bsalomonc0bd6482014-12-09 10:04:14 -080048 */
49enum GrSLPrecision {
50 kLow_GrSLPrecision,
51 kMedium_GrSLPrecision,
52 kHigh_GrSLPrecision,
53
54 // Default precision is medium. This is because on OpenGL ES 2 highp support is not
55 // guaranteed. On (non-ES) OpenGL the specifiers have no effect on precision.
56 kDefault_GrSLPrecision = kMedium_GrSLPrecision,
57
58 kLast_GrSLPrecision = kHigh_GrSLPrecision
59};
60
61static const int kGrSLPrecisionCount = kLast_GrSLPrecision + 1;
62
63/**
jvanverth@google.com054ae992013-04-01 20:06:51 +000064 * Gets the vector size of the SLType. Returns -1 for void, matrices, and samplers.
65 */
66static inline int GrSLTypeVectorCount(GrSLType type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000067 SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
bsalomone5286e02016-01-14 09:24:09 -080068 static const int kCounts[] = { -1, 1, 2, 3, 4, -1, -1, -1, -1, -1 };
jvanverth@google.com054ae992013-04-01 20:06:51 +000069 return kCounts[type];
70
71 GR_STATIC_ASSERT(0 == kVoid_GrSLType);
72 GR_STATIC_ASSERT(1 == kFloat_GrSLType);
73 GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
74 GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
75 GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
76 GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
77 GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
78 GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
bsalomon7ea33f52015-11-22 14:51:00 -080079 GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType);
bsalomone5286e02016-01-14 09:24:09 -080080 GR_STATIC_ASSERT(9 == kSampler2DRect_GrSLType);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000081 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrSLTypeCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +000082}
83
bsalomon@google.com018f1792013-04-18 19:36:09 +000084/** Return the type enum for a vector of floats of length n (1..4),
85 e.g. 1 -> kFloat_GrSLType, 2 -> kVec2_GrSLType, ... */
jvanverth@google.com054ae992013-04-01 20:06:51 +000086static inline GrSLType GrSLFloatVectorType(int count) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000087 SkASSERT(count > 0 && count <= 4);
jvanverth@google.com054ae992013-04-01 20:06:51 +000088 return (GrSLType)(count);
89
90 GR_STATIC_ASSERT(kFloat_GrSLType == 1);
91 GR_STATIC_ASSERT(kVec2f_GrSLType == 2);
92 GR_STATIC_ASSERT(kVec3f_GrSLType == 3);
93 GR_STATIC_ASSERT(kVec4f_GrSLType == 4);
94}
95
bsalomon422f56f2014-12-09 10:18:12 -080096/** Is the shading language type floating point (or vector/matrix of fp)? */
97static inline bool GrSLTypeIsFloatType(GrSLType type) {
98 SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
99 return type >= 1 && type <= 6;
100
101 GR_STATIC_ASSERT(0 == kVoid_GrSLType);
102 GR_STATIC_ASSERT(1 == kFloat_GrSLType);
103 GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
104 GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
105 GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
106 GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
107 GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
108 GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
bsalomon7ea33f52015-11-22 14:51:00 -0800109 GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType);
bsalomone5286e02016-01-14 09:24:09 -0800110 GR_STATIC_ASSERT(9 == kSampler2DRect_GrSLType);
111 GR_STATIC_ASSERT(10 == kGrSLTypeCount);
bsalomon422f56f2014-12-09 10:18:12 -0800112}
egdanielfa896322016-01-13 12:19:30 -0800113
114/** Returns the size in bytes for floating point GrSLTypes. For non floating point type returns 0 */
115static inline size_t GrSLTypeSize(GrSLType type) {
116 SkASSERT(GrSLTypeIsFloatType(type));
117 static const size_t kSizes[] = {
118 0, // kVoid_GrSLType
119 sizeof(float), // kFloat_GrSLType
120 2 * sizeof(float), // kVec2f_GrSLType
121 3 * sizeof(float), // kVec3f_GrSLType
122 4 * sizeof(float), // kVec4f_GrSLType
123 9 * sizeof(float), // kMat33f_GrSLType
124 16 * sizeof(float), // kMat44f_GrSLType
125 0, // kSampler2D_GrSLType
bsalomone5286e02016-01-14 09:24:09 -0800126 0, // kSamplerExternal_GrSLType
127 0 // kSampler2DRect_GrSLType
egdanielfa896322016-01-13 12:19:30 -0800128 };
129 return kSizes[type];
130
131 GR_STATIC_ASSERT(0 == kVoid_GrSLType);
132 GR_STATIC_ASSERT(1 == kFloat_GrSLType);
133 GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
134 GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
135 GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
136 GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
137 GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
138 GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
139 GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType);
bsalomone5286e02016-01-14 09:24:09 -0800140 GR_STATIC_ASSERT(9 == kSampler2DRect_GrSLType);
141 GR_STATIC_ASSERT(10 == kGrSLTypeCount);
142}
143
144static inline bool GrSLTypeIsSamplerType(GrSLType type) {
145 SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
146 return type >= 7 && type <= 9;
147
148 GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
149 GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType);
150 GR_STATIC_ASSERT(9 == kSampler2DRect_GrSLType);
egdanielfa896322016-01-13 12:19:30 -0800151}
152
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000153//////////////////////////////////////////////////////////////////////////////
154
jvanverth@google.com054ae992013-04-01 20:06:51 +0000155/**
156 * Types used to describe format of vertices in arrays.
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000157 */
158enum GrVertexAttribType {
159 kFloat_GrVertexAttribType = 0,
160 kVec2f_GrVertexAttribType,
161 kVec3f_GrVertexAttribType,
162 kVec4f_GrVertexAttribType,
egdaniel37b4d862014-11-03 10:07:07 -0800163
164 kUByte_GrVertexAttribType, // unsigned byte, e.g. coverage
jvanverth5a105ff2015-02-18 11:36:35 -0800165 kVec4ub_GrVertexAttribType, // vector of 4 unsigned bytes, e.g. colors
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000166
jvanverth5a105ff2015-02-18 11:36:35 -0800167 kVec2s_GrVertexAttribType, // vector of 2 shorts, e.g. texture coordinates
168
169 kLast_GrVertexAttribType = kVec2s_GrVertexAttribType
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000170};
171static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
172
jvanverth@google.com054ae992013-04-01 20:06:51 +0000173/**
174 * Returns the vector size of the type.
175 */
176static inline int GrVertexAttribTypeVectorCount(GrVertexAttribType type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000177 SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
jvanverth5a105ff2015-02-18 11:36:35 -0800178 static const int kCounts[] = { 1, 2, 3, 4, 1, 4, 2 };
jvanverth@google.com054ae992013-04-01 20:06:51 +0000179 return kCounts[type];
180
181 GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
182 GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
183 GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
184 GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
egdaniel37b4d862014-11-03 10:07:07 -0800185 GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
186 GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
jvanverth5a105ff2015-02-18 11:36:35 -0800187 GR_STATIC_ASSERT(6 == kVec2s_GrVertexAttribType);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000188 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrVertexAttribTypeCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000189}
190
191/**
192 * Returns the size of the attrib type in bytes.
193 */
194static inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000195 SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000196 static const size_t kSizes[] = {
197 sizeof(float), // kFloat_GrVertexAttribType
198 2*sizeof(float), // kVec2f_GrVertexAttribType
199 3*sizeof(float), // kVec3f_GrVertexAttribType
200 4*sizeof(float), // kVec4f_GrVertexAttribType
egdaniel37b4d862014-11-03 10:07:07 -0800201 1*sizeof(char), // kUByte_GrVertexAttribType
jvanverth5a105ff2015-02-18 11:36:35 -0800202 4*sizeof(char), // kVec4ub_GrVertexAttribType
203 2*sizeof(int16_t) // kVec2s_GrVertexAttribType
jvanverth@google.com054ae992013-04-01 20:06:51 +0000204 };
205 return kSizes[type];
206
207 GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
208 GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
209 GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
210 GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
egdaniel37b4d862014-11-03 10:07:07 -0800211 GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
212 GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
jvanverth5a105ff2015-02-18 11:36:35 -0800213 GR_STATIC_ASSERT(6 == kVec2s_GrVertexAttribType);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000214 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kSizes) == kGrVertexAttribTypeCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000215}
216
217/**
joshualitt2dd1ae02014-12-03 06:24:10 -0800218 * converts a GrVertexAttribType to a GrSLType
jvanverth@google.com054ae992013-04-01 20:06:51 +0000219 */
joshualitt2dd1ae02014-12-03 06:24:10 -0800220static inline GrSLType GrVertexAttribTypeToSLType(GrVertexAttribType type) {
221 switch (type) {
222 default:
223 SkFAIL("Unsupported type conversion");
tzikbc7d2352016-01-05 00:35:50 -0800224 return kVoid_GrSLType;
joshualitt2dd1ae02014-12-03 06:24:10 -0800225 case kUByte_GrVertexAttribType:
226 case kFloat_GrVertexAttribType:
227 return kFloat_GrSLType;
jvanverth5a105ff2015-02-18 11:36:35 -0800228 case kVec2s_GrVertexAttribType:
joshualitt2dd1ae02014-12-03 06:24:10 -0800229 case kVec2f_GrVertexAttribType:
230 return kVec2f_GrSLType;
231 case kVec3f_GrVertexAttribType:
232 return kVec3f_GrSLType;
233 case kVec4ub_GrVertexAttribType:
234 case kVec4f_GrVertexAttribType:
235 return kVec4f_GrSLType;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000236 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800237}
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000238
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000239//////////////////////////////////////////////////////////////////////////////
240
241/**
242* We have coverage effects that clip rendering to the edge of some geometric primitive.
skia.committer@gmail.com06acb582014-03-06 03:02:32 +0000243* This enum specifies how that clipping is performed. Not all factories that take a
joshualittb0a8a372014-09-23 09:50:21 -0700244* 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 +0000245* a NULL return.
246*/
joshualittb0a8a372014-09-23 09:50:21 -0700247enum GrPrimitiveEdgeType {
248 kFillBW_GrProcessorEdgeType,
249 kFillAA_GrProcessorEdgeType,
250 kInverseFillBW_GrProcessorEdgeType,
251 kInverseFillAA_GrProcessorEdgeType,
252 kHairlineAA_GrProcessorEdgeType,
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000253
joshualittb0a8a372014-09-23 09:50:21 -0700254 kLast_GrProcessorEdgeType = kHairlineAA_GrProcessorEdgeType
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000255};
256
joshualittb0a8a372014-09-23 09:50:21 -0700257static const int kGrProcessorEdgeTypeCnt = kLast_GrProcessorEdgeType + 1;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000258
joshualittb0a8a372014-09-23 09:50:21 -0700259static inline bool GrProcessorEdgeTypeIsFill(const GrPrimitiveEdgeType edgeType) {
260 return (kFillAA_GrProcessorEdgeType == edgeType || kFillBW_GrProcessorEdgeType == edgeType);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000261}
262
joshualittb0a8a372014-09-23 09:50:21 -0700263static inline bool GrProcessorEdgeTypeIsInverseFill(const GrPrimitiveEdgeType edgeType) {
264 return (kInverseFillAA_GrProcessorEdgeType == edgeType ||
265 kInverseFillBW_GrProcessorEdgeType == edgeType);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000266}
267
joshualittb0a8a372014-09-23 09:50:21 -0700268static inline bool GrProcessorEdgeTypeIsAA(const GrPrimitiveEdgeType edgeType) {
269 return (kFillBW_GrProcessorEdgeType != edgeType && kInverseFillBW_GrProcessorEdgeType != edgeType);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000270}
271
joshualittb0a8a372014-09-23 09:50:21 -0700272static inline GrPrimitiveEdgeType GrInvertProcessorEdgeType(const GrPrimitiveEdgeType edgeType) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000273 switch (edgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700274 case kFillBW_GrProcessorEdgeType:
275 return kInverseFillBW_GrProcessorEdgeType;
276 case kFillAA_GrProcessorEdgeType:
277 return kInverseFillAA_GrProcessorEdgeType;
278 case kInverseFillBW_GrProcessorEdgeType:
279 return kFillBW_GrProcessorEdgeType;
280 case kInverseFillAA_GrProcessorEdgeType:
281 return kFillAA_GrProcessorEdgeType;
282 case kHairlineAA_GrProcessorEdgeType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000283 SkFAIL("Hairline fill isn't invertible.");
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000284 }
joshualittb0a8a372014-09-23 09:50:21 -0700285 return kFillAA_GrProcessorEdgeType; // suppress warning.
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000286}
287
bsalomonbcf0a522014-10-08 08:40:09 -0700288/**
289 * Indicates the type of pending IO operations that can be recorded for gpu resources.
290 */
291enum GrIOType {
292 kRead_GrIOType,
293 kWrite_GrIOType,
294 kRW_GrIOType
295};
296
bsalomon3e791242014-12-17 13:43:13 -0800297struct GrScissorState {
298 GrScissorState() : fEnabled(false) {}
299 void set(const SkIRect& rect) { fRect = rect; fEnabled = true; }
300 bool operator==(const GrScissorState& other) const {
301 return fEnabled == other.fEnabled &&
302 (false == fEnabled || fRect == other.fRect);
303 }
304 bool operator!=(const GrScissorState& other) const { return !(*this == other); }
robertphillipse85a32d2015-02-10 08:16:55 -0800305
306 bool enabled() const { return fEnabled; }
307 const SkIRect& rect() const { return fRect; }
308
309private:
bsalomon3e791242014-12-17 13:43:13 -0800310 bool fEnabled;
311 SkIRect fRect;
312};
313
jvanverth17aa0472016-01-05 10:41:27 -0800314/**
315 * Indicates the transfer direction for a transfer buffer
316 */
317enum TransferType {
318 /** Caller intends to use the buffer to transfer data to the GPU */
319 kCpuToGpu_TransferType,
320 /** Caller intends to use the buffer to transfer data from the GPU */
321 kGpuToCpu_TransferType
322};
323
324
joshualitt7d022d62015-05-12 12:03:50 -0700325#ifdef SK_DEBUG
bsalomon682c2692015-05-22 14:01:46 -0700326// Takes a pointer to a GrCaps, and will suppress prints if required
327#define GrCapsDebugf(caps, ...) \
328 if (!caps->suppressPrints()) { \
329 SkDebugf(__VA_ARGS__); \
joshualitt7d022d62015-05-12 12:03:50 -0700330 }
331#else
bsalomon682c2692015-05-22 14:01:46 -0700332#define GrCapsDebugf(caps, ...)
joshualitt7d022d62015-05-12 12:03:50 -0700333#endif
334
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000335#endif