Skip to content

Commit 7f57fbf

Browse files
committed
Fix shader compile/load on windows.
1 parent c4cd773 commit 7f57fbf

3 files changed

Lines changed: 43 additions & 11 deletions

File tree

build.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub fn build(b: *std.Build) !void {
5656
.optimize = optimize,
5757
}),
5858
.use_llvm = true,
59+
.use_lld = true,
5960
});
6061
b.installArtifact(combine_shader_parts);
6162

@@ -67,6 +68,7 @@ pub fn build(b: *std.Build) !void {
6768
.optimize = optimize,
6869
}),
6970
.use_llvm = true,
71+
.use_lld = true,
7072
});
7173
b.installArtifact(combine_shaders);
7274

@@ -81,6 +83,7 @@ pub fn build(b: *std.Build) !void {
8183
.optimize = optimize,
8284
}),
8385
.use_llvm = true,
86+
.use_lld = true,
8487
});
8588
bx.addCSourceFiles(.{
8689
.flags = &cxx_options,
@@ -103,6 +106,7 @@ pub fn build(b: *std.Build) !void {
103106
.optimize = optimize,
104107
}),
105108
.use_llvm = true,
109+
.use_lld = true,
106110
});
107111
bimg.addCSourceFiles(.{
108112
.flags = &cxx_options,
@@ -131,6 +135,7 @@ pub fn build(b: *std.Build) !void {
131135
.optimize = optimize,
132136
}),
133137
.use_llvm = true,
138+
.use_lld = true,
134139
});
135140
b.installArtifact(bgfx);
136141
bxInclude(b, bgfx, target, optimize);
@@ -236,6 +241,7 @@ pub fn build(b: *std.Build) !void {
236241
.optimize = options.shaderc_optimize,
237242
}),
238243
.use_llvm = true,
244+
.use_lld = true,
239245
});
240246

241247
b.installArtifact(shaderc);
@@ -323,6 +329,7 @@ pub fn build(b: *std.Build) !void {
323329
.optimize = options.shaderc_optimize,
324330
}),
325331
.use_llvm = true,
332+
.use_lld = true,
326333
});
327334

328335
fcpp_lib.addIncludePath(b.path(fcpp_path));
@@ -358,6 +365,7 @@ pub fn build(b: *std.Build) !void {
358365
.optimize = options.shaderc_optimize,
359366
}),
360367
.use_llvm = true,
368+
.use_lld = true,
361369
});
362370
spirv_opt_lib.addIncludePath(b.path(spirv_opt_path));
363371
spirv_opt_lib.addIncludePath(b.path(spirv_opt_path ++ "include"));
@@ -393,6 +401,7 @@ pub fn build(b: *std.Build) !void {
393401
.optimize = options.shaderc_optimize,
394402
}),
395403
.use_llvm = true,
404+
.use_lld = true,
396405
});
397406
spirv_cross_lib.addIncludePath(b.path(spirv_cross_path ++ "include"));
398407
spirv_cross_lib.addCSourceFiles(.{
@@ -432,6 +441,7 @@ pub fn build(b: *std.Build) !void {
432441
.optimize = options.shaderc_optimize,
433442
}),
434443
.use_llvm = true,
444+
.use_lld = true,
435445
});
436446
glslang_lib.addIncludePath(b.path("libs/bgfx/3rdparty"));
437447
glslang_lib.addIncludePath(b.path(glslang_path));
@@ -505,6 +515,7 @@ pub fn build(b: *std.Build) !void {
505515
.optimize = options.shaderc_optimize,
506516
}),
507517
.use_llvm = true,
518+
.use_lld = true,
508519
});
509520
glsl_optimizer_lib.addIncludePath(b.path(glsl_optimizer_path ++ "include"));
510521
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)