1 /*------------------------------------------------------------------------ 2 * Vulkan Conformance Tests 3 * ------------------------ 4 * 5 * Copyright (c) 2016 The Khronos Group Inc. 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 *//*! 20 * \file 21 * \brief Object creation utilities 22 *//*--------------------------------------------------------------------*/ 23 24 #include "vktPipelineMakeUtil.hpp" 25 #include "vkTypeUtil.hpp" 26 #include "vkPrograms.hpp" 27 #include "vkRefUtil.hpp" 28 #include "vkQueryUtil.hpp" 29 #include <vector> 30 31 namespace vkt 32 { 33 namespace pipeline 34 { 35 using namespace vk; 36 using de::MovePtr; 37 38 Buffer::Buffer (const vk::DeviceInterface& vk, 39 const vk::VkDevice device, 40 vk::Allocator& allocator, 41 const vk::VkBufferCreateInfo& bufferCreateInfo, 42 const vk::MemoryRequirement memoryRequirement) 43 : m_buffer (createBuffer(vk, device, &bufferCreateInfo)) 44 , m_allocation (bindBuffer(vk, device, allocator, *m_buffer, memoryRequirement)) 45 { 46 } 47 48 Image::Image (const vk::DeviceInterface& vk, 49 const vk::VkDevice device, 50 vk::Allocator& allocator, 51 const vk::VkImageCreateInfo& imageCreateInfo, 52 const vk::MemoryRequirement memoryRequirement) 53 : m_image (createImage(vk, device, &imageCreateInfo)) 54 , m_allocation (bindImage(vk, device, allocator, *m_image, memoryRequirement)) 55 { 56 } 57 58 VkBufferCreateInfo makeBufferCreateInfo (const VkDeviceSize bufferSize, 59 const VkBufferUsageFlags usage) 60 { 61 const VkBufferCreateInfo bufferCreateInfo = 62 { 63 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType; 64 DE_NULL, // const void* pNext; 65 (VkBufferCreateFlags)0, // VkBufferCreateFlags flags; 66 bufferSize, // VkDeviceSize size; 67 usage, // VkBufferUsageFlags usage; 68 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode; 69 0u, // deUint32 queueFamilyIndexCount; 70 DE_NULL, // const deUint32* pQueueFamilyIndices; 71 }; 72 return bufferCreateInfo; 73 } 74 75 Move<VkCommandBuffer> makeCommandBuffer (const DeviceInterface& vk, const VkDevice device, const VkCommandPool commandPool) 76 { 77 return allocateCommandBuffer(vk, device, commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY); 78 } 79 80 Move<VkDescriptorSet> makeDescriptorSet (const DeviceInterface& vk, 81 const VkDevice device, 82 const VkDescriptorPool descriptorPool, 83 const VkDescriptorSetLayout setLayout) 84 { 85 const VkDescriptorSetAllocateInfo info = 86 { 87 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, // VkStructureType sType; 88 DE_NULL, // const void* pNext; 89 descriptorPool, // VkDescriptorPool descriptorPool; 90 1u, // deUint32 descriptorSetCount; 91 &setLayout, // const VkDescriptorSetLayout* pSetLayouts; 92 }; 93 return allocateDescriptorSet(vk, device, &info); 94 } 95 96 Move<VkPipelineLayout> makePipelineLayout (const DeviceInterface& vk, 97 const VkDevice device) 98 { 99 const VkPipelineLayoutCreateInfo info = 100 { 101 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, // VkStructureType sType; 102 DE_NULL, // const void* pNext; 103 (VkPipelineLayoutCreateFlags)0, // VkPipelineLayoutCreateFlags flags; 104 0u, // deUint32 setLayoutCount; 105 DE_NULL, // const VkDescriptorSetLayout* pSetLayouts; 106 0u, // deUint32 pushConstantRangeCount; 107 DE_NULL, // const VkPushConstantRange* pPushConstantRanges; 108 }; 109 return createPipelineLayout(vk, device, &info); 110 } 111 112 Move<VkPipelineLayout> makePipelineLayout (const DeviceInterface& vk, 113 const VkDevice device, 114 const VkDescriptorSetLayout descriptorSetLayout) 115 { 116 const VkPipelineLayoutCreateInfo info = 117 { 118 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, // VkStructureType sType; 119 DE_NULL, // const void* pNext; 120 (VkPipelineLayoutCreateFlags)0, // VkPipelineLayoutCreateFlags flags; 121 1u, // deUint32 setLayoutCount; 122 &descriptorSetLayout, // const VkDescriptorSetLayout* pSetLayouts; 123 0u, // deUint32 pushConstantRangeCount; 124 DE_NULL, // const VkPushConstantRange* pPushConstantRanges; 125 }; 126 return createPipelineLayout(vk, device, &info); 127 } 128 129 Move<VkPipeline> makeComputePipeline (const DeviceInterface& vk, 130 const VkDevice device, 131 const VkPipelineLayout pipelineLayout, 132 const VkShaderModule shaderModule, 133 const VkSpecializationInfo* specInfo) 134 { 135 const VkPipelineShaderStageCreateInfo shaderStageInfo = 136 { 137 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, // VkStructureType sType; 138 DE_NULL, // const void* pNext; 139 (VkPipelineShaderStageCreateFlags)0, // VkPipelineShaderStageCreateFlags flags; 140 VK_SHADER_STAGE_COMPUTE_BIT, // VkShaderStageFlagBits stage; 141 shaderModule, // VkShaderModule module; 142 "main", // const char* pName; 143 specInfo, // const VkSpecializationInfo* pSpecializationInfo; 144 }; 145 const VkComputePipelineCreateInfo pipelineInfo = 146 { 147 VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, // VkStructureType sType; 148 DE_NULL, // const void* pNext; 149 (VkPipelineCreateFlags)0, // VkPipelineCreateFlags flags; 150 shaderStageInfo, // VkPipelineShaderStageCreateInfo stage; 151 pipelineLayout, // VkPipelineLayout layout; 152 DE_NULL, // VkPipeline basePipelineHandle; 153 0, // deInt32 basePipelineIndex; 154 }; 155 return createComputePipeline(vk, device, DE_NULL , &pipelineInfo); 156 } 157 158 Move<VkImageView> makeImageView (const DeviceInterface& vk, 159 const VkDevice vkDevice, 160 const VkImage image, 161 const VkImageViewType viewType, 162 const VkFormat format, 163 const VkImageSubresourceRange subresourceRange) 164 { 165 const VkImageViewCreateInfo imageViewParams = 166 { 167 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType; 168 DE_NULL, // const void* pNext; 169 (VkImageViewCreateFlags)0, // VkImageViewCreateFlags flags; 170 image, // VkImage image; 171 viewType, // VkImageViewType viewType; 172 format, // VkFormat format; 173 makeComponentMappingRGBA(), // VkComponentMapping components; 174 subresourceRange, // VkImageSubresourceRange subresourceRange; 175 }; 176 return createImageView(vk, vkDevice, &imageViewParams); 177 } 178 179 Move<VkFramebuffer> makeFramebuffer (const DeviceInterface& vk, 180 const VkDevice device, 181 const VkRenderPass renderPass, 182 const deUint32 attachmentCount, 183 const VkImageView* pAttachments, 184 const deUint32 width, 185 const deUint32 height, 186 const deUint32 layers) 187 { 188 const VkFramebufferCreateInfo framebufferInfo = { 189 VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, // VkStructureType sType; 190 DE_NULL, // const void* pNext; 191 (VkFramebufferCreateFlags)0, // VkFramebufferCreateFlags flags; 192 renderPass, // VkRenderPass renderPass; 193 attachmentCount, // uint32_t attachmentCount; 194 pAttachments, // const VkImageView* pAttachments; 195 width, // uint32_t width; 196 height, // uint32_t height; 197 layers, // uint32_t layers; 198 }; 199 200 return createFramebuffer(vk, device, &framebufferInfo); 201 } 202 203 MovePtr<Allocation> bindImage (const DeviceInterface& vk, const VkDevice device, Allocator& allocator, const VkImage image, const MemoryRequirement requirement) 204 { 205 MovePtr<Allocation> alloc = allocator.allocate(getImageMemoryRequirements(vk, device, image), requirement); 206 VK_CHECK(vk.bindImageMemory(device, image, alloc->getMemory(), alloc->getOffset())); 207 return alloc; 208 } 209 210 MovePtr<Allocation> bindBuffer (const DeviceInterface& vk, const VkDevice device, Allocator& allocator, const VkBuffer buffer, const MemoryRequirement requirement) 211 { 212 MovePtr<Allocation> alloc(allocator.allocate(getBufferMemoryRequirements(vk, device, buffer), requirement)); 213 VK_CHECK(vk.bindBufferMemory(device, buffer, alloc->getMemory(), alloc->getOffset())); 214 return alloc; 215 } 216 217 MovePtr<Allocation> bindImageDedicated (const InstanceInterface& vki, const DeviceInterface& vkd, const VkPhysicalDevice physDevice, const VkDevice device, const VkImage image, const MemoryRequirement requirement) 218 { 219 MovePtr<Allocation> alloc(allocateDedicated(vki, vkd, physDevice, device, image, requirement)); 220 VK_CHECK(vkd.bindImageMemory(device, image, alloc->getMemory(), alloc->getOffset())); 221 return alloc; 222 } 223 224 MovePtr<Allocation> bindBufferDedicated (const InstanceInterface& vki, const DeviceInterface& vkd, const VkPhysicalDevice physDevice, const VkDevice device, const VkBuffer buffer, const MemoryRequirement requirement) 225 { 226 MovePtr<Allocation> alloc(allocateDedicated(vki, vkd, physDevice, device, buffer, requirement)); 227 VK_CHECK(vkd.bindBufferMemory(device, buffer, alloc->getMemory(), alloc->getOffset())); 228 return alloc; 229 } 230 231 } // pipeline 232 } // vkt 233