Skip to content

Commit b58eb1d

Browse files
authored
Upgrade to zig 0.15.1 and fix imgui backend. (#17)
1 parent d5e2d46 commit b58eb1d

File tree

18 files changed

+270
-215
lines changed

18 files changed

+270
-215
lines changed

.github/workflows/test.yaml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ on:
1616
- "tools/**"
1717
- "build.zig"
1818
- "build.zig.zon"
19-
- ".zigversion"
2019

2120
push:
2221
branches:
@@ -29,7 +28,6 @@ on:
2928
- "tools/**"
3029
- "build.zig"
3130
- "build.zig.zon"
32-
- ".zigversion"
3331

3432
concurrency:
3533
# Cancels pending runs when a PR gets updated.
@@ -47,16 +45,8 @@ jobs:
4745
with:
4846
submodules: true
4947

50-
- name: Read .zig-version
51-
id: zigversion
52-
uses: juliangruber/read-file-action@v1
53-
with:
54-
path: ./.zigversion
5548
- name: Install Zig
5649
uses: mlugg/setup-zig@v2
57-
with:
58-
version: ${{ steps.zigversion.outputs.content }}
59-
6050
- name: Lint
6151
run: zig fmt --check .
6252

@@ -83,15 +73,8 @@ jobs:
8373
# - if: matrix.os == 'linux-large'
8474
# run: sudo apt update && sudo apt install libx11-6
8575

86-
- name: Read .zig-version
87-
id: zigversion
88-
uses: juliangruber/read-file-action@v1
89-
with:
90-
path: ./.zigversion
9176
- name: Install Zig
9277
uses: mlugg/setup-zig@v2
93-
with:
94-
version: ${{ steps.zigversion.outputs.content }}
9578

9679
- name: Build examples
9780
shell: bash

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Zig binding is licensed by [WTFPL](LICENSE)
3737

3838
## Zig version
3939

40-
Minimal is `0.14.0`. But you know try your version and believe.
40+
Minimal is `0.15.1`. But you know try your version and believe.
4141

4242
## Bgfx version
4343

build.zig

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,33 @@ pub fn build(b: *std.Build) !void {
5656
//
5757
const combine_bin_h = b.addExecutable(.{
5858
.name = "combine_bin_h",
59-
.optimize = optimize,
60-
.target = target,
61-
.root_source_file = b.path("tools/combine_bin_h.zig"),
59+
.root_module = b.createModule(.{
60+
.root_source_file = b.path("tools/combine_bin_h.zig"),
61+
.target = target,
62+
.optimize = optimize,
63+
}),
6264
});
6365
const combine_bin_zig = b.addExecutable(.{
6466
.name = "combine_bin_zig",
65-
.optimize = optimize,
66-
.target = target,
67-
.root_source_file = b.path("tools/combine_bin_zig.zig"),
67+
.root_module = b.createModule(.{
68+
.root_source_file = b.path("tools/combine_bin_zig.zig"),
69+
.target = target,
70+
.optimize = optimize,
71+
}),
6872
});
6973

7074
b.installArtifact(combine_bin_zig);
7175

7276
//
7377
// Bx
7478
//
75-
const bx = b.addStaticLibrary(.{
79+
const bx = b.addLibrary(.{
80+
.linkage = .static,
7681
.name = "bx",
77-
.target = target,
78-
.optimize = optimize,
82+
.root_module = b.createModule(.{
83+
.target = target,
84+
.optimize = optimize,
85+
}),
7986
});
8087
bx.addCSourceFiles(.{
8188
.flags = &cxx_options,
@@ -90,10 +97,13 @@ pub fn build(b: *std.Build) !void {
9097
//
9198
// Bimg
9299
//
93-
const bimg = b.addStaticLibrary(.{
100+
const bimg = b.addLibrary(.{
101+
.linkage = .static,
94102
.name = "bimg",
95-
.target = target,
96-
.optimize = optimize,
103+
.root_module = b.createModule(.{
104+
.target = target,
105+
.optimize = optimize,
106+
}),
97107
});
98108
bimg.addCSourceFiles(.{
99109
.flags = &cxx_options,
@@ -114,10 +124,13 @@ pub fn build(b: *std.Build) !void {
114124
// Bgfx
115125
//
116126
const bgfx_path = "libs/bgfx/";
117-
const bgfx = b.addStaticLibrary(.{
127+
const bgfx = b.addLibrary(.{
128+
.linkage = .static,
118129
.name = "bgfx",
119-
.target = target,
120-
.optimize = optimize,
130+
.root_module = b.createModule(.{
131+
.target = target,
132+
.optimize = optimize,
133+
}),
121134
});
122135
b.installArtifact(bgfx);
123136
bxInclude(b, bgfx, target, optimize);
@@ -197,17 +210,16 @@ pub fn build(b: *std.Build) !void {
197210
// Shaderc
198211
// Base steal from https://github.com/Interrupt/zig-bgfx-example/blob/main/build_shader_compiler.zig
199212
//
200-
var shaderc_variant = std.ArrayList(*std.Build.Step.Compile).init(b.allocator);
201-
defer shaderc_variant.deinit();
202-
203213
if (options.with_shaderc) {
204214
//
205215
// Shaderc executable
206216
//
207217
const shaderc = b.addExecutable(.{
208218
.name = "shaderc",
209-
.target = target,
210-
.optimize = optimize,
219+
.root_module = b.createModule(.{
220+
.target = target,
221+
.optimize = optimize,
222+
}),
211223
});
212224

213225
b.installArtifact(shaderc);
@@ -340,10 +352,13 @@ pub fn build(b: *std.Build) !void {
340352
};
341353

342354
const fcpp_path = "libs/bgfx/3rdparty/fcpp/";
343-
const fcpp_lib = b.addStaticLibrary(.{
355+
const fcpp_lib = b.addLibrary(.{
356+
.linkage = .static,
344357
.name = "fcpp",
345-
.target = target,
346-
.optimize = optimize,
358+
.root_module = b.createModule(.{
359+
.target = target,
360+
.optimize = optimize,
361+
}),
347362
});
348363

349364
fcpp_lib.addIncludePath(b.path(fcpp_path));
@@ -372,10 +387,12 @@ pub fn build(b: *std.Build) !void {
372387
"-fno-sanitize=undefined",
373388
};
374389

375-
const spirv_opt_lib = b.addStaticLibrary(.{
390+
const spirv_opt_lib = b.addLibrary(.{
376391
.name = "spirv-opt",
377-
.target = target,
378-
.optimize = optimize,
392+
.root_module = b.createModule(.{
393+
.target = target,
394+
.optimize = optimize,
395+
}),
379396
});
380397
spirv_opt_lib.addIncludePath(b.path(spirv_opt_path));
381398
spirv_opt_lib.addIncludePath(b.path(spirv_opt_path ++ "include"));
@@ -404,10 +421,12 @@ pub fn build(b: *std.Build) !void {
404421
};
405422

406423
const spirv_cross_path = "libs/bgfx/3rdparty/spirv-cross/";
407-
const spirv_cross_lib = b.addStaticLibrary(.{
424+
const spirv_cross_lib = b.addLibrary(.{
408425
.name = "spirv-cross",
409-
.target = target,
410-
.optimize = optimize,
426+
.root_module = b.createModule(.{
427+
.target = target,
428+
.optimize = optimize,
429+
}),
411430
});
412431
spirv_cross_lib.addIncludePath(b.path(spirv_cross_path ++ "include"));
413432
spirv_cross_lib.addCSourceFiles(.{
@@ -440,7 +459,13 @@ pub fn build(b: *std.Build) !void {
440459
"-fno-sanitize=undefined",
441460
};
442461

443-
const glslang_lib = b.addStaticLibrary(.{ .name = "glslang", .target = target, .optimize = optimize });
462+
const glslang_lib = b.addLibrary(.{
463+
.name = "glslang",
464+
.root_module = b.createModule(.{
465+
.target = target,
466+
.optimize = optimize,
467+
}),
468+
});
444469
glslang_lib.addIncludePath(b.path("libs/bgfx/3rdparty"));
445470
glslang_lib.addIncludePath(b.path(glslang_path));
446471
glslang_lib.addIncludePath(b.path(glslang_path ++ "include"));
@@ -504,10 +529,12 @@ pub fn build(b: *std.Build) !void {
504529
"-fno-sanitize=undefined",
505530
};
506531

507-
const glsl_optimizer_lib = b.addStaticLibrary(.{
532+
const glsl_optimizer_lib = b.addLibrary(.{
508533
.name = "glsl-optimizer",
509-
.target = target,
510-
.optimize = optimize,
534+
.root_module = b.createModule(.{
535+
.target = target,
536+
.optimize = optimize,
537+
}),
511538
});
512539
glsl_optimizer_lib.addIncludePath(b.path(glsl_optimizer_path ++ "include"));
513540
glsl_optimizer_lib.addIncludePath(b.path(glsl_optimizer_path ++ "src"));

build.zig.zon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
.{
22
.name = .zbgfx,
33
.fingerprint = 0xc48ed871c4086e4a,
4-
.version = "0.4.0",
4+
.version = "0.5.0",
5+
.minimum_zig_version = "0.15.1",
56
.paths = .{
67
"includes",
78
"libs",

examples/00-minimal/build_sample.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const zbgfx = @import("zbgfx");
44

55
pub fn build(
66
b: *std.Build,
7-
optimize: std.builtin.Mode,
7+
optimize: std.builtin.OptimizeMode,
88
target: std.Build.ResolvedTarget,
99
) !void {
1010
//
@@ -81,8 +81,10 @@ pub fn build(
8181

8282
const exe = b.addExecutable(.{
8383
.name = "00-minimal",
84-
.root_source_file = b.path("00-minimal/src/main.zig"),
85-
.target = target,
84+
.root_module = b.createModule(.{
85+
.root_source_file = b.path("00-minimal/src/main.zig"),
86+
.target = target,
87+
}),
8688
});
8789
b.installArtifact(exe);
8890
exe.linkLibrary(zbgfx_dep.artifact("bgfx"));

examples/01-zgui/build_sample.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const zbgfx = @import("zbgfx");
44

55
pub fn build(
66
b: *std.Build,
7-
optimize: std.builtin.Mode,
7+
optimize: std.builtin.OptimizeMode,
88
target: std.Build.ResolvedTarget,
99
) !void {
1010

@@ -31,6 +31,7 @@ pub fn build(
3131
.target = target,
3232
.optimize = optimize,
3333
.backend = .glfw,
34+
.disable_obsolete = false, // TODO: FIXME Assertion failed: (sz_io == sizeof(ImGuiIO) && "Mismatched struct layout!"), function DebugCheckVersionAndDataLayout, file imgui.cpp, line 11150.
3435
},
3536
);
3637

@@ -46,8 +47,10 @@ pub fn build(
4647

4748
const exe = b.addExecutable(.{
4849
.name = "01-zgui",
49-
.root_source_file = b.path("01-zgui/src/main.zig"),
50-
.target = target,
50+
.root_module = b.createModule(.{
51+
.root_source_file = b.path("01-zgui/src/main.zig"),
52+
.target = target,
53+
}),
5154
});
5255
b.installArtifact(exe);
5356

examples/01-zgui/src/backend_glfw_bgfx.zig

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,9 @@ pub fn deinit() void {
1818
zgui.backend.deinit();
1919
}
2020

21-
pub fn newFrame(fb_width: u32, fb_height: u32) void {
22-
const w = fb_width;
23-
const h = fb_height;
24-
21+
pub fn newFrame(viewid: zbgfx.bgfx.ViewId) void {
2522
zgui.backend.newFrame();
26-
27-
zgui.io.setDisplaySize(@floatFromInt(w), @floatFromInt(h));
28-
zgui.io.setDisplayFramebufferScale(1.0, 1.0);
29-
30-
backend_bgfx.newFrame(255);
23+
backend_bgfx.newFrame(viewid);
3124
}
3225

3326
pub fn draw() void {

examples/01-zgui/src/main.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ pub fn main() anyerror!u8 {
115115
const gpa_allocator = gpa.allocator();
116116
defer _ = gpa.deinit();
117117

118+
// Based on: https://github.com/ocornut/imgui/blob/27a9374ef3fc6572f8dd1fa9ddf72e1802fceb8b/backends/imgui_impl_glfw.cpp#L914
118119
const scale_factor = scale_factor: {
120+
if (builtin.os.tag.isDarwin()) break :scale_factor 1;
119121
const scale = window.getContentScale();
120122
break :scale_factor @max(scale[0], scale[1]);
121123
};
@@ -126,9 +128,9 @@ pub fn main() anyerror!u8 {
126128
// Load main font
127129
var main_cfg = zgui.FontConfig.init();
128130
main_cfg.font_data_owned_by_atlas = false;
129-
_ = zgui.io.addFontFromMemoryWithConfig(MAIN_FONT, std.math.floor(16 * scale_factor), main_cfg, null);
130-
131+
_ = zgui.io.addFontFromMemoryWithConfig(MAIN_FONT, 16, main_cfg, null);
131132
zgui.getStyle().scaleAllSizes(scale_factor);
133+
zgui.getStyle().font_scale_dpi = scale_factor;
132134

133135
backend_glfw_bgfx.init(window);
134136
defer backend_glfw_bgfx.deinit();
@@ -193,7 +195,7 @@ pub fn main() anyerror!u8 {
193195
bgfx.dbgTextClear(0, false);
194196

195197
// Do some zgui stuff
196-
backend_glfw_bgfx.newFrame(@intCast(size[0]), @intCast(size[1]));
198+
backend_glfw_bgfx.newFrame(255);
197199
zgui.showDemoWindow(null);
198200
backend_glfw_bgfx.draw();
199201

examples/02-runtime-shaderc/build_sample.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const zbgfx = @import("zbgfx");
44

55
pub fn build(
66
b: *std.Build,
7-
optimize: std.builtin.Mode,
7+
optimize: std.builtin.OptimizeMode,
88
target: std.Build.ResolvedTarget,
99
) !void {
1010
//
@@ -40,8 +40,10 @@ pub fn build(
4040

4141
const exe = b.addExecutable(.{
4242
.name = "02-runtime-shaderc",
43-
.root_source_file = b.path("02-runtime-shaderc/src/main.zig"),
44-
.target = target,
43+
.root_module = b.createModule(.{
44+
.root_source_file = b.path("02-runtime-shaderc/src/main.zig"),
45+
.target = target,
46+
}),
4547
});
4648
b.installArtifact(exe);
4749
exe.linkLibrary(zbgfx_dep.artifact("bgfx"));

examples/02-runtime-shaderc/src/main.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ pub fn main() anyerror!u8 {
366366
return 0;
367367
}
368368

369-
fn readFileFromShaderDirs(allocator: std.mem.Allocator, filename: []const u8) ![:0]u8 {
369+
fn readFileFromShaderDirs(allocator: std.mem.Allocator, filename: []const u8) ![]u8 {
370370
const exe_dir = try std.fs.selfExeDirPathAlloc(allocator);
371371
defer allocator.free(exe_dir);
372372

@@ -376,8 +376,9 @@ fn readFileFromShaderDirs(allocator: std.mem.Allocator, filename: []const u8) ![
376376
const f = try std.fs.cwd().openFile(path, .{});
377377
defer f.close();
378378
const max_size = (try f.getEndPos()) + 1;
379-
var data = std.ArrayList(u8).init(allocator);
380-
try f.reader().readAllArrayList(&data, max_size);
381379

382-
return try data.toOwnedSliceSentinel(0);
380+
var buffer: [1024]u8 = undefined;
381+
var reader = f.reader(&buffer);
382+
var r = &reader.interface;
383+
return try r.readAlloc(allocator, max_size - 1);
383384
}

0 commit comments

Comments
 (0)