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