aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/gl/GrGLSL_impl.h
blob: 292048c6a61147ae3f78359abeb43f0775adfe3e (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrGLSL_impl_DEFINED
#define GrGLSL_impl_DEFINED

#include "SkString.h"

namespace {
template<int N>
GrSLConstantVec return_const_vecf(GrSLConstantVec constVec, SkString* outAppend, bool omitAppend) {
    SkASSERT(kNone_GrSLConstantVec != constVec);
    if (!omitAppend) {
        if (kZeros_GrSLConstantVec == constVec) {
            outAppend->append(GrGLSLZerosVecf(N));
        } else {
            outAppend->append(GrGLSLOnesVecf(N));
        }
    }
    return constVec;
}
}

template <int N>
GrSLConstantVec GrGLSLModulatef(SkString* outAppend,
                                const char* in0,
                                const char* in1,
                                GrSLConstantVec default0,
                                GrSLConstantVec default1,
                                bool omitIfConstVec) {
    SkASSERT(N > 0 && N <= 4);
    SkASSERT(NULL != outAppend);

    bool has0 = NULL != in0 && '\0' != *in0;
    bool has1 = NULL != in1 && '\0' != *in1;

    SkASSERT(has0 || kNone_GrSLConstantVec != default0);
    SkASSERT(has1 || kNone_GrSLConstantVec != default1);

    if (!has0 && !has1) {
        SkASSERT(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec == default0);
        SkASSERT(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec == default1);
        if (kZeros_GrSLConstantVec == default0 || kZeros_GrSLConstantVec == default1) {
            return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitIfConstVec);
        } else {
            // both inputs are ones vectors
            return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIfConstVec);
        }
    } else if (!has0) {
        SkASSERT(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec == default0);
        if (kZeros_GrSLConstantVec == default0) {
            return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitIfConstVec);
        } else {
            outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in1);
            return kNone_GrSLConstantVec;
        }
    } else if (!has1) {
        SkASSERT(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec == default1);
        if (kZeros_GrSLConstantVec == default1) {
            return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitIfConstVec);
        } else {
            outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in0);
            return kNone_GrSLConstantVec;
        }
    } else {
        outAppend->appendf("%s((%s) * (%s))", GrGLSLFloatVectorTypeString(N), in0, in1);
        return kNone_GrSLConstantVec;
    }
}

template <int N>
GrSLConstantVec GrGLSLAddf(SkString* outAppend,
                           const char* in0,
                           const char* in1,
                           GrSLConstantVec default0,
                           GrSLConstantVec default1,
                           bool omitIfConstVec) {
    SkASSERT(N > 0 && N <= 4);
    SkASSERT(NULL != outAppend);

    bool has0 = NULL != in0 && '\0' != *in0;
    bool has1 = NULL != in1 && '\0' != *in1;

    if (!has0 && !has1) {
        SkASSERT(kNone_GrSLConstantVec != default0);
        SkASSERT(kNone_GrSLConstantVec != default1);
        int sum = (kOnes_GrSLConstantVec == default0) + (kOnes_GrSLConstantVec == default1);
        if (0 == sum) {
            return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitIfConstVec);
        } else if (1 == sum) {
            outAppend->append(GrGLSLOnesVecf(N));
            return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIfConstVec);
        } else {
            SkASSERT(2 == sum);
            outAppend->appendf("%s(2)", GrGLSLFloatVectorTypeString(N));
            return kNone_GrSLConstantVec;
        }
    } else if (!has0) {
        SkASSERT(kNone_GrSLConstantVec != default0);
        if (kZeros_GrSLConstantVec == default0) {
            outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in1);
        } else {
            outAppend->appendf("%s(%s) + %s",
                               GrGLSLFloatVectorTypeString(N),
                               in1,
                               GrGLSLOnesVecf(N));
        }
        return kNone_GrSLConstantVec;
    } else if (!has1) {
        SkASSERT(kNone_GrSLConstantVec != default1);
        if (kZeros_GrSLConstantVec == default1) {
            outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in0);
        } else {
            outAppend->appendf("%s(%s) + %s",
                               GrGLSLFloatVectorTypeString(N),
                               in0,
                               GrGLSLOnesVecf(N));
        }
        return kNone_GrSLConstantVec;
    } else {
        outAppend->appendf("(%s(%s) + %s(%s))",
                           GrGLSLFloatVectorTypeString(N),
                           in0,
                           GrGLSLFloatVectorTypeString(N),
                           in1);
        return kNone_GrSLConstantVec;
    }
}

template <int N>
GrSLConstantVec GrGLSLSubtractf(SkString* outAppend,
                                 const char* in0,
                                 const char* in1,
                                 GrSLConstantVec default0,
                                 GrSLConstantVec default1,
                                 bool omitIfConstVec) {
    SkASSERT(N > 0 && N <= 4);
    SkASSERT(NULL != outAppend);

    bool has0 = NULL != in0 && '\0' != *in0;
    bool has1 = NULL != in1 && '\0' != *in1;

    if (!has0 && !has1) {
        SkASSERT(kNone_GrSLConstantVec != default0);
        SkASSERT(kNone_GrSLConstantVec != default1);
        int diff = (kOnes_GrSLConstantVec == default0) - (kOnes_GrSLConstantVec == default1);
        if (-1 == diff) {
            outAppend->appendf("%s(-1)", GrGLSLFloatVectorTypeString(N));
            return kNone_GrSLConstantVec;
        } else if (0 == diff) {
            return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitIfConstVec);
        } else {
            SkASSERT(1 == diff);
            return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIfConstVec);
        }
    } else if (!has0) {
        SkASSERT(kNone_GrSLConstantVec != default0);
        if (kZeros_GrSLConstantVec == default0) {
            outAppend->appendf("-%s(%s)", GrGLSLFloatVectorTypeString(N), in1);
        } else {
            outAppend->appendf("%s - %s(%s)",
                               GrGLSLOnesVecf(N),
                               GrGLSLFloatVectorTypeString(N),
                               in1);
        }
        return kNone_GrSLConstantVec;
    } else if (!has1) {
        SkASSERT(kNone_GrSLConstantVec != default1);
        if (kZeros_GrSLConstantVec == default1) {
            outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in0);
        } else {
            outAppend->appendf("%s(%s) - %s",
                               GrGLSLFloatVectorTypeString(N),
                               in0,
                               GrGLSLOnesVecf(N));
        }
        return kNone_GrSLConstantVec;
    } else {
        outAppend->appendf("(%s(%s) - %s(%s))",
                           GrGLSLFloatVectorTypeString(N),
                           in0,
                           GrGLSLFloatVectorTypeString(N),
                           in1);
        return kNone_GrSLConstantVec;
    }
}

#endif