aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sksl/ir/SkSLType.cpp
blob: d28c4f0666a74002e2c20c6fb67ed232cf0f9dbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
 
#include "SkSLType.h"
#include "SkSLContext.h"

namespace SkSL {

bool Type::determineCoercionCost(const Type& other, int* outCost) const {
    if (*this == other) {
        *outCost = 0;
        return true;
    }
    if (this->kind() == kVector_Kind && other.kind() == kVector_Kind) {
        if (this->columns() == other.columns()) {
            return this->componentType().determineCoercionCost(other.componentType(), outCost);
        }
        return false;
    }
    if (this->kind() == kMatrix_Kind) {
        if (this->columns() == other.columns() && 
            this->rows() == other.rows()) {
            return this->componentType().determineCoercionCost(other.componentType(), outCost);
        }
        return false;
    }
    for (size_t i = 0; i < fCoercibleTypes.size(); i++) {
        if (*fCoercibleTypes[i] == other) {
            *outCost = (int) i + 1;
            return true;
        }
    }
    return false;
}

const Type& Type::toCompound(const Context& context, int columns, int rows) const {
    ASSERT(this->kind() == Type::kScalar_Kind);
    if (columns == 1 && rows == 1) {
        return *this;
    }
    if (*this == *context.fFloat_Type) {
        switch (rows) {
            case 1:
                switch (columns) {
                    case 2: return *context.fVec2_Type;
                    case 3: return *context.fVec3_Type;
                    case 4: return *context.fVec4_Type;
                    default: ABORT("unsupported vector column count (%d)", columns);
                }
            case 2:
                switch (columns) {
                    case 2: return *context.fMat2x2_Type;
                    case 3: return *context.fMat3x2_Type;
                    case 4: return *context.fMat4x2_Type;
                    default: ABORT("unsupported matrix column count (%d)", columns);
                }
            case 3:
                switch (columns) {
                    case 2: return *context.fMat2x3_Type;
                    case 3: return *context.fMat3x3_Type;
                    case 4: return *context.fMat4x3_Type;
                    default: ABORT("unsupported matrix column count (%d)", columns);
                }
            case 4:
                switch (columns) {
                    case 2: return *context.fMat2x4_Type;
                    case 3: return *context.fMat3x4_Type;
                    case 4: return *context.fMat4x4_Type;
                    default: ABORT("unsupported matrix column count (%d)", columns);
                }
            default: ABORT("unsupported row count (%d)", rows);
        }
    } else if (*this == *context.fDouble_Type) {
        switch (rows) {
            case 1:
                switch (columns) {
                    case 2: return *context.fDVec2_Type;
                    case 3: return *context.fDVec3_Type;
                    case 4: return *context.fDVec4_Type;
                    default: ABORT("unsupported vector column count (%d)", columns);
                }
            case 2:
                switch (columns) {
                    case 2: return *context.fDMat2x2_Type;
                    case 3: return *context.fDMat3x2_Type;
                    case 4: return *context.fDMat4x2_Type;
                    default: ABORT("unsupported matrix column count (%d)", columns);
                }
            case 3:
                switch (columns) {
                    case 2: return *context.fDMat2x3_Type;
                    case 3: return *context.fDMat3x3_Type;
                    case 4: return *context.fDMat4x3_Type;
                    default: ABORT("unsupported matrix column count (%d)", columns);
                }
            case 4:
                switch (columns) {
                    case 2: return *context.fDMat2x4_Type;
                    case 3: return *context.fDMat3x4_Type;
                    case 4: return *context.fDMat4x4_Type;
                    default: ABORT("unsupported matrix column count (%d)", columns);
                }
            default: ABORT("unsupported row count (%d)", rows);
        }
    } else if (*this == *context.fInt_Type) {
        switch (rows) {
            case 1:
                switch (columns) {
                    case 2: return *context.fIVec2_Type;
                    case 3: return *context.fIVec3_Type;
                    case 4: return *context.fIVec4_Type;
                    default: ABORT("unsupported vector column count (%d)", columns);
                }
            default: ABORT("unsupported row count (%d)", rows);
        }
    } else if (*this == *context.fUInt_Type) {
        switch (rows) {
            case 1:
                switch (columns) {
                    case 2: return *context.fUVec2_Type;
                    case 3: return *context.fUVec3_Type;
                    case 4: return *context.fUVec4_Type;
                    default: ABORT("unsupported vector column count (%d)", columns);
                }
            default: ABORT("unsupported row count (%d)", rows);
        }
    }
    ABORT("unsupported scalar_to_compound type %s", this->description().c_str());
}

} // namespace