WIP: SPV Remapper: add remapper test framework
This commit is contained in:
@@ -37,4 +37,37 @@ if (TARGET gmock)
|
||||
glslang OSDependent OGLCompiler HLSL glslang
|
||||
SPIRV glslang-default-resource-limits gmock)
|
||||
add_test(NAME glslang-gtests COMMAND glslangtests)
|
||||
|
||||
# -- Remapper tests
|
||||
set(REMAPPER_TEST_SOURCES
|
||||
# Framework related source files
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Initializer.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Settings.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Settings.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TestFixture.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TestFixture.h
|
||||
|
||||
# Test related source files
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Remap.FromFile.cpp
|
||||
)
|
||||
|
||||
add_executable(remappertests ${REMAPPER_TEST_SOURCES})
|
||||
set_property(TARGET remappertests PROPERTY FOLDER tests)
|
||||
glslang_set_link_args(remappertests)
|
||||
install(TARGETS remappertests
|
||||
RUNTIME DESTINATION bin)
|
||||
|
||||
target_compile_definitions(remappertests
|
||||
PRIVATE GLSLANG_TEST_DIRECTORY="${CMAKE_CURRENT_SOURCE_DIR}/../Test")
|
||||
target_include_directories(remappertests PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${PROJECT_SOURCE_DIR}
|
||||
${gmock_SOURCE_DIR}/include
|
||||
${gtest_SOURCE_DIR}/include)
|
||||
target_link_libraries(remappertests PRIVATE
|
||||
SPVRemapper
|
||||
glslang OSDependent OGLCompiler HLSL glslang
|
||||
SPIRV glslang-default-resource-limits gmock)
|
||||
add_test(NAME remapper-gtests COMMAND remappertests)
|
||||
endif()
|
||||
|
||||
117
gtests/Remap.FromFile.cpp
Normal file
117
gtests/Remap.FromFile.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
//
|
||||
// Copyright (C) 2016 LunarG, 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 <gtest/gtest.h>
|
||||
|
||||
#include "TestFixture.h"
|
||||
|
||||
namespace glslangtest {
|
||||
namespace {
|
||||
|
||||
struct RemapTestArgs {
|
||||
const char* fileName;
|
||||
const char* entryPoint;
|
||||
Source sourceLanguage;
|
||||
unsigned int remapOpts;
|
||||
};
|
||||
|
||||
// We are using FileNameEntryPointPair objects as parameters for instantiating
|
||||
// the template, so the global FileNameAsCustomTestSuffix() won't work since
|
||||
// it assumes std::string as parameters. Thus, an overriding one here.
|
||||
std::string FileNameAsCustomTestSuffix(
|
||||
const ::testing::TestParamInfo<RemapTestArgs>& info) {
|
||||
std::string name = info.param.fileName;
|
||||
// A valid test case suffix cannot have '.' and '-' inside.
|
||||
std::replace(name.begin(), name.end(), '.', '_');
|
||||
std::replace(name.begin(), name.end(), '-', '_');
|
||||
return name;
|
||||
}
|
||||
|
||||
using RemapTest = GlslangTest<::testing::TestWithParam<RemapTestArgs>>;
|
||||
|
||||
// Remapping SPIR-V modules.
|
||||
TEST_P(RemapTest, FromFile)
|
||||
{
|
||||
if (GetSuffix(GetParam().fileName) == "spv") {
|
||||
loadFileRemapAndCheck(GLSLANG_TEST_DIRECTORY, GetParam().fileName,
|
||||
GetParam().sourceLanguage,
|
||||
Semantics::Vulkan,
|
||||
Target::Spv,
|
||||
GetParam().remapOpts);
|
||||
} else {
|
||||
loadFileCompileRemapAndCheck(GLSLANG_TEST_DIRECTORY, GetParam().fileName,
|
||||
GetParam().sourceLanguage,
|
||||
Semantics::Vulkan,
|
||||
Target::Spv,
|
||||
GetParam().entryPoint,
|
||||
GetParam().remapOpts);
|
||||
}
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
INSTANTIATE_TEST_CASE_P(
|
||||
ToSpirv, RemapTest,
|
||||
::testing::ValuesIn(std::vector<RemapTestArgs>{
|
||||
// GLSL remapper tests
|
||||
// testname entry language remapper_options
|
||||
{ "remap.basic.none.frag", "main", Source::GLSL, spv::spirvbin_t::NONE },
|
||||
{ "remap.basic.everything.frag", "main", Source::GLSL, spv::spirvbin_t::DO_EVERYTHING },
|
||||
{ "remap.basic.dcefunc.frag", "main", Source::GLSL, spv::spirvbin_t::DCE_FUNCS },
|
||||
{ "remap.basic.strip.frag", "main", Source::GLSL, spv::spirvbin_t::STRIP },
|
||||
{ "remap.switch.none.frag", "main", Source::GLSL, spv::spirvbin_t::NONE },
|
||||
{ "remap.switch.everything.frag", "main", Source::GLSL, spv::spirvbin_t::DO_EVERYTHING },
|
||||
{ "remap.literal64.none.spv", "main", Source::GLSL, spv::spirvbin_t::NONE },
|
||||
{ "remap.literal64.everything.spv", "main", Source::GLSL, spv::spirvbin_t::DO_EVERYTHING },
|
||||
{ "remap.if.none.frag", "main", Source::GLSL, spv::spirvbin_t::NONE },
|
||||
{ "remap.if.everything.frag", "main", Source::GLSL, spv::spirvbin_t::DO_EVERYTHING },
|
||||
{ "remap.similar_1a.none.frag", "main", Source::GLSL, spv::spirvbin_t::NONE },
|
||||
{ "remap.similar_1b.none.frag", "main", Source::GLSL, spv::spirvbin_t::NONE },
|
||||
{ "remap.similar_1a.everything.frag", "main", Source::GLSL, spv::spirvbin_t::DO_EVERYTHING },
|
||||
{ "remap.similar_1b.everything.frag", "main", Source::GLSL, spv::spirvbin_t::DO_EVERYTHING },
|
||||
{ "remap.uniformarray.none.frag", "main", Source::GLSL, spv::spirvbin_t::NONE },
|
||||
{ "remap.uniformarray.everything.frag", "main", Source::GLSL, spv::spirvbin_t::DO_EVERYTHING },
|
||||
|
||||
// HLSL remapper tests
|
||||
{ "remap.hlsl.sample.basic.strip.frag", "main", Source::HLSL, spv::spirvbin_t::STRIP },
|
||||
{ "remap.hlsl.sample.basic.everything.frag", "main", Source::HLSL, spv::spirvbin_t::DO_EVERYTHING },
|
||||
{ "remap.hlsl.sample.basic.none.frag", "main", Source::HLSL, spv::spirvbin_t::NONE },
|
||||
{ "remap.hlsl.templatetypes.none.frag", "main", Source::HLSL, spv::spirvbin_t::NONE },
|
||||
{ "remap.hlsl.templatetypes.everything.frag", "main", Source::HLSL, spv::spirvbin_t::DO_EVERYTHING },
|
||||
}),
|
||||
FileNameAsCustomTestSuffix
|
||||
);
|
||||
// clang-format on
|
||||
|
||||
} // anonymous namespace
|
||||
} // namespace glslangtest
|
||||
@@ -116,6 +116,32 @@ std::pair<bool, std::string> ReadFile(const std::string& path)
|
||||
return std::make_pair(false, "");
|
||||
}
|
||||
|
||||
std::pair<bool, std::vector<std::uint32_t> > ReadSpvBinaryFile(const std::string& path)
|
||||
{
|
||||
std::ifstream fstream(path, std::fstream::in | std::fstream::binary);
|
||||
|
||||
if (!fstream)
|
||||
return std::make_pair(false, std::vector<std::uint32_t>());
|
||||
|
||||
std::vector<std::uint32_t> contents;
|
||||
|
||||
// Reserve space (for efficiency, not for correctness)
|
||||
fstream.seekg(0, fstream.end);
|
||||
contents.reserve(size_t(fstream.tellg()) / sizeof(std::uint32_t));
|
||||
fstream.seekg(0, fstream.beg);
|
||||
|
||||
// There is no istream iterator traversing by uint32_t, so we must loop.
|
||||
while (!fstream.eof()) {
|
||||
std::uint32_t inWord;
|
||||
fstream.read((char *)&inWord, sizeof(inWord));
|
||||
|
||||
if (!fstream.eof())
|
||||
contents.push_back(inWord);
|
||||
}
|
||||
|
||||
return std::make_pair(true, contents); // hopefully, c++11 move semantics optimizes the copy away.
|
||||
}
|
||||
|
||||
bool WriteFile(const std::string& path, const std::string& contents)
|
||||
{
|
||||
std::ofstream fstream(path, std::ios::out);
|
||||
|
||||
@@ -40,12 +40,14 @@
|
||||
#include <sstream>
|
||||
#include <streambuf>
|
||||
#include <tuple>
|
||||
#include <string>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "SPIRV/GlslangToSpv.h"
|
||||
#include "SPIRV/disassemble.h"
|
||||
#include "SPIRV/doc.h"
|
||||
#include "SPIRV/SPVRemapper.h"
|
||||
#include "StandAlone/ResourceLimits.h"
|
||||
#include "glslang/Public/ShaderLang.h"
|
||||
|
||||
@@ -93,6 +95,7 @@ EShMessages DeriveOptions(Source, Semantics, Target);
|
||||
// Reads the content of the file at the given |path|. On success, returns true
|
||||
// and the contents; otherwise, returns false and an empty string.
|
||||
std::pair<bool, std::string> ReadFile(const std::string& path);
|
||||
std::pair<bool, std::vector<std::uint32_t> > ReadSpvBinaryFile(const std::string& path);
|
||||
|
||||
// Writes the given |contents| into the file at the given |path|. Returns true
|
||||
// on successful output.
|
||||
@@ -128,6 +131,16 @@ public:
|
||||
ASSERT_TRUE(fileReadOk) << "Cannot open " << tag << " file: " << path;
|
||||
}
|
||||
|
||||
// Tries to load the contents from the file at the given |path|. On success,
|
||||
// writes the contents into |contents|. On failure, errors out.
|
||||
void tryLoadSpvFile(const std::string& path, const std::string& tag,
|
||||
std::vector<uint32_t>& contents)
|
||||
{
|
||||
bool fileReadOk;
|
||||
std::tie(fileReadOk, contents) = ReadSpvBinaryFile(path);
|
||||
ASSERT_TRUE(fileReadOk) << "Cannot open " << tag << " file: " << path;
|
||||
}
|
||||
|
||||
// Checks the equality of |expected| and |real|. If they are not equal,
|
||||
// write |real| to the given file named as |fname| if update mode is on.
|
||||
void checkEqAndUpdateIfRequested(const std::string& expected,
|
||||
@@ -221,6 +234,68 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
// This is like compileAndLink but with remapping of the SPV binary
|
||||
// through spirvbin_t::remap(). While technically this could be merged
|
||||
// with compileAndLink() above (with the remap step optionally being a no-op)
|
||||
// it is given separately here for ease of future extraction.
|
||||
GlslangResult compileLinkRemap(
|
||||
const std::string shaderName, const std::string& code,
|
||||
const std::string& entryPointName, EShMessages controls,
|
||||
const unsigned int remapOptions = spv::spirvbin_t::NONE)
|
||||
{
|
||||
const EShLanguage kind = GetShaderStage(GetSuffix(shaderName));
|
||||
|
||||
glslang::TShader shader(kind);
|
||||
bool success = compile(&shader, code, entryPointName, controls);
|
||||
|
||||
glslang::TProgram program;
|
||||
program.addShader(&shader);
|
||||
success &= program.link(controls);
|
||||
|
||||
spv::SpvBuildLogger logger;
|
||||
|
||||
if (success && (controls & EShMsgSpvRules)) {
|
||||
std::vector<uint32_t> spirv_binary;
|
||||
glslang::GlslangToSpv(*program.getIntermediate(kind),
|
||||
spirv_binary, &logger);
|
||||
|
||||
spv::spirvbin_t(0 /*verbosity*/).remap(spirv_binary, remapOptions);
|
||||
|
||||
std::ostringstream disassembly_stream;
|
||||
spv::Parameterize();
|
||||
spv::Disassemble(disassembly_stream, spirv_binary);
|
||||
return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
|
||||
program.getInfoLog(), program.getInfoDebugLog(),
|
||||
logger.getAllMessages(), disassembly_stream.str()};
|
||||
} else {
|
||||
return {{{shaderName, shader.getInfoLog(), shader.getInfoDebugLog()},},
|
||||
program.getInfoLog(), program.getInfoDebugLog(), "", ""};
|
||||
}
|
||||
}
|
||||
|
||||
// remap the binary in 'code' with the options in remapOptions
|
||||
GlslangResult remap(
|
||||
const std::string shaderName, const std::vector<uint32_t>& code,
|
||||
EShMessages controls,
|
||||
const unsigned int remapOptions = spv::spirvbin_t::NONE)
|
||||
{
|
||||
if ((controls & EShMsgSpvRules)) {
|
||||
std::vector<uint32_t> spirv_binary(code); // scratch copy
|
||||
|
||||
spv::spirvbin_t(0 /*verbosity*/).remap(spirv_binary, remapOptions);
|
||||
|
||||
std::ostringstream disassembly_stream;
|
||||
spv::Parameterize();
|
||||
spv::Disassemble(disassembly_stream, spirv_binary);
|
||||
|
||||
return {{{shaderName, "", ""},},
|
||||
"", "",
|
||||
"", disassembly_stream.str()};
|
||||
} else {
|
||||
return {{{shaderName, "", ""},}, "", "", "", ""};
|
||||
}
|
||||
}
|
||||
|
||||
void outputResultToStream(std::ostringstream* stream,
|
||||
const GlslangResult& result,
|
||||
EShMessages controls)
|
||||
@@ -272,6 +347,60 @@ public:
|
||||
expectedOutputFname);
|
||||
}
|
||||
|
||||
void loadFileCompileRemapAndCheck(const std::string& testDir,
|
||||
const std::string& testName,
|
||||
Source source,
|
||||
Semantics semantics,
|
||||
Target target,
|
||||
const std::string& entryPointName="",
|
||||
const unsigned int remapOptions = spv::spirvbin_t::NONE)
|
||||
{
|
||||
const std::string inputFname = testDir + "/" + testName;
|
||||
const std::string expectedOutputFname =
|
||||
testDir + "/baseResults/" + testName + ".out";
|
||||
std::string input, expectedOutput;
|
||||
|
||||
tryLoadFile(inputFname, "input", &input);
|
||||
tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
|
||||
|
||||
const EShMessages controls = DeriveOptions(source, semantics, target);
|
||||
GlslangResult result = compileLinkRemap(testName, input, entryPointName, controls, remapOptions);
|
||||
|
||||
// Generate the hybrid output in the way of glslangValidator.
|
||||
std::ostringstream stream;
|
||||
outputResultToStream(&stream, result, controls);
|
||||
|
||||
checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
|
||||
expectedOutputFname);
|
||||
}
|
||||
|
||||
void loadFileRemapAndCheck(const std::string& testDir,
|
||||
const std::string& testName,
|
||||
Source source,
|
||||
Semantics semantics,
|
||||
Target target,
|
||||
const unsigned int remapOptions = spv::spirvbin_t::NONE)
|
||||
{
|
||||
const std::string inputFname = testDir + "/" + testName;
|
||||
const std::string expectedOutputFname =
|
||||
testDir + "/baseResults/" + testName + ".out";
|
||||
std::vector<std::uint32_t> input;
|
||||
std::string expectedOutput;
|
||||
|
||||
tryLoadSpvFile(inputFname, "input", input);
|
||||
tryLoadFile(expectedOutputFname, "expected output", &expectedOutput);
|
||||
|
||||
const EShMessages controls = DeriveOptions(source, semantics, target);
|
||||
GlslangResult result = remap(testName, input, controls, remapOptions);
|
||||
|
||||
// Generate the hybrid output in the way of glslangValidator.
|
||||
std::ostringstream stream;
|
||||
outputResultToStream(&stream, result, controls);
|
||||
|
||||
checkEqAndUpdateIfRequested(expectedOutput, stream.str(),
|
||||
expectedOutputFname);
|
||||
}
|
||||
|
||||
// Preprocesses the given |source| code. On success, returns true, the
|
||||
// preprocessed shader, and warning messages. Otherwise, returns false, an
|
||||
// empty string, and error messages.
|
||||
|
||||
Reference in New Issue
Block a user