From 29095b589d3d848c6e626e869261cd855e40fc5a Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 14 Apr 2023 10:48:59 -0400 Subject: [PATCH] Create image view and sampler --- src/app/data.rs | 67 +++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/src/app/data.rs b/src/app/data.rs index ad936c2..17136ed 100644 --- a/src/app/data.rs +++ b/src/app/data.rs @@ -65,6 +65,8 @@ pub(crate) struct AppData { // Texture texture_image: vk::Image, texture_image_memory: vk::DeviceMemory, + texture_image_view: vk::ImageView, + texture_sampler: vk::Sampler, // Buffers vertex_buffer: vk::Buffer, vertex_buffer_memory: vk::DeviceMemory, @@ -108,6 +110,8 @@ impl AppData { self.create_framebuffers(&device)?; self.create_command_pool(&instance, &device)?; self.create_texture_image(&instance, &device)?; + self.create_texture_image_view(&device)?; + self.create_texture_sampler(&device)?; self.create_vertex_buffer(&instance, &device)?; self.create_index_buffer(&instance, &device)?; self.create_uniform_buffers(&instance, &device)?; @@ -230,6 +234,8 @@ impl AppData { /// Here be Dragons pub unsafe fn destroy(&mut self, instance: &Instance, device: &Device) { self.destroy_swapchain(device); + device.destroy_sampler(self.texture_sampler, None); + device.destroy_image_view(self.texture_image_view, None); device.destroy_image(self.texture_image, None); device.free_memory(self.texture_image_memory, None); device.destroy_descriptor_set_layout(self.descriptor_set_layout, None); @@ -295,6 +301,11 @@ impl AppData { return Err(anyhow!(SuitabilityError("Insufficient swapchain support."))); } + let features = instance.get_physical_device_features(physical_device); + if features.sampler_anisotropy != vk::TRUE { + return Err(anyhow!(SuitabilityError("No sampler anisotropy."))); + } + Ok(()) } @@ -343,7 +354,7 @@ impl AppData { } // Features - let features = vk::PhysicalDeviceFeatures::builder(); + let features = vk::PhysicalDeviceFeatures::builder().sampler_anisotropy(true); // Create let info = vk::DeviceCreateInfo::builder() @@ -450,29 +461,7 @@ impl AppData { self.swapchain_image_views = self .swapchain_images .iter() - .map(|i| { - 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(vk::ImageAspectFlags::COLOR) - .base_mip_level(0) - .level_count(1) - .base_array_layer(0) - .layer_count(1); - - let info = vk::ImageViewCreateInfo::builder() - .image(*i) - .view_type(vk::ImageViewType::_2D) - .format(self.swapchain_format) - .components(components) - .subresource_range(subresource_range); - - device.create_image_view(&info, None) - }) + .map(|i| create_image_view(device, *i, self.swapchain_format)) .collect::, _>>()?; Ok(()) @@ -756,6 +745,36 @@ impl AppData { Ok(()) } + unsafe fn create_texture_image_view(&mut self, device: &Device) -> Result<()> { + self.texture_image_view = + create_image_view(device, self.texture_image, vk::Format::R8G8B8A8_SRGB)?; + + Ok(()) + } + + unsafe fn create_texture_sampler(&mut self, device: &Device) -> Result<()> { + let info = vk::SamplerCreateInfo::builder() + .mag_filter(vk::Filter::LINEAR) + .min_filter(vk::Filter::LINEAR) + .address_mode_u(vk::SamplerAddressMode::REPEAT) + .address_mode_v(vk::SamplerAddressMode::REPEAT) + .address_mode_w(vk::SamplerAddressMode::REPEAT) + .anisotropy_enable(true) + .max_anisotropy(16.0) + .border_color(vk::BorderColor::INT_OPAQUE_BLACK) + .unnormalized_coordinates(false) + .compare_enable(false) + .compare_op(vk::CompareOp::ALWAYS) + .mipmap_mode(vk::SamplerMipmapMode::LINEAR) + .mip_lod_bias(0.0) + .min_lod(0.0) + .max_lod(0.0); + + self.texture_sampler = device.create_sampler(&info, None)?; + + Ok(()) + } + //================================================ // Buffers //================================================