1 #ifndef _VKTTESSELLATIONUTIL_HPP
2 #define _VKTTESSELLATIONUTIL_HPP
3 /*------------------------------------------------------------------------
4 * Vulkan Conformance Tests
5 * ------------------------
6 *
7 * Copyright (c) 2014 The Android Open Source Project
8 * Copyright (c) 2016 The Khronos Group Inc.
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 *//*!
23 * \file
24 * \brief Tessellation Utilities
25 *//*--------------------------------------------------------------------*/
26
27 #include "vkDefs.hpp"
28 #include "vkMemUtil.hpp"
29 #include "vkRef.hpp"
30 #include "vkPrograms.hpp"
31 #include "vkRefUtil.hpp"
32 #include "vkQueryUtil.hpp"
33 #include "vkObjUtil.hpp"
34 #include "vktTestCase.hpp"
35
36 #include "tcuVector.hpp"
37 #include "tcuMaybe.hpp"
38
39 #include "deStringUtil.hpp"
40
41 #include <algorithm> // sort
42 #include <iterator> // distance
43
44 namespace vkt
45 {
46 namespace tessellation
47 {
48
49 class GraphicsPipelineBuilder
50 {
51 public:
GraphicsPipelineBuilder(void)52 GraphicsPipelineBuilder (void) : m_renderSize (0, 0)
53 , m_shaderStageFlags (0u)
54 , m_cullModeFlags (vk::VK_CULL_MODE_NONE)
55 , m_frontFace (vk::VK_FRONT_FACE_COUNTER_CLOCKWISE)
56 , m_patchControlPoints (1u)
57 , m_blendEnable (false)
58 , m_primitiveTopology (vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST)
59 , m_tessellationDomainOrigin (tcu::Nothing) {}
60
setRenderSize(const tcu::IVec2 & size)61 GraphicsPipelineBuilder& setRenderSize (const tcu::IVec2& size) { m_renderSize = size; return *this; }
62 GraphicsPipelineBuilder& setShader (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkShaderStageFlagBits stage, const vk::ProgramBinary& binary, const vk::VkSpecializationInfo* specInfo);
setPatchControlPoints(const deUint32 controlPoints)63 GraphicsPipelineBuilder& setPatchControlPoints (const deUint32 controlPoints) { m_patchControlPoints = controlPoints; return *this; }
setCullModeFlags(const vk::VkCullModeFlags cullModeFlags)64 GraphicsPipelineBuilder& setCullModeFlags (const vk::VkCullModeFlags cullModeFlags) { m_cullModeFlags = cullModeFlags; return *this; }
setFrontFace(const vk::VkFrontFace frontFace)65 GraphicsPipelineBuilder& setFrontFace (const vk::VkFrontFace frontFace) { m_frontFace = frontFace; return *this; }
setBlend(const bool enable)66 GraphicsPipelineBuilder& setBlend (const bool enable) { m_blendEnable = enable; return *this; }
67
68 //! Applies only to pipelines without tessellation shaders.
setPrimitiveTopology(const vk::VkPrimitiveTopology topology)69 GraphicsPipelineBuilder& setPrimitiveTopology (const vk::VkPrimitiveTopology topology) { m_primitiveTopology = topology; return *this; }
70
addVertexBinding(const vk::VkVertexInputBindingDescription vertexBinding)71 GraphicsPipelineBuilder& addVertexBinding (const vk::VkVertexInputBindingDescription vertexBinding) { m_vertexInputBindings.push_back(vertexBinding); return *this; }
addVertexAttribute(const vk::VkVertexInputAttributeDescription vertexAttribute)72 GraphicsPipelineBuilder& addVertexAttribute (const vk::VkVertexInputAttributeDescription vertexAttribute) { m_vertexInputAttributes.push_back(vertexAttribute); return *this; }
73
74 //! Basic vertex input configuration (uses biding 0, location 0, etc.)
75 GraphicsPipelineBuilder& setVertexInputSingleAttribute (const vk::VkFormat vertexFormat, const deUint32 stride);
76
77 //! If tessellation domain origin is set, pipeline requires VK__maintenance2
setTessellationDomainOrigin(const vk::VkTessellationDomainOrigin domainOrigin)78 GraphicsPipelineBuilder& setTessellationDomainOrigin (const vk::VkTessellationDomainOrigin domainOrigin) { return setTessellationDomainOrigin(tcu::just(domainOrigin)); }
setTessellationDomainOrigin(const tcu::Maybe<vk::VkTessellationDomainOrigin> & domainOrigin)79 GraphicsPipelineBuilder& setTessellationDomainOrigin (const tcu::Maybe<vk::VkTessellationDomainOrigin>& domainOrigin) { m_tessellationDomainOrigin = domainOrigin; return *this; }
80
81 vk::Move<vk::VkPipeline> build (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkPipelineLayout pipelineLayout, const vk::VkRenderPass renderPass);
82
83 private:
84 tcu::IVec2 m_renderSize;
85 vk::Move<vk::VkShaderModule> m_vertexShaderModule;
86 vk::Move<vk::VkShaderModule> m_fragmentShaderModule;
87 vk::Move<vk::VkShaderModule> m_geometryShaderModule;
88 vk::Move<vk::VkShaderModule> m_tessControlShaderModule;
89 vk::Move<vk::VkShaderModule> m_tessEvaluationShaderModule;
90 std::vector<vk::VkPipelineShaderStageCreateInfo> m_shaderStages;
91 std::vector<vk::VkVertexInputBindingDescription> m_vertexInputBindings;
92 std::vector<vk::VkVertexInputAttributeDescription> m_vertexInputAttributes;
93 vk::VkShaderStageFlags m_shaderStageFlags;
94 vk::VkCullModeFlags m_cullModeFlags;
95 vk::VkFrontFace m_frontFace;
96 deUint32 m_patchControlPoints;
97 bool m_blendEnable;
98 vk::VkPrimitiveTopology m_primitiveTopology;
99 tcu::Maybe<vk::VkTessellationDomainOrigin> m_tessellationDomainOrigin;
100
101 GraphicsPipelineBuilder (const GraphicsPipelineBuilder&); // "deleted"
102 GraphicsPipelineBuilder& operator= (const GraphicsPipelineBuilder&);
103 };
104
105 struct TessLevels
106 {
107 float inner[2];
108 float outer[4];
109 };
110
111 enum TessPrimitiveType
112 {
113 TESSPRIMITIVETYPE_TRIANGLES = 0,
114 TESSPRIMITIVETYPE_QUADS,
115 TESSPRIMITIVETYPE_ISOLINES,
116
117 TESSPRIMITIVETYPE_LAST,
118 };
119
120 enum SpacingMode
121 {
122 SPACINGMODE_EQUAL = 0,
123 SPACINGMODE_FRACTIONAL_ODD,
124 SPACINGMODE_FRACTIONAL_EVEN,
125
126 SPACINGMODE_LAST,
127 };
128
129 enum Winding
130 {
131 WINDING_CCW = 0,
132 WINDING_CW,
133
134 WINDING_LAST,
135 };
136
137 enum ShaderLanguage
138 {
139 SHADER_LANGUAGE_GLSL = 0,
140 SHADER_LANGUAGE_HLSL = 1,
141
142 SHADER_LANGUAGE_LAST,
143 };
144
145 enum FeatureFlagBits
146 {
147 FEATURE_TESSELLATION_SHADER = 1u << 0,
148 FEATURE_GEOMETRY_SHADER = 1u << 1,
149 FEATURE_SHADER_FLOAT_64 = 1u << 2,
150 FEATURE_VERTEX_PIPELINE_STORES_AND_ATOMICS = 1u << 3,
151 FEATURE_FRAGMENT_STORES_AND_ATOMICS = 1u << 4,
152 FEATURE_SHADER_TESSELLATION_AND_GEOMETRY_POINT_SIZE = 1u << 5,
153 };
154 typedef deUint32 FeatureFlags;
155
156 vk::VkImageCreateInfo makeImageCreateInfo (const tcu::IVec2& size, const vk::VkFormat format, const vk::VkImageUsageFlags usage, const deUint32 numArrayLayers);
157 vk::Move<vk::VkRenderPass> makeRenderPassWithoutAttachments (const vk::DeviceInterface& vk, const vk::VkDevice device);
158 vk::VkBufferImageCopy makeBufferImageCopy (const vk::VkExtent3D extent, const vk::VkImageSubresourceLayers subresourceLayers);
159 void requireFeatures (const vk::InstanceInterface& vki, const vk::VkPhysicalDevice physDevice, const FeatureFlags flags);
160 float getClampedTessLevel (const SpacingMode mode, const float tessLevel);
161 int getRoundedTessLevel (const SpacingMode mode, const float clampedTessLevel);
162 int getClampedRoundedTessLevel (const SpacingMode mode, const float tessLevel);
163 void getClampedRoundedTriangleTessLevels (const SpacingMode mode, const float* innerSrc, const float* outerSrc, int* innerDst, int* outerDst);
164 void getClampedRoundedQuadTessLevels (const SpacingMode mode, const float* innerSrc, const float* outerSrc, int* innerDst, int* outerDst);
165 void getClampedRoundedIsolineTessLevels (const SpacingMode mode, const float* outerSrc, int* outerDst);
166 int numOuterTessellationLevels (const TessPrimitiveType primitiveType);
167 std::string getTessellationLevelsString (const TessLevels& tessLevels, const TessPrimitiveType primitiveType);
168 std::string getTessellationLevelsString (const float* inner, const float* outer);
169 bool isPatchDiscarded (const TessPrimitiveType primitiveType, const float* outerLevels);
170 std::vector<tcu::Vec3> generateReferenceTriangleTessCoords (const SpacingMode spacingMode, const int inner, const int outer0, const int outer1, const int outer2);
171 std::vector<tcu::Vec3> generateReferenceQuadTessCoords (const SpacingMode spacingMode, const int inner0, const int inner1, const int outer0, const int outer1, const int outer2, const int outer3);
172 std::vector<tcu::Vec3> generateReferenceIsolineTessCoords (const int outer0, const int outer1);
173 int referenceVertexCount (const TessPrimitiveType primitiveType, const SpacingMode spacingMode, const bool usePointMode, const float* innerLevels, const float* outerLevels);
174 int referencePrimitiveCount (const TessPrimitiveType primitiveType, const SpacingMode spacingMode, const bool usePointMode, const float* innerLevels, const float* outerLevels);
175 int numVerticesPerPrimitive (const TessPrimitiveType primitiveType, const bool usePointMode);
176
getTessPrimitiveTypeShaderName(const TessPrimitiveType type,bool forSpirv=false)177 static inline const char* getTessPrimitiveTypeShaderName (const TessPrimitiveType type, bool forSpirv = false)
178 {
179 static std::string primitiveName[][2] =
180 {
181 // glsl name spirv name
182 { "triangles", "Triangles"},
183 { "quads" , "Quads" },
184 { "isolines" , "Isolines" }
185 };
186
187 if (type >= TESSPRIMITIVETYPE_LAST)
188 {
189 DE_FATAL("Unexpected primitive type.");
190 return DE_NULL;
191 }
192
193 return primitiveName[type][forSpirv].c_str();
194 }
195
getDomainName(const TessPrimitiveType type)196 static inline const char* getDomainName (const TessPrimitiveType type)
197 {
198 switch (type)
199 {
200 case TESSPRIMITIVETYPE_TRIANGLES: return "tri";
201 case TESSPRIMITIVETYPE_QUADS: return "quad";
202 case TESSPRIMITIVETYPE_ISOLINES: return "isoline";
203 default:
204 DE_FATAL("Unexpected primitive type.");
205 return DE_NULL;
206 }
207 }
208
getOutputTopologyName(const TessPrimitiveType type,const Winding winding,const bool usePointMode)209 static inline const char* getOutputTopologyName (const TessPrimitiveType type, const Winding winding, const bool usePointMode)
210 {
211 if (usePointMode)
212 return "point";
213 else if (type == TESSPRIMITIVETYPE_TRIANGLES || type == TESSPRIMITIVETYPE_QUADS)
214 return (winding == WINDING_CCW ? "triangle_ccw" : "triangle_cw");
215 else if (type == TESSPRIMITIVETYPE_ISOLINES)
216 return "line";
217
218 DE_FATAL("Unexpected primitive type.");
219 return DE_NULL;
220 }
221
getSpacingModeShaderName(SpacingMode mode,bool forSpirv=false)222 static inline const char* getSpacingModeShaderName (SpacingMode mode, bool forSpirv = false)
223 {
224 static std::string spacingName[][2] =
225 {
226 // glsl name spirv name
227 { "equal_spacing", "SpacingEqual"},
228 { "fractional_odd_spacing", "SpacingFractionalOdd" },
229 { "fractional_even_spacing", "SpacingFractionalEven" }
230 };
231
232 if (mode >= SPACINGMODE_LAST)
233 {
234 DE_FATAL("Unexpected spacing type.");
235 return DE_NULL;
236 }
237
238 return spacingName[mode][forSpirv].c_str();
239 }
240
getPartitioningShaderName(SpacingMode mode)241 static inline const char* getPartitioningShaderName (SpacingMode mode)
242 {
243 switch (mode)
244 {
245 case SPACINGMODE_EQUAL: return "integer";
246 case SPACINGMODE_FRACTIONAL_ODD: return "fractional_odd";
247 case SPACINGMODE_FRACTIONAL_EVEN: return "fractional_even";
248 default:
249 DE_FATAL("Unexpected spacing mode.");
250 return DE_NULL;
251 }
252 }
253
getWindingShaderName(const Winding winding)254 static inline const char* getWindingShaderName (const Winding winding)
255 {
256 switch (winding)
257 {
258 case WINDING_CCW: return "ccw";
259 case WINDING_CW: return "cw";
260 default:
261 DE_FATAL("Unexpected winding type.");
262 return DE_NULL;
263 }
264 }
265
getShaderLanguageName(const ShaderLanguage language)266 static inline const char* getShaderLanguageName (const ShaderLanguage language)
267 {
268 switch (language)
269 {
270 case SHADER_LANGUAGE_GLSL: return "glsl";
271 case SHADER_LANGUAGE_HLSL: return "hlsl";
272 default:
273 DE_FATAL("Unexpected shader language.");
274 return DE_NULL;
275 }
276 }
277
getGeometryShaderInputPrimitiveTypeShaderName(const TessPrimitiveType type,const bool usePointMode)278 static inline const char* getGeometryShaderInputPrimitiveTypeShaderName (const TessPrimitiveType type, const bool usePointMode)
279 {
280 if (usePointMode)
281 return "points";
282
283 switch (type)
284 {
285 case TESSPRIMITIVETYPE_TRIANGLES:
286 case TESSPRIMITIVETYPE_QUADS:
287 return "triangles";
288
289 case TESSPRIMITIVETYPE_ISOLINES:
290 return "lines";
291
292 default:
293 DE_FATAL("Unexpected primitive type.");
294 return DE_NULL;
295 }
296 }
297
getGeometryShaderOutputPrimitiveTypeShaderName(const TessPrimitiveType type,const bool usePointMode)298 static inline const char* getGeometryShaderOutputPrimitiveTypeShaderName (const TessPrimitiveType type, const bool usePointMode)
299 {
300 if (usePointMode)
301 return "points";
302
303 switch (type)
304 {
305 case TESSPRIMITIVETYPE_TRIANGLES:
306 case TESSPRIMITIVETYPE_QUADS:
307 return "triangle_strip";
308
309 case TESSPRIMITIVETYPE_ISOLINES:
310 return "line_strip";
311
312 default:
313 DE_FATAL("Unexpected primitive type.");
314 return DE_NULL;
315 }
316 }
317
318 #ifndef CTS_USES_VULKANSC
319
getPortability(const Context & context)320 static inline const vk::VkPhysicalDevicePortabilitySubsetFeaturesKHR* getPortability (const Context& context)
321 {
322 if (context.isDeviceFunctionalitySupported("VK_KHR_portability_subset"))
323 return &context.getPortabilitySubsetFeatures();
324 return DE_NULL;
325 }
326
checkIsolines(const vk::VkPhysicalDevicePortabilitySubsetFeaturesKHR & features)327 static inline void checkIsolines (const vk::VkPhysicalDevicePortabilitySubsetFeaturesKHR& features)
328 {
329 if (!features.tessellationIsolines)
330 TCU_THROW(NotSupportedError, "VK_KHR_portability_subset: Tessellation iso lines are not supported by this implementation");
331 }
332
checkPrimitive(const vk::VkPhysicalDevicePortabilitySubsetFeaturesKHR & features,const TessPrimitiveType primitive)333 static inline void checkPrimitive (const vk::VkPhysicalDevicePortabilitySubsetFeaturesKHR& features, const TessPrimitiveType primitive)
334 {
335 if (primitive == TESSPRIMITIVETYPE_ISOLINES)
336 checkIsolines(features);
337 }
338
checkSupportPrimitive(Context & context,const TessPrimitiveType primitive)339 static inline void checkSupportPrimitive (Context& context, const TessPrimitiveType primitive)
340 {
341 if (const vk::VkPhysicalDevicePortabilitySubsetFeaturesKHR* const features = getPortability(context))
342 checkPrimitive(*features, primitive);
343 }
344
checkPointMode(const vk::VkPhysicalDevicePortabilitySubsetFeaturesKHR & features)345 static inline void checkPointMode (const vk::VkPhysicalDevicePortabilitySubsetFeaturesKHR& features)
346 {
347 if (!features.tessellationPointMode)
348 TCU_THROW(NotSupportedError, "VK_KHR_portability_subset: Tessellation point mode is not supported by this implementation");
349 }
350
351 #endif // CTS_USES_VULKANSC
352
353 template<typename T>
sizeInBytes(const std::vector<T> & vec)354 inline std::size_t sizeInBytes (const std::vector<T>& vec)
355 {
356 return vec.size() * sizeof(vec[0]);
357 }
358
359 template <typename T>
sorted(const std::vector<T> & unsorted)360 static std::vector<T> sorted (const std::vector<T>& unsorted)
361 {
362 std::vector<T> result = unsorted;
363 std::sort(result.begin(), result.end());
364 return result;
365 }
366
367 template <typename T, typename P>
sorted(const std::vector<T> & unsorted,P pred)368 static std::vector<T> sorted (const std::vector<T>& unsorted, P pred)
369 {
370 std::vector<T> result = unsorted;
371 std::sort(result.begin(), result.end(), pred);
372 return result;
373 }
374
375 template <typename IterT>
elemsStr(const IterT & begin,const IterT & end,int wrapLengthParam=0,int numIndentationSpaces=0)376 std::string elemsStr (const IterT& begin, const IterT& end, int wrapLengthParam = 0, int numIndentationSpaces = 0)
377 {
378 const int bigInt = ~0u/2;
379 const std::string baseIndentation = std::string(numIndentationSpaces, ' ');
380 const std::string deepIndentation = baseIndentation + std::string(4, ' ');
381 const int wrapLength = wrapLengthParam > 0 ? wrapLengthParam : bigInt;
382 const int length = static_cast<int>(std::distance(begin, end));
383 std::string result;
384
385 if (length > wrapLength)
386 result += "(amount: " + de::toString(length) + ") ";
387 result += std::string() + "{" + (length > wrapLength ? "\n"+deepIndentation : " ");
388
389 {
390 int index = 0;
391 for (IterT it = begin; it != end; ++it)
392 {
393 if (it != begin)
394 result += std::string() + ", " + (index % wrapLength == 0 ? "\n"+deepIndentation : "");
395 result += de::toString(*it);
396 index++;
397 }
398
399 result += length > wrapLength ? "\n"+baseIndentation : " ";
400 }
401
402 result += "}";
403 return result;
404 }
405
406 template <typename ContainerT>
containerStr(const ContainerT & c,int wrapLengthParam=0,int numIndentationSpaces=0)407 std::string containerStr (const ContainerT& c, int wrapLengthParam = 0, int numIndentationSpaces = 0)
408 {
409 return elemsStr(c.begin(), c.end(), wrapLengthParam, numIndentationSpaces);
410 }
411
412 //! Copy 'count' objects of type T from 'memory' into a vector.
413 //! 'offset' is the offset of first object in memory, and 'stride' is the distance between consecutive objects.
414 template<typename T>
readInterleavedData(const int count,const void * memory,const int offset,const int stride)415 std::vector<T> readInterleavedData (const int count, const void* memory, const int offset, const int stride)
416 {
417 std::vector<T> results(count);
418 const deUint8* pData = static_cast<const deUint8*>(memory) + offset;
419
420 for (int i = 0; i < count; ++i)
421 {
422 deMemcpy(&results[i], pData, sizeof(T));
423 pData += stride;
424 }
425
426 return results;
427 }
428
429 template <typename CaseDef, typename = bool>
430 struct PointMode
431 {
432 #ifndef CTS_USES_VULKANSC
checkvkt::tessellation::PointMode433 static void check(const vk::VkPhysicalDevicePortabilitySubsetFeaturesKHR&, const CaseDef)
434 {
435 }
436 #endif // CTS_USES_VULKANSC
437 };
438
439 template <typename CaseDef>
440 struct PointMode<CaseDef, decltype(CaseDef().usePointMode)>
441 {
442 #ifndef CTS_USES_VULKANSC
checkvkt::tessellation::PointMode443 static void check(const vk::VkPhysicalDevicePortabilitySubsetFeaturesKHR& features, const CaseDef caseDef)
444 {
445 if (caseDef.usePointMode)
446 checkPointMode(features);
447 }
448 #endif // CTS_USES_VULKANSC
449 };
450
451 template <typename CaseDef>
checkSupportCase(Context & context,const CaseDef caseDef)452 void checkSupportCase (Context& context, const CaseDef caseDef)
453 {
454 #ifndef CTS_USES_VULKANSC
455 if (const vk::VkPhysicalDevicePortabilitySubsetFeaturesKHR* const features = getPortability(context))
456 {
457 PointMode<CaseDef>::check(*features, caseDef);
458 checkPrimitive(*features, caseDef.primitiveType);
459 }
460 #else
461 DE_UNREF(context);
462 DE_UNREF(caseDef);
463 #endif // CTS_USES_VULKANSC
464 }
465
466 } // tessellation
467 } // vkt
468
469 #endif // _VKTTESSELLATIONUTIL_HPP
470