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 "vkObjUtil.hpp"
28 #include "vkMemUtil.hpp"
29 #include "vkRef.hpp"
30 #include "vkRefUtil.hpp"
31 #include "vkMemUtil.hpp"
32 #include "vkImageUtil.hpp"
33 #include "deSharedPtr.hpp"
34 #include "deUniquePtr.hpp"
35 
36 namespace vkt
37 {
38 namespace sparse
39 {
40 
41 typedef de::SharedPtr<vk::Unique<vk::VkDeviceMemory>> DeviceMemorySp;
42 
43 enum ImageType
44 {
45     IMAGE_TYPE_1D = 0,
46     IMAGE_TYPE_1D_ARRAY,
47     IMAGE_TYPE_2D,
48     IMAGE_TYPE_2D_ARRAY,
49     IMAGE_TYPE_3D,
50     IMAGE_TYPE_CUBE,
51     IMAGE_TYPE_CUBE_ARRAY,
52     IMAGE_TYPE_BUFFER,
53 
54     IMAGE_TYPE_LAST
55 };
56 
57 enum FeatureFlagBits
58 {
59     FEATURE_TESSELLATION_SHADER                         = 1u << 0,
60     FEATURE_GEOMETRY_SHADER                             = 1u << 1,
61     FEATURE_SHADER_FLOAT_64                             = 1u << 2,
62     FEATURE_VERTEX_PIPELINE_STORES_AND_ATOMICS          = 1u << 3,
63     FEATURE_FRAGMENT_STORES_AND_ATOMICS                 = 1u << 4,
64     FEATURE_SHADER_TESSELLATION_AND_GEOMETRY_POINT_SIZE = 1u << 5,
65 };
66 typedef uint32_t FeatureFlags;
67 
68 enum
69 {
70     NO_MATCH_FOUND = ~((uint32_t)0), //!< no matching index
71 };
72 
73 struct TestFormat
74 {
75     vk::VkFormat format;
76 };
77 
78 struct TestImageParameters
79 {
80     ImageType imageType;
81     std::vector<tcu::UVec3> imageSizes;
82     std::vector<TestFormat> formats;
83 
TestImageParametersvkt::sparse::TestImageParameters84     TestImageParameters(ImageType imageType_, const std::vector<tcu::UVec3> &imageSizes_,
85                         const std::vector<TestFormat> &formats_)
86         : imageType(imageType_)
87         , imageSizes(imageSizes_)
88         , formats(formats_)
89     {
90     }
91 };
92 
93 bool formatIsR64(const vk::VkFormat &format);
94 
95 std::vector<TestFormat> getTestFormats(const ImageType &imageType);
96 
97 vk::VkImageType mapImageType(const ImageType imageType);
98 
99 vk::VkImageViewType mapImageViewType(const ImageType imageType);
100 
101 std::string getImageTypeName(const ImageType imageType);
102 
103 std::string getShaderImageType(const tcu::TextureFormat &format, const ImageType imageType);
104 
105 std::string getShaderImageType(const vk::PlanarFormatDescription &description, const ImageType imageType);
106 
107 std::string getShaderImageDataType(const tcu::TextureFormat &format);
108 
109 std::string getShaderImageDataType(const vk::PlanarFormatDescription &description);
110 
111 std::string getShaderImageFormatQualifier(const tcu::TextureFormat &format);
112 
113 std::string getShaderImageFormatQualifier(vk::VkFormat format);
114 
115 std::string getImageFormatID(vk::VkFormat format);
116 
117 std::string getShaderImageCoordinates(const ImageType imageType, const std::string &x, const std::string &xy,
118                                       const std::string &xyz);
119 
120 //!< Size used for addresing image in a compute shader
121 tcu::UVec3 getShaderGridSize(const ImageType imageType, const tcu::UVec3 &imageSize, const uint32_t mipLevel = 0);
122 
123 //!< Size of a single image layer
124 tcu::UVec3 getLayerSize(const ImageType imageType, const tcu::UVec3 &imageSize);
125 
126 //!< Number of array layers (for array and cube types)
127 uint32_t getNumLayers(const ImageType imageType, const tcu::UVec3 &imageSize);
128 
129 //!< Number of texels in an image
130 uint32_t getNumPixels(const ImageType imageType, const tcu::UVec3 &imageSize);
131 
132 //!< Coordinate dimension used for addressing (e.g. 3 (x,y,z) for 2d array)
133 uint32_t getDimensions(const ImageType imageType);
134 
135 //!< Coordinate dimension used for addressing a single layer (e.g. 2 (x,y) for 2d array)
136 uint32_t getLayerDimensions(const ImageType imageType);
137 
138 //!< Helper function for checking if requested image size does not exceed device limits
139 bool isImageSizeSupported(const vk::InstanceInterface &instance, const vk::VkPhysicalDevice physicalDevice,
140                           const ImageType imageType, const tcu::UVec3 &imageSize);
141 
142 uint32_t getImageMipLevelSizeInBytes(const vk::VkExtent3D &baseExtents, const uint32_t layersCount,
143                                      const tcu::TextureFormat &format, const uint32_t mipmapLevel,
144                                      const uint32_t mipmapMemoryAlignment = 1u);
145 
146 uint32_t getImageSizeInBytes(const vk::VkExtent3D &baseExtents, const uint32_t layersCount,
147                              const tcu::TextureFormat &format, const uint32_t mipmapLevelsCount = 1u,
148                              const uint32_t mipmapMemoryAlignment = 1u);
149 
150 uint32_t getImageMipLevelSizeInBytes(const vk::VkExtent3D &baseExtents, const uint32_t layersCount,
151                                      const vk::PlanarFormatDescription &formatDescription, const uint32_t planeNdx,
152                                      const uint32_t mipmapLevel, const uint32_t mipmapMemoryAlignment = 1u);
153 
154 uint32_t getImageSizeInBytes(const vk::VkExtent3D &baseExtents, const uint32_t layersCount,
155                              const vk::PlanarFormatDescription &formatDescription, const uint32_t planeNdx,
156                              const uint32_t mipmapLevelsCount = 1u, const uint32_t mipmapMemoryAlignment = 1u);
157 
158 vk::VkBufferImageCopy makeBufferImageCopy(const vk::VkExtent3D extent, const uint32_t layersCount,
159                                           const uint32_t mipmapLevel = 0u, const vk::VkDeviceSize bufferOffset = 0ull);
160 
161 vk::VkSparseImageMemoryBind makeSparseImageMemoryBind(const vk::DeviceInterface &vk, const vk::VkDevice device,
162                                                       const vk::VkDeviceSize allocationSize, const uint32_t memoryType,
163                                                       const vk::VkImageSubresource &subresource,
164                                                       const vk::VkOffset3D &offset, const vk::VkExtent3D &extent);
165 
166 vk::VkSparseMemoryBind makeSparseMemoryBind(const vk::DeviceInterface &vk, const vk::VkDevice device,
167                                             const vk::VkDeviceSize allocationSize, const uint32_t memoryType,
168                                             const vk::VkDeviceSize resourceOffset,
169                                             const vk::VkSparseMemoryBindFlags flags = 0u);
170 
171 void submitCommands(const vk::DeviceInterface &vk, const vk::VkQueue queue, const vk::VkCommandBuffer cmdBuffer,
172                     const uint32_t waitSemaphoreCount = 0, const vk::VkSemaphore *pWaitSemaphores = DE_NULL,
173                     const vk::VkPipelineStageFlags *pWaitDstStageMask = DE_NULL,
174                     const uint32_t signalSemaphoreCount = 0, const vk::VkSemaphore *pSignalSemaphores = DE_NULL);
175 
176 void submitCommandsAndWait(const vk::DeviceInterface &vk, const vk::VkDevice device, const vk::VkQueue queue,
177                            const vk::VkCommandBuffer cmdBuffer, const uint32_t waitSemaphoreCount = 0,
178                            const vk::VkSemaphore *pWaitSemaphores            = DE_NULL,
179                            const vk::VkPipelineStageFlags *pWaitDstStageMask = DE_NULL,
180                            const uint32_t signalSemaphoreCount = 0, const vk::VkSemaphore *pSignalSemaphores = DE_NULL,
181                            const bool useDeviceGroups = false, const uint32_t physicalDeviceID = 0);
182 
183 void requireFeatures(const vk::InstanceInterface &vki, const vk::VkPhysicalDevice physicalDevice,
184                      const FeatureFlags flags);
185 
186 uint32_t findMatchingMemoryType(const vk::InstanceInterface &instance, const vk::VkPhysicalDevice physicalDevice,
187                                 const vk::VkMemoryRequirements &objectMemoryRequirements,
188                                 const vk::MemoryRequirement &memoryRequirement);
189 
190 uint32_t getHeapIndexForMemoryType(const vk::InstanceInterface &instance, const vk::VkPhysicalDevice physicalDevice,
191                                    const uint32_t memoryType);
192 
193 bool checkSparseSupportForImageType(const vk::InstanceInterface &instance, const vk::VkPhysicalDevice physicalDevice,
194                                     const ImageType imageType);
195 
196 bool checkSparseSupportForImageFormat(const vk::InstanceInterface &instance, const vk::VkPhysicalDevice physicalDevice,
197                                       const vk::VkImageCreateInfo &imageInfo);
198 
199 bool checkImageFormatFeatureSupport(const vk::InstanceInterface &instance, const vk::VkPhysicalDevice physicalDevice,
200                                     const vk::VkFormat format, const vk::VkFormatFeatureFlags featureFlags);
201 
202 uint32_t getSparseAspectRequirementsIndex(const std::vector<vk::VkSparseImageMemoryRequirements> &requirements,
203                                           const vk::VkImageAspectFlags aspectFlags);
204 
205 vk::VkFormat getPlaneCompatibleFormatForWriting(const vk::PlanarFormatDescription &formatInfo, uint32_t planeNdx);
206 
207 bool areLsb6BitsDontCare(vk::VkFormat format);
208 
209 bool areLsb4BitsDontCare(vk::VkFormat format);
210 
211 template <typename T>
makeVkSharedPtr(vk::Move<T> vkMove)212 inline de::SharedPtr<vk::Unique<T>> makeVkSharedPtr(vk::Move<T> vkMove)
213 {
214     return de::SharedPtr<vk::Unique<T>>(new vk::Unique<T>(vkMove));
215 }
216 
217 template <typename T>
makeDeSharedPtr(de::MovePtr<T> deMove)218 inline de::SharedPtr<de::UniquePtr<T>> makeDeSharedPtr(de::MovePtr<T> deMove)
219 {
220     return de::SharedPtr<de::UniquePtr<T>>(new de::UniquePtr<T>(deMove));
221 }
222 
223 template <typename T>
sizeInBytes(const std::vector<T> & vec)224 inline std::size_t sizeInBytes(const std::vector<T> &vec)
225 {
226     return vec.size() * sizeof(vec[0]);
227 }
228 
229 template <typename T>
getDataOrNullptr(const std::vector<T> & vec,const std::size_t index=0u)230 inline const T *getDataOrNullptr(const std::vector<T> &vec, const std::size_t index = 0u)
231 {
232     return (index < vec.size() ? &vec[index] : DE_NULL);
233 }
234 
235 } // namespace sparse
236 } // namespace vkt
237 
238 #endif // _VKTSPARSERESOURCESTESTSUTIL_HPP
239