• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 The Khronos Group Inc.
6  * Copyright (c) 2015 Intel Corporation
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *//*!
21  * \file
22  * \brief Command draw Tests - Base Class
23  *//*--------------------------------------------------------------------*/
24 
25 #include "vktDrawBaseClass.hpp"
26 #include "vkCmdUtil.hpp"
27 #include "vkTypeUtil.hpp"
28 
29 namespace vkt
30 {
31 namespace Draw
32 {
33 
DrawTestsBaseClass(Context & context,const char * vertexShaderName,const char * fragmentShaderName,bool useDynamicRendering,vk::VkPrimitiveTopology topology)34 DrawTestsBaseClass::DrawTestsBaseClass (Context& context, const char* vertexShaderName, const char* fragmentShaderName, bool useDynamicRendering, vk::VkPrimitiveTopology topology)
35 	: TestInstance				(context)
36 	, m_colorAttachmentFormat	(vk::VK_FORMAT_R8G8B8A8_UNORM)
37 	, m_useDynamicRendering		(useDynamicRendering)
38 	, m_topology				(topology)
39 	, m_vk						(context.getDeviceInterface())
40 	, m_vertexShaderName		(vertexShaderName)
41 	, m_fragmentShaderName		(fragmentShaderName)
42 {
43 }
44 
initialize(void)45 void DrawTestsBaseClass::initialize (void)
46 {
47 	const vk::VkDevice device				= m_context.getDevice();
48 	const deUint32 queueFamilyIndex			= m_context.getUniversalQueueFamilyIndex();
49 
50 	const PipelineLayoutCreateInfo pipelineLayoutCreateInfo;
51 	m_pipelineLayout						= vk::createPipelineLayout(m_vk, device, &pipelineLayoutCreateInfo);
52 
53 	const vk::VkExtent3D targetImageExtent	= { WIDTH, HEIGHT, 1 };
54 	const ImageCreateInfo targetImageCreateInfo(vk::VK_IMAGE_TYPE_2D, m_colorAttachmentFormat, targetImageExtent, 1, 1, vk::VK_SAMPLE_COUNT_1_BIT,
55 		vk::VK_IMAGE_TILING_OPTIMAL, vk::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | vk::VK_IMAGE_USAGE_TRANSFER_SRC_BIT | vk::VK_IMAGE_USAGE_TRANSFER_DST_BIT);
56 
57 	m_colorTargetImage						= Image::createAndAlloc(m_vk, device, targetImageCreateInfo, m_context.getDefaultAllocator(), m_context.getUniversalQueueFamilyIndex());
58 
59 	const ImageViewCreateInfo colorTargetViewInfo(m_colorTargetImage->object(), vk::VK_IMAGE_VIEW_TYPE_2D, m_colorAttachmentFormat);
60 	m_colorTargetView						= vk::createImageView(m_vk, device, &colorTargetViewInfo);
61 
62 	// create renderpass and framebuffer only when we are not using dynamic rendering
63 	if (!m_useDynamicRendering)
64 	{
65 		RenderPassCreateInfo renderPassCreateInfo;
66 		renderPassCreateInfo.addAttachment(AttachmentDescription(m_colorAttachmentFormat,
67 																 vk::VK_SAMPLE_COUNT_1_BIT,
68 																 vk::VK_ATTACHMENT_LOAD_OP_LOAD,
69 																 vk::VK_ATTACHMENT_STORE_OP_STORE,
70 																 vk::VK_ATTACHMENT_LOAD_OP_DONT_CARE,
71 																 vk::VK_ATTACHMENT_STORE_OP_STORE,
72 																 vk::VK_IMAGE_LAYOUT_GENERAL,
73 																 vk::VK_IMAGE_LAYOUT_GENERAL));
74 
75 		const vk::VkAttachmentReference colorAttachmentReference
76 		{
77 			0,
78 			vk::VK_IMAGE_LAYOUT_GENERAL
79 		};
80 
81 		renderPassCreateInfo.addSubpass(SubpassDescription(vk::VK_PIPELINE_BIND_POINT_GRAPHICS,
82 														   0,
83 														   0,
84 														   DE_NULL,
85 														   1,
86 														   &colorAttachmentReference,
87 														   DE_NULL,
88 														   AttachmentReference(),
89 														   0,
90 														   DE_NULL));
91 
92 		m_renderPass = vk::createRenderPass(m_vk, device, &renderPassCreateInfo);
93 
94 		// create framebuffer
95 		std::vector<vk::VkImageView>	colorAttachments		{ *m_colorTargetView };
96 		const FramebufferCreateInfo		framebufferCreateInfo	(*m_renderPass, colorAttachments, WIDTH, HEIGHT, 1);
97 		m_framebuffer = vk::createFramebuffer(m_vk, device, &framebufferCreateInfo);
98 	}
99 
100 	const vk::VkVertexInputBindingDescription vertexInputBindingDescription =
101 	{
102 		0,
103 		sizeof(VertexElementData),
104 		vk::VK_VERTEX_INPUT_RATE_VERTEX,
105 	};
106 
107 	const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[] =
108 	{
109 		{
110 			0u,
111 			0u,
112 			vk::VK_FORMAT_R32G32B32A32_SFLOAT,
113 			0u
114 		},	// VertexElementData::position
115 		{
116 			1u,
117 			0u,
118 			vk::VK_FORMAT_R32G32B32A32_SFLOAT,
119 			static_cast<deUint32>(sizeof(tcu::Vec4))
120 		},  // VertexElementData::color
121 		{
122 			2u,
123 			0u,
124 			vk::VK_FORMAT_R32_SINT,
125 			static_cast<deUint32>(sizeof(tcu::Vec4)) * 2
126 		}   // VertexElementData::refVertexIndex
127 	};
128 
129 	m_vertexInputState = PipelineCreateInfo::VertexInputState(1,
130 															  &vertexInputBindingDescription,
131 															  DE_LENGTH_OF_ARRAY(vertexInputAttributeDescriptions),
132 															  vertexInputAttributeDescriptions);
133 
134 	const vk::VkDeviceSize dataSize = m_data.size() * sizeof(VertexElementData);
135 	m_vertexBuffer = Buffer::createAndAlloc(m_vk, device, BufferCreateInfo(dataSize,
136 		vk::VK_BUFFER_USAGE_VERTEX_BUFFER_BIT), m_context.getDefaultAllocator(), vk::MemoryRequirement::HostVisible);
137 
138 	deUint8* ptr = reinterpret_cast<deUint8*>(m_vertexBuffer->getBoundMemory().getHostPtr());
139 	deMemcpy(ptr, &m_data[0], static_cast<size_t>(dataSize));
140 
141 	vk::flushAlloc(m_vk, device, m_vertexBuffer->getBoundMemory());
142 
143 	const CmdPoolCreateInfo cmdPoolCreateInfo(queueFamilyIndex);
144 	m_cmdPool	= vk::createCommandPool(m_vk, device, &cmdPoolCreateInfo);
145 	m_cmdBuffer	= vk::allocateCommandBuffer(m_vk, device, *m_cmdPool, vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY);
146 
147 	initPipeline(device);
148 }
149 
initPipeline(const vk::VkDevice device)150 void DrawTestsBaseClass::initPipeline (const vk::VkDevice device)
151 {
152 	const vk::Unique<vk::VkShaderModule> vs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_vertexShaderName), 0));
153 	const vk::Unique<vk::VkShaderModule> fs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_fragmentShaderName), 0));
154 
155 	const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
156 
157 	vk::VkViewport viewport	= vk::makeViewport(WIDTH, HEIGHT);
158 	vk::VkRect2D scissor	= vk::makeRect2D(WIDTH, HEIGHT);
159 
160 	PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
161 	pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
162 	pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
163 	pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
164 	pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
165 	pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &vkCbAttachmentState));
166 	pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1, std::vector<vk::VkViewport>(1, viewport), std::vector<vk::VkRect2D>(1, scissor)));
167 	pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
168 	pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
169 	pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
170 
171 	vk::VkPipelineRenderingCreateInfoKHR renderingCreateInfo
172 	{
173 		vk::VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
174 		DE_NULL,
175 		0u,
176 		1u,
177 		&m_colorAttachmentFormat,
178 		vk::VK_FORMAT_UNDEFINED,
179 		vk::VK_FORMAT_UNDEFINED
180 	};
181 
182 	if (m_useDynamicRendering)
183 		pipelineCreateInfo.pNext = &renderingCreateInfo;
184 
185 	m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
186 }
187 
beginRender(const vk::VkSubpassContents content)188 void DrawTestsBaseClass::beginRender (const vk::VkSubpassContents content)
189 {
190 	const vk::VkClearValue clearColor { { { 0.0f, 0.0f, 0.0f, 1.0f } } };
191 
192 	beginCommandBuffer(m_vk, *m_cmdBuffer, 0u);
193 
194 	initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL,
195 								  vk::VK_ACCESS_TRANSFER_WRITE_BIT, vk::VK_PIPELINE_STAGE_TRANSFER_BIT);
196 
197 	const ImageSubresourceRange subresourceRange(vk::VK_IMAGE_ASPECT_COLOR_BIT);
198 	m_vk.cmdClearColorImage(*m_cmdBuffer, m_colorTargetImage->object(),
199 		vk::VK_IMAGE_LAYOUT_GENERAL, &clearColor.color, 1, &subresourceRange);
200 
201 	const vk::VkMemoryBarrier memBarrier =
202 	{
203 		vk::VK_STRUCTURE_TYPE_MEMORY_BARRIER,
204 		DE_NULL,
205 		vk::VK_ACCESS_TRANSFER_WRITE_BIT,
206 		vk::VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
207 	};
208 
209 	m_vk.cmdPipelineBarrier(*m_cmdBuffer, vk::VK_PIPELINE_STAGE_TRANSFER_BIT,
210 		vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
211 		0, 1, &memBarrier, 0, DE_NULL, 0, DE_NULL);
212 
213 	const vk::VkRect2D renderArea = vk::makeRect2D(WIDTH, HEIGHT);
214 	if (m_useDynamicRendering)
215 	{
216 		vk::VkRenderingFlagsKHR renderingFlags = 0;
217 		if (content == vk::VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS)
218 			renderingFlags = vk::VK_RENDERING_CONTENTS_SECONDARY_COMMAND_BUFFERS_BIT_KHR;
219 
220 		vk::beginRendering(m_vk, *m_cmdBuffer, *m_colorTargetView, renderArea, clearColor, vk::VK_IMAGE_LAYOUT_GENERAL, vk::VK_ATTACHMENT_LOAD_OP_LOAD, renderingFlags);
221 	}
222 	else
223 		vk::beginRenderPass(m_vk, *m_cmdBuffer, *m_renderPass, *m_framebuffer, renderArea, content);
224 }
225 
endRender(void)226 void DrawTestsBaseClass::endRender (void)
227 {
228 	if (m_useDynamicRendering)
229 		vk::endRendering(m_vk, *m_cmdBuffer);
230 	else
231 		vk::endRenderPass(m_vk, *m_cmdBuffer);
232 }
233 
234 }	// Draw
235 }	// vkt
236