mirror of
https://github.com/bkaradzic/bgfx.git
synced 2026-02-21 22:33:08 +01:00
Updated glslang and spirv-tools.
This commit is contained in:
130
3rdparty/spirv-tools/test/cpp_interface_test.cpp
vendored
130
3rdparty/spirv-tools/test/cpp_interface_test.cpp
vendored
@@ -12,18 +12,30 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "spirv-tools/optimizer.hpp"
|
||||
#include "spirv/1.1/spirv.h"
|
||||
|
||||
namespace spvtools {
|
||||
namespace {
|
||||
|
||||
using namespace spvtools;
|
||||
using ::testing::ContainerEq;
|
||||
using ::testing::HasSubstr;
|
||||
|
||||
// Return a string that contains the minimum instructions needed to form
|
||||
// a valid module. Other instructions can be appended to this string.
|
||||
std::string Header() {
|
||||
return R"(OpCapability Shader
|
||||
OpCapability Linkage
|
||||
OpMemoryModel Logical GLSL450
|
||||
)";
|
||||
}
|
||||
|
||||
TEST(CppInterface, SuccessfulRoundTrip) {
|
||||
const std::string input_text = "%2 = OpSizeOf %1 %3\n";
|
||||
SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
|
||||
@@ -42,7 +54,7 @@ TEST(CppInterface, SuccessfulRoundTrip) {
|
||||
EXPECT_EQ(0u, position.line);
|
||||
EXPECT_EQ(0u, position.column);
|
||||
EXPECT_EQ(1u, position.index);
|
||||
EXPECT_STREQ("ID 1 has not been defined", message);
|
||||
EXPECT_STREQ("ID 1 has not been defined\n %2 = OpSizeOf %1 %3\n", message);
|
||||
});
|
||||
EXPECT_FALSE(t.Validate(binary));
|
||||
|
||||
@@ -87,28 +99,6 @@ TEST(CppInterface, AssembleOverloads) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(CppInterface, AssembleWithWrongTargetEnv) {
|
||||
const std::string input_text = "%r = OpSizeOf %type %pointer";
|
||||
SpirvTools t(SPV_ENV_UNIVERSAL_1_0);
|
||||
int invocation_count = 0;
|
||||
t.SetMessageConsumer(
|
||||
[&invocation_count](spv_message_level_t level, const char* source,
|
||||
const spv_position_t& position, const char* message) {
|
||||
++invocation_count;
|
||||
EXPECT_EQ(SPV_MSG_ERROR, level);
|
||||
EXPECT_STREQ("input", source);
|
||||
EXPECT_EQ(0u, position.line);
|
||||
EXPECT_EQ(5u, position.column);
|
||||
EXPECT_EQ(5u, position.index);
|
||||
EXPECT_STREQ("Invalid Opcode name 'OpSizeOf'", message);
|
||||
});
|
||||
|
||||
std::vector<uint32_t> binary = {42, 42};
|
||||
EXPECT_FALSE(t.Assemble(input_text, &binary));
|
||||
EXPECT_THAT(binary, ContainerEq(std::vector<uint32_t>{42, 42}));
|
||||
EXPECT_EQ(1, invocation_count);
|
||||
}
|
||||
|
||||
TEST(CppInterface, DisassembleEmptyModule) {
|
||||
std::string text(10, 'x');
|
||||
SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
|
||||
@@ -148,36 +138,7 @@ TEST(CppInterface, DisassembleOverloads) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(CppInterface, DisassembleWithWrongTargetEnv) {
|
||||
const std::string input_text = "%r = OpSizeOf %type %pointer";
|
||||
SpirvTools t11(SPV_ENV_UNIVERSAL_1_1);
|
||||
SpirvTools t10(SPV_ENV_UNIVERSAL_1_0);
|
||||
int invocation_count = 0;
|
||||
t10.SetMessageConsumer(
|
||||
[&invocation_count](spv_message_level_t level, const char* source,
|
||||
const spv_position_t& position, const char* message) {
|
||||
++invocation_count;
|
||||
EXPECT_EQ(SPV_MSG_ERROR, level);
|
||||
EXPECT_STREQ("input", source);
|
||||
EXPECT_EQ(0u, position.line);
|
||||
EXPECT_EQ(0u, position.column);
|
||||
EXPECT_EQ(5u, position.index);
|
||||
EXPECT_STREQ("Invalid opcode: 321", message);
|
||||
});
|
||||
|
||||
std::vector<uint32_t> binary;
|
||||
EXPECT_TRUE(t11.Assemble(input_text, &binary));
|
||||
|
||||
std::string output_text(10, 'x');
|
||||
EXPECT_FALSE(t10.Disassemble(binary, &output_text));
|
||||
EXPECT_EQ("xxxxxxxxxx", output_text); // The original string is unmodified.
|
||||
}
|
||||
|
||||
TEST(CppInterface, SuccessfulValidation) {
|
||||
const std::string input_text = R"(
|
||||
OpCapability Shader
|
||||
OpCapability Linkage
|
||||
OpMemoryModel Logical GLSL450)";
|
||||
SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
|
||||
int invocation_count = 0;
|
||||
t.SetMessageConsumer([&invocation_count](spv_message_level_t, const char*,
|
||||
@@ -186,19 +147,15 @@ TEST(CppInterface, SuccessfulValidation) {
|
||||
});
|
||||
|
||||
std::vector<uint32_t> binary;
|
||||
EXPECT_TRUE(t.Assemble(input_text, &binary));
|
||||
EXPECT_TRUE(t.Assemble(Header(), &binary));
|
||||
EXPECT_TRUE(t.Validate(binary));
|
||||
EXPECT_EQ(0, invocation_count);
|
||||
}
|
||||
|
||||
TEST(CppInterface, ValidateOverloads) {
|
||||
const std::string input_text = R"(
|
||||
OpCapability Shader
|
||||
OpCapability Linkage
|
||||
OpMemoryModel Logical GLSL450)";
|
||||
SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
|
||||
std::vector<uint32_t> binary;
|
||||
EXPECT_TRUE(t.Assemble(input_text, &binary));
|
||||
EXPECT_TRUE(t.Assemble(Header(), &binary));
|
||||
|
||||
{ EXPECT_TRUE(t.Validate(binary)); }
|
||||
{ EXPECT_TRUE(t.Validate(binary.data(), binary.size())); }
|
||||
@@ -226,11 +183,9 @@ TEST(CppInterface, ValidateEmptyModule) {
|
||||
// with the given number of members.
|
||||
std::string MakeModuleHavingStruct(int num_members) {
|
||||
std::stringstream os;
|
||||
os << R"(OpCapability Shader
|
||||
OpCapability Linkage
|
||||
OpMemoryModel Logical GLSL450
|
||||
%1 = OpTypeInt 32 0
|
||||
%2 = OpTypeStruct)";
|
||||
os << Header();
|
||||
os << R"(%1 = OpTypeInt 32 0
|
||||
%2 = OpTypeStruct)";
|
||||
for (int i = 0; i < num_members; i++) os << " %1";
|
||||
return os.str();
|
||||
}
|
||||
@@ -239,7 +194,7 @@ TEST(CppInterface, ValidateWithOptionsPass) {
|
||||
SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
|
||||
std::vector<uint32_t> binary;
|
||||
EXPECT_TRUE(t.Assemble(MakeModuleHavingStruct(10), &binary));
|
||||
const spvtools::ValidatorOptions opts;
|
||||
const ValidatorOptions opts;
|
||||
|
||||
EXPECT_TRUE(t.Validate(binary.data(), binary.size(), opts));
|
||||
}
|
||||
@@ -248,7 +203,7 @@ TEST(CppInterface, ValidateWithOptionsFail) {
|
||||
SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
|
||||
std::vector<uint32_t> binary;
|
||||
EXPECT_TRUE(t.Assemble(MakeModuleHavingStruct(10), &binary));
|
||||
spvtools::ValidatorOptions opts;
|
||||
ValidatorOptions opts;
|
||||
opts.SetUniversalLimit(spv_validator_limit_max_struct_members, 9);
|
||||
std::stringstream os;
|
||||
t.SetMessageConsumer([&os](spv_message_level_t, const char*,
|
||||
@@ -264,8 +219,8 @@ TEST(CppInterface, ValidateWithOptionsFail) {
|
||||
|
||||
// Checks that after running the given optimizer |opt| on the given |original|
|
||||
// source code, we can get the given |optimized| source code.
|
||||
void CheckOptimization(const char* original, const char* optimized,
|
||||
const Optimizer& opt) {
|
||||
void CheckOptimization(const std::string& original,
|
||||
const std::string& optimized, const Optimizer& opt) {
|
||||
SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
|
||||
std::vector<uint32_t> original_binary;
|
||||
ASSERT_TRUE(t.Assemble(original, &original_binary));
|
||||
@@ -286,29 +241,31 @@ TEST(CppInterface, OptimizeEmptyModule) {
|
||||
|
||||
Optimizer o(SPV_ENV_UNIVERSAL_1_1);
|
||||
o.RegisterPass(CreateStripDebugInfoPass());
|
||||
EXPECT_TRUE(o.Run(binary.data(), binary.size(), &binary));
|
||||
|
||||
// Fails to validate.
|
||||
EXPECT_FALSE(o.Run(binary.data(), binary.size(), &binary));
|
||||
}
|
||||
|
||||
TEST(CppInterface, OptimizeModifiedModule) {
|
||||
Optimizer o(SPV_ENV_UNIVERSAL_1_1);
|
||||
o.RegisterPass(CreateStripDebugInfoPass());
|
||||
CheckOptimization("OpSource GLSL 450", "", o);
|
||||
CheckOptimization(Header() + "OpSource GLSL 450", Header(), o);
|
||||
}
|
||||
|
||||
TEST(CppInterface, OptimizeMulitplePasses) {
|
||||
const char* original_text =
|
||||
"OpSource GLSL 450 "
|
||||
"OpDecorate %true SpecId 1 "
|
||||
"%bool = OpTypeBool "
|
||||
"%true = OpSpecConstantTrue %bool";
|
||||
std::string original_text = Header() +
|
||||
"OpSource GLSL 450 "
|
||||
"OpDecorate %true SpecId 1 "
|
||||
"%bool = OpTypeBool "
|
||||
"%true = OpSpecConstantTrue %bool";
|
||||
|
||||
Optimizer o(SPV_ENV_UNIVERSAL_1_1);
|
||||
o.RegisterPass(CreateStripDebugInfoPass())
|
||||
.RegisterPass(CreateFreezeSpecConstantValuePass());
|
||||
|
||||
const char* expected_text =
|
||||
"%bool = OpTypeBool\n"
|
||||
"%true = OpConstantTrue %bool\n";
|
||||
std::string expected_text = Header() +
|
||||
"%bool = OpTypeBool\n"
|
||||
"%true = OpConstantTrue %bool\n";
|
||||
|
||||
CheckOptimization(original_text, expected_text, o);
|
||||
}
|
||||
@@ -323,7 +280,7 @@ TEST(CppInterface, OptimizeReassignPassToken) {
|
||||
token = CreateStripDebugInfoPass();
|
||||
|
||||
CheckOptimization(
|
||||
"OpSource GLSL 450", "",
|
||||
Header() + "OpSource GLSL 450", Header(),
|
||||
Optimizer(SPV_ENV_UNIVERSAL_1_1).RegisterPass(std::move(token)));
|
||||
}
|
||||
|
||||
@@ -332,7 +289,7 @@ TEST(CppInterface, OptimizeMoveConstructPassToken) {
|
||||
Optimizer::PassToken token2(std::move(token1));
|
||||
|
||||
CheckOptimization(
|
||||
"OpSource GLSL 450", "",
|
||||
Header() + "OpSource GLSL 450", Header(),
|
||||
Optimizer(SPV_ENV_UNIVERSAL_1_1).RegisterPass(std::move(token2)));
|
||||
}
|
||||
|
||||
@@ -342,14 +299,14 @@ TEST(CppInterface, OptimizeMoveAssignPassToken) {
|
||||
token2 = std::move(token1);
|
||||
|
||||
CheckOptimization(
|
||||
"OpSource GLSL 450", "",
|
||||
Header() + "OpSource GLSL 450", Header(),
|
||||
Optimizer(SPV_ENV_UNIVERSAL_1_1).RegisterPass(std::move(token2)));
|
||||
}
|
||||
|
||||
TEST(CppInterface, OptimizeSameAddressForOriginalOptimizedBinary) {
|
||||
SpirvTools t(SPV_ENV_UNIVERSAL_1_1);
|
||||
std::vector<uint32_t> binary;
|
||||
ASSERT_TRUE(t.Assemble("OpSource GLSL 450", &binary));
|
||||
ASSERT_TRUE(t.Assemble(Header() + "OpSource GLSL 450", &binary));
|
||||
|
||||
EXPECT_TRUE(Optimizer(SPV_ENV_UNIVERSAL_1_1)
|
||||
.RegisterPass(CreateStripDebugInfoPass())
|
||||
@@ -357,9 +314,10 @@ TEST(CppInterface, OptimizeSameAddressForOriginalOptimizedBinary) {
|
||||
|
||||
std::string optimized_text;
|
||||
EXPECT_TRUE(t.Disassemble(binary, &optimized_text));
|
||||
EXPECT_EQ("", optimized_text);
|
||||
EXPECT_EQ(Header(), optimized_text);
|
||||
}
|
||||
|
||||
// TODO(antiagainst): tests for SetMessageConsumer().
|
||||
|
||||
} // anonymous namespace
|
||||
} // namespace
|
||||
} // namespace spvtools
|
||||
|
||||
Reference in New Issue
Block a user