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 #include "vkCmdUtil.hpp"
27 #include "vkTypeUtil.hpp"
28
29 namespace vkt
30 {
31 namespace Draw
32 {
33
DrawTestsBaseClass(Context & context,const char * vertexShaderName,const char * fragmentShaderName,vk::VkPrimitiveTopology topology)34 DrawTestsBaseClass::DrawTestsBaseClass (Context& context, const char* vertexShaderName, const char* fragmentShaderName, vk::VkPrimitiveTopology topology)
35 : TestInstance (context)
36 , m_colorAttachmentFormat (vk::VK_FORMAT_R8G8B8A8_UNORM)
37 , m_topology (topology)
38 , m_vk (context.getDeviceInterface())
39 , m_vertexShaderName (vertexShaderName)
40 , m_fragmentShaderName (fragmentShaderName)
41 {
42 }
43
initialize(void)44 void DrawTestsBaseClass::initialize (void)
45 {
46 const vk::VkDevice device = m_context.getDevice();
47 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
48
49 const PipelineLayoutCreateInfo pipelineLayoutCreateInfo;
50 m_pipelineLayout = vk::createPipelineLayout(m_vk, device, &pipelineLayoutCreateInfo);
51
52 const vk::VkExtent3D targetImageExtent = { WIDTH, HEIGHT, 1 };
53 const ImageCreateInfo targetImageCreateInfo(vk::VK_IMAGE_TYPE_2D, m_colorAttachmentFormat, targetImageExtent, 1, 1, vk::VK_SAMPLE_COUNT_1_BIT,
54 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);
55
56 m_colorTargetImage = Image::createAndAlloc(m_vk, device, targetImageCreateInfo, m_context.getDefaultAllocator(), m_context.getUniversalQueueFamilyIndex());
57
58 const ImageViewCreateInfo colorTargetViewInfo(m_colorTargetImage->object(), vk::VK_IMAGE_VIEW_TYPE_2D, m_colorAttachmentFormat);
59 m_colorTargetView = vk::createImageView(m_vk, device, &colorTargetViewInfo);
60
61 RenderPassCreateInfo renderPassCreateInfo;
62 renderPassCreateInfo.addAttachment(AttachmentDescription(m_colorAttachmentFormat,
63 vk::VK_SAMPLE_COUNT_1_BIT,
64 vk::VK_ATTACHMENT_LOAD_OP_LOAD,
65 vk::VK_ATTACHMENT_STORE_OP_STORE,
66 vk::VK_ATTACHMENT_LOAD_OP_DONT_CARE,
67 vk::VK_ATTACHMENT_STORE_OP_STORE,
68 vk::VK_IMAGE_LAYOUT_GENERAL,
69 vk::VK_IMAGE_LAYOUT_GENERAL));
70
71
72 const vk::VkAttachmentReference colorAttachmentReference =
73 {
74 0,
75 vk::VK_IMAGE_LAYOUT_GENERAL
76 };
77
78 renderPassCreateInfo.addSubpass(SubpassDescription(vk::VK_PIPELINE_BIND_POINT_GRAPHICS,
79 0,
80 0,
81 DE_NULL,
82 1,
83 &colorAttachmentReference,
84 DE_NULL,
85 AttachmentReference(),
86 0,
87 DE_NULL));
88
89 m_renderPass = vk::createRenderPass(m_vk, device, &renderPassCreateInfo);
90
91 std::vector<vk::VkImageView> colorAttachments(1);
92 colorAttachments[0] = *m_colorTargetView;
93
94 const FramebufferCreateInfo framebufferCreateInfo(*m_renderPass, colorAttachments, WIDTH, HEIGHT, 1);
95
96 m_framebuffer = vk::createFramebuffer(m_vk, device, &framebufferCreateInfo);
97
98 const vk::VkVertexInputBindingDescription vertexInputBindingDescription =
99 {
100 0,
101 sizeof(VertexElementData),
102 vk::VK_VERTEX_INPUT_RATE_VERTEX,
103 };
104
105 const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[] =
106 {
107 {
108 0u,
109 0u,
110 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
111 0u
112 }, // VertexElementData::position
113 {
114 1u,
115 0u,
116 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
117 static_cast<deUint32>(sizeof(tcu::Vec4))
118 }, // VertexElementData::color
119 {
120 2u,
121 0u,
122 vk::VK_FORMAT_R32_SINT,
123 static_cast<deUint32>(sizeof(tcu::Vec4)) * 2
124 } // VertexElementData::refVertexIndex
125 };
126
127 m_vertexInputState = PipelineCreateInfo::VertexInputState(1,
128 &vertexInputBindingDescription,
129 DE_LENGTH_OF_ARRAY(vertexInputAttributeDescriptions),
130 vertexInputAttributeDescriptions);
131
132 const vk::VkDeviceSize dataSize = m_data.size() * sizeof(VertexElementData);
133 m_vertexBuffer = Buffer::createAndAlloc(m_vk, device, BufferCreateInfo(dataSize,
134 vk::VK_BUFFER_USAGE_VERTEX_BUFFER_BIT), m_context.getDefaultAllocator(), vk::MemoryRequirement::HostVisible);
135
136 deUint8* ptr = reinterpret_cast<deUint8*>(m_vertexBuffer->getBoundMemory().getHostPtr());
137 deMemcpy(ptr, &m_data[0], static_cast<size_t>(dataSize));
138
139 vk::flushAlloc(m_vk, device, m_vertexBuffer->getBoundMemory());
140
141 const CmdPoolCreateInfo cmdPoolCreateInfo(queueFamilyIndex);
142 m_cmdPool = vk::createCommandPool(m_vk, device, &cmdPoolCreateInfo);
143 m_cmdBuffer = vk::allocateCommandBuffer(m_vk, device, *m_cmdPool, vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY);
144
145 initPipeline(device);
146 }
147
initPipeline(const vk::VkDevice device)148 void DrawTestsBaseClass::initPipeline (const vk::VkDevice device)
149 {
150 const vk::Unique<vk::VkShaderModule> vs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_vertexShaderName), 0));
151 const vk::Unique<vk::VkShaderModule> fs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_fragmentShaderName), 0));
152
153 const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
154
155 vk::VkViewport viewport = vk::makeViewport(WIDTH, HEIGHT);
156 vk::VkRect2D scissor = vk::makeRect2D(WIDTH, HEIGHT);
157
158 PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
159 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
160 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
161 pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
162 pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
163 pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &vkCbAttachmentState));
164 pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1, std::vector<vk::VkViewport>(1, viewport), std::vector<vk::VkRect2D>(1, scissor)));
165 pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
166 pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
167 pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
168
169 m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
170 }
171
beginRenderPass(const vk::VkSubpassContents content)172 void DrawTestsBaseClass::beginRenderPass (const vk::VkSubpassContents content)
173 {
174 const vk::VkClearColorValue clearColor = { { 0.0f, 0.0f, 0.0f, 1.0f } };
175
176 beginCommandBuffer(m_vk, *m_cmdBuffer, 0u);
177
178 initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL,
179 vk::VK_ACCESS_TRANSFER_WRITE_BIT, vk::VK_PIPELINE_STAGE_TRANSFER_BIT);
180
181 const ImageSubresourceRange subresourceRange(vk::VK_IMAGE_ASPECT_COLOR_BIT);
182 m_vk.cmdClearColorImage(*m_cmdBuffer, m_colorTargetImage->object(),
183 vk::VK_IMAGE_LAYOUT_GENERAL, &clearColor, 1, &subresourceRange);
184
185 const vk::VkMemoryBarrier memBarrier =
186 {
187 vk::VK_STRUCTURE_TYPE_MEMORY_BARRIER,
188 DE_NULL,
189 vk::VK_ACCESS_TRANSFER_WRITE_BIT,
190 vk::VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
191 };
192
193 m_vk.cmdPipelineBarrier(*m_cmdBuffer, vk::VK_PIPELINE_STAGE_TRANSFER_BIT,
194 vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
195 0, 1, &memBarrier, 0, DE_NULL, 0, DE_NULL);
196
197 const vk::VkRect2D renderArea = vk::makeRect2D(WIDTH, HEIGHT);
198 vk::beginRenderPass(m_vk, *m_cmdBuffer, *m_renderPass, *m_framebuffer, renderArea, content);
199 }
200
201 } // Draw
202 } // vkt
203