Skip to content

Commit

Permalink
fix a confusing issue around '#if' vs '#ifdef' conditional compilatio…
Browse files Browse the repository at this point in the history
…n in shaders (fixes #118)
  • Loading branch information
floooh committed Feb 12, 2024
1 parent 06a8956 commit 6362c8f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 12 additions & 4 deletions src/shdc/spirv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& 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);
}
Expand Down
4 changes: 3 additions & 1 deletion test/sapp/primtypes-sapp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,4 +29,3 @@ void main() {
@end

@program primtypes vs fs

0 comments on commit 6362c8f

Please sign in to comment.