Merge branch 'jekstrand_renderpass_transfer_bit_fix' into 'master'
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / dynamic_state / vktDynamicStateBaseClass.cpp
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 The Khronos Group Inc.
6  * Copyright (c) 2015 Intel Corporation
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and/or associated documentation files (the
10  * "Materials"), to deal in the Materials without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Materials, and to
13  * permit persons to whom the Materials are furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice(s) and this permission notice shall be included
17  * in all copies or substantial portions of the Materials.
18  *
19  * The Materials are Confidential Information as defined by the
20  * Khronos Membership Agreement until designated non-confidential by Khronos,
21  * at which point this condition clause shall be removed.
22  *
23  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
27  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
30  *
31  *//*!
32  * \file
33  * \brief Dynamic State Tests - Base Class
34  *//*--------------------------------------------------------------------*/
35
36 #include "vktDynamicStateBaseClass.hpp"
37
38 #include "vkPrograms.hpp"
39
40 namespace vkt
41 {
42 namespace DynamicState
43 {
44
45 DynamicStateBaseClass::DynamicStateBaseClass (Context& context, const char* vertexShaderName, const char* fragmentShaderName)
46         : TestInstance                          (context)
47         , m_colorAttachmentFormat   (vk::VK_FORMAT_R8G8B8A8_UNORM)
48         , m_topology                            (vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP)
49         , m_vk                                          (context.getDeviceInterface())
50         , m_vertexShaderName            (vertexShaderName)
51         , m_fragmentShaderName          (fragmentShaderName)
52 {
53 }
54
55 void DynamicStateBaseClass::initialize (void)
56 {
57         const vk::VkDevice device               = m_context.getDevice();
58         const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
59
60         const PipelineLayoutCreateInfo pipelineLayoutCreateInfo;
61         m_pipelineLayout = vk::createPipelineLayout(m_vk, device, &pipelineLayoutCreateInfo);
62
63         const vk::VkExtent3D targetImageExtent = { WIDTH, HEIGHT, 1 };
64         const ImageCreateInfo targetImageCreateInfo(vk::VK_IMAGE_TYPE_2D, m_colorAttachmentFormat, targetImageExtent, 1, 1, vk::VK_SAMPLE_COUNT_1_BIT,
65                                                                                                 vk::VK_IMAGE_TILING_OPTIMAL, vk::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | vk::VK_IMAGE_USAGE_TRANSFER_SRC_BIT);
66
67         m_colorTargetImage = Image::createAndAlloc(m_vk, device, targetImageCreateInfo, m_context.getDefaultAllocator());
68
69         const ImageViewCreateInfo colorTargetViewInfo(m_colorTargetImage->object(), vk::VK_IMAGE_VIEW_TYPE_2D, m_colorAttachmentFormat);
70         m_colorTargetView = vk::createImageView(m_vk, device, &colorTargetViewInfo);
71
72         RenderPassCreateInfo renderPassCreateInfo;
73         renderPassCreateInfo.addAttachment(AttachmentDescription(m_colorAttachmentFormat,
74                                                                                                                          vk::VK_SAMPLE_COUNT_1_BIT,
75                                                                                                                          vk::VK_ATTACHMENT_LOAD_OP_LOAD,
76                                                                                                                          vk::VK_ATTACHMENT_STORE_OP_STORE,
77                                                                                                                          vk::VK_ATTACHMENT_LOAD_OP_DONT_CARE,
78                                                                                                                          vk::VK_ATTACHMENT_STORE_OP_STORE,
79                                                                                                                          vk::VK_IMAGE_LAYOUT_GENERAL,
80                                                                                                                          vk::VK_IMAGE_LAYOUT_GENERAL));
81
82         const vk::VkAttachmentReference colorAttachmentReference =
83         {
84                 0,
85                 vk::VK_IMAGE_LAYOUT_GENERAL
86         };
87
88         renderPassCreateInfo.addSubpass(SubpassDescription(
89                 vk::VK_PIPELINE_BIND_POINT_GRAPHICS,
90                 0,
91                 0,
92                 DE_NULL,
93                 1,
94                 &colorAttachmentReference,
95                 DE_NULL,
96                 AttachmentReference(),
97                 0,
98                 DE_NULL
99                 )
100                 );
101
102         m_renderPass = vk::createRenderPass(m_vk, device, &renderPassCreateInfo);
103
104         std::vector<vk::VkImageView> colorAttachments(1);
105         colorAttachments[0] = *m_colorTargetView;
106
107         const FramebufferCreateInfo framebufferCreateInfo(*m_renderPass, colorAttachments, WIDTH, HEIGHT, 1);
108
109         m_framebuffer = vk::createFramebuffer(m_vk, device, &framebufferCreateInfo);
110
111         const vk::VkVertexInputBindingDescription vertexInputBindingDescription =
112         {
113                 0,
114                 (deUint32)sizeof(tcu::Vec4) * 2,
115                 vk::VK_VERTEX_INPUT_RATE_VERTEX,
116         };
117
118         const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[2] =
119         {
120                 {
121                         0u,
122                         0u,
123                         vk::VK_FORMAT_R32G32B32A32_SFLOAT,
124                         0u
125                 },
126                 {
127                         1u,
128                         0u,
129                         vk::VK_FORMAT_R32G32B32A32_SFLOAT,
130                         (deUint32)(sizeof(float)* 4),
131                 }
132         };
133
134         m_vertexInputState = PipelineCreateInfo::VertexInputState(
135                 1,
136                 &vertexInputBindingDescription,
137                 2,
138                 vertexInputAttributeDescriptions);
139
140         const vk::VkDeviceSize dataSize = m_data.size() * sizeof(PositionColorVertex);
141         m_vertexBuffer = Buffer::createAndAlloc(m_vk, device, BufferCreateInfo(dataSize, vk::VK_BUFFER_USAGE_VERTEX_BUFFER_BIT),
142                                                                                         m_context.getDefaultAllocator(), vk::MemoryRequirement::HostVisible);
143
144         deUint8* ptr = reinterpret_cast<unsigned char *>(m_vertexBuffer->getBoundMemory().getHostPtr());
145         deMemcpy(ptr, &m_data[0], (size_t)dataSize);
146
147         vk::flushMappedMemoryRange(m_vk, device,
148                 m_vertexBuffer->getBoundMemory().getMemory(),
149                 m_vertexBuffer->getBoundMemory().getOffset(),
150                 dataSize);
151
152         const CmdPoolCreateInfo cmdPoolCreateInfo(queueFamilyIndex);
153         m_cmdPool = vk::createCommandPool(m_vk, device, &cmdPoolCreateInfo);
154
155         const vk::VkCommandBufferAllocateInfo cmdBufferAllocateInfo =
156         {
157                 vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,     // VkStructureType                      sType;
158                 DE_NULL,                                                                                        // const void*                          pNext;
159                 *m_cmdPool,                                                                                     // VkCommandPool                        commandPool;
160                 vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY,                            // VkCommandBufferLevel         level;
161                 1u,                                                                                                     // deUint32                                     bufferCount;
162         };
163         m_cmdBuffer = vk::allocateCommandBuffer(m_vk, device, &cmdBufferAllocateInfo);
164
165         initPipeline(device);
166 }
167
168 void DynamicStateBaseClass::initPipeline (const vk::VkDevice device)
169 {
170         const vk::Unique<vk::VkShaderModule> vs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_vertexShaderName), 0));
171         const vk::Unique<vk::VkShaderModule> fs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_fragmentShaderName), 0));
172
173         const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
174
175         PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
176         pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
177         pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
178         pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
179         pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
180         pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &vkCbAttachmentState));
181         pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1));
182         pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
183         pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
184         pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
185         pipelineCreateInfo.addState(PipelineCreateInfo::DynamicState());
186
187         m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
188 }
189
190 tcu::TestStatus DynamicStateBaseClass::iterate (void)
191 {
192         DE_ASSERT(false);
193         return tcu::TestStatus::fail("Implement iterate() method!");
194 }
195
196 void DynamicStateBaseClass::beginRenderPass (void)
197 {
198         const vk::VkClearColorValue clearColor = { { 0.0f, 0.0f, 0.0f, 1.0f } };
199         beginRenderPassWithClearColor(clearColor);
200 }
201
202 void DynamicStateBaseClass::beginRenderPassWithClearColor (const vk::VkClearColorValue& clearColor)
203 {
204         const CmdBufferBeginInfo beginInfo;
205         m_vk.beginCommandBuffer(*m_cmdBuffer, &beginInfo);
206
207         initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL);
208
209         const ImageSubresourceRange subresourceRange(vk::VK_IMAGE_ASPECT_COLOR_BIT);
210         m_vk.cmdClearColorImage(*m_cmdBuffer, m_colorTargetImage->object(),
211                 vk::VK_IMAGE_LAYOUT_GENERAL, &clearColor, 1, &subresourceRange);
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 void DynamicStateBaseClass::setDynamicViewportState (const deUint32 width, const deUint32 height)
220 {
221         vk::VkViewport viewport;
222         viewport.x = 0;
223         viewport.y = 0;
224         viewport.width = static_cast<float>(width);
225         viewport.height = static_cast<float>(height);
226         viewport.minDepth = 0.0f;
227         viewport.maxDepth = 1.0f;
228
229         m_vk.cmdSetViewport(*m_cmdBuffer, 0, 1, &viewport);
230
231         vk::VkRect2D scissor;
232         scissor.offset.x = 0;
233         scissor.offset.y = 0;
234         scissor.extent.width = width;
235         scissor.extent.height = height;
236         m_vk.cmdSetScissor(*m_cmdBuffer, 0, 1, &scissor);
237 }
238
239 void DynamicStateBaseClass::setDynamicViewportState (deUint32 viewportCount, const vk::VkViewport* pViewports, const vk::VkRect2D* pScissors)
240 {
241         m_vk.cmdSetViewport(*m_cmdBuffer, 0, viewportCount, pViewports);
242         m_vk.cmdSetScissor(*m_cmdBuffer, 0, viewportCount, pScissors);
243 }
244
245 void DynamicStateBaseClass::setDynamicRasterizationState (const float lineWidth,
246                                                                                                                  const float depthBiasConstantFactor,
247                                                                                                                  const float depthBiasClamp,
248                                                                                                                  const float depthBiasSlopeFactor)
249 {
250         m_vk.cmdSetLineWidth(*m_cmdBuffer, lineWidth);
251         m_vk.cmdSetDepthBias(*m_cmdBuffer, depthBiasConstantFactor, depthBiasClamp, depthBiasSlopeFactor);
252 }
253
254 void DynamicStateBaseClass::setDynamicBlendState (const float const1, const float const2, const float const3, const float const4)
255 {
256         float blendConstantsants[4] = { const1, const2, const3, const4 };
257         m_vk.cmdSetBlendConstants(*m_cmdBuffer, blendConstantsants);
258 }
259
260 void DynamicStateBaseClass::setDynamicDepthStencilState (const float    minDepthBounds,
261                                                                                                                  const float    maxDepthBounds,
262                                                                                                                  const deUint32 stencilFrontCompareMask,
263                                                                                                                  const deUint32 stencilFrontWriteMask,
264                                                                                                                  const deUint32 stencilFrontReference,
265                                                                                                                  const deUint32 stencilBackCompareMask,
266                                                                                                                  const deUint32 stencilBackWriteMask,
267                                                                                                                  const deUint32 stencilBackReference)
268 {
269         m_vk.cmdSetDepthBounds(*m_cmdBuffer, minDepthBounds, maxDepthBounds);
270         m_vk.cmdSetStencilCompareMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontCompareMask);
271         m_vk.cmdSetStencilWriteMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontWriteMask);
272         m_vk.cmdSetStencilReference(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontReference);
273         m_vk.cmdSetStencilCompareMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackCompareMask);
274         m_vk.cmdSetStencilWriteMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackWriteMask);
275         m_vk.cmdSetStencilReference(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackReference);
276 }
277
278 } // DynamicState
279 } // vkt