The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

aux/vk: Add many more helper functions

+427
+2
src/xrt/auxiliary/CMakeLists.txt
··· 184 184 list(APPEND UTIL_SOURCE_FILES "${CMAKE_CURRENT_BINARY_DIR}/u_git_tag.c") 185 185 186 186 set(VK_SOURCE_FILES 187 + vk/vk_command_buffer.c 187 188 vk/vk_documentation.h 188 189 vk/vk_helpers.c 189 190 vk/vk_helpers.h 190 191 vk/vk_image_allocator.c 191 192 vk/vk_image_allocator.h 193 + vk/vk_state_creators.c 192 194 ) 193 195 194 196 # Common includes
+2
src/xrt/auxiliary/meson.build
··· 271 271 lib_aux_vk = static_library( 272 272 'aux_vk', 273 273 files( 274 + 'vk/vk_command_buffer.c', 274 275 'vk/vk_documentation.h', 275 276 'vk/vk_helpers.h', 276 277 'vk/vk_helpers.c', 277 278 'vk/vk_image_allocator.h', 278 279 'vk/vk_image_allocator.c', 280 + 'vk/vk_state_creators.c', 279 281 ), 280 282 include_directories: [ 281 283 xrt_include,
+96
src/xrt/auxiliary/vk/vk_command_buffer.c
··· 1 + // Copyright 2019-2021, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief Vulkan command buffer helpers. 6 + * 7 + * @author Jakob Bornecrantz <jakob@collabora.com> 8 + * @author Lubosz Sarnecki <lubosz.sarnecki@collabora.com> 9 + * @ingroup aux_vk 10 + */ 11 + 12 + #include "vk/vk_helpers.h" 13 + 14 + 15 + VkResult 16 + vk_create_command_buffer(struct vk_bundle *vk, VkCommandBuffer *out_command_buffer) 17 + { 18 + VkResult ret; 19 + 20 + VkCommandBufferAllocateInfo cmd_buffer_info = { 21 + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, 22 + .commandPool = vk->cmd_pool, 23 + .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, 24 + .commandBufferCount = 1, 25 + }; 26 + 27 + VkCommandBuffer cmd = VK_NULL_HANDLE; 28 + 29 + os_mutex_lock(&vk->cmd_pool_mutex); 30 + 31 + ret = vk->vkAllocateCommandBuffers( // 32 + vk->device, // device 33 + &cmd_buffer_info, // pAllocateInfo 34 + &cmd); // pCommandBuffers 35 + 36 + os_mutex_unlock(&vk->cmd_pool_mutex); 37 + 38 + if (ret != VK_SUCCESS) { 39 + VK_ERROR(vk, "vkCreateFramebuffer failed: %s", vk_result_string(ret)); 40 + return ret; 41 + } 42 + 43 + *out_command_buffer = cmd; 44 + 45 + return VK_SUCCESS; 46 + } 47 + 48 + void 49 + vk_destroy_command_buffer(struct vk_bundle *vk, VkCommandBuffer command_buffer) 50 + { 51 + os_mutex_lock(&vk->cmd_pool_mutex); 52 + 53 + vk->vkFreeCommandBuffers( // 54 + vk->device, // device 55 + vk->cmd_pool, // commandPool 56 + 1, // commandBufferCount 57 + &command_buffer); // pCommandBuffers 58 + 59 + os_mutex_unlock(&vk->cmd_pool_mutex); 60 + } 61 + 62 + VkResult 63 + vk_begin_command_buffer(struct vk_bundle *vk, VkCommandBuffer command_buffer) 64 + { 65 + VkResult ret; 66 + 67 + VkCommandBufferBeginInfo command_buffer_info = { 68 + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, 69 + }; 70 + 71 + ret = vk->vkBeginCommandBuffer( // 72 + command_buffer, // commandBuffer 73 + &command_buffer_info); // pBeginInfo 74 + if (ret != VK_SUCCESS) { 75 + VK_ERROR(vk, "vkBeginCommandBuffer failed: %s", vk_result_string(ret)); 76 + return ret; 77 + } 78 + 79 + return VK_SUCCESS; 80 + } 81 + 82 + VkResult 83 + vk_end_command_buffer(struct vk_bundle *vk, VkCommandBuffer command_buffer) 84 + { 85 + VkResult ret; 86 + 87 + // End the command buffer. 88 + ret = vk->vkEndCommandBuffer( // 89 + command_buffer); // commandBuffer 90 + if (ret != VK_SUCCESS) { 91 + VK_ERROR(vk, "vkEndCommandBuffer failed: %s", vk_result_string(ret)); 92 + return ret; 93 + } 94 + 95 + return VK_SUCCESS; 96 + }
+115
src/xrt/auxiliary/vk/vk_helpers.h
··· 669 669 VkResult 670 670 vk_locked_submit(struct vk_bundle *vk, VkQueue queue, uint32_t count, const VkSubmitInfo *infos, VkFence fence); 671 671 672 + 673 + /* 674 + * 675 + * State creation helpers, in the vk_state_creators.c file. 676 + * 677 + */ 678 + 679 + /*! 680 + * Arguments to @ref vk_create_descriptor_pool function. 681 + */ 682 + struct vk_descriptor_pool_info 683 + { 684 + uint32_t uniform_per_descriptor_count; //!< VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER 685 + uint32_t sampler_per_descriptor_count; //!< VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER 686 + uint32_t storage_image_per_descriptor_count; //!< VK_DESCRIPTOR_TYPE_STORAGE_IMAGE 687 + uint32_t storage_buffer_per_descriptor_count; //!< VK_DESCRIPTOR_TYPE_STORAGE_BUFFER 688 + 689 + //! The max count of created descriptors. 690 + uint32_t descriptor_count; 691 + 692 + //! Are descriptors freeable, or must vkResetDescriptorPool be used. 693 + bool freeable; 694 + }; 695 + 696 + /*! 697 + * Creates a descriptor pool, made for a single layout. 698 + * 699 + * Does error logging. 700 + */ 701 + VkResult 702 + vk_create_descriptor_pool(struct vk_bundle *vk, 703 + const struct vk_descriptor_pool_info *info, 704 + VkDescriptorPool *out_descriptor_pool); 705 + 706 + /*! 707 + * Creates a descriptor set. 708 + * 709 + * Does error logging. 710 + */ 711 + VkResult 712 + vk_create_descriptor_set(struct vk_bundle *vk, 713 + VkDescriptorPool descriptor_pool, 714 + VkDescriptorSetLayout descriptor_layout, 715 + VkDescriptorSet *out_descriptor_set); 716 + 717 + /*! 718 + * Creates a pipeline layout from a single descriptor set layout. 719 + * 720 + * Does error logging. 721 + */ 722 + VkResult 723 + vk_create_pipeline_layout(struct vk_bundle *vk, 724 + VkDescriptorSetLayout descriptor_set_layout, 725 + VkPipelineLayout *out_pipeline_layout); 726 + 727 + /*! 728 + * Creates a pipeline cache. 729 + * 730 + * Does error logging. 731 + */ 732 + VkResult 733 + vk_create_pipeline_cache(struct vk_bundle *vk, VkPipelineCache *out_pipeline_cache); 734 + 735 + /*! 736 + * Creates a compute pipeline, assumes entry function is called 'main'. 737 + * 738 + * Does error logging. 739 + */ 740 + VkResult 741 + vk_create_compute_pipeline(struct vk_bundle *vk, 742 + VkPipelineCache pipeline_cache, 743 + VkShaderModule shader, 744 + VkPipelineLayout pipeline_layout, 745 + VkPipeline *out_compute_pipeline); 746 + 747 + 748 + /* 749 + * 750 + * Command buffer helpers, in the vk_command_buffer.c file. 751 + * 752 + */ 753 + 754 + /*! 755 + * Creates a new command buffer using the bundle's pool, takes the cmd_pool_mutex. 756 + * 757 + * Does error logging. 758 + */ 759 + VkResult 760 + vk_create_command_buffer(struct vk_bundle *vk, VkCommandBuffer *out_cmd); 761 + 762 + /*! 763 + * Destroys a command buffer, takes the cmd_pool_mutex. 764 + * 765 + * Does error logging. 766 + */ 767 + void 768 + vk_destroy_command_buffer(struct vk_bundle *vk, VkCommandBuffer command_buffer); 769 + 770 + /*! 771 + * Issues the vkBeginCommandBuffer function on the command buffer. 772 + * 773 + * Does error logging. 774 + */ 775 + VkResult 776 + vk_begin_command_buffer(struct vk_bundle *vk, VkCommandBuffer command_buffer); 777 + 778 + /*! 779 + * Issues the vkEndCommandBuffer function on the command buffer. 780 + * 781 + * Does error logging. 782 + */ 783 + VkResult 784 + vk_end_command_buffer(struct vk_bundle *vk, VkCommandBuffer command_buffer); 785 + 786 + 672 787 #ifdef __cplusplus 673 788 } 674 789 #endif
+212
src/xrt/auxiliary/vk/vk_state_creators.c
··· 1 + // Copyright 2019-2021, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief Vulkan state creators helpers. 6 + * 7 + * @author Jakob Bornecrantz <jakob@collabora.com> 8 + * @author Lubosz Sarnecki <lubosz.sarnecki@collabora.com> 9 + * @ingroup aux_vk 10 + */ 11 + 12 + #include "vk/vk_helpers.h" 13 + 14 + 15 + VkResult 16 + vk_create_descriptor_pool(struct vk_bundle *vk, 17 + const struct vk_descriptor_pool_info *info, 18 + VkDescriptorPool *out_descriptor_pool) 19 + { 20 + VkResult ret; 21 + 22 + uint32_t pool_count = 0; 23 + VkDescriptorPoolSize pool_sizes[4] = {0}; 24 + 25 + const uint32_t descriptor_count = info->descriptor_count; 26 + const uint32_t uniform_count = info->uniform_per_descriptor_count * descriptor_count; 27 + const uint32_t sampler_count = info->sampler_per_descriptor_count * descriptor_count; 28 + const uint32_t storage_image_count = info->storage_image_per_descriptor_count * descriptor_count; 29 + const uint32_t storage_buffer_count = info->storage_buffer_per_descriptor_count * descriptor_count; 30 + 31 + if (uniform_count > 0) { 32 + pool_sizes[pool_count].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; 33 + pool_sizes[pool_count].descriptorCount = uniform_count; 34 + pool_count++; 35 + } 36 + 37 + if (sampler_count > 0) { 38 + pool_sizes[pool_count].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; 39 + pool_sizes[pool_count].descriptorCount = sampler_count; 40 + pool_count++; 41 + } 42 + 43 + if (storage_image_count > 0) { 44 + pool_sizes[pool_count].type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; 45 + pool_sizes[pool_count].descriptorCount = storage_image_count; 46 + pool_count++; 47 + } 48 + 49 + if (storage_buffer_count > 0) { 50 + pool_sizes[pool_count].type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; 51 + pool_sizes[pool_count].descriptorCount = storage_buffer_count; 52 + pool_count++; 53 + } 54 + 55 + assert(pool_count > 0 && pool_count <= ARRAY_SIZE(pool_sizes)); 56 + 57 + VkDescriptorPoolCreateFlags flags = 0; 58 + 59 + if (info->freeable) { 60 + flags |= VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; 61 + } 62 + 63 + VkDescriptorPoolCreateInfo descriptor_pool_info = { 64 + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, 65 + .flags = flags, 66 + .maxSets = descriptor_count, 67 + .poolSizeCount = pool_count, 68 + .pPoolSizes = pool_sizes, 69 + }; 70 + 71 + VkDescriptorPool descriptor_pool = VK_NULL_HANDLE; 72 + ret = vk->vkCreateDescriptorPool( // 73 + vk->device, // device 74 + &descriptor_pool_info, // pCreateInfo 75 + NULL, // pAllocator 76 + &descriptor_pool); // pDescriptorPool 77 + if (ret != VK_SUCCESS) { 78 + VK_ERROR(vk, "vkCreateRenderPass failed: %s", vk_result_string(ret)); 79 + return ret; 80 + } 81 + 82 + *out_descriptor_pool = descriptor_pool; 83 + 84 + return VK_SUCCESS; 85 + } 86 + 87 + VkResult 88 + vk_create_descriptor_set(struct vk_bundle *vk, 89 + VkDescriptorPool descriptor_pool, 90 + VkDescriptorSetLayout descriptor_layout, 91 + VkDescriptorSet *out_descriptor_set) 92 + { 93 + VkResult ret; 94 + 95 + VkDescriptorSetAllocateInfo alloc_info = { 96 + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, 97 + .descriptorPool = descriptor_pool, 98 + .descriptorSetCount = 1, 99 + .pSetLayouts = &descriptor_layout, 100 + }; 101 + 102 + VkDescriptorSet descriptor_set = VK_NULL_HANDLE; 103 + ret = vk->vkAllocateDescriptorSets( // 104 + vk->device, // device 105 + &alloc_info, // pAllocateInfo 106 + &descriptor_set); // pDescriptorSets 107 + if (ret != VK_SUCCESS) { 108 + VK_DEBUG(vk, "vkAllocateDescriptorSets failed: %s", vk_result_string(ret)); 109 + return ret; 110 + } 111 + 112 + *out_descriptor_set = descriptor_set; 113 + 114 + return VK_SUCCESS; 115 + } 116 + 117 + VkResult 118 + vk_create_pipeline_layout(struct vk_bundle *vk, 119 + VkDescriptorSetLayout descriptor_set_layout, 120 + VkPipelineLayout *out_pipeline_layout) 121 + { 122 + VkResult ret; 123 + 124 + VkPipelineLayoutCreateInfo pipeline_layout_info = { 125 + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, 126 + .setLayoutCount = 1, 127 + .pSetLayouts = &descriptor_set_layout, 128 + }; 129 + 130 + VkPipelineLayout pipeline_layout = VK_NULL_HANDLE; 131 + ret = vk->vkCreatePipelineLayout( // 132 + vk->device, // device 133 + &pipeline_layout_info, // pCreateInfo 134 + NULL, // pAllocator 135 + &pipeline_layout); // pPipelineLayout 136 + if (ret != VK_SUCCESS) { 137 + VK_ERROR(vk, "vkCreatePipelineLayout failed: %s", vk_result_string(ret)); 138 + return ret; 139 + } 140 + 141 + *out_pipeline_layout = pipeline_layout; 142 + 143 + return VK_SUCCESS; 144 + } 145 + 146 + VkResult 147 + vk_create_pipeline_cache(struct vk_bundle *vk, VkPipelineCache *out_pipeline_cache) 148 + { 149 + VkResult ret; 150 + 151 + VkPipelineCacheCreateInfo pipeline_cache_info = { 152 + .sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO, 153 + }; 154 + 155 + VkPipelineCache pipeline_cache; 156 + ret = vk->vkCreatePipelineCache( // 157 + vk->device, // device 158 + &pipeline_cache_info, // pCreateInfo 159 + NULL, // pAllocator 160 + &pipeline_cache); // pPipelineCache 161 + if (ret != VK_SUCCESS) { 162 + VK_ERROR(vk, "vkCreatePipelineCache failed: %s", vk_result_string(ret)); 163 + return ret; 164 + } 165 + 166 + *out_pipeline_cache = pipeline_cache; 167 + 168 + return VK_SUCCESS; 169 + } 170 + 171 + VkResult 172 + vk_create_compute_pipeline(struct vk_bundle *vk, 173 + VkPipelineCache pipeline_cache, 174 + VkShaderModule shader, 175 + VkPipelineLayout pipeline_layout, 176 + VkPipeline *out_compute_pipeline) 177 + { 178 + VkResult ret; 179 + 180 + VkPipelineShaderStageCreateInfo shader_stage_info = { 181 + .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, 182 + .pNext = NULL, 183 + .stage = VK_SHADER_STAGE_COMPUTE_BIT, 184 + .module = shader, 185 + .pName = "main", 186 + }; 187 + 188 + VkComputePipelineCreateInfo pipeline_info = { 189 + .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, 190 + .pNext = NULL, 191 + .flags = 0, 192 + .stage = shader_stage_info, 193 + .layout = pipeline_layout, 194 + }; 195 + 196 + VkPipeline pipeline = VK_NULL_HANDLE; 197 + ret = vk->vkCreateComputePipelines( // 198 + vk->device, // device 199 + pipeline_cache, // pipelineCache 200 + 1, // createInfoCount 201 + &pipeline_info, // pCreateInfos 202 + NULL, // pAllocator 203 + &pipeline); // pPipelines 204 + if (ret != VK_SUCCESS) { 205 + VK_DEBUG(vk, "vkCreateComputePipelines failed: %s", vk_result_string(ret)); 206 + return ret; 207 + } 208 + 209 + *out_compute_pipeline = pipeline; 210 + 211 + return VK_SUCCESS; 212 + }