现代 C++ 仅包含头文件的交换库
This project lets you download and run GLSL shadertoys as C++17 code. Out of 1000 top shadertoys (on 26.11.2021), 840 compile without any code alterations and 953 compile if code patching is enabled. Code after patching is still GLSL-compatible and can be pasted back to the Shadertoy.
Here is the brilliant "DooM" shadertoy port by P_Malin, run as C++ code.
Another example, "Creation" by Danilo Guanabara, being debugged in Visual Studio:
The repository is structured in the following way:
include: header-only, dependency free headers that replicate GLSL syntax, types and built-in functions in C++, as completely as humanely possible. If you wish to use CxxSwizzle in your project, this directory is all you need.sandbox_template: a cross-platform shadertoy sandbox project template, using SDL2, Dear ImGui, imgui-sdl and optionally Vc (not to be confused with VC++/MSVC). vcpkg is used to resolve the dependencies. Shared by the samples and downloaded shadertoys.samples: a set of some of the best shadertoys, with a license permissive enough to have them included here.shadertoys: placeholder directory where shadertoys are downloaded totextures: placeholder directory where textures are downloaded totest: test project cmake : some very painfully concieved CMake macros that download shaders using Shadertoy API and apply tivial code fixes, if enabled. Uses json-cmake.git clone https://github.com/gwiazdorrr/CxxSwizzle.gitgit submodule update --initsudo apt-get install curl zip unzip tarninja:cmake -G Ninja -B build -DCMAKE_TOOLCHAIN_FILE=vcpkg/scripts/buildsystems/vcpkg.cmake
cd build
ninjaIf you are using CMake GUI, after clicking Configure select Specify toolchain file for cross-compiling and make sure the path in the next window points to vcpkg/scripts/buildsystems/vcpkg.cmake.
If you are on MacOS and using homebrew > 3.0, vcpkg (as of 11.2022) will have hard time finding pkg-config, so make sure to define following environment variables:
export PKG_CONFIG=/opt/homebrew/bin/pkg-config
TL;DR: set SHADERTOY_DOWNLOAD to any of top_* options and run cmake.
Downloading shadertoys takes place in CMake's configure step (not great, not terrible). Note that only shadertoys with public+api visibility can be downloaded this way. The process in controlled with following properties:
SHADERTOY_DOWNLOAD_IDS is either a single shadertoy ID (e.g. WtVfRc) or a list of shadertoy IDs (semicolon-separated)SHADERTOY_DOWNLOAD_QUERY_TYPE specifies the type of query (most loved, newest etc.) to run using Shadertoy API to obtain a list of shadertoys to download.Setting any of the above will trigger the download process. To avoid redownloading same shaders during next CMake's configure phase, both properties are forcefully set to an empty string and "none", respectively. After CMake's generate step is complete, each shadertoy should now be ready to build as a standalone C++ project.
Another properties relevant to downloading Shadertoys:
SHADERTOY_DOWNLOAD_QUERY_ARG - an optional search term for the query.SHADERTOY_DOWNLOAD_QUERY_MAX_COUNT - the upper limit of how many shadertoys to download.SHADERTOY_ROOT - a directory where shadertoys are downloaded to (./shadertoys by default)SHADERTOY_TEXTURES_ROOT - a directory where textures are downloaded to (./textures by default)SHADERTOY_APPLY_TRIVIAL_FIXES can be cheched to avoid headaches with most common GLSL to C++ problems (enabled by default).SHADERTOY_API_KEY (advanced) is the API key used to access Shadertoy API. If the default key gets rate-limited, you will need to create your own key and set the parameter.SHADERTOY_SETUP (advanced) specifies which sandbox setup to use. The default one (scalar) has no support for partial derivatives, but branches/loop work out of the box. If you are feeling adventurous check out simd variants.SHADERTOY_HTTP_USERAGENT (advanced) specifies custom User-Agent string used in HTTP requests. This is useful in case the default ("curl/x.xx.x") results in 403 response codes.SHADERTOY_DOWNLOAD_IDS to an id of any shadertoy with public+api visiblity (e.g. WtVfRc). If you want to download a batch, separate ids with a semicolon.Configure in cmake-gui or run cmakeSHADERTOY_DOWNLOAD_QUERY_TYPE to top_love, top_popular, top_newest or top_hot. SHADERTOY_DOWNLOAD_QUERY_ARG empty or set it to a search term. SHADERTOY_DOWNLOAD_QUERY_MAX_COUNT to limit how many shaders you want to download.Configure in cmake-gui or run cmakeIf SHADERTOY_APPLY_TRIVIAL_FIXES is enabled, shaders of a shadertoy are patched according to these rules:
^^ operator with !=. Could be replaced with C++'s ^, but that's not compatible with GLSL.CXX_CONST (defined as static inline constexpr). CXX_IGNORE. This is not a C++ issue, but rather a consequence of how shaders get included (in a struct scope)length() method, which is used surprisingly often. Sadly, seems there's no silver bullet here, if you want the replacement macros to be compatible with the limited preprocessor GLSL uses.int [size] foo -> CXX_ARRAY_N(int, size) fooint foo[size] -> CXX_ARRAY_N(int, size) fooint [] foo = int[](0, 1, 2, 3) -> CXX_ARRAY_FIELD(int, foo)(0, 1, 2, 3)int foo [] = int[](0, 1, 2, 3) -> CXX_ARRAY_FIELD(int, foo)(0, 1, 2, 3)int [] foo -> CXX_ARRAY(int) fooint foo[] -> CXX_ARRAY(int) fooint[](0, 1, 2, 3) -> CXX_MAKE_ARRAY(int)(0, 1, 2, 3)Note that all these macros are easily GLSL compatible:
#define CXX_CONST const
#define CXX_IGNORE(x) x
#define CXX_ARRAY_FIELD(type, name) type[] name
#define CXX_ARRAY(type) type[]
#define CXX_ARRAY_N(type, size) type[size]
#define CXX_MAKE_ARRAY(type) type[]If a shadertoy can't be downloaded there's always an option of downloading it manually.
Network tab and refresh the shadertoy webpage. Textures should be easily found in the list of requests. Save them in SHADERTOY_TEXTURES_ROOT.SHADERTOY_ROOT, say SHADERTOY_ROOT/foo. That's where passes and config need to be saved to.Image -> image.fragBuffer A -> buffer_a.fragBuffer B -> buffer_b.fragBuffer C -> buffer_c.fragBuffer D -> buffer_d.fragshadertoy_config.hpp file. This is where passes are configured. The most barebones contents are:shadertoy_config shadertoy_config::make_default()
{
shadertoy_config config;
return config;
}If the shadertoy uses textures / buffers, iChannels settings need to be replicated.
config.get_pass(pass_type::image).get_sampler(0) = sampler_config::make_buffer(pass_type::buffer_a)
.init(texture_wrap_modes::clamp, texture_filter_modes::nearest, true); config.get_pass(pass_type::buffer_a).get_sampler(1) = sampler_config::make_texture(
"foo.jpg")
.init(texture_wrap_modes::repeat, texture_filter_modes::mipmap, false); config.get_pass(pass_type::image).get_sampler(3) = sampler_config::make_cubemap({
"face_0.png",
"face_1.png",
"face_2.png",
"face_3.png",
"face_4.png",
"face_5.png"})
.init(texture_wrap_modes::clamp, texture_filter_modes::linear, false); config.get_pass(pass_type::buffer_c).get_sampler(3) = sampler_config::make_keyboard()
.init(texture_wrap_modes::clamp, texture_filter_modes::nearest, true);After that's done, run cmake.
TRACY_PROFILER_ROOT: setting to Tracy Profiler path will enable the profiler integration.Vc_IMPL: enforces specific SIMD instruction set (AVX2, SSE3 etc.) if using one of simd_vc sandbox modesENABLE_PARALLELISM: if there are any problems related to `` header or std::execution::par_unseq, unchecking this option should help - at a cost of single-threaded rendering.BUILD_SAMPLES_SIMD: builds SIMD samples. Uses Vc lib to make that happen. When configuring on Windows, you might expect something like this in the log (harmless, a result of Vc scripts being a tad outdated):c
1: fatal error C1083: Cannot open source file: 'C:/SRC/CxxSwizzle/build/vcpkg_installed/x64-windows/share/vc/msvc_version.c': No such file or directoryParts of GLSL Lang Spec 4.60 CxxSwizzle attempted at replicating:
vec2..4ivec2..4uvec2..4bvec2..4dvec2..4mat2..4mat2..4x2..4dmat2..4dmat2..4x2..4sampler1D (as sampler_generic)sampler2D (as sampler_generic)samplerCube (as sampler_generic)sampler3Dconstinoutinouthighp (no effect)mediump (no effect)lowp (no effect)length methodx compomentdiscardgl_FragCoordgl_FragColorfmafrexp, ldexpftransform暂无开放 Issues,或尚未同步最近议题。