Merge pull request #293 from antiagainst/link-tests
Testing: Add link tests in the GTest framework
This commit is contained in:
commit
e3e92d32bc
@ -14,6 +14,7 @@ if (TARGET gmock)
|
|||||||
${CMAKE_CURRENT_SOURCE_DIR}/AST.FromFile.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/AST.FromFile.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/BuiltInResource.FromFile.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/BuiltInResource.FromFile.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/Hlsl.FromFile.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/Hlsl.FromFile.cpp
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/Link.FromFile.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/Pp.FromFile.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/Pp.FromFile.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/Spv.FromFile.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/Spv.FromFile.cpp
|
||||||
)
|
)
|
||||||
|
@ -59,7 +59,7 @@ std::string FileNameAsCustomTestSuffix(
|
|||||||
using HlslCompileTest = GlslangTest<::testing::TestWithParam<FileNameEntryPointPair>>;
|
using HlslCompileTest = GlslangTest<::testing::TestWithParam<FileNameEntryPointPair>>;
|
||||||
|
|
||||||
// Compiling HLSL to SPIR-V under Vulkan semantics. Expected to successfully
|
// Compiling HLSL to SPIR-V under Vulkan semantics. Expected to successfully
|
||||||
// generate SPIR-V.
|
// generate both AST and SPIR-V.
|
||||||
TEST_P(HlslCompileTest, FromFile)
|
TEST_P(HlslCompileTest, FromFile)
|
||||||
{
|
{
|
||||||
loadFileCompileAndCheck(GLSLANG_TEST_DIRECTORY, GetParam().fileName,
|
loadFileCompileAndCheck(GLSLANG_TEST_DIRECTORY, GetParam().fileName,
|
||||||
|
107
gtests/Link.FromFile.cpp
Normal file
107
gtests/Link.FromFile.cpp
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
//
|
||||||
|
// Copyright (C) 2016 Google, Inc.
|
||||||
|
//
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// Redistribution and use in source and binary forms, with or without
|
||||||
|
// modification, are permitted provided that the following conditions
|
||||||
|
// are met:
|
||||||
|
//
|
||||||
|
// Redistributions of source code must retain the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer.
|
||||||
|
//
|
||||||
|
// Redistributions in binary form must reproduce the above
|
||||||
|
// copyright notice, this list of conditions and the following
|
||||||
|
// disclaimer in the documentation and/or other materials provided
|
||||||
|
// with the distribution.
|
||||||
|
//
|
||||||
|
// Neither the name of Google Inc. nor the names of its
|
||||||
|
// contributors may be used to endorse or promote products derived
|
||||||
|
// from this software without specific prior written permission.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||||
|
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
// POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include "TestFixture.h"
|
||||||
|
|
||||||
|
namespace glslangtest {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using LinkTest = GlslangTest<
|
||||||
|
::testing::TestWithParam<std::vector<std::string>>>;
|
||||||
|
|
||||||
|
TEST_P(LinkTest, FromFile)
|
||||||
|
{
|
||||||
|
const auto& fileNames = GetParam();
|
||||||
|
const size_t fileCount = fileNames.size();
|
||||||
|
const EShMessages controls = DeriveOptions(
|
||||||
|
Source::GLSL, Semantics::OpenGL, Target::AST);
|
||||||
|
GlslangResult result;
|
||||||
|
|
||||||
|
// Compile each input shader file.
|
||||||
|
std::vector<std::unique_ptr<glslang::TShader>> shaders;
|
||||||
|
for (size_t i = 0; i < fileCount; ++i) {
|
||||||
|
std::string contents;
|
||||||
|
tryLoadFile(GLSLANG_TEST_DIRECTORY "/" + fileNames[i],
|
||||||
|
"input", &contents);
|
||||||
|
shaders.emplace_back(
|
||||||
|
new glslang::TShader(GetShaderStage(GetSuffix(fileNames[i]))));
|
||||||
|
auto* shader = shaders.back().get();
|
||||||
|
compile(shader, contents, "", controls);
|
||||||
|
result.shaderResults.push_back(
|
||||||
|
{fileNames[i], shader->getInfoLog(), shader->getInfoDebugLog()});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Link all of them.
|
||||||
|
glslang::TProgram program;
|
||||||
|
for (const auto& shader : shaders) program.addShader(shader.get());
|
||||||
|
program.link(controls);
|
||||||
|
result.linkingOutput = program.getInfoLog();
|
||||||
|
result.linkingError = program.getInfoDebugLog();
|
||||||
|
|
||||||
|
std::ostringstream stream;
|
||||||
|
outputResultToStream(&stream, result, controls);
|
||||||
|
|
||||||
|
// Check with expected results.
|
||||||
|
const std::string expectedOutputFname =
|
||||||
|
GLSLANG_TEST_DIRECTORY "/baseResults/" + fileNames.front() + ".out";
|
||||||
|
std::string expectedOutput;
|
||||||
|
tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
|
||||||
|
|
||||||
|
checkEqAndUpdateIfRequested(expectedOutput, stream.str(), expectedOutputFname);
|
||||||
|
}
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
INSTANTIATE_TEST_CASE_P(
|
||||||
|
Glsl, LinkTest,
|
||||||
|
::testing::ValuesIn(std::vector<std::vector<std::string>>({
|
||||||
|
{"mains1.frag", "mains2.frag", "noMain1.geom", "noMain2.geom"},
|
||||||
|
{"noMain.vert", "mains.frag"},
|
||||||
|
{"link1.frag", "link2.frag", "link3.frag"},
|
||||||
|
{"recurse1.vert", "recurse1.frag", "recurse2.frag"},
|
||||||
|
{"300link.frag"},
|
||||||
|
{"300link2.frag"},
|
||||||
|
{"300link3.frag"},
|
||||||
|
{"empty.frag", "empty2.frag", "empty3.frag"},
|
||||||
|
{"150.tesc", "150.tese", "400.tesc", "400.tese", "410.tesc", "420.tesc", "420.tese"},
|
||||||
|
{"max_vertices_0.geom"},
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
} // anonymous namespace
|
||||||
|
} // namespace glslangtest
|
@ -129,14 +129,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Checks the equality of |expected| and |real|. If they are not equal,
|
// Checks the equality of |expected| and |real|. If they are not equal,
|
||||||
// write
|
// write |real| to the given file named as |fname| if update mode is on.
|
||||||
// |real| to the given file named as |fname| if update mode is on.
|
|
||||||
void checkEqAndUpdateIfRequested(const std::string& expected,
|
void checkEqAndUpdateIfRequested(const std::string& expected,
|
||||||
const std::string& real,
|
const std::string& real,
|
||||||
const std::string& fname)
|
const std::string& fname)
|
||||||
{
|
{
|
||||||
// In order to output the message we want under proper circumstances, we
|
// In order to output the message we want under proper circumstances,
|
||||||
// need the following operator<< stuff.
|
// we need the following operator<< stuff.
|
||||||
EXPECT_EQ(expected, real)
|
EXPECT_EQ(expected, real)
|
||||||
<< (GlobalTestSettings.updateMode
|
<< (GlobalTestSettings.updateMode
|
||||||
? ("Mismatch found and update mode turned on - "
|
? ("Mismatch found and update mode turned on - "
|
||||||
@ -145,57 +144,68 @@ public:
|
|||||||
|
|
||||||
// Update the expected output file if requested.
|
// Update the expected output file if requested.
|
||||||
// It looks weird to duplicate the comparison between expected_output
|
// It looks weird to duplicate the comparison between expected_output
|
||||||
// and
|
// and stream.str(). However, if creating a variable for the comparison
|
||||||
// stream.str(). However, if creating a variable for the comparison
|
// result, we cannot have pretty print of the string diff in the above.
|
||||||
// result,
|
|
||||||
// we cannot have pretty print of the string diff in the above.
|
|
||||||
if (GlobalTestSettings.updateMode && expected != real) {
|
if (GlobalTestSettings.updateMode && expected != real) {
|
||||||
EXPECT_TRUE(WriteFile(fname, real)) << "Flushing failed";
|
EXPECT_TRUE(WriteFile(fname, real)) << "Flushing failed";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ShaderResult {
|
||||||
|
std::string shaderName;
|
||||||
|
std::string output;
|
||||||
|
std::string error;
|
||||||
|
};
|
||||||
|
|
||||||
// A struct for holding all the information returned by glslang compilation
|
// A struct for holding all the information returned by glslang compilation
|
||||||
// and linking.
|
// and linking.
|
||||||
struct GlslangResult {
|
struct GlslangResult {
|
||||||
const std::string compilationOutput;
|
std::vector<ShaderResult> shaderResults;
|
||||||
const std::string compilationError;
|
std::string linkingOutput;
|
||||||
const std::string linkingOutput;
|
std::string linkingError;
|
||||||
const std::string linkingError;
|
std::string spirvWarningsErrors;
|
||||||
const std::string spirvWarningsErrors;
|
std::string spirv; // Optional SPIR-V disassembly text.
|
||||||
const std::string spirv; // Optional SPIR-V disassembly text.
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Compiles and linkes the given source |code| of the given shader
|
// Compiles and the given source |code| of the given shader |stage| into
|
||||||
// |stage| into the given |target| under the given |semantics|. Returns
|
// the target under the semantics conveyed via |controls|. Returns true
|
||||||
// a GlslangResult instance containing all the information generated
|
// and modifies |shader| on success.
|
||||||
// during the process. If |target| is Target::Spirv, also disassembles
|
bool compile(glslang::TShader* shader, const std::string& code,
|
||||||
// the result and returns disassembly text.
|
const std::string& entryPointName, EShMessages controls)
|
||||||
GlslangResult compile(const std::string& code, Source source,
|
|
||||||
const std::string& stage, Semantics semantics,
|
|
||||||
Target target, const std::string& entryPointName)
|
|
||||||
{
|
{
|
||||||
const char* shaderStrings = code.data();
|
const char* shaderStrings = code.data();
|
||||||
const int shaderLengths = static_cast<int>(code.size());
|
const int shaderLengths = static_cast<int>(code.size());
|
||||||
const EShLanguage kind = GetShaderStage(stage);
|
|
||||||
|
|
||||||
glslang::TShader shader(kind);
|
shader->setStringsWithLengths(&shaderStrings, &shaderLengths, 1);
|
||||||
shader.setStringsWithLengths(&shaderStrings, &shaderLengths, 1);
|
if (!entryPointName.empty()) shader->setEntryPoint(entryPointName.c_str());
|
||||||
if (!entryPointName.empty()) shader.setEntryPoint(entryPointName.c_str());
|
|
||||||
const EShMessages messages = DeriveOptions(source, semantics, target);
|
|
||||||
// Reinitialize glslang if the semantics change.
|
// Reinitialize glslang if the semantics change.
|
||||||
GlslangInitializer::InitializationToken token =
|
GlslangInitializer::InitializationToken token =
|
||||||
GlobalTestSettings.initializer->acquire(messages);
|
GlobalTestSettings.initializer->acquire(controls);
|
||||||
bool success =
|
return shader->parse(&glslang::DefaultTBuiltInResource, defaultVersion,
|
||||||
shader.parse(&glslang::DefaultTBuiltInResource, defaultVersion,
|
isForwardCompatible, controls);
|
||||||
isForwardCompatible, messages);
|
}
|
||||||
|
|
||||||
|
// Compiles and links the given source |code| of the given shader
|
||||||
|
// |stage| into the target under the semantics specified via |controls|.
|
||||||
|
// Returns a GlslangResult instance containing all the information generated
|
||||||
|
// during the process. If the target includes SPIR-V, also disassembles
|
||||||
|
// the result and returns disassembly text.
|
||||||
|
GlslangResult compileAndLink(
|
||||||
|
const std::string shaderName, const std::string& code,
|
||||||
|
const std::string& entryPointName, EShMessages controls)
|
||||||
|
{
|
||||||
|
const EShLanguage kind = GetShaderStage(GetSuffix(shaderName));
|
||||||
|
|
||||||
|
glslang::TShader shader(kind);
|
||||||
|
bool success = compile(&shader, code, entryPointName, controls);
|
||||||
|
|
||||||
glslang::TProgram program;
|
glslang::TProgram program;
|
||||||
program.addShader(&shader);
|
program.addShader(&shader);
|
||||||
success &= program.link(messages);
|
success &= program.link(controls);
|
||||||
|
|
||||||
spv::SpvBuildLogger logger;
|
spv::SpvBuildLogger logger;
|
||||||
|
|
||||||
if (success && (target == Target::Spv || target == Target::BothASTAndSpv)) {
|
if (success && (controls & EShMsgSpvRules)) {
|
||||||
std::vector<uint32_t> spirv_binary;
|
std::vector<uint32_t> spirv_binary;
|
||||||
glslang::GlslangToSpv(*program.getIntermediate(kind),
|
glslang::GlslangToSpv(*program.getIntermediate(kind),
|
||||||
spirv_binary, &logger);
|
spirv_binary, &logger);
|
||||||
@ -203,13 +213,37 @@ public:
|
|||||||
std::ostringstream disassembly_stream;
|
std::ostringstream disassembly_stream;
|
||||||
spv::Parameterize();
|
spv::Parameterize();
|
||||||
spv::Disassemble(disassembly_stream, spirv_binary);
|
spv::Disassemble(disassembly_stream, spirv_binary);
|
||||||
return {shader.getInfoLog(), shader.getInfoDebugLog(),
|
return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
|
||||||
program.getInfoLog(), program.getInfoDebugLog(),
|
program.getInfoLog(), program.getInfoDebugLog(),
|
||||||
logger.getAllMessages(), disassembly_stream.str()};
|
logger.getAllMessages(), disassembly_stream.str()};
|
||||||
} else {
|
} else {
|
||||||
return {shader.getInfoLog(), shader.getInfoDebugLog(),
|
return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
|
||||||
program.getInfoLog(), program.getInfoDebugLog(),
|
program.getInfoLog(), program.getInfoDebugLog(), "", ""};
|
||||||
"", ""};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void outputResultToStream(std::ostringstream* stream,
|
||||||
|
const GlslangResult& result,
|
||||||
|
EShMessages controls)
|
||||||
|
{
|
||||||
|
const auto outputIfNotEmpty = [&stream](const std::string& str) {
|
||||||
|
if (!str.empty()) *stream << str << "\n";
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const auto& shaderResult : result.shaderResults) {
|
||||||
|
*stream << shaderResult.shaderName << "\n";
|
||||||
|
outputIfNotEmpty(shaderResult.output);
|
||||||
|
outputIfNotEmpty(shaderResult.error);
|
||||||
|
}
|
||||||
|
outputIfNotEmpty(result.linkingOutput);
|
||||||
|
outputIfNotEmpty(result.linkingError);
|
||||||
|
*stream << result.spirvWarningsErrors;
|
||||||
|
|
||||||
|
if (controls & EShMsgSpvRules) {
|
||||||
|
*stream
|
||||||
|
<< (result.spirv.empty()
|
||||||
|
? "SPIR-V is not generated for failed compile or link\n"
|
||||||
|
: result.spirv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,29 +262,13 @@ public:
|
|||||||
tryLoadFile(inputFname, "input", &input);
|
tryLoadFile(inputFname, "input", &input);
|
||||||
tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
|
tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
|
||||||
|
|
||||||
|
const EShMessages controls = DeriveOptions(source, semantics, target);
|
||||||
GlslangResult result =
|
GlslangResult result =
|
||||||
compile(input, source, GetSuffix(testName),
|
compileAndLink(testName, input, entryPointName, controls);
|
||||||
semantics, target, entryPointName);
|
|
||||||
|
|
||||||
// Generate the hybrid output in the way of glslangValidator.
|
// Generate the hybrid output in the way of glslangValidator.
|
||||||
std::ostringstream stream;
|
std::ostringstream stream;
|
||||||
|
outputResultToStream(&stream, result, controls);
|
||||||
const auto outputIfNotEmpty = [&stream](const std::string& str) {
|
|
||||||
if (!str.empty()) stream << str << "\n";
|
|
||||||
};
|
|
||||||
|
|
||||||
stream << testName << "\n";
|
|
||||||
outputIfNotEmpty(result.compilationOutput);
|
|
||||||
outputIfNotEmpty(result.compilationError);
|
|
||||||
outputIfNotEmpty(result.linkingOutput);
|
|
||||||
outputIfNotEmpty(result.linkingError);
|
|
||||||
stream << result.spirvWarningsErrors;
|
|
||||||
if (target == Target::Spv || target == Target::BothASTAndSpv) {
|
|
||||||
stream
|
|
||||||
<< (result.spirv.empty()
|
|
||||||
? "SPIR-V is not generated for failed compile or link\n"
|
|
||||||
: result.spirv);
|
|
||||||
}
|
|
||||||
|
|
||||||
checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
|
checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
|
||||||
expectedOutputFname);
|
expectedOutputFname);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user