Add create functions for common Vulkan types.
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / draw / vktDrawBaseClass.cpp
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
27 namespace vkt
28 {
29 namespace Draw
30 {
31
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)
39 {
40 }
41
42 void 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                 sizeof(VertexElementData),
100                 vk::VK_VERTEX_INPUT_RATE_VERTEX,
101         };
102
103         const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[] =
104         {
105                 {
106                         0u,
107                         0u,
108                         vk::VK_FORMAT_R32G32B32A32_SFLOAT,
109                         0u
110                 },      // VertexElementData::position
111                 {
112                         1u,
113                         0u,
114                         vk::VK_FORMAT_R32G32B32A32_SFLOAT,
115                         static_cast<deUint32>(sizeof(tcu::Vec4))
116                 },  // VertexElementData::color
117                 {
118                         2u,
119                         0u,
120                         vk::VK_FORMAT_R32_SINT,
121                         static_cast<deUint32>(sizeof(tcu::Vec4)) * 2
122                 }   // VertexElementData::refVertexIndex
123         };
124
125         m_vertexInputState = PipelineCreateInfo::VertexInputState(1,
126                                                                                                                           &vertexInputBindingDescription,
127                                                                                                                           DE_LENGTH_OF_ARRAY(vertexInputAttributeDescriptions),
128                                                                                                                           vertexInputAttributeDescriptions);
129
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);
133
134         deUint8* ptr = reinterpret_cast<deUint8*>(m_vertexBuffer->getBoundMemory().getHostPtr());
135         deMemcpy(ptr, &m_data[0], static_cast<size_t>(dataSize));
136
137         vk::flushMappedMemoryRange(m_vk,
138                                                            device,
139                                                            m_vertexBuffer->getBoundMemory().getMemory(),
140                                                            m_vertexBuffer->getBoundMemory().getOffset(),
141                                                            dataSize);
142
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);
146
147         initPipeline(device);
148 }
149
150 void DrawTestsBaseClass::initPipeline (const vk::VkDevice device)
151 {
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));
154
155         const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
156
157         vk::VkViewport viewport;
158         viewport.x                              = 0;
159         viewport.y                              = 0;
160         viewport.width                  = static_cast<float>(WIDTH);
161         viewport.height                 = static_cast<float>(HEIGHT);
162         viewport.minDepth               = 0.0f;
163         viewport.maxDepth               = 1.0f;
164
165         vk::VkRect2D scissor;
166         scissor.offset.x                = 0;
167         scissor.offset.y                = 0;
168         scissor.extent.width    = WIDTH;
169         scissor.extent.height   = HEIGHT;
170
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());
181
182         m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
183 }
184
185 void DrawTestsBaseClass::beginRenderPass (void)
186 {
187         const vk::VkClearColorValue clearColor = { { 0.0f, 0.0f, 0.0f, 1.0f } };
188         const CmdBufferBeginInfo beginInfo;
189
190         m_vk.beginCommandBuffer(*m_cmdBuffer, &beginInfo);
191
192         initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL);
193
194         const ImageSubresourceRange subresourceRange(vk::VK_IMAGE_ASPECT_COLOR_BIT);
195         m_vk.cmdClearColorImage(*m_cmdBuffer, m_colorTargetImage->object(),
196                 vk::VK_IMAGE_LAYOUT_GENERAL, &clearColor, 1, &subresourceRange);
197
198         const vk::VkMemoryBarrier memBarrier =
199         {
200                 vk::VK_STRUCTURE_TYPE_MEMORY_BARRIER,
201                 DE_NULL,
202                 vk::VK_ACCESS_TRANSFER_WRITE_BIT,
203                 vk::VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
204         };
205
206         m_vk.cmdPipelineBarrier(*m_cmdBuffer, vk::VK_PIPELINE_STAGE_TRANSFER_BIT,
207                 vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
208                 0, 1, &memBarrier, 0, DE_NULL, 0, DE_NULL);
209
210         const vk::VkRect2D renderArea = { { 0, 0 }, { WIDTH, HEIGHT } };
211         const RenderPassBeginInfo renderPassBegin(*m_renderPass, *m_framebuffer, renderArea);
212
213         m_vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBegin, vk::VK_SUBPASS_CONTENTS_INLINE);
214 }
215
216 }       // Draw
217 }       // vkt