83 lines
3 KiB
Zig
83 lines
3 KiB
Zig
const std = @import("std");
|
|
const vk = @import("vulkan");
|
|
|
|
const Context = @import("Context.zig");
|
|
const Utilities = @import("utilities.zig");
|
|
|
|
pub fn createImage(
|
|
ctx: Context,
|
|
width: u32,
|
|
height: u32,
|
|
format: vk.Format,
|
|
tiling: vk.ImageTiling,
|
|
use_flags: vk.ImageUsageFlags,
|
|
prop_flags: vk.MemoryPropertyFlags,
|
|
image_memory: *vk.DeviceMemory,
|
|
) !vk.Image {
|
|
// -- Create Image --
|
|
const image_create_info: vk.ImageCreateInfo = .{
|
|
.image_type = .@"2d", // Type of image (1D, 2D or 3D)
|
|
.extent = .{
|
|
.width = width, // Width of image extent
|
|
.height = height, // Height of image extent
|
|
.depth = 1, // Depth of image (just 1, no 3D aspecct)
|
|
},
|
|
.mip_levels = 1, // Number of mipmap levels
|
|
.array_layers = 1, // Number of level in image array
|
|
.format = format, // Format type of image
|
|
.tiling = tiling, // How image data should be tiled (arranged for optimal reading)
|
|
.initial_layout = .undefined, // Layout of image data on creation
|
|
.usage = use_flags, // Bit flags defining what image will be used for
|
|
.samples = .{ .@"1_bit" = true }, // Number of samples for multi-sampling
|
|
.sharing_mode = .exclusive, // Whether image can be shared between queues
|
|
};
|
|
|
|
const image = try ctx.device.createImage(&image_create_info, null);
|
|
|
|
// -- Create memory for image --
|
|
// Get memory requirements for a type of image
|
|
const memory_requirements = ctx.device.getImageMemoryRequirements(image);
|
|
|
|
// Allocate memory using image requirements and user-defined properties
|
|
const memory_alloc_info: vk.MemoryAllocateInfo = .{
|
|
.allocation_size = memory_requirements.size,
|
|
.memory_type_index = Utilities.findMemoryTypeIndex(ctx.physical_device, ctx.instance, memory_requirements.memory_type_bits, prop_flags),
|
|
};
|
|
|
|
image_memory.* = try ctx.device.allocateMemory(&memory_alloc_info, null);
|
|
|
|
// Connect memory to image
|
|
try ctx.device.bindImageMemory(image, image_memory.*, 0);
|
|
|
|
return image;
|
|
}
|
|
|
|
pub fn createImageView(
|
|
ctx: Context,
|
|
image: vk.Image,
|
|
format: vk.Format,
|
|
aspect_flags: vk.ImageAspectFlags,
|
|
) !vk.ImageView {
|
|
const image_view_create_info: vk.ImageViewCreateInfo = .{
|
|
.image = image,
|
|
.format = format,
|
|
.view_type = .@"2d",
|
|
.components = .{
|
|
// Used for remapping rgba values to other rgba values
|
|
.r = .identity,
|
|
.g = .identity,
|
|
.b = .identity,
|
|
.a = .identity,
|
|
},
|
|
.subresource_range = .{
|
|
.aspect_mask = aspect_flags, // Which aspect of image to view (e.g.: colour, depth, stencil, etc...)
|
|
.base_mip_level = 0, // Start mipmap level to view from
|
|
.level_count = 1, // Number of mipmap levels to view
|
|
.base_array_layer = 0, // Start array level to view from
|
|
.layer_count = 1, // Number of array levels to view
|
|
},
|
|
};
|
|
|
|
return try ctx.device.createImageView(&image_view_create_info, null);
|
|
}
|