1 #ifndef _VKTGEOMETRYTESTSUTIL_HPP
2 #define _VKTGEOMETRYTESTSUTIL_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 Geometry 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 "vktTestCase.hpp"
34
35 #include "tcuVector.hpp"
36 #include "tcuTexture.hpp"
37
38 #include "deStringUtil.hpp"
39 #include "deUniquePtr.hpp"
40
41 namespace vkt
42 {
43 namespace geometry
44 {
45
46 struct PrimitiveTestSpec
47 {
48 vk::VkPrimitiveTopology primitiveType;
49 const char* name;
50 vk::VkPrimitiveTopology outputType;
51 };
52
53 class Buffer
54 {
55 public:
Buffer(const vk::DeviceInterface & vk,const vk::VkDevice device,vk::Allocator & allocator,const vk::VkBufferCreateInfo & bufferCreateInfo,const vk::MemoryRequirement memoryRequirement)56 Buffer (const vk::DeviceInterface& vk,
57 const vk::VkDevice device,
58 vk::Allocator& allocator,
59 const vk::VkBufferCreateInfo& bufferCreateInfo,
60 const vk::MemoryRequirement memoryRequirement)
61
62 : m_buffer (createBuffer(vk, device, &bufferCreateInfo))
63 , m_allocation (allocator.allocate(getBufferMemoryRequirements(vk, device, *m_buffer), memoryRequirement))
64 {
65 VK_CHECK(vk.bindBufferMemory(device, *m_buffer, m_allocation->getMemory(), m_allocation->getOffset()));
66 }
67
get(void) const68 const vk::VkBuffer& get (void) const { return *m_buffer; }
operator *(void) const69 const vk::VkBuffer& operator* (void) const { return get(); }
getAllocation(void) const70 vk::Allocation& getAllocation (void) const { return *m_allocation; }
71
72 private:
73 const vk::Unique<vk::VkBuffer> m_buffer;
74 const de::UniquePtr<vk::Allocation> m_allocation;
75
76 // "deleted"
77 Buffer (const Buffer&);
78 Buffer& operator= (const Buffer&);
79 };
80
81 class Image
82 {
83 public:
Image(const vk::DeviceInterface & vk,const vk::VkDevice device,vk::Allocator & allocator,const vk::VkImageCreateInfo & imageCreateInfo,const vk::MemoryRequirement memoryRequirement)84 Image (const vk::DeviceInterface& vk,
85 const vk::VkDevice device,
86 vk::Allocator& allocator,
87 const vk::VkImageCreateInfo& imageCreateInfo,
88 const vk::MemoryRequirement memoryRequirement)
89
90 : m_image (createImage(vk, device, &imageCreateInfo))
91 , m_allocation (allocator.allocate(getImageMemoryRequirements(vk, device, *m_image), memoryRequirement))
92 {
93 VK_CHECK(vk.bindImageMemory(device, *m_image, m_allocation->getMemory(), m_allocation->getOffset()));
94 }
95
get(void) const96 const vk::VkImage& get (void) const { return *m_image; }
operator *(void) const97 const vk::VkImage& operator* (void) const { return get(); }
getAllocation(void) const98 vk::Allocation& getAllocation (void) const { return *m_allocation; }
99
100 private:
101 const vk::Unique<vk::VkImage> m_image;
102 const de::UniquePtr<vk::Allocation> m_allocation;
103
104 // "deleted"
105 Image (const Image&);
106 Image& operator= (const Image&);
107 };
108
109 class GraphicsPipelineBuilder
110 {
111 public:
GraphicsPipelineBuilder(void)112 GraphicsPipelineBuilder (void) : m_renderSize (0, 0)
113 , m_shaderStageFlags (0u)
114 , m_cullModeFlags (vk::VK_CULL_MODE_NONE)
115 , m_frontFace (vk::VK_FRONT_FACE_COUNTER_CLOCKWISE)
116 , m_patchControlPoints (1u)
117 , m_blendEnable (false)
118 , m_primitiveTopology (vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST) {}
119
setRenderSize(const tcu::IVec2 & size)120 GraphicsPipelineBuilder& setRenderSize (const tcu::IVec2& size) { m_renderSize = size; return *this; }
121 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)122 GraphicsPipelineBuilder& setPatchControlPoints (const deUint32 controlPoints) { m_patchControlPoints = controlPoints; return *this; }
setCullModeFlags(const vk::VkCullModeFlags cullModeFlags)123 GraphicsPipelineBuilder& setCullModeFlags (const vk::VkCullModeFlags cullModeFlags) { m_cullModeFlags = cullModeFlags; return *this; }
setFrontFace(const vk::VkFrontFace frontFace)124 GraphicsPipelineBuilder& setFrontFace (const vk::VkFrontFace frontFace) { m_frontFace = frontFace; return *this; }
setBlend(const bool enable)125 GraphicsPipelineBuilder& setBlend (const bool enable) { m_blendEnable = enable; return *this; }
126
127 //! Applies only to pipelines without tessellation shaders.
setPrimitiveTopology(const vk::VkPrimitiveTopology topology)128 GraphicsPipelineBuilder& setPrimitiveTopology (const vk::VkPrimitiveTopology topology) { m_primitiveTopology = topology; return *this; }
129
addVertexBinding(const vk::VkVertexInputBindingDescription vertexBinding)130 GraphicsPipelineBuilder& addVertexBinding (const vk::VkVertexInputBindingDescription vertexBinding) { m_vertexInputBindings.push_back(vertexBinding); return *this; }
addVertexAttribute(const vk::VkVertexInputAttributeDescription vertexAttribute)131 GraphicsPipelineBuilder& addVertexAttribute (const vk::VkVertexInputAttributeDescription vertexAttribute) { m_vertexInputAttributes.push_back(vertexAttribute); return *this; }
132
133 //! Basic vertex input configuration (uses biding 0, location 0, etc.)
134 GraphicsPipelineBuilder& setVertexInputSingleAttribute (const vk::VkFormat vertexFormat, const deUint32 stride);
135
136 vk::Move<vk::VkPipeline> build (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkPipelineLayout pipelineLayout, const vk::VkRenderPass renderPass);
137
138 private:
139 tcu::IVec2 m_renderSize;
140 vk::Move<vk::VkShaderModule> m_vertexShaderModule;
141 vk::Move<vk::VkShaderModule> m_fragmentShaderModule;
142 vk::Move<vk::VkShaderModule> m_geometryShaderModule;
143 vk::Move<vk::VkShaderModule> m_tessControlShaderModule;
144 vk::Move<vk::VkShaderModule> m_tessEvaluationShaderModule;
145 std::vector<vk::VkPipelineShaderStageCreateInfo> m_shaderStages;
146 std::vector<vk::VkVertexInputBindingDescription> m_vertexInputBindings;
147 std::vector<vk::VkVertexInputAttributeDescription> m_vertexInputAttributes;
148 vk::VkShaderStageFlags m_shaderStageFlags;
149 vk::VkCullModeFlags m_cullModeFlags;
150 vk::VkFrontFace m_frontFace;
151 deUint32 m_patchControlPoints;
152 bool m_blendEnable;
153 vk::VkPrimitiveTopology m_primitiveTopology;
154
155 GraphicsPipelineBuilder (const GraphicsPipelineBuilder&); // "deleted"
156 GraphicsPipelineBuilder& operator= (const GraphicsPipelineBuilder&);
157 };
158
159 template<typename T>
sizeInBytes(const std::vector<T> & vec)160 inline std::size_t sizeInBytes (const std::vector<T>& vec)
161 {
162 return vec.size() * sizeof(vec[0]);
163 }
164
165 std::string inputTypeToGLString (const vk::VkPrimitiveTopology& inputType);
166 std::string outputTypeToGLString (const vk::VkPrimitiveTopology& outputType);
167 std::size_t calcOutputVertices (const vk::VkPrimitiveTopology& inputType);
168
169 vk::VkBufferCreateInfo makeBufferCreateInfo (const vk::VkDeviceSize bufferSize, const vk::VkBufferUsageFlags usage);
170 vk::VkImageCreateInfo makeImageCreateInfo (const tcu::IVec2& size, const vk::VkFormat format, const vk::VkImageUsageFlags usage, const deUint32 numArrayLayers = 1u);
171 vk::Move<vk::VkDescriptorSet> makeDescriptorSet (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkDescriptorPool descriptorPool, const vk::VkDescriptorSetLayout setLayout);
172 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);
173 vk::VkBufferImageCopy makeBufferImageCopy (const vk::VkExtent3D extent, const vk::VkImageSubresourceLayers subresourceLayers);
174 vk::VkBufferImageCopy makeBufferImageCopy (const vk::VkDeviceSize& bufferOffset, const vk::VkImageSubresourceLayers& imageSubresource, const vk::VkOffset3D& imageOffset, const vk::VkExtent3D& imageExtent);
175 vk::Move<vk::VkPipelineLayout> makePipelineLayout (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkDescriptorSetLayout descriptorSetLayout = DE_NULL);
176 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);
177 de::MovePtr<vk::Allocation> bindImage (const vk::DeviceInterface& vk, const vk::VkDevice device, vk::Allocator& allocator, const vk::VkImage image, const vk::MemoryRequirement requirement);
178 de::MovePtr<vk::Allocation> bindBuffer (const vk::DeviceInterface& vk, const vk::VkDevice device, vk::Allocator& allocator, const vk::VkBuffer buffer, const vk::MemoryRequirement requirement);
179
180 bool compareWithFileImage (Context& context, const tcu::ConstPixelBufferAccess& resultImage, std::string name);
181
182 void fillBuffer (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::Allocation& alloc, const vk::VkDeviceSize size, const vk::VkDeviceSize offset, const vk::VkFormat format, const tcu::Vec4& color);
183 void fillBuffer (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::Allocation& alloc, const vk::VkDeviceSize size, const vk::VkDeviceSize offset, const vk::VkFormat format, const float depth);
184 void zeroBuffer (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::Allocation& alloc, const vk::VkDeviceSize size);
185
186 void checkGeometryShaderSupport (const vk::InstanceInterface& vki, const vk::VkPhysicalDevice physDevice, const int numGeometryShaderInvocations = 0);
187 vk::VkBool32 checkPointSize (const vk::InstanceInterface& vki, const vk::VkPhysicalDevice physDevice);
188
makeBuffer(const vk::DeviceInterface & vk,const vk::VkDevice device,const vk::VkBufferCreateInfo & createInfo)189 inline vk::Move<vk::VkBuffer> makeBuffer (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkBufferCreateInfo& createInfo)
190 {
191 return createBuffer(vk, device, &createInfo);
192 }
193
makeImage(const vk::DeviceInterface & vk,const vk::VkDevice device,const vk::VkImageCreateInfo & createInfo)194 inline vk::Move<vk::VkImage> makeImage (const vk::DeviceInterface& vk, const vk::VkDevice device, const vk::VkImageCreateInfo& createInfo)
195 {
196 return createImage(vk, device, &createInfo);
197 }
198
199 } //vkt
200 } //geometry
201
202 #endif // _VKTGEOMETRYTESTSUTIL_HPP
203