From 4158fed109ae789cb3ca7ff53c2790797a3b4087 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Sun, 9 Aug 2026 17:10:03 -0700 Subject: refactor Texture into its own thing as part of this, the command pools are moved from window dressing to permanent note also how mip count is a value computed by loading the texture, and used when creating the sampler, which is part of the window dressing. the assumption about how it's computed will have to change when we support multiple textures, but for now we settle for just pushing the assumption to the top level. Force-Push: yes Change-Id: I545c53feedc99628fbe943120798fe66fe9e065b --- src/graphics/util.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'src/graphics/util.rs') diff --git a/src/graphics/util.rs b/src/graphics/util.rs index c970d18..d09d144 100644 --- a/src/graphics/util.rs +++ b/src/graphics/util.rs @@ -150,6 +150,84 @@ pub fn pick_memory_type(instance: &Instance, } +#[allow(unsafe_code)] +pub fn allocate_image(instance: &Instance, device: &Device, width: u32, + height: u32, mip_count: u32, + sample_count: vk::SampleCountFlags, format: vk::Format, + tiling: vk::ImageTiling, usage: vk::ImageUsageFlags, + memory_flags: vk::MemoryPropertyFlags) + -> Result<(vk::Image, vk::DeviceMemory)> +{ + let physical_device = device.physical_device(); + + let image_info = vk::ImageCreateInfo::builder() + .image_type(vk::ImageType::_2D) + .extent(vk::Extent3D { width, height, depth: 1 }) + .mip_levels(mip_count) + .samples(sample_count) + .array_layers(1) + .format(format) + .tiling(tiling) + .initial_layout(vk::ImageLayout::UNDEFINED) + .usage(usage) + .sharing_mode(vk::SharingMode::EXCLUSIVE) + .flags(vk::ImageCreateFlags::empty()); + let image = unsafe { device.create_image(&image_info, None) }?; + + let requirements = unsafe { device.get_image_memory_requirements(image) }; + + let type_index = pick_memory_type(instance, &physical_device, + &memory_flags, &requirements)?; + + let image_memory_info = vk::MemoryAllocateInfo::builder() + .allocation_size(requirements.size) + .memory_type_index(type_index); + let image_memory = unsafe { + device.allocate_memory(&image_memory_info, None) + }?; + + unsafe { device.bind_image_memory(image, image_memory, 0) }?; + + Ok((image, image_memory)) +} + + + +#[allow(unsafe_code)] +pub fn init_image_view(device: &Device, image: &vk::Image, mip_count: u32, + format: vk::Format, aspects: vk::ImageAspectFlags) + -> Result +{ + // Component mapping is only for color components (not, for example, depth + // or stencil components), so we always just want it like this. + let components = vk::ComponentMapping::builder() + .r(vk::ComponentSwizzle::IDENTITY) + .g(vk::ComponentSwizzle::IDENTITY) + .b(vk::ComponentSwizzle::IDENTITY) + .a(vk::ComponentSwizzle::IDENTITY); + + let subresource_range = vk::ImageSubresourceRange::builder() + .aspect_mask(aspects) + .base_mip_level(0) + .level_count(mip_count) + .base_array_layer(0) + .layer_count(1); + + let view_info = vk::ImageViewCreateInfo::builder() + .image(*image) + .view_type(vk::ImageViewType::_2D) + .format(format) + .components(components) + .subresource_range(subresource_range); + + let view = unsafe { + device.create_image_view(&view_info, None) + }?; + + Ok(view) +} + + #[allow(unsafe_code)] pub fn begin_transient_commands(device: &Device, command_pool: &vk::CommandPool) -- cgit 1.4.1