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 "SkRect.h"
     13 
     14  /**
     15   * Types of shader-language-specific boxed variables we can create. (Currently only GrGLShaderVars,
     16   * but should be applicable to other shader languages.)
     17   */
     18 enum GrSLType {
     19     kVoid_GrSLType,
     20     kFloat_GrSLType,
     21     kVec2f_GrSLType,
     22     kVec3f_GrSLType,
     23     kVec4f_GrSLType,
     24     kMat33f_GrSLType,
     25     kMat44f_GrSLType,
     26     kSampler2D_GrSLType,
     27     kSamplerExternal_GrSLType,
     28     kSampler2DRect_GrSLType,
     29     kBool_GrSLType,
     30     kInt_GrSLType,
     31     kUint_GrSLType,
     32 
     33     kLast_GrSLType = kUint_GrSLType
     34 };
     35 static const int kGrSLTypeCount = kLast_GrSLType + 1;
     36 
     37 enum GrShaderType {
     38     kVertex_GrShaderType,
     39     kGeometry_GrShaderType,
     40     kFragment_GrShaderType,
     41 
     42     kLastkFragment_GrShaderType = kFragment_GrShaderType
     43 };
     44 static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1;
     45 
     46 enum GrShaderFlags {
     47     kNone_GrShaderFlags = 0,
     48     kVertex_GrShaderFlag = 1 << kVertex_GrShaderType,
     49     kGeometry_GrShaderFlag = 1 << kGeometry_GrShaderType,
     50     kFragment_GrShaderFlag = 1 << kFragment_GrShaderType
     51 };
     52 GR_MAKE_BITFIELD_OPS(GrShaderFlags);
     53 
     54 /**
     55  * Precisions of shader language variables. Not all shading languages support precisions or actually
     56  * vary the internal precision based on the qualifiers. These currently only apply to float types (
     57  * including float vectors and matrices).
     58  */
     59 enum GrSLPrecision {
     60     kLow_GrSLPrecision,
     61     kMedium_GrSLPrecision,
     62     kHigh_GrSLPrecision,
     63 
     64     // Default precision is medium. This is because on OpenGL ES 2 highp support is not
     65     // guaranteed. On (non-ES) OpenGL the specifiers have no effect on precision.
     66     kDefault_GrSLPrecision = kMedium_GrSLPrecision,
     67 
     68     kLast_GrSLPrecision = kHigh_GrSLPrecision
     69 };
     70 
     71 static const int kGrSLPrecisionCount = kLast_GrSLPrecision + 1;
     72 
     73 /**
     74  * Gets the vector size of the SLType. Returns -1 for void, matrices, and samplers.
     75  */
     76 static inline int GrSLTypeVectorCount(GrSLType type) {
     77     SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
     78     static const int kCounts[] = { -1, 1, 2, 3, 4, -1, -1, -1, -1, -1, 1, 1, 1 };
     79     return kCounts[type];
     80 
     81     GR_STATIC_ASSERT(0 == kVoid_GrSLType);
     82     GR_STATIC_ASSERT(1 == kFloat_GrSLType);
     83     GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
     84     GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
     85     GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
     86     GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
     87     GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
     88     GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
     89     GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType);
     90     GR_STATIC_ASSERT(9 == kSampler2DRect_GrSLType);
     91     GR_STATIC_ASSERT(10 == kBool_GrSLType);
     92     GR_STATIC_ASSERT(11 == kInt_GrSLType);
     93     GR_STATIC_ASSERT(12 == kUint_GrSLType);
     94     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrSLTypeCount);
     95 }
     96 
     97 /** Return the type enum for a vector of floats of length n (1..4),
     98  e.g. 1 -> kFloat_GrSLType, 2 -> kVec2_GrSLType, ... */
     99 static inline GrSLType GrSLFloatVectorType(int count) {
    100     SkASSERT(count > 0 && count <= 4);
    101     return (GrSLType)(count);
    102 
    103     GR_STATIC_ASSERT(kFloat_GrSLType == 1);
    104     GR_STATIC_ASSERT(kVec2f_GrSLType == 2);
    105     GR_STATIC_ASSERT(kVec3f_GrSLType == 3);
    106     GR_STATIC_ASSERT(kVec4f_GrSLType == 4);
    107 }
    108 
    109 /** Is the shading language type float (including vectors/matrices)? */
    110 static inline bool GrSLTypeIsFloatType(GrSLType type) {
    111     SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
    112     return (type >= 1 && type <= 6);
    113 
    114     GR_STATIC_ASSERT(0 == kVoid_GrSLType);
    115     GR_STATIC_ASSERT(1 == kFloat_GrSLType);
    116     GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
    117     GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
    118     GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
    119     GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
    120     GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
    121     GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
    122     GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType);
    123     GR_STATIC_ASSERT(9 == kSampler2DRect_GrSLType);
    124     GR_STATIC_ASSERT(10 == kBool_GrSLType);
    125     GR_STATIC_ASSERT(11 == kInt_GrSLType);
    126     GR_STATIC_ASSERT(12 == kUint_GrSLType);
    127     GR_STATIC_ASSERT(13 == kGrSLTypeCount);
    128 }
    129 
    130 /** Is the shading language type integral (including vectors/matrices)? */
    131 static inline bool GrSLTypeIsIntType(GrSLType type) {
    132     SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
    133     return type >= kInt_GrSLType;
    134 
    135     GR_STATIC_ASSERT(0 == kVoid_GrSLType);
    136     GR_STATIC_ASSERT(1 == kFloat_GrSLType);
    137     GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
    138     GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
    139     GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
    140     GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
    141     GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
    142     GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
    143     GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType);
    144     GR_STATIC_ASSERT(9 == kSampler2DRect_GrSLType);
    145     GR_STATIC_ASSERT(10 == kBool_GrSLType);
    146     GR_STATIC_ASSERT(11 == kInt_GrSLType);
    147     GR_STATIC_ASSERT(12 == kUint_GrSLType);
    148     GR_STATIC_ASSERT(13 == kGrSLTypeCount);
    149 }
    150 
    151 /** Is the shading language type numeric (including vectors/matrices)? */
    152 static inline bool GrSLTypeIsNumeric(GrSLType type) {
    153     return GrSLTypeIsFloatType(type) || GrSLTypeIsIntType(type);
    154 }
    155 
    156 /** Returns the size in bytes for floating point GrSLTypes. For non floating point type returns 0 */
    157 static inline size_t GrSLTypeSize(GrSLType type) {
    158     SkASSERT(GrSLTypeIsFloatType(type));
    159     static const size_t kSizes[] = {
    160         0,                        // kVoid_GrSLType
    161         sizeof(float),            // kFloat_GrSLType
    162         2 * sizeof(float),        // kVec2f_GrSLType
    163         3 * sizeof(float),        // kVec3f_GrSLType
    164         4 * sizeof(float),        // kVec4f_GrSLType
    165         9 * sizeof(float),        // kMat33f_GrSLType
    166         16 * sizeof(float),       // kMat44f_GrSLType
    167         0,                        // kSampler2D_GrSLType
    168         0,                        // kSamplerExternal_GrSLType
    169         0,                        // kSampler2DRect_GrSLType
    170         0,                        // kBool_GrSLType
    171         0,                        // kInt_GrSLType
    172         0,                        // kUint_GrSLType
    173     };
    174     return kSizes[type];
    175 
    176     GR_STATIC_ASSERT(0 == kVoid_GrSLType);
    177     GR_STATIC_ASSERT(1 == kFloat_GrSLType);
    178     GR_STATIC_ASSERT(2 == kVec2f_GrSLType);
    179     GR_STATIC_ASSERT(3 == kVec3f_GrSLType);
    180     GR_STATIC_ASSERT(4 == kVec4f_GrSLType);
    181     GR_STATIC_ASSERT(5 == kMat33f_GrSLType);
    182     GR_STATIC_ASSERT(6 == kMat44f_GrSLType);
    183     GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
    184     GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType);
    185     GR_STATIC_ASSERT(9 == kSampler2DRect_GrSLType);
    186     GR_STATIC_ASSERT(10 == kBool_GrSLType);
    187     GR_STATIC_ASSERT(11 == kInt_GrSLType);
    188     GR_STATIC_ASSERT(12 == kUint_GrSLType);
    189     GR_STATIC_ASSERT(13 == kGrSLTypeCount);
    190 }
    191 
    192 static inline bool GrSLTypeIsSamplerType(GrSLType type) {
    193     SkASSERT(type >= 0 && type < static_cast<GrSLType>(kGrSLTypeCount));
    194     return type >= 7 && type <= 9;
    195 
    196     GR_STATIC_ASSERT(7 == kSampler2D_GrSLType);
    197     GR_STATIC_ASSERT(8 == kSamplerExternal_GrSLType);
    198     GR_STATIC_ASSERT(9 == kSampler2DRect_GrSLType);
    199 }
    200 
    201 //////////////////////////////////////////////////////////////////////////////
    202 
    203 /**
    204  * Types used to describe format of vertices in arrays.
    205   */
    206 enum GrVertexAttribType {
    207     kFloat_GrVertexAttribType = 0,
    208     kVec2f_GrVertexAttribType,
    209     kVec3f_GrVertexAttribType,
    210     kVec4f_GrVertexAttribType,
    211 
    212     kUByte_GrVertexAttribType,   // unsigned byte, e.g. coverage
    213     kVec4ub_GrVertexAttribType,  // vector of 4 unsigned bytes, e.g. colors
    214 
    215     kVec2us_GrVertexAttribType,   // vector of 2 shorts, e.g. texture coordinates
    216 
    217     kInt_GrVertexAttribType,
    218     kUint_GrVertexAttribType,
    219 
    220     kLast_GrVertexAttribType = kUint_GrVertexAttribType
    221 };
    222 static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
    223 
    224 /**
    225  * Returns the vector size of the type.
    226  */
    227 static inline int GrVertexAttribTypeVectorCount(GrVertexAttribType type) {
    228     SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
    229     static const int kCounts[] = { 1, 2, 3, 4, 1, 4, 2, 1, 1 };
    230     return kCounts[type];
    231 
    232     GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
    233     GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
    234     GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
    235     GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
    236     GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
    237     GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
    238     GR_STATIC_ASSERT(6 == kVec2us_GrVertexAttribType);
    239     GR_STATIC_ASSERT(7 == kInt_GrVertexAttribType);
    240     GR_STATIC_ASSERT(8 == kUint_GrVertexAttribType);
    241     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kCounts) == kGrVertexAttribTypeCount);
    242 }
    243 
    244 /**
    245  * Returns the size of the attrib type in bytes.
    246  */
    247 static inline size_t GrVertexAttribTypeSize(GrVertexAttribType type) {
    248     static const size_t kSizes[] = {
    249         sizeof(float),          // kFloat_GrVertexAttribType
    250         2*sizeof(float),        // kVec2f_GrVertexAttribType
    251         3*sizeof(float),        // kVec3f_GrVertexAttribType
    252         4*sizeof(float),        // kVec4f_GrVertexAttribType
    253         1*sizeof(char),         // kUByte_GrVertexAttribType
    254         4*sizeof(char),         // kVec4ub_GrVertexAttribType
    255         2*sizeof(int16_t),      // kVec2us_GrVertexAttribType
    256         sizeof(int32_t),        // kInt_GrVertexAttribType
    257         sizeof(uint32_t)        // kUint_GrVertexAttribType
    258     };
    259     return kSizes[type];
    260 
    261     GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
    262     GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
    263     GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
    264     GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
    265     GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
    266     GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
    267     GR_STATIC_ASSERT(6 == kVec2us_GrVertexAttribType);
    268     GR_STATIC_ASSERT(7 == kInt_GrVertexAttribType);
    269     GR_STATIC_ASSERT(8 == kUint_GrVertexAttribType);
    270     GR_STATIC_ASSERT(SK_ARRAY_COUNT(kSizes) == kGrVertexAttribTypeCount);
    271 }
    272 
    273 /**
    274  * Is the attrib type integral?
    275  */
    276 static inline bool GrVertexAttribTypeIsIntType(GrVertexAttribType type) {
    277     SkASSERT(type >= 0 && type < static_cast<GrVertexAttribType>(kGrVertexAttribTypeCount));
    278     return type >= kInt_GrVertexAttribType;
    279 
    280     GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
    281     GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
    282     GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
    283     GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
    284     GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
    285     GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
    286     GR_STATIC_ASSERT(6 == kVec2us_GrVertexAttribType);
    287     GR_STATIC_ASSERT(7 == kInt_GrVertexAttribType);
    288     GR_STATIC_ASSERT(8 == kUint_GrVertexAttribType);
    289     GR_STATIC_ASSERT(9 == kGrVertexAttribTypeCount);
    290 }
    291 
    292 /**
    293  * converts a GrVertexAttribType to a GrSLType
    294  */
    295 static inline GrSLType GrVertexAttribTypeToSLType(GrVertexAttribType type) {
    296     switch (type) {
    297         default:
    298             SkFAIL("Unsupported type conversion");
    299             return kVoid_GrSLType;
    300         case kUByte_GrVertexAttribType:
    301         case kFloat_GrVertexAttribType:
    302             return kFloat_GrSLType;
    303         case kVec2us_GrVertexAttribType:
    304         case kVec2f_GrVertexAttribType:
    305             return kVec2f_GrSLType;
    306         case kVec3f_GrVertexAttribType:
    307             return kVec3f_GrSLType;
    308         case kVec4ub_GrVertexAttribType:
    309         case kVec4f_GrVertexAttribType:
    310             return kVec4f_GrSLType;
    311         case kInt_GrVertexAttribType:
    312             return kInt_GrSLType;
    313         case kUint_GrVertexAttribType:
    314             return kUint_GrSLType;
    315     }
    316 }
    317 
    318 //////////////////////////////////////////////////////////////////////////////
    319 
    320 /**
    321 * We have coverage effects that clip rendering to the edge of some geometric primitive.
    322 * This enum specifies how that clipping is performed. Not all factories that take a
    323 * GrProcessorEdgeType will succeed with all values and it is up to the caller to check for
    324 * a NULL return.
    325 */
    326 enum GrPrimitiveEdgeType {
    327     kFillBW_GrProcessorEdgeType,
    328     kFillAA_GrProcessorEdgeType,
    329     kInverseFillBW_GrProcessorEdgeType,
    330     kInverseFillAA_GrProcessorEdgeType,
    331     kHairlineAA_GrProcessorEdgeType,
    332 
    333     kLast_GrProcessorEdgeType = kHairlineAA_GrProcessorEdgeType
    334 };
    335 
    336 static const int kGrProcessorEdgeTypeCnt = kLast_GrProcessorEdgeType + 1;
    337 
    338 static inline bool GrProcessorEdgeTypeIsFill(const GrPrimitiveEdgeType edgeType) {
    339     return (kFillAA_GrProcessorEdgeType == edgeType || kFillBW_GrProcessorEdgeType == edgeType);
    340 }
    341 
    342 static inline bool GrProcessorEdgeTypeIsInverseFill(const GrPrimitiveEdgeType edgeType) {
    343     return (kInverseFillAA_GrProcessorEdgeType == edgeType ||
    344             kInverseFillBW_GrProcessorEdgeType == edgeType);
    345 }
    346 
    347 static inline bool GrProcessorEdgeTypeIsAA(const GrPrimitiveEdgeType edgeType) {
    348     return (kFillBW_GrProcessorEdgeType != edgeType && kInverseFillBW_GrProcessorEdgeType != edgeType);
    349 }
    350 
    351 static inline GrPrimitiveEdgeType GrInvertProcessorEdgeType(const GrPrimitiveEdgeType edgeType) {
    352     switch (edgeType) {
    353         case kFillBW_GrProcessorEdgeType:
    354             return kInverseFillBW_GrProcessorEdgeType;
    355         case kFillAA_GrProcessorEdgeType:
    356             return kInverseFillAA_GrProcessorEdgeType;
    357         case kInverseFillBW_GrProcessorEdgeType:
    358             return kFillBW_GrProcessorEdgeType;
    359         case kInverseFillAA_GrProcessorEdgeType:
    360             return kFillAA_GrProcessorEdgeType;
    361         case kHairlineAA_GrProcessorEdgeType:
    362             SkFAIL("Hairline fill isn't invertible.");
    363     }
    364     return kFillAA_GrProcessorEdgeType; // suppress warning.
    365 }
    366 
    367 /**
    368  * Indicates the type of pending IO operations that can be recorded for gpu resources.
    369  */
    370 enum GrIOType {
    371     kRead_GrIOType,
    372     kWrite_GrIOType,
    373     kRW_GrIOType
    374 };
    375 
    376 struct GrScissorState {
    377     GrScissorState() : fEnabled(false) {}
    378     void set(const SkIRect& rect) { fRect = rect; fEnabled = true; }
    379     bool operator==(const GrScissorState& other) const {
    380         return fEnabled == other.fEnabled &&
    381                 (false == fEnabled || fRect == other.fRect);
    382     }
    383     bool operator!=(const GrScissorState& other) const { return !(*this == other); }
    384 
    385     bool enabled() const { return fEnabled; }
    386     const SkIRect& rect() const { return fRect; }
    387 
    388 private:
    389     bool    fEnabled;
    390     SkIRect fRect;
    391 };
    392 
    393 /**
    394  * Indicates the transfer direction for a transfer buffer
    395  */
    396 enum TransferType {
    397     /** Caller intends to use the buffer to transfer data to the GPU */
    398     kCpuToGpu_TransferType,
    399     /** Caller intends to use the buffer to transfer data from the GPU */
    400     kGpuToCpu_TransferType
    401 };
    402 
    403 
    404 #ifdef SK_DEBUG
    405 // Takes a pointer to a GrCaps, and will suppress prints if required
    406 #define GrCapsDebugf(caps, ...)         \
    407     if (!caps->suppressPrints()) {      \
    408         SkDebugf(__VA_ARGS__);          \
    409     }
    410 #else
    411 #define GrCapsDebugf(caps, ...)
    412 #endif
    413 
    414 #endif
    415