Skip to content

danilw/vulkan-shader-launcher

Repository files navigation

vulkan shader launcher

what is it creating simple minimal and Valid "shader launcher" using Vulkan and C, for launch any shaders(from shadertoy or any else). Without validation errors and crossplatform.

Used Vulkan 1.0 and only single extension VK_KHR_swapchain VK_KHR_SWAPCHAIN_EXTENSION_NAME that on 100% devices with Vulkan.

Download example build from Releases.

Minimal binary build size, after upx compression, ~30kb (without built-in shaders in example_minimal). Using yari-v to compress shaders, in example_fbo size of bin(exe) with shaders ~50Kb.

Do not use UPX - reason upx/upx#337

Dependencies - only vulkan library(sdk), and win32/X11/Wayland for window and keyboard/mouse control.

Source of code - I made this code following cube-demo from VulkanSDK and vktut, this my code released under The MIT License.

Compiler/platform support - VisualStudio, GCC/Clang, Mingw. OS Windows and Linux(X11/Wayland)

Wayland - minimal setup with stable xdg-shell. Added hotkey f as example of resize event.


Example minimal - single shader loader, shader from file. Control: Keyboard 1-debug, 2-vsynk 60fps, Space-pause. Binary build(exe) 30kb size.

Example game - single shader game example, drawing many elements in vulkan-drawcall loop, using Blend to draw mix color. Also using yari-v to compress shaders, and shaders build in to binary file. This example on video youtube link.

Example FBO - base on this shader game that on screenshot, game logic on GPU, using framebuffer to store/read data, also yari-v and build in shaders.

Example images - imgages/textures loading. Used shader to test mipmaps, mipmaps supported. Default image format is SRGB, to change edit line 125 example_images/main.c.

use Validation layer - use VK_validation.sh script for that. Requires VulkanSDK installed. This shader launcher code does not have validation errors.

Shadertoy launcher - proper launcher for shadertoy shaders, 4 framebuffers, texture support. Moved to its own repository vulkan-shadertoy-launcher.


Building

Use cmake to build.

Using VS2019 in Windows (open links to see tutorial screenshot):

  1. Download and install Vulkan SDK.
  2. Launch VS press Continue without code and File-Open-CMake select CMakeLists.txt file from one of the example_ folders.
  3. Press Manage Configureations and Add new configuration select x64 Release.
  4. In General Configuration type set MinSizeRel and press Ctrl+S to save.
  5. Then select created configuration and press Build-Build All.
  6. Compiled build location example_\out\build\x64-Release<name>.exe.

In Linux use command:

cd example_minimal
mkdir build
cd build
cmake ..
make

To build with Wayland in CMakeLists.txt set OPTION(USE_WAYLAND_LINUX "use Wayland for Linux" ON) by default used X11.

Building without cmake:

Build with gcc (linux): (to build with clang change gcc to clang)

X11:

gcc -DVK_USE_PLATFORM_XCB_KHR -O2 -s ../vk_utils/vk_utils.c ../vk_utils/vk_error_print.c ../vk_utils/vk_render_helper.c main.c -o VKexample -lm -lxcb -lvulkan

Wayland:

cp ../wayland_xdg/xdg-shell-client-protocol.c xdg-shell-client-protocol.c
cp ../wayland_xdg/xdg-shell-client-protocol.h xdg-shell-client-protocol.h
gcc -DVK_USE_PLATFORM_WAYLAND_KHR -O2 -s -I. -I/usr/include/wayland ../vk_utils/vk_utils.c ../vk_utils/vk_error_print.c ../vk_utils/vk_render_helper.c xdg-shell-client-protocol.c main.c -o VKexample -lm -lvulkan -lwayland-client

to generare Wayland xdg-shell headers (from wayland_xdg folder):

wayland_protocols_dir=$(pkg-config --variable=pkgdatadir wayland-protocols)
wayland-scanner client-header $wayland_protocols_dir/stable/xdg-shell/xdg-shell.xml xdg-shell-client-protocol.h
wayland-scanner private-code $wayland_protocols_dir/stable/xdg-shell/xdg-shell.xml xdg-shell-client-protocol.c

Build with mingw64 (vulkan-1.dll from VulkanSDK, vulkan.h in system(cygwin or native) path):

x86_64-w64-mingw32-gcc -DVK_USE_PLATFORM_WIN32_KHR -O3 -s ../vk_utils/vk_utils.c ../vk_utils/vk_error_print.c ../vk_utils/vk_render_helper.c main.c -o VKexample.exe -lm -mwindows <path to>/vulkan-1.dll

(in the example game and fbo add -DYARIV_SHADER to build command to have yari-v shaders in the bin(exe), to compile yari-v shader use shaders/build_shaders_yariv.sh)

Images:

img

Video:

youtube