Use *Alloc() functions in example

This commit is contained in:
Patrick O'Connell
2024-06-29 17:13:06 -07:00
parent 7a6986faba
commit 4707aca68d
3 changed files with 11 additions and 30 deletions

View File

@@ -196,14 +196,9 @@ fn pickPhysicalDevice(
allocator: Allocator, allocator: Allocator,
surface: vk.SurfaceKHR, surface: vk.SurfaceKHR,
) !DeviceCandidate { ) !DeviceCandidate {
var device_count: u32 = undefined; const pdevs = try instance.enumeratePhysicalDevicesAlloc(allocator);
_ = try instance.enumeratePhysicalDevices(&device_count, null);
const pdevs = try allocator.alloc(vk.PhysicalDevice, device_count);
defer allocator.free(pdevs); defer allocator.free(pdevs);
_ = try instance.enumeratePhysicalDevices(&device_count, pdevs.ptr);
for (pdevs) |pdev| { for (pdevs) |pdev| {
if (try checkSuitable(instance, pdev, allocator, surface)) |candidate| { if (try checkSuitable(instance, pdev, allocator, surface)) |candidate| {
return candidate; return candidate;
@@ -240,12 +235,8 @@ fn checkSuitable(
} }
fn allocateQueues(instance: Instance, pdev: vk.PhysicalDevice, allocator: Allocator, surface: vk.SurfaceKHR) !?QueueAllocation { fn allocateQueues(instance: Instance, pdev: vk.PhysicalDevice, allocator: Allocator, surface: vk.SurfaceKHR) !?QueueAllocation {
var family_count: u32 = undefined; const families = try instance.getPhysicalDeviceQueueFamilyPropertiesAlloc(pdev, allocator);
instance.getPhysicalDeviceQueueFamilyProperties(pdev, &family_count, null);
const families = try allocator.alloc(vk.QueueFamilyProperties, family_count);
defer allocator.free(families); defer allocator.free(families);
instance.getPhysicalDeviceQueueFamilyProperties(pdev, &family_count, families.ptr);
var graphics_family: ?u32 = null; var graphics_family: ?u32 = null;
var present_family: ?u32 = null; var present_family: ?u32 = null;
@@ -287,14 +278,9 @@ fn checkExtensionSupport(
pdev: vk.PhysicalDevice, pdev: vk.PhysicalDevice,
allocator: Allocator, allocator: Allocator,
) !bool { ) !bool {
var count: u32 = undefined; const propsv = try instance.enumerateDeviceExtensionPropertiesAlloc(pdev, null, allocator);
_ = try instance.enumerateDeviceExtensionProperties(pdev, null, &count, null);
const propsv = try allocator.alloc(vk.ExtensionProperties, count);
defer allocator.free(propsv); defer allocator.free(propsv);
_ = try instance.enumerateDeviceExtensionProperties(pdev, null, &count, propsv.ptr);
for (required_device_extensions) |ext| { for (required_device_extensions) |ext| {
for (propsv) |props| { for (propsv) |props| {
if (std.mem.eql(u8, std.mem.span(ext), std.mem.sliceTo(&props.extension_name, 0))) { if (std.mem.eql(u8, std.mem.span(ext), std.mem.sliceTo(&props.extension_name, 0))) {

View File

@@ -247,13 +247,10 @@ const SwapImage = struct {
}; };
fn initSwapchainImages(gc: *const GraphicsContext, swapchain: vk.SwapchainKHR, format: vk.Format, allocator: Allocator) ![]SwapImage { fn initSwapchainImages(gc: *const GraphicsContext, swapchain: vk.SwapchainKHR, format: vk.Format, allocator: Allocator) ![]SwapImage {
var count: u32 = undefined; const images = try gc.dev.getSwapchainImagesKhrAlloc(swapchain, allocator);
_ = try gc.dev.getSwapchainImagesKHR(swapchain, &count, null);
const images = try allocator.alloc(vk.Image, count);
defer allocator.free(images); defer allocator.free(images);
_ = try gc.dev.getSwapchainImagesKHR(swapchain, &count, images.ptr);
const swap_images = try allocator.alloc(SwapImage, count); const swap_images = try allocator.alloc(SwapImage, images.len);
errdefer allocator.free(swap_images); errdefer allocator.free(swap_images);
var i: usize = 0; var i: usize = 0;
@@ -273,11 +270,8 @@ fn findSurfaceFormat(gc: *const GraphicsContext, allocator: Allocator) !vk.Surfa
.color_space = .srgb_nonlinear_khr, .color_space = .srgb_nonlinear_khr,
}; };
var count: u32 = undefined; const surface_formats = try gc.instance.getPhysicalDeviceSurfaceFormatsKhrAlloc(gc.pdev, gc.surface, allocator);
_ = try gc.instance.getPhysicalDeviceSurfaceFormatsKHR(gc.pdev, gc.surface, &count, null);
const surface_formats = try allocator.alloc(vk.SurfaceFormatKHR, count);
defer allocator.free(surface_formats); defer allocator.free(surface_formats);
_ = try gc.instance.getPhysicalDeviceSurfaceFormatsKHR(gc.pdev, gc.surface, &count, surface_formats.ptr);
for (surface_formats) |sfmt| { for (surface_formats) |sfmt| {
if (std.meta.eql(sfmt, preferred)) { if (std.meta.eql(sfmt, preferred)) {
@@ -289,11 +283,8 @@ fn findSurfaceFormat(gc: *const GraphicsContext, allocator: Allocator) !vk.Surfa
} }
fn findPresentMode(gc: *const GraphicsContext, allocator: Allocator) !vk.PresentModeKHR { fn findPresentMode(gc: *const GraphicsContext, allocator: Allocator) !vk.PresentModeKHR {
var count: u32 = undefined; const present_modes = try gc.instance.getPhysicalDeviceSurfacePresentModesKhrAlloc(gc.pdev, gc.surface, allocator);
_ = try gc.instance.getPhysicalDeviceSurfacePresentModesKHR(gc.pdev, gc.surface, &count, null);
const present_modes = try allocator.alloc(vk.PresentModeKHR, count);
defer allocator.free(present_modes); defer allocator.free(present_modes);
_ = try gc.instance.getPhysicalDeviceSurfacePresentModesKHR(gc.pdev, gc.surface, &count, present_modes.ptr);
const preferred = [_]vk.PresentModeKHR{ const preferred = [_]vk.PresentModeKHR{
.mailbox_khr, .mailbox_khr,

View File

@@ -197,6 +197,10 @@ const enumerate_functions = std.StaticStringMap(void).initComptime(.{
.{"vkEnumerateInstanceExtensionProperties"}, .{"vkEnumerateInstanceExtensionProperties"},
.{"vkEnumeratePhysicalDevices"}, .{"vkEnumeratePhysicalDevices"},
.{"vkGetPhysicalDeviceQueueFamilyProperties"}, .{"vkGetPhysicalDeviceQueueFamilyProperties"},
.{"vkGetPhysicalDeviceSurfaceFormatsKHR"},
.{"vkGetPhysicalDeviceSurfacePresentModesKHR"},
.{"vkEnumerateDeviceExtensionProperties"},
.{"vkGetSwapchainImagesKHR"},
}); });
// Given one of the above commands, returns the type of the array elements // Given one of the above commands, returns the type of the array elements