summaryrefslogtreecommitdiff
path: root/generated_tests/gen_tcs_input_tests.py
blob: 5d2b4a5db90b014dca4565dc9858c6113a2e4023 (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
# coding=utf-8
#
# Copyright © 2014 The Piglit Project
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

"""Test passing variables from the vertex shader to the tessellation control
shader.

For every varying type create one tests that passes a scalar of that type
and one test that passes a two element array.
Copy a uniform value to the varying in the vertex shader and compare the
varying to the same uniform in the tessellation control shader.
If the values are equal draw the screen green, red otherwise.

Draw for tessellated quads. Each should cover one quarter of the screen.

This script outputs, to stdout, the name of each file it generates.

"""

from __future__ import print_function, division, absolute_import
import os
import sys
import random


class Test(object):
    def __init__(self, type_name, array, name):
        """Creates a test.

        type_name -- varying type to test (e.g.: vec4, mat3x2, int, ...)
        array -- number of array elements to test, None for no array
        name -- name of the variable to test

        """
        self.var_name = name or 'var'

        if self.var_name == 'gl_Position':
            self.var_type = 'vec4'
            self.var_array = None
        elif self.var_name == 'gl_PointSize':
            self.var_type = 'float'
            self.var_array = None
        elif self.var_name == 'gl_ClipDistance':
            self.var_type = 'float'
            self.var_array = 8
        else:
            self.var_type = type_name
            self.var_array = array

        if self.built_in:
            self.interface_name = 'gl_PerVertex'
            self.interface_vs_instance = ''
            self.interface_tcs_instance = 'gl_in'
        else:
            self.interface_name = 'v2tc_interface'
            self.interface_vs_instance = ''
            self.interface_tcs_instance = 'v2tc'

        if self.var_array:
            self.var_type_full = self.var_type + '[{0}]'.format(self.var_array)
        else:
            self.var_type_full = self.var_type

    @property
    def built_in(self):
        return self.var_name.startswith('gl_')

    @property
    def vs_var_ref(self):
        return '.' + self.var_name if self.interface_vs_instance else self.var_name

    @property
    def tcs_var_ref(self):
        return '.' + self.var_name if self.interface_tcs_instance else self.var_name

    @property
    def uniform_string(self):
        """Returns string for loading uniform data by the shader_runner."""
        data = self.test_data()
        uniforms = ''
        if self.var_array:
            for i in xrange(12):
                for j in xrange(self.var_array):
                    uniforms += 'uniform {0} reference[{1}].v[{2}] {3}\n'.format(
                        self.var_type, i, j, data[i*j])
        else:
            for i in xrange(12):
                uniforms += 'uniform {0} reference[{1}].v {2}\n'.format(
                    self.var_type,
                    i,
                    data[i])

        #strip last newline
        return uniforms[:-1]

    def components(self):
        """Returns the number of scalar components of the used data type."""
        n = 1

        if self.var_type.startswith('mat'):
            if 'x' in self.var_type:
                n *= int(self.var_type[-1])
                n *= int(self.var_type[-3])
            else:
                n *= int(self.var_type[-1])
                n *= int(self.var_type[-1])
        elif 'vec' in self.var_type:
            n *= int(self.var_type[-1])

        return n

    def test_data(self):
        """Returns random but deterministic data as a list of strings.

        n strings are returned containing c random values, each.
        Where n is the number of vertices times the array length and
        c is the number of components in the tested scalar data type.
        """
        random.seed(17)

        if self.var_array:
            n = self.var_array * 12
        else:
            n = 12

        if self.var_type.startswith('i'):
            rand = lambda: random.randint(-0x80000000, 0x7fffffff)
        elif self.var_type.startswith('u'):
            rand = lambda: random.randint(0, 0xffffffff)
        else:
            rand = lambda: ((-1 + 2 * random.randint(0, 1)) *
                            random.randint(0, 2**23-1) *
                            2.0**(random.randint(-126, 127)))

        c = self.components()

        ret = []
        for i in xrange(n):
            ret.append(" ".join(str(rand()) for _ in xrange(c)))

        return ret

    def filename(self):
        """Returns the file name (including path) for the test."""
        if self.built_in:
            name = self.var_name
        elif self.var_array:
            name = self.var_type + '_{0}'.format(self.var_array)
        else:
            name = self.var_type
        return os.path.join('spec',
                            'arb_tessellation_shader',
                            'execution',
                            'tcs-input',
                            'tcs-input-{0}.shader_test'.format(name))

    def generate(self):
        """Generates and writes the test to disc."""
        test = \
"""# Test generated by:
# {generator_command}
# Test tessellation control shader inputs
[require]
GLSL >= 1.50
GL_ARB_tessellation_shader

[vertex shader]
uniform struct S0 {{
        {self.var_type_full} v;
}} reference[12];

out {self.interface_name} {{
        {self.var_type_full} {self.var_name};
}} {self.interface_vs_instance};

void main()
{{
        {self.interface_vs_instance}{self.vs_var_ref} = reference[gl_VertexID].v;
}}

[tessellation control shader]
#extension GL_ARB_tessellation_shader : require
layout(vertices = 3) out;

uniform struct S0 {{
        {self.var_type_full} v;
}} reference[12];

in {self.interface_name} {{
        {self.var_type_full} {self.var_name};
}} {self.interface_tcs_instance}[];

out int pass[];

void main()
{{
        const int vertices_in = 3;
        int local_pass = 1;
        for (int i = 0; i < vertices_in; ++i) {{
                int vertex_ID = gl_PrimitiveID * vertices_in + i;
                if ({self.interface_tcs_instance}[i]{self.tcs_var_ref} != reference[vertex_ID].v)
                        local_pass = 0;
        }}
        pass[gl_InvocationID] = local_pass;
        gl_TessLevelOuter = float[4](1.0, 1.0, 1.0, 1.0);
        gl_TessLevelInner = float[2](1.0, 1.0);
}}

[tessellation evaluation shader]
#extension GL_ARB_tessellation_shader : require
layout(quads) in;

in int pass[];

out vec4 vert_color;

void main()
{{
        const vec4 red = vec4(1, 0, 0, 1);
        const vec4 green = vec4(0, 1, 0, 1);
        vec2[3] position = vec2[3](
                vec2(float(gl_PrimitiveID / 2) - 1.0, float(gl_PrimitiveID % 2) - 1.0),
                vec2(float(gl_PrimitiveID / 2) - 0.0, float(gl_PrimitiveID % 2) - 1.0),
                vec2(float(gl_PrimitiveID / 2) - 1.0, float(gl_PrimitiveID % 2) - 0.0)
        );
        gl_Position = vec4(position[0]
                    + (position[1] - position[0]) * gl_TessCoord[0]
                    + (position[2] - position[0]) * gl_TessCoord[1], 0.0, 1.0);
        vert_color = green;
        if (pass[0] == 0 || pass[1] == 0 || pass[2] == 0) {{
                vert_color = red;
        }}
}}

[fragment shader]

in vec4 vert_color;

out vec4 frag_color;

void main()
{{
        frag_color = vert_color;
}}

[test]
{self.uniform_string}
draw arrays GL_PATCHES 0 12
relative probe rgb (0.25, 0.25) (0.0, 1.0, 0.0)
relative probe rgb (0.75, 0.25) (0.0, 1.0, 0.0)
relative probe rgb (0.25, 0.75) (0.0, 1.0, 0.0)
relative probe rgb (0.75, 0.75) (0.0, 1.0, 0.0)
"""

        test = test.format(self=self, generator_command=" ".join(sys.argv))

        filename = self.filename()
        dirname = os.path.dirname(filename)
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        with open(filename, 'w') as f:
            f.write(test)


def all_tests():
    for type_name in ['float', 'vec2', 'vec3', 'vec4',
                      'mat2', 'mat3', 'mat4',
                      'mat2x3', 'mat2x4', 'mat3x2',
                      'mat3x4', 'mat4x2', 'mat4x3',
                      'int', 'ivec2', 'ivec3', 'ivec4',
                      'uint', 'uvec2', 'uvec3', 'uvec4']:
        for array in [None, 2]:
            yield Test(type_name=type_name, array=array, name=None)
    for var in ['gl_Position', 'gl_PointSize', 'gl_ClipDistance']:
        yield Test(type_name=None, array=None, name=var)


def main():
    for test in all_tests():
        test.generate()
        print(test.filename())


if __name__ == '__main__':
    main()