summaryrefslogtreecommitdiff
path: root/tests/glslparsertest/glsl2/gen_builtin_test.py
blob: e1e05e325210249fb74843ebc603eabc6529c74d (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python

one_parameter_110 = [
    "radians",
    "degrees",
    "sin",
    "cos",
    "tan",
    "asin",
    "acos",
    "atan",
    "exp",
    "log",
    "exp2",
    "log2",
    "sqrt",
    "inversesqrt",
    "abs",
    "sign",
    "floor",
    "ceil",
    "fract",
    "length",
    "normalize",
    ]

one_parameter_110_fs = [
    "dFdx",
    "dFdy",
    "fwidth",
    ]

two_parameter_110 = [
    "atan",
    "pow",
    "mod",
    "min",
    "max",
    "step",
    "distance",
    "dot",
    "reflect",
    ]

three_parameter_110 = [
    "clamp",
    "mix",
    "smoothstep",
    "faceforward",
    ]

misc_parameter_110 = [
    [ "genType", "mod", "genType", "float" ],
    [ "genType", "min", "genType", "float" ],
    [ "genType", "max", "genType", "float" ],
    [ "genType", "clamp", "genType", "float", "float" ],
    [ "genType", "mix", "genType", "genType", "float" ],
    [ "genType", "step", "float", "genType" ],
    [ "genType", "smoothstep", "float", "float", "genType" ],
    [ "genType", "refract", "genType", "genType", "float" ],
    [ "float", "noise1", "genType" ],
    [ "vec2", "noise2", "genType" ],
    [ "vec3", "noise3", "genType" ],
    [ "vec4", "noise4", "genType" ],
    ]

vec_parameter_110 = [
    ["bvec", "lessThan", "vecType", "vecType" ],
    ["bvec", "lessThanEqual", "vecType", "vecType" ],
    ["bvec", "greaterThan", "vecType", "vecType" ],
    ["bvec", "greaterThanEqual", "vecType", "vecType" ],
    ["bvec", "equal", "vecType", "vecType" ],
    ["bvec", "notEqual", "vecType", "vecType" ],
    ]

bvec_parameter_110 = [
    ["bvec", "equal", "bvec", "bvec" ],
    ["bvec", "notEqual", "bvec", "bvec" ],
    ["bool", "any", "bvec" ],
    ["bool", "all", "bvec" ],
    ["bvec", "not", "bvec" ],
    ]

genType = [ "float", "vec2", "vec3", "vec4" ]
vecType = [ "vec2", "vec3", "vec4", "ivec2", "ivec3", "ivec4" ]
bvecType = [ "bvec2", "bvec3", "bvec4" ]


def typename(t, gen):
    if (t == "genType"):
        return gen
    elif (t == "vecType"):
        return gen
    elif (t == "bvec"):
        return "bvec" + gen[len(gen) - 1]
    else:
        return t


def emit_pattern_call(f, pat_list, type_list):
    for sig in pat_list:
        for t in type_list:
            name = sig[1]
            ret = typename(sig[0], t)

            params = "u_%s" % (typename(sig[2], t))
            for p in sig[3:]:
                params += ", u_%s" % (typename(p, t))

            f.write("  t_%s = %s(%s);\n" % (ret, name, params))

    f.write("\n")


def emit_matrixCompMult(f, matTypes):
    for mat in matTypes:
        f.write("  t_%s = matrixCompMult(u_%s, u_%s);\n" % (mat, mat, mat))


def emit_110_tests(f):
    f.write("/* PASS */\n#version 110\n\n")
    f.write("uniform float u_float;\n")


    for s in ["vec", "ivec", "bvec", "mat"]:
        for i in [2, 3, 4]:
            f.write("uniform %s%d u_%s%d;\n" % (s, i, s, i))

    f.write("""\nvoid main()
{
  gl_Position = gl_Vertex;

  float t_float = float(0.0);
  bool t_bool = false;\n""")

    for i in [2, 3, 4]:
        f.write("  vec%d  t_vec%d  = vec%d (0.0);\n" % (i, i, i))
        f.write("  bvec%d t_bvec%d = bvec%d(0.0);\n" % (i, i, i))
        f.write("  mat%d  t_mat%d  = mat%d (0.0);\n" % (i, i, i))

    f.write("\n")

    for name in one_parameter_110:
        for t in genType:
            f.write("  t_%s += %s(u_%s);\n" % (t, name, t))

    f.write("\n")

    for name in two_parameter_110:
        for t in genType:
            f.write("  t_%s += %s(u_%s, -u_%s);\n" % (t, name, t, t))

    f.write("\n")

    for name in three_parameter_110:
        for t in genType:
            f.write("  t_%s += %s(u_%s, -u_%s, 11.7 * u_%s);\n" %
                    (t, name, t, t, t))

    f.write("\n")

    emit_pattern_call(f, misc_parameter_110, genType)
    emit_pattern_call(f, vec_parameter_110, vecType)
    emit_pattern_call(f, bvec_parameter_110, bvecType)
    emit_matrixCompMult(f, ["mat2", "mat3", "mat4"])

    f.write("}\n")


def emit_110_fs_tests(f):
    f.write("/* PASS */\n#version 110\n\n")
    f.write("uniform float u_float;\n")


    for i in [2, 3, 4]:
        f.write("uniform vec%d u_vec%d;\n" % (i, i))

    f.write("""\nvoid main()
{
  gl_FragColor = gl_Color;

  float t_float = float(0.0);\n""")

    for i in [2, 3, 4]:
        f.write("  vec%d  t_vec%d  = vec%d (0.0);\n" % (i, i, i))

    f.write("\n")

    for name in one_parameter_110_fs:
        for t in genType:
            f.write("  t_%s = %s(u_%s);\n" % (t, name, t))

    f.write("}\n")


def emit_120_tests(f):
    f.write("/* PASS */\n#version 120\n\n")

    for cols in [2, 3, 4]:
        f.write("uniform mat%d u_mat%d;\n" % (cols, cols));
        f.write("uniform vec%d u_vec%d;\n" % (cols, cols));

        for rows in [2, 3, 4]:
            mat = "mat%dx%d" % (cols, rows)
            f.write("uniform %s u_%s;\n" % (mat, mat))

    f.write("""\nvoid main()
{
  gl_Position = gl_Vertex;

""")

    for cols in [2, 3, 4]:
        f.write("  mat%d   t_mat%d   = mat%d  (0.0);\n" % (cols, cols, cols));

        for rows in [2, 3, 4]:
            mat = "mat%dx%d" % (cols, rows)
            f.write("  %s t_%s = %s(0.0);\n" % (mat, mat, mat))

    f.write("\n")

    for cols in [2, 3, 4]:
        dst = "t_mat%d" % (cols)
        src = "u_mat%d" % (cols)
        f.write("  %s   = transpose(%s);\n" % (dst, src));

        for rows in [2, 3, 4]:
            dst = "t_mat%dx%d" % (cols, rows)
            src = "u_mat%dx%d" % (rows, cols)
            f.write("  %s = transpose(%s);\n" % (dst, src));

    f.write("\n")

    for cols in [2, 3, 4]:
        f.write("  t_mat%d   = outerProduct(u_vec%d, u_vec%d);\n"
                % (cols, cols, cols));

        for rows in [2, 3, 4]:
             f.write("  t_mat%dx%d = outerProduct(u_vec%d, u_vec%d);\n"
                     % (cols, rows, rows, cols));

    f.write("\n")

    emit_matrixCompMult(f, ["mat2x2", "mat2x3", "mat2x4",
                            "mat3x2", "mat3x3", "mat3x4",
                            "mat4x2", "mat4x3", "mat4x4"])

    f.write("}\n")


f = open("builtin-functions-110.vert", "w")
emit_110_tests(f)
f.close()

f = open("builtin-functions-110.frag", "w")
emit_110_fs_tests(f)
f.close()

f = open("builtin-functions-120.vert", "w")
emit_120_tests(f)
f.close()