1 #ifndef _VKTYPEUTIL_HPP
2 #define _VKTYPEUTIL_HPP
3 /*-------------------------------------------------------------------------
4 * Vulkan CTS Framework
5 * --------------------
6 *
7 * Copyright (c) 2015 Google 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
23 * \brief Utilities for creating commonly used composite types.
24 *//*--------------------------------------------------------------------*/
25
26 #include "vkDefs.hpp"
27 #include "tcuVector.hpp"
28
29 namespace vk
30 {
31 #include "vkTypeUtil.inl"
32
makeVkBool(bool b)33 inline VkBool32 makeVkBool(bool b)
34 {
35 return (b ? VK_TRUE : VK_FALSE);
36 }
37
makeClearValueColorF32(float r,float g,float b,float a)38 inline VkClearValue makeClearValueColorF32(float r, float g, float b, float a)
39 {
40 VkClearValue v;
41 v.color.float32[0] = r;
42 v.color.float32[1] = g;
43 v.color.float32[2] = b;
44 v.color.float32[3] = a;
45 return v;
46 }
47
makeClearValueColorVec4(tcu::Vec4 vec)48 inline VkClearValue makeClearValueColorVec4(tcu::Vec4 vec)
49 {
50 return makeClearValueColorF32(vec.x(), vec.y(), vec.z(), vec.w());
51 }
52
makeClearValueColorU32(uint32_t r,uint32_t g,uint32_t b,uint32_t a)53 inline VkClearValue makeClearValueColorU32(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
54 {
55 VkClearValue v;
56 v.color.uint32[0] = r;
57 v.color.uint32[1] = g;
58 v.color.uint32[2] = b;
59 v.color.uint32[3] = a;
60 return v;
61 }
62
makeClearValueColorI32(int32_t r,int32_t g,int32_t b,int32_t a)63 inline VkClearValue makeClearValueColorI32(int32_t r, int32_t g, int32_t b, int32_t a)
64 {
65 VkClearValue v;
66 v.color.int32[0] = r;
67 v.color.int32[1] = g;
68 v.color.int32[2] = b;
69 v.color.int32[3] = a;
70 return v;
71 }
72
makeClearValueColor(const tcu::Vec4 & color)73 inline VkClearValue makeClearValueColor(const tcu::Vec4 &color)
74 {
75 VkClearValue v;
76 v.color.float32[0] = color[0];
77 v.color.float32[1] = color[1];
78 v.color.float32[2] = color[2];
79 v.color.float32[3] = color[3];
80 return v;
81 }
82
makeClearValueDepthStencil(float depth,uint32_t stencil)83 inline VkClearValue makeClearValueDepthStencil(float depth, uint32_t stencil)
84 {
85 VkClearValue v;
86 v.depthStencil.depth = depth;
87 v.depthStencil.stencil = stencil;
88 return v;
89 }
90
makeClearValue(VkClearColorValue color)91 inline VkClearValue makeClearValue(VkClearColorValue color)
92 {
93 VkClearValue v;
94 v.color = color;
95 return v;
96 }
97
makeComponentMappingRGBA(void)98 inline VkComponentMapping makeComponentMappingRGBA(void)
99 {
100 return makeComponentMapping(VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B,
101 VK_COMPONENT_SWIZZLE_A);
102 }
103
makeComponentMappingIdentity(void)104 inline VkComponentMapping makeComponentMappingIdentity(void)
105 {
106 return makeComponentMapping(VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY,
107 VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY);
108 }
109
makeExtent3D(const tcu::IVec3 & vec)110 inline VkExtent3D makeExtent3D(const tcu::IVec3 &vec)
111 {
112 return makeExtent3D((uint32_t)vec.x(), (uint32_t)vec.y(), (uint32_t)vec.z());
113 }
114
makeExtent3D(const tcu::UVec3 & vec)115 inline VkExtent3D makeExtent3D(const tcu::UVec3 &vec)
116 {
117 return makeExtent3D(vec.x(), vec.y(), vec.z());
118 }
119
makeRect2D(int32_t x,int32_t y,uint32_t width,uint32_t height)120 inline VkRect2D makeRect2D(int32_t x, int32_t y, uint32_t width, uint32_t height)
121 {
122 VkRect2D r;
123 r.offset.x = x;
124 r.offset.y = y;
125 r.extent.width = width;
126 r.extent.height = height;
127
128 return r;
129 }
130
makeRect2D(const tcu::IVec2 & vec)131 inline VkRect2D makeRect2D(const tcu::IVec2 &vec)
132 {
133 return makeRect2D(0, 0, vec.x(), vec.y());
134 }
135
makeRect2D(const tcu::IVec3 & vec)136 inline VkRect2D makeRect2D(const tcu::IVec3 &vec)
137 {
138 return makeRect2D(0, 0, vec.x(), vec.y());
139 }
140
makeRect2D(const tcu::UVec2 & vec)141 inline VkRect2D makeRect2D(const tcu::UVec2 &vec)
142 {
143 return makeRect2D(0, 0, vec.x(), vec.y());
144 }
145
makeRect2D(const VkExtent3D & extent)146 inline VkRect2D makeRect2D(const VkExtent3D &extent)
147 {
148 return makeRect2D(0, 0, extent.width, extent.height);
149 }
150
makeRect2D(const VkExtent2D & extent)151 inline VkRect2D makeRect2D(const VkExtent2D &extent)
152 {
153 return makeRect2D(0, 0, extent.width, extent.height);
154 }
155
makeRect2D(const uint32_t width,const uint32_t height)156 inline VkRect2D makeRect2D(const uint32_t width, const uint32_t height)
157 {
158 return makeRect2D(0, 0, width, height);
159 }
160
makeViewport(const tcu::IVec2 & vec)161 inline VkViewport makeViewport(const tcu::IVec2 &vec)
162 {
163 return makeViewport(0.0f, 0.0f, (float)vec.x(), (float)vec.y(), 0.0f, 1.0f);
164 }
165
makeViewport(const tcu::IVec3 & vec)166 inline VkViewport makeViewport(const tcu::IVec3 &vec)
167 {
168 return makeViewport(0.0f, 0.0f, (float)vec.x(), (float)vec.y(), 0.0f, 1.0f);
169 }
170
makeViewport(const tcu::UVec2 & vec)171 inline VkViewport makeViewport(const tcu::UVec2 &vec)
172 {
173 return makeViewport(0.0f, 0.0f, (float)vec.x(), (float)vec.y(), 0.0f, 1.0f);
174 }
175
makeViewport(const VkExtent3D & extent)176 inline VkViewport makeViewport(const VkExtent3D &extent)
177 {
178 return makeViewport(0.0f, 0.0f, (float)extent.width, (float)extent.height, 0.0f, 1.0f);
179 }
180
makeViewport(const VkExtent2D & extent)181 inline VkViewport makeViewport(const VkExtent2D &extent)
182 {
183 return makeViewport(0.0f, 0.0f, (float)extent.width, (float)extent.height, 0.0f, 1.0f);
184 }
185
makeViewport(const uint32_t width,const uint32_t height)186 inline VkViewport makeViewport(const uint32_t width, const uint32_t height)
187 {
188 return makeViewport(0.0f, 0.0f, (float)width, (float)height, 0.0f, 1.0f);
189 }
190
makeSemaphoreSubmitInfo(VkSemaphore semaphore,VkPipelineStageFlags2KHR stageMask,uint64_t value=0,uint32_t deviceIndex=0)191 inline VkSemaphoreSubmitInfoKHR makeSemaphoreSubmitInfo(VkSemaphore semaphore, VkPipelineStageFlags2KHR stageMask,
192 uint64_t value = 0, uint32_t deviceIndex = 0)
193 {
194 return VkSemaphoreSubmitInfoKHR{
195 VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO_KHR, // VkStructureType sType;
196 DE_NULL, // const void* pNext;
197 semaphore, // VkSemaphore semaphore;
198 value, // uint64_t value;
199 stageMask, // VkPipelineStageFlags2KHR stageMask;
200 deviceIndex, // uint32_t deviceIndex;
201 };
202 }
203
makePipelineShaderStageCreateInfo(VkShaderStageFlagBits stage,VkShaderModule module,const VkSpecializationInfo * pSpecializationInfo=nullptr,const void * pNext=nullptr)204 inline VkPipelineShaderStageCreateInfo makePipelineShaderStageCreateInfo(
205 VkShaderStageFlagBits stage, VkShaderModule module, const VkSpecializationInfo *pSpecializationInfo = nullptr,
206 const void *pNext = nullptr)
207 {
208 const VkPipelineShaderStageCreateInfo stageInfo = {
209 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, // VkStructureType sType;
210 pNext, // const void* pNext;
211 0u, // VkPipelineShaderStageCreateFlags flags;
212 stage, // VkShaderStageFlagBits stage;
213 module, // VkShaderModule module;
214 "main", // const char* pName;
215 pSpecializationInfo, // const VkSpecializationInfo* pSpecializationInfo;
216 };
217 return stageInfo;
218 }
219
primitiveTopologyCastToList(const VkPrimitiveTopology primitiveTopology)220 inline VkPrimitiveTopology primitiveTopologyCastToList(const VkPrimitiveTopology primitiveTopology)
221 {
222 DE_STATIC_ASSERT(static_cast<uint64_t>(VK_PRIMITIVE_TOPOLOGY_PATCH_LIST) + 1 ==
223 static_cast<uint64_t>(VK_PRIMITIVE_TOPOLOGY_LAST));
224
225 switch (primitiveTopology)
226 {
227 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
228 return VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
229 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
230 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
231 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
232 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
233 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
234 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
235 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
236 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
237 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
238 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
239 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
240 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
241 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
242 return VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
243 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
244 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
245 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
246 return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
247 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
248 return VK_PRIMITIVE_TOPOLOGY_PATCH_LIST;
249 default:
250 TCU_THROW(InternalError, "Unknown primitive topology.");
251 }
252 }
253
isPrimitiveTopologyPoint(const VkPrimitiveTopology primitiveTopology)254 inline bool isPrimitiveTopologyPoint(const VkPrimitiveTopology primitiveTopology)
255 {
256 return primitiveTopologyCastToList(primitiveTopology) == VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
257 }
258
isPrimitiveTopologyLine(const VkPrimitiveTopology primitiveTopology)259 inline bool isPrimitiveTopologyLine(const VkPrimitiveTopology primitiveTopology)
260 {
261 return primitiveTopologyCastToList(primitiveTopology) == VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
262 }
263
isPrimitiveTopologyTriangle(const VkPrimitiveTopology primitiveTopology)264 inline bool isPrimitiveTopologyTriangle(const VkPrimitiveTopology primitiveTopology)
265 {
266 return primitiveTopologyCastToList(primitiveTopology) == VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
267 }
268
isPrimitiveTopologyPatch(const VkPrimitiveTopology primitiveTopology)269 inline bool isPrimitiveTopologyPatch(const VkPrimitiveTopology primitiveTopology)
270 {
271 return primitiveTopologyCastToList(primitiveTopology) == VK_PRIMITIVE_TOPOLOGY_PATCH_LIST;
272 }
273
isAllInStage(const VkShaderStageFlags shaderStageFlags,const VkShaderStageFlags stageMask)274 inline bool isAllInStage(const VkShaderStageFlags shaderStageFlags, const VkShaderStageFlags stageMask)
275 {
276 return (shaderStageFlags & stageMask) != 0 && ((shaderStageFlags & ~stageMask) == 0);
277 }
278
isAllComputeStages(const VkShaderStageFlags shaderStageFlags)279 inline bool isAllComputeStages(const VkShaderStageFlags shaderStageFlags)
280 {
281 return isAllInStage(shaderStageFlags, VK_SHADER_STAGE_COMPUTE_BIT);
282 }
283
isAllGraphicsStages(const VkShaderStageFlags shaderStageFlags)284 inline bool isAllGraphicsStages(const VkShaderStageFlags shaderStageFlags)
285 {
286 return isAllInStage(shaderStageFlags, VK_SHADER_STAGE_ALL_GRAPHICS);
287 }
288
289 #ifndef CTS_USES_VULKANSC
isAllRayTracingStages(const VkShaderStageFlags shaderStageFlags)290 inline bool isAllRayTracingStages(const VkShaderStageFlags shaderStageFlags)
291 {
292 const VkShaderStageFlags rayTracingStageFlags =
293 VK_SHADER_STAGE_RAYGEN_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR | VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR |
294 VK_SHADER_STAGE_MISS_BIT_KHR | VK_SHADER_STAGE_INTERSECTION_BIT_KHR | VK_SHADER_STAGE_CALLABLE_BIT_KHR;
295
296 return isAllInStage(shaderStageFlags, rayTracingStageFlags);
297 }
298
isAllMeshShadingStages(const VkShaderStageFlags shaderStageFlags)299 inline bool isAllMeshShadingStages(const VkShaderStageFlags shaderStageFlags)
300 {
301 const VkShaderStageFlags meshStages = (VK_SHADER_STAGE_MESH_BIT_EXT | VK_SHADER_STAGE_TASK_BIT_EXT);
302 return isAllInStage(shaderStageFlags, meshStages);
303 }
304
305 #endif // CTS_USES_VULKANSC
306
307 template <typename T>
308 class StructChainAdder
309 {
310 public:
StructChainAdder(T * baseStruct)311 StructChainAdder(T *baseStruct) : m_baseStruct(baseStruct)
312 {
313 }
314
315 template <typename U>
operator ()(U * nextStruct) const316 void operator()(U *nextStruct) const
317 {
318 nextStruct->pNext = m_baseStruct->pNext;
319 m_baseStruct->pNext = nextStruct;
320 }
321
322 private:
323 T *const m_baseStruct;
324 };
325
326 template <typename T>
makeStructChainAdder(T * baseStruct)327 StructChainAdder<T> makeStructChainAdder(T *baseStruct)
328 {
329 return StructChainAdder<T>(baseStruct);
330 }
331
332 } // namespace vk
333
334 #endif // _VKTYPEUTIL_HPP
335