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, vk::VkPrimitiveTopology topology)
33 : TestInstance (context)
34 , m_colorAttachmentFormat (vk::VK_FORMAT_R8G8B8A8_UNORM)
35 , m_topology (topology)
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(), m_context.getUniversalQueueFamilyIndex());
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 sizeof(VertexElementData),
100 vk::VK_VERTEX_INPUT_RATE_VERTEX,
103 const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[] =
108 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
110 }, // VertexElementData::position
114 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
115 static_cast<deUint32>(sizeof(tcu::Vec4))
116 }, // VertexElementData::color
120 vk::VK_FORMAT_R32_SINT,
121 static_cast<deUint32>(sizeof(tcu::Vec4)) * 2
122 } // VertexElementData::refVertexIndex
125 m_vertexInputState = PipelineCreateInfo::VertexInputState(1,
126 &vertexInputBindingDescription,
127 DE_LENGTH_OF_ARRAY(vertexInputAttributeDescriptions),
128 vertexInputAttributeDescriptions);
130 const vk::VkDeviceSize dataSize = m_data.size() * sizeof(VertexElementData);
131 m_vertexBuffer = Buffer::createAndAlloc(m_vk, device, BufferCreateInfo(dataSize,
132 vk::VK_BUFFER_USAGE_VERTEX_BUFFER_BIT), m_context.getDefaultAllocator(), vk::MemoryRequirement::HostVisible);
134 deUint8* ptr = reinterpret_cast<deUint8*>(m_vertexBuffer->getBoundMemory().getHostPtr());
135 deMemcpy(ptr, &m_data[0], static_cast<size_t>(dataSize));
137 vk::flushMappedMemoryRange(m_vk,
139 m_vertexBuffer->getBoundMemory().getMemory(),
140 m_vertexBuffer->getBoundMemory().getOffset(),
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);
147 initPipeline(device);
150 void DrawTestsBaseClass::initPipeline (const vk::VkDevice device)
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));
155 const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
157 vk::VkViewport viewport;
160 viewport.width = static_cast<float>(WIDTH);
161 viewport.height = static_cast<float>(HEIGHT);
162 viewport.minDepth = 0.0f;
163 viewport.maxDepth = 1.0f;
165 vk::VkRect2D scissor;
166 scissor.offset.x = 0;
167 scissor.offset.y = 0;
168 scissor.extent.width = WIDTH;
169 scissor.extent.height = HEIGHT;
171 PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
172 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
173 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
174 pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
175 pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
176 pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &vkCbAttachmentState));
177 pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1, std::vector<vk::VkViewport>(1, viewport), std::vector<vk::VkRect2D>(1, scissor)));
178 pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
179 pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
180 pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
182 m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
185 void DrawTestsBaseClass::beginRenderPass (void)
187 const vk::VkClearColorValue clearColor = { { 0.0f, 0.0f, 0.0f, 1.0f } };
188 const CmdBufferBeginInfo beginInfo;
190 m_vk.beginCommandBuffer(*m_cmdBuffer, &beginInfo);
192 initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL,
193 vk::VK_ACCESS_TRANSFER_WRITE_BIT, vk::VK_PIPELINE_STAGE_TRANSFER_BIT);
195 const ImageSubresourceRange subresourceRange(vk::VK_IMAGE_ASPECT_COLOR_BIT);
196 m_vk.cmdClearColorImage(*m_cmdBuffer, m_colorTargetImage->object(),
197 vk::VK_IMAGE_LAYOUT_GENERAL, &clearColor, 1, &subresourceRange);
199 const vk::VkMemoryBarrier memBarrier =
201 vk::VK_STRUCTURE_TYPE_MEMORY_BARRIER,
203 vk::VK_ACCESS_TRANSFER_WRITE_BIT,
204 vk::VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
207 m_vk.cmdPipelineBarrier(*m_cmdBuffer, vk::VK_PIPELINE_STAGE_TRANSFER_BIT,
208 vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
209 0, 1, &memBarrier, 0, DE_NULL, 0, DE_NULL);
211 const vk::VkRect2D renderArea = { { 0, 0 }, { WIDTH, HEIGHT } };
212 const RenderPassBeginInfo renderPassBegin(*m_renderPass, *m_framebuffer, renderArea);
214 m_vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBegin, vk::VK_SUBPASS_CONTENTS_INLINE);