librashader/librashader-runtime-vk/src/framebuffer.rs
2023-01-11 17:53:43 -05:00

110 lines
No EOL
4.3 KiB
Rust

use crate::{error, util};
use crate::filter_chain::Vulkan;
use crate::renderpass::VulkanRenderPass;
use crate::texture::{OwnedTexture, VulkanImage};
use ash::vk;
use ash::vk::{ImageAspectFlags, ImageViewType};
use librashader_common::Size;
#[derive(Clone)]
pub(crate) struct OutputFramebuffer {
pub size: Size<u32>,
pub image_view: vk::ImageView,
device: ash::Device,
image: vk::Image,
}
impl OutputFramebuffer {
pub fn new(vulkan: &Vulkan,
image: VulkanImage) -> error::Result<OutputFramebuffer> {
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)
.format(image.format)
.image(image.image.clone())
.subresource_range(image_subresource)
.components(swizzle_components)
.build();
let image_view = unsafe { vulkan.device.create_image_view(
&view_info, None)? };
Ok(OutputFramebuffer {
device: vulkan.device.clone(),
size: image.size,
image: image.image,
image_view,
})
}
pub fn begin_pass(&self, cmd: vk::CommandBuffer) {
unsafe {
util::vulkan_image_layout_transition_levels(&self.device, cmd, self.image,
1,
vk::ImageLayout::UNDEFINED,
vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL,
vk::AccessFlags::empty(),
vk::AccessFlags::COLOR_ATTACHMENT_READ | vk::AccessFlags::COLOR_ATTACHMENT_WRITE,
vk::PipelineStageFlags::ALL_GRAPHICS,
vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT,
vk::QUEUE_FAMILY_IGNORED,
vk::QUEUE_FAMILY_IGNORED)
}
}
pub fn end_pass(&self, cmd: vk::CommandBuffer) {
// todo: generate mips
unsafe {
util::vulkan_image_layout_transition_levels(&self.device, cmd, self.image,
vk::REMAINING_MIP_LEVELS,
vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL,
vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL,
vk::AccessFlags::COLOR_ATTACHMENT_WRITE,
vk::AccessFlags::SHADER_READ,
vk::PipelineStageFlags::ALL_GRAPHICS,
vk::PipelineStageFlags::FRAGMENT_SHADER,
vk::QUEUE_FAMILY_IGNORED,
vk::QUEUE_FAMILY_IGNORED)
}
}
// pub fn get_renderpass_begin_info(&self, area: vk::Rect2D, clear: Option<&[vk::ClearValue]>) -> vk::RenderPassBeginInfo {
// let mut builder = vk::RenderPassBeginInfo::builder()
// .render_pass(self.render_pass.handle)
// .framebuffer(self.handle)
// .render_area(area);
//
// if let Some(clear) = clear {
// builder = builder.clear_values(clear)
// }
//
// builder.build()
// }
}
impl Drop for OutputFramebuffer {
fn drop(&mut self) {
unsafe {
// if self.framebuffer != vk::Framebuffer::null() {
// self.device.destroy_framebuffer(self.framebuffer, None);
// }
// if self.image_view != vk::ImageView::null() {
// self.device.destroy_image_view(self.image_view, None);
// }
}
}
}