1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2015 The Khronos Group Inc.
6 * Copyright (c) 2015 Intel Corporation
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * \brief Command draw Tests - Base Class
23 *//*--------------------------------------------------------------------*/
25 #include "vktDrawBaseClass.hpp"
26 #include "vkQueryUtil.hpp"
27 #include "vkCmdUtil.hpp"
28 #include "vkTypeUtil.hpp"
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)
46 void DrawTestsBaseClass::initialize (void)
48 const vk::VkDevice device = m_context.getDevice();
49 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
51 const PipelineLayoutCreateInfo pipelineLayoutCreateInfo;
52 m_pipelineLayout = vk::createPipelineLayout(m_vk, device, &pipelineLayoutCreateInfo);
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);
58 m_colorTargetImage = Image::createAndAlloc(m_vk, device, targetImageCreateInfo, m_context.getDefaultAllocator(), m_context.getUniversalQueueFamilyIndex());
60 const ImageViewCreateInfo colorTargetViewInfo(m_colorTargetImage->object(), vk::VK_IMAGE_VIEW_TYPE_2D, m_colorAttachmentFormat);
61 m_colorTargetView = vk::createImageView(m_vk, device, &colorTargetViewInfo);
63 // create renderpass and framebuffer only when we are not using dynamic rendering
64 if (!m_groupParams->useDynamicRendering)
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));
76 const vk::VkAttachmentReference colorAttachmentReference
79 vk::VK_IMAGE_LAYOUT_GENERAL
82 renderPassCreateInfo.addSubpass(SubpassDescription(vk::VK_PIPELINE_BIND_POINT_GRAPHICS,
87 &colorAttachmentReference,
89 AttachmentReference(),
93 m_renderPass = vk::createRenderPass(m_vk, device, &renderPassCreateInfo);
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);
101 const vk::VkVertexInputBindingDescription vertexInputBindingDescription =
104 sizeof(VertexElementData),
105 vk::VK_VERTEX_INPUT_RATE_VERTEX,
108 const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[] =
113 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
115 }, // VertexElementData::position
119 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
120 static_cast<deUint32>(sizeof(tcu::Vec4))
121 }, // VertexElementData::color
125 vk::VK_FORMAT_R32_SINT,
126 static_cast<deUint32>(sizeof(tcu::Vec4)) * 2
127 } // VertexElementData::refVertexIndex
130 m_vertexInputState = PipelineCreateInfo::VertexInputState(1,
131 &vertexInputBindingDescription,
132 DE_LENGTH_OF_ARRAY(vertexInputAttributeDescriptions),
133 vertexInputAttributeDescriptions);
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);
139 deUint8* ptr = reinterpret_cast<deUint8*>(m_vertexBuffer->getBoundMemory().getHostPtr());
140 deMemcpy(ptr, &m_data[0], static_cast<size_t>(dataSize));
142 vk::flushAlloc(m_vk, device, m_vertexBuffer->getBoundMemory());
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);
148 if (m_groupParams->useSecondaryCmdBuffer)
149 m_secCmdBuffer = vk::allocateCommandBuffer(m_vk, device, *m_cmdPool, vk::VK_COMMAND_BUFFER_LEVEL_SECONDARY);
151 initPipeline(device);
154 void DrawTestsBaseClass::initPipeline (const vk::VkDevice device)
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));
159 const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
161 vk::VkViewport viewport = vk::makeViewport(WIDTH, HEIGHT);
162 vk::VkRect2D scissor = vk::makeRect2D(WIDTH, HEIGHT);
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());
175 #ifndef CTS_USES_VULKANSC
176 vk::VkPipelineRenderingCreateInfoKHR renderingCreateInfo
178 vk::VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
182 &m_colorAttachmentFormat,
183 vk::VK_FORMAT_UNDEFINED,
184 vk::VK_FORMAT_UNDEFINED
187 if (m_groupParams->useDynamicRendering)
188 pipelineCreateInfo.pNext = &renderingCreateInfo;
189 #endif // CTS_USES_VULKANSC
191 m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
194 void DrawTestsBaseClass::preRenderBarriers(void)
196 const vk::VkClearValue clearColor { { { 0.0f, 0.0f, 0.0f, 1.0f } } };
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);
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);
205 const vk::VkMemoryBarrier memBarrier
207 vk::VK_STRUCTURE_TYPE_MEMORY_BARRIER,
209 vk::VK_ACCESS_TRANSFER_WRITE_BIT,
210 vk::VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
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);
218 void DrawTestsBaseClass::beginLegacyRender (vk::VkCommandBuffer cmdBuffer, const vk::VkSubpassContents content)
220 const vk::VkRect2D renderArea = vk::makeRect2D(WIDTH, HEIGHT);
222 vk::beginRenderPass(m_vk, cmdBuffer, *m_renderPass, *m_framebuffer, renderArea, content);
225 void DrawTestsBaseClass::endLegacyRender (vk::VkCommandBuffer cmdBuffer)
227 vk::endRenderPass(m_vk, cmdBuffer);
230 #ifndef CTS_USES_VULKANSC
231 void DrawTestsBaseClass::beginSecondaryCmdBuffer(const vk::DeviceInterface& vk, const vk::VkRenderingFlagsKHR renderingFlags)
233 vk::VkCommandBufferInheritanceRenderingInfoKHR inheritanceRenderingInfo
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;
245 const vk::VkCommandBufferInheritanceInfo bufferInheritanceInfo = vk::initVulkanStructure(&inheritanceRenderingInfo);
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;
251 const vk::VkCommandBufferBeginInfo commandBufBeginParams
253 vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, // VkStructureType sType;
254 DE_NULL, // const void* pNext;
255 usageFlags, // VkCommandBufferUsageFlags flags;
256 &bufferInheritanceInfo
259 VK_CHECK(vk.beginCommandBuffer(*m_secCmdBuffer, &commandBufBeginParams));
262 void DrawTestsBaseClass::beginDynamicRender(vk::VkCommandBuffer cmdBuffer, const vk::VkRenderingFlagsKHR renderingFlags)
264 const vk::VkClearValue clearColor{ { { 0.0f, 0.0f, 0.0f, 1.0f } } };
265 const vk::VkRect2D renderArea = vk::makeRect2D(WIDTH, HEIGHT);
267 vk::beginRendering(m_vk, cmdBuffer, *m_colorTargetView, renderArea, clearColor, vk::VK_IMAGE_LAYOUT_GENERAL, vk::VK_ATTACHMENT_LOAD_OP_LOAD, renderingFlags);
270 void DrawTestsBaseClass::endDynamicRender(vk::VkCommandBuffer cmdBuffer)
272 vk::endRendering(m_vk, cmdBuffer);
274 #endif // CTS_USES_VULKANSC