Improve VK_MAKE_VERSION and VK_BIT macros
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / dynamic_state / vktDynamicStateRSTests.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 Raster State Tests
23  *//*--------------------------------------------------------------------*/
24
25 #include "vktDynamicStateRSTests.hpp"
26
27 #include "vktDynamicStateBaseClass.hpp"
28 #include "vktDynamicStateTestCaseUtil.hpp"
29
30 #include "vkImageUtil.hpp"
31
32 #include "tcuTextureUtil.hpp"
33 #include "tcuImageCompare.hpp"
34 #include "tcuRGBA.hpp"
35
36 #include "deMath.h"
37
38 namespace vkt
39 {
40 namespace DynamicState
41 {
42 namespace
43 {
44
45 class DepthBiasBaseCase : public TestInstance
46 {
47 public:
48         DepthBiasBaseCase (Context& context, const char* vertexShaderName, const char* fragmentShaderName)
49                 : TestInstance                                          (context)
50                 , m_colorAttachmentFormat                       (vk::VK_FORMAT_R8G8B8A8_UNORM)
51                 , m_topology                                            (vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP)
52                 , m_vk                                                          (context.getDeviceInterface())
53                 , m_vertexShaderName                            (vertexShaderName)
54                 , m_fragmentShaderName                          (fragmentShaderName)
55         {
56         }
57
58 protected:
59
60         enum
61         {
62                 WIDTH   = 128,
63                 HEIGHT  = 128
64         };
65
66         vk::VkFormat                                                                    m_colorAttachmentFormat;
67         vk::VkFormat                                                                    m_depthStencilAttachmentFormat;
68
69         vk::VkPrimitiveTopology                                                 m_topology;
70
71         const vk::DeviceInterface&                                              m_vk;
72
73         vk::Move<vk::VkPipeline>                                                m_pipeline;
74         vk::Move<vk::VkPipelineLayout>                                  m_pipelineLayout;
75
76         de::SharedPtr<Image>                                                    m_colorTargetImage;
77         vk::Move<vk::VkImageView>                                               m_colorTargetView;
78
79         de::SharedPtr<Image>                                                    m_depthStencilImage;
80         vk::Move<vk::VkImageView>                                               m_attachmentView;
81
82         PipelineCreateInfo::VertexInputState                    m_vertexInputState;
83         de::SharedPtr<Buffer>                                                   m_vertexBuffer;
84
85         vk::Move<vk::VkCommandPool>                                             m_cmdPool;
86         vk::Move<vk::VkCommandBuffer>                                   m_cmdBuffer;
87
88         vk::Move<vk::VkFramebuffer>                                             m_framebuffer;
89         vk::Move<vk::VkRenderPass>                                              m_renderPass;
90
91         std::string                                                                             m_vertexShaderName;
92         std::string                                                                             m_fragmentShaderName;
93
94         std::vector<PositionColorVertex>                                m_data;
95
96         PipelineCreateInfo::DepthStencilState                   m_depthStencilState;
97
98         void initialize (void)
99         {
100                 const vk::VkDevice device       = m_context.getDevice();
101
102                 vk::VkFormatProperties formatProperties;
103                 // check for VK_FORMAT_D24_UNORM_S8_UINT support
104                 m_context.getInstanceInterface().getPhysicalDeviceFormatProperties(m_context.getPhysicalDevice(), vk::VK_FORMAT_D24_UNORM_S8_UINT, &formatProperties);
105                 if (formatProperties.optimalTilingFeatures & vk::VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
106                 {
107                         m_depthStencilAttachmentFormat = vk::VK_FORMAT_D24_UNORM_S8_UINT;
108                 }
109                 else
110                 {
111                         // check for VK_FORMAT_D32_SFLOAT_S8_UINT support
112                         m_context.getInstanceInterface().getPhysicalDeviceFormatProperties(m_context.getPhysicalDevice(), vk::VK_FORMAT_D32_SFLOAT_S8_UINT, &formatProperties);
113                         if (formatProperties.optimalTilingFeatures & vk::VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
114                         {
115                                 m_depthStencilAttachmentFormat = vk::VK_FORMAT_D32_SFLOAT_S8_UINT;
116                         }
117                         else
118                                 throw tcu::NotSupportedError("No valid depth stencil attachment available");
119                 }
120
121                 const PipelineLayoutCreateInfo pipelineLayoutCreateInfo;
122                 m_pipelineLayout                        = vk::createPipelineLayout(m_vk, device, &pipelineLayoutCreateInfo);
123
124                 const vk::Unique<vk::VkShaderModule> vs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_vertexShaderName), 0));
125                 const vk::Unique<vk::VkShaderModule> fs(createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_fragmentShaderName), 0));
126
127                 const vk::VkExtent3D imageExtent = { WIDTH, HEIGHT, 1 };
128                 ImageCreateInfo targetImageCreateInfo(vk::VK_IMAGE_TYPE_2D, m_colorAttachmentFormat, imageExtent, 1, 1, vk::VK_SAMPLE_COUNT_1_BIT, vk::VK_IMAGE_TILING_OPTIMAL,
129                                                                                           vk::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | vk::VK_IMAGE_USAGE_TRANSFER_SRC_BIT | vk::VK_IMAGE_USAGE_TRANSFER_DST_BIT);
130
131                 m_colorTargetImage = Image::createAndAlloc(m_vk, device, targetImageCreateInfo, m_context.getDefaultAllocator());
132
133                 const ImageCreateInfo depthStencilImageCreateInfo(vk::VK_IMAGE_TYPE_2D, m_depthStencilAttachmentFormat, imageExtent,
134                                                                                                                   1, 1, vk::VK_SAMPLE_COUNT_1_BIT, vk::VK_IMAGE_TILING_OPTIMAL,
135                                                                                                                   vk::VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | vk::VK_IMAGE_USAGE_TRANSFER_DST_BIT);
136
137                 m_depthStencilImage = Image::createAndAlloc(m_vk, device, depthStencilImageCreateInfo, m_context.getDefaultAllocator());
138
139                 const ImageViewCreateInfo colorTargetViewInfo(m_colorTargetImage->object(), vk::VK_IMAGE_VIEW_TYPE_2D, m_colorAttachmentFormat);
140                 m_colorTargetView = vk::createImageView(m_vk, device, &colorTargetViewInfo);
141
142                 const ImageViewCreateInfo attachmentViewInfo(m_depthStencilImage->object(), vk::VK_IMAGE_VIEW_TYPE_2D, m_depthStencilAttachmentFormat);
143                 m_attachmentView = vk::createImageView(m_vk, device, &attachmentViewInfo);
144
145                 RenderPassCreateInfo renderPassCreateInfo;
146                 renderPassCreateInfo.addAttachment(AttachmentDescription(m_colorAttachmentFormat,
147                                                                                                                                  vk::VK_SAMPLE_COUNT_1_BIT,
148                                                                                                                                  vk::VK_ATTACHMENT_LOAD_OP_LOAD,
149                                                                                                                                  vk::VK_ATTACHMENT_STORE_OP_STORE,
150                                                                                                                                  vk::VK_ATTACHMENT_LOAD_OP_DONT_CARE,
151                                                                                                                                  vk::VK_ATTACHMENT_STORE_OP_STORE,
152                                                                                                                                  vk::VK_IMAGE_LAYOUT_GENERAL,
153                                                                                                                                  vk::VK_IMAGE_LAYOUT_GENERAL));
154
155                 renderPassCreateInfo.addAttachment(AttachmentDescription(m_depthStencilAttachmentFormat,
156                                                                                                                                  vk::VK_SAMPLE_COUNT_1_BIT,
157                                                                                                                                  vk::VK_ATTACHMENT_LOAD_OP_LOAD,
158                                                                                                                                  vk::VK_ATTACHMENT_STORE_OP_STORE,
159                                                                                                                                  vk::VK_ATTACHMENT_LOAD_OP_DONT_CARE,
160                                                                                                                                  vk::VK_ATTACHMENT_STORE_OP_STORE,
161                                                                                                                                  vk::VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
162                                                                                                                                  vk::VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));
163
164                 const vk::VkAttachmentReference colorAttachmentReference =
165                 {
166                         0,
167                         vk::VK_IMAGE_LAYOUT_GENERAL
168                 };
169
170                 const vk::VkAttachmentReference depthAttachmentReference =
171                 {
172                         1,
173                         vk::VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
174                 };
175
176                 renderPassCreateInfo.addSubpass(SubpassDescription(vk::VK_PIPELINE_BIND_POINT_GRAPHICS,
177                                                                                                                    0,
178                                                                                                                    0,
179                                                                                                                    DE_NULL,
180                                                                                                                    1,
181                                                                                                                    &colorAttachmentReference,
182                                                                                                                    DE_NULL,
183                                                                                                                    depthAttachmentReference,
184                                                                                                                    0,
185                                                                                                                    DE_NULL));
186
187                 m_renderPass = vk::createRenderPass(m_vk, device, &renderPassCreateInfo);
188
189                 const vk::VkVertexInputBindingDescription vertexInputBindingDescription =
190                 {
191                         0,
192                         (deUint32)sizeof(tcu::Vec4) * 2,
193                         vk::VK_VERTEX_INPUT_RATE_VERTEX,
194                 };
195
196                 const vk::VkVertexInputAttributeDescription vertexInputAttributeDescriptions[2] =
197                 {
198                         {
199                                 0u,
200                                 0u,
201                                 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
202                                 0u
203                         },
204                         {
205                                 1u,
206                                 0u,
207                                 vk::VK_FORMAT_R32G32B32A32_SFLOAT,
208                                 (deUint32)(sizeof(float)* 4),
209                         }
210                 };
211
212                 m_vertexInputState = PipelineCreateInfo::VertexInputState(1,
213                                                                                                                                   &vertexInputBindingDescription,
214                                                                                                                                   2,
215                                                                                                                                   vertexInputAttributeDescriptions);
216
217                 const PipelineCreateInfo::ColorBlendState::Attachment vkCbAttachmentState;
218
219                 PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
220                 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
221                 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
222                 pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
223                 pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
224                 pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &vkCbAttachmentState));
225                 pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1));
226                 pipelineCreateInfo.addState(m_depthStencilState);
227                 pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
228                 pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
229                 pipelineCreateInfo.addState(PipelineCreateInfo::DynamicState());
230
231                 m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
232
233                 std::vector<vk::VkImageView> attachments(2);
234                 attachments[0] = *m_colorTargetView;
235                 attachments[1] = *m_attachmentView;
236
237                 const FramebufferCreateInfo framebufferCreateInfo(*m_renderPass, attachments, WIDTH, HEIGHT, 1);
238
239                 m_framebuffer = vk::createFramebuffer(m_vk, device, &framebufferCreateInfo);
240
241                 const vk::VkDeviceSize dataSize = m_data.size() * sizeof(PositionColorVertex);
242                 m_vertexBuffer = Buffer::createAndAlloc(m_vk, device, BufferCreateInfo(dataSize,
243                         vk::VK_BUFFER_USAGE_VERTEX_BUFFER_BIT),
244                         m_context.getDefaultAllocator(), vk::MemoryRequirement::HostVisible);
245
246                 deUint8* ptr = reinterpret_cast<unsigned char *>(m_vertexBuffer->getBoundMemory().getHostPtr());
247                 deMemcpy(ptr, &m_data[0], static_cast<size_t>(dataSize));
248
249                 vk::flushMappedMemoryRange(m_vk, device,
250                                                                    m_vertexBuffer->getBoundMemory().getMemory(),
251                                                                    m_vertexBuffer->getBoundMemory().getOffset(),
252                                                                    sizeof(dataSize));
253
254                 const CmdPoolCreateInfo cmdPoolCreateInfo(m_context.getUniversalQueueFamilyIndex());
255                 m_cmdPool = vk::createCommandPool(m_vk, device, &cmdPoolCreateInfo);
256
257                 const vk::VkCommandBufferAllocateInfo cmdBufferAllocateInfo =
258                 {
259                         vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,     // VkStructureType                      sType;
260                         DE_NULL,                                                                                        // const void*                          pNext;
261                         *m_cmdPool,                                                                                     // VkCommandPool                        commandPool;
262                         vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY,                            // VkCommandBufferLevel         level;
263                         1u,                                                                                                     // deUint32                                     bufferCount;
264                 };
265                 m_cmdBuffer = vk::allocateCommandBuffer(m_vk, device, &cmdBufferAllocateInfo);
266         }
267
268         virtual tcu::TestStatus iterate (void)
269         {
270                 DE_ASSERT(false);
271                 return tcu::TestStatus::fail("Should reimplement iterate() method");
272         }
273
274         void beginRenderPass (void)
275         {
276                 const vk::VkClearColorValue clearColor = { { 0.0f, 0.0f, 0.0f, 1.0f } };
277                 beginRenderPassWithClearColor(clearColor);
278         }
279
280         void beginRenderPassWithClearColor (const vk::VkClearColorValue &clearColor)
281         {
282                 const CmdBufferBeginInfo beginInfo;
283                 m_vk.beginCommandBuffer(*m_cmdBuffer, &beginInfo);
284
285                 initialTransitionColor2DImage(m_vk, *m_cmdBuffer, m_colorTargetImage->object(), vk::VK_IMAGE_LAYOUT_GENERAL);
286                 initialTransitionDepthStencil2DImage(m_vk, *m_cmdBuffer, m_depthStencilImage->object(), vk::VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
287
288                 const ImageSubresourceRange subresourceRangeImage(vk::VK_IMAGE_ASPECT_COLOR_BIT);
289                 m_vk.cmdClearColorImage(*m_cmdBuffer, m_colorTargetImage->object(),
290                                                                 vk::VK_IMAGE_LAYOUT_GENERAL, &clearColor, 1, &subresourceRangeImage);
291
292                 const vk::VkClearDepthStencilValue depthStencilClearValue = { 0.0f, 0 };
293
294                 const ImageSubresourceRange subresourceRangeDepthStencil[2] = { vk::VK_IMAGE_ASPECT_DEPTH_BIT, vk::VK_IMAGE_ASPECT_STENCIL_BIT };
295                 m_vk.cmdClearDepthStencilImage(*m_cmdBuffer, m_depthStencilImage->object(),
296                                                                            vk::VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &depthStencilClearValue, 2, subresourceRangeDepthStencil);
297
298                 const vk::VkRect2D renderArea = { { 0, 0 }, { WIDTH, HEIGHT } };
299                 const RenderPassBeginInfo renderPassBegin(*m_renderPass, *m_framebuffer, renderArea);
300
301                 m_vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBegin, vk::VK_SUBPASS_CONTENTS_INLINE);
302         }
303
304         void setDynamicViewportState (const deUint32 width, const deUint32 height)
305         {
306                 vk::VkViewport viewport;
307                 viewport.x = 0;
308                 viewport.y = 0;
309                 viewport.width = static_cast<float>(width);
310                 viewport.height = static_cast<float>(height);
311                 viewport.minDepth = 0.0f;
312                 viewport.maxDepth = 1.0f;
313
314                 m_vk.cmdSetViewport(*m_cmdBuffer, 0, 1, &viewport);
315
316                 vk::VkRect2D scissor;
317                 scissor.offset.x = 0;
318                 scissor.offset.y = 0;
319                 scissor.extent.width = width;
320                 scissor.extent.height = height;
321                 m_vk.cmdSetScissor(*m_cmdBuffer, 0, 1, &scissor);
322         }
323
324         void setDynamicViewportState (const deUint32 viewportCount, const vk::VkViewport* pViewports, const vk::VkRect2D* pScissors)
325         {
326                 m_vk.cmdSetViewport(*m_cmdBuffer, 0, viewportCount, pViewports);
327                 m_vk.cmdSetScissor(*m_cmdBuffer, 0, viewportCount, pScissors);
328         }
329
330         void setDynamicRasterizationState (const float lineWidth = 1.0f,
331                 const float depthBiasConstantFactor = 0.0f,
332                 const float depthBiasClamp = 0.0f,
333                 const float depthBiasSlopeFactor = 0.0f)
334         {
335                 m_vk.cmdSetLineWidth(*m_cmdBuffer, lineWidth);
336                 m_vk.cmdSetDepthBias(*m_cmdBuffer, depthBiasConstantFactor, depthBiasClamp, depthBiasSlopeFactor);
337         }
338
339         void setDynamicBlendState (const float const1 = 0.0f, const float const2 = 0.0f,
340                 const float const3 = 0.0f, const float const4 = 0.0f)
341         {
342                 float blendConstantsants[4] = { const1, const2, const3, const4 };
343                 m_vk.cmdSetBlendConstants(*m_cmdBuffer, blendConstantsants);
344         }
345
346         void setDynamicDepthStencilState (const float minDepthBounds = -1.0f, const float maxDepthBounds = 1.0f,
347                 const deUint32 stencilFrontCompareMask = 0xffffffffu, const deUint32 stencilFrontWriteMask = 0xffffffffu,
348                 const deUint32 stencilFrontReference = 0, const deUint32 stencilBackCompareMask = 0xffffffffu,
349                 const deUint32 stencilBackWriteMask = 0xffffffffu, const deUint32 stencilBackReference = 0)
350         {
351                 m_vk.cmdSetDepthBounds(*m_cmdBuffer, minDepthBounds, maxDepthBounds);
352                 m_vk.cmdSetStencilCompareMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontCompareMask);
353                 m_vk.cmdSetStencilWriteMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontWriteMask);
354                 m_vk.cmdSetStencilReference(*m_cmdBuffer, vk::VK_STENCIL_FACE_FRONT_BIT, stencilFrontReference);
355                 m_vk.cmdSetStencilCompareMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackCompareMask);
356                 m_vk.cmdSetStencilWriteMask(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackWriteMask);
357                 m_vk.cmdSetStencilReference(*m_cmdBuffer, vk::VK_STENCIL_FACE_BACK_BIT, stencilBackReference);
358         }
359 };
360
361 class DepthBiasParamTestInstance : public DepthBiasBaseCase
362 {
363 public:
364         DepthBiasParamTestInstance (Context& context, ShaderMap shaders)
365                 : DepthBiasBaseCase (context, shaders[glu::SHADERTYPE_VERTEX], shaders[glu::SHADERTYPE_FRAGMENT])
366         {
367                 m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, 1.0f, 0.5f, 1.0f), tcu::RGBA::blue().toVec()));
368                 m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, 1.0f, 0.5f, 1.0f), tcu::RGBA::blue().toVec()));
369                 m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, -1.0f, 0.5f, 1.0f), tcu::RGBA::blue().toVec()));
370                 m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, -1.0f, 0.5f, 1.0f), tcu::RGBA::blue().toVec()));
371
372                 m_data.push_back(PositionColorVertex(tcu::Vec4(-0.5f, 0.5f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
373                 m_data.push_back(PositionColorVertex(tcu::Vec4(0.5f, 0.5f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
374                 m_data.push_back(PositionColorVertex(tcu::Vec4(-0.5f, -0.5f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
375                 m_data.push_back(PositionColorVertex(tcu::Vec4(0.5f, -0.5f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
376
377                 m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, 1.0f, 0.5f, 1.0f), tcu::RGBA::red().toVec()));
378                 m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, 1.0f, 0.5f, 1.0f), tcu::RGBA::red().toVec()));
379                 m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, -1.0f, 0.5f, 1.0f), tcu::RGBA::red().toVec()));
380                 m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, -1.0f, 0.5f, 1.0f), tcu::RGBA::red().toVec()));
381
382                 // enable depth test
383                 m_depthStencilState = PipelineCreateInfo::DepthStencilState(
384                         vk::VK_TRUE, vk::VK_TRUE, vk::VK_COMPARE_OP_GREATER_OR_EQUAL);
385
386                 DepthBiasBaseCase::initialize();
387         }
388
389         virtual tcu::TestStatus iterate (void)
390         {
391                 tcu::TestLog &log               = m_context.getTestContext().getLog();
392                 const vk::VkQueue queue = m_context.getUniversalQueue();
393
394                 beginRenderPass();
395
396                 // set states here
397                 setDynamicViewportState(WIDTH, HEIGHT);
398                 setDynamicBlendState();
399                 setDynamicDepthStencilState();
400
401                 m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
402
403                 const vk::VkDeviceSize vertexBufferOffset       = 0;
404                 const vk::VkBuffer vertexBuffer                         = m_vertexBuffer->object();
405                 m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
406
407                 setDynamicRasterizationState(1.0f, 0.0f);
408                 m_vk.cmdDraw(*m_cmdBuffer, 4, 1, 0, 0);
409                 m_vk.cmdDraw(*m_cmdBuffer, 4, 1, 4, 0);
410
411                 setDynamicRasterizationState(1.0f, -1.0f);
412                 m_vk.cmdDraw(*m_cmdBuffer, 4, 1, 8, 0);
413
414                 m_vk.cmdEndRenderPass(*m_cmdBuffer);
415                 m_vk.endCommandBuffer(*m_cmdBuffer);
416
417                 vk::VkSubmitInfo submitInfo =
418                 {
419                         vk::VK_STRUCTURE_TYPE_SUBMIT_INFO,      // VkStructureType                      sType;
420                         DE_NULL,                                                        // const void*                          pNext;
421                         0,                                                                      // deUint32                                     waitSemaphoreCount;
422                         DE_NULL,                                                        // const VkSemaphore*           pWaitSemaphores;
423                         (const vk::VkPipelineStageFlags*)DE_NULL,
424                         1,                                                                      // deUint32                                     commandBufferCount;
425                         &m_cmdBuffer.get(),                                     // const VkCommandBuffer*       pCommandBuffers;
426                         0,                                                                      // deUint32                                     signalSemaphoreCount;
427                         DE_NULL                                                         // const VkSemaphore*           pSignalSemaphores;
428                 };
429                 m_vk.queueSubmit(queue, 1, &submitInfo, DE_NULL);
430
431                 // validation
432                 {
433                         VK_CHECK(m_vk.queueWaitIdle(queue));
434
435                         tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
436                         referenceFrame.allocLevel(0);
437
438                         const deInt32 frameWidth = referenceFrame.getWidth();
439                         const deInt32 frameHeight = referenceFrame.getHeight();
440
441                         tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
442
443                         for (int y = 0; y < frameHeight; y++)
444                         {
445                                 const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
446
447                                 for (int x = 0; x < frameWidth; x++)
448                                 {
449                                         const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
450
451                                         if (xCoord >= -0.5f && xCoord <= 0.5f && yCoord >= -0.5f && yCoord <= 0.5f)
452                                                 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f), x, y);
453                                         else
454                                                 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), x, y);
455                                 }
456                         }
457
458                         const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
459                         const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
460                                 vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
461
462                         if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
463                                 referenceFrame.getLevel(0), renderedFrame, 0.05f,
464                                 tcu::COMPARE_LOG_RESULT))
465                         {
466                                 return tcu::TestStatus(QP_TEST_RESULT_FAIL, "Image verification failed");
467                         }
468
469                         return tcu::TestStatus(QP_TEST_RESULT_PASS, "Image verification passed");
470                 }
471         }
472 };
473
474 class DepthBiasClampParamTestInstance : public DepthBiasBaseCase
475 {
476 public:
477         DepthBiasClampParamTestInstance (Context& context, ShaderMap shaders)
478                 : DepthBiasBaseCase (context, shaders[glu::SHADERTYPE_VERTEX], shaders[glu::SHADERTYPE_FRAGMENT])
479         {
480                 m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, 1.0f, 0.0f, 1.0f), tcu::RGBA::blue().toVec()));
481                 m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, 1.0f, 0.0f, 1.0f), tcu::RGBA::blue().toVec()));
482                 m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, -1.0f, 0.0f, 1.0f), tcu::RGBA::blue().toVec()));
483                 m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, -1.0f, 0.0f, 1.0f), tcu::RGBA::blue().toVec()));
484
485                 m_data.push_back(PositionColorVertex(tcu::Vec4(-0.5f, 0.5f, 0.01f, 1.0f), tcu::RGBA::green().toVec()));
486                 m_data.push_back(PositionColorVertex(tcu::Vec4(0.5f, 0.5f, 0.01f, 1.0f), tcu::RGBA::green().toVec()));
487                 m_data.push_back(PositionColorVertex(tcu::Vec4(-0.5f, -0.5f, 0.01f, 1.0f), tcu::RGBA::green().toVec()));
488                 m_data.push_back(PositionColorVertex(tcu::Vec4(0.5f, -0.5f, 0.01f, 1.0f), tcu::RGBA::green().toVec()));
489
490                 // enable depth test
491                 m_depthStencilState = PipelineCreateInfo::DepthStencilState(vk::VK_TRUE, vk::VK_TRUE, vk::VK_COMPARE_OP_GREATER_OR_EQUAL);
492
493                 DepthBiasBaseCase::initialize();
494         }
495
496         virtual tcu::TestStatus iterate (void)
497         {
498                 tcu::TestLog &log = m_context.getTestContext().getLog();
499                 const vk::VkQueue queue = m_context.getUniversalQueue();
500
501                 beginRenderPass();
502
503                 // set states here
504                 setDynamicViewportState(WIDTH, HEIGHT);
505                 setDynamicBlendState();
506                 setDynamicDepthStencilState();
507
508                 m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
509
510                 const vk::VkDeviceSize vertexBufferOffset = 0;
511                 const vk::VkBuffer vertexBuffer = m_vertexBuffer->object();
512                 m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
513
514                 setDynamicRasterizationState(1.0f, 1000.0f, 0.005f);
515                 m_vk.cmdDraw(*m_cmdBuffer, 4, 1, 0, 0);
516
517                 setDynamicRasterizationState(1.0f, 0.0f);
518                 m_vk.cmdDraw(*m_cmdBuffer, 4, 1, 4, 0);
519
520                 m_vk.cmdEndRenderPass(*m_cmdBuffer);
521                 m_vk.endCommandBuffer(*m_cmdBuffer);
522
523                 vk::VkSubmitInfo submitInfo =
524                 {
525                         vk::VK_STRUCTURE_TYPE_SUBMIT_INFO,      // VkStructureType                      sType;
526                         DE_NULL,                                                        // const void*                          pNext;
527                         0,                                                                      // deUint32                                     waitSemaphoreCount;
528                         DE_NULL,                                                        // const VkSemaphore*           pWaitSemaphores;
529                         (const vk::VkPipelineStageFlags*)DE_NULL,
530                         1,                                                                      // deUint32                                     commandBufferCount;
531                         &m_cmdBuffer.get(),                                     // const VkCommandBuffer*       pCommandBuffers;
532                         0,                                                                      // deUint32                                     signalSemaphoreCount;
533                         DE_NULL                                                         // const VkSemaphore*           pSignalSemaphores;
534                 };
535                 m_vk.queueSubmit(queue, 1, &submitInfo, DE_NULL);
536
537                 // validation
538                 {
539                         VK_CHECK(m_vk.queueWaitIdle(queue));
540
541                         tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
542                         referenceFrame.allocLevel(0);
543
544                         const deInt32 frameWidth        = referenceFrame.getWidth();
545                         const deInt32 frameHeight       = referenceFrame.getHeight();
546
547                         tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
548
549                         for (int y = 0; y < frameHeight; y++)
550                         {
551                                 float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
552
553                                 for (int x = 0; x < frameWidth; x++)
554                                 {
555                                         float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
556
557                                         if (xCoord >= -0.5f && xCoord <= 0.5f && yCoord >= -0.5f && yCoord <= 0.5f)
558                                                 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f), x, y);
559                                         else
560                                                 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 1.0f, 1.0f), x, y);
561                                 }
562                         }
563
564                         const vk::VkOffset3D zeroOffset                                 = { 0, 0, 0 };
565                         const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
566                                 vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
567
568                         if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
569                                 referenceFrame.getLevel(0), renderedFrame, 0.05f,
570                                 tcu::COMPARE_LOG_RESULT))
571                         {
572                                 return tcu::TestStatus(QP_TEST_RESULT_FAIL, "Image verification failed");
573                         }
574
575                         return tcu::TestStatus(QP_TEST_RESULT_PASS, "Image verification passed");
576                 }
577         }
578 };
579
580 class LineWidthParamTestInstance : public DynamicStateBaseClass
581 {
582 public:
583         LineWidthParamTestInstance (Context& context, ShaderMap shaders)
584                 : DynamicStateBaseClass (context, shaders[glu::SHADERTYPE_VERTEX], shaders[glu::SHADERTYPE_FRAGMENT])
585         {
586                 // Check if line width test is supported
587                 {
588                         const vk::VkPhysicalDeviceFeatures& deviceFeatures = m_context.getDeviceFeatures();
589
590                         if (!deviceFeatures.wideLines)
591                                 throw tcu::NotSupportedError("Line width test is unsupported");
592                 }
593
594                 m_topology = vk::VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
595
596                 m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, 0.0f, 0.0f, 1.0f), tcu::RGBA::green().toVec()));
597                 m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, 0.0f, 0.0f, 1.0f), tcu::RGBA::green().toVec()));
598
599                 DynamicStateBaseClass::initialize();
600         }
601
602         virtual tcu::TestStatus iterate (void)
603         {
604                 tcu::TestLog &log               = m_context.getTestContext().getLog();
605                 const vk::VkQueue queue = m_context.getUniversalQueue();
606
607                 beginRenderPass();
608
609                 // set states here
610                 vk::VkPhysicalDeviceProperties deviceProperties;
611                 m_context.getInstanceInterface().getPhysicalDeviceProperties(m_context.getPhysicalDevice(), &deviceProperties);
612
613                 setDynamicViewportState(WIDTH, HEIGHT);
614                 setDynamicBlendState();
615                 setDynamicDepthStencilState();
616                 setDynamicRasterizationState(deFloatFloor(deviceProperties.limits.lineWidthRange[1]));
617
618                 m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
619
620                 const vk::VkDeviceSize vertexBufferOffset       = 0;
621                 const vk::VkBuffer vertexBuffer                         = m_vertexBuffer->object();
622                 m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
623
624                 m_vk.cmdDraw(*m_cmdBuffer, static_cast<deUint32>(m_data.size()), 1, 0, 0);
625
626                 m_vk.cmdEndRenderPass(*m_cmdBuffer);
627                 m_vk.endCommandBuffer(*m_cmdBuffer);
628
629                 vk::VkSubmitInfo submitInfo =
630                 {
631                         vk::VK_STRUCTURE_TYPE_SUBMIT_INFO,      // VkStructureType                      sType;
632                         DE_NULL,                                                        // const void*                          pNext;
633                         0,                                                                      // deUint32                                     waitSemaphoreCount;
634                         DE_NULL,                                                        // const VkSemaphore*           pWaitSemaphores;
635                         (const vk::VkPipelineStageFlags*)DE_NULL,
636                         1,                                                                      // deUint32                                     commandBufferCount;
637                         &m_cmdBuffer.get(),                                     // const VkCommandBuffer*       pCommandBuffers;
638                         0,                                                                      // deUint32                                     signalSemaphoreCount;
639                         DE_NULL                                                         // const VkSemaphore*           pSignalSemaphores;
640                 };
641                 m_vk.queueSubmit(queue, 1, &submitInfo, DE_NULL);
642
643                 // validation
644                 {
645                         VK_CHECK(m_vk.queueWaitIdle(queue));
646
647                         tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
648                         referenceFrame.allocLevel(0);
649
650                         const deInt32 frameWidth = referenceFrame.getWidth();
651                         const deInt32 frameHeight = referenceFrame.getHeight();
652
653                         tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
654
655                         for (int y = 0; y < frameHeight; y++)
656                         {
657                                 float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
658
659                                 for (int x = 0; x < frameWidth; x++)
660                                 {
661                                         float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
662                                         float lineHalfWidth = (float)(deFloor(deviceProperties.limits.lineWidthRange[1]) / frameHeight);
663
664                                         if (xCoord >= -1.0f && xCoord <= 1.0f && yCoord >= -lineHalfWidth && yCoord <= lineHalfWidth)
665                                                 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f), x, y);
666                                 }
667                         }
668
669                         const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
670                         const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
671                                                                                                                                                                                           vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT,
672                                                                                                                                                                                           vk::VK_IMAGE_ASPECT_COLOR_BIT);
673
674                         if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
675                                 referenceFrame.getLevel(0), renderedFrame, 0.05f,
676                                 tcu::COMPARE_LOG_RESULT))
677                         {
678                                 return tcu::TestStatus(QP_TEST_RESULT_FAIL, "Image verification failed");
679                         }
680
681                         return tcu::TestStatus(QP_TEST_RESULT_PASS, "Image verification passed");
682                 }
683         }
684 };
685
686 } //anonymous
687
688 DynamicStateRSTests::DynamicStateRSTests (tcu::TestContext& testCtx)
689         : TestCaseGroup (testCtx, "rs_state", "Tests for rasterizer state")
690 {
691         /* Left blank on purpose */
692 }
693
694 DynamicStateRSTests::~DynamicStateRSTests ()
695 {
696 }
697
698 void DynamicStateRSTests::init (void)
699 {
700         ShaderMap shaderPaths;
701         shaderPaths[glu::SHADERTYPE_VERTEX]             = "vulkan/dynamic_state/VertexFetch.vert";
702         shaderPaths[glu::SHADERTYPE_FRAGMENT]   = "vulkan/dynamic_state/VertexFetch.frag";
703
704         addChild(new InstanceFactory<DepthBiasParamTestInstance>(m_testCtx, "depth_bias", "Test depth bias functionality", shaderPaths));
705         addChild(new InstanceFactory<DepthBiasClampParamTestInstance>(m_testCtx, "depth_bias_clamp", "Test depth bias clamp functionality", shaderPaths));
706         addChild(new InstanceFactory<LineWidthParamTestInstance>(m_testCtx, "line_width", "Draw a line with width set to max defined by physical device", shaderPaths));
707 }
708
709 } // DynamicState
710 } // vkt