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 "vkQueryUtil.hpp"
27 #include "vkCmdUtil.hpp"
28 #include "vkTypeUtil.hpp"
29
30 namespace vkt
31 {
32 namespace Draw
33 {
34
DrawTestsBaseClass(Context & context,const char * vertexShaderName,const char * fragmentShaderName,const SharedGroupParams groupParams,vk::VkPrimitiveTopology topology)35 DrawTestsBaseClass::DrawTestsBaseClass (Context& context, const char* vertexShaderName, const char* fragmentShaderName, const SharedGroupParams groupParams, vk::VkPrimitiveTopology topology)
36 : TestInstance (context)
37 , m_colorAttachmentFormat (vk::VK_FORMAT_R8G8B8A8_UNORM)
38 , m_groupParams (groupParams)
39 , m_topology (topology)
40 , m_vk (context.getDeviceInterface())
41 , m_vertexShaderName (vertexShaderName)
42 , m_fragmentShaderName (fragmentShaderName)
43 {
44 }
45
initialize(void)46 void DrawTestsBaseClass::initialize (void)
47 {
48 const vk::VkDevice device = m_context.getDevice();
49 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
50
51 const PipelineLayoutCreateInfo pipelineLayoutCreateInfo;
52 m_pipelineLayout = vk::createPipelineLayout(m_vk, device, &pipelineLayoutCreateInfo);
53
54 const vk::VkExtent3D targetImageExtent = { WIDTH, HEIGHT, 1 };
55 const ImageCreateInfo targetImageCreateInfo(vk::VK_IMAGE_TYPE_2D, m_colorAttachmentFormat, targetImageExtent, 1, 1, vk::VK_SAMPLE_COUNT_1_BIT,
56 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);
57
58 m_colorTargetImage = Image::createAndAlloc(m_vk, device, targetImageCreateInfo, m_context.getDefaultAllocator(), m_context.getUniversalQueueFamilyIndex());
59
60 const ImageViewCreateInfo colorTargetViewInfo(m_colorTargetImage->object(), vk::VK_IMAGE_VIEW_TYPE_2D, m_colorAttachmentFormat);
61 m_colorTargetView = vk::createImageView(m_vk, device, &colorTargetViewInfo);
62
63 // create renderpass and framebuffer only when we are not using dynamic rendering
64 if (!m_groupParams->useDynamicRendering)
65 {
66 RenderPassCreateInfo renderPassCreateInfo;
67 renderPassCreateInfo.addAttachment(AttachmentDescription(m_colorAttachmentFormat,
68 vk::VK_SAMPLE_COUNT_1_BIT,
69 vk::VK_ATTACHMENT_LOAD_OP_LOAD,
70 vk::VK_ATTACHMENT_STORE_OP_STORE,
71 vk::VK_ATTACHMENT_LOAD_OP_DONT_CARE,
72 vk::VK_ATTACHMENT_STORE_OP_STORE,
73 vk::VK_IMAGE_LAYOUT_GENERAL,
74 vk::VK_IMAGE_LAYOUT_GENERAL));
75
76 const vk::VkAttachmentReference colorAttachmentReference
77 {
78 0,
79 vk::VK_IMAGE_LAYOUT_GENERAL
80 };
81
82 renderPassCreateInfo.addSubpass(SubpassDescription(vk::VK_PIPELINE_BIND_POINT_GRAPHICS,
83 0,
84 0,
85 DE_NULL,
86 1,
87 &colorAttachmentReference,
88 DE_NULL,
89 AttachmentReference(),
90 0,
91 DE_NULL));
92
93 m_renderPass = vk::createRenderPass(m_vk, device, &renderPassCreateInfo);
94
95 // create framebuffer
96 std::vector<vk::VkImageView> colorAttachments { *m_colorTargetView };
97 const FramebufferCreateInfo framebufferCreateInfo (*m_renderPass, colorAttachments, WIDTH, HEIGHT, 1);
98 m_framebuffer = vk::createFramebuffer(m_vk, device, &framebufferCreateInfo);
99 }
100
101 const vk::VkVertexInputBindingDescription vertexInputBindingDescription =
102 {
103 0,
104 sizeof(VertexElementData),
105 vk::VK_VERTEX_INPUT_RATE_VERTEX,
106 };
107
108 const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[] =
109 {
110 {
111 0u,
112 0u,
113 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
114 0u
115 }, // VertexElementData::position
116 {
117 1u,
118 0u,
119 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
120 static_cast<deUint32>(sizeof(tcu::Vec4))
121 }, // VertexElementData::color
122 {
123 2u,
124 0u,
125 vk::VK_FORMAT_R32_SINT,
126 static_cast<deUint32>(sizeof(tcu::Vec4)) * 2
127 } // VertexElementData::refVertexIndex
128 };
129
130 m_vertexInputState = PipelineCreateInfo::VertexInputState(1,
131 &vertexInputBindingDescription,
132 DE_LENGTH_OF_ARRAY(vertexInputAttributeDescriptions),
133 vertexInputAttributeDescriptions);
134
135 const vk::VkDeviceSize dataSize = m_data.size() * sizeof(VertexElementData);
136 m_vertexBuffer = Buffer::createAndAlloc(m_vk, device, BufferCreateInfo(dataSize,
137 vk::VK_BUFFER_USAGE_VERTEX_BUFFER_BIT), m_context.getDefaultAllocator(), vk::MemoryRequirement::HostVisible);
138
139 deUint8* ptr = reinterpret_cast<deUint8*>(m_vertexBuffer->getBoundMemory().getHostPtr());
140 deMemcpy(ptr, &m_data[0], static_cast<size_t>(dataSize));
141
142 vk::flushAlloc(m_vk, device, m_vertexBuffer->getBoundMemory());
143
144 const CmdPoolCreateInfo cmdPoolCreateInfo(queueFamilyIndex);
145 m_cmdPool = vk::createCommandPool(m_vk, device, &cmdPoolCreateInfo);
146 m_cmdBuffer = vk::allocateCommandBuffer(m_vk, device, *m_cmdPool, vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY);
147
148 if (m_groupParams->useSecondaryCmdBuffer)
149 m_secCmdBuffer = vk::allocateCommandBuffer(m_vk, device, *m_cmdPool, vk::VK_COMMAND_BUFFER_LEVEL_SECONDARY);
150
151 initPipeline(device);
152 }
153
initPipeline(const vk::VkDevice device)154 void DrawTestsBaseClass::initPipeline (const vk::VkDevice device)
155 {
156 const vk::Unique<vk::VkShaderModule> vs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_vertexShaderName), 0));
157 const vk::Unique<vk::VkShaderModule> fs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_fragmentShaderName), 0));
158
159 const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
160
161 vk::VkViewport viewport = vk::makeViewport(WIDTH, HEIGHT);
162 vk::VkRect2D scissor = vk::makeRect2D(WIDTH, HEIGHT);
163
164 PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
165 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
166 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
167 pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
168 pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
169 pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &vkCbAttachmentState));
170 pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1, std::vector<vk::VkViewport>(1, viewport), std::vector<vk::VkRect2D>(1, scissor)));
171 pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
172 pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
173 pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
174
175 #ifndef CTS_USES_VULKANSC
176 vk::VkPipelineRenderingCreateInfoKHR renderingCreateInfo
177 {
178 vk::VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
179 DE_NULL,
180 0u,
181 1u,
182 &m_colorAttachmentFormat,
183 vk::VK_FORMAT_UNDEFINED,
184 vk::VK_FORMAT_UNDEFINED
185 };
186
187 if (m_groupParams->useDynamicRendering)
188 pipelineCreateInfo.pNext = &renderingCreateInfo;
189 #endif // CTS_USES_VULKANSC
190
191 m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
192 }
193
preRenderBarriers(void)194 void DrawTestsBaseClass::preRenderBarriers(void)
195 {
196 const vk::VkClearValue clearColor { { { 0.0f, 0.0f, 0.0f, 1.0f } } };
197
198 initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL,
199 vk::VK_ACCESS_TRANSFER_WRITE_BIT, vk::VK_PIPELINE_STAGE_TRANSFER_BIT);
200
201 const ImageSubresourceRange subresourceRange(vk::VK_IMAGE_ASPECT_COLOR_BIT);
202 m_vk.cmdClearColorImage(*m_cmdBuffer, m_colorTargetImage->object(),
203 vk::VK_IMAGE_LAYOUT_GENERAL, &clearColor.color, 1, &subresourceRange);
204
205 const vk::VkMemoryBarrier memBarrier
206 {
207 vk::VK_STRUCTURE_TYPE_MEMORY_BARRIER,
208 DE_NULL,
209 vk::VK_ACCESS_TRANSFER_WRITE_BIT,
210 vk::VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
211 };
212
213 m_vk.cmdPipelineBarrier(*m_cmdBuffer, vk::VK_PIPELINE_STAGE_TRANSFER_BIT,
214 vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
215 0, 1, &memBarrier, 0, DE_NULL, 0, DE_NULL);
216 }
217
beginLegacyRender(vk::VkCommandBuffer cmdBuffer,const vk::VkSubpassContents content)218 void DrawTestsBaseClass::beginLegacyRender (vk::VkCommandBuffer cmdBuffer, const vk::VkSubpassContents content)
219 {
220 const vk::VkRect2D renderArea = vk::makeRect2D(WIDTH, HEIGHT);
221
222 vk::beginRenderPass(m_vk, cmdBuffer, *m_renderPass, *m_framebuffer, renderArea, content);
223 }
224
endLegacyRender(vk::VkCommandBuffer cmdBuffer)225 void DrawTestsBaseClass::endLegacyRender (vk::VkCommandBuffer cmdBuffer)
226 {
227 vk::endRenderPass(m_vk, cmdBuffer);
228 }
229
230 #ifndef CTS_USES_VULKANSC
beginSecondaryCmdBuffer(const vk::DeviceInterface & vk,const vk::VkRenderingFlagsKHR renderingFlags)231 void DrawTestsBaseClass::beginSecondaryCmdBuffer(const vk::DeviceInterface& vk, const vk::VkRenderingFlagsKHR renderingFlags)
232 {
233 vk::VkCommandBufferInheritanceRenderingInfoKHR inheritanceRenderingInfo
234 {
235 vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_RENDERING_INFO_KHR, // VkStructureType sType;
236 DE_NULL, // const void* pNext;
237 renderingFlags, // VkRenderingFlagsKHR flags;
238 0u, // uint32_t viewMask;
239 1u, // uint32_t colorAttachmentCount;
240 &m_colorAttachmentFormat, // const VkFormat* pColorAttachmentFormats;
241 vk::VK_FORMAT_UNDEFINED, // VkFormat depthAttachmentFormat;
242 vk::VK_FORMAT_UNDEFINED, // VkFormat stencilAttachmentFormat;
243 vk::VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits rasterizationSamples;
244 };
245 const vk::VkCommandBufferInheritanceInfo bufferInheritanceInfo = vk::initVulkanStructure(&inheritanceRenderingInfo);
246
247 vk::VkCommandBufferUsageFlags usageFlags = vk::VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
248 if (!m_groupParams->secondaryCmdBufferCompletelyContainsDynamicRenderpass)
249 usageFlags |= vk::VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
250
251 const vk::VkCommandBufferBeginInfo commandBufBeginParams
252 {
253 vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, // VkStructureType sType;
254 DE_NULL, // const void* pNext;
255 usageFlags, // VkCommandBufferUsageFlags flags;
256 &bufferInheritanceInfo
257 };
258
259 VK_CHECK(vk.beginCommandBuffer(*m_secCmdBuffer, &commandBufBeginParams));
260 }
261
beginDynamicRender(vk::VkCommandBuffer cmdBuffer,const vk::VkRenderingFlagsKHR renderingFlags)262 void DrawTestsBaseClass::beginDynamicRender(vk::VkCommandBuffer cmdBuffer, const vk::VkRenderingFlagsKHR renderingFlags)
263 {
264 const vk::VkClearValue clearColor{ { { 0.0f, 0.0f, 0.0f, 1.0f } } };
265 const vk::VkRect2D renderArea = vk::makeRect2D(WIDTH, HEIGHT);
266
267 vk::beginRendering(m_vk, cmdBuffer, *m_colorTargetView, renderArea, clearColor, vk::VK_IMAGE_LAYOUT_GENERAL, vk::VK_ATTACHMENT_LOAD_OP_LOAD, renderingFlags);
268 }
269
endDynamicRender(vk::VkCommandBuffer cmdBuffer)270 void DrawTestsBaseClass::endDynamicRender(vk::VkCommandBuffer cmdBuffer)
271 {
272 vk::endRendering(m_vk, cmdBuffer);
273 }
274 #endif // CTS_USES_VULKANSC
275
276 } // Draw
277 } // vkt
278