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