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.

116 lines
2.5KB

  1. use crate::render::{Buffer, DescriptorSetLayout, Device, Uniforms};
  2. use ash::{version::DeviceV1_0, vk};
  3. use std::rc::Rc;
  4. pub struct DescriptorSet {
  5. handle: vk::DescriptorSet,
  6. }
  7. pub struct DescriptorPool {
  8. handle: vk::DescriptorPool,
  9. pub device: Rc<Device>,
  10. pub buffers: Vec<Rc<Buffer>>,
  11. pub sets: Vec<DescriptorSet>,
  12. }
  13. impl DescriptorPool {
  14. pub fn create(
  15. device: Rc<Device>,
  16. buffers: Vec<Rc<Buffer>>,
  17. layout: Rc<DescriptorSetLayout>,
  18. ) -> Result<Rc<Self>, vk::Result> {
  19. let size_info = [
  20. vk::DescriptorPoolSize {
  21. ty: vk::DescriptorType::UNIFORM_BUFFER,
  22. descriptor_count: buffers.len() as u32,
  23. ..Default::default()
  24. }
  25. ];
  26. let create_info = vk::DescriptorPoolCreateInfo {
  27. pool_size_count: size_info.len() as u32,
  28. p_pool_sizes: size_info.as_ptr(),
  29. max_sets: buffers.len() as u32,
  30. ..Default::default()
  31. };
  32. let handle =
  33. unsafe { device.create_descriptor_pool(&create_info, None)? };
  34. let set_layouts = vec![**layout; buffers.len()];
  35. let alloc_info = vk::DescriptorSetAllocateInfo {
  36. descriptor_pool: handle,
  37. descriptor_set_count: set_layouts.len() as u32,
  38. p_set_layouts: set_layouts.as_ptr(),
  39. ..Default::default()
  40. };
  41. let sets = unsafe {
  42. device
  43. .allocate_descriptor_sets(&alloc_info)?
  44. .iter()
  45. .map(|&handle| DescriptorSet { handle })
  46. .collect::<Vec<_>>()
  47. };
  48. let buffer_infos =
  49. buffers
  50. .iter()
  51. .map(|buffer| {
  52. vk::DescriptorBufferInfo {
  53. buffer: ***buffer,
  54. range: std::mem::size_of::<Uniforms>() as vk::DeviceSize,
  55. ..Default::default()
  56. }
  57. })
  58. .collect::<Vec<_>>();
  59. let write_descriptors =
  60. sets
  61. .iter()
  62. .zip(&buffer_infos)
  63. .map(|(dst_set, buffer_info)| {
  64. vk::WriteDescriptorSet {
  65. dst_set: **dst_set,
  66. descriptor_type: vk::DescriptorType::UNIFORM_BUFFER,
  67. descriptor_count: 1,
  68. p_buffer_info: &*buffer_info,
  69. ..Default::default()
  70. }
  71. })
  72. .collect::<Vec<_>>();
  73. unsafe {
  74. device.update_descriptor_sets(&write_descriptors, &[]);
  75. }
  76. Ok(Rc::new(Self { handle, device, buffers, sets }))
  77. }
  78. }
  79. impl Drop for DescriptorPool {
  80. fn drop(&mut self) {
  81. unsafe {
  82. self.device.destroy_descriptor_pool(self.handle, None);
  83. }
  84. }
  85. }
  86. impl std::ops::Deref for DescriptorSet {
  87. type Target = vk::DescriptorSet;
  88. fn deref(&self) -> &Self::Target {
  89. &self.handle
  90. }
  91. }
  92. impl std::ops::Deref for DescriptorPool {
  93. type Target = vk::DescriptorPool;
  94. fn deref(&self) -> &Self::Target {
  95. &self.handle
  96. }
  97. }
  98. // EOF