Advertisement
Chillzy

Descriptor Sets

Nov 19th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.79 KB | Source Code | 0 0
  1. typedef struct
  2. {
  3.     vec2s pos;
  4.     float height;
  5.     float angle;
  6.     float tilt;
  7.     float vel;
  8.     float opacity;
  9.     float temp;
  10. } galaxy_particle;
  11.  
  12. typedef struct
  13. {
  14.     VkBuffer buffer;
  15.     VkDeviceMemory memory;
  16. }   vk_buffer;
  17.  
  18. typedef struct
  19. {
  20.     vk_buffer b;
  21.     void *mapped;
  22. }   vk_uniform;
  23.  
  24. static VkBuffer vk_buffer_create(vk_device *d, VkBufferUsageFlags usage, size_t size)
  25. {
  26.     VkBuffer buffer = {0};
  27.  
  28.     uint32_t queue_families[2] = {d->indices.graphics.index, d->indices.transfer.index};
  29.     uint32_t queue_family_count = 2;
  30.     bool separate_queues = (d->indices.graphics.index != d->indices.transfer.index) ? 1 : 0;
  31.  
  32.     VkBufferCreateInfo buffer_info = vk_buffer_info_init(separate_queues, queue_families, queue_family_count, usage, size);
  33.  
  34.     if (vkCreateBuffer(d->device, &buffer_info, NULL, &buffer) != VK_SUCCESS)
  35.     {
  36.         printf("Failed to create buffer\n");
  37.     }
  38.  
  39.     return buffer;
  40. }
  41.  
  42. static VkDeviceMemory vk_buffer_memory_init(vk_device *d, VkBuffer buffer, VkMemoryPropertyFlags properties)
  43. {
  44.     VkDeviceMemory memory = {0};
  45.  
  46.     VkMemoryRequirements mem_requirements = {0};
  47.     vkGetBufferMemoryRequirements(d->device, buffer, &mem_requirements);
  48.  
  49.     VkMemoryAllocateInfo alloc_info = vk_memory_alloc_info_init(d, mem_requirements, properties);
  50.  
  51.     if (vkAllocateMemory(d->device, &alloc_info, NULL, &memory) != VK_SUCCESS)
  52.     {
  53.         printf("Failed to allocate vertex buffer memory\n");
  54.     }
  55.  
  56.     if (vkBindBufferMemory(d->device, buffer, memory, 0) != VK_SUCCESS)
  57.     {
  58.         printf("Failed to bind buffer memory\n");
  59.     }
  60.  
  61.     return memory;
  62. }
  63.  
  64. static vk_buffer vk_buffer_init(vk_device *d, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, size_t size)
  65. {
  66.     vk_buffer b =
  67.     {
  68.         .buffer = vk_buffer_create(d, usage, size),
  69.         .memory = vk_buffer_memory_init(d, b.buffer, properties)
  70.     };
  71.  
  72.     return b;
  73. }
  74.  
  75. vk_uniform vk_uniform_buffer_init(vk_device *d, size_t ub_size)
  76. {
  77.     vk_uniform ub = {0};
  78.     ub.b = vk_buffer_init(d, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, ub_size);
  79.  
  80.     vkMapMemory(d->device, ub.b.memory, 0, ub_size, 0, &ub.mapped);
  81.  
  82.     return ub;
  83. }
  84.  
  85. vk_buffer vk_storage_buffer_init(vk_device *d)
  86. {
  87.     vk_buffer sb = vk_buffer_init(d, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, sizeof(galaxy_particle) * PARTICLE_COUNT);
  88.  
  89.     return sb;
  90. }
  91.  
  92. typedef struct
  93. {
  94.     VkDescriptorSetLayout layout;
  95.     VkDescriptorPool pool;
  96.     VkDescriptorSet sets[MAX_FRAMES_IN_FLIGHT]; // MAX_FRAMES_IN_FLIGHT is 3
  97.     vk_buffer galaxy_particles[MAX_FRAMES_IN_FLIGHT], compute_particles[MAX_FRAMES_IN_FLIGHT];
  98.     vk_uniform galaxy_camera[MAX_FRAMES_IN_FLIGHT];
  99. }   vk_desc_set;
  100.  
  101. void vk_desc_set_galaxy_update(vk_uniform *ub, camera *c, VkExtent2D img_extent)    // Camera code not important
  102. {
  103.     c->view = camera_view_mat_init(c);
  104.     c->camera = camera_buffer_init(c, img_extent);
  105.     memcpy(ub->mapped, &c->view, sizeof(c->view));
  106.     memcpy(ub->mapped, &c->camera, sizeof(c->camera));
  107. }
  108.  
  109. static VkDescriptorSetAllocateInfo vk_desc_set_alloc_info_init(VkDescriptorPool pool, VkDescriptorSetLayout *layouts)
  110. {
  111.     VkDescriptorSetAllocateInfo alloc_info =
  112.     {
  113.         .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
  114.         .pNext = NULL,
  115.         .descriptorPool = pool,
  116.         .descriptorSetCount = MAX_FRAMES_IN_FLIGHT,
  117.         .pSetLayouts = layouts
  118.     };
  119.  
  120.     return alloc_info;
  121. }
  122.  
  123. static VkDescriptorBufferInfo vk_desc_set_buffer_info_init(VkBuffer buffer, uint32_t offset, size_t size)
  124. {
  125.     VkDescriptorBufferInfo buffer_info =
  126.     {
  127.         .buffer = buffer,
  128.         .offset = offset,
  129.         .range = size
  130.     };
  131.  
  132.     return buffer_info;
  133. }
  134.  
  135. static VkWriteDescriptorSet vk_desc_set_write_info_init(VkDescriptorSet set, uint32_t binding, VkDescriptorType type, VkDescriptorBufferInfo *buffer_info)
  136. {
  137.     VkWriteDescriptorSet write_info =
  138.     {
  139.         .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
  140.         .pNext = NULL,
  141.         .dstSet = set,
  142.         .dstBinding = binding,
  143.         .dstArrayElement = 0,
  144.         .descriptorCount = 1,
  145.         .descriptorType = type,
  146.         .pImageInfo = NULL,
  147.         .pBufferInfo = buffer_info,
  148.         .pTexelBufferView = NULL
  149.     };
  150.  
  151.     return write_info;
  152. }
  153.  
  154. void vk_desc_galaxy_sets_init(vk_device *d, vk_desc_set *ds)
  155. {
  156.     VkDescriptorSetLayout layouts[MAX_FRAMES_IN_FLIGHT] = {0};
  157.  
  158.     for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
  159.     {
  160.         layouts[i] = ds->layout;
  161.         ds->galaxy_camera[i] = vk_uniform_buffer_init(d, sizeof(mat4s) * 2);
  162.         ds->galaxy_particles[i] = vk_storage_buffer_init(d);
  163.     }
  164.  
  165.     VkDescriptorSetAllocateInfo alloc_info = vk_desc_set_alloc_info_init(ds->pool, layouts);
  166.  
  167.     if (vkAllocateDescriptorSets(d->device, &alloc_info, ds->sets) != VK_SUCCESS)
  168.     {
  169.         printf("Failed to allocate galaxy descriptor sets\n");
  170.     }
  171.  
  172.     for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
  173.     {
  174.         VkDescriptorBufferInfo uniform_info = vk_desc_set_buffer_info_init(ds->galaxy_camera[i].b.buffer, 0, sizeof(mat4s) * 2);
  175.         VkWriteDescriptorSet uniform_write_info = vk_desc_set_write_info_init(ds->sets[i], 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &uniform_info);
  176.  
  177.         VkDescriptorBufferInfo storage_info = vk_desc_set_buffer_info_init(ds->galaxy_particles[i].buffer, 0, VK_WHOLE_SIZE);
  178.         VkWriteDescriptorSet storage_write_info = vk_desc_set_write_info_init(ds->sets[i], 1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, &storage_info);
  179.  
  180.         VkWriteDescriptorSet desc_writes[2] = {uniform_write_info, storage_write_info};
  181.  
  182.         vkUpdateDescriptorSets(d->device, 2, desc_writes, 0, NULL);
  183.     }
  184. }
  185.  
  186. void vk_desc_compute_sets_init(vk_device *d, vk_desc_set *ds)
  187. {
  188.     VkDescriptorSetLayout layouts[MAX_FRAMES_IN_FLIGHT] = {0};
  189.  
  190.     for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
  191.     {
  192.         layouts[i] = ds->layout;
  193.         ds->compute_particles[i] = vk_storage_buffer_init(d);
  194.     }
  195.  
  196.     VkDescriptorSetAllocateInfo alloc_info = vk_desc_set_alloc_info_init(ds->pool, layouts);
  197.  
  198.     if (vkAllocateDescriptorSets(d->device, &alloc_info, ds->sets) != VK_SUCCESS)
  199.     {
  200.         printf("Failed to allocate compute descriptor sets\n");
  201.     }
  202.  
  203.     for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
  204.     {
  205.         VkDescriptorBufferInfo storage_info = vk_desc_set_buffer_info_init(ds->compute_particles[i].buffer, 0, VK_WHOLE_SIZE);
  206.         VkWriteDescriptorSet storage_write_info = vk_desc_set_write_info_init(ds->sets[i], 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, &storage_info);
  207.  
  208.         vkUpdateDescriptorSets(d->device, 1, &storage_write_info, 0, NULL);
  209.     }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement