All Projects → dfranx → Spirv Vm

dfranx / Spirv Vm

Licence: mit
Virtual machine for executing SPIR-V

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Spirv Vm

Reshade
A generic post-processing injector for games and video software.
Stars: ✭ 2,285 (+1220.81%)
Mutual labels:  spir-v, glsl, shaders, hlsl
Glslang
Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.
Stars: ✭ 2,034 (+1075.72%)
Mutual labels:  spir-v, glsl, shader, hlsl
Shaderconductor
ShaderConductor is a tool designed for cross-compiling HLSL to other shading languages
Stars: ✭ 1,146 (+562.43%)
Mutual labels:  spir-v, glsl, shader, hlsl
3d Game Shaders For Beginners
🎮 A step-by-step guide to implementing SSAO, depth of field, lighting, normal mapping, and more for your 3D game.
Stars: ✭ 11,698 (+6661.85%)
Mutual labels:  glsl, shaders, shader, hlsl
Hlsl2glslfork
HLSL to GLSL language translator based on ATI's HLSL2GLSL. Used in Unity.
Stars: ✭ 488 (+182.08%)
Mutual labels:  glsl, shaders, hlsl
Pmtech
Lightweight, multi-platform, data-oriented game engine.
Stars: ✭ 478 (+176.3%)
Mutual labels:  spir-v, glsl, hlsl
Gpu Gems Book Source Code
💿 CD Content ( Source Code ) Collection of Book <GPU Gems > 1~ 3 | 《GPU精粹》 1~ 3 随书CD(源代码)珍藏
Stars: ✭ 567 (+227.75%)
Mutual labels:  glsl, shader, hlsl
Glsl Godrays
This module implements a volumetric light scattering effect(godrays)
Stars: ✭ 155 (-10.4%)
Mutual labels:  glsl, shaders, shader
ios-spritekit-shader-sandbox
👾 Collection of custom effects for SpriteKit implemented using GLSL/Metal shaders.
Stars: ✭ 63 (-63.58%)
Mutual labels:  shaders, glsl, shader
Slang
Making it easier to work with shaders
Stars: ✭ 627 (+262.43%)
Mutual labels:  glsl, shaders, hlsl
Shader Printf
Simple printf functionality for GLSL.
Stars: ✭ 170 (-1.73%)
Mutual labels:  glsl, shader, debugging
Shadergen
Proof-of-concept library for generating HLSL, GLSL, and Metal shader code from C#,
Stars: ✭ 395 (+128.32%)
Mutual labels:  glsl, shaders, hlsl
Curtainsjs
curtains.js is a lightweight vanilla WebGL javascript library that turns HTML DOM elements into interactive textured planes.
Stars: ✭ 1,039 (+500.58%)
Mutual labels:  glsl, shaders, shader
Glsl Worley
Worley noise implementation for WebGL shaders
Stars: ✭ 66 (-61.85%)
Mutual labels:  glsl, shaders, shader
Thebookofshaders
Step-by-step guide through the abstract and complex universe of Fragment Shaders.
Stars: ✭ 4,070 (+2252.6%)
Mutual labels:  glsl, shaders, shader
Bonzomatic
Live shader coding tool and Shader Showdown workhorse
Stars: ✭ 829 (+379.19%)
Mutual labels:  glsl, shader, hlsl
glsl-cos-palette
glsl function for making cosine palettes
Stars: ✭ 26 (-84.97%)
Mutual labels:  shaders, glsl, shader
ada
A general porpose OpenGL app library
Stars: ✭ 105 (-39.31%)
Mutual labels:  glsl, shader, hlsl
Tess Opt
Demonstration of how we can use tessellation shaders to make faster fragment shaders.
Stars: ✭ 13 (-92.49%)
Mutual labels:  glsl, shaders, shader
Hlslexplorer
See how hardware understands your HLSL
Stars: ✭ 91 (-47.4%)
Mutual labels:  shaders, shader, hlsl

SPIRV-VM

SPIRV-VM is a virtual machine for executing SPIR-V shaders. It is written in C, has no dependencies & is licensed under MIT license. Both HLSL & GLSL shaders can be compiled to SPIR-V using tools such as glslangValidator and shaderc which means that you can use this library to debug shaders.

Example

First, create a SPIRV-VM context:

spvm_context_t ctx = spvm_context_initialize();

Load your SPIR-V binary file:

size_t spv_length = 0;
spvm_source spv = load_source("shader.spv", &spv_length);

Now you can create a SPIR-V program and a state. The program holds general information about the SPIR-V file (like generator version, used capabilities, etc...) while the spvm_state holds information about state of program while executing it. The idea behind states is to make it possible, for example, to create four states for one program and run the same shader simultaneously in 4 different threads (this is not tested though):

spvm_program_t prog = spvm_program_create(ctx, spv, spv_length);
spvm_state_t state = spvm_state_create(prog);

You have to add extensions manually (if your program uses it):

spvm_ext_opcode_func* glsl_ext_data = spvm_build_glsl450_ext();
spvm_result_t glsl_std_450 = spvm_state_get_result(state, "GLSL.std.450");
if (glsl_std_450)
	glsl_std_450->extension = glsl_ext_data;

Before debugging your shader, you have to initialize global and uniform variables.

To set a single uniform:

float someUniformData[2] = { 0.5f, 0.6f };
spvm_result_t someUniform = spvm_state_get_result(state, "someUniform");
spvm_member_set_value_f(someUniform->members, someUniform->member_count, someUniformData); // vec2

But if you are using newer GLSL version, you probably have to deal with interface blocks:

// first get the block
spvm_result_t uBlock = spvm_state_get_result(state, "uBlock");

// then set its members
float timeData = 0.5f;
spvm_member_t uBlock_time = spvm_state_get_object_member(state, uBlock, "time"); // uBlock.time
spvm_member_set_value_f(uBlock_time->members, uBlock_time->member_count, &timeData);

To bind textures:

spvm_image noise2D_data;
spvm_image_create(&noise2D_data, image_data, image_width, image_height, 1);
spvm_result_t noise2D = spvm_state_get_result(state, "noise2D");
noise2D->members[0].image_data = &noise2D_data;

You can run your shader in two different ways. You can step line by line through the code. This can be done with the spvm_state_step_into function (which executes only one line). Or you can execute whole shader with a single function call:

spvm_word fnMain = spvm_state_get_result_location(state, "main");
spvm_state_prepare(state, fnMain);
spvm_state_call_function(state);

You can then retrieve results:

spvm_result_t outColor = spvm_state_get_result(state, "outColor");
for (int i = 0; i < outColor->member_count; i++)
	printf("%.2f ", outColor->members[i].value.f);
printf("\n");

Use spvm_state_get_local_result() function if you are stepping through code line by line and want to get local variable's value. Example:

spvm_result_t a = spvm_state_get_local_result(state, fnMain, "a");
printf("a = %.2f\n", a->members[0].value.f);

Don't forget to free the memory:

spvm_state_delete(state);
spvm_program_delete(prog);
free(glsl_ext_data);
free(spv);

spvm_context_deinitialize(ctx);

How to link

If you are using CMake add these lines to your CMakeLists.txt file:

add_subdirectory(./path/to/your/SPIRV-VM)
target_include_directories(example PRIVATE ./path/to/your/SPIRV-VM/inc)

If you just want to build this project, run these two commands:

cmake .
make

TODO

  • better image support (mipmaps, image arrays, samplers)

Contact me on this e-mail address: dfranx at shadered dot org

LICENSE

SPIRV-VM is licensed under MIT license. See LICENSE for more details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].