1# Copyright 2015 The Shaderc Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import expect
16import os.path
17from glslc_test_framework import inside_glslc_testsuite
18from placeholder import FileShader, StdinShader, TempFileName
19
20
21@inside_glslc_testsuite('File')
22class SimpleFileCompiled(expect.ValidObjectFile):
23    """Tests whether or not a simple glsl file compiles."""
24
25    shader = FileShader('#version 310 es\nvoid main() {}', '.frag')
26    glslc_args = ['-c', shader]
27
28
29@inside_glslc_testsuite('File')
30class NotSpecifyingOutputName(expect.SuccessfulReturn,
31                              expect.CorrectObjectFilePreamble):
32    """Tests that when there is no -o and -E/-S/-c specified, output as a.spv."""
33
34    shader = FileShader('#version 140\nvoid main() {}', '.frag')
35    glslc_args = [shader]
36
37    def check_output_a_spv(self, status):
38        output_name = os.path.join(status.directory, 'a.spv')
39        return self.verify_object_file_preamble(output_name)
40
41
42@inside_glslc_testsuite('Parameters')
43class HelpParameters(
44    expect.ReturnCodeIsZero, expect.StdoutMatch, expect.StderrMatch):
45    """Tests the --help flag outputs correctly and does not produce and error."""
46
47    glslc_args = ['--help']
48
49    expected_stdout = '''glslc - Compile shaders into SPIR-V
50
51Usage: glslc [options] file...
52
53An input file of - represents standard input.
54
55Options:
56  -c                Only run preprocess, compile, and assemble steps.
57  -Dmacro[=defn]    Add an implicit macro definition.
58  -E                Outputs only the results of the preprocessing step.
59                    Output defaults to standard output.
60  -fauto-bind-uniforms
61                    Automatically assign bindings to uniform variables that
62                    don't have an explicit 'binding' layout in the shader
63                    source.
64  -fauto-map-locations
65                    Automatically assign locations to uniform variables that
66                    don't have an explicit 'location' layout in the shader
67                    source.
68  -fauto-combined-image-sampler
69                    Removes sampler variables and converts existing textures
70                    to combined image-samplers.
71  -fentry-point=<name>
72                    Specify the entry point name for HLSL compilation, for
73                    all subsequent source files.  Default is "main".
74  -fhlsl_functionality1, -fhlsl-functionality1
75                    Enable extension SPV_GOOGLE_hlsl_functionality1 for HLSL
76                    compilation.
77  -finvert-y        Invert position.Y output in vertex shader.
78  -fhlsl-iomap      Use HLSL IO mappings for bindings.
79  -fhlsl-offsets    Use HLSL offset rules for packing members of blocks.
80                    Affects only GLSL.  HLSL rules are always used for HLSL.
81  -flimit=<settings>
82                    Specify resource limits. Each limit is specified by a limit
83                    name followed by an integer value.  Tokens should be
84                    separated by whitespace.  If the same limit is specified
85                    several times, only the last setting takes effect.
86  -flimit-file <file>
87                    Set limits as specified in the given file.
88  -fnan-clamp       Generate code for max and min builtins so that, when given
89                    a NaN operand, the other operand is returned. Similarly,
90                    the clamp builtin will favour the non-NaN operands, as if
91                    clamp were implemented as a composition of max and min.
92  -fresource-set-binding [stage] <reg0> <set0> <binding0>
93                        [<reg1> <set1> <binding1>...]
94                    Explicitly sets the descriptor set and binding for
95                    HLSL resources, by register name.  Optionally restrict
96                    it to a single stage.
97  -fcbuffer-binding-base [stage] <value>
98                    Same as -fubo-binding-base.
99  -fimage-binding-base [stage] <value>
100                    Sets the lowest automatically assigned binding number for
101                    images.  Optionally only set it for a single shader stage.
102                    For HLSL, the resource register number is added to this
103                    base.
104  -fsampler-binding-base [stage] <value>
105                    Sets the lowest automatically assigned binding number for
106                    samplers  Optionally only set it for a single shader stage.
107                    For HLSL, the resource register number is added to this
108                    base.
109  -fssbo-binding-base [stage] <value>
110                    Sets the lowest automatically assigned binding number for
111                    shader storage buffer objects (SSBO).  Optionally only set
112                    it for a single shader stage.  Only affects GLSL.
113  -ftexture-binding-base [stage] <value>
114                    Sets the lowest automatically assigned binding number for
115                    textures.  Optionally only set it for a single shader stage.
116                    For HLSL, the resource register number is added to this
117                    base.
118  -fuav-binding-base [stage] <value>
119                    For automatically assigned bindings for unordered access
120                    views (UAV), the register number is added to this base to
121                    determine the binding number.  Optionally only set it for
122                    a single shader stage.  Only affects HLSL.
123  -fubo-binding-base [stage] <value>
124                    Sets the lowest automatically assigned binding number for
125                    uniform buffer objects (UBO).  Optionally only set it for
126                    a single shader stage.
127                    For HLSL, the resource register number is added to this
128                    base.
129  -fshader-stage=<stage>
130                    Treat subsequent input files as having stage <stage>.
131                    Valid stages are vertex, vert, fragment, frag, tesscontrol,
132                    tesc, tesseval, tese, geometry, geom, compute, and comp.
133  -g                Generate source-level debug information.
134  -h                Display available options.
135  --help            Display available options.
136  -I <value>        Add directory to include search path.
137  -mfmt=<format>    Output SPIR-V binary code using the selected format. This
138                    option may be specified only when the compilation output is
139                    in SPIR-V binary code form. Available options are:
140                      bin   - SPIR-V binary words.  This is the default.
141                      c     - Binary words as C initializer list of 32-bit ints
142                      num   - List of comma-separated 32-bit hex integers
143  -M                Generate make dependencies. Implies -E and -w.
144  -MM               An alias for -M.
145  -MD               Generate make dependencies and compile.
146  -MF <file>        Write dependency output to the given file.
147  -MT <target>      Specify the target of the rule emitted by dependency
148                    generation.
149  -O                Optimize the generated SPIR-V code for better performance.
150  -Os               Optimize the generated SPIR-V code for smaller size.
151  -O0               Disable optimization.
152  -o <file>         Write output to <file>.
153                    A file name of '-' represents standard output.
154  -std=<value>      Version and profile for GLSL input files. Possible values
155                    are concatenations of version and profile, e.g. 310es,
156                    450core, etc.  Ignored for HLSL files.
157  -S                Emit SPIR-V assembly instead of binary.
158  --show-limits     Display available limit names and their default values.
159  --target-env=<environment>
160                    Set the target client environment, and the semantics
161                    of warnings and errors.  An optional suffix can specify
162                    the client version.  Values are:
163                        vulkan1.0       # The default
164                        vulkan1.1
165                        vulkan1.2
166                        vulkan          # Same as vulkan1.0
167                        opengl4.5
168                        opengl          # Same as opengl4.5
169  --target-spv=<spirv-version>
170                    Set the SPIR-V version to be used for the generated SPIR-V
171                    module.  The default is the highest version of SPIR-V
172                    required to be supported for the target environment.
173                    For example, default for vulkan1.0 is spv1.0, and
174                    the default for vulkan1.1 is spv1.3,
175                    the default for vulkan1.2 is spv1.5.
176                    Values are:
177                        spv1.0, spv1.1, spv1.2, spv1.3, spv1.4, spv1.5
178  --version         Display compiler version information.
179  -w                Suppresses all warning messages.
180  -Werror           Treat all warnings as errors.
181  -x <language>     Treat subsequent input files as having type <language>.
182                    Valid languages are: glsl, hlsl.
183                    For files ending in .hlsl the default is hlsl.
184                    Otherwise the default is glsl.
185'''
186
187    expected_stderr = ''
188
189
190@inside_glslc_testsuite('Parameters')
191class HelpIsNotTooWide(expect.StdoutNoWiderThan80Columns):
192    """Tests that --help output is not too wide."""
193
194    glslc_args = ['--help']
195
196
197@inside_glslc_testsuite('Parameters')
198class UnknownSingleLetterArgument(expect.ErrorMessage):
199    """Tests that an unknown argument triggers an error message."""
200
201    glslc_args = ['-a']
202    expected_error = ["glslc: error: unknown argument: '-a'\n"]
203
204
205@inside_glslc_testsuite('Parameters')
206class UnknownMultiLetterArgument(expect.ErrorMessage):
207    """Tests that an unknown argument triggers an error message."""
208
209    glslc_args = ['-zzz']
210    expected_error = ["glslc: error: unknown argument: '-zzz'\n"]
211
212
213@inside_glslc_testsuite('Parameters')
214class UnsupportedOption(expect.ErrorMessage):
215    """Tests that an unsupported option triggers an error message."""
216
217    glslc_args = ['--unsupported-option']
218    expected_error = [
219        "glslc: error: unsupported option: '--unsupported-option'\n"]
220
221
222@inside_glslc_testsuite('File')
223class FileNotFound(expect.ErrorMessage):
224    """Tests the error message if a file cannot be found."""
225
226    blabla_file = TempFileName('blabla.frag')
227    glslc_args = [blabla_file]
228    expected_error = [
229        "glslc: error: cannot open input file: '", blabla_file,
230        "': No such file or directory\n"]
231
232
233@inside_glslc_testsuite('Unsupported')
234class LinkingNotSupported(expect.ErrorMessage):
235    """Tests the error message generated by linking not supported yet."""
236
237    shader1 = FileShader('#version 140\nvoid main() {}', '.vert')
238    shader2 = FileShader('#version 140\nvoid main() {}', '.frag')
239    glslc_args = [shader1, shader2]
240    expected_error = [
241        'glslc: error: linking multiple files is not supported yet. ',
242        'Use -c to compile files individually.\n']
243
244
245@inside_glslc_testsuite('Unsupported')
246class MultipleStdinUnsupported(expect.ErrorMessage):
247    """Tests the error message generated by having more than one - input."""
248
249    glslc_args = ['-c', '-fshader-stage=vertex', '-', '-']
250    expected_error = [
251        'glslc: error: specifying standard input "-" as input more'
252        ' than once is not allowed.\n']
253
254
255@inside_glslc_testsuite('Parameters')
256class StdinWithoutShaderStage(expect.StdoutMatch, expect.StderrMatch):
257    """Tests that you must use -fshader-stage when specifying - as input."""
258    shader = StdinShader(
259        """#version 140
260    int a() {
261    }
262    void main() {
263      int x = a();
264    }
265    """)
266    glslc_args = [shader]
267
268    expected_stdout = ''
269    expected_stderr = [
270        "glslc: error: '-': -fshader-stage required when input is from "
271        'standard input "-"\n']
272
273
274@inside_glslc_testsuite('Parameters')
275class LimitsHelp(expect.StdoutMatch, expect.StderrMatch):
276    """Tests --show-limits shows correct output."""
277
278    glslc_args = ['--show-limits']
279
280    expected_stderr = ''
281    expected_stdout = """MaxLights 8
282MaxClipPlanes 6
283MaxTextureUnits 2
284MaxTextureCoords 8
285MaxVertexAttribs 16
286MaxVertexUniformComponents 4096
287MaxVaryingFloats 60
288MaxVertexTextureImageUnits 16
289MaxCombinedTextureImageUnits 80
290MaxTextureImageUnits 16
291MaxFragmentUniformComponents 1024
292MaxDrawBuffers 8
293MaxVertexUniformVectors 256
294MaxVaryingVectors 15
295MaxFragmentUniformVectors 256
296MaxVertexOutputVectors 16
297MaxFragmentInputVectors 15
298MinProgramTexelOffset -8
299MaxProgramTexelOffset 7
300MaxClipDistances 8
301MaxComputeWorkGroupCountX 65535
302MaxComputeWorkGroupCountY 65535
303MaxComputeWorkGroupCountZ 65535
304MaxComputeWorkGroupSizeX 1024
305MaxComputeWorkGroupSizeY 1024
306MaxComputeWorkGroupSizeZ 64
307MaxComputeUniformComponents 512
308MaxComputeTextureImageUnits 16
309MaxComputeImageUniforms 8
310MaxComputeAtomicCounters 8
311MaxComputeAtomicCounterBuffers 1
312MaxVaryingComponents 60
313MaxVertexOutputComponents 64
314MaxGeometryInputComponents 64
315MaxGeometryOutputComponents 128
316MaxFragmentInputComponents 128
317MaxImageUnits 8
318MaxCombinedImageUnitsAndFragmentOutputs 8
319MaxCombinedShaderOutputResources 8
320MaxImageSamples 0
321MaxVertexImageUniforms 0
322MaxTessControlImageUniforms 0
323MaxTessEvaluationImageUniforms 0
324MaxGeometryImageUniforms 0
325MaxFragmentImageUniforms 8
326MaxCombinedImageUniforms 8
327MaxGeometryTextureImageUnits 16
328MaxGeometryOutputVertices 256
329MaxGeometryTotalOutputComponents 1024
330MaxGeometryUniformComponents 512
331MaxGeometryVaryingComponents 60
332MaxTessControlInputComponents 128
333MaxTessControlOutputComponents 128
334MaxTessControlTextureImageUnits 16
335MaxTessControlUniformComponents 1024
336MaxTessControlTotalOutputComponents 4096
337MaxTessEvaluationInputComponents 128
338MaxTessEvaluationOutputComponents 128
339MaxTessEvaluationTextureImageUnits 16
340MaxTessEvaluationUniformComponents 1024
341MaxTessPatchComponents 120
342MaxPatchVertices 32
343MaxTessGenLevel 64
344MaxViewports 16
345MaxVertexAtomicCounters 0
346MaxTessControlAtomicCounters 0
347MaxTessEvaluationAtomicCounters 0
348MaxGeometryAtomicCounters 0
349MaxFragmentAtomicCounters 8
350MaxCombinedAtomicCounters 8
351MaxAtomicCounterBindings 1
352MaxVertexAtomicCounterBuffers 0
353MaxTessControlAtomicCounterBuffers 0
354MaxTessEvaluationAtomicCounterBuffers 0
355MaxGeometryAtomicCounterBuffers 0
356MaxFragmentAtomicCounterBuffers 0
357MaxCombinedAtomicCounterBuffers 1
358MaxAtomicCounterBufferSize 32
359MaxTransformFeedbackBuffers 4
360MaxTransformFeedbackInterleavedComponents 64
361MaxCullDistances 8
362MaxCombinedClipAndCullDistances 8
363MaxSamples 4
364"""
365