Skip to content

Commit 879d68c

Browse files
committed
Fix shader compile/load on windows.
1 parent f464e57 commit 879d68c

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
@@ -58,6 +58,7 @@ pub fn build(b: *std.Build) !void {
5858
.optimize = optimize,
5959
}),
6060
.use_llvm = true,
61+
.use_lld = true,
6162
});
6263
b.installArtifact(combine_shader_parts);
6364

@@ -69,6 +70,7 @@ pub fn build(b: *std.Build) !void {
6970
.optimize = optimize,
7071
}),
7172
.use_llvm = true,
73+
.use_lld = true,
7274
});
7375
b.installArtifact(combine_shaders);
7476

@@ -83,6 +85,7 @@ pub fn build(b: *std.Build) !void {
8385
.optimize = optimize,
8486
}),
8587
.use_llvm = true,
88+
.use_lld = true,
8689
});
8790
bx.addCSourceFiles(.{
8891
.flags = &cxx_options,
@@ -105,6 +108,7 @@ pub fn build(b: *std.Build) !void {
105108
.optimize = optimize,
106109
}),
107110
.use_llvm = true,
111+
.use_lld = true,
108112
});
109113
bimg.addCSourceFiles(.{
110114
.flags = &cxx_options,
@@ -133,6 +137,7 @@ pub fn build(b: *std.Build) !void {
133137
.optimize = optimize,
134138
}),
135139
.use_llvm = true,
140+
.use_lld = true,
136141
});
137142
b.installArtifact(bgfx);
138143
bxInclude(b, bgfx, target, optimize);
@@ -243,6 +248,7 @@ pub fn build(b: *std.Build) !void {
243248
.optimize = options.shaderc_optimize,
244249
}),
245250
.use_llvm = true,
251+
.use_lld = true,
246252
});
247253

248254
b.installArtifact(shaderc);
@@ -330,6 +336,7 @@ pub fn build(b: *std.Build) !void {
330336
.optimize = options.shaderc_optimize,
331337
}),
332338
.use_llvm = true,
339+
.use_lld = true,
333340
});
334341

335342
fcpp_lib.addIncludePath(b.path(fcpp_path));
@@ -365,6 +372,7 @@ pub fn build(b: *std.Build) !void {
365372
.optimize = options.shaderc_optimize,
366373
}),
367374
.use_llvm = true,
375+
.use_lld = true,
368376
});
369377
spirv_opt_lib.addIncludePath(b.path(spirv_opt_path));
370378
spirv_opt_lib.addIncludePath(b.path(spirv_opt_path ++ "include"));
@@ -400,6 +408,7 @@ pub fn build(b: *std.Build) !void {
400408
.optimize = options.shaderc_optimize,
401409
}),
402410
.use_llvm = true,
411+
.use_lld = true,
403412
});
404413
spirv_cross_lib.addIncludePath(b.path(spirv_cross_path ++ "include"));
405414
spirv_cross_lib.addCSourceFiles(.{
@@ -439,6 +448,7 @@ pub fn build(b: *std.Build) !void {
439448
.optimize = options.shaderc_optimize,
440449
}),
441450
.use_llvm = true,
451+
.use_lld = true,
442452
});
443453
glslang_lib.addIncludePath(b.path("libs/bgfx/3rdparty"));
444454
glslang_lib.addIncludePath(b.path(glslang_path));
@@ -512,6 +522,7 @@ pub fn build(b: *std.Build) !void {
512522
.optimize = options.shaderc_optimize,
513523
}),
514524
.use_llvm = true,
525+
.use_lld = true,
515526
});
516527
glsl_optimizer_lib.addIncludePath(b.path(glsl_optimizer_path ++ "include"));
517528
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)