Skip to content

Commit

Permalink
Merge branch 'Snektron:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
flut2 authored Jul 6, 2024
2 parents 0623579 + 51c5566 commit 982178e
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 38 deletions.
20 changes: 3 additions & 17 deletions examples/graphics_context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,9 @@ fn pickPhysicalDevice(
allocator: Allocator,
surface: vk.SurfaceKHR,
) !DeviceCandidate {
var device_count: u32 = undefined;
_ = try instance.enumeratePhysicalDevices(&device_count, null);

const pdevs = try allocator.alloc(vk.PhysicalDevice, device_count);
const pdevs = try instance.enumeratePhysicalDevicesAlloc(allocator);
defer allocator.free(pdevs);

_ = try instance.enumeratePhysicalDevices(&device_count, pdevs.ptr);

for (pdevs) |pdev| {
if (try checkSuitable(instance, pdev, allocator, surface)) |candidate| {
return candidate;
Expand Down Expand Up @@ -240,12 +235,8 @@ fn checkSuitable(
}

fn allocateQueues(instance: Instance, pdev: vk.PhysicalDevice, allocator: Allocator, surface: vk.SurfaceKHR) !?QueueAllocation {
var family_count: u32 = undefined;
instance.getPhysicalDeviceQueueFamilyProperties(pdev, &family_count, null);

const families = try allocator.alloc(vk.QueueFamilyProperties, family_count);
const families = try instance.getPhysicalDeviceQueueFamilyPropertiesAlloc(pdev, allocator);
defer allocator.free(families);
instance.getPhysicalDeviceQueueFamilyProperties(pdev, &family_count, families.ptr);

var graphics_family: ?u32 = null;
var present_family: ?u32 = null;
Expand Down Expand Up @@ -287,14 +278,9 @@ fn checkExtensionSupport(
pdev: vk.PhysicalDevice,
allocator: Allocator,
) !bool {
var count: u32 = undefined;
_ = try instance.enumerateDeviceExtensionProperties(pdev, null, &count, null);

const propsv = try allocator.alloc(vk.ExtensionProperties, count);
const propsv = try instance.enumerateDeviceExtensionPropertiesAlloc(pdev, null, allocator);
defer allocator.free(propsv);

_ = try instance.enumerateDeviceExtensionProperties(pdev, null, &count, propsv.ptr);

for (required_device_extensions) |ext| {
for (propsv) |props| {
if (std.mem.eql(u8, std.mem.span(ext), std.mem.sliceTo(&props.extension_name, 0))) {
Expand Down
17 changes: 4 additions & 13 deletions examples/swapchain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,10 @@ const SwapImage = struct {
};

fn initSwapchainImages(gc: *const GraphicsContext, swapchain: vk.SwapchainKHR, format: vk.Format, allocator: Allocator) ![]SwapImage {
var count: u32 = undefined;
_ = try gc.dev.getSwapchainImagesKHR(swapchain, &count, null);
const images = try allocator.alloc(vk.Image, count);
const images = try gc.dev.getSwapchainImagesAllocKHR(swapchain, allocator);
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);

var i: usize = 0;
Expand All @@ -273,11 +270,8 @@ fn findSurfaceFormat(gc: *const GraphicsContext, allocator: Allocator) !vk.Surfa
.color_space = .srgb_nonlinear_khr,
};

var count: u32 = undefined;
_ = try gc.instance.getPhysicalDeviceSurfaceFormatsKHR(gc.pdev, gc.surface, &count, null);
const surface_formats = try allocator.alloc(vk.SurfaceFormatKHR, count);
const surface_formats = try gc.instance.getPhysicalDeviceSurfaceFormatsAllocKHR(gc.pdev, gc.surface, allocator);
defer allocator.free(surface_formats);
_ = try gc.instance.getPhysicalDeviceSurfaceFormatsKHR(gc.pdev, gc.surface, &count, surface_formats.ptr);

for (surface_formats) |sfmt| {
if (std.meta.eql(sfmt, preferred)) {
Expand All @@ -289,11 +283,8 @@ fn findSurfaceFormat(gc: *const GraphicsContext, allocator: Allocator) !vk.Surfa
}

fn findPresentMode(gc: *const GraphicsContext, allocator: Allocator) !vk.PresentModeKHR {
var count: u32 = undefined;
_ = try gc.instance.getPhysicalDeviceSurfacePresentModesKHR(gc.pdev, gc.surface, &count, null);
const present_modes = try allocator.alloc(vk.PresentModeKHR, count);
const present_modes = try gc.instance.getPhysicalDeviceSurfacePresentModesAllocKHR(gc.pdev, gc.surface, allocator);
defer allocator.free(present_modes);
_ = try gc.instance.getPhysicalDeviceSurfacePresentModesKHR(gc.pdev, gc.surface, &count, present_modes.ptr);

const preferred = [_]vk.PresentModeKHR{
.mailbox_khr,
Expand Down
Loading

0 comments on commit 982178e

Please sign in to comment.