From: patrick-scho Date: Wed, 20 Aug 2025 11:59:44 +0000 (+0200) Subject: move shader, update build.zig X-Git-Url: https://gitweb.ps.run/sporegirl/commitdiff_plain/85192a41f309b9cd98900f284e5a56e0dc22bccc move shader, update build.zig --- diff --git a/build.zig b/build.zig index 590d913..0fe3fbf 100644 --- a/build.zig +++ b/build.zig @@ -10,11 +10,9 @@ pub fn build(b: *Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const opt_docking = b.option(bool, "docking", "Build with docking support") orelse false; - // Get the matching Zig module name, C header search path and C library for // vanilla imgui vs the imgui docking branch. - const cimgui_conf = cimgui.getConfig(opt_docking); + const cimgui_conf = cimgui.getConfig(true); // note that the sokol dependency is built with `.with_sokol_imgui = true` const dep_sokol = b.dependency("sokol", .{ @@ -35,8 +33,8 @@ pub fn build(b: *Build) !void { const dep_shdc = dep_sokol.builder.dependency("shdc", .{}); const shdc_step = try sokol.shdc.createSourceFile(b, .{ .shdc_dep = dep_shdc, - .input = "src/shader/quad.glsl", - .output = "src/shader/quad.glsl.zig", + .input = "shd/quad.glsl", + .output = "src/shd/quad.glsl.zig", .slang = .{ .glsl430 = true }, }); @@ -49,10 +47,11 @@ pub fn build(b: *Build) !void { .{ .name = "sokol", .module = dep_sokol.module("sokol") }, .{ .name = cimgui_conf.module_name, .module = dep_cimgui.module(cimgui_conf.module_name) }, }, + .link_libc = true, }); - const mod_options = b.addOptions(); - mod_options.addOption(bool, "docking", opt_docking); - mod_main.addOptions("build_options", mod_options); + + mod_main.addIncludePath(b.path("src")); + mod_main.addCSourceFile(.{.file = b.path("src/stb_image.c")}); // from here on different handling for native vs wasm builds if (target.result.cpu.arch.isWasm()) { diff --git a/shd/quad.glsl b/shd/quad.glsl new file mode 100644 index 0000000..7f073c4 --- /dev/null +++ b/shd/quad.glsl @@ -0,0 +1,153 @@ +/* quad vertex shader */ +@vs vs +const vec2 positions[4] = { vec2(0, 1), vec2(1, 1), vec2(1, 0), vec2(0, 0) }; +const vec2 uvs[4] = { vec2(0, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1) }; +const int indices[6] = { 0, 1, 2, 0, 2, 3 }; + +layout(binding=0) uniform Game +{ + vec2 screen; + vec2 cam; +}; + +in vec2 inst_pos; +in ivec2 inst_size; +out vec2 uv; + +void main() { + int idx = indices[gl_VertexIndex]; + vec2 pos = positions[idx]; + uv = uvs[idx]; + + vec2 inst_pos_abs = inst_pos + inst_size * pos - cam - screen / 2; + gl_Position = vec4(inst_pos_abs / screen * 2, 1, 1); +} +@end + +/* quad fragment shader */ +@fs fs +layout(binding=0) uniform texture2D tex; +layout(binding=0) uniform sampler smp; + +in vec2 uv; +out vec4 frag_color; + +void main() { + vec4 tex_color = texture(sampler2D(tex, smp), uv); + if (tex_color.a < 1) + discard; + frag_color = tex_color; +} +@end + +/* quad shader program */ +@program quad vs fs + + + + + +@vs vs_shadow +const vec2 positions[4] = { vec2(0, 1), vec2(1, 1), vec2(1, 0), vec2(0, 0) }; +const vec2 uvs[4] = { vec2(0, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1) }; +const int indices[6] = { 0, 1, 2, 0, 2, 3 }; + +layout(binding=0) uniform Game +{ + vec2 screen; + vec2 cam; +}; + +layout(binding=1) uniform Light +{ + vec2 light_pos; +}; + +in vec2 inst_pos; +in ivec2 inst_size; +out vec2 uv; + +vec2 rotate(vec2 v, float a) { + float s = sin(a); + float c = cos(a); + mat2 m = mat2(c, s, -s, c); + return m * v; +} + +void main() { + int idx = indices[gl_VertexIndex]; + vec2 pos = positions[idx]; + uv = uvs[idx]; + + if (idx == 2 || idx == 3) { // keep pos for lower vertices (base) + vec2 inst_pos_abs = inst_pos + inst_size * pos - cam - screen / 2; + gl_Position = vec4(inst_pos_abs / screen * 2, 1, 1); + } + else { + vec2 anchor = vec2(0.5, 0); + vec2 anchor_abs = inst_pos + inst_size * anchor - cam - screen / 2; + vec2 light_pos_abs = light_pos - cam - screen / 2; + vec2 dir = anchor_abs - light_pos_abs; + + vec2 left_right[] = { + rotate(dir, 1.5708), + rotate(dir, -1.5708), + }; + + vec2 inst_pos_abs = anchor_abs + dir + left_right[idx] / 2; + gl_Position = vec4(inst_pos_abs / screen * 2, 1, 1); + } +} +@end + +@fs fs_shadow +layout(binding=0) uniform texture2D tex; +layout(binding=0) uniform sampler smp; + +in vec2 uv; +out vec4 frag_color; + +void main() { + vec4 tex_color = texture(sampler2D(tex, smp), uv); + // if (tex_color.a < 1) + // discard; + frag_color = vec4(0, 0, 0, 1); +} +@end + +@program shadow vs_shadow fs_shadow + + + +@vs vs_light +const vec2 positions[4] = { vec2(-0.5, 0.5), vec2(0.5, 0.5), vec2(0.5, -0.5), vec2(-0.5, -0.5) }; +const int indices[6] = { 0, 1, 2, 0, 2, 3 }; + +layout(binding=0) uniform Game +{ + vec2 screen; + vec2 cam; +}; + +in vec2 light_pos; + +void main() { + int idx = indices[gl_VertexIndex]; + vec2 pos = positions[idx]; + + vec2 light_size = vec2(10, 10); + + vec2 inst_pos_abs = light_pos + light_size * pos - cam - screen / 2; + gl_Position = vec4(inst_pos_abs / screen * 2, 1, 1); +} +@end + +@fs fs_light +out vec4 frag_color; + +void main() { + frag_color = vec4(1, 0, 0, 1); +} +@end + +@program light vs_light fs_light diff --git a/src/shader/quad.glsl b/src/shader/quad.glsl deleted file mode 100644 index b3e7e5c..0000000 --- a/src/shader/quad.glsl +++ /dev/null @@ -1,25 +0,0 @@ -/* quad vertex shader */ -@vs vs -in vec4 position; -in vec4 color0; -out vec4 color; - -void main() { - gl_Position = position; - color = color0; -} -@end - -/* quad fragment shader */ -@fs fs -in vec4 color; -out vec4 frag_color; - -void main() { - frag_color = color; -} -@end - -/* quad shader program */ -@program quad vs fs -