1 #ifndef _VKTTESSELLATIONUTIL_HPP
2 #define _VKTTESSELLATIONUTIL_HPP
3 /*------------------------------------------------------------------------
4 * Vulkan Conformance Tests
5 * ------------------------
7 * Copyright (c) 2014 The Android Open Source Project
8 * Copyright (c) 2016 The Khronos Group Inc.
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 * \brief Tessellation Utilities
25 *//*--------------------------------------------------------------------*/
28 #include "vkMemUtil.hpp"
30 #include "vkPrograms.hpp"
31 #include "vkRefUtil.hpp"
32 #include "vkQueryUtil.hpp"
34 #include "tcuVector.hpp"
36 #include "deStringUtil.hpp"
38 #include <algorithm> // sort
39 #include <iterator> // distance
43 namespace tessellation
49 Buffer (const vk::DeviceInterface& vk,
50 const vk::VkDevice device,
51 vk::Allocator& allocator,
52 const vk::VkBufferCreateInfo& bufferCreateInfo,
53 const vk::MemoryRequirement memoryRequirement)
55 : m_buffer (createBuffer(vk, device, &bufferCreateInfo))
56 , m_allocation (allocator.allocate(getBufferMemoryRequirements(vk, device, *m_buffer), memoryRequirement))
58 VK_CHECK(vk.bindBufferMemory(device, *m_buffer, m_allocation->getMemory(), m_allocation->getOffset()));
61 const vk::VkBuffer& get (void) const { return *m_buffer; }
62 const vk::VkBuffer& operator* (void) const { return get(); }
63 vk::Allocation& getAllocation (void) const { return *m_allocation; }
66 const vk::Unique<vk::VkBuffer> m_buffer;
67 const de::UniquePtr<vk::Allocation> m_allocation;
70 Buffer (const Buffer&);
71 Buffer& operator= (const Buffer&);
77 Image (const vk::DeviceInterface& vk,
78 const vk::VkDevice device,
79 vk::Allocator& allocator,
80 const vk::VkImageCreateInfo& imageCreateInfo,
81 const vk::MemoryRequirement memoryRequirement)
83 : m_image (createImage(vk, device, &imageCreateInfo))
84 , m_allocation (allocator.allocate(getImageMemoryRequirements(vk, device, *m_image), memoryRequirement))
86 VK_CHECK(vk.bindImageMemory(device, *m_image, m_allocation->getMemory(), m_allocation->getOffset()));
89 const vk::VkImage& get (void) const { return *m_image; }
90 const vk::VkImage& operator* (void) const { return get(); }
91 vk::Allocation& getAllocation (void) const { return *m_allocation; }
94 const vk::Unique<vk::VkImage> m_image;
95 const de::UniquePtr<vk::Allocation> m_allocation;
99 Image& operator= (const Image&);
102 class GraphicsPipelineBuilder
105 GraphicsPipelineBuilder (void) : m_renderSize (0, 0)
106 , m_shaderStageFlags (0u)
107 , m_cullModeFlags (vk::VK_CULL_MODE_NONE)
108 , m_frontFace (vk::VK_FRONT_FACE_COUNTER_CLOCKWISE)
109 , m_patchControlPoints (1u)
110 , m_blendEnable (false)
111 , m_primitiveTopology (vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST) {}
113 GraphicsPipelineBuilder& setRenderSize (const tcu::IVec2& size) { m_renderSize = size; return *this; }
114 GraphicsPipelineBuilder& setShader (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkShaderStageFlagBits stage, const vk::ProgramBinary& binary, const vk::VkSpecializationInfo* specInfo);
115 GraphicsPipelineBuilder& setPatchControlPoints (const deUint32 controlPoints) { m_patchControlPoints = controlPoints; return *this; }
116 GraphicsPipelineBuilder& setCullModeFlags (const vk::VkCullModeFlags cullModeFlags) { m_cullModeFlags = cullModeFlags; return *this; }
117 GraphicsPipelineBuilder& setFrontFace (const vk::VkFrontFace frontFace) { m_frontFace = frontFace; return *this; }
118 GraphicsPipelineBuilder& setBlend (const bool enable) { m_blendEnable = enable; return *this; }
120 //! Applies only to pipelines without tessellation shaders.
121 GraphicsPipelineBuilder& setPrimitiveTopology (const vk::VkPrimitiveTopology topology) { m_primitiveTopology = topology; return *this; }
123 GraphicsPipelineBuilder& addVertexBinding (const vk::VkVertexInputBindingDescription vertexBinding) { m_vertexInputBindings.push_back(vertexBinding); return *this; }
124 GraphicsPipelineBuilder& addVertexAttribute (const vk::VkVertexInputAttributeDescription vertexAttribute) { m_vertexInputAttributes.push_back(vertexAttribute); return *this; }
126 //! Basic vertex input configuration (uses biding 0, location 0, etc.)
127 GraphicsPipelineBuilder& setVertexInputSingleAttribute (const vk::VkFormat vertexFormat, const deUint32 stride);
129 vk::Move<vk::VkPipeline> build (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkPipelineLayout pipelineLayout, const vk::VkRenderPass renderPass);
132 tcu::IVec2 m_renderSize;
133 vk::Move<vk::VkShaderModule> m_vertexShaderModule;
134 vk::Move<vk::VkShaderModule> m_fragmentShaderModule;
135 vk::Move<vk::VkShaderModule> m_geometryShaderModule;
136 vk::Move<vk::VkShaderModule> m_tessControlShaderModule;
137 vk::Move<vk::VkShaderModule> m_tessEvaluationShaderModule;
138 std::vector<vk::VkPipelineShaderStageCreateInfo> m_shaderStages;
139 std::vector<vk::VkVertexInputBindingDescription> m_vertexInputBindings;
140 std::vector<vk::VkVertexInputAttributeDescription> m_vertexInputAttributes;
141 vk::VkShaderStageFlags m_shaderStageFlags;
142 vk::VkCullModeFlags m_cullModeFlags;
143 vk::VkFrontFace m_frontFace;
144 deUint32 m_patchControlPoints;
146 vk::VkPrimitiveTopology m_primitiveTopology;
148 GraphicsPipelineBuilder (const GraphicsPipelineBuilder&); // "deleted"
149 GraphicsPipelineBuilder& operator= (const GraphicsPipelineBuilder&);
158 enum TessPrimitiveType
160 TESSPRIMITIVETYPE_TRIANGLES = 0,
161 TESSPRIMITIVETYPE_QUADS,
162 TESSPRIMITIVETYPE_ISOLINES,
164 TESSPRIMITIVETYPE_LAST,
169 SPACINGMODE_EQUAL = 0,
170 SPACINGMODE_FRACTIONAL_ODD,
171 SPACINGMODE_FRACTIONAL_EVEN,
186 FEATURE_TESSELLATION_SHADER = 1u << 0,
187 FEATURE_GEOMETRY_SHADER = 1u << 1,
188 FEATURE_SHADER_FLOAT_64 = 1u << 2,
189 FEATURE_VERTEX_PIPELINE_STORES_AND_ATOMICS = 1u << 3,
190 FEATURE_FRAGMENT_STORES_AND_ATOMICS = 1u << 4,
191 FEATURE_SHADER_TESSELLATION_AND_GEOMETRY_POINT_SIZE = 1u << 5,
193 typedef deUint32 FeatureFlags;
195 vk::VkBufferCreateInfo makeBufferCreateInfo (const vk::VkDeviceSize bufferSize, const vk::VkBufferUsageFlags usage);
196 vk::VkImageCreateInfo makeImageCreateInfo (const tcu::IVec2& size, const vk::VkFormat format, const vk::VkImageUsageFlags usage, const deUint32 numArrayLayers);
197 vk::Move<vk::VkCommandPool> makeCommandPool (const vk::DeviceInterface& vk, const vk::VkDevice device, const deUint32 queueFamilyIndex);
198 vk::Move<vk::VkDescriptorSet> makeDescriptorSet (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkDescriptorPool descriptorPool, const vk::VkDescriptorSetLayout setLayout);
199 vk::Move<vk::VkPipelineLayout> makePipelineLayout (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkDescriptorSetLayout descriptorSetLayout);
200 vk::Move<vk::VkPipelineLayout> makePipelineLayoutWithoutDescriptors (const vk::DeviceInterface& vk, const vk::VkDevice device);
201 vk::Move<vk::VkPipeline> makeComputePipeline (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkPipelineLayout pipelineLayout, const vk::VkShaderModule shaderModule, const vk::VkSpecializationInfo* specInfo);
202 vk::Move<vk::VkRenderPass> makeRenderPass (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkFormat colorFormat);
203 vk::Move<vk::VkRenderPass> makeRenderPassWithoutAttachments (const vk::DeviceInterface& vk, const vk::VkDevice device);
204 vk::Move<vk::VkFramebuffer> makeFramebuffer (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkRenderPass renderPass, const vk::VkImageView colorAttachment, const deUint32 width, const deUint32 height, const deUint32 layers);
205 vk::Move<vk::VkFramebuffer> makeFramebufferWithoutAttachments (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkRenderPass renderPass);
206 vk::Move<vk::VkImageView> makeImageView (const vk::DeviceInterface& vk, const vk::VkDevice vkDevice, const vk::VkImage image, const vk::VkImageViewType viewType, const vk::VkFormat format, const vk::VkImageSubresourceRange subresourceRange);
207 vk::VkBufferImageCopy makeBufferImageCopy (const vk::VkExtent3D extent, const vk::VkImageSubresourceLayers subresourceLayers);
208 vk::VkBufferMemoryBarrier makeBufferMemoryBarrier (const vk::VkAccessFlags srcAccessMask, const vk::VkAccessFlags dstAccessMask, const vk::VkBuffer buffer, const vk::VkDeviceSize offset, const vk::VkDeviceSize bufferSizeBytes);
209 vk::VkImageMemoryBarrier makeImageMemoryBarrier (const vk::VkAccessFlags srcAccessMask, const vk::VkAccessFlags dstAccessMask, const vk::VkImageLayout oldLayout, const vk::VkImageLayout newLayout, const vk::VkImage image, const vk::VkImageSubresourceRange subresourceRange);
211 void beginCommandBuffer (const vk::DeviceInterface& vk, const vk::VkCommandBuffer commandBuffer);
212 void endCommandBuffer (const vk::DeviceInterface& vk, const vk::VkCommandBuffer commandBuffer);
213 void submitCommandsAndWait (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkQueue queue, const vk::VkCommandBuffer commandBuffer);
214 void beginRenderPass (const vk::DeviceInterface& vk, const vk::VkCommandBuffer commandBuffer, const vk::VkRenderPass renderPass, const vk::VkFramebuffer framebuffer, const vk::VkRect2D& renderArea, const tcu::Vec4& clearColor);
215 void beginRenderPassWithRasterizationDisabled (const vk::DeviceInterface& vk, const vk::VkCommandBuffer commandBuffer, const vk::VkRenderPass renderPass, const vk::VkFramebuffer framebuffer);
216 void endRenderPass (const vk::DeviceInterface& vk, const vk::VkCommandBuffer commandBuffer);
217 void requireFeatures (const vk::InstanceInterface& vki, const vk::VkPhysicalDevice physDevice, const FeatureFlags flags);
219 float getClampedTessLevel (const SpacingMode mode, const float tessLevel);
220 int getRoundedTessLevel (const SpacingMode mode, const float clampedTessLevel);
221 int getClampedRoundedTessLevel (const SpacingMode mode, const float tessLevel);
222 void getClampedRoundedTriangleTessLevels (const SpacingMode mode, const float* innerSrc, const float* outerSrc, int* innerDst, int* outerDst);
223 void getClampedRoundedQuadTessLevels (const SpacingMode mode, const float* innerSrc, const float* outerSrc, int* innerDst, int* outerDst);
224 void getClampedRoundedIsolineTessLevels (const SpacingMode mode, const float* outerSrc, int* outerDst);
225 int numOuterTessellationLevels (const TessPrimitiveType primitiveType);
226 std::string getTessellationLevelsString (const TessLevels& tessLevels, const TessPrimitiveType primitiveType);
227 std::string getTessellationLevelsString (const float* inner, const float* outer);
228 bool isPatchDiscarded (const TessPrimitiveType primitiveType, const float* outerLevels);
229 std::vector<tcu::Vec3> generateReferenceTriangleTessCoords (const SpacingMode spacingMode, const int inner, const int outer0, const int outer1, const int outer2);
230 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);
231 std::vector<tcu::Vec3> generateReferenceIsolineTessCoords (const int outer0, const int outer1);
232 int referenceVertexCount (const TessPrimitiveType primitiveType, const SpacingMode spacingMode, const bool usePointMode, const float* innerLevels, const float* outerLevels);
233 int referencePrimitiveCount (const TessPrimitiveType primitiveType, const SpacingMode spacingMode, const bool usePointMode, const float* innerLevels, const float* outerLevels);
234 int numVerticesPerPrimitive (const TessPrimitiveType primitiveType, const bool usePointMode);
236 static inline const char* getTessPrimitiveTypeShaderName (const TessPrimitiveType type)
240 case TESSPRIMITIVETYPE_TRIANGLES: return "triangles";
241 case TESSPRIMITIVETYPE_QUADS: return "quads";
242 case TESSPRIMITIVETYPE_ISOLINES: return "isolines";
249 static inline const char* getSpacingModeShaderName (SpacingMode mode)
253 case SPACINGMODE_EQUAL: return "equal_spacing";
254 case SPACINGMODE_FRACTIONAL_ODD: return "fractional_odd_spacing";
255 case SPACINGMODE_FRACTIONAL_EVEN: return "fractional_even_spacing";
262 static inline const char* getWindingShaderName (const Winding winding)
266 case WINDING_CCW: return "ccw";
267 case WINDING_CW: return "cw";
274 static inline const char* getGeometryShaderInputPrimitiveTypeShaderName (const TessPrimitiveType type, const bool usePointMode)
281 case TESSPRIMITIVETYPE_TRIANGLES:
282 case TESSPRIMITIVETYPE_QUADS:
285 case TESSPRIMITIVETYPE_ISOLINES:
294 static inline const char* getGeometryShaderOutputPrimitiveTypeShaderName (const TessPrimitiveType type, const bool usePointMode)
301 case TESSPRIMITIVETYPE_TRIANGLES:
302 case TESSPRIMITIVETYPE_QUADS:
303 return "triangle_strip";
305 case TESSPRIMITIVETYPE_ISOLINES:
315 inline std::size_t sizeInBytes (const std::vector<T>& vec)
317 return vec.size() * sizeof(vec[0]);
320 template <typename T>
321 static std::vector<T> sorted (const std::vector<T>& unsorted)
323 std::vector<T> result = unsorted;
324 std::sort(result.begin(), result.end());
328 template <typename T, typename P>
329 static std::vector<T> sorted (const std::vector<T>& unsorted, P pred)
331 std::vector<T> result = unsorted;
332 std::sort(result.begin(), result.end(), pred);
336 template <typename IterT>
337 std::string elemsStr (const IterT& begin, const IterT& end, int wrapLengthParam = 0, int numIndentationSpaces = 0)
339 const int bigInt = ~0u/2;
340 const std::string baseIndentation = std::string(numIndentationSpaces, ' ');
341 const std::string deepIndentation = baseIndentation + std::string(4, ' ');
342 const int wrapLength = wrapLengthParam > 0 ? wrapLengthParam : bigInt;
343 const int length = static_cast<int>(std::distance(begin, end));
346 if (length > wrapLength)
347 result += "(amount: " + de::toString(length) + ") ";
348 result += std::string() + "{" + (length > wrapLength ? "\n"+deepIndentation : " ");
352 for (IterT it = begin; it != end; ++it)
355 result += std::string() + ", " + (index % wrapLength == 0 ? "\n"+deepIndentation : "");
356 result += de::toString(*it);
360 result += length > wrapLength ? "\n"+baseIndentation : " ";
367 template <typename ContainerT>
368 std::string containerStr (const ContainerT& c, int wrapLengthParam = 0, int numIndentationSpaces = 0)
370 return elemsStr(c.begin(), c.end(), wrapLengthParam, numIndentationSpaces);
373 //! Copy 'count' objects of type T from 'memory' into a vector.
374 //! 'offset' is the offset of first object in memory, and 'stride' is the distance between consecutive objects.
376 std::vector<T> readInterleavedData (const int count, const void* memory, const int offset, const int stride)
378 std::vector<T> results(count);
379 const deUint8* pData = static_cast<const deUint8*>(memory) + offset;
381 for (int i = 0; i < count; ++i)
383 deMemcpy(&results[i], pData, sizeof(T));
393 #endif // _VKTTESSELLATIONUTIL_HPP