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 27namespace vkt 28{ 29namespace Draw 30{ 31 32DrawTestsBaseClass::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) 39{ 40} 41 42void DrawTestsBaseClass::initialize (void) 43{ 44 const vk::VkDevice device = m_context.getDevice(); 45 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex(); 46 47 const PipelineLayoutCreateInfo pipelineLayoutCreateInfo; 48 m_pipelineLayout = vk::createPipelineLayout(m_vk, device, &pipelineLayoutCreateInfo); 49 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); 53 54 m_colorTargetImage = Image::createAndAlloc(m_vk, device, targetImageCreateInfo, m_context.getDefaultAllocator()); 55 56 const ImageViewCreateInfo colorTargetViewInfo(m_colorTargetImage->object(), vk::VK_IMAGE_VIEW_TYPE_2D, m_colorAttachmentFormat); 57 m_colorTargetView = vk::createImageView(m_vk, device, &colorTargetViewInfo); 58 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)); 68 69 70 const vk::VkAttachmentReference colorAttachmentReference = 71 { 72 0, 73 vk::VK_IMAGE_LAYOUT_GENERAL 74 }; 75 76 renderPassCreateInfo.addSubpass(SubpassDescription(vk::VK_PIPELINE_BIND_POINT_GRAPHICS, 77 0, 78 0, 79 DE_NULL, 80 1, 81 &colorAttachmentReference, 82 DE_NULL, 83 AttachmentReference(), 84 0, 85 DE_NULL)); 86 87 m_renderPass = vk::createRenderPass(m_vk, device, &renderPassCreateInfo); 88 89 std::vector<vk::VkImageView> colorAttachments(1); 90 colorAttachments[0] = *m_colorTargetView; 91 92 const FramebufferCreateInfo framebufferCreateInfo(*m_renderPass, colorAttachments, WIDTH, HEIGHT, 1); 93 94 m_framebuffer = vk::createFramebuffer(m_vk, device, &framebufferCreateInfo); 95 96 const vk::VkVertexInputBindingDescription vertexInputBindingDescription = 97 { 98 0, 99 (deUint32)sizeof(tcu::Vec4) * 2, 100 vk::VK_VERTEX_INPUT_RATE_VERTEX, 101 }; 102 103 const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[2] = 104 { 105 { 106 0u, 107 0u, 108 vk::VK_FORMAT_R32G32B32A32_SFLOAT, 109 0u 110 }, 111 { 112 1u, 113 0u, 114 vk::VK_FORMAT_R32G32B32A32_SFLOAT, 115 (deUint32)(sizeof(float)* 4), 116 } 117 }; 118 119 m_vertexInputState = PipelineCreateInfo::VertexInputState(1, 120 &vertexInputBindingDescription, 121 2, 122 vertexInputAttributeDescriptions); 123 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); 127 128 deUint8* ptr = reinterpret_cast<deUint8*>(m_vertexBuffer->getBoundMemory().getHostPtr()); 129 deMemcpy(ptr, &m_data[0], static_cast<size_t>(dataSize)); 130 131 vk::flushMappedMemoryRange(m_vk, 132 device, 133 m_vertexBuffer->getBoundMemory().getMemory(), 134 m_vertexBuffer->getBoundMemory().getOffset(), 135 dataSize); 136 137 const CmdPoolCreateInfo cmdPoolCreateInfo(queueFamilyIndex); 138 m_cmdPool = vk::createCommandPool(m_vk, device, &cmdPoolCreateInfo); 139 140 const vk::VkCommandBufferAllocateInfo cmdBufferAllocateInfo = 141 { 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; 147 }; 148 m_cmdBuffer = vk::allocateCommandBuffer(m_vk, device, &cmdBufferAllocateInfo); 149 150 initPipeline(device); 151} 152 153void DrawTestsBaseClass::initPipeline (const vk::VkDevice device) 154{ 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)); 157 158 const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState; 159 160 vk::VkViewport viewport; 161 viewport.x = 0; 162 viewport.y = 0; 163 viewport.width = static_cast<float>(WIDTH); 164 viewport.height = static_cast<float>(HEIGHT); 165 viewport.minDepth = 0.0f; 166 viewport.maxDepth = 1.0f; 167 168 vk::VkRect2D scissor; 169 scissor.offset.x = 0; 170 scissor.offset.y = 0; 171 scissor.extent.width = WIDTH; 172 scissor.extent.height = HEIGHT; 173 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()); 184 185 m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo); 186} 187 188void DrawTestsBaseClass::beginRenderPass (void) 189{ 190 const vk::VkClearColorValue clearColor = { { 0.0f, 0.0f, 0.0f, 1.0f } }; 191 const CmdBufferBeginInfo beginInfo; 192 193 m_vk.beginCommandBuffer(*m_cmdBuffer, &beginInfo); 194 195 initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL); 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, 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 = { { 0, 0 }, { WIDTH, HEIGHT } }; 214 const RenderPassBeginInfo renderPassBegin(*m_renderPass, *m_framebuffer, renderArea); 215 216 m_vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBegin, vk::VK_SUBPASS_CONTENTS_INLINE); 217} 218 219} // Draw 220} // vkt 221