You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.3KB

  1. use crate::render::Device;
  2. use ash::{version::DeviceV1_0, vk};
  3. use std::rc::Rc;
  4. pub struct RenderPass {
  5. handle: vk::RenderPass,
  6. pub device: Rc<Device>,
  7. }
  8. impl RenderPass {
  9. pub fn create(
  10. device: Rc<Device>,
  11. format: vk::Format,
  12. ) -> Result<Rc<Self>, vk::Result> {
  13. let attachments = [
  14. vk::AttachmentDescription {
  15. format,
  16. flags: vk::AttachmentDescriptionFlags::empty(),
  17. samples: vk::SampleCountFlags::TYPE_1,
  18. load_op: vk::AttachmentLoadOp::CLEAR,
  19. store_op: vk::AttachmentStoreOp::STORE,
  20. stencil_load_op: vk::AttachmentLoadOp::DONT_CARE,
  21. stencil_store_op: vk::AttachmentStoreOp::DONT_CARE,
  22. initial_layout: vk::ImageLayout::UNDEFINED,
  23. final_layout: vk::ImageLayout::PRESENT_SRC_KHR,
  24. }
  25. ];
  26. let attachment_references = [
  27. vk::AttachmentReference {
  28. attachment: 0,
  29. layout: vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL,
  30. }
  31. ];
  32. let subpasses = [
  33. vk::SubpassDescription {
  34. pipeline_bind_point: vk::PipelineBindPoint::GRAPHICS,
  35. color_attachment_count: attachment_references.len() as u32,
  36. p_color_attachments: attachment_references.as_ptr(),
  37. ..Default::default()
  38. }
  39. ];
  40. let dependencies = [
  41. vk::SubpassDependency {
  42. src_subpass: vk::SUBPASS_EXTERNAL,
  43. dst_subpass: 0,
  44. src_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT,
  45. dst_stage_mask: vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT,
  46. src_access_mask: vk::AccessFlags::empty(),
  47. dst_access_mask:
  48. vk::AccessFlags::COLOR_ATTACHMENT_READ |
  49. vk::AccessFlags::COLOR_ATTACHMENT_WRITE,
  50. dependency_flags: vk::DependencyFlags::empty(),
  51. }
  52. ];
  53. let create_info = vk::RenderPassCreateInfo {
  54. attachment_count: attachments.len() as u32,
  55. p_attachments: attachments.as_ptr(),
  56. subpass_count: subpasses.len() as u32,
  57. p_subpasses: subpasses.as_ptr(),
  58. dependency_count: dependencies.len() as u32,
  59. p_dependencies: dependencies.as_ptr(),
  60. ..Default::default()
  61. };
  62. let handle = unsafe { device.create_render_pass(&create_info, None)? };
  63. Ok(Rc::new(Self { handle, device }))
  64. }
  65. }
  66. impl Drop for RenderPass {
  67. fn drop(&mut self) {
  68. unsafe {
  69. self.device.destroy_render_pass(self.handle, None);
  70. }
  71. }
  72. }
  73. impl std::ops::Deref for RenderPass {
  74. type Target = vk::RenderPass;
  75. fn deref(&self) -> &Self::Target {
  76. &self.handle
  77. }
  78. }
  79. // EOF