Revert "Allow FPs to elevate default precision for the entire fragment program"
[platform/upstream/libSkiaSharp.git] / include / gpu / GrTypesPriv.h
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
11 #include <chrono>
12 #include "GrTypes.h"
13 #include "SkRefCnt.h"
14
15 // The old libstdc++ uses the draft name "monotonic_clock" rather than "steady_clock". This might
16 // not actually be monotonic, depending on how libstdc++ was built. However, this is only currently
17 // used for idle resource purging so it shouldn't cause a correctness problem.
18 #if defined(__GLIBCXX__) && (__GLIBCXX__ < 20130000)
19 using GrStdSteadyClock = std::chrono::monotonic_clock;
20 #else
21 using GrStdSteadyClock = std::chrono::steady_clock;
22 #endif
23
24 /** This enum indicates the type of antialiasing to be performed. */
25 enum class GrAAType : unsigned {
26     /** No antialiasing */
27     kNone,
28     /** Use fragment shader code to compute a fractional pixel coverage. */
29     kCoverage,
30     /** Use normal MSAA. */
31     kMSAA,
32     /**
33      * Use "mixed samples" MSAA such that the stencil buffer is multisampled but the color buffer is
34      * not.
35      */
36     kMixedSamples
37 };
38
39 static inline bool GrAATypeIsHW(GrAAType type) {
40     switch (type) {
41         case GrAAType::kNone:
42             return false;
43         case GrAAType::kCoverage:
44             return false;
45         case GrAAType::kMSAA:
46             return true;
47         case GrAAType::kMixedSamples:
48             return true;
49     }
50     SkFAIL("Unknown AA Type");
51     return false;
52 }
53
54 /**
55  * Types of shader-language-specific boxed variables we can create. (Currently only GrGLShaderVars,
56  * but should be applicable to other shader languages.)
57  */
58 enum GrSLType {
59     kVoid_GrSLType,
60     kBool_GrSLType,
61     kInt_GrSLType,
62     kUint_GrSLType,
63     kFloat_GrSLType,
64     kVec2f_GrSLType,
65     kVec3f_GrSLType,
66     kVec4f_GrSLType,
67     kVec2i_GrSLType,
68     kVec3i_GrSLType,
69     kVec4i_GrSLType,
70     kMat22f_GrSLType,
71     kMat33f_GrSLType,
72     kMat44f_GrSLType,
73     kTexture2DSampler_GrSLType,
74     kITexture2DSampler_GrSLType,
75     kTextureExternalSampler_GrSLType,
76     kTexture2DRectSampler_GrSLType,
77     kBufferSampler_GrSLType,
78     kTexture2D_GrSLType,
79     kSampler_GrSLType,
80     kImageStorage2D_GrSLType,
81     kIImageStorage2D_GrSLType,
82 };
83
84 enum GrShaderType {
85     kVertex_GrShaderType,
86     kGeometry_GrShaderType,
87     kFragment_GrShaderType,
88
89     kLastkFragment_GrShaderType = kFragment_GrShaderType
90 };
91 static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1;
92
93 enum GrShaderFlags {
94     kNone_GrShaderFlags = 0,
95     kVertex_GrShaderFlag = 1 << kVertex_GrShaderType,
96     kGeometry_GrShaderFlag = 1 << kGeometry_GrShaderType,
97     kFragment_GrShaderFlag = 1 << kFragment_GrShaderType
98 };
99 GR_MAKE_BITFIELD_OPS(GrShaderFlags);
100
101 enum class GrDrawFace {
102     kInvalid = -1,
103
104     kBoth,
105     kCCW,
106     kCW,
107 };
108
109 /**
110  * Precisions of shader language variables. Not all shading languages support precisions or actually
111  * vary the internal precision based on the qualifiers. These currently only apply to float types (
112  * including float vectors and matrices).
113  */
114 enum GrSLPrecision {
115     kLow_GrSLPrecision,
116     kMedium_GrSLPrecision,
117     kHigh_GrSLPrecision,
118
119     // Default precision is medium. This is because on OpenGL ES 2 highp support is not
120     // guaranteed. On (non-ES) OpenGL the specifiers have no effect on precision.
121     kDefault_GrSLPrecision = kMedium_GrSLPrecision,
122
123     kLast_GrSLPrecision = kHigh_GrSLPrecision
124 };
125
126 static const int kGrSLPrecisionCount = kLast_GrSLPrecision + 1;
127
128 /** Is the shading language type float (including vectors/matrices)? */
129 static inline bool GrSLTypeIsFloatType(GrSLType type) {
130     switch (type) {
131         case kFloat_GrSLType:
132         case kVec2f_GrSLType:
133         case kVec3f_GrSLType:
134         case kVec4f_GrSLType:
135         case kMat22f_GrSLType:
136         case kMat33f_GrSLType:
137         case kMat44f_GrSLType:
138             return true;
139
140         case kVoid_GrSLType:
141         case kTexture2DSampler_GrSLType:
142         case kITexture2DSampler_GrSLType:
143         case kTextureExternalSampler_GrSLType:
144         case kTexture2DRectSampler_GrSLType:
145         case kBufferSampler_GrSLType:
146         case kBool_GrSLType:
147         case kInt_GrSLType:
148         case kUint_GrSLType:
149         case kVec2i_GrSLType:
150         case kVec3i_GrSLType:
151         case kVec4i_GrSLType:
152         case kTexture2D_GrSLType:
153         case kSampler_GrSLType:
154         case kImageStorage2D_GrSLType:
155         case kIImageStorage2D_GrSLType:
156             return false;
157     }
158     SkFAIL("Unexpected type");
159     return false;
160 }
161
162 static inline bool GrSLTypeIs2DCombinedSamplerType(GrSLType type) {
163     switch (type) {
164         case kTexture2DSampler_GrSLType:
165         case kITexture2DSampler_GrSLType:
166         case kTextureExternalSampler_GrSLType:
167         case kTexture2DRectSampler_GrSLType:
168             return true;
169
170         case kVoid_GrSLType:
171         case kFloat_GrSLType:
172         case kVec2f_GrSLType:
173         case kVec3f_GrSLType:
174         case kVec4f_GrSLType:
175         case kVec2i_GrSLType:
176         case kVec3i_GrSLType:
177         case kVec4i_GrSLType:
178         case kMat22f_GrSLType:
179         case kMat33f_GrSLType:
180         case kMat44f_GrSLType:
181         case kBufferSampler_GrSLType:
182         case kInt_GrSLType:
183         case kUint_GrSLType:
184         case kBool_GrSLType:
185         case kTexture2D_GrSLType:
186         case kSampler_GrSLType:
187         case kImageStorage2D_GrSLType:
188         case kIImageStorage2D_GrSLType:
189             return false;
190     }
191     SkFAIL("Unexpected type");
192     return false;
193 }
194
195 static inline bool GrSLTypeIsCombinedSamplerType(GrSLType type) {
196     switch (type) {
197         case kTexture2DSampler_GrSLType:
198         case kITexture2DSampler_GrSLType:
199         case kTextureExternalSampler_GrSLType:
200         case kTexture2DRectSampler_GrSLType:
201         case kBufferSampler_GrSLType:
202             return true;
203
204         case kVoid_GrSLType:
205         case kFloat_GrSLType:
206         case kVec2f_GrSLType:
207         case kVec3f_GrSLType:
208         case kVec4f_GrSLType:
209         case kVec2i_GrSLType:
210         case kVec3i_GrSLType:
211         case kVec4i_GrSLType:
212         case kMat22f_GrSLType:
213         case kMat33f_GrSLType:
214         case kMat44f_GrSLType:
215         case kInt_GrSLType:
216         case kUint_GrSLType:
217         case kBool_GrSLType:
218         case kTexture2D_GrSLType:
219         case kSampler_GrSLType:
220         case kImageStorage2D_GrSLType:
221         case kIImageStorage2D_GrSLType:
222             return false;
223     }
224     SkFAIL("Unexpected type");
225     return false;
226 }
227
228 static inline bool GrSLTypeIsImageStorage(GrSLType type) {
229     switch (type) {
230         case kImageStorage2D_GrSLType:
231         case kIImageStorage2D_GrSLType:
232             return true;
233
234         case kVoid_GrSLType:
235         case kFloat_GrSLType:
236         case kVec2f_GrSLType:
237         case kVec3f_GrSLType:
238         case kVec4f_GrSLType:
239         case kVec2i_GrSLType:
240         case kVec3i_GrSLType:
241         case kVec4i_GrSLType:
242         case kMat22f_GrSLType:
243         case kMat33f_GrSLType:
244         case kMat44f_GrSLType:
245         case kInt_GrSLType:
246         case kUint_GrSLType:
247         case kBool_GrSLType:
248         case kTexture2D_GrSLType:
249         case kSampler_GrSLType:
250         case kTexture2DSampler_GrSLType:
251         case kITexture2DSampler_GrSLType:
252         case kTextureExternalSampler_GrSLType:
253         case kTexture2DRectSampler_GrSLType:
254         case kBufferSampler_GrSLType:
255             return false;
256     }
257     SkFAIL("Unexpected type");
258     return false;
259 }
260
261 static inline bool GrSLTypeAcceptsPrecision(GrSLType type) {
262     switch (type) {
263         case kInt_GrSLType:
264         case kUint_GrSLType:
265         case kFloat_GrSLType:
266         case kVec2f_GrSLType:
267         case kVec3f_GrSLType:
268         case kVec4f_GrSLType:
269         case kVec2i_GrSLType:
270         case kVec3i_GrSLType:
271         case kVec4i_GrSLType:
272         case kMat22f_GrSLType:
273         case kMat33f_GrSLType:
274         case kMat44f_GrSLType:
275         case kTexture2DSampler_GrSLType:
276         case kITexture2DSampler_GrSLType:
277         case kTextureExternalSampler_GrSLType:
278         case kTexture2DRectSampler_GrSLType:
279         case kBufferSampler_GrSLType:
280         case kTexture2D_GrSLType:
281         case kSampler_GrSLType:
282         case kImageStorage2D_GrSLType:
283         case kIImageStorage2D_GrSLType:
284             return true;
285
286         case kVoid_GrSLType:
287         case kBool_GrSLType:
288             return false;
289     }
290     SkFAIL("Unexpected type");
291     return false;
292 }
293
294 //////////////////////////////////////////////////////////////////////////////
295
296 /**
297  * Types used to describe format of vertices in arrays.
298   */
299 enum GrVertexAttribType {
300     kFloat_GrVertexAttribType = 0,
301     kVec2f_GrVertexAttribType,
302     kVec3f_GrVertexAttribType,
303     kVec4f_GrVertexAttribType,
304
305     kVec2i_GrVertexAttribType,   // vector of 2 32-bit ints
306     kVec3i_GrVertexAttribType,   // vector of 3 32-bit ints
307     kVec4i_GrVertexAttribType,   // vector of 4 32-bit ints
308
309     kUByte_GrVertexAttribType,   // unsigned byte, e.g. coverage
310     kVec4ub_GrVertexAttribType,  // vector of 4 unsigned bytes, e.g. colors
311
312     kVec2us_GrVertexAttribType,   // vector of 2 shorts, e.g. texture coordinates
313
314     kInt_GrVertexAttribType,
315     kUint_GrVertexAttribType,
316
317     kLast_GrVertexAttribType = kUint_GrVertexAttribType
318 };
319 static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
320
321
322 /**
323  * Returns the size of the attrib type in bytes.
324  */
325 static inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
326     switch (type) {
327         case kFloat_GrVertexAttribType:
328             return sizeof(float);
329         case kVec2f_GrVertexAttribType:
330             return 2*sizeof(float);
331         case kVec3f_GrVertexAttribType:
332             return 3*sizeof(float);
333         case kVec4f_GrVertexAttribType:
334             return 4*sizeof(float);
335         case kVec2i_GrVertexAttribType:
336             return 2*sizeof(int32_t);
337         case kVec3i_GrVertexAttribType:
338             return 3*sizeof(int32_t);
339         case kVec4i_GrVertexAttribType:
340             return 4*sizeof(int32_t);
341         case kUByte_GrVertexAttribType:
342             return 1*sizeof(char);
343         case kVec4ub_GrVertexAttribType:
344             return 4*sizeof(char);
345         case kVec2us_GrVertexAttribType:
346             return 2*sizeof(int16_t);
347         case kInt_GrVertexAttribType:
348             return sizeof(int32_t);
349         case kUint_GrVertexAttribType:
350             return sizeof(uint32_t);
351     }
352     SkFAIL("Unexpected attribute type");
353     return 0;
354 }
355
356 /**
357  * Is the attrib type integral?
358  */
359 static inline bool GrVertexAttribTypeIsIntType(GrVertexAttribType type) {
360     switch (type) {
361         case kFloat_GrVertexAttribType:
362             return false;
363         case kVec2f_GrVertexAttribType:
364             return false;
365         case kVec3f_GrVertexAttribType:
366             return false;
367         case kVec4f_GrVertexAttribType:
368             return false;
369         case kVec2i_GrVertexAttribType:
370             return true;
371         case kVec3i_GrVertexAttribType:
372             return true;
373         case kVec4i_GrVertexAttribType:
374             return true;
375         case kUByte_GrVertexAttribType:
376             return false;
377         case kVec4ub_GrVertexAttribType:
378             return false;
379         case kVec2us_GrVertexAttribType:
380             return false;
381         case kInt_GrVertexAttribType:
382             return true;
383         case kUint_GrVertexAttribType:
384             return true;
385     }
386     SkFAIL("Unexpected attribute type");
387     return false;
388 }
389
390 /**
391  * converts a GrVertexAttribType to a GrSLType
392  */
393 static inline GrSLType GrVertexAttribTypeToSLType(GrVertexAttribType type) {
394     switch (type) {
395         case kUByte_GrVertexAttribType:
396         case kFloat_GrVertexAttribType:
397             return kFloat_GrSLType;
398         case kVec2us_GrVertexAttribType:
399         case kVec2f_GrVertexAttribType:
400             return kVec2f_GrSLType;
401         case kVec3f_GrVertexAttribType:
402             return kVec3f_GrSLType;
403         case kVec4ub_GrVertexAttribType:
404         case kVec4f_GrVertexAttribType:
405             return kVec4f_GrSLType;
406         case kVec2i_GrVertexAttribType:
407             return kVec2i_GrSLType;
408         case kVec3i_GrVertexAttribType:
409             return kVec3i_GrSLType;
410         case kVec4i_GrVertexAttribType:
411             return kVec4i_GrSLType;
412         case kInt_GrVertexAttribType:
413             return kInt_GrSLType;
414         case kUint_GrVertexAttribType:
415             return kUint_GrSLType;
416     }
417     SkFAIL("Unsupported type conversion");
418     return kVoid_GrSLType;
419 }
420
421 //////////////////////////////////////////////////////////////////////////////
422
423 enum class GrImageStorageFormat {
424     kRGBA8,
425     kRGBA8i,
426     kRGBA16f,
427     kRGBA32f,
428 };
429
430 /**
431  * Describes types of caching and compiler optimizations allowed for certain variable types
432  * (currently only image storages).
433  **/
434 enum class GrSLMemoryModel {
435     /** No special restrctions on memory accesses or compiler optimizations */
436     kNone,
437     /** Cache coherent across shader invocations */
438     kCoherent,
439     /**
440      * Disallows compiler from eliding loads or stores that appear redundant in a single
441      * invocation. Implies coherent.
442      */
443     kVolatile
444 };
445
446 /**
447  * If kYes then the memory backing the varialble is only accessed via the variable. This is
448  * currently only used with image storages.
449  */
450 enum class GrSLRestrict {
451     kYes,
452     kNo,
453 };
454
455 //////////////////////////////////////////////////////////////////////////////
456
457 /**
458 * We have coverage effects that clip rendering to the edge of some geometric primitive.
459 * This enum specifies how that clipping is performed. Not all factories that take a
460 * GrProcessorEdgeType will succeed with all values and it is up to the caller to check for
461 * a NULL return.
462 */
463 enum GrPrimitiveEdgeType {
464     kFillBW_GrProcessorEdgeType,
465     kFillAA_GrProcessorEdgeType,
466     kInverseFillBW_GrProcessorEdgeType,
467     kInverseFillAA_GrProcessorEdgeType,
468     kHairlineAA_GrProcessorEdgeType,
469
470     kLast_GrProcessorEdgeType = kHairlineAA_GrProcessorEdgeType
471 };
472
473 static const int kGrProcessorEdgeTypeCnt = kLast_GrProcessorEdgeType + 1;
474
475 static inline bool GrProcessorEdgeTypeIsFill(const GrPrimitiveEdgeType edgeType) {
476     return (kFillAA_GrProcessorEdgeType == edgeType || kFillBW_GrProcessorEdgeType == edgeType);
477 }
478
479 static inline bool GrProcessorEdgeTypeIsInverseFill(const GrPrimitiveEdgeType edgeType) {
480     return (kInverseFillAA_GrProcessorEdgeType == edgeType ||
481             kInverseFillBW_GrProcessorEdgeType == edgeType);
482 }
483
484 static inline bool GrProcessorEdgeTypeIsAA(const GrPrimitiveEdgeType edgeType) {
485     return (kFillBW_GrProcessorEdgeType != edgeType && kInverseFillBW_GrProcessorEdgeType != edgeType);
486 }
487
488 static inline GrPrimitiveEdgeType GrInvertProcessorEdgeType(const GrPrimitiveEdgeType edgeType) {
489     switch (edgeType) {
490         case kFillBW_GrProcessorEdgeType:
491             return kInverseFillBW_GrProcessorEdgeType;
492         case kFillAA_GrProcessorEdgeType:
493             return kInverseFillAA_GrProcessorEdgeType;
494         case kInverseFillBW_GrProcessorEdgeType:
495             return kFillBW_GrProcessorEdgeType;
496         case kInverseFillAA_GrProcessorEdgeType:
497             return kFillAA_GrProcessorEdgeType;
498         case kHairlineAA_GrProcessorEdgeType:
499             SkFAIL("Hairline fill isn't invertible.");
500     }
501     return kFillAA_GrProcessorEdgeType; // suppress warning.
502 }
503
504 /**
505  * Indicates the type of pending IO operations that can be recorded for gpu resources.
506  */
507 enum GrIOType {
508     kRead_GrIOType,
509     kWrite_GrIOType,
510     kRW_GrIOType
511 };
512
513 /**
514 * Indicates the type of data that a GPU buffer will be used for.
515 */
516 enum GrBufferType {
517     kVertex_GrBufferType,
518     kIndex_GrBufferType,
519     kTexel_GrBufferType,
520     kDrawIndirect_GrBufferType,
521     kXferCpuToGpu_GrBufferType,
522     kXferGpuToCpu_GrBufferType,
523
524     kLast_GrBufferType = kXferGpuToCpu_GrBufferType
525 };
526 static const int kGrBufferTypeCount = kLast_GrBufferType + 1;
527
528 static inline bool GrBufferTypeIsVertexOrIndex(GrBufferType type) {
529     SkASSERT(type >= 0 && type < kGrBufferTypeCount);
530     return type <= kIndex_GrBufferType;
531
532     GR_STATIC_ASSERT(0 == kVertex_GrBufferType);
533     GR_STATIC_ASSERT(1 == kIndex_GrBufferType);
534 }
535
536 /**
537 * Provides a performance hint regarding the frequency at which a data store will be accessed.
538 */
539 enum GrAccessPattern {
540     /** Data store will be respecified repeatedly and used many times. */
541     kDynamic_GrAccessPattern,
542     /** Data store will be specified once and used many times. (Thus disqualified from caching.) */
543     kStatic_GrAccessPattern,
544     /** Data store will be specified once and used at most a few times. (Also can't be cached.) */
545     kStream_GrAccessPattern,
546
547     kLast_GrAccessPattern = kStream_GrAccessPattern
548 };
549
550
551 #ifdef SK_DEBUG
552 // Takes a pointer to a GrCaps, and will suppress prints if required
553 #define GrCapsDebugf(caps, ...)         \
554     if (!(caps)->suppressPrints()) {    \
555         SkDebugf(__VA_ARGS__);          \
556     }
557 #else
558 #define GrCapsDebugf(caps, ...)
559 #endif
560
561 /**
562  * Specifies if the holder owns the backend, OpenGL or Vulkan, object.
563  */
564 enum class GrBackendObjectOwnership : bool {
565     /** Holder does not destroy the backend object. */
566     kBorrowed = false,
567     /** Holder destroys the backend object. */
568     kOwned = true
569 };
570
571 template <typename T> T * const * sk_sp_address_as_pointer_address(sk_sp<T> const * sp) {
572     static_assert(sizeof(T*) == sizeof(sk_sp<T>), "sk_sp not expected size.");
573     return reinterpret_cast<T * const *>(sp);
574 }
575
576 /*
577  * Object for CPU-GPU synchronization
578  */
579 typedef uint64_t GrFence;
580
581 #endif