1617a3babSopenharmony_ci//
2617a3babSopenharmony_ci// Copyright (C) 2016-2017 Google, Inc.
3617a3babSopenharmony_ci//
4617a3babSopenharmony_ci// All rights reserved.
5617a3babSopenharmony_ci//
6617a3babSopenharmony_ci// Redistribution and use in source and binary forms, with or without
7617a3babSopenharmony_ci// modification, are permitted provided that the following conditions
8617a3babSopenharmony_ci// are met:
9617a3babSopenharmony_ci//
10617a3babSopenharmony_ci//    Redistributions of source code must retain the above copyright
11617a3babSopenharmony_ci//    notice, this list of conditions and the following disclaimer.
12617a3babSopenharmony_ci//
13617a3babSopenharmony_ci//    Redistributions in binary form must reproduce the above
14617a3babSopenharmony_ci//    copyright notice, this list of conditions and the following
15617a3babSopenharmony_ci//    disclaimer in the documentation and/or other materials provided
16617a3babSopenharmony_ci//    with the distribution.
17617a3babSopenharmony_ci//
18617a3babSopenharmony_ci//    Neither the name of Google Inc. nor the names of its
19617a3babSopenharmony_ci//    contributors may be used to endorse or promote products derived
20617a3babSopenharmony_ci//    from this software without specific prior written permission.
21617a3babSopenharmony_ci//
22617a3babSopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23617a3babSopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24617a3babSopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25617a3babSopenharmony_ci// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26617a3babSopenharmony_ci// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27617a3babSopenharmony_ci// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28617a3babSopenharmony_ci// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29617a3babSopenharmony_ci// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30617a3babSopenharmony_ci// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31617a3babSopenharmony_ci// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32617a3babSopenharmony_ci// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33617a3babSopenharmony_ci// POSSIBILITY OF SUCH DAMAGE.
34617a3babSopenharmony_ci
35617a3babSopenharmony_ci#include <memory>
36617a3babSopenharmony_ci
37617a3babSopenharmony_ci#include <gtest/gtest.h>
38617a3babSopenharmony_ci
39617a3babSopenharmony_ci#include "TestFixture.h"
40617a3babSopenharmony_ci
41617a3babSopenharmony_cinamespace glslangtest {
42617a3babSopenharmony_cinamespace {
43617a3babSopenharmony_ci
44617a3babSopenharmony_ciusing LinkTestVulkan = GlslangTest<
45617a3babSopenharmony_ci    ::testing::TestWithParam<std::vector<std::string>>>;
46617a3babSopenharmony_ci
47617a3babSopenharmony_ciTEST_P(LinkTestVulkan, FromFile)
48617a3babSopenharmony_ci{
49617a3babSopenharmony_ci    const auto& fileNames = GetParam();
50617a3babSopenharmony_ci    const size_t fileCount = fileNames.size();
51617a3babSopenharmony_ci    const EShMessages controls = DeriveOptions(Source::GLSL, Semantics::Vulkan, Target::AST);
52617a3babSopenharmony_ci    GlslangResult result;
53617a3babSopenharmony_ci    result.validationResult = false;
54617a3babSopenharmony_ci
55617a3babSopenharmony_ci    // Compile each input shader file.
56617a3babSopenharmony_ci    bool success = true;
57617a3babSopenharmony_ci    std::vector<std::unique_ptr<glslang::TShader>> shaders;
58617a3babSopenharmony_ci    for (size_t i = 0; i < fileCount; ++i) {
59617a3babSopenharmony_ci        std::string contents;
60617a3babSopenharmony_ci        tryLoadFile(GlobalTestSettings.testRoot + "/" + fileNames[i],
61617a3babSopenharmony_ci                    "input", &contents);
62617a3babSopenharmony_ci        shaders.emplace_back(
63617a3babSopenharmony_ci                new glslang::TShader(GetShaderStage(GetSuffix(fileNames[i]))));
64617a3babSopenharmony_ci        auto* shader = shaders.back().get();
65617a3babSopenharmony_ci        shader->setAutoMapLocations(true);
66617a3babSopenharmony_ci        success &= compile(shader, contents, "", controls);
67617a3babSopenharmony_ci        result.shaderResults.push_back(
68617a3babSopenharmony_ci            {fileNames[i], shader->getInfoLog(), shader->getInfoDebugLog()});
69617a3babSopenharmony_ci    }
70617a3babSopenharmony_ci
71617a3babSopenharmony_ci    // Link all of them.
72617a3babSopenharmony_ci    glslang::TProgram program;
73617a3babSopenharmony_ci    for (const auto& shader : shaders) program.addShader(shader.get());
74617a3babSopenharmony_ci    success &= program.link(controls);
75617a3babSopenharmony_ci    result.linkingOutput = program.getInfoLog();
76617a3babSopenharmony_ci    result.linkingError = program.getInfoDebugLog();
77617a3babSopenharmony_ci
78617a3babSopenharmony_ci    if (success)
79617a3babSopenharmony_ci        program.mapIO();
80617a3babSopenharmony_ci
81617a3babSopenharmony_ci    if (success && (controls & EShMsgSpvRules)) {
82617a3babSopenharmony_ci        spv::SpvBuildLogger logger;
83617a3babSopenharmony_ci        std::vector<uint32_t> spirv_binary;
84617a3babSopenharmony_ci        options().disableOptimizer = true;
85617a3babSopenharmony_ci        glslang::GlslangToSpv(*program.getIntermediate(shaders.front()->getStage()),
86617a3babSopenharmony_ci                                spirv_binary, &logger, &options());
87617a3babSopenharmony_ci
88617a3babSopenharmony_ci        std::ostringstream disassembly_stream;
89617a3babSopenharmony_ci        spv::Parameterize();
90617a3babSopenharmony_ci        spv::Disassemble(disassembly_stream, spirv_binary);
91617a3babSopenharmony_ci        result.spirvWarningsErrors = logger.getAllMessages();
92617a3babSopenharmony_ci        result.spirv = disassembly_stream.str();
93617a3babSopenharmony_ci        result.validationResult = !options().validate || logger.getAllMessages().empty();
94617a3babSopenharmony_ci    }
95617a3babSopenharmony_ci
96617a3babSopenharmony_ci    std::ostringstream stream;
97617a3babSopenharmony_ci    outputResultToStream(&stream, result, controls);
98617a3babSopenharmony_ci
99617a3babSopenharmony_ci    // Check with expected results.
100617a3babSopenharmony_ci    const std::string expectedOutputFname =
101617a3babSopenharmony_ci        GlobalTestSettings.testRoot + "/baseResults/" + fileNames.front() + ".out";
102617a3babSopenharmony_ci    std::string expectedOutput;
103617a3babSopenharmony_ci    tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
104617a3babSopenharmony_ci
105617a3babSopenharmony_ci    checkEqAndUpdateIfRequested(expectedOutput, stream.str(), expectedOutputFname,
106617a3babSopenharmony_ci                                result.spirvWarningsErrors);
107617a3babSopenharmony_ci}
108617a3babSopenharmony_ci
109617a3babSopenharmony_ci// clang-format off
110617a3babSopenharmony_ciINSTANTIATE_TEST_SUITE_P(
111617a3babSopenharmony_ci    Glsl, LinkTestVulkan,
112617a3babSopenharmony_ci    ::testing::ValuesIn(std::vector<std::vector<std::string>>({
113617a3babSopenharmony_ci        {"link1.vk.frag", "link2.vk.frag"},
114617a3babSopenharmony_ci        {"spv.unit1.frag", "spv.unit2.frag", "spv.unit3.frag"},
115617a3babSopenharmony_ci        {"link.vk.matchingPC.0.0.frag", "link.vk.matchingPC.0.1.frag",
116617a3babSopenharmony_ci            "link.vk.matchingPC.0.2.frag"},
117617a3babSopenharmony_ci           {"link.vk.differentPC.0.0.frag", "link.vk.differentPC.0.1.frag",
118617a3babSopenharmony_ci            "link.vk.differentPC.0.2.frag"},
119617a3babSopenharmony_ci        {"link.vk.differentPC.1.0.frag", "link.vk.differentPC.1.1.frag",
120617a3babSopenharmony_ci            "link.vk.differentPC.1.2.frag"},
121617a3babSopenharmony_ci        {"link.vk.pcNamingValid.0.0.vert", "link.vk.pcNamingValid.0.1.vert"},
122617a3babSopenharmony_ci        {"link.vk.pcNamingInvalid.0.0.vert", "link.vk.pcNamingInvalid.0.1.vert"},
123617a3babSopenharmony_ci        {"link.vk.multiBlocksValid.0.0.vert", "link.vk.multiBlocksValid.0.1.vert"},
124617a3babSopenharmony_ci        {"link.vk.multiBlocksValid.1.0.geom", "link.vk.multiBlocksValid.1.1.geom"},
125617a3babSopenharmony_ci        {"link.vk.inconsistentGLPerVertex.0.vert", "link.vk.inconsistentGLPerVertex.0.geom"},
126617a3babSopenharmony_ci    }))
127617a3babSopenharmony_ci);
128617a3babSopenharmony_ci// clang-format on
129617a3babSopenharmony_ci
130617a3babSopenharmony_ci}  // anonymous namespace
131617a3babSopenharmony_ci}  // namespace glslangtest
132