From 6362c8fa26013f6113b6689a40f8b19774afac46 Mon Sep 17 00:00:00 2001 From: Andre Weissflog Date: Mon, 12 Feb 2024 14:34:16 +0100 Subject: [PATCH] fix a confusing issue around '#if' vs '#ifdef' conditional compilation in shaders (fixes #118) --- CHANGELOG.md | 18 ++++++++++++++++++ src/shdc/spirv.cc | 16 ++++++++++++---- test/sapp/primtypes-sapp.glsl | 4 +++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 626727e0..59846f93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,24 @@ CHANGELOG ========= +#### **12-Feb-2024** + +Fixed a confusing behaviour when writing conditional shader code via +`#if` vs `#ifdef`. Previously, when a shader is compiled it would get +an implicit definition block at the start looking like this: + +``` +#define SOKOL_GLSL (1) +#define SOKOL_HLSL (0) +#define SOKOL_MSL (0) +#define SOKOL_WGSL (0) +``` + +...meaning an `#ifdef` would always trigger and only `#if` had worked. + +Now only the define for the current output language is present, so that +both `#if` and `#ifdef` works. + #### **29-Oct-2023** Fix a reflection parsing regression that was introduced in the last diff --git a/src/shdc/spirv.cc b/src/shdc/spirv.cc index 50b70072..7743695a 100644 --- a/src/shdc/spirv.cc +++ b/src/shdc/spirv.cc @@ -27,10 +27,18 @@ extern const TBuiltInResource DefaultTBuiltInResource; /* merge shader snippet source into a single string */ static std::string merge_source(const input_t& inp, const snippet_t& snippet, slang_t::type_t slang, const std::vector& defines) { std::string src = "#version 450\n"; - src += fmt::format("#define SOKOL_GLSL ({})\n", slang_t::is_glsl(slang) ? 1 : 0); - src += fmt::format("#define SOKOL_HLSL ({})\n", slang_t::is_hlsl(slang) ? 1 : 0); - src += fmt::format("#define SOKOL_MSL ({})\n", slang_t::is_msl(slang) ? 1 : 0); - src += fmt::format("#define SOKOL_WGSL ({})\n", slang_t::is_wgsl(slang) ? 1 : 0); + if (slang_t::is_glsl(slang)) { + src += "#define SOKOL_GLSL (1)\n"; + } + if (slang_t::is_hlsl(slang)) { + src += "#define SOKOL_HLSL (1)\n"; + } + if (slang_t::is_msl(slang)) { + src += "#define SOKOL_MSL (1)\n"; + } + if (slang_t::is_wgsl(slang)) { + src += "#define SOKOL_WGSL (1)\n"; + } for (const std::string& define : defines) { src += fmt::format("#define {} (1)\n", define); } diff --git a/test/sapp/primtypes-sapp.glsl b/test/sapp/primtypes-sapp.glsl index 2bb4a62f..842d9100 100644 --- a/test/sapp/primtypes-sapp.glsl +++ b/test/sapp/primtypes-sapp.glsl @@ -12,7 +12,10 @@ out vec4 color; void main() { gl_Position = mvp * vec4(position.xy, 0, 1); + // WebGPU doesn't support point size + #ifndef SOKOL_WGSL gl_PointSize = point_size; + #endif color = color0; } @end @@ -26,4 +29,3 @@ void main() { @end @program primtypes vs fs -