Skip to content

Commit 0da8c17

Browse files
committed
Fix shader compile/load on windows.
1 parent f464e57 commit 0da8c17

4 files changed

Lines changed: 48 additions & 11 deletions

File tree

.github/workflows/test.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444

4545
- name: Install Zig
4646
uses: mlugg/setup-zig@v2
47+
4748
- name: Lint
4849
run: zig fmt --check . --exclude libs/
4950

@@ -71,6 +72,8 @@ jobs:
7172

7273
- name: Install Zig
7374
uses: mlugg/setup-zig@v2
75+
with:
76+
cache-size-limit: 4096
7477

7578
- name: Build examples
7679
shell: bash

build.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub fn build(b: *std.Build) !void {
2424
const options_module = options_step.createModule();
2525
_ = options_module; // autofix
2626

27+
const use_lld = !target.result.os.tag.isDarwin();
28+
2729
const common_options = [_][]const u8{
2830
"-fno-sanitize=undefined", // Spentime... 3 fucking days... and randomly found this https://ruoyusun.com/2022/02/27/zig-cc.html (Thx ;))
2931
"-fno-strict-aliasing",
@@ -58,6 +60,7 @@ pub fn build(b: *std.Build) !void {
5860
.optimize = optimize,
5961
}),
6062
.use_llvm = true,
63+
.use_lld = use_lld,
6164
});
6265
b.installArtifact(combine_shader_parts);
6366

@@ -69,6 +72,7 @@ pub fn build(b: *std.Build) !void {
6972
.optimize = optimize,
7073
}),
7174
.use_llvm = true,
75+
.use_lld = use_lld,
7276
});
7377
b.installArtifact(combine_shaders);
7478

@@ -83,6 +87,7 @@ pub fn build(b: *std.Build) !void {
8387
.optimize = optimize,
8488
}),
8589
.use_llvm = true,
90+
.use_lld = use_lld,
8691
});
8792
bx.addCSourceFiles(.{
8893
.flags = &cxx_options,
@@ -105,6 +110,7 @@ pub fn build(b: *std.Build) !void {
105110
.optimize = optimize,
106111
}),
107112
.use_llvm = true,
113+
.use_lld = use_lld,
108114
});
109115
bimg.addCSourceFiles(.{
110116
.flags = &cxx_options,
@@ -133,6 +139,7 @@ pub fn build(b: *std.Build) !void {
133139
.optimize = optimize,
134140
}),
135141
.use_llvm = true,
142+
.use_lld = use_lld,
136143
});
137144
b.installArtifact(bgfx);
138145
bxInclude(b, bgfx, target, optimize);
@@ -243,6 +250,7 @@ pub fn build(b: *std.Build) !void {
243250
.optimize = options.shaderc_optimize,
244251
}),
245252
.use_llvm = true,
253+
.use_lld = use_lld,
246254
});
247255

248256
b.installArtifact(shaderc);
@@ -330,6 +338,7 @@ pub fn build(b: *std.Build) !void {
330338
.optimize = options.shaderc_optimize,
331339
}),
332340
.use_llvm = true,
341+
.use_lld = use_lld,
333342
});
334343

335344
fcpp_lib.addIncludePath(b.path(fcpp_path));
@@ -365,6 +374,7 @@ pub fn build(b: *std.Build) !void {
365374
.optimize = options.shaderc_optimize,
366375
}),
367376
.use_llvm = true,
377+
.use_lld = use_lld,
368378
});
369379
spirv_opt_lib.addIncludePath(b.path(spirv_opt_path));
370380
spirv_opt_lib.addIncludePath(b.path(spirv_opt_path ++ "include"));
@@ -400,6 +410,7 @@ pub fn build(b: *std.Build) !void {
400410
.optimize = options.shaderc_optimize,
401411
}),
402412
.use_llvm = true,
413+
.use_lld = use_lld,
403414
});
404415
spirv_cross_lib.addIncludePath(b.path(spirv_cross_path ++ "include"));
405416
spirv_cross_lib.addCSourceFiles(.{
@@ -439,6 +450,7 @@ pub fn build(b: *std.Build) !void {
439450
.optimize = options.shaderc_optimize,
440451
}),
441452
.use_llvm = true,
453+
.use_lld = use_lld,
442454
});
443455
glslang_lib.addIncludePath(b.path("libs/bgfx/3rdparty"));
444456
glslang_lib.addIncludePath(b.path(glslang_path));
@@ -512,6 +524,7 @@ pub fn build(b: *std.Build) !void {
512524
.optimize = options.shaderc_optimize,
513525
}),
514526
.use_llvm = true,
527+
.use_lld = use_lld,
515528
});
516529
glsl_optimizer_lib.addIncludePath(b.path(glsl_optimizer_path ++ "include"));
517530
glsl_optimizer_lib.addIncludePath(b.path(glsl_optimizer_path ++ "src"));

build.zig.zon

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
.{
22
.name = .zbgfx,
33
.fingerprint = 0xc48ed871c4086e4a,
4-
.version = "0.8.0",
4+
.version = "0.8.1",
55
.minimum_zig_version = "0.15.2",
66
.paths = .{
77
"includes",
88
"libs",
99
"shaders",
1010
"src",
11-
"tools",
1211
"build.zig",
1312
"build.zig.zon",
1413
},

src/shaderc.zig

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,19 +295,27 @@ pub fn compileShader(
295295
const varying_f = try std.fs.createFileAbsolute(varying_file_path, .{});
296296
try varying_f.writeAll(varying);
297297
varying_f.close();
298-
299298
defer std.fs.deleteFileAbsolute(varying_file_path) catch undefined;
300299

300+
const use_file_output = builtin.os.tag == .windows; // FIXME: Problem only on windows. Load shader in bgfx failed.
301+
301302
// Create shader output path
302-
var out_random_path: [RANDOM_PATH_LEN]u8 = undefined;
303-
generateRandomFileName(&out_random_path);
304-
const out_file_path = try std.fs.path.join(allocator, &.{ tmp_dir_path, &out_random_path });
305-
defer allocator.free(out_file_path);
303+
var out_file_path: ?[]u8 = null;
304+
defer {
305+
if (out_file_path) |p| allocator.free(p);
306+
}
306307

307308
var new_options = options;
308309
new_options.inputFilePath = source_file_path;
309310
new_options.varyingFilePath = varying_file_path;
310311

312+
if (use_file_output) {
313+
var out_random_path: [RANDOM_PATH_LEN]u8 = undefined;
314+
generateRandomFileName(&out_random_path);
315+
out_file_path = try std.fs.path.join(allocator, &.{ tmp_dir_path, &out_random_path });
316+
new_options.outputFilePath = out_file_path;
317+
}
318+
311319
var shadercp = try shadercProcess(allocator, executable_path, new_options);
312320

313321
var buffer: [1024]u8 = undefined;
@@ -326,12 +334,26 @@ pub fn compileShader(
326334
if (term.Exited != 0) {
327335
const out = try writer.toOwnedSlice();
328336
defer allocator.free(out);
329-
std.log.err("Shaderc error:\n{s}", .{out[10..]});
337+
std.log.err("Shaderc error:\n{s}", .{if (use_file_output) out else out[10..]});
330338
return error.ShaderCompileError;
331339
} else {
332-
const data = try writer.toOwnedSlice();
333-
// std.log.debug("BEGIN\n{s}\nEND", .{data});
334-
return data;
340+
if (out_file_path) |output_path| {
341+
defer std.fs.deleteFileAbsolute(output_path) catch undefined;
342+
343+
const out_f = try std.fs.openFileAbsolute(output_path, .{ .mode = .read_only });
344+
defer out_f.close();
345+
346+
const size = try out_f.getEndPos();
347+
const data = try allocator.alloc(u8, size);
348+
_ = try out_f.readAll(data);
349+
350+
// std.log.debug("BEGIN\n{s}\nEND", .{data});
351+
return data;
352+
} else {
353+
const data = try writer.toOwnedSlice();
354+
// std.log.debug("BEGIN\n{s}\nEND", .{data});
355+
return data;
356+
}
335357
}
336358
}
337359

0 commit comments

Comments
 (0)