Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct
- {
- vec2s pos;
- float height;
- float angle;
- float tilt;
- float vel;
- float opacity;
- float temp;
- } galaxy_particle;
- typedef struct
- {
- VkBuffer buffer;
- VkDeviceMemory memory;
- } vk_buffer;
- typedef struct
- {
- vk_buffer b;
- void *mapped;
- } vk_uniform;
- static VkBuffer vk_buffer_create(vk_device *d, VkBufferUsageFlags usage, size_t size)
- {
- VkBuffer buffer = {0};
- uint32_t queue_families[2] = {d->indices.graphics.index, d->indices.transfer.index};
- uint32_t queue_family_count = 2;
- bool separate_queues = (d->indices.graphics.index != d->indices.transfer.index) ? 1 : 0;
- VkBufferCreateInfo buffer_info = vk_buffer_info_init(separate_queues, queue_families, queue_family_count, usage, size);
- if (vkCreateBuffer(d->device, &buffer_info, NULL, &buffer) != VK_SUCCESS)
- {
- printf("Failed to create buffer\n");
- }
- return buffer;
- }
- static VkDeviceMemory vk_buffer_memory_init(vk_device *d, VkBuffer buffer, VkMemoryPropertyFlags properties)
- {
- VkDeviceMemory memory = {0};
- VkMemoryRequirements mem_requirements = {0};
- vkGetBufferMemoryRequirements(d->device, buffer, &mem_requirements);
- VkMemoryAllocateInfo alloc_info = vk_memory_alloc_info_init(d, mem_requirements, properties);
- if (vkAllocateMemory(d->device, &alloc_info, NULL, &memory) != VK_SUCCESS)
- {
- printf("Failed to allocate vertex buffer memory\n");
- }
- if (vkBindBufferMemory(d->device, buffer, memory, 0) != VK_SUCCESS)
- {
- printf("Failed to bind buffer memory\n");
- }
- return memory;
- }
- static vk_buffer vk_buffer_init(vk_device *d, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, size_t size)
- {
- vk_buffer b =
- {
- .buffer = vk_buffer_create(d, usage, size),
- .memory = vk_buffer_memory_init(d, b.buffer, properties)
- };
- return b;
- }
- vk_uniform vk_uniform_buffer_init(vk_device *d, size_t ub_size)
- {
- vk_uniform ub = {0};
- 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);
- vkMapMemory(d->device, ub.b.memory, 0, ub_size, 0, &ub.mapped);
- return ub;
- }
- vk_buffer vk_storage_buffer_init(vk_device *d)
- {
- 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);
- return sb;
- }
- typedef struct
- {
- VkDescriptorSetLayout layout;
- VkDescriptorPool pool;
- VkDescriptorSet sets[MAX_FRAMES_IN_FLIGHT]; // MAX_FRAMES_IN_FLIGHT is 3
- vk_buffer galaxy_particles[MAX_FRAMES_IN_FLIGHT], compute_particles[MAX_FRAMES_IN_FLIGHT];
- vk_uniform galaxy_camera[MAX_FRAMES_IN_FLIGHT];
- } vk_desc_set;
- void vk_desc_set_galaxy_update(vk_uniform *ub, camera *c, VkExtent2D img_extent) // Camera code not important
- {
- c->view = camera_view_mat_init(c);
- c->camera = camera_buffer_init(c, img_extent);
- memcpy(ub->mapped, &c->view, sizeof(c->view));
- memcpy(ub->mapped, &c->camera, sizeof(c->camera));
- }
- static VkDescriptorSetAllocateInfo vk_desc_set_alloc_info_init(VkDescriptorPool pool, VkDescriptorSetLayout *layouts)
- {
- VkDescriptorSetAllocateInfo alloc_info =
- {
- .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
- .pNext = NULL,
- .descriptorPool = pool,
- .descriptorSetCount = MAX_FRAMES_IN_FLIGHT,
- .pSetLayouts = layouts
- };
- return alloc_info;
- }
- static VkDescriptorBufferInfo vk_desc_set_buffer_info_init(VkBuffer buffer, uint32_t offset, size_t size)
- {
- VkDescriptorBufferInfo buffer_info =
- {
- .buffer = buffer,
- .offset = offset,
- .range = size
- };
- return buffer_info;
- }
- static VkWriteDescriptorSet vk_desc_set_write_info_init(VkDescriptorSet set, uint32_t binding, VkDescriptorType type, VkDescriptorBufferInfo *buffer_info)
- {
- VkWriteDescriptorSet write_info =
- {
- .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
- .pNext = NULL,
- .dstSet = set,
- .dstBinding = binding,
- .dstArrayElement = 0,
- .descriptorCount = 1,
- .descriptorType = type,
- .pImageInfo = NULL,
- .pBufferInfo = buffer_info,
- .pTexelBufferView = NULL
- };
- return write_info;
- }
- void vk_desc_galaxy_sets_init(vk_device *d, vk_desc_set *ds)
- {
- VkDescriptorSetLayout layouts[MAX_FRAMES_IN_FLIGHT] = {0};
- for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
- {
- layouts[i] = ds->layout;
- ds->galaxy_camera[i] = vk_uniform_buffer_init(d, sizeof(mat4s) * 2);
- ds->galaxy_particles[i] = vk_storage_buffer_init(d);
- }
- VkDescriptorSetAllocateInfo alloc_info = vk_desc_set_alloc_info_init(ds->pool, layouts);
- if (vkAllocateDescriptorSets(d->device, &alloc_info, ds->sets) != VK_SUCCESS)
- {
- printf("Failed to allocate galaxy descriptor sets\n");
- }
- for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
- {
- VkDescriptorBufferInfo uniform_info = vk_desc_set_buffer_info_init(ds->galaxy_camera[i].b.buffer, 0, sizeof(mat4s) * 2);
- VkWriteDescriptorSet uniform_write_info = vk_desc_set_write_info_init(ds->sets[i], 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &uniform_info);
- VkDescriptorBufferInfo storage_info = vk_desc_set_buffer_info_init(ds->galaxy_particles[i].buffer, 0, VK_WHOLE_SIZE);
- VkWriteDescriptorSet storage_write_info = vk_desc_set_write_info_init(ds->sets[i], 1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, &storage_info);
- VkWriteDescriptorSet desc_writes[2] = {uniform_write_info, storage_write_info};
- vkUpdateDescriptorSets(d->device, 2, desc_writes, 0, NULL);
- }
- }
- void vk_desc_compute_sets_init(vk_device *d, vk_desc_set *ds)
- {
- VkDescriptorSetLayout layouts[MAX_FRAMES_IN_FLIGHT] = {0};
- for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
- {
- layouts[i] = ds->layout;
- ds->compute_particles[i] = vk_storage_buffer_init(d);
- }
- VkDescriptorSetAllocateInfo alloc_info = vk_desc_set_alloc_info_init(ds->pool, layouts);
- if (vkAllocateDescriptorSets(d->device, &alloc_info, ds->sets) != VK_SUCCESS)
- {
- printf("Failed to allocate compute descriptor sets\n");
- }
- for (uint32_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
- {
- VkDescriptorBufferInfo storage_info = vk_desc_set_buffer_info_init(ds->compute_particles[i].buffer, 0, VK_WHOLE_SIZE);
- VkWriteDescriptorSet storage_write_info = vk_desc_set_write_info_init(ds->sets[i], 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, &storage_info);
- vkUpdateDescriptorSets(d->device, 1, &storage_write_info, 0, NULL);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement