2015-08-21 15:50:09 +00:00
|
|
|
// Copyright (c) 2015 The Khronos Group Inc.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and/or associated documentation files (the
|
|
|
|
// "Materials"), to deal in the Materials without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Materials, and to
|
|
|
|
// permit persons to whom the Materials are furnished to do so, subject to
|
|
|
|
// the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Materials.
|
|
|
|
//
|
|
|
|
// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
|
|
|
|
// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
|
|
|
|
// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
|
|
|
|
// https://www.khronos.org/registry/
|
|
|
|
//
|
|
|
|
// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
2015-08-21 17:39:34 +00:00
|
|
|
#ifndef _TEST_FIXTURE_H_
|
|
|
|
#define _TEST_FIXTURE_H_
|
2015-08-21 15:50:09 +00:00
|
|
|
|
|
|
|
#include "UnitSPIRV.h"
|
|
|
|
|
2015-09-21 15:36:44 +00:00
|
|
|
namespace spvtest {
|
2015-08-31 19:24:32 +00:00
|
|
|
|
2015-08-21 15:50:09 +00:00
|
|
|
// Common setup for TextToBinary tests. SetText() should be called to populate
|
|
|
|
// the actual test text.
|
2015-09-02 18:48:33 +00:00
|
|
|
template <typename T>
|
2015-08-21 17:39:34 +00:00
|
|
|
class TextToBinaryTestBase : public T {
|
2015-08-21 15:50:09 +00:00
|
|
|
public:
|
2015-09-02 18:48:33 +00:00
|
|
|
// Shorthand for SPIR-V compilation result.
|
|
|
|
using SpirvVector = std::vector<uint32_t>;
|
|
|
|
|
|
|
|
// Offset into a SpirvVector at which the first instruction starts.
|
|
|
|
const SpirvVector::size_type kFirstInstruction = 5;
|
|
|
|
|
2015-08-21 17:39:34 +00:00
|
|
|
TextToBinaryTestBase()
|
2015-08-21 15:50:09 +00:00
|
|
|
: opcodeTable(nullptr),
|
|
|
|
operandTable(nullptr),
|
|
|
|
diagnostic(nullptr),
|
|
|
|
text(),
|
2015-09-02 18:48:33 +00:00
|
|
|
binary(nullptr) {
|
|
|
|
EXPECT_EQ(SPV_SUCCESS, spvOpcodeTableGet(&opcodeTable));
|
|
|
|
EXPECT_EQ(SPV_SUCCESS, spvOperandTableGet(&operandTable));
|
|
|
|
EXPECT_EQ(SPV_SUCCESS, spvExtInstTableGet(&extInstTable));
|
2015-08-21 15:50:09 +00:00
|
|
|
char textStr[] = "substitute the text member variable with your test";
|
|
|
|
text = {textStr, strlen(textStr)};
|
|
|
|
}
|
|
|
|
|
2015-09-02 18:48:33 +00:00
|
|
|
virtual ~TextToBinaryTestBase() {
|
2015-09-08 19:29:22 +00:00
|
|
|
DestroyBinary();
|
2015-08-21 15:50:09 +00:00
|
|
|
if (diagnostic) spvDiagnosticDestroy(diagnostic);
|
|
|
|
}
|
|
|
|
|
2015-09-02 18:48:33 +00:00
|
|
|
// Returns subvector v[from:end).
|
|
|
|
SpirvVector Subvector(const SpirvVector& v, SpirvVector::size_type from) {
|
|
|
|
assert(from < v.size());
|
|
|
|
return SpirvVector(v.begin() + from, v.end());
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:00:00 +00:00
|
|
|
// Compiles SPIR-V text in the given assembly syntax format, asserting
|
|
|
|
// compilation success. Returns the compiled code.
|
2015-09-12 00:10:54 +00:00
|
|
|
SpirvVector CompileSuccessfully(const std::string& text,
|
|
|
|
spv_assembly_syntax_format_t format =
|
|
|
|
SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT) {
|
2015-09-10 18:00:00 +00:00
|
|
|
spv_result_t status = spvTextWithFormatToBinary(
|
|
|
|
text.c_str(), text.size(), format, opcodeTable, operandTable,
|
|
|
|
extInstTable, &binary, &diagnostic);
|
2015-09-04 12:51:52 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, status) << text;
|
2015-09-02 18:48:33 +00:00
|
|
|
SpirvVector code_copy;
|
|
|
|
if (status == SPV_SUCCESS) {
|
|
|
|
code_copy = SpirvVector(binary->code, binary->code + binary->wordCount);
|
2015-09-08 19:29:22 +00:00
|
|
|
DestroyBinary();
|
2015-09-02 18:48:33 +00:00
|
|
|
} else {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
}
|
|
|
|
return code_copy;
|
|
|
|
}
|
|
|
|
|
2015-09-08 18:17:14 +00:00
|
|
|
// Compiles SPIR-V text, asserting compilation failure. Returns the error
|
|
|
|
// message(s).
|
|
|
|
std::string CompileFailure(const std::string& text) {
|
|
|
|
EXPECT_NE(SPV_SUCCESS,
|
2015-09-09 17:04:32 +00:00
|
|
|
spvTextToBinary(text.c_str(), text.size(), opcodeTable,
|
|
|
|
operandTable, extInstTable, &binary, &diagnostic))
|
2015-09-08 18:17:14 +00:00
|
|
|
<< text;
|
2015-09-08 19:29:22 +00:00
|
|
|
DestroyBinary();
|
2015-09-08 18:17:14 +00:00
|
|
|
return diagnostic->error;
|
|
|
|
}
|
|
|
|
|
2015-09-09 14:36:48 +00:00
|
|
|
// Encodes SPIR-V text into binary and then decodes the binary. Returns the
|
|
|
|
// decoded text.
|
2015-09-09 19:44:32 +00:00
|
|
|
std::string EncodeAndDecodeSuccessfully(const std::string& text) {
|
2015-09-09 14:36:48 +00:00
|
|
|
DestroyBinary();
|
2015-09-09 19:44:32 +00:00
|
|
|
spv_result_t error =
|
|
|
|
spvTextToBinary(text.c_str(), text.size(), opcodeTable, operandTable,
|
|
|
|
extInstTable, &binary, &diagnostic);
|
2015-09-09 14:36:48 +00:00
|
|
|
if (error) {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
}
|
|
|
|
EXPECT_EQ(SPV_SUCCESS, error);
|
|
|
|
|
|
|
|
spv_text decoded_text;
|
2015-09-09 19:44:32 +00:00
|
|
|
error = spvBinaryToText(
|
|
|
|
binary->code, binary->wordCount, SPV_BINARY_TO_TEXT_OPTION_NONE,
|
|
|
|
opcodeTable, operandTable, extInstTable, &decoded_text, &diagnostic);
|
2015-09-09 14:36:48 +00:00
|
|
|
if (error) {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
}
|
|
|
|
EXPECT_EQ(SPV_SUCCESS, error);
|
|
|
|
|
|
|
|
const std::string decoded_string = decoded_text->str;
|
|
|
|
spvTextDestroy(decoded_text);
|
|
|
|
|
2015-09-17 22:24:53 +00:00
|
|
|
// Remove the preamble comments generated by disassembler.
|
|
|
|
const std::string schema0 = "Schema: 0\n";
|
|
|
|
std::string::size_type preamble_end = decoded_string.find(schema0);
|
|
|
|
return decoded_string.substr(preamble_end + schema0.size());
|
2015-09-09 14:36:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 21:11:40 +00:00
|
|
|
// Compiles SPIR-V text, asserts success, and returns the words representing
|
|
|
|
// the instructions. In particular, skip the words in the SPIR-V header.
|
2015-09-22 19:50:33 +00:00
|
|
|
SpirvVector CompiledInstructions(const std::string& text,
|
|
|
|
spv_assembly_syntax_format_t format) {
|
|
|
|
const SpirvVector code = CompileSuccessfully(text, format);
|
2015-09-08 21:11:40 +00:00
|
|
|
SpirvVector result;
|
|
|
|
// Extract just the instructions.
|
|
|
|
// If the code fails to compile, then return the empty vector.
|
|
|
|
// In any case, don't crash or invoke undefined behaviour.
|
|
|
|
if (code.size() >= kFirstInstruction)
|
|
|
|
result = Subvector(code, kFirstInstruction);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:50:33 +00:00
|
|
|
// Compiles SPIR-V text with the default assembly format, asserts success, and
|
|
|
|
// returns the words representing the instructions. In particular, skip the
|
|
|
|
// words in the SPIR-V header.
|
|
|
|
SpirvVector CompiledInstructions(const std::string& text) {
|
|
|
|
return CompiledInstructions(text, SPV_ASSEMBLY_SYNTAX_FORMAT_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2015-08-21 15:50:09 +00:00
|
|
|
void SetText(const std::string& code) {
|
|
|
|
textString = code;
|
|
|
|
text.str = textString.c_str();
|
|
|
|
text.length = textString.size();
|
|
|
|
}
|
|
|
|
|
2015-09-08 19:29:22 +00:00
|
|
|
// Destroys the binary, if it exists.
|
|
|
|
void DestroyBinary() {
|
|
|
|
spvBinaryDestroy(binary);
|
|
|
|
binary = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-21 15:50:09 +00:00
|
|
|
spv_opcode_table opcodeTable;
|
|
|
|
spv_operand_table operandTable;
|
|
|
|
spv_ext_inst_table extInstTable;
|
|
|
|
spv_diagnostic diagnostic;
|
|
|
|
|
|
|
|
std::string textString;
|
|
|
|
spv_text_t text;
|
|
|
|
spv_binary binary;
|
|
|
|
};
|
2015-08-21 17:39:34 +00:00
|
|
|
|
2015-08-31 19:24:32 +00:00
|
|
|
using TextToBinaryTest = TextToBinaryTestBase<::testing::Test>;
|
|
|
|
|
2015-09-21 15:36:44 +00:00
|
|
|
} // namespace spvtest
|
2015-08-21 17:39:34 +00:00
|
|
|
|
2015-09-02 18:48:33 +00:00
|
|
|
#endif // _TEXT_FIXTURE_H_
|