Giter Site home page Giter Site logo

fabersanz / spirv-cross.net Goto Github PK

View Code? Open in Web Editor NEW
14.0 2.0 4.0 11.88 MB

SPIRV-Cross.NET is a practical tool and library for performing reflection on SPIR-V and disassembling SPIR-V back to high level languages.

License: MIT License

C# 56.81% GLSL 0.97% C 41.69% HLSL 0.53%
spirv-cross vulkan

spirv-cross.net's Introduction

SPIRV-Cross

SPIRV-Cross.NET is a .NET5 low-level bindings for SPIRV-Cross.


NuGet License: MIT

The code is licensed under MIT. Feel free to use it for whatever purpose.


Ideas and generator part is based on Vortice.Vulkan

Overview

SPIRV-Cross is a tool designed for parsing and converting SPIR-V to other shader languages.

Features

  • Convert SPIR-V to readable, usable and efficient GLSL
  • Convert SPIR-V to readable, usable and efficient Metal Shading Language (MSL)
  • Convert SPIR-V to readable, usable and efficient HLSL
  • Reflection API to simplify the creation of Vulkan pipeline layouts
  • Reflection API to modify and tweak OpDecorations
  • Supports "all" of vertex, fragment, tessellation, geometry and compute shaders.

string GetString(byte* ptr)
{
    int length = 0;
    while (length < 4096 && ptr[length] != 0)
        length++;
    // Decode UTF-8 bytes to string.
    return Encoding.UTF8.GetString(ptr, length);
}

byte[] bytecode = GetBytecode();

SpvId* spirv;

fixed (byte* ptr = bytecode)
    spirv = (SpvId*)ptr;

uint word_count = (uint)bytecode.Length / 4;

spvc_context context = default;
spvc_parsed_ir ir;
spvc_compiler compiler_glsl;
spvc_compiler_options options;
spvc_resources resources;
spvc_reflected_resource* list = default;
nuint count = default;
spvc_error_callback error_callback = default;

// Create context.
spvc_context_create(&context);

// Set debug callback.
spvc_context_set_error_callback(context, error_callback, null);

// Parse the SPIR-V.
spvc_context_parse_spirv(context, spirv, word_count, &ir);

// Hand it off to a compiler instance and give it ownership of the IR.
spvc_context_create_compiler(context, spvc_backend.Glsl, ir, spvc_capture_mode.TakeOwnership, &compiler_glsl);

// Do some basic reflection.
spvc_compiler_create_shader_resources(compiler_glsl, &resources);
spvc_resources_get_resource_list_for_type(resources, spvc_resource_type.UniformBuffer, (spvc_reflected_resource*)&list, &count);

for (uint i = 0; i < count; i++)
{
    Console.WriteLine("ID: {0}, BaseTypeID: {1}, TypeID: {2}, Name: {3}", list[i].id, list[i].base_type_id, list[i].type_id, GetString(list[i].name));

    uint set = spvc_compiler_get_decoration(compiler_glsl, (SpvId)list[i].id, SpvDecoration.SpvDecorationDescriptorSet);
    Console.WriteLine($"Set: {set}");

    uint binding = spvc_compiler_get_decoration(compiler_glsl, (SpvId)list[i].id, SpvDecoration.SpvDecorationBinding);
    Console.WriteLine($"Binding: {binding}");

    Console.WriteLine("=========");
}
Console.WriteLine("\n \n");

// Modify options.
spvc_compiler_create_compiler_options(compiler_glsl, &options);
spvc_compiler_options_set_uint(options, spvc_compiler_option.GlslVersion, 450);
spvc_compiler_options_set_bool(options, spvc_compiler_option.GlslEs, false);
spvc_compiler_install_compiler_options(compiler_glsl, options);

byte* result = default;
spvc_compiler_compile(compiler_glsl, (byte*)&result);
Console.WriteLine("Cross-compiled source: {0}", GetString(result));

// Frees all memory we allocated so far.
spvc_context_destroy(context);

spirv-cross.net's People

Contributors

fabersanz avatar thucommix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

spirv-cross.net's Issues

Unable to load native library

This is awesome - thank you so much for creating this library!

As far as I can tell, it's not loading the native library correctly though. When I run SPIRVCross.Test, I get this exception:

Unhandled exception. System.TypeInitializationException: The type initializer for 'SPIRVCross.SPIRV' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'spirv-cross-c-shared.dll' or one of its dependencies: The specified module could not be found. (0x8007007E)
   at System.Runtime.InteropServices.NativeLibrary.LoadFromPath(String libraryName, Boolean throwOnError)
   at System.Runtime.InteropServices.NativeLibrary.Load(String libraryPath)
   at SPIRVCross.LibraryLoader.LoadLocalLibrary(String libraryName) in C:\Users\Tim Jones\Downloads\SPIRV-Cross.NET-main\Src\SPIRVCross\SPIRV.cs:line 57
   at SPIRVCross.SPIRV.LoadNativeLibrary() in C:\Users\Tim Jones\Downloads\SPIRV-Cross.NET-main\Src\SPIRVCross\SPIRV.cs:line 17
   at SPIRVCross.SPIRV..cctor() in C:\Users\Tim Jones\Downloads\SPIRV-Cross.NET-main\Src\SPIRVCross\Generate\Commands.cs:line 17
   --- End of inner exception stack trace ---
   at SPIRVCross.SPIRV.spvc_context_create(spvc_context* context) in C:\Users\Tim Jones\Downloads\SPIRV-Cross.NET-main\Src\SPIRVCross\Generate\Commands.cs:line 96
   at SPIRVCross.Test.Program.Main(String[] args) in C:\Users\Tim Jones\Downloads\SPIRV-Cross.NET-main\Src\SPIRVCross.Test\Program.cs:line 62

I think this is because SPIRVCross.csproj needs to include some extra stuff to copy the native dlls to the output folder. For example, Vortice.Dxc includes a .targets file in the csproj:

https://github.com/amerkoleci/Vortice.Windows/blob/7481411e43659c6e4d25c52b705a7eadcee2cfc2/src/Vortice.Dxc/Vortice.Dxc.csproj#L28-L31

And that .targets file looks like this:

https://github.com/amerkoleci/Vortice.Windows/blob/7481411e43659c6e4d25c52b705a7eadcee2cfc2/src/Vortice.Dxc/build/netstandard2.0/Vortice.Dxc.targets#L6-L12

... unless I'm missing something, and it's working for you without doing that?

Unable to load native library on linux

It seems that the method SPIRVCross.SPIRV.LoadNativeLibrary tries to look for spirv-cross-c-shared.so but the file is actually named libspirv-cross-c-shared.so. Normally this would be fine since it should add the prefix automatically, but I guess because you give it a full path it does not do that...

I think this might also affect mac because it has the same prefix...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.