6f867dd7d7d6 — Leonard Ritter tip 2 days ago
* added `sc_spirv_to_glsl` API function for debugging spirv shaders / spirv cross
5 files changed, 37 insertions(+), 3 deletions(-)

M include/scopes/scopes.h
M src/gen_spirv.cpp
M src/gen_spirv.hpp
M src/globals.cpp
M testing/test_glsl.sc
M include/scopes/scopes.h +1 -0
@@ 169,6 169,7 @@ SCOPES_LIBEXPORT sc_valueref_raises_t sc
 SCOPES_LIBEXPORT sc_valueref_raises_t sc_compile(sc_valueref_t srcl, uint64_t flags);
 SCOPES_LIBEXPORT sc_string_raises_t sc_compile_spirv(sc_symbol_t target, sc_valueref_t srcl, uint64_t flags);
 SCOPES_LIBEXPORT sc_string_raises_t sc_compile_glsl(int version, sc_symbol_t target, sc_valueref_t srcl, uint64_t flags);
+SCOPES_LIBEXPORT const sc_string_t *sc_spirv_to_glsl(const sc_string_t *binary);
 SCOPES_LIBEXPORT const sc_string_t *sc_default_target_triple();
 SCOPES_LIBEXPORT sc_void_raises_t sc_compile_object(const sc_string_t *target_triple, int file_kind, const sc_string_t *path, const sc_scope_t *table, uint64_t flags);
 SCOPES_LIBEXPORT void sc_enter_solver_cli ();

          
M src/gen_spirv.cpp +23 -2
@@ 250,6 250,7 @@ struct SPIRVGenerator {
     FunctionRef active_function;
     int functions_generated;
     spv_target_env env;
+    bool use_combined_image_samplers = false;
 
     spv::Id get_op_ex(Symbol name) {
         auto it = intrinsic_ops.find(name);

          
@@ 629,6 630,7 @@ struct SPIRVGenerator {
                 SCOPES_GET_RESULT(image_format_from_symbol(it->format)));
         } break;
         case TK_Sampler: {
+            use_combined_image_samplers = true;
             return builder.makeSamplerType();
         } break;
         default: break;

          
@@ 2438,6 2440,27 @@ SCOPES_RESULT(const String *) compile_sp
     return String::from((char *)&result[0], bytesize);
 }
 
+const String *spirv_to_glsl(const String *binary) {
+    std::vector<unsigned int> bytes;
+    unsigned int sz = binary->count / sizeof(unsigned int);
+    bytes.resize(sz);
+    memcpy(&bytes[0], binary->data, binary->count);
+
+	spirv_cross::CompilerGLSL glsl(std::move(bytes));
+
+    // Set some options.
+    spirv_cross::CompilerGLSL::Options options;
+    options.version = 450;
+    options.vulkan_semantics = true;
+    glsl.build_combined_image_samplers();
+    glsl.set_common_options(options);
+
+    // Compile to GLSL, ready to give to GL driver.
+    std::string source = glsl.compile();
+
+    return String::from_stdstring(source);
+}
+
 SCOPES_RESULT(const String *) compile_glsl(int version, Symbol target, const FunctionRef &fn, uint64_t flags) {
     SCOPES_RESULT_TYPE(const String *);
     Timer sum_compile_time(TIMER_CompileSPIRV);

          
@@ 2498,8 2521,6 @@ SCOPES_RESULT(const String *) compile_gl
     options.version = (version <= 0)?450:version;
     glsl.set_common_options(options);
 
-    glsl.build_combined_image_samplers();
-
     // Compile to GLSL, ready to give to GL driver.
     std::string source = glsl.compile();
 

          
M src/gen_spirv.hpp +2 -0
@@ 21,6 21,8 @@ struct Function;
 SCOPES_RESULT(const String *) compile_spirv(Symbol target, const FunctionRef &fn, uint64_t flags);
 SCOPES_RESULT(const String *) compile_glsl(int version, Symbol target, const FunctionRef &fn, uint64_t flags);
 
+const String *spirv_to_glsl(const String *binary);
+
 } // namespace scopes
 
 #endif // SCOPES_GEN_SPIRV_HPP
  No newline at end of file

          
M src/globals.cpp +6 -0
@@ 295,6 295,11 @@ sc_string_raises_t sc_compile_glsl(int v
     return convert_result(compile_glsl(version, target, result, flags));
 }
 
+const sc_string_t *sc_spirv_to_glsl(const sc_string_t *binary) {
+    using namespace scopes;
+    return spirv_to_glsl(binary);
+}
+
 const sc_string_t *sc_default_target_triple() {
     using namespace scopes;
     return get_default_target_triple();

          
@@ 2303,6 2308,7 @@ void init_globals(int argc, char *argv[]
     DEFINE_RAISING_EXTERN_C_FUNCTION(sc_compile, TYPE_ValueRef, TYPE_ValueRef, TYPE_U64);
     DEFINE_RAISING_EXTERN_C_FUNCTION(sc_compile_spirv, TYPE_String, TYPE_Symbol, TYPE_ValueRef, TYPE_U64);
     DEFINE_RAISING_EXTERN_C_FUNCTION(sc_compile_glsl, TYPE_String, TYPE_I32, TYPE_Symbol, TYPE_ValueRef, TYPE_U64);
+    DEFINE_EXTERN_C_FUNCTION(sc_spirv_to_glsl, TYPE_String, TYPE_String);
     DEFINE_EXTERN_C_FUNCTION(sc_default_target_triple, TYPE_String);
     DEFINE_RAISING_EXTERN_C_FUNCTION(sc_compile_object, _void, TYPE_String, TYPE_I32, TYPE_String, TYPE_Scope, TYPE_U64);
     DEFINE_EXTERN_C_FUNCTION(sc_enter_solver_cli, _void);

          
M testing/test_glsl.sc +5 -1
@@ 162,11 162,15 @@ do
                 sampler2D t_diffuse s_diffuse
                 v_tex_coords
 
-    print
+    let bin =
         compile-spirv 'fragment
             typify main
             'dump-disassembly
             #'no-opts
 
+    print bin
+    print
+        sc_spirv_to_glsl bin
+
 
 ;