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
DrawTestsBaseClass(Context & context,const char * vertexShaderName,const char * fragmentShaderName,vk::VkPrimitiveTopology topology)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
initialize(void)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
146 const vk::VkCommandBufferAllocateInfo cmdBufferAllocateInfo =
147 {
148 vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // VkStructureType sType;
149 DE_NULL, // const void* pNext;
150 *m_cmdPool, // VkCommandPool commandPool;
151 vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY, // VkCommandBufferLevel level;
152 1u, // deUint32 bufferCount;
153 };
154 m_cmdBuffer = vk::allocateCommandBuffer(m_vk, device, &cmdBufferAllocateInfo);
155
156 initPipeline(device);
157 }
158
initPipeline(const vk::VkDevice device)159 void DrawTestsBaseClass::initPipeline (const vk::VkDevice device)
160 {
161 const vk::Unique<vk::VkShaderModule> vs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_vertexShaderName), 0));
162 const vk::Unique<vk::VkShaderModule> fs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_fragmentShaderName), 0));
163
164 const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
165
166 vk::VkViewport viewport;
167 viewport.x = 0;
168 viewport.y = 0;
169 viewport.width = static_cast<float>(WIDTH);
170 viewport.height = static_cast<float>(HEIGHT);
171 viewport.minDepth = 0.0f;
172 viewport.maxDepth = 1.0f;
173
174 vk::VkRect2D scissor;
175 scissor.offset.x = 0;
176 scissor.offset.y = 0;
177 scissor.extent.width = WIDTH;
178 scissor.extent.height = HEIGHT;
179
180 PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
181 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
182 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
183 pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
184 pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
185 pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &vkCbAttachmentState));
186 pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1, std::vector<vk::VkViewport>(1, viewport), std::vector<vk::VkRect2D>(1, scissor)));
187 pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
188 pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
189 pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
190
191 m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
192 }
193
beginRenderPass(void)194 void DrawTestsBaseClass::beginRenderPass (void)
195 {
196 const vk::VkClearColorValue clearColor = { { 0.0f, 0.0f, 0.0f, 1.0f } };
197 const CmdBufferBeginInfo beginInfo;
198
199 m_vk.beginCommandBuffer(*m_cmdBuffer, &beginInfo);
200
201 initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL);
202
203 const ImageSubresourceRange subresourceRange(vk::VK_IMAGE_ASPECT_COLOR_BIT);
204 m_vk.cmdClearColorImage(*m_cmdBuffer, m_colorTargetImage->object(),
205 vk::VK_IMAGE_LAYOUT_GENERAL, &clearColor, 1, &subresourceRange);
206
207 const vk::VkMemoryBarrier memBarrier =
208 {
209 vk::VK_STRUCTURE_TYPE_MEMORY_BARRIER,
210 DE_NULL,
211 vk::VK_ACCESS_TRANSFER_WRITE_BIT,
212 vk::VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | vk::VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
213 };
214
215 m_vk.cmdPipelineBarrier(*m_cmdBuffer, vk::VK_PIPELINE_STAGE_TRANSFER_BIT,
216 vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
217 0, 1, &memBarrier, 0, DE_NULL, 0, DE_NULL);
218
219 const vk::VkRect2D renderArea = { { 0, 0 }, { WIDTH, HEIGHT } };
220 const RenderPassBeginInfo renderPassBegin(*m_renderPass, *m_framebuffer, renderArea);
221
222 m_vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBegin, vk::VK_SUBPASS_CONTENTS_INLINE);
223 }
224
225 } // Draw
226 } // vkt
227