aboutsummaryrefslogtreecommitdiff
path: root/glslc/test/option_std.py
blob: 52609afdfd56bfbeef9c461adf59a49cdf9fe608 (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
# Copyright 2015 The Shaderc Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import expect
from glslc_test_framework import inside_glslc_testsuite
from placeholder import FileShader


def core_vert_shader_without_version():
    # gl_ClipDistance doesn't exist in es profile (at least until 3.10).
    return 'void main() { gl_ClipDistance[0] = 5.; }'


def core_frag_shader_without_version():
    # gl_SampleID appears in core profile from 4.00.
    # gl_sampleID doesn't exsit in es profile (at least until 3.10).
    return 'void main() { int temp = gl_SampleID; }'


def hlsl_compute_shader_with_barriers():
    # Use "main" to avoid the need for -fentry-point
    return 'void main() { AllMemoryBarrierWithGroupSync(); }'


@inside_glslc_testsuite('OptionStd')
class TestStdNoArg(expect.ErrorMessage):
    """Tests -std alone."""

    glslc_args = ['-std']
    expected_error = ["glslc: error: unknown argument: '-std'\n"]


@inside_glslc_testsuite('OptionStd')
class TestStdEqNoArg(expect.ErrorMessage):
    """Tests -std= with no argument."""

    glslc_args = ['-std=']
    expected_error = ["glslc: error: invalid value '' in '-std='\n"]


@inside_glslc_testsuite('OptionStd')
class TestStdEqSpaceArg(expect.ErrorMessage):
    """Tests -std= <version-profile>."""

    shader = FileShader(core_frag_shader_without_version(), '.frag')
    glslc_args = ['-c', '-std=', '450core', shader]
    expected_error = ["glslc: error: invalid value '' in '-std='\n"]


# TODO(dneto): The error message changes with different versions of glslang.
@inside_glslc_testsuite('OptionStd')
class TestMissingVersionAndStd(expect.ErrorMessageSubstr):
    """Tests that missing both #version and -std results in errors."""

    shader = FileShader(core_frag_shader_without_version(), '.frag')
    glslc_args = ['-c', shader]
    expected_error_substr = ['error:']


@inside_glslc_testsuite('OptionStd')
class TestMissingVersionButHavingStd(expect.ValidObjectFile):
    """Tests that correct -std fixes missing #version."""

    shader = FileShader(core_frag_shader_without_version(), '.frag')
    glslc_args = ['-c', '-std=450core', shader]


@inside_glslc_testsuite('OptionStd')
class TestStdIgnoredInHlsl(expect.ValidObjectFile):
    """Tests HLSL compilation ignores -std."""

    # Compute shaders are not available in OpenGL 150
    shader = FileShader(hlsl_compute_shader_with_barriers(), '.comp')
    glslc_args = ['-c', '-x', 'hlsl', '-std=150', shader]


@inside_glslc_testsuite('OptionStd')
class TestMissingVersionAndWrongStd(expect.ErrorMessage):
    """Tests missing #version and wrong -std results in errors."""

    shader = FileShader(core_frag_shader_without_version(), '.frag')
    glslc_args = ['-c', '-std=310es', shader]
    expected_error = [
        shader, ":1: error: 'gl_SampleID' : required extension not requested: "
        'GL_OES_sample_variables\n1 error generated.\n']


@inside_glslc_testsuite('OptionStd')
class TestConflictingVersionAndStd(expect.ValidObjectFileWithWarning):
    """Tests that with both #version and -std, -std takes precedence."""

    # Wrong #version here on purpose.
    shader = FileShader(
        '#version 310 es\n' + core_frag_shader_without_version(), '.frag')
    # -std overwrites the wrong #version.
    glslc_args = ['-c', '-std=450core', shader]

    expected_warning = [
        shader, ': warning: (version, profile) forced to be (450, core), while '
        'in source code it is (310, es)\n1 warning generated.\n']


@inside_glslc_testsuite('OptionStd')
class TestMultipleStd(expect.ValidObjectFile):
    """Tests that for multiple -std, the last one takes effect."""

    shader = FileShader(core_frag_shader_without_version(), '.frag')
    glslc_args = ['-c', '-std=100', '-std=310es', shader, '-std=450core']


@inside_glslc_testsuite('OptionStd')
class TestMultipleFiles(expect.ValidObjectFileWithWarning):
    """Tests that -std covers all files."""

    shader1 = FileShader(core_frag_shader_without_version(), '.frag')
    shader2 = FileShader(core_vert_shader_without_version(), '.vert')
    shader3 = FileShader(
        '#version 310 es\n' + core_frag_shader_without_version(), '.frag')
    glslc_args = ['-c', '-std=450core', shader1, shader2, shader3]

    expected_warning = [
        shader3, ': warning: (version, profile) forced to be (450, '
        'core), while in source code it is (310, es)\n'
        '1 warning generated.\n']


@inside_glslc_testsuite('OptionStd')
class TestUnkownProfile(expect.ErrorMessage):
    """Tests that -std rejects unknown profile."""

    shader = FileShader(core_frag_shader_without_version(), '.frag')
    glslc_args = ['-c', '-std=450google', shader]
    expected_error = [
        "glslc: error: invalid value '450google' in '-std=450google'\n"]


@inside_glslc_testsuite('OptionStd')
class TestUnkownVersion(expect.ErrorMessage):
    """Tests that -std rejects unknown version."""

    shader = FileShader(core_frag_shader_without_version(), '.frag')
    glslc_args = ['-c', '-std=42core', shader]
    expected_error = [
        "glslc: error: invalid value '42core' in '-std=42core'\n"]


@inside_glslc_testsuite('OptionStd')
class TestTotallyWrongStdValue(expect.ErrorMessage):
    """Tests that -std rejects totally wrong -std value."""

    shader = FileShader(core_vert_shader_without_version(), '.vert')
    glslc_args = ['-c', '-std=wrong42', shader]

    expected_error = [
        "glslc: error: invalid value 'wrong42' in '-std=wrong42'\n"]


@inside_glslc_testsuite('OptionStd')
class TestVersionInsideSlashSlashComment(expect.ValidObjectFileWithWarning):
    """Tests that -std substitutes the correct #version string."""

    # The second #version string should be substituted and this shader
    # should compile successfully with -std=450core.
    shader = FileShader(
        '// #version 310 es\n#version 310 es\n' +
        core_vert_shader_without_version(), '.vert')
    glslc_args = ['-c', '-std=450core', shader]

    expected_warning = [
        shader, ': warning: (version, profile) forced to be (450, core), while '
        'in source code it is (310, es)\n1 warning generated.\n']


@inside_glslc_testsuite('OptionStd')
class TestVersionInsideSlashStarComment(expect.ValidObjectFileWithWarning):
    """Tests that -std substitutes the correct #version string."""

    # The second #version string should be substituted and this shader
    # should compile successfully with -std=450core.
    shader = FileShader(
        '/* #version 310 es */\n#version 310 es\n' +
        core_vert_shader_without_version(), '.vert')
    glslc_args = ['-c', '-std=450core', shader]

    expected_warning = [
        shader, ': warning: (version, profile) forced to be (450, core), while '
        'in source code it is (310, es)\n1 warning generated.\n']


@inside_glslc_testsuite('OptionStd')
class TestCommentBeforeVersion(expect.ValidObjectFileWithWarning):
    """Tests that comments before #version (same line) is correctly handled."""

    shader = FileShader(
        '/* some comment */ #version 150\n' +
        core_vert_shader_without_version(), '.vert')
    glslc_args = ['-c', '-std=450', shader]

    expected_warning = [
        shader, ': warning: (version, profile) forced to be (450, none), while '
        'in source code it is (150, none)\n1 warning generated.\n']


@inside_glslc_testsuite('OptionStd')
class TestCommentAfterVersion(expect.ValidObjectFileWithWarning):
    """Tests that multiple-line comments after #version is correctly handled."""

    shader = FileShader(
        '#version 150 compatibility ' +
        '/* start \n second line \n end */\n' +
        core_vert_shader_without_version(), '.vert')
    glslc_args = ['-c', '-std=450core', shader]

    expected_warning = [
        shader, ': warning: (version, profile) forced to be (450, core), while '
        'in source code it is (150, compatibility)\n1 warning generated.\n']


# The following test case is disabled because of a bug in glslang.
# When checking non-newline whitespaces, glslang only recognizes
# ' ' and '\t', leaving '\v' and '\f' unhandled. The following test
# case exposes this problem. It should be turned on once a fix for
# glslang is landed.
#@inside_glslc_testsuite('OptionStd')
class TestSpaceAroundVersion(expect.ValidObjectFileWithWarning):
    """Tests that space around #version is correctly handled."""

    shader = FileShader(
        '\t   \t  # \t \f\f version  \v \t\t  310 \v\v \t  es \n' +
        core_vert_shader_without_version(), '.vert')
    glslc_args = ['-c', '-std=450core', shader]

    expected_warning = [
        shader, ': warning: (version, profile) forced to be (450, core), while '
        'in source code it is (310, es)\n1 warning generated.\n']


@inside_glslc_testsuite('OptionStd')
class TestVersionInsideCrazyComment(expect.ValidObjectFileWithWarning):
    """Tests that -std substitutes the correct #version string."""

    # The fourth #version string should be substituted and this shader
    # should compile successfully with -std=450core.
    shader = FileShader(
        '/* */ /* // /* #version 310 es */\n' +  # /*-style comment
        '// /* */ /* /* // #version 310 es\n' +  # //-style comment
        '///*////*//*/*/ #version 310 es*/\n' +  # //-style comment
        '#version 310 es\n' + core_vert_shader_without_version(), '.vert')
    glslc_args = ['-c', '-std=450core', shader]

    expected_warning = [
        shader, ': warning: (version, profile) forced to be (450, core), while '
        'in source code it is (310, es)\n1 warning generated.\n']


@inside_glslc_testsuite('OptionStd')
class TestVersionMissingProfile(expect.ErrorMessage):
    """Tests that missing required profile in -std results in an error."""

    shader = FileShader('#version 140\nvoid main() {}', '.vert')
    glslc_args = ['-c', '-std=310', shader]

    expected_error = [
        shader, ': error: #version: versions 300, 310, and 320 require ',
        "specifying the 'es' profile\n1 error generated.\n"]


@inside_glslc_testsuite('OptionStd')
class TestVersionRedundantProfile(expect.ErrorMessageSubstr):
    """Tests that adding non-required profile in -std results in an error."""

    shader = FileShader('#version 140\nvoid main() {}', '.vert')
    glslc_args = ['-c', '-std=100core', shader]

    expected_error_substr = [
        shader, ': error: #version: versions before 150 do not allow '
        'a profile token\n']