Skip to content

Commit dbfe25e

Browse files
committed
compatibility with zig 0.16 and improvements
1 parent 62b7d55 commit dbfe25e

4 files changed

Lines changed: 74 additions & 66 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.zig-cache/
2+
zig-pkg/
23
zig-out/
34
gitignore_*

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ packaged for the [Zig](https://ziglang.org/) build system.
55

66
## how to use
77

8-
1. Add `VulkanMemoryAllocator` to the dependency list in `build.zig.zon`:
8+
1. Add `VulkanMemoryAllocator` to the dependency list in `build.zig.zon`:
99

1010
```sh
1111
zig fetch --save git+https://github.com/johan0A/VulkanMemoryAllocator
@@ -17,6 +17,11 @@ zig fetch --save git+https://github.com/johan0A/VulkanMemoryAllocator
1717
const vma_dep = b.dependency("VulkanMemoryAllocator", .{
1818
.target = target,
1919
.optimize = optimize,
20+
.VMA_DYNAMIC_VULKAN_FUNCTIONS = true,
21+
.VMA_STATIC_VULKAN_FUNCTIONS = false,
2022
});
21-
root_module.linkLibrary("vma", vma_dep.artifact("VulkanMemoryAllocator"));
23+
root_module.linkLibrary(vma_dep.artifact("VulkanMemoryAllocator"));
24+
// Installed headers along the VulkanMemoryAllocator artifact:
25+
// "vk_mem_alloc_config.h" generated configuration header that reflects the provided options, should be included before vk_mem_alloc.h
26+
// "vk_mem_alloc.h"
2227
```

build.zig

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,80 @@
11
const std = @import("std");
22

33
pub fn build(b: *std.Build) !void {
4-
const lib = b.addLibrary(.{
5-
.name = "VulkanMemoryAllocator",
6-
.linkage = b.option(std.builtin.LinkMode, "linkage", "defaults to static") orelse .static,
7-
.root_module = b.createModule(.{
8-
.target = b.standardTargetOptions(.{}),
9-
.optimize = b.standardOptimizeOption(.{}),
10-
}),
11-
});
12-
lib.linkLibCpp();
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
136

14-
const install_vulkan_headers = b.option(bool, "install-vulkan-headers", "(defaults to false)") orelse false;
7+
const options = .{
8+
.{ "VMA_STATIC_VULKAN_FUNCTIONS", bool, "Use statically linked Vulkan functions" },
9+
.{ "VMA_DYNAMIC_VULKAN_FUNCTIONS", bool, "Dynamically load Vulkan functions" },
10+
.{ "VMA_VULKAN_VERSION", i64, "Target specific Vulkan API version" },
11+
.{ "VMA_STATS_STRING_ENABLED", bool, "Enable vmaBuildStatsString / vmaFreeStatsString" },
12+
.{ "VMA_DEDICATED_ALLOCATION", bool, "Enable KHR dedicated allocation support" },
13+
.{ "VMA_BIND_MEMORY2", bool, "Enable KHR bind memory 2 support" },
14+
.{ "VMA_MEMORY_BUDGET", bool, "Enable memory budget tracking" },
15+
.{ "VMA_BUFFER_DEVICE_ADDRESS", bool, "Enable buffer device address support" },
16+
.{ "VMA_MEMORY_PRIORITY", bool, "Enable memory priority support" },
17+
.{ "VMA_KHR_MAINTENANCE4", bool, "Enable KHR maintenance4 support" },
18+
.{ "VMA_KHR_MAINTENANCE5", bool, "Enable KHR maintenance5 support" },
19+
.{ "VMA_EXTERNAL_MEMORY", bool, "Enable external memory support" },
20+
.{ "VMA_EXTERNAL_MEMORY_WIN32", bool, "Enable Win32 external memory handle support" },
1521

16-
if (b.option(std.Build.LazyPath, "vulkan-headers-path", "Path to Vulkan headers (defaults to bundled headers)")) |vulkan_headers_path| {
17-
lib.addIncludePath(vulkan_headers_path);
18-
if (install_vulkan_headers) lib.installHeadersDirectory(vulkan_headers_path, "", .{});
19-
} else {
20-
if (b.lazyDependency("vulkan_headers", .{})) |vulkan_headers| {
21-
lib.addIncludePath(vulkan_headers.namedLazyPath("vulkan-headers"));
22-
if (install_vulkan_headers) lib.installHeadersDirectory(vulkan_headers.namedLazyPath("vulkan-headers"), "", .{});
22+
.{ "VMA_DEBUG_ALWAYS_DEDICATED_MEMORY", bool, "Force every allocation into its own VkDeviceMemory" },
23+
.{ "VMA_DEBUG_INITIALIZE_ALLOCATIONS", bool, "Fill new/destroyed allocations with a bit pattern" },
24+
.{ "VMA_DEBUG_DETECT_CORRUPTION", bool, "Enable corruption detection (requires VMA_DEBUG_MARGIN > 0)" },
25+
.{ "VMA_DEBUG_GLOBAL_MUTEX", bool, "Single global mutex protecting all entry points" },
26+
.{ "VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT", bool, "Respect maxMemoryAllocationCount" },
27+
.{ "VMA_DEBUG_DONT_EXCEED_HEAP_SIZE_WITH_ALLOCATION_SIZE", bool, "Refuse allocations exceeding heap size" },
28+
.{ "VMA_MAPPING_HYSTERESIS_ENABLED", bool, "Enable mapping hysteresis to avoid frequent map/unmap" },
29+
.{ "VMA_MIN_ALIGNMENT", i64, "Minimum alignment of all allocations in bytes (power of two)" },
30+
.{ "VMA_DEBUG_MARGIN", i64, "Margin in bytes after every allocation for corruption detection" },
31+
.{ "VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY", i64, "Override minimum bufferImageGranularity" },
32+
.{ "VMA_SMALL_HEAP_MAX_SIZE", i64, "Max heap size in bytes to consider 'small'" },
33+
.{ "VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE", i64, "Default block size in bytes for large heap allocations" },
34+
};
35+
36+
const config_h = b.addConfigHeader(.{ .include_path = "vk_mem_alloc_config.h" }, .{});
37+
38+
inline for (options) |entry| {
39+
const name, const T, const desc = entry;
40+
if (b.option(T, name, desc)) |value| {
41+
config_h.addValue(name, T, value);
2342
}
2443
}
2544

26-
const upstream = b.dependency("VulkanMemoryAllocator", .{});
27-
lib.installHeader(upstream.path("include/vk_mem_alloc.h"), "vk_mem_alloc.h");
28-
lib.addIncludePath(upstream.path("include/"));
45+
const root_module = b.createModule(.{
46+
.target = target,
47+
.optimize = optimize,
48+
.link_libcpp = true,
49+
});
2950

30-
lib.addCSourceFile(.{
31-
.file = b.addWriteFiles().add("vk_mem_alloc.cpp", "#include \"vk_mem_alloc.h\""),
32-
.flags = &.{ "-DVMA_IMPLEMENTATION", "-std=c++17" },
51+
root_module.addCSourceFile(.{
52+
.file = b.addWriteFiles().add("stub.cpp",
53+
\\#include "vk_mem_alloc_config.h"
54+
\\#define VMA_IMPLEMENTATION
55+
\\#include "vk_mem_alloc.h"
56+
),
57+
.flags = &.{"-std=c++17"},
3358
});
3459

35-
{
36-
const allocPrint = std.fmt.allocPrint;
37-
const bool_options = [_][2][]const u8{
38-
.{ "macro_static_vulkan_functions", "VMA_STATIC_VULKAN_FUNCTIONS" },
39-
.{ "macro_dynamic_vulkan_functions", "VMA_DYNAMIC_VULKAN_FUNCTIONS" },
40-
.{ "macro_stats_string_enabled", "VMA_STATS_STRING_ENABLED" },
41-
.{ "macro_debug_initialize_allocations", "VMA_DEBUG_INITIALIZE_ALLOCATIONS" },
42-
.{ "macro_debug_detect_corruption", "VMA_DEBUG_DETECT_CORRUPTION" },
43-
.{ "macro_debug_global_mutex", "VMA_DEBUG_GLOBAL_MUTEX" },
44-
.{ "macro_use_stl_shared_mutex", "VMA_USE_STL_SHARED_MUTEX" },
45-
.{ "macro_debug_always_dedicated_memory", "VMA_DEBUG_ALWAYS_DEDICATED_MEMORY" },
46-
.{ "macro_debug_dont_exceed_max_memory_allocation_count", "VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT" },
47-
.{ "macro_mapping_hysteresis_enabled", "VMA_MAPPING_HYSTERESIS_ENABLED" },
48-
};
49-
for (bool_options) |bool_option| {
50-
if (b.option(bool, bool_option[0], "")) |opt|
51-
lib.root_module.addCMacro(bool_option[1], try allocPrint(b.allocator, "{}", .{@intFromBool(opt)}));
52-
}
60+
root_module.addIncludePath(config_h.getOutputDir());
5361

54-
if (b.option(i64, "macro_debug_min_buffer_image_granularity", "")) |opt|
55-
lib.root_module.addCMacro("VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY", try allocPrint(b.allocator, "{}", .{opt}));
56-
if (b.option(i64, "macro_debug_margin", "")) |opt|
57-
lib.root_module.addCMacro("VMA_DEBUG_MARGIN", try allocPrint(b.allocator, "{}", .{opt}));
58-
if (b.option(i64, "macro_min_alignment", "")) |opt|
59-
lib.root_module.addCMacro("VMA_MIN_ALIGNMENT", try allocPrint(b.allocator, "{}", .{opt}));
62+
const upstream = b.dependency("VulkanMemoryAllocator", .{});
63+
root_module.addIncludePath(upstream.path("include/"));
6064

61-
if (b.option(u64, "macro_small_heap_max_size", "")) |opt|
62-
lib.root_module.addCMacro("VMA_SMALL_HEAP_MAX_SIZE", try allocPrint(b.allocator, "{}ull", .{opt}));
63-
if (b.option(u64, "macro_default_large_heap_block_size", "")) |opt|
64-
lib.root_module.addCMacro("VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE", try allocPrint(b.allocator, "{}ull", .{opt}));
65+
if (b.option(std.Build.LazyPath, "vulkan-include-path", "Path to Vulkan headers")) |vulkan_include_path| {
66+
root_module.addIncludePath(vulkan_include_path);
6567

66-
if (b.option([]const u8, "macro_null", "")) |opt|
67-
lib.root_module.addCMacro("VMA_NULL", opt);
68-
if (b.option([]const u8, "macro_configuration_user_includes_h", "")) |opt|
69-
lib.root_module.addCMacro("VMA_CONFIGURATION_USER_INCLUDES_H", try allocPrint(b.allocator, "\"{s}\"", .{opt}));
68+
const lib = b.addLibrary(.{
69+
.name = "VulkanMemoryAllocator",
70+
.linkage = b.option(std.builtin.LinkMode, "linkage", "defaults to static") orelse .static,
71+
.root_module = root_module,
72+
});
73+
b.installArtifact(lib);
74+
lib.installHeader(upstream.path("include/vk_mem_alloc.h"), "vk_mem_alloc.h");
75+
lib.installHeader(config_h.getOutputFile(), "vk_mem_alloc_config.h");
76+
} else {
77+
const fail = b.addFail("missing vulkan headers, specify a path to vulkan headers using the vulkan-include-path option");
78+
b.getInstallStep().dependOn(&fail.step);
7079
}
71-
72-
b.installArtifact(lib);
7380
}

build.zig.zon

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
.{
22
.name = .VulkanMemoryAllocator,
3-
.version = "0.1.2+3.3.0",
3+
.version = "0.2.0+3.3.0",
44
.fingerprint = 0xb8aa25de81d2ad78, // Changing this has security and trust implications.
5-
.minimum_zig_version = "0.14.0",
5+
.minimum_zig_version = "0.15.2",
66
.dependencies = .{
77
.VulkanMemoryAllocator = .{
88
.url = "git+https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator?ref=v3.3.0#1d8f600fd424278486eade7ed3e877c99f0846b1",
99
.hash = "N-V-__8AAIutPgABk6NlXZhlJ5P8qVECvakZIKoO94h7xUOw",
1010
},
11-
.vulkan_headers = .{
12-
.url = "git+https://github.com/johan0A/vulkan-headers-zig#acab9467e982b249203db493dd5a3089aeb4c767",
13-
.hash = "vulkan_headers-0.1.0+1.4.326-IPSI1uADAACG8vYTRpu3hY-wLEy_WGmJ5YwI_v2ajFAp",
14-
.lazy = true,
15-
},
1611
},
1712
.paths = .{
1813
"build.zig",

0 commit comments

Comments
 (0)