2022-12-22 17:30:14 +11:00
|
|
|
use crate::error;
|
|
|
|
use crate::filter_chain::Vulkan;
|
|
|
|
use crate::util::find_vulkan_memory_type;
|
|
|
|
use crate::vulkan_primitives::VulkanImageMemory;
|
2023-01-10 14:54:54 +11:00
|
|
|
use ash::vk;
|
|
|
|
use ash::vk::{
|
|
|
|
ImageAspectFlags, ImageLayout, ImageTiling, ImageType, ImageUsageFlags, ImageViewType,
|
|
|
|
SampleCountFlags, SharingMode,
|
|
|
|
};
|
|
|
|
use librashader_common::{FilterMode, ImageFormat, Size, WrapMode};
|
2023-01-10 16:11:05 +11:00
|
|
|
use librashader_presets::Scale2D;
|
|
|
|
use librashader_runtime::scaling::{MipmapSize, ViewportSize};
|
2022-12-22 17:30:14 +11:00
|
|
|
|
|
|
|
pub struct OwnedTexture {
|
|
|
|
pub device: ash::Device,
|
2023-01-10 16:11:05 +11:00
|
|
|
pub mem_props: vk::PhysicalDeviceMemoryProperties,
|
2022-12-22 17:30:14 +11:00
|
|
|
pub image_view: vk::ImageView,
|
2022-12-25 17:18:11 +11:00
|
|
|
pub image: VulkanImage,
|
2022-12-22 17:30:14 +11:00
|
|
|
pub memory: VulkanImageMemory,
|
|
|
|
pub max_miplevels: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OwnedTexture {
|
2023-01-10 16:11:05 +11:00
|
|
|
fn new_internal(
|
|
|
|
device: ash::Device,
|
|
|
|
mem_props: vk::PhysicalDeviceMemoryProperties,
|
2023-01-10 14:54:54 +11:00
|
|
|
size: Size<u32>,
|
|
|
|
format: ImageFormat,
|
|
|
|
max_miplevels: u32,
|
|
|
|
) -> error::Result<OwnedTexture> {
|
2022-12-22 17:30:14 +11:00
|
|
|
let image_create_info = vk::ImageCreateInfo::builder()
|
|
|
|
.image_type(ImageType::TYPE_2D)
|
|
|
|
.format(format.into())
|
|
|
|
.extent(size.into())
|
2023-01-10 14:54:54 +11:00
|
|
|
.mip_levels(std::cmp::min(max_miplevels, size.calculate_miplevels()))
|
2022-12-22 17:30:14 +11:00
|
|
|
.array_layers(1)
|
|
|
|
.samples(SampleCountFlags::TYPE_1)
|
|
|
|
.tiling(ImageTiling::OPTIMAL)
|
|
|
|
.usage(
|
|
|
|
ImageUsageFlags::SAMPLED
|
|
|
|
| ImageUsageFlags::COLOR_ATTACHMENT
|
|
|
|
| ImageUsageFlags::TRANSFER_DST
|
|
|
|
| ImageUsageFlags::TRANSFER_SRC,
|
|
|
|
)
|
|
|
|
.sharing_mode(SharingMode::EXCLUSIVE)
|
|
|
|
.initial_layout(ImageLayout::UNDEFINED)
|
|
|
|
.build();
|
|
|
|
|
2023-01-10 16:11:05 +11:00
|
|
|
let image = unsafe { device.create_image(&image_create_info, None)? };
|
|
|
|
let mem_reqs = unsafe { device.get_image_memory_requirements(image.clone()) };
|
2022-12-22 17:30:14 +11:00
|
|
|
|
|
|
|
let alloc_info = vk::MemoryAllocateInfo::builder()
|
|
|
|
.allocation_size(mem_reqs.size)
|
|
|
|
.memory_type_index(find_vulkan_memory_type(
|
2023-01-10 16:11:05 +11:00
|
|
|
&mem_props,
|
2022-12-22 17:30:14 +11:00
|
|
|
mem_reqs.memory_type_bits,
|
|
|
|
vk::MemoryPropertyFlags::DEVICE_LOCAL,
|
|
|
|
))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
// todo: optimize by reusing existing memory.
|
2023-01-10 16:11:05 +11:00
|
|
|
let memory = VulkanImageMemory::new(&device, &alloc_info)?;
|
2022-12-22 17:30:14 +11:00
|
|
|
memory.bind(&image)?;
|
|
|
|
|
|
|
|
let image_subresource = vk::ImageSubresourceRange::builder()
|
|
|
|
.base_mip_level(0)
|
|
|
|
.base_array_layer(0)
|
|
|
|
.level_count(image_create_info.mip_levels)
|
|
|
|
.layer_count(1)
|
|
|
|
.aspect_mask(ImageAspectFlags::COLOR)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let swizzle_components = vk::ComponentMapping::builder()
|
|
|
|
.r(vk::ComponentSwizzle::R)
|
|
|
|
.g(vk::ComponentSwizzle::G)
|
|
|
|
.b(vk::ComponentSwizzle::B)
|
|
|
|
.a(vk::ComponentSwizzle::A)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let mut view_info = vk::ImageViewCreateInfo::builder()
|
|
|
|
.view_type(ImageViewType::TYPE_2D)
|
|
|
|
.format(format.into())
|
|
|
|
.image(image.clone())
|
|
|
|
.subresource_range(image_subresource)
|
|
|
|
.components(swizzle_components)
|
|
|
|
.build();
|
|
|
|
|
2023-01-10 16:11:05 +11:00
|
|
|
let image_view = unsafe { device.create_image_view(&view_info, None)? };
|
2022-12-22 17:30:14 +11:00
|
|
|
|
|
|
|
Ok(OwnedTexture {
|
2023-01-10 16:11:05 +11:00
|
|
|
device,
|
|
|
|
mem_props,
|
2022-12-22 17:30:14 +11:00
|
|
|
image_view,
|
2022-12-25 17:18:11 +11:00
|
|
|
image: VulkanImage {
|
|
|
|
image,
|
|
|
|
size,
|
2023-01-10 14:54:54 +11:00
|
|
|
format: format.into(),
|
2022-12-25 17:18:11 +11:00
|
|
|
},
|
2022-12-22 17:30:14 +11:00
|
|
|
memory,
|
|
|
|
max_miplevels,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-10 16:11:05 +11:00
|
|
|
pub fn new(
|
|
|
|
vulkan: &Vulkan,
|
|
|
|
size: Size<u32>,
|
|
|
|
format: ImageFormat,
|
|
|
|
max_miplevels: u32,
|
|
|
|
) -> error::Result<OwnedTexture> {
|
|
|
|
Self::new_internal(vulkan.device.clone(), vulkan.memory_properties, size, format, max_miplevels)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn scale(
|
|
|
|
&mut self,
|
|
|
|
scaling: Scale2D,
|
|
|
|
format: ImageFormat,
|
|
|
|
viewport_size: &Size<u32>,
|
|
|
|
_original: &Texture,
|
|
|
|
source: &Texture,
|
|
|
|
) -> error::Result<Size<u32>> {
|
|
|
|
|
|
|
|
|
|
|
|
let size = source.image.size.scale_viewport(scaling, *viewport_size);
|
|
|
|
|
|
|
|
if self.image.size != size {
|
|
|
|
let mut new = OwnedTexture::new_internal(self.device.clone(), self.mem_props, size, if format == ImageFormat::Unknown {
|
|
|
|
ImageFormat::R8G8B8A8Unorm
|
|
|
|
} else {
|
|
|
|
format
|
|
|
|
}, self.max_miplevels)?;
|
|
|
|
|
|
|
|
std::mem::swap(self, &mut new)
|
|
|
|
}
|
|
|
|
Ok(size)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-22 17:30:14 +11:00
|
|
|
pub fn create_texture_view(&self) -> error::Result<vk::ImageView> {
|
|
|
|
let image_subresource = vk::ImageSubresourceRange::builder()
|
|
|
|
.base_mip_level(0)
|
|
|
|
.base_array_layer(0)
|
|
|
|
.level_count(1)
|
|
|
|
.layer_count(1)
|
|
|
|
.aspect_mask(ImageAspectFlags::COLOR)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let swizzle_components = vk::ComponentMapping::builder()
|
|
|
|
.r(vk::ComponentSwizzle::R)
|
|
|
|
.g(vk::ComponentSwizzle::G)
|
|
|
|
.b(vk::ComponentSwizzle::B)
|
|
|
|
.a(vk::ComponentSwizzle::A)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let mut view_info = vk::ImageViewCreateInfo::builder()
|
|
|
|
.view_type(ImageViewType::TYPE_2D)
|
2022-12-29 16:50:48 +11:00
|
|
|
.format(self.image.format)
|
|
|
|
.image(self.image.image.clone())
|
2022-12-22 17:30:14 +11:00
|
|
|
.subresource_range(image_subresource)
|
|
|
|
.components(swizzle_components)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let image_view = unsafe { self.device.create_image_view(&view_info, None)? };
|
|
|
|
Ok(image_view)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for OwnedTexture {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
if self.image_view != vk::ImageView::null() {
|
|
|
|
self.device.destroy_image_view(self.image_view, None);
|
|
|
|
}
|
2022-12-29 16:50:48 +11:00
|
|
|
if self.image.image != vk::Image::null() {
|
2022-12-25 17:18:11 +11:00
|
|
|
self.device.destroy_image(self.image.image, None);
|
2022-12-22 17:30:14 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 16:11:05 +11:00
|
|
|
#[derive(Clone)]
|
2022-12-25 17:18:11 +11:00
|
|
|
pub struct VulkanImage {
|
2022-12-22 17:30:14 +11:00
|
|
|
pub size: Size<u32>,
|
|
|
|
pub image: vk::Image,
|
|
|
|
pub format: vk::Format,
|
2022-12-25 17:18:11 +11:00
|
|
|
}
|
|
|
|
|
2023-01-10 16:11:05 +11:00
|
|
|
#[derive(Clone)]
|
2022-12-25 17:18:11 +11:00
|
|
|
pub struct Texture {
|
|
|
|
pub image: VulkanImage,
|
|
|
|
pub image_view: vk::ImageView,
|
2022-12-22 17:30:14 +11:00
|
|
|
pub wrap_mode: WrapMode,
|
|
|
|
pub filter_mode: FilterMode,
|
|
|
|
pub mip_filter: FilterMode,
|
|
|
|
}
|