Improve VK_MAKE_VERSION and VK_BIT macros
[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  * 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 Dynamic State Tests - Base Class
23  *//*--------------------------------------------------------------------*/
24
25 #include "vktDynamicStateBaseClass.hpp"
26
27 #include "vkPrograms.hpp"
28
29 namespace vkt
30 {
31 namespace DynamicState
32 {
33
34 DynamicStateBaseClass::DynamicStateBaseClass (Context& context, const char* vertexShaderName, const char* fragmentShaderName)
35         : TestInstance                          (context)
36         , m_colorAttachmentFormat   (vk::VK_FORMAT_R8G8B8A8_UNORM)
37         , m_topology                            (vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP)
38         , m_vk                                          (context.getDeviceInterface())
39         , m_vertexShaderName            (vertexShaderName)
40         , m_fragmentShaderName          (fragmentShaderName)
41 {
42 }
43
44 void DynamicStateBaseClass::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());
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         const vk::VkAttachmentReference colorAttachmentReference =
72         {
73                 0,
74                 vk::VK_IMAGE_LAYOUT_GENERAL
75         };
76
77         renderPassCreateInfo.addSubpass(SubpassDescription(
78                 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                 );
90
91         m_renderPass = vk::createRenderPass(m_vk, device, &renderPassCreateInfo);
92
93         std::vector<vk::VkImageView> colorAttachments(1);
94         colorAttachments[0] = *m_colorTargetView;
95
96         const FramebufferCreateInfo framebufferCreateInfo(*m_renderPass, colorAttachments, WIDTH, HEIGHT, 1);
97
98         m_framebuffer = vk::createFramebuffer(m_vk, device, &framebufferCreateInfo);
99
100         const vk::VkVertexInputBindingDescription vertexInputBindingDescription =
101         {
102                 0,
103                 (deUint32)sizeof(tcu::Vec4) * 2,
104                 vk::VK_VERTEX_INPUT_RATE_VERTEX,
105         };
106
107         const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[2] =
108         {
109                 {
110                         0u,
111                         0u,
112                         vk::VK_FORMAT_R32G32B32A32_SFLOAT,
113                         0u
114                 },
115                 {
116                         1u,
117                         0u,
118                         vk::VK_FORMAT_R32G32B32A32_SFLOAT,
119                         (deUint32)(sizeof(float)* 4),
120                 }
121         };
122
123         m_vertexInputState = PipelineCreateInfo::VertexInputState(
124                 1,
125                 &vertexInputBindingDescription,
126                 2,
127                 vertexInputAttributeDescriptions);
128
129         const vk::VkDeviceSize dataSize = m_data.size() * sizeof(PositionColorVertex);
130         m_vertexBuffer = Buffer::createAndAlloc(m_vk, device, BufferCreateInfo(dataSize, vk::VK_BUFFER_USAGE_VERTEX_BUFFER_BIT),
131                                                                                         m_context.getDefaultAllocator(), vk::MemoryRequirement::HostVisible);
132
133         deUint8* ptr = reinterpret_cast<unsigned char *>(m_vertexBuffer->getBoundMemory().getHostPtr());
134         deMemcpy(ptr, &m_data[0], (size_t)dataSize);
135
136         vk::flushMappedMemoryRange(m_vk, device,
137                 m_vertexBuffer->getBoundMemory().getMemory(),
138                 m_vertexBuffer->getBoundMemory().getOffset(),
139                 dataSize);
140
141         const CmdPoolCreateInfo cmdPoolCreateInfo(queueFamilyIndex);
142         m_cmdPool = vk::createCommandPool(m_vk, device, &cmdPoolCreateInfo);
143
144         const vk::VkCommandBufferAllocateInfo cmdBufferAllocateInfo =
145         {
146                 vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,     // VkStructureType                      sType;
147                 DE_NULL,                                                                                        // const void*                          pNext;
148                 *m_cmdPool,                                                                                     // VkCommandPool                        commandPool;
149                 vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY,                            // VkCommandBufferLevel         level;
150                 1u,                                                                                                     // deUint32                                     bufferCount;
151         };
152         m_cmdBuffer = vk::allocateCommandBuffer(m_vk, device, &cmdBufferAllocateInfo);
153
154         initPipeline(device);
155 }
156
157 void DynamicStateBaseClass::initPipeline (const vk::VkDevice device)
158 {
159         const vk::Unique<vk::VkShaderModule> vs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_vertexShaderName), 0));
160         const vk::Unique<vk::VkShaderModule> fs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_fragmentShaderName), 0));
161
162         const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
163
164         PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
165         pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
166         pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
167         pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
168         pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
169         pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &vkCbAttachmentState));
170         pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1));
171         pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
172         pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
173         pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
174         pipelineCreateInfo.addState(PipelineCreateInfo::DynamicState());
175
176         m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
177 }
178
179 tcu::TestStatus DynamicStateBaseClass::iterate (void)
180 {
181         DE_ASSERT(false);
182         return tcu::TestStatus::fail("Implement iterate() method!");
183 }
184
185 void DynamicStateBaseClass::beginRenderPass (void)
186 {
187         const vk::VkClearColorValue clearColor = { { 0.0f, 0.0f, 0.0f, 1.0f } };
188         beginRenderPassWithClearColor(clearColor);
189 }
190
191 void DynamicStateBaseClass::beginRenderPassWithClearColor (const vk::VkClearColorValue& clearColor)
192 {
193         const CmdBufferBeginInfo beginInfo;
194         m_vk.beginCommandBuffer(*m_cmdBuffer, &beginInfo);
195
196         initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL);
197
198         const ImageSubresourceRange subresourceRange(vk::VK_IMAGE_ASPECT_COLOR_BIT);
199         m_vk.cmdClearColorImage(*m_cmdBuffer, m_colorTargetImage->object(),
200                 vk::VK_IMAGE_LAYOUT_GENERAL, &clearColor, 1, &subresourceRange);
201
202         const vk::VkRect2D renderArea = { { 0, 0 }, { WIDTH, HEIGHT } };
203         const RenderPassBeginInfo renderPassBegin(*m_renderPass, *m_framebuffer, renderArea);
204
205         m_vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBegin, vk::VK_SUBPASS_CONTENTS_INLINE);
206 }
207
208 void DynamicStateBaseClass::setDynamicViewportState (const deUint32 width, const deUint32 height)
209 {
210         vk::VkViewport viewport;
211         viewport.x = 0;
212         viewport.y = 0;
213         viewport.width = static_cast<float>(width);
214         viewport.height = static_cast<float>(height);
215         viewport.minDepth = 0.0f;
216         viewport.maxDepth = 1.0f;
217
218         m_vk.cmdSetViewport(*m_cmdBuffer, 0, 1, &viewport);
219
220         vk::VkRect2D scissor;
221         scissor.offset.x = 0;
222         scissor.offset.y = 0;
223         scissor.extent.width = width;
224         scissor.extent.height = height;
225         m_vk.cmdSetScissor(*m_cmdBuffer, 0, 1, &scissor);
226 }
227
228 void DynamicStateBaseClass::setDynamicViewportState (deUint32 viewportCount, const vk::VkViewport* pViewports, const vk::VkRect2D* pScissors)
229 {
230         m_vk.cmdSetViewport(*m_cmdBuffer, 0, viewportCount, pViewports);
231         m_vk.cmdSetScissor(*m_cmdBuffer, 0, viewportCount, pScissors);
232 }
233
234 void DynamicStateBaseClass::setDynamicRasterizationState (const float lineWidth,
235                                                                                                                  const float depthBiasConstantFactor,
236                                                                                                                  const float depthBiasClamp,
237                                                                                                                  const float depthBiasSlopeFactor)
238 {
239         m_vk.cmdSetLineWidth(*m_cmdBuffer, lineWidth);
240         m_vk.cmdSetDepthBias(*m_cmdBuffer, depthBiasConstantFactor, depthBiasClamp, depthBiasSlopeFactor);
241 }
242
243 void DynamicStateBaseClass::setDynamicBlendState (const float const1, const float const2, const float const3, const float const4)
244 {
245         float blendConstantsants[4] = { const1, const2, const3, const4 };
246         m_vk.cmdSetBlendConstants(*m_cmdBuffer, blendConstantsants);
247 }
248
249 void DynamicStateBaseClass::setDynamicDepthStencilState (const float    minDepthBounds,
250                                                                                                                  const float    maxDepthBounds,
251                                                                                                                  const deUint32 stencilFrontCompareMask,
252                                                                                                                  const deUint32 stencilFrontWriteMask,
253                                                                                                                  const deUint32 stencilFrontReference,
254                                                                                                                  const deUint32 stencilBackCompareMask,
255                                                                                                                  const deUint32 stencilBackWriteMask,
256                                                                                                                  const deUint32 stencilBackReference)
257 {
258         m_vk.cmdSetDepthBounds(*m_cmdBuffer, minDepthBounds, maxDepthBounds);
259         m_vk.cmdSetStencilCompareMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontCompareMask);
260         m_vk.cmdSetStencilWriteMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontWriteMask);
261         m_vk.cmdSetStencilReference(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontReference);
262         m_vk.cmdSetStencilCompareMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackCompareMask);
263         m_vk.cmdSetStencilWriteMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackWriteMask);
264         m_vk.cmdSetStencilReference(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackReference);
265 }
266
267 } // DynamicState
268 } // vkt