diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e843850..cfd59bf 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -16,7 +16,6 @@ on: - "tools/**" - "build.zig" - "build.zig.zon" - - ".zigversion" push: branches: @@ -29,7 +28,6 @@ on: - "tools/**" - "build.zig" - "build.zig.zon" - - ".zigversion" concurrency: # Cancels pending runs when a PR gets updated. @@ -47,16 +45,8 @@ jobs: with: submodules: true - - name: Read .zig-version - id: zigversion - uses: juliangruber/read-file-action@v1 - with: - path: ./.zigversion - name: Install Zig uses: mlugg/setup-zig@v2 - with: - version: ${{ steps.zigversion.outputs.content }} - - name: Lint run: zig fmt --check . @@ -83,15 +73,8 @@ jobs: # - if: matrix.os == 'linux-large' # run: sudo apt update && sudo apt install libx11-6 - - name: Read .zig-version - id: zigversion - uses: juliangruber/read-file-action@v1 - with: - path: ./.zigversion - name: Install Zig uses: mlugg/setup-zig@v2 - with: - version: ${{ steps.zigversion.outputs.content }} - name: Build examples shell: bash diff --git a/README.md b/README.md index 7782c65..3fbc3ea 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Zig binding is licensed by [WTFPL](LICENSE) ## Zig version -Minimal is `0.14.0`. But you know try your version and believe. +Minimal is `0.15.1`. But you know try your version and believe. ## Bgfx version diff --git a/build.zig b/build.zig index c2e56f2..e073ce5 100644 --- a/build.zig +++ b/build.zig @@ -56,15 +56,19 @@ pub fn build(b: *std.Build) !void { // const combine_bin_h = b.addExecutable(.{ .name = "combine_bin_h", - .optimize = optimize, - .target = target, - .root_source_file = b.path("tools/combine_bin_h.zig"), + .root_module = b.createModule(.{ + .root_source_file = b.path("tools/combine_bin_h.zig"), + .target = target, + .optimize = optimize, + }), }); const combine_bin_zig = b.addExecutable(.{ .name = "combine_bin_zig", - .optimize = optimize, - .target = target, - .root_source_file = b.path("tools/combine_bin_zig.zig"), + .root_module = b.createModule(.{ + .root_source_file = b.path("tools/combine_bin_zig.zig"), + .target = target, + .optimize = optimize, + }), }); b.installArtifact(combine_bin_zig); @@ -72,10 +76,13 @@ pub fn build(b: *std.Build) !void { // // Bx // - const bx = b.addStaticLibrary(.{ + const bx = b.addLibrary(.{ + .linkage = .static, .name = "bx", - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + }), }); bx.addCSourceFiles(.{ .flags = &cxx_options, @@ -90,10 +97,13 @@ pub fn build(b: *std.Build) !void { // // Bimg // - const bimg = b.addStaticLibrary(.{ + const bimg = b.addLibrary(.{ + .linkage = .static, .name = "bimg", - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + }), }); bimg.addCSourceFiles(.{ .flags = &cxx_options, @@ -114,10 +124,13 @@ pub fn build(b: *std.Build) !void { // Bgfx // const bgfx_path = "libs/bgfx/"; - const bgfx = b.addStaticLibrary(.{ + const bgfx = b.addLibrary(.{ + .linkage = .static, .name = "bgfx", - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + }), }); b.installArtifact(bgfx); bxInclude(b, bgfx, target, optimize); @@ -197,17 +210,16 @@ pub fn build(b: *std.Build) !void { // Shaderc // Base steal from https://github.com/Interrupt/zig-bgfx-example/blob/main/build_shader_compiler.zig // - var shaderc_variant = std.ArrayList(*std.Build.Step.Compile).init(b.allocator); - defer shaderc_variant.deinit(); - if (options.with_shaderc) { // // Shaderc executable // const shaderc = b.addExecutable(.{ .name = "shaderc", - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + }), }); b.installArtifact(shaderc); @@ -340,10 +352,13 @@ pub fn build(b: *std.Build) !void { }; const fcpp_path = "libs/bgfx/3rdparty/fcpp/"; - const fcpp_lib = b.addStaticLibrary(.{ + const fcpp_lib = b.addLibrary(.{ + .linkage = .static, .name = "fcpp", - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + }), }); fcpp_lib.addIncludePath(b.path(fcpp_path)); @@ -372,10 +387,12 @@ pub fn build(b: *std.Build) !void { "-fno-sanitize=undefined", }; - const spirv_opt_lib = b.addStaticLibrary(.{ + const spirv_opt_lib = b.addLibrary(.{ .name = "spirv-opt", - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + }), }); spirv_opt_lib.addIncludePath(b.path(spirv_opt_path)); spirv_opt_lib.addIncludePath(b.path(spirv_opt_path ++ "include")); @@ -404,10 +421,12 @@ pub fn build(b: *std.Build) !void { }; const spirv_cross_path = "libs/bgfx/3rdparty/spirv-cross/"; - const spirv_cross_lib = b.addStaticLibrary(.{ + const spirv_cross_lib = b.addLibrary(.{ .name = "spirv-cross", - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + }), }); spirv_cross_lib.addIncludePath(b.path(spirv_cross_path ++ "include")); spirv_cross_lib.addCSourceFiles(.{ @@ -440,7 +459,13 @@ pub fn build(b: *std.Build) !void { "-fno-sanitize=undefined", }; - const glslang_lib = b.addStaticLibrary(.{ .name = "glslang", .target = target, .optimize = optimize }); + const glslang_lib = b.addLibrary(.{ + .name = "glslang", + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + }), + }); glslang_lib.addIncludePath(b.path("libs/bgfx/3rdparty")); glslang_lib.addIncludePath(b.path(glslang_path)); glslang_lib.addIncludePath(b.path(glslang_path ++ "include")); @@ -504,10 +529,12 @@ pub fn build(b: *std.Build) !void { "-fno-sanitize=undefined", }; - const glsl_optimizer_lib = b.addStaticLibrary(.{ + const glsl_optimizer_lib = b.addLibrary(.{ .name = "glsl-optimizer", - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + }), }); glsl_optimizer_lib.addIncludePath(b.path(glsl_optimizer_path ++ "include")); glsl_optimizer_lib.addIncludePath(b.path(glsl_optimizer_path ++ "src")); diff --git a/build.zig.zon b/build.zig.zon index a9f1fbb..3f24b5a 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,7 +1,8 @@ .{ .name = .zbgfx, .fingerprint = 0xc48ed871c4086e4a, - .version = "0.4.0", + .version = "0.5.0", + .minimum_zig_version = "0.15.1", .paths = .{ "includes", "libs", diff --git a/examples/00-minimal/build_sample.zig b/examples/00-minimal/build_sample.zig index e45ce7b..1ef2c65 100644 --- a/examples/00-minimal/build_sample.zig +++ b/examples/00-minimal/build_sample.zig @@ -4,7 +4,7 @@ const zbgfx = @import("zbgfx"); pub fn build( b: *std.Build, - optimize: std.builtin.Mode, + optimize: std.builtin.OptimizeMode, target: std.Build.ResolvedTarget, ) !void { // @@ -81,8 +81,10 @@ pub fn build( const exe = b.addExecutable(.{ .name = "00-minimal", - .root_source_file = b.path("00-minimal/src/main.zig"), - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("00-minimal/src/main.zig"), + .target = target, + }), }); b.installArtifact(exe); exe.linkLibrary(zbgfx_dep.artifact("bgfx")); diff --git a/examples/01-zgui/build_sample.zig b/examples/01-zgui/build_sample.zig index 57335cf..b9b54c2 100644 --- a/examples/01-zgui/build_sample.zig +++ b/examples/01-zgui/build_sample.zig @@ -4,7 +4,7 @@ const zbgfx = @import("zbgfx"); pub fn build( b: *std.Build, - optimize: std.builtin.Mode, + optimize: std.builtin.OptimizeMode, target: std.Build.ResolvedTarget, ) !void { @@ -31,6 +31,7 @@ pub fn build( .target = target, .optimize = optimize, .backend = .glfw, + .disable_obsolete = false, // TODO: FIXME Assertion failed: (sz_io == sizeof(ImGuiIO) && "Mismatched struct layout!"), function DebugCheckVersionAndDataLayout, file imgui.cpp, line 11150. }, ); @@ -46,8 +47,10 @@ pub fn build( const exe = b.addExecutable(.{ .name = "01-zgui", - .root_source_file = b.path("01-zgui/src/main.zig"), - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("01-zgui/src/main.zig"), + .target = target, + }), }); b.installArtifact(exe); diff --git a/examples/01-zgui/src/backend_glfw_bgfx.zig b/examples/01-zgui/src/backend_glfw_bgfx.zig index da00421..60107e5 100644 --- a/examples/01-zgui/src/backend_glfw_bgfx.zig +++ b/examples/01-zgui/src/backend_glfw_bgfx.zig @@ -18,16 +18,9 @@ pub fn deinit() void { zgui.backend.deinit(); } -pub fn newFrame(fb_width: u32, fb_height: u32) void { - const w = fb_width; - const h = fb_height; - +pub fn newFrame(viewid: zbgfx.bgfx.ViewId) void { zgui.backend.newFrame(); - - zgui.io.setDisplaySize(@floatFromInt(w), @floatFromInt(h)); - zgui.io.setDisplayFramebufferScale(1.0, 1.0); - - backend_bgfx.newFrame(255); + backend_bgfx.newFrame(viewid); } pub fn draw() void { diff --git a/examples/01-zgui/src/main.zig b/examples/01-zgui/src/main.zig index e3e5ca5..63b130e 100644 --- a/examples/01-zgui/src/main.zig +++ b/examples/01-zgui/src/main.zig @@ -115,7 +115,9 @@ pub fn main() anyerror!u8 { const gpa_allocator = gpa.allocator(); defer _ = gpa.deinit(); + // Based on: https://github.com/ocornut/imgui/blob/27a9374ef3fc6572f8dd1fa9ddf72e1802fceb8b/backends/imgui_impl_glfw.cpp#L914 const scale_factor = scale_factor: { + if (builtin.os.tag.isDarwin()) break :scale_factor 1; const scale = window.getContentScale(); break :scale_factor @max(scale[0], scale[1]); }; @@ -126,9 +128,9 @@ pub fn main() anyerror!u8 { // Load main font var main_cfg = zgui.FontConfig.init(); main_cfg.font_data_owned_by_atlas = false; - _ = zgui.io.addFontFromMemoryWithConfig(MAIN_FONT, std.math.floor(16 * scale_factor), main_cfg, null); - + _ = zgui.io.addFontFromMemoryWithConfig(MAIN_FONT, 16, main_cfg, null); zgui.getStyle().scaleAllSizes(scale_factor); + zgui.getStyle().font_scale_dpi = scale_factor; backend_glfw_bgfx.init(window); defer backend_glfw_bgfx.deinit(); @@ -193,7 +195,7 @@ pub fn main() anyerror!u8 { bgfx.dbgTextClear(0, false); // Do some zgui stuff - backend_glfw_bgfx.newFrame(@intCast(size[0]), @intCast(size[1])); + backend_glfw_bgfx.newFrame(255); zgui.showDemoWindow(null); backend_glfw_bgfx.draw(); diff --git a/examples/02-runtime-shaderc/build_sample.zig b/examples/02-runtime-shaderc/build_sample.zig index 20c553f..02e48ee 100644 --- a/examples/02-runtime-shaderc/build_sample.zig +++ b/examples/02-runtime-shaderc/build_sample.zig @@ -4,7 +4,7 @@ const zbgfx = @import("zbgfx"); pub fn build( b: *std.Build, - optimize: std.builtin.Mode, + optimize: std.builtin.OptimizeMode, target: std.Build.ResolvedTarget, ) !void { // @@ -40,8 +40,10 @@ pub fn build( const exe = b.addExecutable(.{ .name = "02-runtime-shaderc", - .root_source_file = b.path("02-runtime-shaderc/src/main.zig"), - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("02-runtime-shaderc/src/main.zig"), + .target = target, + }), }); b.installArtifact(exe); exe.linkLibrary(zbgfx_dep.artifact("bgfx")); diff --git a/examples/02-runtime-shaderc/src/main.zig b/examples/02-runtime-shaderc/src/main.zig index 76ea9f2..56fe081 100644 --- a/examples/02-runtime-shaderc/src/main.zig +++ b/examples/02-runtime-shaderc/src/main.zig @@ -366,7 +366,7 @@ pub fn main() anyerror!u8 { return 0; } -fn readFileFromShaderDirs(allocator: std.mem.Allocator, filename: []const u8) ![:0]u8 { +fn readFileFromShaderDirs(allocator: std.mem.Allocator, filename: []const u8) ![]u8 { const exe_dir = try std.fs.selfExeDirPathAlloc(allocator); defer allocator.free(exe_dir); @@ -376,8 +376,9 @@ fn readFileFromShaderDirs(allocator: std.mem.Allocator, filename: []const u8) ![ const f = try std.fs.cwd().openFile(path, .{}); defer f.close(); const max_size = (try f.getEndPos()) + 1; - var data = std.ArrayList(u8).init(allocator); - try f.reader().readAllArrayList(&data, max_size); - return try data.toOwnedSliceSentinel(0); + var buffer: [1024]u8 = undefined; + var reader = f.reader(&buffer); + var r = &reader.interface; + return try r.readAlloc(allocator, max_size - 1); } diff --git a/examples/03-debugdraw/build_sample.zig b/examples/03-debugdraw/build_sample.zig index 5d9df43..7ef49e5 100644 --- a/examples/03-debugdraw/build_sample.zig +++ b/examples/03-debugdraw/build_sample.zig @@ -4,7 +4,7 @@ const zbgfx = @import("zbgfx"); pub fn build( b: *std.Build, - optimize: std.builtin.Mode, + optimize: std.builtin.OptimizeMode, target: std.Build.ResolvedTarget, ) !void { // @@ -40,8 +40,10 @@ pub fn build( const exe = b.addExecutable(.{ .name = "03-debugdraw", - .root_source_file = b.path("03-debugdraw/src/main.zig"), - .target = target, + .root_module = b.createModule(.{ + .root_source_file = b.path("03-debugdraw/src/main.zig"), + .target = target, + }), }); b.installArtifact(exe); exe.linkLibrary(zbgfx_dep.artifact("bgfx")); diff --git a/examples/build.zig.zon b/examples/build.zig.zon index 5e4aaef..4679b77 100644 --- a/examples/build.zig.zon +++ b/examples/build.zig.zon @@ -2,6 +2,7 @@ .name = .examples, .fingerprint = 0x7bd0ad45636a9f2b, .version = "0.1.0", + .minimum_zig_version = "0.15.1", .paths = .{ "build.zig", "build.zig.zon", @@ -11,18 +12,18 @@ .zbgfx = .{ .path = "../" }, .zglfw = .{ - .url = "https://github.com/zig-gamedev/zglfw/archive/dcb1dae7d4a90f88b70dbae73300ab9b19fb250c.tar.gz", - .hash = "zglfw-0.10.0-dev-zgVDNJmZIQCIiO_94Sp5jqrPtz4Eumk3bDcxS7dSZfIj", + .url = "https://github.com/zig-gamedev/zglfw/archive/3a3f0d2c040902c717402d58e96ae1553bee550d.tar.gz", + .hash = "zglfw-0.10.0-dev-zgVDNHOuIQC9B7kIg7w6a0K07ZKD_QKYf-OJ6SqiLwg1", }, .zgui = .{ - .url = "https://github.com/zig-gamedev/zgui/archive/020b27ef9356abbecceb9e32103b79d4798a7b4a.tar.gz", - .hash = "zgui-0.6.0-dev--L6sZMGHaAB-y1HhtLJ3CXhfoMOOi4RAJDdnr_IGYKoh", + .url = "https://github.com/zig-gamedev/zgui/archive/1082a309eb423bde95dce0d57db7f8a0a5091b33.tar.gz", + .hash = "zgui-0.6.0-dev--L6sZAzobQDQSKMsQdqmeqqvgN3ck6AgpY5E0uWGqRhM", }, .zmath = .{ - .url = "https://github.com/zig-gamedev/zmath/archive/ccf7297ef6c01e21b2d51ad81b5b6ce929e86a00.tar.gz", - .hash = "zmath-0.11.0-dev-wjwivZY1AwDO7yxNmZ5HWoU03f9mFBet8LN9-oYc3i29", + .url = "https://github.com/zig-gamedev/zmath/archive/3a5955b2b72cd081563fbb084eff05bffd1e3fbb.tar.gz", + .hash = "zmath-0.11.0-dev-wjwivdMsAwD-xaLj76YHUq3t9JDH-X16xuMTmnDzqbu2", }, }, } diff --git a/libs/bgfx/examples/common/imgui/imgui.cpp b/libs/bgfx/examples/common/imgui/imgui.cpp index fe8fdfe..aacc317 100644 --- a/libs/bgfx/examples/common/imgui/imgui.cpp +++ b/libs/bgfx/examples/common/imgui/imgui.cpp @@ -13,7 +13,6 @@ #include #include -// #include #include "imgui.h" #include "../bgfx_utils.h" @@ -33,13 +32,12 @@ static const bgfx::EmbeddedShader s_embeddedShaders[] = { struct OcornutImguiContext { - void render(ImDrawData* _drawData) + void render(ImDrawData *_drawData) { // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) - int32_t dispWidth = int32_t(_drawData->DisplaySize.x * _drawData->FramebufferScale.x); + int32_t dispWidth = int32_t(_drawData->DisplaySize.x * _drawData->FramebufferScale.x); int32_t dispHeight = int32_t(_drawData->DisplaySize.y * _drawData->FramebufferScale.y); - if (dispWidth <= 0 - || dispHeight <= 0) + if (dispWidth <= 0 || dispHeight <= 0) { return; } @@ -47,7 +45,55 @@ struct OcornutImguiContext bgfx::setViewName(m_viewId, "ImGui"); bgfx::setViewMode(m_viewId, bgfx::ViewMode::Sequential); - const bgfx::Caps* caps = bgfx::getCaps(); + if (_drawData->Textures != NULL) + { + for (ImTextureData *tex : *_drawData->Textures) + { + if (tex->Status != ImTextureStatus_OK) + { + if (tex->Status == ImTextureStatus_WantCreate) + { + // Create and upload new texture to graphics system + IM_ASSERT(tex->TexID == 0 && tex->BackendUserData == nullptr); + IM_ASSERT(tex->Format == ImTextureFormat_RGBA32); + const void *pixels = tex->GetPixels(); + + const bgfx::TextureHandle new_tex = bgfx::createTexture2D(tex->Width, tex->Height, false, 1, bgfx::TextureFormat::BGRA8, 0 | BGFX_SAMPLER_U_BORDER | BGFX_SAMPLER_V_BORDER, NULL); + bgfx::updateTexture2D(new_tex, 0, 0, 0, 0, tex->Width, tex->Height, bgfx::copy(pixels, tex->GetSizeInBytes())); + + // Store identifiers + tex->SetTexID((ImTextureID)(intptr_t)new_tex.idx); + tex->SetStatus(ImTextureStatus_OK); + } + else if (tex->Status == ImTextureStatus_WantUpdates) + { + for (ImTextureRect &r : tex->Updates) + { + const int src_pitch = r.w * tex->BytesPerPixel; + const bgfx::Memory *buffer = bgfx::alloc(r.h * src_pitch); + uint8_t *out_p = buffer->data; + for (int y = 0; y < r.h; y++, out_p += src_pitch) + { + memcpy(out_p, tex->GetPixelsAt(r.x, r.y + y), src_pitch); + } + const bgfx::TextureHandle t = bgfx::TextureHandle{.idx = (uint16_t)tex->GetTexID()}; + bgfx::updateTexture2D(t, 0, 0, r.x, r.y, r.w, r.h, buffer); + } + tex->SetStatus(ImTextureStatus_OK); + } + else if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0) + { + const bgfx::TextureHandle t = bgfx::TextureHandle{.idx = (uint16_t)tex->GetTexID()}; + bgfx::destroy(t); + + tex->SetTexID(ImTextureID_Invalid); + tex->SetStatus(ImTextureStatus_Destroyed); + } + } + } + } + + const bgfx::Caps *caps = bgfx::getCaps(); { float ortho[16]; float x = _drawData->DisplayPos.x; @@ -57,10 +103,10 @@ struct OcornutImguiContext bx::mtxOrtho(ortho, x, x + width, y + height, y, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth); bgfx::setViewTransform(m_viewId, NULL, ortho); - bgfx::setViewRect(m_viewId, 0, 0, uint16_t(width), uint16_t(height) ); + bgfx::setViewRect(m_viewId, 0, 0, uint16_t(dispWidth), uint16_t(dispHeight)); } - const ImVec2 clipPos = _drawData->DisplayPos; // (0,0) unless using multi-viewports + const ImVec2 clipPos = _drawData->DisplayPos; // (0,0) unless using multi-viewports const ImVec2 clipScale = _drawData->FramebufferScale; // (1,1) unless using retina display which are often (2,2) // Render command lists @@ -69,11 +115,12 @@ struct OcornutImguiContext bgfx::TransientVertexBuffer tvb; bgfx::TransientIndexBuffer tib; - const ImDrawList* drawList = _drawData->CmdLists[ii]; + const ImDrawList *drawList = _drawData->CmdLists[ii]; + uint32_t numVertices = (uint32_t)drawList->VtxBuffer.size(); - uint32_t numIndices = (uint32_t)drawList->IdxBuffer.size(); + uint32_t numIndices = (uint32_t)drawList->IdxBuffer.size(); - if (!checkAvailTransientBuffers(numVertices, m_layout, numIndices) ) + if (!checkAvailTransientBuffers(numVertices, m_layout, numIndices)) { // not enough space in transient buffer just quit drawing the rest... break; @@ -82,15 +129,15 @@ struct OcornutImguiContext bgfx::allocTransientVertexBuffer(&tvb, numVertices, m_layout); bgfx::allocTransientIndexBuffer(&tib, numIndices, sizeof(ImDrawIdx) == 4); - ImDrawVert* verts = (ImDrawVert*)tvb.data; - bx::memCopy(verts, drawList->VtxBuffer.begin(), numVertices * sizeof(ImDrawVert) ); + ImDrawVert *verts = (ImDrawVert *)tvb.data; + bx::memCopy(verts, drawList->VtxBuffer.begin(), numVertices * sizeof(ImDrawVert)); - ImDrawIdx* indices = (ImDrawIdx*)tib.data; - bx::memCopy(indices, drawList->IdxBuffer.begin(), numIndices * sizeof(ImDrawIdx) ); + ImDrawIdx *indices = (ImDrawIdx *)tib.data; + bx::memCopy(indices, drawList->IdxBuffer.begin(), numIndices * sizeof(ImDrawIdx)); - bgfx::Encoder* encoder = bgfx::begin(); + bgfx::Encoder *encoder = bgfx::begin(); - for (const ImDrawCmd* cmd = drawList->CmdBuffer.begin(), *cmdEnd = drawList->CmdBuffer.end(); cmd != cmdEnd; ++cmd) + for (const ImDrawCmd *cmd = drawList->CmdBuffer.begin(), *cmdEnd = drawList->CmdBuffer.end(); cmd != cmdEnd; ++cmd) { if (cmd->UserCallback) { @@ -98,28 +145,33 @@ struct OcornutImguiContext } else if (0 != cmd->ElemCount) { - uint64_t state = 0 - | BGFX_STATE_WRITE_RGB - | BGFX_STATE_WRITE_A - | BGFX_STATE_MSAA - ; + uint64_t state = 0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_MSAA; - bgfx::TextureHandle th = m_texture; + bgfx::TextureHandle th = {.idx = BGFX_INVALID_HANDLE}; bgfx::ProgramHandle program = m_program; - if (NULL != cmd->TextureId) + if (NULL != cmd->GetTexID()) { - union { ImTextureID ptr; struct { bgfx::TextureHandle handle; uint8_t flags; uint8_t mip; } s; } texture = { cmd->TextureId }; + union + { + ImTextureID ptr; + struct + { + bgfx::TextureHandle handle; + uint8_t flags; + uint8_t mip; + } s; + } texture = {cmd->GetTexID()}; + texture.s.flags = IMGUI_FLAGS_ALPHA_BLEND; // TODO: Fix this state |= 0 != (IMGUI_FLAGS_ALPHA_BLEND & texture.s.flags) - ? BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA) - : BGFX_STATE_NONE - ; + ? BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA) + : BGFX_STATE_NONE; th = texture.s.handle; if (0 != texture.s.mip) { - const float lodEnabled[4] = { float(texture.s.mip), 1.0f, 0.0f, 0.0f }; + const float lodEnabled[4] = {float(texture.s.mip), 1.0f, 0.0f, 0.0f}; bgfx::setUniform(u_imageLodEnabled, lodEnabled); program = m_imageProgram; } @@ -136,17 +188,11 @@ struct OcornutImguiContext clipRect.z = (cmd->ClipRect.z - clipPos.x) * clipScale.x; clipRect.w = (cmd->ClipRect.w - clipPos.y) * clipScale.y; - if (clipRect.x < dispWidth - && clipRect.y < dispHeight - && clipRect.z >= 0.0f - && clipRect.w >= 0.0f) + if (clipRect.x < dispWidth && clipRect.y < dispHeight && clipRect.z >= 0.0f && clipRect.w >= 0.0f) { - const uint16_t xx = uint16_t(bx::max(clipRect.x, 0.0f) ); - const uint16_t yy = uint16_t(bx::max(clipRect.y, 0.0f) ); - encoder->setScissor(xx, yy - , uint16_t(bx::min(clipRect.z, 65535.0f)-xx) - , uint16_t(bx::min(clipRect.w, 65535.0f)-yy) - ); + const uint16_t xx = uint16_t(bx::max(clipRect.x, 0.0f)); + const uint16_t yy = uint16_t(bx::max(clipRect.y, 0.0f)); + encoder->setScissor(xx, yy, uint16_t(bx::min(clipRect.z, 65535.0f) - xx), uint16_t(bx::min(clipRect.w, 65535.0f) - yy)); encoder->setState(state); encoder->setTexture(0, s_tex, th); @@ -161,7 +207,7 @@ struct OcornutImguiContext } } - void create(float _fontSize, bx::AllocatorI* _allocator) + void create(float _fontSize, bx::AllocatorI *_allocator) { IMGUI_CHECKVERSION(); @@ -177,56 +223,39 @@ struct OcornutImguiContext ImGuiIO &io = ImGui::GetIO(); io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; + io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; bgfx::RendererType::Enum type = bgfx::getRendererType(); m_program = bgfx::createProgram( - bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_ocornut_imgui") - , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_ocornut_imgui") - , true - ); + bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_ocornut_imgui"), bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_ocornut_imgui"), true); u_imageLodEnabled = bgfx::createUniform("u_imageLodEnabled", bgfx::UniformType::Vec4); m_imageProgram = bgfx::createProgram( - bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_imgui_image") - , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_imgui_image") - , true - ); + bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_imgui_image"), bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_imgui_image"), true); m_layout .begin() - .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float) + .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float) .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float) - .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) + .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) .end(); s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Sampler); - - uint8_t* data; - int32_t width; - int32_t height; - - io.Fonts->GetTexDataAsRGBA32(&data, &width, &height); - - m_texture = bgfx::createTexture2D( - (uint16_t)width - , (uint16_t)height - , false - , 1 - , bgfx::TextureFormat::BGRA8 - , 0 - , bgfx::copy(data, width*height*4) - ); } void destroy() { bgfx::destroy(s_tex); - bgfx::destroy(m_texture); - bgfx::destroy(u_imageLodEnabled); bgfx::destroy(m_imageProgram); bgfx::destroy(m_program); + for (ImTextureData *tex : ImGui::GetPlatformIO().Textures) + { + const bgfx::TextureHandle t = bgfx::TextureHandle{.idx = (uint16_t)tex->GetTexID()}; + bgfx::destroy(t); + } + m_allocator = NULL; } diff --git a/src/build_step.zig b/src/build_step.zig index 1bb4aae..a18929b 100644 --- a/src/build_step.zig +++ b/src/build_step.zig @@ -129,8 +129,8 @@ pub fn compileBasicBinZig( input: BasicCompileInput, options: BasicCompileOptions, ) !*std.Build.Module { - var shaders = std.ArrayList(std.Build.LazyPath).init(b.allocator); - defer shaders.deinit(); + var shaders = std.ArrayList(std.Build.LazyPath){}; + defer shaders.deinit(b.allocator); try compileBasic( b, @@ -141,8 +141,8 @@ pub fn compileBasicBinZig( options, ); - var variants = std.ArrayList([]const u8).init(b.allocator); - defer variants.deinit(); + var variants = std.ArrayList([]const u8){}; + defer variants.deinit(b.allocator); var shaders_module = b.createModule(.{ .imports = &.{ @@ -154,7 +154,7 @@ pub fn compileBasicBinZig( if (target.result.os.tag != .windows and profile == .s_5_0) continue; const variant_name = profileToPostfix(profile); shaders_module.addAnonymousImport(variant_name, .{ .root_source_file = shaders.items[idx] }); - try variants.append(variant_name); + try variants.append(b.allocator, variant_name); } const combine_step = combineBinZigStep( @@ -180,8 +180,8 @@ pub fn compileBasicBinH( input: BasicCompileInput, options: BasicCompileOptions, ) !*std.Build.Step { - var shaders = std.ArrayList(std.Build.LazyPath).init(b.allocator); - defer shaders.deinit(); + var shaders = std.ArrayList(std.Build.LazyPath){}; + defer shaders.deinit(b.allocator); try compileBasic( b, @@ -251,6 +251,6 @@ pub fn compileBasic( .includes = options.includes, }, ); - try out_shaders.append(shader_build.output); + try out_shaders.append(b.allocator, shader_build.output); } } diff --git a/src/callbacks.zig b/src/callbacks.zig index cca57cb..beda40d 100644 --- a/src/callbacks.zig +++ b/src/callbacks.zig @@ -13,11 +13,11 @@ const Self = @This(); pub const CAllocInterfaceT = extern struct { vtable: *const CAllocVtblT }; pub const CAllocVtblT = extern struct { - realloc: *const fn (_this: *CAllocInterfaceT, _ptr: [*c]u8, _size: usize, _align: usize, _file: [*:0]const u8, _line: u32) callconv(.C) ?*anyopaque, + realloc: *const fn (_this: *CAllocInterfaceT, _ptr: [*c]u8, _size: usize, _align: usize, _file: [*:0]const u8, _line: u32) callconv(.c) ?*anyopaque, }; pub const ZigAllocatorVtbl = extern struct { - fn realloc(_this: *CAllocInterfaceT, _ptr: [*c]u8, _size: usize, _align: usize, _file: [*:0]const u8, _line: u32) callconv(.C) ?*anyopaque { + fn realloc(_this: *CAllocInterfaceT, _ptr: [*c]u8, _size: usize, _align: usize, _file: [*:0]const u8, _line: u32) callconv(.c) ?*anyopaque { var self: *ZigAllocator = @ptrCast(_this); _ = _file; // autofix _ = _line; // autofix @@ -54,27 +54,27 @@ pub const VaList = extern struct { _: *anyopaque }; // dirty&tricky is your best pub const CCallbackInterfaceT = extern struct { vtable: *const CCallbackVtblT }; pub const CCallbackVtblT = extern struct { - fatal: *const fn (_this: *CCallbackInterfaceT, _filePath: [*:0]const u8, _line: u16, _code: bgfx.Fatal, c_str: [*:0]const u8) callconv(.C) void, - trace_vargs: *const fn (_this: *CCallbackInterfaceT, _filePath: [*:0]const u8, _line: u16, _format: [*:0]const u8, va_list: VaList) callconv(.C) void, - profiler_begin: *const fn (_this: *CCallbackInterfaceT, _name: [*:0]const u8, _abgr: u32, _filePath: [*:0]const u8, _line: u16) callconv(.C) void, - profiler_begin_literal: *const fn (_this: *CCallbackInterfaceT, _name: [*:0]const u8, _abgr: u32, _filePath: [*:0]const u8, _line: u16) callconv(.C) void, - profiler_end: *const fn (_this: *CCallbackInterfaceT) callconv(.C) void, - cache_read_size: *const fn (_this: *CCallbackInterfaceT, _id: u64) callconv(.C) u32, - cache_read: *const fn (_this: *CCallbackInterfaceT, _id: u64, _data: [*c]u8, _size: u32) callconv(.C) bool, - cache_write: *const fn (_this: *CCallbackInterfaceT, _id: u64, _data: [*c]u8, _size: u32) callconv(.C) void, - screen_shot: *const fn (_this: *CCallbackInterfaceT, _filePath: [*:0]const u8, _width: u32, _height: u32, _pitch: u32, _data: [*c]u8, _size: u32, _yflip: bool) callconv(.C) void, - capture_begin: *const fn (_this: *CCallbackInterfaceT, _width: u32, _height: u32, _pitch: u32, _format: bgfx.TextureFormat, _yflip: bool) callconv(.C) void, - capture_end: *const fn (_this: *CCallbackInterfaceT) callconv(.C) void, - capture_frame: *const fn (_this: *CCallbackInterfaceT, _data: [*c]u8, _size: u32) callconv(.C) void, + fatal: *const fn (_this: *CCallbackInterfaceT, _filePath: [*:0]const u8, _line: u16, _code: bgfx.Fatal, c_str: [*:0]const u8) callconv(.c) void, + trace_vargs: *const fn (_this: *CCallbackInterfaceT, _filePath: [*:0]const u8, _line: u16, _format: [*:0]const u8, va_list: VaList) callconv(.c) void, + profiler_begin: *const fn (_this: *CCallbackInterfaceT, _name: [*:0]const u8, _abgr: u32, _filePath: [*:0]const u8, _line: u16) callconv(.c) void, + profiler_begin_literal: *const fn (_this: *CCallbackInterfaceT, _name: [*:0]const u8, _abgr: u32, _filePath: [*:0]const u8, _line: u16) callconv(.c) void, + profiler_end: *const fn (_this: *CCallbackInterfaceT) callconv(.c) void, + cache_read_size: *const fn (_this: *CCallbackInterfaceT, _id: u64) callconv(.c) u32, + cache_read: *const fn (_this: *CCallbackInterfaceT, _id: u64, _data: [*c]u8, _size: u32) callconv(.c) bool, + cache_write: *const fn (_this: *CCallbackInterfaceT, _id: u64, _data: [*c]u8, _size: u32) callconv(.c) void, + screen_shot: *const fn (_this: *CCallbackInterfaceT, _filePath: [*:0]const u8, _width: u32, _height: u32, _pitch: u32, _data: [*c]u8, _size: u32, _yflip: bool) callconv(.c) void, + capture_begin: *const fn (_this: *CCallbackInterfaceT, _width: u32, _height: u32, _pitch: u32, _format: bgfx.TextureFormat, _yflip: bool) callconv(.c) void, + capture_end: *const fn (_this: *CCallbackInterfaceT) callconv(.c) void, + capture_frame: *const fn (_this: *CCallbackInterfaceT, _data: [*c]u8, _size: u32) callconv(.c) void, }; pub const DefaultZigCallbackVTable = struct { - pub fn fatal(_this: *CCallbackInterfaceT, _filePath: [*:0]const u8, _line: u16, _code: bgfx.Fatal, c_str: [*:0]const u8) callconv(.C) void { + pub fn fatal(_this: *CCallbackInterfaceT, _filePath: [*:0]const u8, _line: u16, _code: bgfx.Fatal, c_str: [*:0]const u8) callconv(.c) void { _ = _this; const cstr = std.mem.span(c_str); log.err("FATAL in {s}:{d}: {s} => {s}", .{ _filePath, _line, @tagName(_code), cstr }); } - pub fn trace_vargs(_this: *CCallbackInterfaceT, _filePath: [*:0]const u8, _line: u16, _format: [*:0]const u8, va_list: Self.VaList) callconv(.C) void { + pub fn trace_vargs(_this: *CCallbackInterfaceT, _filePath: [*:0]const u8, _line: u16, _format: [*:0]const u8, va_list: Self.VaList) callconv(.c) void { _ = _this; _ = _filePath; _ = _line; @@ -87,42 +87,42 @@ pub const DefaultZigCallbackVTable = struct { log.debug("{s}", .{msg}); } - pub fn profiler_begin(_this: *Self.CCallbackInterfaceT, _name: [*:0]const u8, _abgr: u32, _filePath: [*:0]const u8, _line: u16) callconv(.C) void { + pub fn profiler_begin(_this: *Self.CCallbackInterfaceT, _name: [*:0]const u8, _abgr: u32, _filePath: [*:0]const u8, _line: u16) callconv(.c) void { _ = _this; _ = _name; _ = _abgr; _ = _filePath; _ = _line; } - pub fn profiler_begin_literal(_this: *Self.CCallbackInterfaceT, _name: [*:0]const u8, _abgr: u32, _filePath: [*:0]const u8, _line: u16) callconv(.C) void { + pub fn profiler_begin_literal(_this: *Self.CCallbackInterfaceT, _name: [*:0]const u8, _abgr: u32, _filePath: [*:0]const u8, _line: u16) callconv(.c) void { _ = _this; _ = _name; _ = _abgr; _ = _filePath; _ = _line; } - pub fn profiler_end(_this: *Self.CCallbackInterfaceT) callconv(.C) void { + pub fn profiler_end(_this: *Self.CCallbackInterfaceT) callconv(.c) void { _ = _this; } - pub fn cache_read_size(_this: *Self.CCallbackInterfaceT, _id: u64) callconv(.C) u32 { + pub fn cache_read_size(_this: *Self.CCallbackInterfaceT, _id: u64) callconv(.c) u32 { _ = _this; _ = _id; return 0; } - pub fn cache_read(_this: *Self.CCallbackInterfaceT, _id: u64, _data: [*c]u8, _size: u32) callconv(.C) bool { + pub fn cache_read(_this: *Self.CCallbackInterfaceT, _id: u64, _data: [*c]u8, _size: u32) callconv(.c) bool { _ = _this; _ = _id; _ = _data; _ = _size; return false; } - pub fn cache_write(_this: *Self.CCallbackInterfaceT, _id: u64, _data: [*c]u8, _size: u32) callconv(.C) void { + pub fn cache_write(_this: *Self.CCallbackInterfaceT, _id: u64, _data: [*c]u8, _size: u32) callconv(.c) void { _ = _this; _ = _id; _ = _data; _ = _size; } - pub fn screen_shot(_this: *Self.CCallbackInterfaceT, _filePath: [*:0]const u8, _width: u32, _height: u32, _pitch: u32, _data: [*c]u8, _size: u32, _yflip: bool) callconv(.C) void { + pub fn screen_shot(_this: *Self.CCallbackInterfaceT, _filePath: [*:0]const u8, _width: u32, _height: u32, _pitch: u32, _data: [*c]u8, _size: u32, _yflip: bool) callconv(.c) void { _ = _this; _ = _filePath; _ = _width; @@ -132,7 +132,7 @@ pub const DefaultZigCallbackVTable = struct { _ = _size; _ = _yflip; } - pub fn capture_begin(_this: *Self.CCallbackInterfaceT, _width: u32, _height: u32, _pitch: u32, _format: bgfx.TextureFormat, _yflip: bool) callconv(.C) void { + pub fn capture_begin(_this: *Self.CCallbackInterfaceT, _width: u32, _height: u32, _pitch: u32, _format: bgfx.TextureFormat, _yflip: bool) callconv(.c) void { _ = _this; _ = _width; _ = _height; @@ -141,10 +141,10 @@ pub const DefaultZigCallbackVTable = struct { _ = _yflip; log.warn("{s}", .{"Using capture without callback (a.k.a. pointless)."}); } - pub fn capture_end(_this: *Self.CCallbackInterfaceT) callconv(.C) void { + pub fn capture_end(_this: *Self.CCallbackInterfaceT) callconv(.c) void { _ = _this; } - pub fn capture_frame(_this: *Self.CCallbackInterfaceT, _data: [*c]u8, _size: u32) callconv(.C) void { + pub fn capture_frame(_this: *Self.CCallbackInterfaceT, _data: [*c]u8, _size: u32) callconv(.c) void { _ = _this; _ = _data; _ = _size; diff --git a/src/shaderc.zig b/src/shaderc.zig index 45844c7..03e176b 100644 --- a/src/shaderc.zig +++ b/src/shaderc.zig @@ -22,8 +22,8 @@ pub const ShaderType = enum { step.addArgs(&.{ "--type", t.toStr() }); } - pub fn appendArg(t: ShaderType, args: *ArgsList) !void { - try args.appendSlice(&.{ "--type", t.toStr() }); + pub fn appendArg(t: ShaderType, alloctor: std.mem.Allocator, args: *ArgsList) !void { + try args.appendSlice(alloctor, &.{ "--type", t.toStr() }); } }; @@ -44,8 +44,8 @@ pub const Optimize = enum(u32) { step.addArgs(&.{ "-O", t.toStr() }); } - pub fn appendArg(t: Optimize, args: *ArgsList) !void { - try args.appendSlice(&.{ "-O", t.toStr() }); + pub fn appendArg(t: Optimize, alloctor: std.mem.Allocator, args: *ArgsList) !void { + try args.appendSlice(alloctor, &.{ "-O", t.toStr() }); } }; @@ -69,8 +69,8 @@ pub const Platform = enum { step.addArgs(&.{ "--platform", platform.toStr() }); } - pub fn appendArg(platform: Platform, args: *ArgsList) !void { - try args.appendSlice(&.{ "--platform", platform.toStr() }); + pub fn appendArg(platform: Platform, alloctor: std.mem.Allocator, args: *ArgsList) !void { + try args.appendSlice(alloctor, &.{ "--platform", platform.toStr() }); } }; @@ -161,8 +161,8 @@ pub const Profile = enum { step.addArgs(&.{ "-p", profile.toStr() }); } - pub fn appendArg(profile: Profile, args: *ArgsList) !void { - try args.appendSlice(&.{ "-p", profile.toStr() }); + pub fn appendArg(profile: Profile, alloctor: std.mem.Allocator, args: *ArgsList) !void { + try args.appendSlice(alloctor, &.{ "-p", profile.toStr() }); } }; @@ -312,47 +312,47 @@ pub fn compileShader( } pub fn shadercProcess(allocator: std.mem.Allocator, executablePath: []const u8, options: ShadercOptions) !std.process.Child { - var args = ArgsList.init(allocator); - defer args.deinit(); - try args.append(executablePath); + var args = ArgsList{}; + defer args.deinit(allocator); + try args.append(allocator, executablePath); - try options.shaderType.appendArg(&args); - try options.platform.appendArg(&args); - try options.profile.appendArg(&args); - try options.optimizationLevel.appendArg(&args); + try options.shaderType.appendArg(allocator, &args); + try options.platform.appendArg(allocator, &args); + try options.profile.appendArg(allocator, &args); + try options.optimizationLevel.appendArg(allocator, &args); if (options.inputFilePath) |path| { - try args.appendSlice(&.{ "-f", path }); + try args.appendSlice(allocator, &.{ "-f", path }); } if (options.outputFilePath) |path| { - try args.appendSlice(&.{ "-o", path }); + try args.appendSlice(allocator, &.{ "-o", path }); } if (options.varyingFilePath) |path| { - try args.appendSlice(&.{ "--varyingdef", path }); + try args.appendSlice(allocator, &.{ "--varyingdef", path }); } if (options.includeDirs) |includes| { for (includes) |include| { - try args.appendSlice(&.{ "-i", include }); + try args.appendSlice(allocator, &.{ "-i", include }); } } - var all_defines = std.ArrayList(u8).init(allocator); - defer all_defines.deinit(); + var all_defines = std.ArrayList(u8){}; + defer all_defines.deinit(allocator); if (options.defines) |defines| { const last_idx = defines.len - 1; for (defines, 0..) |define, idx| { - try all_defines.appendSlice(define); + try all_defines.appendSlice(allocator, define); if (idx != last_idx) { - try all_defines.appendSlice(";"); + try all_defines.appendSlice(allocator, ";"); } } - try args.appendSlice(&.{ "--define", all_defines.items }); + try args.appendSlice(allocator, &.{ "--define", all_defines.items }); } var process = std.process.Child.init(args.items, allocator); diff --git a/tools/combine_bin_h.zig b/tools/combine_bin_h.zig index 645c4a4..c044d35 100644 --- a/tools/combine_bin_h.zig +++ b/tools/combine_bin_h.zig @@ -17,16 +17,22 @@ pub fn main() !void { }; defer output_file.close(); + var buffer: [1024]u8 = undefined; + var writer = output_file.writer(&buffer); + const w = &writer.interface; + defer w.flush() catch undefined; + var it: u32 = 3; while (it < args.len) : (it += 1) { const path = args[it]; var f = try std.fs.cwd().openFile(path, .{}); + defer f.close(); - try output_file.writeFileAll(f, .{}); + var reader = f.reader(&.{}); + _ = try w.sendFileAll(&reader, .unlimited); } - var w = output_file.writer(); try w.print("extern const uint8_t* {s}_pssl;\n", .{name}); try w.print("extern const uint32_t {s}_pssl_size;\n", .{name}); } diff --git a/tools/combine_bin_zig.zig b/tools/combine_bin_zig.zig index dd4911f..710f9cd 100644 --- a/tools/combine_bin_zig.zig +++ b/tools/combine_bin_zig.zig @@ -25,7 +25,10 @@ pub fn main() !void { }; defer output_file.close(); - var w = output_file.writer(); + var buffer: [1024]u8 = undefined; + var writer = output_file.writer(&buffer); + const w = &writer.interface; + defer w.flush() catch undefined; try w.print("//\n", .{}); try w.print("// GENERATED - DO NOT EDIT\n", .{});