Initial commit

This commit is contained in:
Przemyslaw Gasinski 2024-06-22 22:10:45 +02:00
commit 06eb4ac012
6 changed files with 27339 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/zig-out
/.zig-cache

77
build.zig Normal file
View file

@ -0,0 +1,77 @@
const std = @import("std");
const sdl = @import("sdl");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "vulkan-test",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const vkzig_dep = b.dependency("vulkan_zig", .{
.registry = @as([]const u8, b.pathFromRoot("./vk.xml")),
});
const vkzig_bindings = vkzig_dep.module("vulkan-zig");
exe.root_module.addImport("vulkan", vkzig_bindings);
const sdl_sdk = sdl.init(b, null);
sdl_sdk.link(exe, .dynamic);
exe.root_module.addImport("sdl2", sdl_sdk.getWrapperModuleVulkan(vkzig_bindings));
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_unit_tests.step);
}

43
build.zig.zon Normal file
View file

@ -0,0 +1,43 @@
.{
// This is the default name used by packages depending on this one. For
// example, when a user runs `zig fetch --save <url>`, this field is used
// as the key in the `dependencies` table. Although the user can choose a
// different name, most users will stick with this provided value.
//
// It is redundant to include "zig" in this name because it is already
// within the Zig package namespace.
.name = "vulkan-test",
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.0.0",
// This field is optional.
// This is currently advisory only; Zig does not yet do anything
// with this value.
//.minimum_zig_version = "0.11.0",
// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{
.vulkan_zig = .{
.url = "https://github.com/Snektron/vulkan-zig/archive/f637a0d2525f62c7803c18c89a95cc2f8d8b2789.tar.gz",
.hash = "12209c5e452e25acb4570cda7cfec3c23bac53b49dbcfb96b4555d0e73c39310283b",
},
.sdl = .{
.url = "https://github.com/ikskuh/SDL.zig/archive/3d310985c2e8b77df2af8089a9bfb052eea62c7d.tar.gz",
.hash = "1220821a34cc5fa538f4f5f96541322019e73104586e4f3fbafc614646e8e9bf64d0",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
// For example...
//"LICENSE",
//"README.md",
},
}

49
src/main.zig Normal file
View file

@ -0,0 +1,49 @@
const std = @import("std");
const vk = @import("vulkan");
const sdl = @import("sdl2");
const VulkanRenderer = @import("vulkan_renderer.zig").VulkanRenderer;
pub fn main() !void {
const window = try initWindow();
defer sdl.quit();
defer sdl.vulkan.unloadLibrary();
defer window.destroy();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const vulkan_renderer = try VulkanRenderer.init(window, allocator);
_ = vulkan_renderer;
mainLoop: while (true) {
while (sdl.pollEvent()) |ev| {
switch (ev) {
.quit => break :mainLoop,
.key_down => |key| {
if (key.keycode == sdl.Keycode.escape) {
break :mainLoop;
}
},
else => {},
}
}
}
}
fn initWindow() !sdl.Window {
sdl.init(.{ .timer = true, .video = true, .events = true }) catch {
std.log.err("Failed to initialize SDL2", .{});
};
try sdl.vulkan.loadLibrary(null);
return try sdl.createWindow(
"Vulkan Test",
.{ .centered = {} },
.{ .centered = {} },
800,
600,
.{ .vis = .shown, .context = .vulkan },
);
}

120
src/vulkan_renderer.zig Normal file
View file

@ -0,0 +1,120 @@
const std = @import("std");
const sdl = @import("sdl2");
const vk = @import("vulkan");
const apis: []const vk.ApiInfo = &.{
// You can either add invidiual functions by manually creating an 'api'
.{
.base_commands = .{
.createInstance = true,
},
.instance_commands = .{
.createDevice = true,
},
},
// Or you can add entire feature sets or extensions
vk.features.version_1_0,
vk.extensions.khr_surface,
vk.extensions.khr_swapchain,
};
const BaseDispatch = vk.BaseWrapper(apis);
const InstanceDispatch = vk.InstanceWrapper(apis);
const Instance = vk.InstanceProxy(apis);
pub const VulkanRenderer = struct {
const Self = @This();
allocator: std.mem.Allocator,
vkb: BaseDispatch,
window: sdl.Window,
instance: Instance,
surface: vk.SurfaceKHR,
pub fn init(window: sdl.Window, allocator: std.mem.Allocator) !Self {
const vkb = try BaseDispatch.load(try sdl.vulkan.getVkGetInstanceProcAddr());
const extensions = try sdl.vulkan.getInstanceExtensionsAlloc(window, allocator);
defer allocator.free(extensions);
std.debug.print("[Required instance extensions]\n", .{});
for (extensions) |ext| {
std.debug.print("\t- {s}\n", .{ext});
}
if (!try checkExtensions(vkb, &extensions, allocator)) {
return error.ExtensionNotPresent;
}
const app_info = vk.ApplicationInfo{
.p_application_name = "Vulkan SDL Test",
.application_version = vk.makeApiVersion(0, 0, 1, 0),
.p_engine_name = "Vulkan SDL Test",
.engine_version = vk.makeApiVersion(0, 0, 1, 0),
.api_version = vk.API_VERSION_1_3,
};
const inst = try vkb.createInstance(&.{
.p_application_info = &app_info,
.enabled_extension_count = @intCast(extensions.len),
.pp_enabled_extension_names = @ptrCast(extensions),
}, null);
const vki = try allocator.create(InstanceDispatch);
defer allocator.destroy(vki);
vki.* = try InstanceDispatch.load(inst, vkb.dispatch.vkGetInstanceProcAddr);
var instance = Instance.init(inst, vki);
errdefer instance.destroyInstance(null);
const surface = try sdl.vulkan.createSurface(window, instance.handle);
var physical_device_count: u32 = 0;
_ = try instance.enumeratePhysicalDevices(&physical_device_count, null);
std.debug.print("Physical devices: {d}\n", .{physical_device_count});
return .{
.allocator = allocator,
.window = window,
.vkb = vkb,
.instance = instance,
.surface = surface,
};
}
pub fn deinit(self: *Self) void {
self.instance.destroySurfaceKHR(self.surface, null);
self.instance.destroyInstance(null);
self.allocator.destroy(self.instance.wrapper);
}
};
fn checkExtensions(vkb: BaseDispatch, required_extensions: *const [][*:0]const u8, allocator: std.mem.Allocator) !bool {
var prop_count: u32 = 0;
_ = try vkb.enumerateInstanceExtensionProperties(null, &prop_count, null);
const props = try allocator.alloc(vk.ExtensionProperties, prop_count);
defer allocator.free(props);
_ = try vkb.enumerateInstanceExtensionProperties(null, &prop_count, props.ptr);
for (required_extensions.*) |required_extension| {
var found = false;
for (props) |prop| {
if (std.mem.eql(u8, std.mem.sliceTo(&prop.extension_name, 0), std.mem.span(required_extension))) {
found = true;
}
}
if (!found) {
return false;
}
}
return true;
}

27048
vk.xml Normal file

File diff suppressed because it is too large Load diff