Merge vk-gl-cts/opengl-cts-4.6.1 into vk-gl-cts/opengl-cts-4.6.2
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / renderpass / vktRenderPassSparseRenderTargetTests.cpp
1 /*-------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2017 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 sparse render target.
22  *//*--------------------------------------------------------------------*/
23
24 #include "vktRenderPassSparseRenderTargetTests.hpp"
25 #include "vktRenderPassTestsUtil.hpp"
26
27 #include "vktTestCaseUtil.hpp"
28 #include "vktTestGroupUtil.hpp"
29
30 #include "vkDefs.hpp"
31 #include "vkImageUtil.hpp"
32 #include "vkMemUtil.hpp"
33 #include "vkPrograms.hpp"
34 #include "vkQueryUtil.hpp"
35 #include "vkRef.hpp"
36 #include "vkRefUtil.hpp"
37 #include "vkTypeUtil.hpp"
38 #include "vkCmdUtil.hpp"
39 #include "vkObjUtil.hpp"
40
41 #include "tcuImageCompare.hpp"
42 #include "tcuResultCollector.hpp"
43 #include "tcuTextureUtil.hpp"
44
45 #include "deUniquePtr.hpp"
46 #include "deSharedPtr.hpp"
47
48 using namespace vk;
49
50 using tcu::UVec4;
51 using tcu::Vec4;
52
53 using tcu::ConstPixelBufferAccess;
54 using tcu::PixelBufferAccess;
55
56 using tcu::TestLog;
57
58 using std::string;
59 using std::vector;
60
61 namespace vkt
62 {
63 namespace
64 {
65 using namespace renderpass;
66
67 de::MovePtr<Allocation> createBufferMemory (const DeviceInterface&      vk,
68                                                                                         VkDevice                                device,
69                                                                                         Allocator&                              allocator,
70                                                                                         VkBuffer                                buffer)
71 {
72         de::MovePtr<Allocation> allocation (allocator.allocate(getBufferMemoryRequirements(vk, device, buffer), MemoryRequirement::HostVisible));
73         VK_CHECK(vk.bindBufferMemory(device, buffer, allocation->getMemory(), allocation->getOffset()));
74         return allocation;
75 }
76
77 Move<VkImage> createSparseImageAndMemory (const DeviceInterface&                                vk,
78                                                                                   VkDevice                                                              device,
79                                                                                   const VkPhysicalDevice                                physicalDevice,
80                                                                                   const InstanceInterface&                              instance,
81                                                                                   Allocator&                                                    allocator,
82                                                                                   vector<de::SharedPtr<Allocation> >&   allocations,
83                                                                                   deUint32                                                              universalQueueFamilyIndex,
84                                                                                   VkQueue                                                               sparseQueue,
85                                                                                   deUint32                                                              sparseQueueFamilyIndex,
86                                                                                   const VkSemaphore&                                    bindSemaphore,
87                                                                                   VkFormat                                                              format,
88                                                                                   deUint32                                                              width,
89                                                                                   deUint32                                                              height)
90 {
91         deUint32                                queueFamilyIndices[]    = {universalQueueFamilyIndex, sparseQueueFamilyIndex};
92         const VkSharingMode             sharingMode             = universalQueueFamilyIndex != sparseQueueFamilyIndex ? VK_SHARING_MODE_CONCURRENT : VK_SHARING_MODE_EXCLUSIVE;
93
94         const VkExtent3D                imageExtent                             =
95         {
96                 width,
97                 height,
98                 1u
99         };
100
101         const VkImageCreateInfo imageCreateInfo                 =
102         {
103                 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
104                 DE_NULL,
105                 VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT,
106                 VK_IMAGE_TYPE_2D,
107                 format,
108                 imageExtent,
109                 1u,
110                 1u,
111                 VK_SAMPLE_COUNT_1_BIT,
112                 VK_IMAGE_TILING_OPTIMAL,
113                 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
114                 sharingMode,
115                 sharingMode == VK_SHARING_MODE_CONCURRENT ? 2u : 1u,
116                 queueFamilyIndices,
117                 VK_IMAGE_LAYOUT_UNDEFINED
118         };
119
120         if (!checkSparseImageFormatSupport(physicalDevice, instance, imageCreateInfo))
121                 TCU_THROW(NotSupportedError, "The image format does not support sparse operations");
122
123         Move<VkImage> destImage = createImage(vk, device, &imageCreateInfo);
124         allocateAndBindSparseImage(vk, device, physicalDevice, instance, imageCreateInfo, bindSemaphore, sparseQueue, allocator, allocations, mapVkFormat(format), *destImage);
125
126         return destImage;
127 }
128
129 Move<VkImageView> createImageView (const DeviceInterface&       vk,
130                                                                    VkDevice                                     device,
131                                                                    VkImageViewCreateFlags       flags,
132                                                                    VkImage                                      image,
133                                                                    VkImageViewType                      viewType,
134                                                                    VkFormat                                     format,
135                                                                    VkComponentMapping           components,
136                                                                    VkImageSubresourceRange      subresourceRange)
137 {
138         const VkImageViewCreateInfo pCreateInfo =
139         {
140                 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
141                 DE_NULL,
142                 flags,
143                 image,
144                 viewType,
145                 format,
146                 components,
147                 subresourceRange,
148         };
149
150         return createImageView(vk, device, &pCreateInfo);
151 }
152
153 Move<VkImageView> createImageView (const DeviceInterface&       vkd,
154                                                                    VkDevice                                     device,
155                                                                    VkImage                                      image,
156                                                                    VkFormat                                     format,
157                                                                    VkImageAspectFlags           aspect)
158 {
159         const VkImageSubresourceRange range =
160         {
161                 aspect,
162                 0u,
163                 1u,
164                 0u,
165                 1u
166         };
167
168         return createImageView(vkd, device, 0u, image, VK_IMAGE_VIEW_TYPE_2D, format, makeComponentMappingRGBA(), range);
169 }
170
171 Move<VkBuffer> createBuffer (const DeviceInterface&             vkd,
172                                                          VkDevice                                       device,
173                                                          VkFormat                                       format,
174                                                          deUint32                                       width,
175                                                          deUint32                                       height)
176 {
177         const VkBufferUsageFlags        bufferUsage                     (VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
178         const VkDeviceSize                      pixelSize                       = mapVkFormat(format).getPixelSize();
179         const VkBufferCreateInfo        createInfo                      =
180         {
181                 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
182                 DE_NULL,
183                 0u,
184
185                 width * height * pixelSize,
186                 bufferUsage,
187
188                 VK_SHARING_MODE_EXCLUSIVE,
189                 0u,
190                 DE_NULL
191         };
192
193         return createBuffer(vkd, device, &createInfo);
194 }
195
196 template<typename AttachmentDesc, typename AttachmentRef, typename SubpassDesc, typename SubpassDep, typename RenderPassCreateInfo>
197 Move<VkRenderPass> createRenderPass (const DeviceInterface&     vkd,
198                                                                          VkDevice                               device,
199                                                                          VkFormat                               dstFormat)
200 {
201         const AttachmentRef             dstAttachmentRef                //  VkAttachmentReference                                                                               ||  VkAttachmentReference2KHR
202         (
203                                                                                                         //                                                                                                                              ||  VkStructureType                                             sType;
204                 DE_NULL,                                                                        //                                                                                                                              ||  const void*                                                 pNext;
205                 0u,                                                                                     //  deUint32                                            attachment;                                     ||  deUint32                                                    attachment;
206                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,       //  VkImageLayout                                       layout;                                         ||  VkImageLayout                                               layout;
207                 0u                                                                                      //                                                                                                                              ||  VkImageAspectFlags                                  aspectMask;
208         );
209         const AttachmentDesc    dstAttachment                   //  VkAttachmentDescription                                                                             ||  VkAttachmentDescription2KHR
210         (
211                                                                                                         //                                                                                                                              ||  VkStructureType                                             sType;
212                 DE_NULL,                                                                        //                                                                                                                              ||  const void*                                                 pNext;
213                 0u,                                                                                     //  VkAttachmentDescriptionFlags        flags;                                          ||  VkAttachmentDescriptionFlags                flags;
214                 dstFormat,                                                                      //  VkFormat                                            format;                                         ||  VkFormat                                                    format;
215                 VK_SAMPLE_COUNT_1_BIT,                                          //  VkSampleCountFlagBits                       samples;                                        ||  VkSampleCountFlagBits                               samples;
216                 VK_ATTACHMENT_LOAD_OP_DONT_CARE,                        //  VkAttachmentLoadOp                          loadOp;                                         ||  VkAttachmentLoadOp                                  loadOp;
217                 VK_ATTACHMENT_STORE_OP_STORE,                           //  VkAttachmentStoreOp                         storeOp;                                        ||  VkAttachmentStoreOp                                 storeOp;
218                 VK_ATTACHMENT_LOAD_OP_DONT_CARE,                        //  VkAttachmentLoadOp                          stencilLoadOp;                          ||  VkAttachmentLoadOp                                  stencilLoadOp;
219                 VK_ATTACHMENT_STORE_OP_DONT_CARE,                       //  VkAttachmentStoreOp                         stencilStoreOp;                         ||  VkAttachmentStoreOp                                 stencilStoreOp;
220                 VK_IMAGE_LAYOUT_UNDEFINED,                                      //  VkImageLayout                                       initialLayout;                          ||  VkImageLayout                                               initialLayout;
221                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL        //  VkImageLayout                                       finalLayout;                            ||  VkImageLayout                                               finalLayout;
222         );
223         const SubpassDesc                       subpass                         //  VkSubpassDescription                                                                                ||  VkSubpassDescription2KHR
224         (
225                                                                                                         //                                                                                                                              ||  VkStructureType                                             sType;
226                 DE_NULL,                                                                        //                                                                                                                              ||  const void*                                                 pNext;
227                 (VkSubpassDescriptionFlags)0,                           //  VkSubpassDescriptionFlags           flags;                                          ||  VkSubpassDescriptionFlags                   flags;
228                 VK_PIPELINE_BIND_POINT_GRAPHICS,                        //  VkPipelineBindPoint                         pipelineBindPoint;                      ||  VkPipelineBindPoint                                 pipelineBindPoint;
229                 0u,                                                                                     //                                                                                                                              ||  deUint32                                                    viewMask;
230                 0u,                                                                                     //  deUint32                                            inputAttachmentCount;           ||  deUint32                                                    inputAttachmentCount;
231                 DE_NULL,                                                                        //  const VkAttachmentReference*        pInputAttachments;                      ||  const VkAttachmentReference2KHR*    pInputAttachments;
232                 1u,                                                                                     //  deUint32                                            colorAttachmentCount;           ||  deUint32                                                    colorAttachmentCount;
233                 &dstAttachmentRef,                                                      //  const VkAttachmentReference*        pColorAttachments;                      ||  const VkAttachmentReference2KHR*    pColorAttachments;
234                 DE_NULL,                                                                        //  const VkAttachmentReference*        pResolveAttachments;            ||  const VkAttachmentReference2KHR*    pResolveAttachments;
235                 DE_NULL,                                                                        //  const VkAttachmentReference*        pDepthStencilAttachment;        ||  const VkAttachmentReference2KHR*    pDepthStencilAttachment;
236                 0u,                                                                                     //  deUint32                                            preserveAttachmentCount;        ||  deUint32                                                    preserveAttachmentCount;
237                 DE_NULL                                                                         //  const deUint32*                                     pPreserveAttachments;           ||  const deUint32*                                             pPreserveAttachments;
238         );
239         const RenderPassCreateInfo      renderPassCreator       //  VkRenderPassCreateInfo                                                                              ||  VkRenderPassCreateInfo2KHR
240         (
241                                                                                                         //  VkStructureType                                     sType;                                          ||  VkStructureType                                             sType;
242                 DE_NULL,                                                                        //  const void*                                         pNext;                                          ||  const void*                                                 pNext;
243                 (VkRenderPassCreateFlags)0u,                            //  VkRenderPassCreateFlags                     flags;                                          ||  VkRenderPassCreateFlags                             flags;
244                 1u,                                                                                     //  deUint32                                            attachmentCount;                        ||  deUint32                                                    attachmentCount;
245                 &dstAttachment,                                                         //  const VkAttachmentDescription*      pAttachments;                           ||  const VkAttachmentDescription2KHR*  pAttachments;
246                 1u,                                                                                     //  deUint32                                            subpassCount;                           ||  deUint32                                                    subpassCount;
247                 &subpass,                                                                       //  const VkSubpassDescription*         pSubpasses;                                     ||  const VkSubpassDescription2KHR*             pSubpasses;
248                 0u,                                                                                     //  deUint32                                            dependencyCount;                        ||  deUint32                                                    dependencyCount;
249                 DE_NULL,                                                                        //  const VkSubpassDependency*          pDependencies;                          ||  const VkSubpassDependency2KHR*              pDependencies;
250                 0u,                                                                                     //                                                                                                                              ||  deUint32                                                    correlatedViewMaskCount;
251                 DE_NULL                                                                         //                                                                                                                              ||  const deUint32*                                             pCorrelatedViewMasks;
252         );
253
254         return renderPassCreator.createRenderPass(vkd, device);
255 }
256
257 Move<VkRenderPass> createRenderPass (const DeviceInterface&     vkd,
258                                                                          VkDevice                               device,
259                                                                          VkFormat                               dstFormat,
260                                                                          const RenderingType    renderingType)
261 {
262         switch (renderingType)
263         {
264                 case RENDERING_TYPE_RENDERPASS_LEGACY:
265                         return createRenderPass<AttachmentDescription1, AttachmentReference1, SubpassDescription1, SubpassDependency1, RenderPassCreateInfo1>(vkd, device, dstFormat);
266                 case RENDERING_TYPE_RENDERPASS2:
267                         return createRenderPass<AttachmentDescription2, AttachmentReference2, SubpassDescription2, SubpassDependency2, RenderPassCreateInfo2>(vkd, device, dstFormat);
268                 case RENDERING_TYPE_DYNAMIC_RENDERING:
269                         return Move<VkRenderPass>();
270                 default:
271                         TCU_THROW(InternalError, "Impossible");
272         }
273 }
274
275 Move<VkFramebuffer> createFramebuffer (const DeviceInterface&   vkd,
276                                                                            VkDevice                                     device,
277                                                                            VkRenderPass                         renderPass,
278                                                                            VkImageView                          dstImageView,
279                                                                            deUint32                                     width,
280                                                                            deUint32                                     height)
281 {
282         // when RenderPass was not created then we are testing dynamic rendering
283         // and we can't create framebuffer without valid RenderPass object
284         if (!renderPass)
285                 return Move<VkFramebuffer>();
286
287         const VkFramebufferCreateInfo createInfo =
288         {
289                 VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
290                 DE_NULL,
291                 0u,
292
293                 renderPass,
294                 1u,
295                 &dstImageView,
296
297                 width,
298                 height,
299                 1u
300         };
301
302         return createFramebuffer(vkd, device, &createInfo);
303 }
304
305 Move<VkPipelineLayout> createRenderPipelineLayout (const DeviceInterface&       vkd,
306                                                                                                    VkDevice                                     device)
307 {
308         const VkPipelineLayoutCreateInfo createInfo =
309         {
310                 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
311                 DE_NULL,
312                 (vk::VkPipelineLayoutCreateFlags)0,
313
314                 0u,
315                 DE_NULL,
316
317                 0u,
318                 DE_NULL
319         };
320
321         return createPipelineLayout(vkd, device, &createInfo);
322 }
323
324 Move<VkPipeline> createRenderPipeline (const DeviceInterface&                                                   vkd,
325                                                                            VkDevice                                                                                     device,
326                                                                            VkRenderPass                                                                         renderPass,
327                                                                            VkFormat                                                                                     format,
328                                                                            VkPipelineLayout                                                                     pipelineLayout,
329                                                                            const BinaryCollection&                                                      binaryCollection,
330                                                                            deUint32                                                                                     width,
331                                                                            deUint32                                                                                     height)
332 {
333         const Unique<VkShaderModule>                                    vertexShaderModule                              (createShaderModule(vkd, device, binaryCollection.get("quad-vert"), 0u));
334         const Unique<VkShaderModule>                                    fragmentShaderModule                    (createShaderModule(vkd, device, binaryCollection.get("quad-frag"), 0u));
335
336         const VkPipelineVertexInputStateCreateInfo              vertexInputState                                =
337         {
338                 VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
339                 DE_NULL,
340                 (VkPipelineVertexInputStateCreateFlags)0u,
341
342                 0u,
343                 DE_NULL,
344
345                 0u,
346                 DE_NULL
347         };
348
349         const std::vector<VkViewport>                                   viewports                                               (1, makeViewport(tcu::UVec2(width, height)));
350         const std::vector<VkRect2D>                                             scissors                                                (1, makeRect2D(tcu::UVec2(width, height)));
351
352         VkPipelineRenderingCreateInfoKHR* pNext = DE_NULL;
353         VkPipelineRenderingCreateInfoKHR renderingCreateInfo
354         {
355                 VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
356                 0u,
357                 DE_NULL,
358                 1u,
359                 &format,
360                 VK_FORMAT_UNDEFINED,
361                 VK_FORMAT_UNDEFINED
362         };
363         if (renderPass == DE_NULL)
364                 pNext = &renderingCreateInfo;
365
366         return makeGraphicsPipeline(vkd,                                                                        // const DeviceInterface&                        vk
367                                                                 device,                                                                 // const VkDevice                                device
368                                                                 pipelineLayout,                                                 // const VkPipelineLayout                        pipelineLayout
369                                                                 *vertexShaderModule,                                    // const VkShaderModule                          vertexShaderModule
370                                                                 DE_NULL,                                                                // const VkShaderModule                          tessellationControlShaderModule
371                                                                 DE_NULL,                                                                // const VkShaderModule                          tessellationEvalShaderModule
372                                                                 DE_NULL,                                                                // const VkShaderModule                          geometryShaderModule
373                                                                 *fragmentShaderModule,                                  // const VkShaderModule                          fragmentShaderModule
374                                                                 renderPass,                                                             // const VkRenderPass                            renderPass
375                                                                 viewports,                                                              // const std::vector<VkViewport>&                viewports
376                                                                 scissors,                                                               // const std::vector<VkRect2D>&                  scissors
377                                                                 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,    // const VkPrimitiveTopology                     topology
378                                                                 0u,                                                                             // const deUint32                                subpass
379                                                                 0u,                                                                             // const deUint32                                patchControlPoints
380                                                                 &vertexInputState,                                              // const VkPipelineVertexInputStateCreateInfo*   vertexInputStateCreateInfo
381                                                                 DE_NULL,                                                                // const VkPipelineRasterizationStateCreateInfo* rasterizationStateCreateInfo
382                                                                 DE_NULL,                                                                // const VkPipelineMultisampleStateCreateInfo*   multisampleStateCreateInfo
383                                                                 DE_NULL,                                                                // const VkPipelineDepthStencilStateCreateInfo*  depthStencilStateCreateInfo
384                                                                 DE_NULL,                                                                // const VkPipelineColorBlendStateCreateInfo*    colorBlendStateCreateInfo
385                                                                 DE_NULL,                                                                // const VkPipelineDynamicStateCreateInfo*       dynamicStateCreateInfo
386                                                                 pNext);                                                                 // const void*                                   pNext
387 }
388
389 struct TestConfig
390 {
391                                 TestConfig              (VkFormat               format_,
392                                                                  RenderingType  renderingType_)
393                 : format                        (format_)
394                 , renderingType         (renderingType_)
395         {
396         }
397
398         VkFormat                format;
399         RenderingType   renderingType;
400 };
401
402 class SparseRenderTargetTestInstance : public TestInstance
403 {
404 public:
405                                                                                         SparseRenderTargetTestInstance  (Context& context, TestConfig testConfig);
406                                                                                         ~SparseRenderTargetTestInstance (void);
407
408         tcu::TestStatus                                                 iterate                                 (void);
409
410         template<typename RenderpassSubpass>
411         tcu::TestStatus                                                 iterateInternal                 (void);
412
413 private:
414         const RenderingType                                             m_renderingType;
415
416         const deUint32                                                  m_width;
417         const deUint32                                                  m_height;
418         const VkFormat                                                  m_format;
419
420         vector<de::SharedPtr<Allocation> >              m_allocations;
421
422         const Unique<VkSemaphore>                               m_bindSemaphore;
423
424         const Unique<VkImage>                                   m_dstImage;
425         const Unique<VkImageView>                               m_dstImageView;
426
427         const Unique<VkBuffer>                                  m_dstBuffer;
428         const de::UniquePtr<Allocation>                 m_dstBufferMemory;
429
430         const Unique<VkRenderPass>                              m_renderPass;
431         const Unique<VkFramebuffer>                             m_framebuffer;
432
433         const Unique<VkPipelineLayout>                  m_renderPipelineLayout;
434         const Unique<VkPipeline>                                m_renderPipeline;
435
436         const Unique<VkCommandPool>                             m_commandPool;
437         tcu::ResultCollector                                    m_resultCollector;
438 };
439
440 SparseRenderTargetTestInstance::SparseRenderTargetTestInstance (Context& context, TestConfig testConfig)
441         : TestInstance                          (context)
442         , m_renderingType                       (testConfig.renderingType)
443         , m_width                                       (32u)
444         , m_height                                      (32u)
445         , m_format                                      (testConfig.format)
446         , m_bindSemaphore                       (createSemaphore(context.getDeviceInterface(), context.getDevice()))
447         , m_dstImage                            (createSparseImageAndMemory(context.getDeviceInterface(), context.getDevice(), context.getPhysicalDevice(), context.getInstanceInterface(), context.getDefaultAllocator(), m_allocations, context.getUniversalQueueFamilyIndex(), context.getSparseQueue(), context.getSparseQueueFamilyIndex(), *m_bindSemaphore, m_format, m_width, m_height))
448         , m_dstImageView                        (createImageView(context.getDeviceInterface(), context.getDevice(), *m_dstImage, m_format, VK_IMAGE_ASPECT_COLOR_BIT))
449         , m_dstBuffer                           (createBuffer(context.getDeviceInterface(), context.getDevice(), m_format, m_width, m_height))
450         , m_dstBufferMemory                     (createBufferMemory(context.getDeviceInterface(), context.getDevice(), context.getDefaultAllocator(), *m_dstBuffer))
451         , m_renderPass                          (createRenderPass(context.getDeviceInterface(), context.getDevice(), m_format, testConfig.renderingType))
452         , m_framebuffer                         (createFramebuffer(context.getDeviceInterface(), context.getDevice(), *m_renderPass, *m_dstImageView, m_width, m_height))
453         , m_renderPipelineLayout        (createRenderPipelineLayout(context.getDeviceInterface(), context.getDevice()))
454         , m_renderPipeline                      (createRenderPipeline(context.getDeviceInterface(), context.getDevice(), *m_renderPass, testConfig.format, *m_renderPipelineLayout, context.getBinaryCollection(), m_width, m_height))
455         , m_commandPool                         (createCommandPool(context.getDeviceInterface(), context.getDevice(), VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, context.getUniversalQueueFamilyIndex()))
456 {
457 }
458
459 SparseRenderTargetTestInstance::~SparseRenderTargetTestInstance (void)
460 {
461 }
462
463 tcu::TestStatus SparseRenderTargetTestInstance::iterate (void)
464 {
465         switch (m_renderingType)
466         {
467                 case RENDERING_TYPE_RENDERPASS_LEGACY:
468                         return iterateInternal<RenderpassSubpass1>();
469                 case RENDERING_TYPE_RENDERPASS2:
470                 case RENDERING_TYPE_DYNAMIC_RENDERING:
471                         return iterateInternal<RenderpassSubpass2>();
472                 default:
473                         TCU_THROW(InternalError, "Impossible");
474         }
475 }
476
477 template<typename RenderpassSubpass>
478 tcu::TestStatus SparseRenderTargetTestInstance::iterateInternal (void)
479 {
480         const DeviceInterface&                  vkd                             (m_context.getDeviceInterface());
481         const Unique<VkCommandBuffer>   commandBuffer   (allocateCommandBuffer(vkd, m_context.getDevice(), *m_commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
482
483         beginCommandBuffer(vkd, *commandBuffer);
484
485         VkRect2D renderArea = makeRect2D(m_width, m_height);
486         if (m_renderingType == RENDERING_TYPE_DYNAMIC_RENDERING)
487         {
488                 const VkClearValue clearValue = makeClearValueColor({ 0.0f, 0.0f, 0.0f, 1.0f });
489                 beginRendering(vkd, *commandBuffer, *m_dstImageView, renderArea, clearValue, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_ATTACHMENT_LOAD_OP_DONT_CARE);
490         }
491         else
492         {
493                 const typename RenderpassSubpass::SubpassBeginInfo subpassBeginInfo(DE_NULL, VK_SUBPASS_CONTENTS_INLINE);
494                 const VkRenderPassBeginInfo beginInfo =
495                 {
496                         VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
497                         DE_NULL,
498                         *m_renderPass,
499                         *m_framebuffer,
500                         renderArea,
501                         0u,
502                         DE_NULL
503                 };
504                 RenderpassSubpass::cmdBeginRenderPass(vkd, *commandBuffer, &beginInfo, &subpassBeginInfo);
505         }
506
507         vkd.cmdBindPipeline(*commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *m_renderPipeline);
508         vkd.cmdDraw(*commandBuffer, 6u, 1u, 0u, 0u);
509
510         if (m_renderingType == RENDERING_TYPE_DYNAMIC_RENDERING)
511                 vkd.cmdEndRenderingKHR(*commandBuffer);
512         else
513         {
514                 const typename RenderpassSubpass::SubpassEndInfo subpassEndInfo(DE_NULL);
515                 RenderpassSubpass::cmdEndRenderPass(vkd, *commandBuffer, &subpassEndInfo);
516         }
517
518         copyImageToBuffer(vkd, *commandBuffer, *m_dstImage, *m_dstBuffer, tcu::IVec2(m_width, m_height));
519
520         endCommandBuffer(vkd, *commandBuffer);
521
522         submitCommandsAndWait(vkd, m_context.getDevice(), m_context.getUniversalQueue(), *commandBuffer);
523
524         {
525                 const tcu::TextureFormat                        format                  (mapVkFormat(m_format));
526                 const void* const                                       ptr                             (m_dstBufferMemory->getHostPtr());
527                 const tcu::ConstPixelBufferAccess       access                  (format, m_width, m_height, 1, ptr);
528                 tcu::TextureLevel                                       reference               (format, m_width, m_height);
529                 const tcu::TextureChannelClass          channelClass    (tcu::getTextureChannelClass(format.type));
530
531                 switch (channelClass)
532                 {
533                         case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
534                         {
535                                 const UVec4     bits    (tcu::getTextureFormatBitDepth(format).cast<deUint32>());
536                                 const UVec4     color   (1u << (bits.x()-1), 1u << (bits.y()-2), 1u << (bits.z()-3), 0xffffffff);
537
538                                 for (deUint32 y = 0; y < m_height; y++)
539                                 for (deUint32 x = 0; x < m_width; x++)
540                                 {
541                                         reference.getAccess().setPixel(color, x, y);
542                                 }
543
544                                 if (!tcu::intThresholdCompare(m_context.getTestContext().getLog(), "", "", reference.getAccess(), access, UVec4(0u), tcu::COMPARE_LOG_ON_ERROR))
545                                         m_resultCollector.fail("Compare failed.");
546                         }
547                         break;
548
549                         case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
550                         {
551                                 const UVec4     bits    (tcu::getTextureFormatBitDepth(format).cast<deUint32>());
552                                 const UVec4     color   (1u << (bits.x()-2), 1u << (bits.y()-3), 1u << (bits.z()-4), 0xffffffff);
553
554                                 for (deUint32 y = 0; y < m_height; y++)
555                                 for (deUint32 x = 0; x < m_width; x++)
556                                 {
557                                         reference.getAccess().setPixel(color, x, y);
558                                 }
559
560                                 if (!tcu::intThresholdCompare(m_context.getTestContext().getLog(), "", "", reference.getAccess(), access, UVec4(0u), tcu::COMPARE_LOG_ON_ERROR))
561                                         m_resultCollector.fail("Compare failed.");
562                         }
563                         break;
564
565                         case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
566                         case tcu::TEXTURECHANNELCLASS_SIGNED_FIXED_POINT:
567                         {
568                                 const tcu::TextureFormatInfo    info            (tcu::getTextureFormatInfo(format));
569                                 const Vec4                                              maxValue        (info.valueMax);
570                                 const Vec4                                              color           (maxValue.x() / 2.0f, maxValue.y() / 4.0f, maxValue.z() / 8.0f, maxValue.w());
571
572                                 for (deUint32 y = 0; y < m_height; y++)
573                                 for (deUint32 x = 0; x < m_width; x++)
574                                 {
575                                         if (tcu::isSRGB(format))
576                                                 reference.getAccess().setPixel(tcu::linearToSRGB(color), x, y);
577                                         else
578                                                 reference.getAccess().setPixel(color, x, y);
579                                 }
580
581                                 {
582                                         // Allow error of 4 times the minimum presentable difference
583                                         const Vec4 threshold (4.0f * 1.0f / ((UVec4(1u) << tcu::getTextureFormatMantissaBitDepth(format).cast<deUint32>()) - 1u).cast<float>());
584
585                                         if (!tcu::floatThresholdCompare(m_context.getTestContext().getLog(), "", "", reference.getAccess(), access, threshold, tcu::COMPARE_LOG_ON_ERROR))
586                                                 m_resultCollector.fail("Compare failed.");
587                                 }
588                         }
589                         break;
590
591                         case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
592                         {
593                                 const Vec4 color(0.5f, 0.25f, 0.125f, 1.0f);
594
595                                 for (deUint32 y = 0; y < m_height; y++)
596                                 for (deUint32 x = 0; x < m_width; x++)
597                                 {
598                                         if (tcu::isSRGB(format))
599                                                 reference.getAccess().setPixel(tcu::linearToSRGB(color), x, y);
600                                         else
601                                                 reference.getAccess().setPixel(color, x, y);
602                                 }
603
604                                 {
605                                         // Convert target format ulps to float ulps and allow 64ulp differences
606                                         const UVec4 threshold (64u * (UVec4(1u) << (UVec4(23) - tcu::getTextureFormatMantissaBitDepth(format).cast<deUint32>())));
607
608                                         if (!tcu::floatUlpThresholdCompare(m_context.getTestContext().getLog(), "", "", reference.getAccess(), access, threshold, tcu::COMPARE_LOG_ON_ERROR))
609                                                 m_resultCollector.fail("Compare failed.");
610                                 }
611                         }
612                         break;
613
614                         default:
615                                 DE_FATAL("Unknown channel class");
616                 }
617         }
618
619         return tcu::TestStatus(m_resultCollector.getResult(), m_resultCollector.getMessage());
620 }
621
622 struct Programs
623 {
624         void init (vk::SourceCollections& dst, TestConfig testConfig) const
625         {
626                 std::ostringstream                              fragmentShader;
627                 const VkFormat                                  format                  (testConfig.format);
628                 const tcu::TextureFormat                texFormat               (mapVkFormat(format));
629                 const UVec4                                             bits                    (tcu::getTextureFormatBitDepth(texFormat).cast<deUint32>());
630                 const tcu::TextureChannelClass  channelClass    (tcu::getTextureChannelClass(texFormat.type));
631
632                 dst.glslSources.add("quad-vert") << glu::VertexSource(
633                         "#version 450\n"
634                         "out gl_PerVertex {\n"
635                         "\tvec4 gl_Position;\n"
636                         "};\n"
637                         "highp float;\n"
638                         "void main (void)\n"
639                         "{\n"
640                         "    gl_Position = vec4(((gl_VertexIndex + 2) / 3) % 2 == 0 ? -1.0 : 1.0,\n"
641                         "                       ((gl_VertexIndex + 1) / 3) % 2 == 0 ? -1.0 : 1.0, 0.0, 1.0);\n"
642                         "}\n");
643
644                 switch (channelClass)
645                 {
646                         case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
647                         {
648                                 fragmentShader <<
649                                         "#version 450\n"
650                                         "layout(location = 0) out highp uvec4 o_color;\n"
651                                         "void main (void)\n"
652                                         "{\n"
653                                         "    o_color = uvec4(" << de::toString(1u << (bits.x()-1)) << ", " << de::toString(1u << (bits.y()-2)) << ", " << de::toString(1u << (bits.z()-3)) << ", 0xffffffff);"
654                                         "}\n";
655                         }
656                         break;
657
658                         case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
659                         {
660                                 fragmentShader <<
661                                         "#version 450\n"
662                                         "layout(location = 0) out highp ivec4 o_color;\n"
663                                         "void main (void)\n"
664                                         "{\n"
665                                         "    o_color = ivec4(" << de::toString(1u << (bits.x()-2)) << ", " << de::toString(1u << (bits.y()-3)) << ", " << de::toString(1u << (bits.z()-4)) << ", 0xffffffff);"
666                                         "}\n";
667                         }
668                         break;
669
670                         default:
671                         {
672                                 fragmentShader <<
673                                         "#version 450\n"
674                                         "layout(location = 0) out highp vec4 o_color;\n"
675                                         "void main (void)\n"
676                                         "{\n"
677                                         "    o_color = vec4(0.5, 0.25, 0.125, 1.0);\n"
678                                         "}\n";
679                         }
680                         break;
681                 }
682
683                 dst.glslSources.add("quad-frag") << glu::FragmentSource(fragmentShader.str());
684         }
685 };
686
687 std::string formatToName (VkFormat format)
688 {
689         const std::string       formatStr       = de::toString(format);
690         const std::string       prefix          = "VK_FORMAT_";
691
692         DE_ASSERT(formatStr.substr(0, prefix.length()) == prefix);
693
694         return de::toLower(formatStr.substr(prefix.length()));
695 }
696
697 template<class TestConfigType>
698 void checkSupport(Context& context, TestConfigType config)
699 {
700         if (config.renderingType == RENDERING_TYPE_RENDERPASS2)
701                 context.requireDeviceFunctionality("VK_KHR_create_renderpass2");
702
703         if (config.renderingType == RENDERING_TYPE_DYNAMIC_RENDERING)
704                 context.requireDeviceFunctionality("VK_KHR_dynamic_rendering");
705 }
706
707 void initTests (tcu::TestCaseGroup* group, const RenderingType renderingType)
708 {
709         static const VkFormat   formats[]       =
710         {
711                 VK_FORMAT_R5G6B5_UNORM_PACK16,
712                 VK_FORMAT_R8_UNORM,
713                 VK_FORMAT_R8_SNORM,
714                 VK_FORMAT_R8_UINT,
715                 VK_FORMAT_R8_SINT,
716                 VK_FORMAT_R8G8_UNORM,
717                 VK_FORMAT_R8G8_SNORM,
718                 VK_FORMAT_R8G8_UINT,
719                 VK_FORMAT_R8G8_SINT,
720                 VK_FORMAT_R8G8B8A8_UNORM,
721                 VK_FORMAT_R8G8B8A8_SNORM,
722                 VK_FORMAT_R8G8B8A8_UINT,
723                 VK_FORMAT_R8G8B8A8_SINT,
724                 VK_FORMAT_R8G8B8A8_SRGB,
725                 VK_FORMAT_A8B8G8R8_UNORM_PACK32,
726                 VK_FORMAT_A8B8G8R8_SNORM_PACK32,
727                 VK_FORMAT_A8B8G8R8_UINT_PACK32,
728                 VK_FORMAT_A8B8G8R8_SINT_PACK32,
729                 VK_FORMAT_A8B8G8R8_SRGB_PACK32,
730                 VK_FORMAT_B8G8R8A8_UNORM,
731                 VK_FORMAT_B8G8R8A8_SRGB,
732                 VK_FORMAT_A2R10G10B10_UNORM_PACK32,
733                 VK_FORMAT_A2B10G10R10_UNORM_PACK32,
734                 VK_FORMAT_A2B10G10R10_UINT_PACK32,
735                 VK_FORMAT_R16_UNORM,
736                 VK_FORMAT_R16_SNORM,
737                 VK_FORMAT_R16_UINT,
738                 VK_FORMAT_R16_SINT,
739                 VK_FORMAT_R16_SFLOAT,
740                 VK_FORMAT_R16G16_UNORM,
741                 VK_FORMAT_R16G16_SNORM,
742                 VK_FORMAT_R16G16_UINT,
743                 VK_FORMAT_R16G16_SINT,
744                 VK_FORMAT_R16G16_SFLOAT,
745                 VK_FORMAT_R16G16B16A16_UNORM,
746                 VK_FORMAT_R16G16B16A16_SNORM,
747                 VK_FORMAT_R16G16B16A16_UINT,
748                 VK_FORMAT_R16G16B16A16_SINT,
749                 VK_FORMAT_R16G16B16A16_SFLOAT,
750                 VK_FORMAT_R32_UINT,
751                 VK_FORMAT_R32_SINT,
752                 VK_FORMAT_R32_SFLOAT,
753                 VK_FORMAT_R32G32_UINT,
754                 VK_FORMAT_R32G32_SINT,
755                 VK_FORMAT_R32G32_SFLOAT,
756                 VK_FORMAT_R32G32B32A32_UINT,
757                 VK_FORMAT_R32G32B32A32_SINT,
758                 VK_FORMAT_R32G32B32A32_SFLOAT,
759                 VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16
760         };
761
762         tcu::TestContext&               testCtx         (group->getTestContext());
763
764         for (size_t formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(formats); formatNdx++)
765         {
766                 const VkFormat          format          (formats[formatNdx]);
767                 const TestConfig        testConfig      (format, renderingType);
768                 string                          testName        (formatToName(format));
769
770                 group->addChild(new InstanceFactory1WithSupport<SparseRenderTargetTestInstance, TestConfig, FunctionSupport1<TestConfig>, Programs>(testCtx, tcu::NODETYPE_SELF_VALIDATE, testName.c_str(), testName.c_str(), testConfig, typename FunctionSupport1<TestConfig>::Args(checkSupport, testConfig)));
771         }
772 }
773
774 } // anonymous
775
776 tcu::TestCaseGroup* createRenderPassSparseRenderTargetTests (tcu::TestContext& testCtx)
777 {
778         return createTestGroup(testCtx, "sparserendertarget", "Sparse render target tests", initTests, RENDERING_TYPE_RENDERPASS_LEGACY);
779 }
780
781 tcu::TestCaseGroup* createRenderPass2SparseRenderTargetTests (tcu::TestContext& testCtx)
782 {
783         return createTestGroup(testCtx, "sparserendertarget", "Sparse render target tests", initTests, RENDERING_TYPE_RENDERPASS2);
784 }
785
786 tcu::TestCaseGroup* createDynamicRenderingSparseRenderTargetTests(tcu::TestContext& testCtx)
787 {
788         return createTestGroup(testCtx, "sparserendertarget", "Sparse render target tests", initTests, RENDERING_TYPE_DYNAMIC_RENDERING);
789 }
790
791 } // vkt