Home | History | Annotate | Download | only in ir
      1 /*
      2  * Copyright 2016 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 #include "SkSLType.h"
      9 #include "SkSLContext.h"
     10 
     11 namespace SkSL {
     12 
     13 bool Type::determineCoercionCost(const Type& other, int* outCost) const {
     14     if (*this == other) {
     15         *outCost = 0;
     16         return true;
     17     }
     18     if (this->kind() == kVector_Kind && other.kind() == kVector_Kind) {
     19         if (this->columns() == other.columns()) {
     20             return this->componentType().determineCoercionCost(other.componentType(), outCost);
     21         }
     22         return false;
     23     }
     24     if (this->kind() == kMatrix_Kind) {
     25         if (this->columns() == other.columns() &&
     26             this->rows() == other.rows()) {
     27             return this->componentType().determineCoercionCost(other.componentType(), outCost);
     28         }
     29         return false;
     30     }
     31     for (size_t i = 0; i < fCoercibleTypes.size(); i++) {
     32         if (*fCoercibleTypes[i] == other) {
     33             *outCost = (int) i + 1;
     34             return true;
     35         }
     36     }
     37     return false;
     38 }
     39 
     40 const Type& Type::toCompound(const Context& context, int columns, int rows) const {
     41     ASSERT(this->kind() == Type::kScalar_Kind);
     42     if (columns == 1 && rows == 1) {
     43         return *this;
     44     }
     45     if (*this == *context.fFloat_Type) {
     46         switch (rows) {
     47             case 1:
     48                 switch (columns) {
     49                     case 2: return *context.fVec2_Type;
     50                     case 3: return *context.fVec3_Type;
     51                     case 4: return *context.fVec4_Type;
     52                     default: ABORT("unsupported vector column count (%d)", columns);
     53                 }
     54             case 2:
     55                 switch (columns) {
     56                     case 2: return *context.fMat2x2_Type;
     57                     case 3: return *context.fMat3x2_Type;
     58                     case 4: return *context.fMat4x2_Type;
     59                     default: ABORT("unsupported matrix column count (%d)", columns);
     60                 }
     61             case 3:
     62                 switch (columns) {
     63                     case 2: return *context.fMat2x3_Type;
     64                     case 3: return *context.fMat3x3_Type;
     65                     case 4: return *context.fMat4x3_Type;
     66                     default: ABORT("unsupported matrix column count (%d)", columns);
     67                 }
     68             case 4:
     69                 switch (columns) {
     70                     case 2: return *context.fMat2x4_Type;
     71                     case 3: return *context.fMat3x4_Type;
     72                     case 4: return *context.fMat4x4_Type;
     73                     default: ABORT("unsupported matrix column count (%d)", columns);
     74                 }
     75             default: ABORT("unsupported row count (%d)", rows);
     76         }
     77     } else if (*this == *context.fDouble_Type) {
     78         switch (rows) {
     79             case 1:
     80                 switch (columns) {
     81                     case 2: return *context.fDVec2_Type;
     82                     case 3: return *context.fDVec3_Type;
     83                     case 4: return *context.fDVec4_Type;
     84                     default: ABORT("unsupported vector column count (%d)", columns);
     85                 }
     86             case 2:
     87                 switch (columns) {
     88                     case 2: return *context.fDMat2x2_Type;
     89                     case 3: return *context.fDMat3x2_Type;
     90                     case 4: return *context.fDMat4x2_Type;
     91                     default: ABORT("unsupported matrix column count (%d)", columns);
     92                 }
     93             case 3:
     94                 switch (columns) {
     95                     case 2: return *context.fDMat2x3_Type;
     96                     case 3: return *context.fDMat3x3_Type;
     97                     case 4: return *context.fDMat4x3_Type;
     98                     default: ABORT("unsupported matrix column count (%d)", columns);
     99                 }
    100             case 4:
    101                 switch (columns) {
    102                     case 2: return *context.fDMat2x4_Type;
    103                     case 3: return *context.fDMat3x4_Type;
    104                     case 4: return *context.fDMat4x4_Type;
    105                     default: ABORT("unsupported matrix column count (%d)", columns);
    106                 }
    107             default: ABORT("unsupported row count (%d)", rows);
    108         }
    109     } else if (*this == *context.fInt_Type) {
    110         switch (rows) {
    111             case 1:
    112                 switch (columns) {
    113                     case 2: return *context.fIVec2_Type;
    114                     case 3: return *context.fIVec3_Type;
    115                     case 4: return *context.fIVec4_Type;
    116                     default: ABORT("unsupported vector column count (%d)", columns);
    117                 }
    118             default: ABORT("unsupported row count (%d)", rows);
    119         }
    120     } else if (*this == *context.fUInt_Type) {
    121         switch (rows) {
    122             case 1:
    123                 switch (columns) {
    124                     case 2: return *context.fUVec2_Type;
    125                     case 3: return *context.fUVec3_Type;
    126                     case 4: return *context.fUVec4_Type;
    127                     default: ABORT("unsupported vector column count (%d)", columns);
    128                 }
    129             default: ABORT("unsupported row count (%d)", rows);
    130         }
    131     } else if (*this == *context.fBool_Type) {
    132         switch (rows) {
    133             case 1:
    134                 switch (columns) {
    135                     case 2: return *context.fBVec2_Type;
    136                     case 3: return *context.fBVec3_Type;
    137                     case 4: return *context.fBVec4_Type;
    138                     default: ABORT("unsupported vector column count (%d)", columns);
    139                 }
    140             default: ABORT("unsupported row count (%d)", rows);
    141         }
    142     }
    143     ABORT("unsupported scalar_to_compound type %s", this->description().c_str());
    144 }
    145 
    146 } // namespace
    147