Skip to content
This repository was archived by the owner on Dec 1, 2025. It is now read-only.

Commit 27b13d1

Browse files
committed
Template WIP
1 parent 528108d commit 27b13d1

File tree

4 files changed

+89
-10
lines changed

4 files changed

+89
-10
lines changed

gpu_template/build.zig

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
const std = @import("std");
22

3-
fn shadercross(b: *std.Build, sdl_dep_lib: *std.Build.Step.Compile) *std.Build.Step.Compile {
4-
const upstream = b.lazyDependency("sdl_shadercross", .{}) orelse return;
3+
const ShaderFormat = enum {
4+
vertex,
5+
fragment,
6+
compute,
7+
};
58

9+
const OutputFormat = enum {
10+
dxbc,
11+
dxil,
12+
msl,
13+
spirv,
14+
hlsl,
15+
};
16+
17+
fn shadercross(b: *std.Build, sdl3: *std.Build.Dependency) *std.Build.Step.Compile {
618
const exe = b.addExecutable(.{
719
.name = "shadercross",
820
.root_module = b.createModule(.{
921
.target = b.graph.host,
1022
.link_libc = true,
1123
}),
1224
});
13-
exe.root_module.linkLibrary(sdl_dep_lib);
25+
exe.root_module.linkLibrary(sdl3.builder.dependency("sdl", .{ .target = b.graph.host }).artifact("SDL3"));
1426

27+
const upstream = sdl3.builder.dependency("sdl_shadercross", .{ .target = b.graph.host });
1528
exe.root_module.addIncludePath(upstream.path("include"));
1629
exe.root_module.addIncludePath(upstream.path("src"));
1730

@@ -25,8 +38,10 @@ fn shadercross(b: *std.Build, sdl_dep_lib: *std.Build.Step.Compile) *std.Build.S
2538

2639
exe.installHeadersDirectory(upstream.path("include"), "", .{});
2740

28-
const spirv_headers = b.dependency("spirv_headers", .{});
29-
const spirv_cross = b.dependency("spirv_cross", .{
41+
const spirv_headers = sdl3.builder.dependency("spirv_headers", .{
42+
.target = b.graph.host,
43+
});
44+
const spirv_cross = sdl3.builder.dependency("spirv_cross", .{
3045
.target = b.graph.host,
3146
.optimize = .ReleaseFast, // There is a C bug in spirv-cross upstream! Ignore undefined behavior for now.
3247
.spv_cross_reflect = true,
@@ -42,12 +57,35 @@ fn setupShader(
4257
b: *std.Build,
4358
module: *std.Build.Module,
4459
name: []const u8,
45-
) !void {}
60+
shadercross_exe: *std.Build.Step.Compile,
61+
debug: bool,
62+
output_format: OutputFormat,
63+
) !void {
64+
const name_ext = name[std.mem.indexOf(u8, name, ".").?..];
65+
const format: ShaderFormat = if (std.mem.eql(u8, name_ext, ".vert"))
66+
.vertex
67+
else if (std.mem.eql(u8, name_ext, ".frag"))
68+
.fragment
69+
else
70+
.compute;
71+
const run_shadercross = b.addRunArtifact(shadercross_exe);
72+
const upper = try std.ascii.allocUpperString(b.allocator, @tagName(output_format));
73+
run_shadercross.addFileArg(b.path(b.fmt("{s}/{s}.hlsl", .{ "shaders", name })));
74+
run_shadercross.addArgs(&.{ "--source", "HLSL", "--entrypoint", "main", "--stage", @tagName(format), "--dest", upper });
75+
if (debug)
76+
run_shadercross.addArg("--debug");
77+
run_shadercross.addArg("--output");
78+
const output = run_shadercross.addOutputFileArg(b.fmt("{s}.{s}", .{ name, @tagName(output_format) }));
79+
module.addAnonymousImport(name, .{ .root_source_file = output });
80+
}
4681

4782
fn buildShaders(
4883
b: *std.Build,
4984
exe: *std.Build.Step.Compile,
50-
) void {
85+
shadercross_exe: *std.Build.Step.Compile,
86+
debug: bool,
87+
output_format: OutputFormat,
88+
) !void {
5189
var dir = (try std.fs.openDirAbsolute(b.path("shaders").getPath(b), .{ .iterate = true }));
5290
defer dir.close();
5391
var dir_iterator = try dir.walk(b.allocator);
@@ -57,12 +95,19 @@ fn buildShaders(
5795
const extension = ".hlsl";
5896
if (!std.mem.endsWith(u8, file.basename, extension))
5997
continue;
60-
try setupShader(b, exe.root_module, file.basename[0..(file.basename.len - extension.len)], format);
98+
try setupShader(
99+
b,
100+
exe.root_module,
101+
file.basename[0..(file.basename.len - extension.len)],
102+
shadercross_exe,
103+
debug,
104+
output_format,
105+
);
61106
}
62107
}
63108
}
64109

65-
pub fn build(b: *std.Build) void {
110+
pub fn build(b: *std.Build) !void {
66111
const target = b.standardTargetOptions(.{});
67112
const optimize = b.standardOptimizeOption(.{});
68113

@@ -83,6 +128,17 @@ pub fn build(b: *std.Build) void {
83128
.ext_image = true,
84129
});
85130
exe.root_module.addImport("sdl3", sdl3.module("sdl3"));
131+
const shadercross_exe = shadercross(
132+
b,
133+
sdl3,
134+
);
135+
try buildShaders(
136+
b,
137+
exe,
138+
shadercross_exe,
139+
b.option(bool, "shader_debug", "If have debug info in the shaders") orelse false,
140+
b.option(OutputFormat, "shader_format", "Output shader format") orelse .spirv,
141+
);
86142
b.installArtifact(exe);
87143

88144
const run_cmd = b.addRunArtifact(exe);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct Input
2+
{
3+
float3 Position : TEXCOORD0;
4+
float4 Color : TEXCOORD1;
5+
};
6+
7+
struct Output
8+
{
9+
float4 Color : TEXCOORD0;
10+
float4 Position : SV_Position;
11+
};
12+
13+
Output main(Input input)
14+
{
15+
Output output;
16+
output.Color = input.Color;
17+
output.Position = float4(input.Position, 1.0f);
18+
return output;
19+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
float4 main(float4 Color : TEXCOORD0) : SV_Target0
2+
{
3+
return Color;
4+
}

gpu_template/src/main.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn importShader(shader: anytype) Shader {
2828
};
2929
}
3030
const vert_shader = importShader(@import("positionColor.vert.zig"));
31-
const frag_shader = importShader(@import("SolidColor.frag.zig"));
31+
const frag_shader = importShader(@import("solidColor.frag.zig"));
3232

3333
const window_width = 640;
3434
const window_height = 480;

0 commit comments

Comments
 (0)