Home | History | Annotate | Download | only in gpu
      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 "GrTypes.h"
     12 #include "SkTArray.h"
     13 #include "SkRect.h"
     14 
     15 /**
     16  * Types of shader-language-specific boxed variables we can create. (Currently only GrGLShaderVars,
     17  * but should be applicable to other shader languages.)
     18  */
     19 enum GrSLType {
     20     kVoid_GrSLType,
     21     kFloat_GrSLType,
     22     kVec2f_GrSLType,
     23     kVec3f_GrSLType,
     24     kVec4f_GrSLType,
     25     kMat33f_GrSLType,
     26     kMat44f_GrSLType,
     27     kSampler2D_GrSLType,
     28 
     29     kLast_GrSLType = kSampler2D_GrSLType
     30 };
     31 static const int kGrSLTypeCount = kLast_GrSLType + 1;
     32 
     33 enum GrShaderType {
     34     kVertex_GrShaderType,
     35     kGeometry_GrShaderType,
     36     kFragment_GrShaderType,
     37 
     38     kLastkFragment_GrShaderType = kFragment_GrShaderType
     39 };
     40 static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1;
     41 
     42 /**
     43  * Precisions of shader language variables. Not all shading languages support precisions or actually
     44  * vary the internal precision based on the qualifiers. These currently only apply to float types (
     45  * including float vectors and matrices).
     46  */
     47 enum 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 
     59 static const int kGrSLPrecisionCount = kLast_GrSLPrecision + 1;
     60 
     61 /**
     62  * Gets the vector size of the SLType. Returns -1 for void, matrices, and samplers.
     63  */
     64 static inline int GrSLTypeVectorCount(GrSLType type) {
     65     SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
     66     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);
     77     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrSLTypeCount);
     78 }
     79 
     80 /** Return the type enum for a vector of floats of length n (1..4),
     81  e.g. 1 -> kFloat_GrSLType, 2 -> kVec2_GrSLType, ... */
     82 static inline GrSLType GrSLFloatVectorType(int count) {
     83     SkASSERT(count > 0 && count <= 4);
     84     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 
     92 /** Is the shading language type floating point (or vector/matrix of fp)? */
     93 static 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 }
    107 //////////////////////////////////////////////////////////////////////////////
    108 
    109 /**
    110  * Types used to describe format of vertices in arrays.
    111   */
    112 enum GrVertexAttribType {
    113     kFloat_GrVertexAttribType = 0,
    114     kVec2f_GrVertexAttribType,
    115     kVec3f_GrVertexAttribType,
    116     kVec4f_GrVertexAttribType,
    117 
    118     kUByte_GrVertexAttribType,   // unsigned byte, e.g. coverage
    119     kVec4ub_GrVertexAttribType,  // vector of 4 unsigned bytes, e.g. colors
    120 
    121     kVec2s_GrVertexAttribType,   // vector of 2 shorts, e.g. texture coordinates
    122 
    123     kLast_GrVertexAttribType = kVec2s_GrVertexAttribType
    124 };
    125 static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
    126 
    127 /**
    128  * Returns the vector size of the type.
    129  */
    130 static inline int GrVertexAttribTypeVectorCount(GrVertexAttribType type) {
    131     SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
    132     static const int kCounts[] = { 1, 2, 3, 4, 1, 4, 2 };
    133     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);
    139     GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
    140     GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
    141     GR_STATIC_ASSERT(6 == kVec2s_GrVertexAttribType);
    142     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrVertexAttribTypeCount);
    143 }
    144 
    145 /**
    146  * Returns the size of the attrib type in bytes.
    147  */
    148 static inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
    149     SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
    150     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
    155         1*sizeof(char),         // kUByte_GrVertexAttribType
    156         4*sizeof(char),         // kVec4ub_GrVertexAttribType
    157         2*sizeof(int16_t)       // kVec2s_GrVertexAttribType
    158     };
    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);
    165     GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
    166     GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
    167     GR_STATIC_ASSERT(6 == kVec2s_GrVertexAttribType);
    168     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kSizes) == kGrVertexAttribTypeCount);
    169 }
    170 
    171 /**
    172  * converts a GrVertexAttribType to a GrSLType
    173  */
    174 static 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;
    181         case kVec2s_GrVertexAttribType:
    182         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;
    189     }
    190 }
    191 
    192 //////////////////////////////////////////////////////////////////////////////
    193 
    194 /**
    195 * We have coverage effects that clip rendering to the edge of some geometric primitive.
    196 * This enum specifies how that clipping is performed. Not all factories that take a
    197 * GrProcessorEdgeType will succeed with all values and it is up to the caller to check for
    198 * a NULL return.
    199 */
    200 enum GrPrimitiveEdgeType {
    201     kFillBW_GrProcessorEdgeType,
    202     kFillAA_GrProcessorEdgeType,
    203     kInverseFillBW_GrProcessorEdgeType,
    204     kInverseFillAA_GrProcessorEdgeType,
    205     kHairlineAA_GrProcessorEdgeType,
    206 
    207     kLast_GrProcessorEdgeType = kHairlineAA_GrProcessorEdgeType
    208 };
    209 
    210 static const int kGrProcessorEdgeTypeCnt = kLast_GrProcessorEdgeType + 1;
    211 
    212 static inline bool GrProcessorEdgeTypeIsFill(const GrPrimitiveEdgeType edgeType) {
    213     return (kFillAA_GrProcessorEdgeType == edgeType || kFillBW_GrProcessorEdgeType == edgeType);
    214 }
    215 
    216 static inline bool GrProcessorEdgeTypeIsInverseFill(const GrPrimitiveEdgeType edgeType) {
    217     return (kInverseFillAA_GrProcessorEdgeType == edgeType ||
    218             kInverseFillBW_GrProcessorEdgeType == edgeType);
    219 }
    220 
    221 static inline bool GrProcessorEdgeTypeIsAA(const GrPrimitiveEdgeType edgeType) {
    222     return (kFillBW_GrProcessorEdgeType != edgeType && kInverseFillBW_GrProcessorEdgeType != edgeType);
    223 }
    224 
    225 static inline GrPrimitiveEdgeType GrInvertProcessorEdgeType(const GrPrimitiveEdgeType edgeType) {
    226     switch (edgeType) {
    227         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:
    236             SkFAIL("Hairline fill isn't invertible.");
    237     }
    238     return kFillAA_GrProcessorEdgeType; // suppress warning.
    239 }
    240 
    241 /**
    242  * Indicates the type of pending IO operations that can be recorded for gpu resources.
    243  */
    244 enum GrIOType {
    245     kRead_GrIOType,
    246     kWrite_GrIOType,
    247     kRW_GrIOType
    248 };
    249 
    250 struct 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); }
    258 
    259     bool enabled() const { return fEnabled; }
    260     const SkIRect& rect() const { return fRect; }
    261 
    262 private:
    263     bool    fEnabled;
    264     SkIRect fRect;
    265 };
    266 
    267 #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 
    277 #endif
    278