1 #ifndef _VKTSPARSERESOURCESTESTSUTIL_HPP 2 #define _VKTSPARSERESOURCESTESTSUTIL_HPP 3 /*------------------------------------------------------------------------ 4 * Vulkan Conformance Tests 5 * ------------------------ 6 * 7 * Copyright (c) 2016 The Khronos Group Inc. 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 *//*! 22 * \file vktSparseResourcesTestsUtil.hpp 23 * \brief Sparse Resources Tests Utility Classes 24 *//*--------------------------------------------------------------------*/ 25 26 #include "vkDefs.hpp" 27 #include "vkMemUtil.hpp" 28 #include "vkRef.hpp" 29 #include "vkRefUtil.hpp" 30 #include "vkMemUtil.hpp" 31 #include "vkImageUtil.hpp" 32 #include "deSharedPtr.hpp" 33 #include "deUniquePtr.hpp" 34 35 namespace vkt 36 { 37 namespace sparse 38 { 39 40 typedef de::SharedPtr<vk::Unique<vk::VkDeviceMemory> > DeviceMemorySp; 41 42 enum ImageType 43 { 44 IMAGE_TYPE_1D = 0, 45 IMAGE_TYPE_1D_ARRAY, 46 IMAGE_TYPE_2D, 47 IMAGE_TYPE_2D_ARRAY, 48 IMAGE_TYPE_3D, 49 IMAGE_TYPE_CUBE, 50 IMAGE_TYPE_CUBE_ARRAY, 51 IMAGE_TYPE_BUFFER, 52 53 IMAGE_TYPE_LAST 54 }; 55 56 enum FeatureFlagBits 57 { 58 FEATURE_TESSELLATION_SHADER = 1u << 0, 59 FEATURE_GEOMETRY_SHADER = 1u << 1, 60 FEATURE_SHADER_FLOAT_64 = 1u << 2, 61 FEATURE_VERTEX_PIPELINE_STORES_AND_ATOMICS = 1u << 3, 62 FEATURE_FRAGMENT_STORES_AND_ATOMICS = 1u << 4, 63 FEATURE_SHADER_TESSELLATION_AND_GEOMETRY_POINT_SIZE = 1u << 5, 64 }; 65 typedef deUint32 FeatureFlags; 66 67 enum 68 { 69 BUFFER_IMAGE_COPY_OFFSET_GRANULARITY = 4u, 70 NO_MATCH_FOUND = ~((deUint32)0), //!< no matching index 71 }; 72 73 vk::VkImageType mapImageType (const ImageType imageType); 74 75 vk::VkImageViewType mapImageViewType (const ImageType imageType); 76 77 std::string getImageTypeName (const ImageType imageType); 78 79 std::string getShaderImageType (const tcu::TextureFormat& format, 80 const ImageType imageType); 81 82 std::string getShaderImageDataType (const tcu::TextureFormat& format); 83 84 std::string getShaderImageFormatQualifier (const tcu::TextureFormat& format); 85 86 std::string getShaderImageCoordinates (const ImageType imageType, 87 const std::string& x, 88 const std::string& xy, 89 const std::string& xyz); 90 91 //!< Size used for addresing image in a compute shader 92 tcu::UVec3 getShaderGridSize (const ImageType imageType, 93 const tcu::UVec3& imageSize, 94 const deUint32 mipLevel = 0); 95 96 //!< Size of a single image layer 97 tcu::UVec3 getLayerSize (const ImageType imageType, 98 const tcu::UVec3& imageSize); 99 100 //!< Number of array layers (for array and cube types) 101 deUint32 getNumLayers (const ImageType imageType, 102 const tcu::UVec3& imageSize); 103 104 //!< Number of texels in an image 105 deUint32 getNumPixels (const ImageType imageType, 106 const tcu::UVec3& imageSize); 107 108 //!< Coordinate dimension used for addressing (e.g. 3 (x,y,z) for 2d array) 109 deUint32 getDimensions (const ImageType imageType); 110 111 //!< Coordinate dimension used for addressing a single layer (e.g. 2 (x,y) for 2d array) 112 deUint32 getLayerDimensions (const ImageType imageType); 113 114 //!< Helper function for checking if requested image size does not exceed device limits 115 bool isImageSizeSupported (const vk::InstanceInterface& instance, 116 const vk::VkPhysicalDevice physicalDevice, 117 const ImageType imageType, 118 const tcu::UVec3& imageSize); 119 120 deUint32 getImageMaxMipLevels (const vk::VkImageFormatProperties& imageFormatProperties, 121 const vk::VkExtent3D& extent); 122 123 deUint32 getImageMipLevelSizeInBytes (const vk::VkExtent3D& baseExtents, 124 const deUint32 layersCount, 125 const tcu::TextureFormat& format, 126 const deUint32 mipmapLevel, 127 const deUint32 mipmapMemoryAlignment = 1u); 128 129 deUint32 getImageSizeInBytes (const vk::VkExtent3D& baseExtents, 130 const deUint32 layersCount, 131 const tcu::TextureFormat& format, 132 const deUint32 mipmapLevelsCount = 1u, 133 const deUint32 mipmapMemoryAlignment = 1u); 134 135 vk::Move<vk::VkCommandPool> makeCommandPool (const vk::DeviceInterface& vk, 136 const vk::VkDevice device, 137 const deUint32 queueFamilyIndex); 138 139 vk::Move<vk::VkPipelineLayout> makePipelineLayout (const vk::DeviceInterface& vk, 140 const vk::VkDevice device, 141 const vk::VkDescriptorSetLayout descriptorSetLayout = DE_NULL); 142 143 vk::Move<vk::VkPipeline> makeComputePipeline (const vk::DeviceInterface& vk, 144 const vk::VkDevice device, 145 const vk::VkPipelineLayout pipelineLayout, 146 const vk::VkShaderModule shaderModule, 147 const vk::VkSpecializationInfo* specializationInfo = 0); 148 149 vk::Move<vk::VkBufferView> makeBufferView (const vk::DeviceInterface& vk, 150 const vk::VkDevice device, 151 const vk::VkBuffer buffer, 152 const vk::VkFormat format, 153 const vk::VkDeviceSize offset, 154 const vk::VkDeviceSize size); 155 156 vk::Move<vk::VkImageView> makeImageView (const vk::DeviceInterface& vk, 157 const vk::VkDevice device, 158 const vk::VkImage image, 159 const vk::VkImageViewType imageViewType, 160 const vk::VkFormat format, 161 const vk::VkImageSubresourceRange subresourceRange); 162 163 vk::Move<vk::VkDescriptorSet> makeDescriptorSet (const vk::DeviceInterface& vk, 164 const vk::VkDevice device, 165 const vk::VkDescriptorPool descriptorPool, 166 const vk::VkDescriptorSetLayout setLayout); 167 168 vk::Move<vk::VkFramebuffer> makeFramebuffer (const vk::DeviceInterface& vk, 169 const vk::VkDevice device, 170 const vk::VkRenderPass renderPass, 171 const deUint32 attachmentCount, 172 const vk::VkImageView* pAttachments, 173 const deUint32 width, 174 const deUint32 height, 175 const deUint32 layers = 1u); 176 177 de::MovePtr<vk::Allocation> bindImage (const vk::DeviceInterface& vk, 178 const vk::VkDevice device, 179 vk::Allocator& allocator, 180 const vk::VkImage image, 181 const vk::MemoryRequirement requirement); 182 183 de::MovePtr<vk::Allocation> bindBuffer (const vk::DeviceInterface& vk, 184 const vk::VkDevice device, 185 vk::Allocator& allocator, 186 const vk::VkBuffer buffer, 187 const vk::MemoryRequirement requirement); 188 189 vk::VkBufferCreateInfo makeBufferCreateInfo (const vk::VkDeviceSize bufferSize, 190 const vk::VkBufferUsageFlags usage); 191 192 vk::VkBufferImageCopy makeBufferImageCopy (const vk::VkExtent3D extent, 193 const deUint32 layersCount, 194 const deUint32 mipmapLevel = 0u, 195 const vk::VkDeviceSize bufferOffset = 0ull); 196 197 vk::VkSparseImageMemoryBind makeSparseImageMemoryBind (const vk::DeviceInterface& vk, 198 const vk::VkDevice device, 199 const vk::VkDeviceSize allocationSize, 200 const deUint32 memoryType, 201 const vk::VkImageSubresource& subresource, 202 const vk::VkOffset3D& offset, 203 const vk::VkExtent3D& extent); 204 205 vk::VkSparseMemoryBind makeSparseMemoryBind (const vk::DeviceInterface& vk, 206 const vk::VkDevice device, 207 const vk::VkDeviceSize allocationSize, 208 const deUint32 memoryType, 209 const vk::VkDeviceSize resourceOffset, 210 const vk::VkSparseMemoryBindFlags flags = 0u); 211 212 void submitCommands (const vk::DeviceInterface& vk, 213 const vk::VkQueue queue, 214 const vk::VkCommandBuffer cmdBuffer, 215 const deUint32 waitSemaphoreCount = 0, 216 const vk::VkSemaphore* pWaitSemaphores = DE_NULL, 217 const vk::VkPipelineStageFlags* pWaitDstStageMask = DE_NULL, 218 const deUint32 signalSemaphoreCount = 0, 219 const vk::VkSemaphore* pSignalSemaphores = DE_NULL); 220 221 void submitCommandsAndWait (const vk::DeviceInterface& vk, 222 const vk::VkDevice device, 223 const vk::VkQueue queue, 224 const vk::VkCommandBuffer cmdBuffer, 225 const deUint32 waitSemaphoreCount = 0, 226 const vk::VkSemaphore* pWaitSemaphores = DE_NULL, 227 const vk::VkPipelineStageFlags* pWaitDstStageMask = DE_NULL, 228 const deUint32 signalSemaphoreCount = 0, 229 const vk::VkSemaphore* pSignalSemaphores = DE_NULL, 230 const bool useDeviceGroups = false, 231 const deUint32 physicalDeviceID = 0); 232 233 void requireFeatures (const vk::InstanceInterface& vki, 234 const vk::VkPhysicalDevice physicalDevice, 235 const FeatureFlags flags); 236 237 deUint32 findMatchingMemoryType (const vk::InstanceInterface& instance, 238 const vk::VkPhysicalDevice physicalDevice, 239 const vk::VkMemoryRequirements& objectMemoryRequirements, 240 const vk::MemoryRequirement& memoryRequirement); 241 242 deUint32 getHeapIndexForMemoryType (const vk::InstanceInterface& instance, 243 const vk::VkPhysicalDevice physicalDevice, 244 const deUint32 memoryType); 245 246 bool checkSparseSupportForImageType (const vk::InstanceInterface& instance, 247 const vk::VkPhysicalDevice physicalDevice, 248 const ImageType imageType); 249 250 bool checkSparseSupportForImageFormat (const vk::InstanceInterface& instance, 251 const vk::VkPhysicalDevice physicalDevice, 252 const vk::VkImageCreateInfo& imageInfo); 253 254 bool checkImageFormatFeatureSupport (const vk::InstanceInterface& instance, 255 const vk::VkPhysicalDevice physicalDevice, 256 const vk::VkFormat format, 257 const vk::VkFormatFeatureFlags featureFlags); 258 259 deUint32 getSparseAspectRequirementsIndex (const std::vector<vk::VkSparseImageMemoryRequirements>& requirements, 260 const vk::VkImageAspectFlags aspectFlags); 261 262 inline vk::Move<vk::VkBuffer> makeBuffer (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkBufferCreateInfo& createInfo) 263 { 264 return createBuffer(vk, device, &createInfo); 265 } 266 267 inline vk::Move<vk::VkImage> makeImage (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkImageCreateInfo& createInfo) 268 { 269 return createImage(vk, device, &createInfo); 270 } 271 272 template<typename T> 273 inline de::SharedPtr<vk::Unique<T> > makeVkSharedPtr (vk::Move<T> vkMove) 274 { 275 return de::SharedPtr<vk::Unique<T> >(new vk::Unique<T>(vkMove)); 276 } 277 278 template<typename T> 279 inline de::SharedPtr<de::UniquePtr<T> > makeDeSharedPtr (de::MovePtr<T> deMove) 280 { 281 return de::SharedPtr<de::UniquePtr<T> >(new de::UniquePtr<T>(deMove)); 282 } 283 284 template<typename T> 285 inline std::size_t sizeInBytes (const std::vector<T>& vec) 286 { 287 return vec.size() * sizeof(vec[0]); 288 } 289 290 template<typename T> 291 inline const T* getDataOrNullptr (const std::vector<T>& vec, const std::size_t index = 0u) 292 { 293 return (index < vec.size() ? &vec[index] : DE_NULL); 294 } 295 296 } // sparse 297 } // vkt 298 299 #endif // _VKTSPARSERESOURCESTESTSUTIL_HPP 300