]> git.tdb.fi Git - libs/gl.git/commitdiff
Test the SPIR-V backend using libspirv
authorMikko Rasa <tdb@tdb.fi>
Fri, 9 Apr 2021 14:36:22 +0000 (17:36 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 10 Apr 2021 00:47:27 +0000 (03:47 +0300)
tests/Build
tests/glsl/glslcompiler.cpp

index 85dced18c4320a0f9bd7c518a5af057e182b7753..1190e72a6b0abfc5f3f3213bb314774c7c92af17 100644 (file)
@@ -8,6 +8,7 @@ package "mspgl-tests"
        program "glsltest"
        {
                source "glsl";
+               require "SPIRV-Tools";
                build_info
                {
                        standard CXX "c++11";
index 01c1fba28e2e4fe90d0f2268a485538fd671f258..35bbc8bcfb82449e1261d8d1ce542df069b86833 100644 (file)
@@ -1,3 +1,4 @@
+#include <spirv-tools/libspirv.hpp>
 #include <msp/core/algorithm.h>
 #include <msp/fs/dir.h>
 #include <msp/fs/utils.h>
@@ -58,6 +59,22 @@ private:
        virtual void fail(const std::string &m) { Test::fail(m); }
 };
 
+class GlslCompilerSpirV: public Msp::Test::RegisteredTest<GlslCompilerSpirV>, private GlslCompilerHelper
+{
+private:
+       spvtools::SpirvTools spirv_tools;
+
+public:
+       GlslCompilerSpirV();
+
+       static const char *get_name() { return "GLSL to SPIR-V compilation"; }
+
+private:
+       void run_test_case(const TestCase *);
+       void diagnostic(spv_message_level_t, const char *, const spv_position_t &, const char *);
+       virtual void fail(const std::string &m) { Test::fail(m); }
+};
+
 using namespace std;
 using namespace Msp;
 
@@ -318,3 +335,41 @@ void GlslCompilerIdempotence::run_test_case(const TestCase *test_case)
        if(j!=stages2.end())
                fail(format("Second pass produced extra stage %s", GL::SL::Stage::get_stage_name(*j)));
 }
+
+
+GlslCompilerSpirV::GlslCompilerSpirV():
+       spirv_tools(SPV_ENV_UNIVERSAL_1_5)
+{
+       load_all_test_cases("glsl");
+       for(const auto &tc: test_cases)
+               if(tc.expect_success)
+                       add(&GlslCompilerSpirV::run_test_case, &tc, tc.name);
+
+       using namespace std::placeholders;
+       spirv_tools.SetMessageConsumer(std::bind(std::mem_fn(&GlslCompilerSpirV::diagnostic), this, _1, _2, _3, _4));
+}
+
+void GlslCompilerSpirV::run_test_case(const TestCase *test_case)
+{
+       GL::SL::Compiler compiler(GL::SL::Features::latest());
+       compiler.set_source(test_case->source, "<test>");
+       compiler.compile(GL::SL::Compiler::SPIRV);
+
+       vector<UInt32> code = compiler.get_combined_spirv();
+       if(!spirv_tools.Validate(code))
+               fail("Invalid SPIR-V generated");
+}
+
+void GlslCompilerSpirV::diagnostic(spv_message_level_t level, const char *, const spv_position_t &, const char *message)
+{
+       const char *prefix;
+       switch(level)
+       {
+       case SPV_MSG_DEBUG: prefix = "debug: "; break;
+       case SPV_MSG_INFO: prefix = "info: "; break;
+       case SPV_MSG_WARNING: prefix = "warning: "; break;
+       case SPV_MSG_ERROR: prefix = "error: "; break;
+       default: prefix = "";
+       }
+       info(format("%s%s", prefix, message));
+}