Limit changes by xor to upper 8 bits in mixed atomic tests am: 6bc3c7a634 am: eef2e71...
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / wsi / vktWsiIncrementalPresentTests.cpp
1 /*-------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2016 Google Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Tests for incremental present extension
22  *//*--------------------------------------------------------------------*/
23
24 #include "vktWsiIncrementalPresentTests.hpp"
25
26 #include "vktTestCaseUtil.hpp"
27 #include "vktTestGroupUtil.hpp"
28 #include "vkRefUtil.hpp"
29 #include "vkWsiPlatform.hpp"
30 #include "vkWsiUtil.hpp"
31 #include "vkQueryUtil.hpp"
32 #include "vkDeviceUtil.hpp"
33 #include "vkPlatform.hpp"
34 #include "vkTypeUtil.hpp"
35 #include "vkPrograms.hpp"
36
37 #include "vkWsiUtil.hpp"
38
39 #include "tcuPlatform.hpp"
40 #include "tcuResultCollector.hpp"
41 #include "tcuTestLog.hpp"
42
43 #include <vector>
44 #include <string>
45
46 using std::vector;
47 using std::string;
48
49 using tcu::Maybe;
50 using tcu::UVec2;
51 using tcu::TestLog;
52
53 namespace vkt
54 {
55 namespace wsi
56 {
57 namespace
58 {
59 enum Scaling
60 {
61         SCALING_NONE,
62         SCALING_UP,
63         SCALING_DOWN
64 };
65
66 typedef vector<vk::VkExtensionProperties> Extensions;
67
68 void checkAllSupported (const Extensions& supportedExtensions, const vector<string>& requiredExtensions)
69 {
70         for (vector<string>::const_iterator requiredExtName = requiredExtensions.begin();
71                  requiredExtName != requiredExtensions.end();
72                  ++requiredExtName)
73         {
74                 if (!isExtensionSupported(supportedExtensions, vk::RequiredExtension(*requiredExtName)))
75                         TCU_THROW(NotSupportedError, (*requiredExtName + " is not supported").c_str());
76         }
77 }
78
79 vk::Move<vk::VkInstance> createInstanceWithWsi (const vk::PlatformInterface&            vkp,
80                                                                                                 const Extensions&                                       supportedExtensions,
81                                                                                                 vk::wsi::Type                                           wsiType)
82 {
83         vector<string>  extensions;
84
85         extensions.push_back("VK_KHR_surface");
86         extensions.push_back(getExtensionName(wsiType));
87
88         checkAllSupported(supportedExtensions, extensions);
89
90         return vk::createDefaultInstance(vkp, vector<string>(), extensions);
91 }
92
93 vk::VkPhysicalDeviceFeatures getDeviceNullFeatures (void)
94 {
95         vk::VkPhysicalDeviceFeatures features;
96         deMemset(&features, 0, sizeof(features));
97         return features;
98 }
99
100 deUint32 getNumQueueFamilyIndices (const vk::InstanceInterface& vki, vk::VkPhysicalDevice physicalDevice)
101 {
102         deUint32        numFamilies             = 0;
103
104         vki.getPhysicalDeviceQueueFamilyProperties(physicalDevice, &numFamilies, DE_NULL);
105
106         return numFamilies;
107 }
108
109 vector<deUint32> getSupportedQueueFamilyIndices (const vk::InstanceInterface& vki, vk::VkPhysicalDevice physicalDevice, vk::VkSurfaceKHR surface)
110 {
111         const deUint32          numTotalFamilyIndices   = getNumQueueFamilyIndices(vki, physicalDevice);
112         vector<deUint32>        supportedFamilyIndices;
113
114         for (deUint32 queueFamilyNdx = 0; queueFamilyNdx < numTotalFamilyIndices; ++queueFamilyNdx)
115         {
116                 if (vk::wsi::getPhysicalDeviceSurfaceSupport(vki, physicalDevice, queueFamilyNdx, surface) != VK_FALSE)
117                         supportedFamilyIndices.push_back(queueFamilyNdx);
118         }
119
120         return supportedFamilyIndices;
121 }
122
123 deUint32 chooseQueueFamilyIndex (const vk::InstanceInterface& vki, vk::VkPhysicalDevice physicalDevice, vk::VkSurfaceKHR surface)
124 {
125         const vector<deUint32>  supportedFamilyIndices  = getSupportedQueueFamilyIndices(vki, physicalDevice, surface);
126
127         if (supportedFamilyIndices.empty())
128                 TCU_THROW(NotSupportedError, "Device doesn't support presentation");
129
130         return supportedFamilyIndices[0];
131 }
132
133 vk::Move<vk::VkDevice> createDeviceWithWsi (const vk::InstanceInterface&                vki,
134                                                                                         vk::VkPhysicalDevice                            physicalDevice,
135                                                                                         const Extensions&                                       supportedExtensions,
136                                                                                         const deUint32                                          queueFamilyIndex,
137                                                                                         bool                                                            requiresIncrementalPresent,
138                                                                                         const vk::VkAllocationCallbacks*        pAllocator = DE_NULL)
139 {
140         const float                                                     queuePriorities[]       = { 1.0f };
141         const vk::VkDeviceQueueCreateInfo       queueInfos[]            =
142         {
143                 {
144                         vk::VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
145                         DE_NULL,
146                         (vk::VkDeviceQueueCreateFlags)0,
147                         queueFamilyIndex,
148                         DE_LENGTH_OF_ARRAY(queuePriorities),
149                         &queuePriorities[0]
150                 }
151         };
152         const vk::VkPhysicalDeviceFeatures      features                = getDeviceNullFeatures();
153         const char* const                                       extensions[]    =
154         {
155                 "VK_KHR_swapchain",
156                 "VK_KHR_incremental_present"
157         };
158
159         const vk::VkDeviceCreateInfo            deviceParams    =
160         {
161                 vk::VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
162                 DE_NULL,
163                 (vk::VkDeviceCreateFlags)0,
164                 DE_LENGTH_OF_ARRAY(queueInfos),
165                 &queueInfos[0],
166                 0u,
167                 DE_NULL,
168                 requiresIncrementalPresent ? 2u : 1u,
169                 DE_ARRAY_BEGIN(extensions),
170                 &features
171         };
172
173         for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(extensions); ++ndx)
174         {
175                 if (!isExtensionSupported(supportedExtensions, vk::RequiredExtension(extensions[ndx])))
176                         TCU_THROW(NotSupportedError, (string(extensions[ndx]) + " is not supported").c_str());
177         }
178
179         return createDevice(vki, physicalDevice, &deviceParams, pAllocator);
180 }
181
182 de::MovePtr<vk::wsi::Display> createDisplay (const vk::Platform&        platform,
183                                                                                          const Extensions&              supportedExtensions,
184                                                                                          vk::wsi::Type                  wsiType)
185 {
186         try
187         {
188                 return de::MovePtr<vk::wsi::Display>(platform.createWsiDisplay(wsiType));
189         }
190         catch (const tcu::NotSupportedError& e)
191         {
192                 if (isExtensionSupported(supportedExtensions, vk::RequiredExtension(getExtensionName(wsiType))))
193                 {
194                         // If VK_KHR_{platform}_surface was supported, vk::Platform implementation
195                         // must support creating native display & window for that WSI type.
196                         throw tcu::TestError(e.getMessage());
197                 }
198                 else
199                         throw;
200         }
201 }
202
203 de::MovePtr<vk::wsi::Window> createWindow (const vk::wsi::Display& display, const Maybe<UVec2>& initialSize)
204 {
205         try
206         {
207                 return de::MovePtr<vk::wsi::Window>(display.createWindow(initialSize));
208         }
209         catch (const tcu::NotSupportedError& e)
210         {
211                 // See createDisplay - assuming that wsi::Display was supported platform port
212                 // should also support creating a window.
213                 throw tcu::TestError(e.getMessage());
214         }
215 }
216
217 void initSemaphores (const vk::DeviceInterface&         vkd,
218                                          vk::VkDevice                                   device,
219                                          std::vector<vk::VkSemaphore>&  semaphores)
220 {
221         for (size_t ndx = 0; ndx < semaphores.size(); ndx++)
222                 semaphores[ndx] = createSemaphore(vkd, device).disown();
223 }
224
225 void deinitSemaphores (const vk::DeviceInterface&       vkd,
226                                          vk::VkDevice                                   device,
227                                          std::vector<vk::VkSemaphore>&  semaphores)
228 {
229         for (size_t ndx = 0; ndx < semaphores.size(); ndx++)
230         {
231                 if (semaphores[ndx] != (vk::VkSemaphore)0)
232                         vkd.destroySemaphore(device, semaphores[ndx], DE_NULL);
233
234                 semaphores[ndx] = (vk::VkSemaphore)0;
235         }
236
237         semaphores.clear();
238 }
239
240 void initFences (const vk::DeviceInterface&     vkd,
241                                  vk::VkDevice                           device,
242                                  std::vector<vk::VkFence>&      fences)
243 {
244         for (size_t ndx = 0; ndx < fences.size(); ndx++)
245                 fences[ndx] = createFence(vkd, device).disown();
246 }
247
248 void deinitFences (const vk::DeviceInterface&   vkd,
249                                    vk::VkDevice                                 device,
250                                    std::vector<vk::VkFence>&    fences)
251 {
252         for (size_t ndx = 0; ndx < fences.size(); ndx++)
253         {
254                 if (fences[ndx] != (vk::VkFence)0)
255                         vkd.destroyFence(device, fences[ndx], DE_NULL);
256
257                 fences[ndx] = (vk::VkFence)0;
258         }
259
260         fences.clear();
261 }
262
263 vk::VkRect2D getRenderFrameRect (size_t         frameNdx,
264                                                                  deUint32       imageWidth,
265                                                                  deUint32       imageHeight)
266 {
267         const deUint32          x               = frameNdx == 0
268                                                                 ? 0
269                                                                 : de::min(((deUint32)frameNdx) % imageWidth, imageWidth - 1u);
270         const deUint32          y               = frameNdx == 0
271                                                                 ? 0
272                                                                 : de::min(((deUint32)frameNdx) % imageHeight, imageHeight - 1u);
273         const deUint32          width   = frameNdx == 0
274                                                                 ? imageWidth
275                                                                 : 1 + de::min((deUint32)(frameNdx) % de::min<deUint32>(100, imageWidth / 3), imageWidth - x);
276         const deUint32          height  = frameNdx == 0
277                                                                 ? imageHeight
278                                                                 : 1 + de::min((deUint32)(frameNdx) % de::min<deUint32>(100, imageHeight / 3), imageHeight - y);
279         const vk::VkRect2D      rect    =
280         {
281                 { (deInt32)x, (deInt32)y },
282                 { width, height }
283         };
284
285         DE_ASSERT(width > 0);
286         DE_ASSERT(height > 0);
287
288         return rect;
289 }
290
291 vector<vk::VkRectLayerKHR> getUpdatedRects (size_t              firstFrameNdx,
292                                                                                         size_t          lastFrameNdx,
293                                                                                         deUint32        width,
294                                                                                         deUint32        height)
295 {
296         vector<vk::VkRectLayerKHR> rects;
297
298         for (size_t frameNdx =  firstFrameNdx; frameNdx <= lastFrameNdx; frameNdx++)
299         {
300                 const vk::VkRect2D                      rect            = getRenderFrameRect(frameNdx, width, height);
301                 const vk::VkRectLayerKHR        rectLayer       =
302                 {
303                         rect.offset,
304                         rect.extent,
305                         0
306                 };
307
308                 rects.push_back(rectLayer);
309         }
310
311         return rects;
312 }
313
314 void cmdRenderFrame (const vk::DeviceInterface& vkd,
315                                          vk::VkCommandBuffer            commandBuffer,
316                                          vk::VkPipelineLayout           pipelineLayout,
317                                          vk::VkPipeline                         pipeline,
318                                          size_t                                         frameNdx,
319                                          deUint32                                       imageWidth,
320                                          deUint32                                       imageHeight)
321 {
322         const deUint32 mask = (deUint32)frameNdx;
323
324         if (frameNdx == 0)
325         {
326                 const vk::VkRect2D      scissor =
327                 {
328                         { 0u, 0u },
329                         { imageWidth, imageHeight }
330                 };
331
332                 vkd.cmdSetScissor(commandBuffer, 0u, 1u, &scissor);
333                 const vk::VkClearAttachment     attachment      =
334                 {
335                         vk::VK_IMAGE_ASPECT_COLOR_BIT,
336                         0u,
337                         vk::makeClearValueColorF32(0.25f, 0.50, 0.75f, 1.00f)
338                 };
339                 const vk::VkClearRect           rect            =
340                 {
341                         scissor,
342                         0u,
343                         1u
344                 };
345
346                 vkd.cmdClearAttachments(commandBuffer, 1u, &attachment, 1u, &rect);
347         }
348
349         {
350                 const vk::VkRect2D      scissor = getRenderFrameRect(frameNdx, imageWidth, imageHeight);
351                 vkd.cmdSetScissor(commandBuffer, 0u, 1u, &scissor);
352
353                 vkd.cmdPushConstants(commandBuffer, pipelineLayout, vk::VK_SHADER_STAGE_FRAGMENT_BIT, 0u, 4u, &mask);
354                 vkd.cmdBindPipeline(commandBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
355                 vkd.cmdDraw(commandBuffer, 6u, 1u, 0u, 0u);
356         }
357 }
358
359 vk::Move<vk::VkCommandBuffer> createCommandBuffer (const vk::DeviceInterface&   vkd,
360                                                                                                    vk::VkDevice                                 device,
361                                                                                                    vk::VkCommandPool                    commandPool,
362                                                                                                    vk::VkPipelineLayout                 pipelineLayout,
363                                                                                                    vk::VkRenderPass                             renderPass,
364                                                                                                    vk::VkFramebuffer                    framebuffer,
365                                                                                                    vk::VkPipeline                               pipeline,
366                                                                                                    size_t                                               imageNextFrame,
367                                                                                                    size_t                                               currentFrame,
368                                                                                                    deUint32                                             imageWidth,
369                                                                                                    deUint32                                             imageHeight)
370 {
371         const vk::VkCommandBufferAllocateInfo allocateInfo =
372         {
373                 vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
374                 DE_NULL,
375
376                 commandPool,
377                 vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY,
378                 1
379         };
380         const vk::VkCommandBufferBeginInfo      beginInfo               =
381         {
382                 vk::VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
383                 DE_NULL,
384                 0u,
385                 DE_NULL
386         };
387
388         vk::Move<vk::VkCommandBuffer>   commandBuffer   (vk::allocateCommandBuffer(vkd, device, &allocateInfo));
389         VK_CHECK(vkd.beginCommandBuffer(*commandBuffer, &beginInfo));
390
391         {
392                 const vk::VkClearValue                  clearValue                      = vk::makeClearValueColorF32(0.25f, 0.50f, 0.75f, 1.00f);
393                 const vk::VkRenderPassBeginInfo renderPassBeginInfo     =
394                 {
395                         vk::VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
396                         DE_NULL,
397
398                         renderPass,
399                         framebuffer,
400
401                         {
402                                 { (deInt32)0, (deInt32)0 },
403                                 { imageWidth, imageHeight }
404                         },
405                         1u,
406                         &clearValue
407                 };
408                 vkd.cmdBeginRenderPass(*commandBuffer, &renderPassBeginInfo, vk::VK_SUBPASS_CONTENTS_INLINE);
409         }
410
411         for (size_t frameNdx = imageNextFrame; frameNdx <= currentFrame; frameNdx++)
412                 cmdRenderFrame(vkd, *commandBuffer, pipelineLayout, pipeline, frameNdx, imageWidth, imageHeight);
413
414         vkd.cmdEndRenderPass(*commandBuffer);
415
416         VK_CHECK(vkd.endCommandBuffer(*commandBuffer));
417         return commandBuffer;
418 }
419
420 void deinitCommandBuffers (const vk::DeviceInterface&                   vkd,
421                                                    vk::VkDevice                                                 device,
422                                                    vk::VkCommandPool                                    commandPool,
423                                                    std::vector<vk::VkCommandBuffer>&    commandBuffers)
424 {
425         for (size_t ndx = 0; ndx < commandBuffers.size(); ndx++)
426         {
427                 if (commandBuffers[ndx] != (vk::VkCommandBuffer)0)
428                         vkd.freeCommandBuffers(device, commandPool, 1u,  &commandBuffers[ndx]);
429
430                 commandBuffers[ndx] = (vk::VkCommandBuffer)0;
431         }
432
433         commandBuffers.clear();
434 }
435
436 vk::Move<vk::VkCommandPool> createCommandPool (const vk::DeviceInterface&       vkd,
437                                                                                            vk::VkDevice                                 device,
438                                                                                            deUint32                                             queueFamilyIndex)
439 {
440         const vk::VkCommandPoolCreateInfo createInfo =
441         {
442                 vk::VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
443                 DE_NULL,
444                 0u,
445                 queueFamilyIndex
446         };
447
448         return vk::createCommandPool(vkd, device, &createInfo);
449 }
450
451 vk::Move<vk::VkFramebuffer>     createFramebuffer (const vk::DeviceInterface&   vkd,
452                                                                                            vk::VkDevice                                 device,
453                                                                                            vk::VkRenderPass                             renderPass,
454                                                                                            vk::VkImageView                              imageView,
455                                                                                            deUint32                                             width,
456                                                                                            deUint32                                             height)
457 {
458         const vk::VkFramebufferCreateInfo createInfo =
459         {
460                 vk::VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
461                 DE_NULL,
462
463                 0u,
464                 renderPass,
465                 1u,
466                 &imageView,
467                 width,
468                 height,
469                 1u
470         };
471
472         return vk::createFramebuffer(vkd, device, &createInfo);
473 }
474
475 void initFramebuffers (const vk::DeviceInterface&               vkd,
476                                            vk::VkDevice                                         device,
477                                            vk::VkRenderPass                                     renderPass,
478                                            std::vector<vk::VkImageView>         imageViews,
479                                            deUint32                                                     width,
480                                            deUint32                                                     height,
481                                            std::vector<vk::VkFramebuffer>&      framebuffers)
482 {
483         DE_ASSERT(framebuffers.size() == imageViews.size());
484
485         for (size_t ndx = 0; ndx < framebuffers.size(); ndx++)
486                 framebuffers[ndx] = createFramebuffer(vkd, device, renderPass, imageViews[ndx], width, height).disown();
487 }
488
489 void deinitFramebuffers (const vk::DeviceInterface&                     vkd,
490                                                  vk::VkDevice                                           device,
491                                                  std::vector<vk::VkFramebuffer>&        framebuffers)
492 {
493         for (size_t ndx = 0; ndx < framebuffers.size(); ndx++)
494         {
495                 if (framebuffers[ndx] != (vk::VkFramebuffer)0)
496                         vkd.destroyFramebuffer(device, framebuffers[ndx], DE_NULL);
497
498                 framebuffers[ndx] = (vk::VkFramebuffer)0;
499         }
500
501         framebuffers.clear();
502 }
503
504 vk::Move<vk::VkImageView> createImageView (const vk::DeviceInterface&   vkd,
505                                                                                    vk::VkDevice                                 device,
506                                                                                    vk::VkImage                                  image,
507                                                                                    vk::VkFormat                                 format)
508 {
509         const vk::VkImageViewCreateInfo createInfo =
510         {
511                 vk::VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
512                 DE_NULL,
513
514                 0u,
515                 image,
516                 vk::VK_IMAGE_VIEW_TYPE_2D,
517                 format,
518                 vk::makeComponentMappingRGBA(),
519                 {
520                         vk::VK_IMAGE_ASPECT_COLOR_BIT,
521                         0u,
522                         1u,
523                         0u,
524                         1u
525                 }
526         };
527
528         return vk::createImageView(vkd, device, &createInfo, DE_NULL);
529 }
530
531 void initImageViews (const vk::DeviceInterface&                 vkd,
532                                          vk::VkDevice                                           device,
533                                          const std::vector<vk::VkImage>&        images,
534                                          vk::VkFormat                                           format,
535                                          std::vector<vk::VkImageView>&          imageViews)
536 {
537         DE_ASSERT(images.size() == imageViews.size());
538
539         for (size_t ndx = 0; ndx < imageViews.size(); ndx++)
540                 imageViews[ndx] = createImageView(vkd, device, images[ndx], format).disown();
541 }
542
543 void deinitImageViews (const vk::DeviceInterface&               vkd,
544                                            vk::VkDevice                                         device,
545                                            std::vector<vk::VkImageView>&        imageViews)
546 {
547         for (size_t ndx = 0; ndx < imageViews.size(); ndx++)
548         {
549                 if (imageViews[ndx] != (vk::VkImageView)0)
550                         vkd.destroyImageView(device, imageViews[ndx], DE_NULL);
551
552                 imageViews[ndx] = (vk::VkImageView)0;
553         }
554
555         imageViews.clear();
556 }
557
558 vk::Move<vk::VkRenderPass> createRenderPass (const vk::DeviceInterface& vkd,
559                                                                                          vk::VkDevice                           device,
560                                                                                          vk::VkFormat                           format)
561 {
562         const vk::VkAttachmentDescription       attachments[]                   =
563         {
564                 {
565                         0u,
566                         format,
567                         vk::VK_SAMPLE_COUNT_1_BIT,
568
569                         vk::VK_ATTACHMENT_LOAD_OP_LOAD,
570                         vk::VK_ATTACHMENT_STORE_OP_STORE,
571
572                         vk::VK_ATTACHMENT_LOAD_OP_DONT_CARE,
573                         vk::VK_ATTACHMENT_STORE_OP_DONT_CARE,
574
575                         vk::VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
576                         vk::VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
577                 }
578         };
579         const vk::VkAttachmentReference         colorAttachmentRefs[]   =
580         {
581                 {
582                         0u,
583                         vk::VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
584                 }
585         };
586         const vk::VkSubpassDescription          subpasses[]                             =
587         {
588                 {
589                         0u,
590                         vk::VK_PIPELINE_BIND_POINT_GRAPHICS,
591                         0u,
592                         DE_NULL,
593
594                         DE_LENGTH_OF_ARRAY(colorAttachmentRefs),
595                         colorAttachmentRefs,
596                         DE_NULL,
597
598                         DE_NULL,
599                         0u,
600                         DE_NULL
601                 }
602         };
603
604         const vk::VkRenderPassCreateInfo        createInfo                              =
605         {
606                 vk::VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
607                 DE_NULL,
608                 0u,
609
610                 DE_LENGTH_OF_ARRAY(attachments),
611                 attachments,
612
613                 DE_LENGTH_OF_ARRAY(subpasses),
614                 subpasses,
615
616                 0u,
617                 DE_NULL
618         };
619
620         return vk::createRenderPass(vkd, device, &createInfo);
621 }
622
623 vk::Move<vk::VkPipeline> createPipeline (const vk::DeviceInterface&     vkd,
624                                                                                  vk::VkDevice                           device,
625                                                                                  vk::VkRenderPass                       renderPass,
626                                                                                  vk::VkPipelineLayout           layout,
627                                                                                  vk::VkShaderModule                     vertexShaderModule,
628                                                                                  vk::VkShaderModule                     fragmentShaderModule,
629                                                                                  deUint32                                       width,
630                                                                                  deUint32                                       height)
631 {
632         const vk::VkSpecializationInfo                          shaderSpecialization    =
633         {
634                 0u,
635                 DE_NULL,
636                 0,
637                 DE_NULL
638         };
639         const vk::VkPipelineShaderStageCreateInfo               stages[]                        =
640         {
641                 {
642                         vk::VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
643                         DE_NULL,
644                         0u,
645                         vk::VK_SHADER_STAGE_VERTEX_BIT,
646                         vertexShaderModule,
647                         "main",
648                         &shaderSpecialization
649                 },
650                 {
651                         vk::VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
652                         DE_NULL,
653                         0u,
654                         vk::VK_SHADER_STAGE_FRAGMENT_BIT,
655                         fragmentShaderModule,
656                         "main",
657                         &shaderSpecialization
658                 }
659         };
660         const vk::VkPipelineVertexInputStateCreateInfo  vertexInputState        =
661         {
662                 vk::VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
663                 DE_NULL,
664                 0u,
665                 0u,
666                 DE_NULL,
667                 0u,
668                 DE_NULL
669         };
670         const vk::VkPipelineInputAssemblyStateCreateInfo        inputAssemblyState      =
671         {
672                 vk::VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
673                 DE_NULL,
674                 0u,
675                 vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
676                 VK_FALSE
677         };
678         const vk::VkViewport viewports[] =
679         {
680                 {
681                         0.0f, 0.0f,
682                         (float)width, (float)height,
683                         0.0f, 1.0f
684                 }
685         };
686         const vk::VkRect2D scissors[] =
687         {
688                 {
689                         { 0u, 0u },
690                         { width, height }
691                 }
692         };
693         const vk::VkPipelineViewportStateCreateInfo                     viewportState           =
694         {
695                 vk::VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
696                 DE_NULL,
697                 0u,
698
699                 DE_LENGTH_OF_ARRAY(viewports),
700                 viewports,
701                 DE_LENGTH_OF_ARRAY(scissors),
702                 scissors
703         };
704         const vk::VkPipelineRasterizationStateCreateInfo        rasterizationState      =
705         {
706                 vk::VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
707                 DE_NULL,
708                 0u,
709                 VK_TRUE,
710                 VK_FALSE,
711                 vk::VK_POLYGON_MODE_FILL,
712                 vk::VK_CULL_MODE_NONE,
713                 vk::VK_FRONT_FACE_CLOCKWISE,
714                 VK_FALSE,
715                 0.0f,
716                 0.0f,
717                 0.0f,
718                 1.0f
719         };
720         const vk::VkSampleMask                                                          sampleMask                      = ~0u;
721         const vk::VkPipelineMultisampleStateCreateInfo          multisampleState        =
722         {
723                 vk::VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
724                 DE_NULL,
725                 0u,
726                 vk::VK_SAMPLE_COUNT_1_BIT,
727                 VK_FALSE,
728                 0.0f,
729                 &sampleMask,
730                 VK_FALSE,
731                 VK_FALSE
732         };
733         const vk::VkPipelineDepthStencilStateCreateInfo depthStencilState               =
734         {
735                 vk::VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
736                 DE_NULL,
737                 0u,
738                 DE_FALSE,
739                 DE_FALSE,
740                 vk::VK_COMPARE_OP_ALWAYS,
741                 DE_FALSE,
742                 DE_FALSE,
743                 {
744                         vk::VK_STENCIL_OP_KEEP,
745                         vk::VK_STENCIL_OP_KEEP,
746                         vk::VK_STENCIL_OP_KEEP,
747                         vk::VK_COMPARE_OP_ALWAYS,
748                         0u,
749                         0u,
750                         0u,
751                 },
752                 {
753                         vk::VK_STENCIL_OP_KEEP,
754                         vk::VK_STENCIL_OP_KEEP,
755                         vk::VK_STENCIL_OP_KEEP,
756                         vk::VK_COMPARE_OP_ALWAYS,
757                         0u,
758                         0u,
759                         0u,
760                 },
761                 0.0f,
762                 1.0f
763         };
764         const vk::VkPipelineColorBlendAttachmentState   attachmentBlendState                    =
765         {
766                 VK_FALSE,
767                 vk::VK_BLEND_FACTOR_ONE,
768                 vk::VK_BLEND_FACTOR_ZERO,
769                 vk::VK_BLEND_OP_ADD,
770                 vk::VK_BLEND_FACTOR_ONE,
771                 vk::VK_BLEND_FACTOR_ZERO,
772                 vk::VK_BLEND_OP_ADD,
773                 (vk::VK_COLOR_COMPONENT_R_BIT|
774                  vk::VK_COLOR_COMPONENT_G_BIT|
775                  vk::VK_COLOR_COMPONENT_B_BIT|
776                  vk::VK_COLOR_COMPONENT_A_BIT),
777         };
778         const vk::VkPipelineColorBlendStateCreateInfo   blendState                              =
779         {
780                 vk::VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
781                 DE_NULL,
782                 0u,
783                 DE_FALSE,
784                 vk::VK_LOGIC_OP_COPY,
785                 1u,
786                 &attachmentBlendState,
787                 { 0.0f, 0.0f, 0.0f, 0.0f }
788         };
789         const vk::VkDynamicState                                                        dynamicStates[]         =
790         {
791                 vk::VK_DYNAMIC_STATE_SCISSOR
792         };
793         const vk::VkPipelineDynamicStateCreateInfo                      dynamicState            =
794         {
795                 vk::VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
796                 DE_NULL,
797                 0u,
798
799                 DE_LENGTH_OF_ARRAY(dynamicStates),
800                 dynamicStates
801         };
802         const vk::VkGraphicsPipelineCreateInfo                          createInfo                      =
803         {
804                 vk::VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
805                 DE_NULL,
806                 0u,
807
808                 DE_LENGTH_OF_ARRAY(stages),
809                 stages,
810                 &vertexInputState,
811                 &inputAssemblyState,
812                 DE_NULL,
813                 &viewportState,
814                 &rasterizationState,
815                 &multisampleState,
816                 &depthStencilState,
817                 &blendState,
818                 &dynamicState,
819                 layout,
820                 renderPass,
821                 0u,
822                 DE_NULL,
823                 0u
824         };
825
826         return vk::createGraphicsPipeline(vkd, device, DE_NULL,  &createInfo);
827 }
828
829 vk::Move<vk::VkPipelineLayout> createPipelineLayout (const vk::DeviceInterface& vkd,
830                                                                                                          vk::VkDevice                           device)
831 {
832         const vk::VkPushConstantRange                   pushConstants[] =
833         {
834                 {
835                         vk::VK_SHADER_STAGE_FRAGMENT_BIT,
836                         0u,
837                         4u
838                 }
839         };
840         const vk::VkPipelineLayoutCreateInfo    createInfo      =
841         {
842                 vk::VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
843                 DE_NULL,
844                 0u,
845
846                 0u,
847                 DE_NULL,
848
849                 DE_LENGTH_OF_ARRAY(pushConstants),
850                 pushConstants
851         };
852
853         return vk::createPipelineLayout(vkd, device, &createInfo);
854 }
855
856 struct TestConfig
857 {
858         vk::wsi::Type                   wsiType;
859         Scaling                                 scaling;
860         bool                                    useIncrementalPresent;
861         vk::VkPresentModeKHR    presentMode;
862 };
863
864 class IncrementalPresentTestInstance : public TestInstance
865 {
866 public:
867                                                                                                         IncrementalPresentTestInstance  (Context& context, const TestConfig& testConfig);
868                                                                                                         ~IncrementalPresentTestInstance (void);
869
870         tcu::TestStatus                                                                 iterate                                                 (void);
871
872 private:
873         const TestConfig                                                                m_testConfig;
874         const bool                                                                              m_useIncrementalPresent;
875         const vk::PlatformInterface&                                    m_vkp;
876         const Extensions                                                                m_instanceExtensions;
877         const vk::Unique<vk::VkInstance>                                m_instance;
878         const vk::InstanceDriver                                                m_vki;
879         const vk::VkPhysicalDevice                                              m_physicalDevice;
880         const de::UniquePtr<vk::wsi::Display>                   m_nativeDisplay;
881         const de::UniquePtr<vk::wsi::Window>                    m_nativeWindow;
882         const vk::Unique<vk::VkSurfaceKHR>                              m_surface;
883
884         const deUint32                                                                  m_queueFamilyIndex;
885         const Extensions                                                                m_deviceExtensions;
886         const vk::Unique<vk::VkDevice>                                  m_device;
887         const vk::DeviceDriver                                                  m_vkd;
888         const vk::VkQueue                                                               m_queue;
889
890         const vk::Unique<vk::VkCommandPool>                             m_commandPool;
891         const vk::Unique<vk::VkShaderModule>                    m_vertexShaderModule;
892         const vk::Unique<vk::VkShaderModule>                    m_fragmentShaderModule;
893         const vk::Unique<vk::VkPipelineLayout>                  m_pipelineLayout;
894
895         const vk::VkSurfaceCapabilitiesKHR                              m_surfaceProperties;
896         const vector<vk::VkSurfaceFormatKHR>                    m_surfaceFormats;
897         const vector<vk::VkPresentModeKHR>                              m_presentModes;
898
899         tcu::ResultCollector                                                    m_resultCollector;
900
901         vk::Move<vk::VkSwapchainKHR>                                    m_swapchain;
902         std::vector<vk::VkImage>                                                m_swapchainImages;
903         std::vector<size_t>                                                             m_imageNextFrames;
904
905         vk::Move<vk::VkRenderPass>                                              m_renderPass;
906         vk::Move<vk::VkPipeline>                                                m_pipeline;
907
908         std::vector<vk::VkImageView>                                    m_swapchainImageViews;
909         std::vector<vk::VkFramebuffer>                                  m_framebuffers;
910         std::vector<vk::VkCommandBuffer>                                m_commandBuffers;
911         std::vector<vk::VkSemaphore>                                    m_acquireSemaphores;
912         std::vector<vk::VkSemaphore>                                    m_renderSemaphores;
913         std::vector<vk::VkFence>                                                m_fences;
914
915         vk::VkSemaphore                                                                 m_freeAcquireSemaphore;
916         vk::VkSemaphore                                                                 m_freeRenderSemaphore;
917
918         std::vector<vk::VkSwapchainCreateInfoKHR>               m_swapchainConfigs;
919         size_t                                                                                  m_swapchainConfigNdx;
920
921         const size_t                                                                    m_frameCount;
922         size_t                                                                                  m_frameNdx;
923
924         const size_t                                                                    m_maxOutOfDateCount;
925         size_t                                                                                  m_outOfDateCount;
926
927         void                                                                                    initSwapchainResources          (void);
928         void                                                                                    deinitSwapchainResources        (void);
929         void                                                                                    render                                          (void);
930 };
931
932 std::vector<vk::VkSwapchainCreateInfoKHR> generateSwapchainConfigs (vk::VkSurfaceKHR                                            surface,
933                                                                                                                                         deUint32                                                                queueFamilyIndex,
934                                                                                                                                         Scaling                                                                 scaling,
935                                                                                                                                         const vk::VkSurfaceCapabilitiesKHR&             properties,
936                                                                                                                                         const vector<vk::VkSurfaceFormatKHR>&   formats,
937                                                                                                                                         const vector<vk::VkPresentModeKHR>&             presentModes,
938                                                                                                                                         vk::VkPresentModeKHR                                    presentMode)
939 {
940         const deUint32                                                  imageLayers                     = 1u;
941         const vk::VkImageUsageFlags                             imageUsage                      = properties.supportedUsageFlags;
942         const vk::VkBool32                                              clipped                         = VK_FALSE;
943         vector<vk::VkSwapchainCreateInfoKHR>    createInfos;
944
945         const deUint32                          imageWidth              = scaling == SCALING_NONE
946                                                                                                 ? (properties.currentExtent.width != 0xFFFFFFFFu
947                                                                                                         ? properties.currentExtent.width
948                                                                                                         : de::min(1024u, properties.minImageExtent.width + ((properties.maxImageExtent.width - properties.minImageExtent.width) / 2)))
949                                                                                                 : (scaling == SCALING_UP
950                                                                                                         ? de::max(31u, properties.minImageExtent.width)
951                                                                                                         : properties.maxImageExtent.width);
952         const deUint32                          imageHeight             = scaling == SCALING_NONE
953                                                                                                 ? (properties.currentExtent.height != 0xFFFFFFFFu
954                                                                                                         ? properties.currentExtent.height
955                                                                                                         : de::min(1024u, properties.minImageExtent.height + ((properties.maxImageExtent.height - properties.minImageExtent.height) / 2)))
956                                                                                                 : (scaling == SCALING_UP
957                                                                                                         ? de::max(31u, properties.minImageExtent.height)
958                                                                                                         : properties.maxImageExtent.height);
959         const vk::VkExtent2D            imageSize               = { imageWidth, imageHeight };
960
961         {
962                 size_t presentModeNdx;
963
964                 for (presentModeNdx = 0; presentModeNdx < presentModes.size(); presentModeNdx++)
965                 {
966                         if (presentModes[presentModeNdx] == presentMode)
967                                 break;
968                 }
969
970                 if (presentModeNdx == presentModes.size())
971                         TCU_THROW(NotSupportedError, "Present mode not supported");
972         }
973
974         for (size_t formatNdx = 0; formatNdx < formats.size(); formatNdx++)
975         {
976                 for (vk::VkSurfaceTransformFlagsKHR transform = 1u; transform <= properties.supportedTransforms; transform = transform << 1u)
977                 {
978                         if ((properties.supportedTransforms & transform) == 0)
979                                 continue;
980
981                         for (vk::VkCompositeAlphaFlagsKHR alpha = 1u; alpha <= properties.supportedCompositeAlpha; alpha = alpha << 1u)
982                         {
983                                 if ((alpha & properties.supportedCompositeAlpha) == 0)
984                                         continue;
985
986                                 const vk::VkSurfaceTransformFlagBitsKHR preTransform    = (vk::VkSurfaceTransformFlagBitsKHR)transform;
987                                 const vk::VkCompositeAlphaFlagBitsKHR   compositeAlpha  = (vk::VkCompositeAlphaFlagBitsKHR)alpha;
988                                 const vk::VkFormat                                              imageFormat             = formats[formatNdx].format;
989                                 const vk::VkColorSpaceKHR                               imageColorSpace = formats[formatNdx].colorSpace;
990                                 const vk::VkSwapchainCreateInfoKHR              createInfo              =
991                                 {
992                                         vk::VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
993                                         DE_NULL,
994                                         0u,
995                                         surface,
996                                         properties.minImageCount,
997                                         imageFormat,
998                                         imageColorSpace,
999                                         imageSize,
1000                                         imageLayers,
1001                                         imageUsage,
1002                                         vk::VK_SHARING_MODE_EXCLUSIVE,
1003                                         1u,
1004                                         &queueFamilyIndex,
1005                                         preTransform,
1006                                         compositeAlpha,
1007                                         presentMode,
1008                                         clipped,
1009                                         (vk::VkSwapchainKHR)0
1010                                 };
1011
1012                                 createInfos.push_back(createInfo);
1013                         }
1014                 }
1015         }
1016
1017         return createInfos;
1018 }
1019
1020 IncrementalPresentTestInstance::IncrementalPresentTestInstance (Context& context, const TestConfig& testConfig)
1021         : TestInstance                          (context)
1022         , m_testConfig                          (testConfig)
1023         , m_useIncrementalPresent       (testConfig.useIncrementalPresent)
1024         , m_vkp                                         (context.getPlatformInterface())
1025         , m_instanceExtensions          (vk::enumerateInstanceExtensionProperties(m_vkp, DE_NULL))
1026         , m_instance                            (createInstanceWithWsi(m_vkp, m_instanceExtensions, testConfig.wsiType))
1027         , m_vki                                         (m_vkp, *m_instance)
1028         , m_physicalDevice                      (vk::chooseDevice(m_vki, *m_instance, context.getTestContext().getCommandLine()))
1029         , m_nativeDisplay                       (createDisplay(context.getTestContext().getPlatform().getVulkanPlatform(), m_instanceExtensions, testConfig.wsiType))
1030         , m_nativeWindow                        (createWindow(*m_nativeDisplay, tcu::nothing<UVec2>()))
1031         , m_surface                                     (vk::wsi::createSurface(m_vki, *m_instance, testConfig.wsiType, *m_nativeDisplay, *m_nativeWindow))
1032
1033         , m_queueFamilyIndex            (chooseQueueFamilyIndex(m_vki, m_physicalDevice, *m_surface))
1034         , m_deviceExtensions            (vk::enumerateDeviceExtensionProperties(m_vki, m_physicalDevice, DE_NULL))
1035         , m_device                                      (createDeviceWithWsi(m_vki, m_physicalDevice, m_deviceExtensions, m_queueFamilyIndex, testConfig.useIncrementalPresent))
1036         , m_vkd                                         (m_vki, *m_device)
1037         , m_queue                                       (getDeviceQueue(m_vkd, *m_device, m_queueFamilyIndex, 0u))
1038
1039         , m_commandPool                         (createCommandPool(m_vkd, *m_device, m_queueFamilyIndex))
1040         , m_vertexShaderModule          (vk::createShaderModule(m_vkd, *m_device, context.getBinaryCollection().get("quad-vert"), 0u))
1041         , m_fragmentShaderModule        (vk::createShaderModule(m_vkd, *m_device, context.getBinaryCollection().get("quad-frag"), 0u))
1042         , m_pipelineLayout                      (createPipelineLayout(m_vkd, *m_device))
1043
1044         , m_surfaceProperties           (vk::wsi::getPhysicalDeviceSurfaceCapabilities(m_vki, m_physicalDevice, *m_surface))
1045         , m_surfaceFormats                      (vk::wsi::getPhysicalDeviceSurfaceFormats(m_vki, m_physicalDevice, *m_surface))
1046         , m_presentModes                        (vk::wsi::getPhysicalDeviceSurfacePresentModes(m_vki, m_physicalDevice, *m_surface))
1047
1048         , m_freeAcquireSemaphore        ((vk::VkSemaphore)0)
1049         , m_freeRenderSemaphore         ((vk::VkSemaphore)0)
1050
1051         , m_swapchainConfigs            (generateSwapchainConfigs(*m_surface, m_queueFamilyIndex, testConfig.scaling, m_surfaceProperties, m_surfaceFormats, m_presentModes, testConfig.presentMode))
1052         , m_swapchainConfigNdx          (0u)
1053
1054         , m_frameCount                          (60u * 5u)
1055         , m_frameNdx                            (0u)
1056
1057         , m_maxOutOfDateCount           (20u)
1058         , m_outOfDateCount                      (0u)
1059 {
1060         {
1061                 const tcu::ScopedLogSection surfaceInfo (m_context.getTestContext().getLog(), "SurfaceCapabilities", "SurfaceCapabilities");
1062                 m_context.getTestContext().getLog() << TestLog::Message << m_surfaceProperties << TestLog::EndMessage;
1063         }
1064 }
1065
1066 IncrementalPresentTestInstance::~IncrementalPresentTestInstance (void)
1067 {
1068         deinitSwapchainResources();
1069 }
1070
1071 void IncrementalPresentTestInstance::initSwapchainResources (void)
1072 {
1073         const size_t            fenceCount      = 6;
1074         const deUint32          imageWidth      = m_swapchainConfigs[m_swapchainConfigNdx].imageExtent.width;
1075         const deUint32          imageHeight     = m_swapchainConfigs[m_swapchainConfigNdx].imageExtent.height;
1076         const vk::VkFormat      imageFormat     = m_swapchainConfigs[m_swapchainConfigNdx].imageFormat;
1077
1078         m_swapchain                             = vk::createSwapchainKHR(m_vkd, *m_device, &m_swapchainConfigs[m_swapchainConfigNdx]);
1079         m_swapchainImages               = vk::wsi::getSwapchainImages(m_vkd, *m_device, *m_swapchain);
1080
1081         m_imageNextFrames.resize(m_swapchainImages.size(), 0);
1082
1083         m_renderPass                    = createRenderPass(m_vkd, *m_device, imageFormat);
1084         m_pipeline                              = createPipeline(m_vkd, *m_device, *m_renderPass, *m_pipelineLayout, *m_vertexShaderModule, *m_fragmentShaderModule, imageWidth, imageHeight);
1085
1086         m_swapchainImageViews   = std::vector<vk::VkImageView>(m_swapchainImages.size(), (vk::VkImageView)0);
1087         m_framebuffers                  = std::vector<vk::VkFramebuffer>(m_swapchainImages.size(), (vk::VkFramebuffer)0);
1088         m_acquireSemaphores             = std::vector<vk::VkSemaphore>(m_swapchainImages.size(), (vk::VkSemaphore)0);
1089         m_renderSemaphores              = std::vector<vk::VkSemaphore>(m_swapchainImages.size(), (vk::VkSemaphore)0);
1090
1091         m_fences                                = std::vector<vk::VkFence>(fenceCount, (vk::VkFence)0);
1092         m_commandBuffers                = std::vector<vk::VkCommandBuffer>(m_fences.size(), (vk::VkCommandBuffer)0);
1093
1094         m_freeAcquireSemaphore  = (vk::VkSemaphore)0;
1095         m_freeRenderSemaphore   = (vk::VkSemaphore)0;
1096
1097         m_freeAcquireSemaphore  = createSemaphore(m_vkd, *m_device).disown();
1098         m_freeRenderSemaphore   = createSemaphore(m_vkd, *m_device).disown();
1099
1100         initImageViews(m_vkd, *m_device, m_swapchainImages, imageFormat, m_swapchainImageViews);
1101         initFramebuffers(m_vkd, *m_device, *m_renderPass, m_swapchainImageViews, imageWidth, imageHeight, m_framebuffers);
1102         initSemaphores(m_vkd, *m_device, m_acquireSemaphores);
1103         initSemaphores(m_vkd, *m_device, m_renderSemaphores);
1104
1105         initFences(m_vkd, *m_device, m_fences);
1106 }
1107
1108 void IncrementalPresentTestInstance::deinitSwapchainResources (void)
1109 {
1110         VK_CHECK(m_vkd.queueWaitIdle(m_queue));
1111
1112         if (m_freeAcquireSemaphore != (vk::VkSemaphore)0)
1113         {
1114                 m_vkd.destroySemaphore(*m_device, m_freeAcquireSemaphore, DE_NULL);
1115                 m_freeAcquireSemaphore = (vk::VkSemaphore)0;
1116         }
1117
1118         if (m_freeRenderSemaphore != (vk::VkSemaphore)0)
1119         {
1120                 m_vkd.destroySemaphore(*m_device, m_freeRenderSemaphore, DE_NULL);
1121                 m_freeRenderSemaphore = (vk::VkSemaphore)0;
1122         }
1123
1124         deinitSemaphores(m_vkd, *m_device, m_acquireSemaphores);
1125         deinitSemaphores(m_vkd, *m_device, m_renderSemaphores);
1126         deinitFences(m_vkd, *m_device, m_fences);
1127         deinitCommandBuffers(m_vkd, *m_device, *m_commandPool, m_commandBuffers);
1128         deinitFramebuffers(m_vkd, *m_device, m_framebuffers);
1129         deinitImageViews(m_vkd, *m_device, m_swapchainImageViews);
1130
1131         m_swapchainImages.clear();
1132         m_imageNextFrames.clear();
1133
1134         m_swapchain             = vk::Move<vk::VkSwapchainKHR>();
1135         m_renderPass    = vk::Move<vk::VkRenderPass>();
1136         m_pipeline              = vk::Move<vk::VkPipeline>();
1137
1138 }
1139
1140 void IncrementalPresentTestInstance::render (void)
1141 {
1142         const deUint64          foreverNs               = 0xFFFFFFFFFFFFFFFFul;
1143         const vk::VkFence       fence                   = m_fences[m_frameNdx % m_fences.size()];
1144         const deUint32          width                   = m_swapchainConfigs[m_swapchainConfigNdx].imageExtent.width;
1145         const deUint32          height                  = m_swapchainConfigs[m_swapchainConfigNdx].imageExtent.height;
1146         size_t                          imageNextFrame;
1147
1148         // Throttle execution
1149         if (m_frameNdx >= m_fences.size())
1150         {
1151                 VK_CHECK(m_vkd.waitForFences(*m_device, 1u, &fence, VK_TRUE, foreverNs));
1152                 VK_CHECK(m_vkd.resetFences(*m_device, 1u, &fence));
1153
1154                 m_vkd.freeCommandBuffers(*m_device, *m_commandPool, 1u, &m_commandBuffers[m_frameNdx % m_commandBuffers.size()]);
1155                 m_commandBuffers[m_frameNdx % m_commandBuffers.size()] = (vk::VkCommandBuffer)0;
1156         }
1157
1158         vk::VkSemaphore         currentAcquireSemaphore = m_freeAcquireSemaphore;
1159         vk::VkSemaphore         currentRenderSemaphore  = m_freeRenderSemaphore;
1160         deUint32                        imageIndex;
1161
1162         // Acquire next image
1163         VK_CHECK(m_vkd.acquireNextImageKHR(*m_device, *m_swapchain, foreverNs, currentAcquireSemaphore, fence, &imageIndex));
1164
1165         // Create command buffer
1166         {
1167                 imageNextFrame = m_imageNextFrames[imageIndex];
1168                 m_commandBuffers[m_frameNdx % m_commandBuffers.size()] = createCommandBuffer(m_vkd, *m_device, *m_commandPool, *m_pipelineLayout, *m_renderPass, m_framebuffers[imageIndex], *m_pipeline, imageNextFrame, m_frameNdx, width, height).disown();
1169                 m_imageNextFrames[imageIndex] = m_frameNdx + 1;
1170         }
1171
1172         // Submit command buffer
1173         {
1174                 const vk::VkPipelineStageFlags  dstStageMask    = vk::VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1175                 const vk::VkSubmitInfo                  submitInfo              =
1176                 {
1177                         vk::VK_STRUCTURE_TYPE_SUBMIT_INFO,
1178                         DE_NULL,
1179                         1u,
1180                         &currentAcquireSemaphore,
1181                         &dstStageMask,
1182                         1u,
1183                         &m_commandBuffers[m_frameNdx % m_commandBuffers.size()],
1184                         1u,
1185                         &currentRenderSemaphore
1186                 };
1187
1188                 VK_CHECK(m_vkd.queueSubmit(m_queue, 1u, &submitInfo, (vk::VkFence)0));
1189         }
1190
1191         // Present frame
1192         if (m_useIncrementalPresent)
1193         {
1194                 vk::VkResult result;
1195                 const vector<vk::VkRectLayerKHR>        rects           = getUpdatedRects(imageNextFrame, m_frameNdx, width, height);
1196                 const vk::VkPresentRegionKHR            region          =
1197                 {
1198                         (deUint32)rects.size(),
1199                         rects.empty() ? DE_NULL : &rects[0]
1200                 };
1201                 const vk::VkPresentRegionsKHR   regionInfo      =
1202                 {
1203                         vk::VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR,
1204                         DE_NULL,
1205                         1u,
1206                         &region
1207                 };
1208                 const vk::VkPresentInfoKHR presentInfo =
1209                 {
1210                         vk::VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1211                         &regionInfo,
1212                         1u,
1213                         &currentRenderSemaphore,
1214                         1u,
1215                         &*m_swapchain,
1216                         &imageIndex,
1217                         &result
1218                 };
1219
1220                 VK_CHECK(m_vkd.queuePresentKHR(m_queue, &presentInfo));
1221                 VK_CHECK(result);
1222         }
1223         else
1224         {
1225                 vk::VkResult result;
1226                 const vk::VkPresentInfoKHR presentInfo =
1227                 {
1228                         vk::VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1229                         DE_NULL,
1230                         1u,
1231                         &currentRenderSemaphore,
1232                         1u,
1233                         &*m_swapchain,
1234                         &imageIndex,
1235                         &result
1236                 };
1237
1238                 VK_CHECK(m_vkd.queuePresentKHR(m_queue, &presentInfo));
1239                 VK_CHECK(result);
1240         }
1241
1242         {
1243                 m_freeAcquireSemaphore = m_acquireSemaphores[imageIndex];
1244                 m_acquireSemaphores[imageIndex] = currentAcquireSemaphore;
1245
1246                 m_freeRenderSemaphore = m_renderSemaphores[imageIndex];
1247                 m_renderSemaphores[imageIndex] = currentRenderSemaphore;
1248         }
1249 }
1250
1251 tcu::TestStatus IncrementalPresentTestInstance::iterate (void)
1252 {
1253         // Initialize swapchain specific resources
1254         // Render test
1255         try
1256         {
1257                 if (m_frameNdx == 0)
1258                 {
1259                         if (m_outOfDateCount == 0)
1260                                 m_context.getTestContext().getLog() << tcu::TestLog::Message << "Swapchain: " << m_swapchainConfigs[m_swapchainConfigNdx] << tcu::TestLog::EndMessage;
1261
1262                         initSwapchainResources();
1263                 }
1264
1265                 render();
1266         }
1267         catch (const vk::Error& error)
1268         {
1269                 if (error.getError() == vk::VK_ERROR_OUT_OF_DATE_KHR)
1270                 {
1271                         m_swapchainConfigs = generateSwapchainConfigs(*m_surface, m_queueFamilyIndex, m_testConfig.scaling, m_surfaceProperties, m_surfaceFormats, m_presentModes, m_testConfig.presentMode);
1272
1273                         if (m_outOfDateCount < m_maxOutOfDateCount)
1274                         {
1275                                 m_context.getTestContext().getLog() << TestLog::Message << "Frame " << m_frameNdx << ": Swapchain out of date. Recreating resources." << TestLog::EndMessage;
1276                                 deinitSwapchainResources();
1277                                 m_frameNdx = 0;
1278                                 m_outOfDateCount++;
1279
1280                                 return tcu::TestStatus::incomplete();
1281                         }
1282                         else
1283                         {
1284                                 m_context.getTestContext().getLog() << TestLog::Message << "Frame " << m_frameNdx << ": Swapchain out of date." << TestLog::EndMessage;
1285                                 m_resultCollector.fail("Received too many VK_ERROR_OUT_OF_DATE_KHR errors. Received " + de::toString(m_outOfDateCount) + ", max " + de::toString(m_maxOutOfDateCount));
1286                         }
1287                 }
1288                 else
1289                 {
1290                         m_resultCollector.fail(error.what());
1291                 }
1292
1293                 deinitSwapchainResources();
1294
1295                 m_swapchainConfigNdx++;
1296                 m_frameNdx = 0;
1297                 m_outOfDateCount = 0;
1298
1299                 if (m_swapchainConfigNdx >= m_swapchainConfigs.size())
1300                         return tcu::TestStatus(m_resultCollector.getResult(), m_resultCollector.getMessage());
1301                 else
1302                         return tcu::TestStatus::incomplete();
1303         }
1304
1305         m_frameNdx++;
1306
1307         if (m_frameNdx >= m_frameCount)
1308         {
1309                 m_frameNdx = 0;
1310                 m_outOfDateCount = 0;
1311                 m_swapchainConfigNdx++;
1312
1313                 deinitSwapchainResources();
1314
1315                 if (m_swapchainConfigNdx >= m_swapchainConfigs.size())
1316                         return tcu::TestStatus(m_resultCollector.getResult(), m_resultCollector.getMessage());
1317                 else
1318                         return tcu::TestStatus::incomplete();
1319         }
1320         else
1321                 return tcu::TestStatus::incomplete();
1322 }
1323
1324 struct Programs
1325 {
1326         static void init (vk::SourceCollections& dst, TestConfig)
1327         {
1328                 dst.glslSources.add("quad-vert") << glu::VertexSource(
1329                         "#version 450\n"
1330                         "out gl_PerVertex {\n"
1331                         "\tvec4 gl_Position;\n"
1332                         "};\n"
1333                         "highp float;\n"
1334                         "void main (void) {\n"
1335                         "\tgl_Position = vec4(((gl_VertexIndex + 2) / 3) % 2 == 0 ? -1.0 : 1.0,\n"
1336                         "\t                   ((gl_VertexIndex + 1) / 3) % 2 == 0 ? -1.0 : 1.0, 0.0, 1.0);\n"
1337                         "}\n");
1338                 dst.glslSources.add("quad-frag") << glu::FragmentSource(
1339                         "#version 310 es\n"
1340                         "layout(location = 0) out highp vec4 o_color;\n"
1341                         "layout(push_constant) uniform PushConstant {\n"
1342                         "\thighp uint mask;\n"
1343                         "} pushConstants;\n"
1344                         "void main (void)\n"
1345                         "{\n"
1346                         "\thighp uint mask = pushConstants.mask;\n"
1347                         "\thighp uint x = mask ^ uint(gl_FragCoord.x);\n"
1348                         "\thighp uint y = mask ^ uint(gl_FragCoord.y);\n"
1349                         "\thighp uint r = 128u * bitfieldExtract(x, 0, 1)\n"
1350                         "\t             +  64u * bitfieldExtract(y, 1, 1)\n"
1351                         "\t             +  32u * bitfieldExtract(x, 3, 1);\n"
1352                         "\thighp uint g = 128u * bitfieldExtract(y, 0, 1)\n"
1353                         "\t             +  64u * bitfieldExtract(x, 2, 1)\n"
1354                         "\t             +  32u * bitfieldExtract(y, 3, 1);\n"
1355                         "\thighp uint b = 128u * bitfieldExtract(x, 1, 1)\n"
1356                         "\t             +  64u * bitfieldExtract(y, 2, 1)\n"
1357                         "\t             +  32u * bitfieldExtract(x, 4, 1);\n"
1358                         "\to_color = vec4(float(r) / 255.0, float(g) / 255.0, float(b) / 255.0, 1.0);\n"
1359                         "}\n");
1360         }
1361 };
1362
1363 } // anonymous
1364
1365 void createIncrementalPresentTests (tcu::TestCaseGroup* testGroup, vk::wsi::Type wsiType)
1366 {
1367         const struct
1368         {
1369                 Scaling         scaling;
1370                 const char*     name;
1371         } scaling [] =
1372         {
1373                 { SCALING_NONE, "scale_none"    },
1374                 { SCALING_UP,   "scale_up"              },
1375                 { SCALING_DOWN, "scale_down"    }
1376         };
1377         const struct
1378         {
1379                 vk::VkPresentModeKHR    mode;
1380                 const char*                             name;
1381         } presentModes[] =
1382         {
1383                 { vk::VK_PRESENT_MODE_IMMEDIATE_KHR,    "immediate"             },
1384                 { vk::VK_PRESENT_MODE_MAILBOX_KHR,              "mailbox"               },
1385                 { vk::VK_PRESENT_MODE_FIFO_KHR,                 "fifo"                  },
1386                 { vk::VK_PRESENT_MODE_FIFO_RELAXED_KHR, "fifo_relaxed"  }
1387         };
1388
1389         for (size_t scalingNdx = 0; scalingNdx < DE_LENGTH_OF_ARRAY(scaling); scalingNdx++)
1390         {
1391                 if (scaling[scalingNdx].scaling != SCALING_NONE && wsiType == vk::wsi::TYPE_WAYLAND)
1392                         continue;
1393
1394                 if (scaling[scalingNdx].scaling != SCALING_NONE && vk::wsi::getPlatformProperties(wsiType).swapchainExtent != vk::wsi::PlatformProperties::SWAPCHAIN_EXTENT_SCALED_TO_WINDOW_SIZE)
1395                         continue;
1396
1397                 {
1398
1399                         de::MovePtr<tcu::TestCaseGroup> scaleGroup      (new tcu::TestCaseGroup(testGroup->getTestContext(), scaling[scalingNdx].name, scaling[scalingNdx].name));
1400
1401                         for (size_t presentModeNdx = 0; presentModeNdx < DE_LENGTH_OF_ARRAY(presentModes); presentModeNdx++)
1402                         {
1403                                 de::MovePtr<tcu::TestCaseGroup> presentModeGroup        (new tcu::TestCaseGroup(testGroup->getTestContext(), presentModes[presentModeNdx].name, presentModes[presentModeNdx].name));
1404
1405                                 for (size_t ref = 0; ref < 2; ref++)
1406                                 {
1407                                         const bool                                              isReference     = (ref == 0);
1408                                         const char* const                               name            = isReference ? "reference" : "incremental_present";
1409                                         TestConfig                                              config;
1410
1411                                         config.wsiType                                  = wsiType;
1412                                         config.scaling                                  = scaling[scalingNdx].scaling;
1413                                         config.useIncrementalPresent    = !isReference;
1414                                         config.presentMode                              = presentModes[presentModeNdx].mode;
1415
1416                                         presentModeGroup->addChild(new vkt::InstanceFactory1<IncrementalPresentTestInstance, TestConfig, Programs>(testGroup->getTestContext(), tcu::NODETYPE_SELF_VALIDATE, name, name, Programs(), config));
1417                                 }
1418
1419                                 scaleGroup->addChild(presentModeGroup.release());
1420                         }
1421
1422                         testGroup->addChild(scaleGroup.release());
1423                 }
1424         }
1425 }
1426
1427 } // wsi
1428 } // vkt