1# Copyright 2018 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
16from glslc_test_framework import inside_glslc_testsuite
17from placeholder import FileShader
18
19# A GLSL shader with inputs and outputs explicit locations.
20GLSL_SHADER_IO_WITHOUT_LOCATIONS = """#version 310 es
21  in vec4 m_in;
22  in vec4 m_in1;
23  out vec4 m_out;
24  out vec4 m_out1;
25  void main() {
26    m_out = m_in;
27    m_out1 = m_in1;
28  }"""
29
30
31# An HLSL fragment shader with inputs and outputs explicit locations.
32HLSL_SHADER_IO_WITHOUT_LOCATIONS = """
33  float4 Foo(float4 a, float4 b) : COLOR0 {
34    return a + b;
35  }"""
36
37
38@inside_glslc_testsuite('OptionFAutoMapLocations')
39class MissingLocationsResultsInError(expect.ErrorMessageSubstr):
40    """Tests that compilation fails when inputs or outputs have no location."""
41
42    shader = FileShader(GLSL_SHADER_IO_WITHOUT_LOCATIONS, '.vert')
43    glslc_args = ['-S', shader]
44    expected_error_substr = "SPIR-V requires location for user input/output"
45
46
47@inside_glslc_testsuite('OptionFAutoMapLocations')
48class FAutoMapLocationsGeneratesLocationsCheckInput(expect.ValidAssemblyFileWithSubstr):
49    """Tests that the compiler generates locations upon request:  Input 0"""
50
51    shader = FileShader(GLSL_SHADER_IO_WITHOUT_LOCATIONS, '.vert')
52    glslc_args = ['-S', shader, '-fauto-map-locations']
53    expected_assembly_substr = "OpDecorate %m_in Location 0"
54
55
56@inside_glslc_testsuite('OptionFAutoMapLocations')
57class FAutoMapLocationsGeneratesLocationsCheckOutput0(expect.ValidAssemblyFileWithSubstr):
58    """Tests that the compiler generates locations upon request:  Output 0"""
59
60    shader = FileShader(GLSL_SHADER_IO_WITHOUT_LOCATIONS, '.vert')
61    glslc_args = ['-S', shader, '-fauto-map-locations']
62    expected_assembly_substr = "OpDecorate %m_out Location 0"
63
64
65# Currently Glslang only generates Location 0.
66# See https://github.com/KhronosGroup/glslang/issues/1261
67# TODO(dneto): Write tests that check Location 1 is generated for inputs and
68# outputs.
69
70
71# Glslang's HLSL compiler automatically assigns locations inptus and outputs.
72@inside_glslc_testsuite('OptionFAutoMapLocations')
73class HLSLCompilerGeneratesLocationsCheckInput0(expect.ValidAssemblyFileWithSubstr):
74    """Tests that the HLSL compiler generates locations automatically: Input 0."""
75
76    shader = FileShader(HLSL_SHADER_IO_WITHOUT_LOCATIONS, '.hlsl')
77    glslc_args = ['-S', '-fshader-stage=frag', '-fentry-point=Foo', shader]
78    expected_assembly_substr = "OpDecorate %a Location 0"
79
80
81@inside_glslc_testsuite('OptionFAutoMapLocations')
82class HLSLCompilerGeneratesLocationsCheckOutput(expect.ValidAssemblyFileWithSubstr):
83    """Tests that the HLSL compiler generates locations automatically: Output."""
84
85    shader = FileShader(HLSL_SHADER_IO_WITHOUT_LOCATIONS, '.hlsl')
86    glslc_args = ['-S', '-fshader-stage=frag', '-fentry-point=Foo', shader]
87    expected_assembly_substr = "OpDecorate %_entryPointOutput Location 0"
88