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"
32 DrawTestsBaseClass::DrawTestsBaseClass (Context& context, const char* vertexShaderName, const char* fragmentShaderName)
33 : TestInstance (context)
34 , m_colorAttachmentFormat (vk::VK_FORMAT_R8G8B8A8_UNORM)
35 , m_topology (vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP)
36 , m_vk (context.getDeviceInterface())
37 , m_vertexShaderName (vertexShaderName)
38 , m_fragmentShaderName (fragmentShaderName)
42 void DrawTestsBaseClass::initialize (void)
44 const vk::VkDevice device = m_context.getDevice();
45 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
47 const PipelineLayoutCreateInfo pipelineLayoutCreateInfo;
48 m_pipelineLayout = vk::createPipelineLayout(m_vk, device, &pipelineLayoutCreateInfo);
50 const vk::VkExtent3D targetImageExtent = { WIDTH, HEIGHT, 1 };
51 const ImageCreateInfo targetImageCreateInfo(vk::VK_IMAGE_TYPE_2D, m_colorAttachmentFormat, targetImageExtent, 1, 1, vk::VK_SAMPLE_COUNT_1_BIT,
52 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);
54 m_colorTargetImage = Image::createAndAlloc(m_vk, device, targetImageCreateInfo, m_context.getDefaultAllocator());
56 const ImageViewCreateInfo colorTargetViewInfo(m_colorTargetImage->object(), vk::VK_IMAGE_VIEW_TYPE_2D, m_colorAttachmentFormat);
57 m_colorTargetView = vk::createImageView(m_vk, device, &colorTargetViewInfo);
59 RenderPassCreateInfo renderPassCreateInfo;
60 renderPassCreateInfo.addAttachment(AttachmentDescription(m_colorAttachmentFormat,
61 vk::VK_SAMPLE_COUNT_1_BIT,
62 vk::VK_ATTACHMENT_LOAD_OP_LOAD,
63 vk::VK_ATTACHMENT_STORE_OP_STORE,
64 vk::VK_ATTACHMENT_LOAD_OP_DONT_CARE,
65 vk::VK_ATTACHMENT_STORE_OP_STORE,
66 vk::VK_IMAGE_LAYOUT_GENERAL,
67 vk::VK_IMAGE_LAYOUT_GENERAL));
70 const vk::VkAttachmentReference colorAttachmentReference =
73 vk::VK_IMAGE_LAYOUT_GENERAL
76 renderPassCreateInfo.addSubpass(SubpassDescription(vk::VK_PIPELINE_BIND_POINT_GRAPHICS,
81 &colorAttachmentReference,
83 AttachmentReference(),
87 m_renderPass = vk::createRenderPass(m_vk, device, &renderPassCreateInfo);
89 std::vector<vk::VkImageView> colorAttachments(1);
90 colorAttachments[0] = *m_colorTargetView;
92 const FramebufferCreateInfo framebufferCreateInfo(*m_renderPass, colorAttachments, WIDTH, HEIGHT, 1);
94 m_framebuffer = vk::createFramebuffer(m_vk, device, &framebufferCreateInfo);
96 const vk::VkVertexInputBindingDescription vertexInputBindingDescription =
99 (deUint32)sizeof(tcu::Vec4) * 2,
100 vk::VK_VERTEX_INPUT_RATE_VERTEX,
103 const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[2] =
108 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
114 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
115 (deUint32)(sizeof(float)* 4),
119 m_vertexInputState = PipelineCreateInfo::VertexInputState(1,
120 &vertexInputBindingDescription,
122 vertexInputAttributeDescriptions);
124 const vk::VkDeviceSize dataSize = m_data.size() * sizeof(PositionColorVertex);
125 m_vertexBuffer = Buffer::createAndAlloc(m_vk, device, BufferCreateInfo(dataSize,
126 vk::VK_BUFFER_USAGE_VERTEX_BUFFER_BIT), m_context.getDefaultAllocator(), vk::MemoryRequirement::HostVisible);
128 deUint8* ptr = reinterpret_cast<deUint8*>(m_vertexBuffer->getBoundMemory().getHostPtr());
129 deMemcpy(ptr, &m_data[0], static_cast<size_t>(dataSize));
131 vk::flushMappedMemoryRange(m_vk,
133 m_vertexBuffer->getBoundMemory().getMemory(),
134 m_vertexBuffer->getBoundMemory().getOffset(),
137 const CmdPoolCreateInfo cmdPoolCreateInfo(queueFamilyIndex);
138 m_cmdPool = vk::createCommandPool(m_vk, device, &cmdPoolCreateInfo);
140 const vk::VkCommandBufferAllocateInfo cmdBufferAllocateInfo =
142 vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // VkStructureType sType;
143 DE_NULL, // const void* pNext;
144 *m_cmdPool, // VkCommandPool commandPool;
145 vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY, // VkCommandBufferLevel level;
146 1u, // deUint32 bufferCount;
148 m_cmdBuffer = vk::allocateCommandBuffer(m_vk, device, &cmdBufferAllocateInfo);
150 initPipeline(device);
153 void DrawTestsBaseClass::initPipeline (const vk::VkDevice device)
155 const vk::Unique<vk::VkShaderModule> vs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_vertexShaderName), 0));
156 const vk::Unique<vk::VkShaderModule> fs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_fragmentShaderName), 0));
158 const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
160 vk::VkViewport viewport;
163 viewport.width = static_cast<float>(WIDTH);
164 viewport.height = static_cast<float>(HEIGHT);
165 viewport.minDepth = 0.0f;
166 viewport.maxDepth = 1.0f;
168 vk::VkRect2D scissor;
169 scissor.offset.x = 0;
170 scissor.offset.y = 0;
171 scissor.extent.width = WIDTH;
172 scissor.extent.height = HEIGHT;
174 PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
175 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
176 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
177 pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
178 pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
179 pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &vkCbAttachmentState));
180 pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1, std::vector<vk::VkViewport>(1, viewport), std::vector<vk::VkRect2D>(1, scissor)));
181 pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
182 pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
183 pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
185 m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
188 void DrawTestsBaseClass::beginRenderPass (void)
190 const vk::VkClearColorValue clearColor = { { 0.0f, 0.0f, 0.0f, 1.0f } };
191 const CmdBufferBeginInfo beginInfo;
193 m_vk.beginCommandBuffer(*m_cmdBuffer, &beginInfo);
195 initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL);
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, 1, &subresourceRange);
201 const vk::VkMemoryBarrier memBarrier =
203 vk::VK_STRUCTURE_TYPE_MEMORY_BARRIER,
205 vk::VK_ACCESS_TRANSFER_WRITE_BIT,
206 vk::VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
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);
213 const vk::VkRect2D renderArea = { { 0, 0 }, { WIDTH, HEIGHT } };
214 const RenderPassBeginInfo renderPassBegin(*m_renderPass, *m_framebuffer, renderArea);
216 m_vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBegin, vk::VK_SUBPASS_CONTENTS_INLINE);