blob: 6135c14c12198232b67e41dbc7ea63545a278634 [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,
bsalomon7ea33f52015-11-22 14:51:00 -080028 kSamplerExternal_GrSLType,
jvanverth@google.com054ae992013-04-01 20:06:51 +000029
bsalomon7ea33f52015-11-22 14:51:00 -080030 kLast_GrSLType = kSamplerExternal_GrSLType
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000031};
jvanverth@google.com054ae992013-04-01 20:06:51 +000032static const int kGrSLTypeCount = kLast_GrSLType + 1;
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000033
bsalomon17168df2014-12-09 09:00:49 -080034enum GrShaderType {
35 kVertex_GrShaderType,
36 kGeometry_GrShaderType,
37 kFragment_GrShaderType,
38
39 kLastkFragment_GrShaderType = kFragment_GrShaderType
40};
41static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1;
42
bsalomon@google.com31ec7982013-03-27 18:14:57 +000043/**
bsalomonc0bd6482014-12-09 10:04:14 -080044 * Precisions of shader language variables. Not all shading languages support precisions or actually
bsalomon422f56f2014-12-09 10:18:12 -080045 * vary the internal precision based on the qualifiers. These currently only apply to float types (
46 * including float vectors and matrices).
bsalomonc0bd6482014-12-09 10:04:14 -080047 */
48enum GrSLPrecision {
49 kLow_GrSLPrecision,
50 kMedium_GrSLPrecision,
51 kHigh_GrSLPrecision,
52
53 // Default precision is medium. This is because on OpenGL ES 2 highp support is not
54 // guaranteed. On (non-ES) OpenGL the specifiers have no effect on precision.
55 kDefault_GrSLPrecision = kMedium_GrSLPrecision,
56
57 kLast_GrSLPrecision = kHigh_GrSLPrecision
58};
59
60static const int kGrSLPrecisionCount = kLast_GrSLPrecision + 1;
61
62/**
jvanverth@google.com054ae992013-04-01 20:06:51 +000063 * Gets the vector size of the SLType. Returns -1 for void, matrices, and samplers.
64 */
65static inline int GrSLTypeVectorCount(GrSLType type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000066 SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
bsalomon7ea33f52015-11-22 14:51:00 -080067 static const int kCounts[] = { -1, 1, 2, 3, 4, -1, -1, -1, -1 };
jvanverth@google.com054ae992013-04-01 20:06:51 +000068 return kCounts[type];
69
70 GR_STATIC_ASSERT(0 == kVoid_GrSLType);
71 GR_STATIC_ASSERT(1 == kFloat_GrSLType);
72 GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
73 GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
74 GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
75 GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
76 GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
77 GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
bsalomon7ea33f52015-11-22 14:51:00 -080078 GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000079 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrSLTypeCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +000080}
81
bsalomon@google.com018f1792013-04-18 19:36:09 +000082/** Return the type enum for a vector of floats of length n (1..4),
83 e.g. 1 -> kFloat_GrSLType, 2 -> kVec2_GrSLType, ... */
jvanverth@google.com054ae992013-04-01 20:06:51 +000084static inline GrSLType GrSLFloatVectorType(int count) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000085 SkASSERT(count > 0 && count <= 4);
jvanverth@google.com054ae992013-04-01 20:06:51 +000086 return (GrSLType)(count);
87
88 GR_STATIC_ASSERT(kFloat_GrSLType == 1);
89 GR_STATIC_ASSERT(kVec2f_GrSLType == 2);
90 GR_STATIC_ASSERT(kVec3f_GrSLType == 3);
91 GR_STATIC_ASSERT(kVec4f_GrSLType == 4);
92}
93
bsalomon422f56f2014-12-09 10:18:12 -080094/** Is the shading language type floating point (or vector/matrix of fp)? */
95static inline bool GrSLTypeIsFloatType(GrSLType type) {
96 SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
97 return type >= 1 && type <= 6;
98
99 GR_STATIC_ASSERT(0 == kVoid_GrSLType);
100 GR_STATIC_ASSERT(1 == kFloat_GrSLType);
101 GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
102 GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
103 GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
104 GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
105 GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
106 GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
bsalomon7ea33f52015-11-22 14:51:00 -0800107 GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType);
108 GR_STATIC_ASSERT(9 == kGrSLTypeCount);
bsalomon422f56f2014-12-09 10:18:12 -0800109}
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000110//////////////////////////////////////////////////////////////////////////////
111
jvanverth@google.com054ae992013-04-01 20:06:51 +0000112/**
113 * Types used to describe format of vertices in arrays.
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000114 */
115enum GrVertexAttribType {
116 kFloat_GrVertexAttribType = 0,
117 kVec2f_GrVertexAttribType,
118 kVec3f_GrVertexAttribType,
119 kVec4f_GrVertexAttribType,
egdaniel37b4d862014-11-03 10:07:07 -0800120
121 kUByte_GrVertexAttribType, // unsigned byte, e.g. coverage
jvanverth5a105ff2015-02-18 11:36:35 -0800122 kVec4ub_GrVertexAttribType, // vector of 4 unsigned bytes, e.g. colors
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000123
jvanverth5a105ff2015-02-18 11:36:35 -0800124 kVec2s_GrVertexAttribType, // vector of 2 shorts, e.g. texture coordinates
125
126 kLast_GrVertexAttribType = kVec2s_GrVertexAttribType
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000127};
128static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
129
jvanverth@google.com054ae992013-04-01 20:06:51 +0000130/**
131 * Returns the vector size of the type.
132 */
133static inline int GrVertexAttribTypeVectorCount(GrVertexAttribType type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000134 SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
jvanverth5a105ff2015-02-18 11:36:35 -0800135 static const int kCounts[] = { 1, 2, 3, 4, 1, 4, 2 };
jvanverth@google.com054ae992013-04-01 20:06:51 +0000136 return kCounts[type];
137
138 GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
139 GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
140 GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
141 GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
egdaniel37b4d862014-11-03 10:07:07 -0800142 GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
143 GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
jvanverth5a105ff2015-02-18 11:36:35 -0800144 GR_STATIC_ASSERT(6 == kVec2s_GrVertexAttribType);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000145 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrVertexAttribTypeCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000146}
147
148/**
149 * Returns the size of the attrib type in bytes.
150 */
151static inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000152 SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000153 static const size_t kSizes[] = {
154 sizeof(float), // kFloat_GrVertexAttribType
155 2*sizeof(float), // kVec2f_GrVertexAttribType
156 3*sizeof(float), // kVec3f_GrVertexAttribType
157 4*sizeof(float), // kVec4f_GrVertexAttribType
egdaniel37b4d862014-11-03 10:07:07 -0800158 1*sizeof(char), // kUByte_GrVertexAttribType
jvanverth5a105ff2015-02-18 11:36:35 -0800159 4*sizeof(char), // kVec4ub_GrVertexAttribType
160 2*sizeof(int16_t) // kVec2s_GrVertexAttribType
jvanverth@google.com054ae992013-04-01 20:06:51 +0000161 };
162 return kSizes[type];
163
164 GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
165 GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
166 GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
167 GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
egdaniel37b4d862014-11-03 10:07:07 -0800168 GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
169 GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
jvanverth5a105ff2015-02-18 11:36:35 -0800170 GR_STATIC_ASSERT(6 == kVec2s_GrVertexAttribType);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000171 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kSizes) == kGrVertexAttribTypeCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000172}
173
174/**
joshualitt2dd1ae02014-12-03 06:24:10 -0800175 * converts a GrVertexAttribType to a GrSLType
jvanverth@google.com054ae992013-04-01 20:06:51 +0000176 */
joshualitt2dd1ae02014-12-03 06:24:10 -0800177static inline GrSLType GrVertexAttribTypeToSLType(GrVertexAttribType type) {
178 switch (type) {
179 default:
180 SkFAIL("Unsupported type conversion");
181 case kUByte_GrVertexAttribType:
182 case kFloat_GrVertexAttribType:
183 return kFloat_GrSLType;
jvanverth5a105ff2015-02-18 11:36:35 -0800184 case kVec2s_GrVertexAttribType:
joshualitt2dd1ae02014-12-03 06:24:10 -0800185 case kVec2f_GrVertexAttribType:
186 return kVec2f_GrSLType;
187 case kVec3f_GrVertexAttribType:
188 return kVec3f_GrSLType;
189 case kVec4ub_GrVertexAttribType:
190 case kVec4f_GrVertexAttribType:
191 return kVec4f_GrSLType;
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000192 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800193}
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000194
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000195//////////////////////////////////////////////////////////////////////////////
196
197/**
198* We have coverage effects that clip rendering to the edge of some geometric primitive.
skia.committer@gmail.com06acb582014-03-06 03:02:32 +0000199* This enum specifies how that clipping is performed. Not all factories that take a
joshualittb0a8a372014-09-23 09:50:21 -0700200* 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 +0000201* a NULL return.
202*/
joshualittb0a8a372014-09-23 09:50:21 -0700203enum GrPrimitiveEdgeType {
204 kFillBW_GrProcessorEdgeType,
205 kFillAA_GrProcessorEdgeType,
206 kInverseFillBW_GrProcessorEdgeType,
207 kInverseFillAA_GrProcessorEdgeType,
208 kHairlineAA_GrProcessorEdgeType,
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000209
joshualittb0a8a372014-09-23 09:50:21 -0700210 kLast_GrProcessorEdgeType = kHairlineAA_GrProcessorEdgeType
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000211};
212
joshualittb0a8a372014-09-23 09:50:21 -0700213static const int kGrProcessorEdgeTypeCnt = kLast_GrProcessorEdgeType + 1;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000214
joshualittb0a8a372014-09-23 09:50:21 -0700215static inline bool GrProcessorEdgeTypeIsFill(const GrPrimitiveEdgeType edgeType) {
216 return (kFillAA_GrProcessorEdgeType == edgeType || kFillBW_GrProcessorEdgeType == edgeType);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000217}
218
joshualittb0a8a372014-09-23 09:50:21 -0700219static inline bool GrProcessorEdgeTypeIsInverseFill(const GrPrimitiveEdgeType edgeType) {
220 return (kInverseFillAA_GrProcessorEdgeType == edgeType ||
221 kInverseFillBW_GrProcessorEdgeType == edgeType);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000222}
223
joshualittb0a8a372014-09-23 09:50:21 -0700224static inline bool GrProcessorEdgeTypeIsAA(const GrPrimitiveEdgeType edgeType) {
225 return (kFillBW_GrProcessorEdgeType != edgeType && kInverseFillBW_GrProcessorEdgeType != edgeType);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000226}
227
joshualittb0a8a372014-09-23 09:50:21 -0700228static inline GrPrimitiveEdgeType GrInvertProcessorEdgeType(const GrPrimitiveEdgeType edgeType) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000229 switch (edgeType) {
joshualittb0a8a372014-09-23 09:50:21 -0700230 case kFillBW_GrProcessorEdgeType:
231 return kInverseFillBW_GrProcessorEdgeType;
232 case kFillAA_GrProcessorEdgeType:
233 return kInverseFillAA_GrProcessorEdgeType;
234 case kInverseFillBW_GrProcessorEdgeType:
235 return kFillBW_GrProcessorEdgeType;
236 case kInverseFillAA_GrProcessorEdgeType:
237 return kFillAA_GrProcessorEdgeType;
238 case kHairlineAA_GrProcessorEdgeType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000239 SkFAIL("Hairline fill isn't invertible.");
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000240 }
joshualittb0a8a372014-09-23 09:50:21 -0700241 return kFillAA_GrProcessorEdgeType; // suppress warning.
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000242}
243
bsalomonbcf0a522014-10-08 08:40:09 -0700244/**
245 * Indicates the type of pending IO operations that can be recorded for gpu resources.
246 */
247enum GrIOType {
248 kRead_GrIOType,
249 kWrite_GrIOType,
250 kRW_GrIOType
251};
252
bsalomon3e791242014-12-17 13:43:13 -0800253struct GrScissorState {
254 GrScissorState() : fEnabled(false) {}
255 void set(const SkIRect& rect) { fRect = rect; fEnabled = true; }
256 bool operator==(const GrScissorState& other) const {
257 return fEnabled == other.fEnabled &&
258 (false == fEnabled || fRect == other.fRect);
259 }
260 bool operator!=(const GrScissorState& other) const { return !(*this == other); }
robertphillipse85a32d2015-02-10 08:16:55 -0800261
262 bool enabled() const { return fEnabled; }
263 const SkIRect& rect() const { return fRect; }
264
265private:
bsalomon3e791242014-12-17 13:43:13 -0800266 bool fEnabled;
267 SkIRect fRect;
268};
269
joshualitt7d022d62015-05-12 12:03:50 -0700270#ifdef SK_DEBUG
bsalomon682c2692015-05-22 14:01:46 -0700271// Takes a pointer to a GrCaps, and will suppress prints if required
272#define GrCapsDebugf(caps, ...) \
273 if (!caps->suppressPrints()) { \
274 SkDebugf(__VA_ARGS__); \
joshualitt7d022d62015-05-12 12:03:50 -0700275 }
276#else
bsalomon682c2692015-05-22 14:01:46 -0700277#define GrCapsDebugf(caps, ...)
joshualitt7d022d62015-05-12 12:03:50 -0700278#endif
279
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000280#endif