Skip OOB SSBO fragment tests for ES3.1 GPUs am: 66241e9dbb
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / sparse_resources / vktSparseResourcesBufferTests.cpp
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2016 The Khronos Group 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 Sparse buffer tests
22  *//*--------------------------------------------------------------------*/
23
24 #include "vktSparseResourcesBufferTests.hpp"
25 #include "vktTestCaseUtil.hpp"
26 #include "vktTestGroupUtil.hpp"
27 #include "vktSparseResourcesTestsUtil.hpp"
28 #include "vktSparseResourcesBase.hpp"
29 #include "vktSparseResourcesBufferSparseBinding.hpp"
30 #include "vktSparseResourcesBufferSparseResidency.hpp"
31 #include "vktSparseResourcesBufferMemoryAliasing.hpp"
32
33 #include "vkRef.hpp"
34 #include "vkRefUtil.hpp"
35 #include "vkPlatform.hpp"
36 #include "vkPrograms.hpp"
37 #include "vkMemUtil.hpp"
38 #include "vkBuilderUtil.hpp"
39 #include "vkQueryUtil.hpp"
40 #include "vkTypeUtil.hpp"
41
42 #include "tcuTestLog.hpp"
43
44 #include "deUniquePtr.hpp"
45 #include "deSharedPtr.hpp"
46 #include "deMath.h"
47
48 #include <string>
49 #include <vector>
50 #include <map>
51
52 using namespace vk;
53 using de::MovePtr;
54 using de::UniquePtr;
55 using de::SharedPtr;
56 using tcu::Vec4;
57 using tcu::IVec2;
58 using tcu::IVec4;
59
60 namespace vkt
61 {
62 namespace sparse
63 {
64 namespace
65 {
66
67 typedef SharedPtr<UniquePtr<Allocation> > AllocationSp;
68
69 enum
70 {
71         RENDER_SIZE     = 128,                          //!< framebuffer size in pixels
72         GRID_SIZE       = RENDER_SIZE / 8,      //!< number of grid tiles in a row
73 };
74
75 enum TestFlagBits
76 {
77                                                                                                 //   sparseBinding is implied
78         TEST_FLAG_ALIASED                               = 1u << 0,      //!< sparseResidencyAliased
79         TEST_FLAG_RESIDENCY                             = 1u << 1,      //!< sparseResidencyBuffer
80         TEST_FLAG_NON_RESIDENT_STRICT   = 1u << 2,      //!< residencyNonResidentStrict
81 };
82 typedef deUint32 TestFlags;
83
84 //! SparseAllocationBuilder output. Owns the allocated memory.
85 struct SparseAllocation
86 {
87         deUint32                                                        numResourceChunks;
88         VkDeviceSize                                            resourceSize;           //!< buffer size in bytes
89         std::vector<AllocationSp>                       allocations;            //!< actual allocated memory
90         std::vector<VkSparseMemoryBind>         memoryBinds;            //!< memory binds backing the resource
91 };
92
93 //! Utility to lay out memory allocations for a sparse buffer, including holes and aliased regions.
94 //! Will allocate memory upon building.
95 class SparseAllocationBuilder
96 {
97 public:
98                                                                 SparseAllocationBuilder (void);
99
100         // \note "chunk" is the smallest (due to alignment) bindable amount of memory
101
102         SparseAllocationBuilder&        addMemoryHole                   (const deUint32 numChunks = 1u);
103         SparseAllocationBuilder&        addResourceHole                 (const deUint32 numChunks = 1u);
104         SparseAllocationBuilder&        addMemoryBind                   (const deUint32 numChunks = 1u);
105         SparseAllocationBuilder&        addAliasedMemoryBind    (const deUint32 allocationNdx, const deUint32 chunkOffset, const deUint32 numChunks = 1u);
106         SparseAllocationBuilder&        addMemoryAllocation             (void);
107
108         MovePtr<SparseAllocation>       build                                   (const DeviceInterface&         vk,
109                                                                                                                  const VkDevice                         device,
110                                                                                                                  Allocator&                                     allocator,
111                                                                                                                  VkBufferCreateInfo                     referenceCreateInfo,            //!< buffer size is ignored in this info
112                                                                                                                  const VkDeviceSize                     minChunkSize = 0ull) const;     //!< make sure chunks are at least this big
113
114 private:
115         struct MemoryBind
116         {
117                 deUint32        allocationNdx;
118                 deUint32        resourceChunkNdx;
119                 deUint32        memoryChunkNdx;
120                 deUint32        numChunks;
121         };
122
123         deUint32                                        m_allocationNdx;
124         deUint32                                        m_resourceChunkNdx;
125         deUint32                                        m_memoryChunkNdx;
126         std::vector<MemoryBind>         m_memoryBinds;
127         std::vector<deUint32>           m_chunksPerAllocation;
128
129 };
130
131 SparseAllocationBuilder::SparseAllocationBuilder (void)
132         : m_allocationNdx               (0)
133         , m_resourceChunkNdx    (0)
134         , m_memoryChunkNdx              (0)
135 {
136         m_chunksPerAllocation.push_back(0);
137 }
138
139 SparseAllocationBuilder& SparseAllocationBuilder::addMemoryHole (const deUint32 numChunks)
140 {
141         m_memoryChunkNdx                                                += numChunks;
142         m_chunksPerAllocation[m_allocationNdx]  += numChunks;
143
144         return *this;
145 }
146
147 SparseAllocationBuilder& SparseAllocationBuilder::addResourceHole (const deUint32 numChunks)
148 {
149         m_resourceChunkNdx += numChunks;
150
151         return *this;
152 }
153
154 SparseAllocationBuilder& SparseAllocationBuilder::addMemoryAllocation (void)
155 {
156         DE_ASSERT(m_memoryChunkNdx != 0);       // doesn't make sense to have an empty allocation
157
158         m_allocationNdx  += 1;
159         m_memoryChunkNdx  = 0;
160         m_chunksPerAllocation.push_back(0);
161
162         return *this;
163 }
164
165 SparseAllocationBuilder& SparseAllocationBuilder::addMemoryBind (const deUint32 numChunks)
166 {
167         const MemoryBind memoryBind =
168         {
169                 m_allocationNdx,
170                 m_resourceChunkNdx,
171                 m_memoryChunkNdx,
172                 numChunks
173         };
174         m_memoryBinds.push_back(memoryBind);
175
176         m_resourceChunkNdx                                              += numChunks;
177         m_memoryChunkNdx                                                += numChunks;
178         m_chunksPerAllocation[m_allocationNdx]  += numChunks;
179
180         return *this;
181 }
182
183 SparseAllocationBuilder& SparseAllocationBuilder::addAliasedMemoryBind  (const deUint32 allocationNdx, const deUint32 chunkOffset, const deUint32 numChunks)
184 {
185         DE_ASSERT(allocationNdx <= m_allocationNdx);
186
187         const MemoryBind memoryBind =
188         {
189                 allocationNdx,
190                 m_resourceChunkNdx,
191                 chunkOffset,
192                 numChunks
193         };
194         m_memoryBinds.push_back(memoryBind);
195
196         m_resourceChunkNdx += numChunks;
197
198         return *this;
199 }
200
201 inline VkMemoryRequirements requirementsWithSize (VkMemoryRequirements requirements, const VkDeviceSize size)
202 {
203         requirements.size = size;
204         return requirements;
205 }
206
207 MovePtr<SparseAllocation> SparseAllocationBuilder::build (const DeviceInterface&        vk,
208                                                                                                                   const VkDevice                        device,
209                                                                                                                   Allocator&                            allocator,
210                                                                                                                   VkBufferCreateInfo            referenceCreateInfo,
211                                                                                                                   const VkDeviceSize            minChunkSize) const
212 {
213
214         MovePtr<SparseAllocation>       sparseAllocation                        (new SparseAllocation());
215
216                                                                 referenceCreateInfo.size        = sizeof(deUint32);
217         const Unique<VkBuffer>          refBuffer                                       (createBuffer(vk, device, &referenceCreateInfo));
218         const VkMemoryRequirements      memoryRequirements                      = getBufferMemoryRequirements(vk, device, *refBuffer);
219         const VkDeviceSize                      chunkSize                                       = std::max(memoryRequirements.alignment, static_cast<VkDeviceSize>(deAlign64(minChunkSize, memoryRequirements.alignment)));
220
221         for (std::vector<deUint32>::const_iterator numChunksIter = m_chunksPerAllocation.begin(); numChunksIter != m_chunksPerAllocation.end(); ++numChunksIter)
222         {
223                 sparseAllocation->allocations.push_back(makeDeSharedPtr(
224                         allocator.allocate(requirementsWithSize(memoryRequirements, *numChunksIter * chunkSize), MemoryRequirement::Any)));
225         }
226
227         for (std::vector<MemoryBind>::const_iterator memBindIter = m_memoryBinds.begin(); memBindIter != m_memoryBinds.end(); ++memBindIter)
228         {
229                 const Allocation&                       alloc   = **sparseAllocation->allocations[memBindIter->allocationNdx];
230                 const VkSparseMemoryBind        bind    =
231                 {
232                         memBindIter->resourceChunkNdx * chunkSize,                                                      // VkDeviceSize               resourceOffset;
233                         memBindIter->numChunks * chunkSize,                                                                     // VkDeviceSize               size;
234                         alloc.getMemory(),                                                                                                      // VkDeviceMemory             memory;
235                         alloc.getOffset() + memBindIter->memoryChunkNdx * chunkSize,            // VkDeviceSize               memoryOffset;
236                         (VkSparseMemoryBindFlags)0,                                                                                     // VkSparseMemoryBindFlags    flags;
237                 };
238                 sparseAllocation->memoryBinds.push_back(bind);
239                 referenceCreateInfo.size = std::max(referenceCreateInfo.size, bind.resourceOffset + bind.size);
240         }
241
242         sparseAllocation->resourceSize          = referenceCreateInfo.size;
243         sparseAllocation->numResourceChunks = m_resourceChunkNdx;
244
245         return sparseAllocation;
246 }
247
248 VkImageCreateInfo makeImageCreateInfo (const VkFormat format, const IVec2& size, const VkImageUsageFlags usage)
249 {
250         const VkImageCreateInfo imageParams =
251         {
252                 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,                    // VkStructureType                      sType;
253                 DE_NULL,                                                                                // const void*                          pNext;
254                 (VkImageCreateFlags)0,                                                  // VkImageCreateFlags           flags;
255                 VK_IMAGE_TYPE_2D,                                                               // VkImageType                          imageType;
256                 format,                                                                                 // VkFormat                                     format;
257                 makeExtent3D(size.x(), size.y(), 1),                    // VkExtent3D                           extent;
258                 1u,                                                                                             // deUint32                                     mipLevels;
259                 1u,                                                                                             // deUint32                                     arrayLayers;
260                 VK_SAMPLE_COUNT_1_BIT,                                                  // VkSampleCountFlagBits        samples;
261                 VK_IMAGE_TILING_OPTIMAL,                                                // VkImageTiling                        tiling;
262                 usage,                                                                                  // VkImageUsageFlags            usage;
263                 VK_SHARING_MODE_EXCLUSIVE,                                              // VkSharingMode                        sharingMode;
264                 0u,                                                                                             // deUint32                                     queueFamilyIndexCount;
265                 DE_NULL,                                                                                // const deUint32*                      pQueueFamilyIndices;
266                 VK_IMAGE_LAYOUT_UNDEFINED,                                              // VkImageLayout                        initialLayout;
267         };
268         return imageParams;
269 }
270
271 Move<VkRenderPass> makeRenderPass (const DeviceInterface&       vk,
272                                                                    const VkDevice                       device,
273                                                                    const VkFormat                       colorFormat)
274 {
275         const VkAttachmentDescription colorAttachmentDescription =
276         {
277                 (VkAttachmentDescriptionFlags)0,                                        // VkAttachmentDescriptionFlags         flags;
278                 colorFormat,                                                                            // VkFormat                                                     format;
279                 VK_SAMPLE_COUNT_1_BIT,                                                          // VkSampleCountFlagBits                        samples;
280                 VK_ATTACHMENT_LOAD_OP_CLEAR,                                            // VkAttachmentLoadOp                           loadOp;
281                 VK_ATTACHMENT_STORE_OP_STORE,                                           // VkAttachmentStoreOp                          storeOp;
282                 VK_ATTACHMENT_LOAD_OP_DONT_CARE,                                        // VkAttachmentLoadOp                           stencilLoadOp;
283                 VK_ATTACHMENT_STORE_OP_DONT_CARE,                                       // VkAttachmentStoreOp                          stencilStoreOp;
284                 VK_IMAGE_LAYOUT_UNDEFINED,                                                      // VkImageLayout                                        initialLayout;
285                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                       // VkImageLayout                                        finalLayout;
286         };
287
288         const VkAttachmentReference colorAttachmentRef =
289         {
290                 0u,                                                                                                     // deUint32                     attachment;
291                 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL                        // VkImageLayout        layout;
292         };
293
294         const VkSubpassDescription subpassDescription =
295         {
296                 (VkSubpassDescriptionFlags)0,                                           // VkSubpassDescriptionFlags            flags;
297                 VK_PIPELINE_BIND_POINT_GRAPHICS,                                        // VkPipelineBindPoint                          pipelineBindPoint;
298                 0u,                                                                                                     // deUint32                                                     inputAttachmentCount;
299                 DE_NULL,                                                                                        // const VkAttachmentReference*         pInputAttachments;
300                 1u,                                                                                                     // deUint32                                                     colorAttachmentCount;
301                 &colorAttachmentRef,                                                            // const VkAttachmentReference*         pColorAttachments;
302                 DE_NULL,                                                                                        // const VkAttachmentReference*         pResolveAttachments;
303                 DE_NULL,                                                                                        // const VkAttachmentReference*         pDepthStencilAttachment;
304                 0u,                                                                                                     // deUint32                                                     preserveAttachmentCount;
305                 DE_NULL                                                                                         // const deUint32*                                      pPreserveAttachments;
306         };
307
308         const VkRenderPassCreateInfo renderPassInfo =
309         {
310                 VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,                      // VkStructureType                                      sType;
311                 DE_NULL,                                                                                        // const void*                                          pNext;
312                 (VkRenderPassCreateFlags)0,                                                     // VkRenderPassCreateFlags                      flags;
313                 1u,                                                                                                     // deUint32                                                     attachmentCount;
314                 &colorAttachmentDescription,                                            // const VkAttachmentDescription*       pAttachments;
315                 1u,                                                                                                     // deUint32                                                     subpassCount;
316                 &subpassDescription,                                                            // const VkSubpassDescription*          pSubpasses;
317                 0u,                                                                                                     // deUint32                                                     dependencyCount;
318                 DE_NULL                                                                                         // const VkSubpassDependency*           pDependencies;
319         };
320
321         return createRenderPass(vk, device, &renderPassInfo);
322 }
323
324 Move<VkPipeline> makeGraphicsPipeline (const DeviceInterface&                                   vk,
325                                                                            const VkDevice                                                       device,
326                                                                            const VkPipelineLayout                                       pipelineLayout,
327                                                                            const VkRenderPass                                           renderPass,
328                                                                            const IVec2                                                          renderSize,
329                                                                            const VkPrimitiveTopology                            topology,
330                                                                            const deUint32                                                       stageCount,
331                                                                            const VkPipelineShaderStageCreateInfo*       pStages)
332 {
333         const VkVertexInputBindingDescription vertexInputBindingDescription =
334         {
335                 0u,                                                             // uint32_t                             binding;
336                 sizeof(Vec4),                                   // uint32_t                             stride;
337                 VK_VERTEX_INPUT_RATE_VERTEX,    // VkVertexInputRate    inputRate;
338         };
339
340         const VkVertexInputAttributeDescription vertexInputAttributeDescription =
341         {
342                 0u,                                                                     // uint32_t                     location;
343                 0u,                                                                     // uint32_t                     binding;
344                 VK_FORMAT_R32G32B32A32_SFLOAT,          // VkFormat                     format;
345                 0u,                                                                     // uint32_t                     offset;
346         };
347
348         const VkPipelineVertexInputStateCreateInfo vertexInputStateInfo =
349         {
350                 VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,              // VkStructureType                             sType;
351                 DE_NULL,                                                                                                                // const void*                                 pNext;
352                 (VkPipelineVertexInputStateCreateFlags)0,                                               // VkPipelineVertexInputStateCreateFlags       flags;
353                 1u,                                                                                                                             // uint32_t                                    vertexBindingDescriptionCount;
354                 &vertexInputBindingDescription,                                                                 // const VkVertexInputBindingDescription*      pVertexBindingDescriptions;
355                 1u,                                                                                                                             // uint32_t                                    vertexAttributeDescriptionCount;
356                 &vertexInputAttributeDescription,                                                               // const VkVertexInputAttributeDescription*    pVertexAttributeDescriptions;
357         };
358
359         const VkPipelineInputAssemblyStateCreateInfo pipelineInputAssemblyStateInfo =
360         {
361                 VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,    // VkStructureType                             sType;
362                 DE_NULL,                                                                                                                // const void*                                 pNext;
363                 (VkPipelineInputAssemblyStateCreateFlags)0,                                             // VkPipelineInputAssemblyStateCreateFlags     flags;
364                 topology,                                                                                                               // VkPrimitiveTopology                         topology;
365                 VK_FALSE,                                                                                                               // VkBool32                                    primitiveRestartEnable;
366         };
367
368         const VkViewport viewport = makeViewport(
369                 0.0f, 0.0f,
370                 static_cast<float>(renderSize.x()), static_cast<float>(renderSize.y()),
371                 0.0f, 1.0f);
372
373         const VkRect2D scissor = {
374                 makeOffset2D(0, 0),
375                 makeExtent2D(static_cast<deUint32>(renderSize.x()), static_cast<deUint32>(renderSize.y())),
376         };
377
378         const VkPipelineViewportStateCreateInfo pipelineViewportStateInfo =
379         {
380                 VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,                  // VkStructureType                             sType;
381                 DE_NULL,                                                                                                                // const void*                                 pNext;
382                 (VkPipelineViewportStateCreateFlags)0,                                                  // VkPipelineViewportStateCreateFlags          flags;
383                 1u,                                                                                                                             // uint32_t                                    viewportCount;
384                 &viewport,                                                                                                              // const VkViewport*                           pViewports;
385                 1u,                                                                                                                             // uint32_t                                    scissorCount;
386                 &scissor,                                                                                                               // const VkRect2D*                             pScissors;
387         };
388
389         const VkPipelineRasterizationStateCreateInfo pipelineRasterizationStateInfo =
390         {
391                 VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,             // VkStructureType                          sType;
392                 DE_NULL,                                                                                                                // const void*                              pNext;
393                 (VkPipelineRasterizationStateCreateFlags)0,                                             // VkPipelineRasterizationStateCreateFlags  flags;
394                 VK_FALSE,                                                                                                               // VkBool32                                 depthClampEnable;
395                 VK_FALSE,                                                                                                               // VkBool32                                 rasterizerDiscardEnable;
396                 VK_POLYGON_MODE_FILL,                                                                                   // VkPolygonMode                                                        polygonMode;
397                 VK_CULL_MODE_NONE,                                                                                              // VkCullModeFlags                                                      cullMode;
398                 VK_FRONT_FACE_COUNTER_CLOCKWISE,                                                                // VkFrontFace                                                          frontFace;
399                 VK_FALSE,                                                                                                               // VkBool32                                                                     depthBiasEnable;
400                 0.0f,                                                                                                                   // float                                                                        depthBiasConstantFactor;
401                 0.0f,                                                                                                                   // float                                                                        depthBiasClamp;
402                 0.0f,                                                                                                                   // float                                                                        depthBiasSlopeFactor;
403                 1.0f,                                                                                                                   // float                                                                        lineWidth;
404         };
405
406         const VkPipelineMultisampleStateCreateInfo pipelineMultisampleStateInfo =
407         {
408                 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,               // VkStructureType                                                      sType;
409                 DE_NULL,                                                                                                                // const void*                                                          pNext;
410                 (VkPipelineMultisampleStateCreateFlags)0,                                               // VkPipelineMultisampleStateCreateFlags        flags;
411                 VK_SAMPLE_COUNT_1_BIT,                                                                                  // VkSampleCountFlagBits                                        rasterizationSamples;
412                 VK_FALSE,                                                                                                               // VkBool32                                                                     sampleShadingEnable;
413                 0.0f,                                                                                                                   // float                                                                        minSampleShading;
414                 DE_NULL,                                                                                                                // const VkSampleMask*                                          pSampleMask;
415                 VK_FALSE,                                                                                                               // VkBool32                                                                     alphaToCoverageEnable;
416                 VK_FALSE                                                                                                                // VkBool32                                                                     alphaToOneEnable;
417         };
418
419         const VkStencilOpState stencilOpState = makeStencilOpState(
420                 VK_STENCIL_OP_KEEP,                             // stencil fail
421                 VK_STENCIL_OP_KEEP,                             // depth & stencil pass
422                 VK_STENCIL_OP_KEEP,                             // depth only fail
423                 VK_COMPARE_OP_ALWAYS,                   // compare op
424                 0u,                                                             // compare mask
425                 0u,                                                             // write mask
426                 0u);                                                    // reference
427
428         VkPipelineDepthStencilStateCreateInfo pipelineDepthStencilStateInfo =
429         {
430                 VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,             // VkStructureType                                                      sType;
431                 DE_NULL,                                                                                                                // const void*                                                          pNext;
432                 (VkPipelineDepthStencilStateCreateFlags)0,                                              // VkPipelineDepthStencilStateCreateFlags       flags;
433                 VK_FALSE,                                                                                                               // VkBool32                                                                     depthTestEnable;
434                 VK_FALSE,                                                                                                               // VkBool32                                                                     depthWriteEnable;
435                 VK_COMPARE_OP_LESS,                                                                                             // VkCompareOp                                                          depthCompareOp;
436                 VK_FALSE,                                                                                                               // VkBool32                                                                     depthBoundsTestEnable;
437                 VK_FALSE,                                                                                                               // VkBool32                                                                     stencilTestEnable;
438                 stencilOpState,                                                                                                 // VkStencilOpState                                                     front;
439                 stencilOpState,                                                                                                 // VkStencilOpState                                                     back;
440                 0.0f,                                                                                                                   // float                                                                        minDepthBounds;
441                 1.0f,                                                                                                                   // float                                                                        maxDepthBounds;
442         };
443
444         const VkColorComponentFlags                                     colorComponentsAll                                      = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
445         const VkPipelineColorBlendAttachmentState       pipelineColorBlendAttachmentState       =
446         {
447                 VK_FALSE,                                               // VkBool32                                     blendEnable;
448                 VK_BLEND_FACTOR_ONE,                    // VkBlendFactor                        srcColorBlendFactor;
449                 VK_BLEND_FACTOR_ZERO,                   // VkBlendFactor                        dstColorBlendFactor;
450                 VK_BLEND_OP_ADD,                                // VkBlendOp                            colorBlendOp;
451                 VK_BLEND_FACTOR_ONE,                    // VkBlendFactor                        srcAlphaBlendFactor;
452                 VK_BLEND_FACTOR_ZERO,                   // VkBlendFactor                        dstAlphaBlendFactor;
453                 VK_BLEND_OP_ADD,                                // VkBlendOp                            alphaBlendOp;
454                 colorComponentsAll,                             // VkColorComponentFlags        colorWriteMask;
455         };
456
457         const VkPipelineColorBlendStateCreateInfo pipelineColorBlendStateInfo =
458         {
459                 VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,               // VkStructureType                                                              sType;
460                 DE_NULL,                                                                                                                // const void*                                                                  pNext;
461                 (VkPipelineColorBlendStateCreateFlags)0,                                                // VkPipelineColorBlendStateCreateFlags                 flags;
462                 VK_FALSE,                                                                                                               // VkBool32                                                                             logicOpEnable;
463                 VK_LOGIC_OP_COPY,                                                                                               // VkLogicOp                                                                    logicOp;
464                 1u,                                                                                                                             // deUint32                                                                             attachmentCount;
465                 &pipelineColorBlendAttachmentState,                                                             // const VkPipelineColorBlendAttachmentState*   pAttachments;
466                 { 0.0f, 0.0f, 0.0f, 0.0f },                                                                             // float                                                                                blendConstants[4];
467         };
468
469         const VkGraphicsPipelineCreateInfo graphicsPipelineInfo =
470         {
471                 VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,        // VkStructureType                                                                      sType;
472                 DE_NULL,                                                                                        // const void*                                                                          pNext;
473                 (VkPipelineCreateFlags)0,                                                       // VkPipelineCreateFlags                                                        flags;
474                 stageCount,                                                                                     // deUint32                                                                                     stageCount;
475                 pStages,                                                                                        // const VkPipelineShaderStageCreateInfo*                       pStages;
476                 &vertexInputStateInfo,                                                          // const VkPipelineVertexInputStateCreateInfo*          pVertexInputState;
477                 &pipelineInputAssemblyStateInfo,                                        // const VkPipelineInputAssemblyStateCreateInfo*        pInputAssemblyState;
478                 DE_NULL,                                                                                        // const VkPipelineTessellationStateCreateInfo*         pTessellationState;
479                 &pipelineViewportStateInfo,                                                     // const VkPipelineViewportStateCreateInfo*                     pViewportState;
480                 &pipelineRasterizationStateInfo,                                        // const VkPipelineRasterizationStateCreateInfo*        pRasterizationState;
481                 &pipelineMultisampleStateInfo,                                          // const VkPipelineMultisampleStateCreateInfo*          pMultisampleState;
482                 &pipelineDepthStencilStateInfo,                                         // const VkPipelineDepthStencilStateCreateInfo*         pDepthStencilState;
483                 &pipelineColorBlendStateInfo,                                           // const VkPipelineColorBlendStateCreateInfo*           pColorBlendState;
484                 DE_NULL,                                                                                        // const VkPipelineDynamicStateCreateInfo*                      pDynamicState;
485                 pipelineLayout,                                                                         // VkPipelineLayout                                                                     layout;
486                 renderPass,                                                                                     // VkRenderPass                                                                         renderPass;
487                 0u,                                                                                                     // deUint32                                                                                     subpass;
488                 DE_NULL,                                                                                        // VkPipeline                                                                           basePipelineHandle;
489                 0,                                                                                                      // deInt32                                                                                      basePipelineIndex;
490         };
491
492         return createGraphicsPipeline(vk, device, DE_NULL, &graphicsPipelineInfo);
493 }
494
495 //! Return true if there are any red (or all zero) pixels in the image
496 bool imageHasErrorPixels (const tcu::ConstPixelBufferAccess image)
497 {
498         const Vec4 errorColor   = Vec4(1.0f, 0.0f, 0.0f, 1.0f);
499         const Vec4 blankColor   = Vec4();
500
501         for (int y = 0; y < image.getHeight(); ++y)
502         for (int x = 0; x < image.getWidth(); ++x)
503         {
504                 const Vec4 color = image.getPixel(x, y);
505                 if (color == errorColor || color == blankColor)
506                         return true;
507         }
508
509         return false;
510 }
511
512 class Renderer
513 {
514 public:
515         typedef std::map<VkShaderStageFlagBits, const VkSpecializationInfo*>    SpecializationMap;
516
517         //! Use the delegate to bind descriptor sets, vertex buffers, etc. and make a draw call
518         struct Delegate
519         {
520                 virtual                 ~Delegate               (void) {}
521                 virtual void    rendererDraw    (const VkPipelineLayout pipelineLayout, const VkCommandBuffer cmdBuffer) const = 0;
522         };
523
524         Renderer (const DeviceInterface&                                        vk,
525                           const VkDevice                                                        device,
526                           Allocator&                                                            allocator,
527                           const deUint32                                                        queueFamilyIndex,
528                           const VkDescriptorSetLayout                           descriptorSetLayout,    //!< may be NULL, if no descriptors are used
529                           ProgramCollection<vk::ProgramBinary>&         binaryCollection,
530                           const std::string&                                            vertexName,
531                           const std::string&                                            fragmentName,
532                           const VkBuffer                                                        colorBuffer,
533                           const IVec2&                                                          renderSize,
534                           const VkFormat                                                        colorFormat,
535                           const Vec4&                                                           clearColor,
536                           const VkPrimitiveTopology                                     topology,
537                           SpecializationMap                                                     specMap = SpecializationMap())
538                 : m_colorBuffer                         (colorBuffer)
539                 , m_renderSize                          (renderSize)
540                 , m_colorFormat                         (colorFormat)
541                 , m_colorSubresourceRange       (makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, 1u))
542                 , m_clearColor                          (clearColor)
543                 , m_topology                            (topology)
544                 , m_descriptorSetLayout         (descriptorSetLayout)
545         {
546                 m_colorImage            = makeImage             (vk, device, makeImageCreateInfo(m_colorFormat, m_renderSize, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT));
547                 m_colorImageAlloc       = bindImage             (vk, device, allocator, *m_colorImage, MemoryRequirement::Any);
548                 m_colorAttachment       = makeImageView (vk, device, *m_colorImage, VK_IMAGE_VIEW_TYPE_2D, m_colorFormat, m_colorSubresourceRange);
549
550                 m_vertexModule          = createShaderModule    (vk, device, binaryCollection.get(vertexName), 0u);
551                 m_fragmentModule        = createShaderModule    (vk, device, binaryCollection.get(fragmentName), 0u);
552
553                 const VkPipelineShaderStageCreateInfo pShaderStages[] =
554                 {
555                         {
556                                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,            // VkStructureType                                              sType;
557                                 DE_NULL,                                                                                                        // const void*                                                  pNext;
558                                 (VkPipelineShaderStageCreateFlags)0,                                            // VkPipelineShaderStageCreateFlags             flags;
559                                 VK_SHADER_STAGE_VERTEX_BIT,                                                                     // VkShaderStageFlagBits                                stage;
560                                 *m_vertexModule,                                                                                        // VkShaderModule                                               module;
561                                 "main",                                                                                                         // const char*                                                  pName;
562                                 specMap[VK_SHADER_STAGE_VERTEX_BIT],                                            // const VkSpecializationInfo*                  pSpecializationInfo;
563                         },
564                         {
565                                 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,            // VkStructureType                                              sType;
566                                 DE_NULL,                                                                                                        // const void*                                                  pNext;
567                                 (VkPipelineShaderStageCreateFlags)0,                                            // VkPipelineShaderStageCreateFlags             flags;
568                                 VK_SHADER_STAGE_FRAGMENT_BIT,                                                           // VkShaderStageFlagBits                                stage;
569                                 *m_fragmentModule,                                                                                      // VkShaderModule                                               module;
570                                 "main",                                                                                                         // const char*                                                  pName;
571                                 specMap[VK_SHADER_STAGE_FRAGMENT_BIT],                                          // const VkSpecializationInfo*                  pSpecializationInfo;
572                         }
573                 };
574
575                 m_renderPass            = makeRenderPass                (vk, device, m_colorFormat);
576                 m_framebuffer           = makeFramebuffer               (vk, device, *m_renderPass, 1u, &m_colorAttachment.get(),
577                                                                                                          static_cast<deUint32>(m_renderSize.x()), static_cast<deUint32>(m_renderSize.y()));
578                 m_pipelineLayout        = makePipelineLayout    (vk, device, m_descriptorSetLayout);
579                 m_pipeline                      = makeGraphicsPipeline  (vk, device, *m_pipelineLayout, *m_renderPass, m_renderSize, m_topology, DE_LENGTH_OF_ARRAY(pShaderStages), pShaderStages);
580                 m_cmdPool                       = makeCommandPool               (vk, device, queueFamilyIndex);
581                 m_cmdBuffer                     = allocateCommandBuffer (vk, device, *m_cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
582         }
583
584         void draw (const DeviceInterface&       vk,
585                            const VkDevice                       device,
586                            const VkQueue                        queue,
587                            const Delegate&                      drawDelegate) const
588         {
589                 beginCommandBuffer(vk, *m_cmdBuffer);
590
591                 const VkClearValue                      clearValue      = makeClearValueColor(m_clearColor);
592                 const VkRect2D                          renderArea      =
593                 {
594                         makeOffset2D(0, 0),
595                         makeExtent2D(m_renderSize.x(), m_renderSize.y()),
596                 };
597                 const VkRenderPassBeginInfo renderPassBeginInfo =
598                 {
599                         VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,               // VkStructureType         sType;
600                         DE_NULL,                                                                                // const void*             pNext;
601                         *m_renderPass,                                                                  // VkRenderPass            renderPass;
602                         *m_framebuffer,                                                                 // VkFramebuffer           framebuffer;
603                         renderArea,                                                                             // VkRect2D                renderArea;
604                         1u,                                                                                             // uint32_t                clearValueCount;
605                         &clearValue,                                                                    // const VkClearValue*     pClearValues;
606                 };
607                 vk.cmdBeginRenderPass(*m_cmdBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
608
609                 vk.cmdBindPipeline(*m_cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
610                 drawDelegate.rendererDraw(*m_pipelineLayout, *m_cmdBuffer);
611
612                 vk.cmdEndRenderPass(*m_cmdBuffer);
613
614                 // Prepare color image for copy
615                 {
616                         const VkImageMemoryBarrier barriers[] =
617                         {
618                                 {
619                                         VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,                                         // VkStructureType                      sType;
620                                         DE_NULL,                                                                                                        // const void*                          pNext;
621                                         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,                                           // VkAccessFlags                        outputMask;
622                                         VK_ACCESS_TRANSFER_READ_BIT,                                                            // VkAccessFlags                        inputMask;
623                                         VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,                                       // VkImageLayout                        oldLayout;
624                                         VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,                                           // VkImageLayout                        newLayout;
625                                         VK_QUEUE_FAMILY_IGNORED,                                                                        // deUint32                                     srcQueueFamilyIndex;
626                                         VK_QUEUE_FAMILY_IGNORED,                                                                        // deUint32                                     destQueueFamilyIndex;
627                                         *m_colorImage,                                                                                          // VkImage                                      image;
628                                         m_colorSubresourceRange,                                                                        // VkImageSubresourceRange      subresourceRange;
629                                 },
630                         };
631
632                         vk.cmdPipelineBarrier(*m_cmdBuffer, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u,
633                                 0u, DE_NULL, 0u, DE_NULL, DE_LENGTH_OF_ARRAY(barriers), barriers);
634                 }
635                 // Color image -> host buffer
636                 {
637                         const VkBufferImageCopy region =
638                         {
639                                 0ull,                                                                                                                                           // VkDeviceSize                bufferOffset;
640                                 0u,                                                                                                                                                     // uint32_t                    bufferRowLength;
641                                 0u,                                                                                                                                                     // uint32_t                    bufferImageHeight;
642                                 makeImageSubresourceLayers(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 0u, 1u),                      // VkImageSubresourceLayers    imageSubresource;
643                                 makeOffset3D(0, 0, 0),                                                                                                          // VkOffset3D                  imageOffset;
644                                 makeExtent3D(m_renderSize.x(), m_renderSize.y(), 1u),                                           // VkExtent3D                  imageExtent;
645                         };
646
647                         vk.cmdCopyImageToBuffer(*m_cmdBuffer, *m_colorImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, m_colorBuffer, 1u, &region);
648                 }
649                 // Buffer write barrier
650                 {
651                         const VkBufferMemoryBarrier barriers[] =
652                         {
653                                 {
654                                         VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,                // VkStructureType    sType;
655                                         DE_NULL,                                                                                // const void*        pNext;
656                                         VK_ACCESS_TRANSFER_WRITE_BIT,                                   // VkAccessFlags      srcAccessMask;
657                                         VK_ACCESS_HOST_READ_BIT,                                                // VkAccessFlags      dstAccessMask;
658                                         VK_QUEUE_FAMILY_IGNORED,                                                // uint32_t           srcQueueFamilyIndex;
659                                         VK_QUEUE_FAMILY_IGNORED,                                                // uint32_t           dstQueueFamilyIndex;
660                                         m_colorBuffer,                                                                  // VkBuffer           buffer;
661                                         0ull,                                                                                   // VkDeviceSize       offset;
662                                         VK_WHOLE_SIZE,                                                                  // VkDeviceSize       size;
663                                 },
664                         };
665
666                         vk.cmdPipelineBarrier(*m_cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u,
667                                 0u, DE_NULL, DE_LENGTH_OF_ARRAY(barriers), barriers, DE_NULL, 0u);
668                 }
669
670                 VK_CHECK(vk.endCommandBuffer(*m_cmdBuffer));
671                 submitCommandsAndWait(vk, device, queue, *m_cmdBuffer);
672         }
673
674 private:
675         const VkBuffer                                  m_colorBuffer;
676         const IVec2                                             m_renderSize;
677         const VkFormat                                  m_colorFormat;
678         const VkImageSubresourceRange   m_colorSubresourceRange;
679         const Vec4                                              m_clearColor;
680         const VkPrimitiveTopology               m_topology;
681         const VkDescriptorSetLayout             m_descriptorSetLayout;
682
683         Move<VkImage>                                   m_colorImage;
684         MovePtr<Allocation>                             m_colorImageAlloc;
685         Move<VkImageView>                               m_colorAttachment;
686         Move<VkShaderModule>                    m_vertexModule;
687         Move<VkShaderModule>                    m_fragmentModule;
688         Move<VkRenderPass>                              m_renderPass;
689         Move<VkFramebuffer>                             m_framebuffer;
690         Move<VkPipelineLayout>                  m_pipelineLayout;
691         Move<VkPipeline>                                m_pipeline;
692         Move<VkCommandPool>                             m_cmdPool;
693         Move<VkCommandBuffer>                   m_cmdBuffer;
694
695         // "deleted"
696                                 Renderer        (const Renderer&);
697         Renderer&       operator=       (const Renderer&);
698 };
699
700 void bindSparseBuffer (const DeviceInterface& vk, const VkDevice device, const VkQueue sparseQueue, const VkBuffer buffer, const SparseAllocation& sparseAllocation)
701 {
702         const VkSparseBufferMemoryBindInfo sparseBufferMemoryBindInfo =
703         {
704                 buffer,                                                                                                         // VkBuffer                     buffer;
705                 static_cast<deUint32>(sparseAllocation.memoryBinds.size()),     // uint32_t                     bindCount;
706                 &sparseAllocation.memoryBinds[0],                                                       // const VkSparseMemoryBind*    pBinds;
707         };
708
709         const VkBindSparseInfo bindInfo =
710         {
711                 VK_STRUCTURE_TYPE_BIND_SPARSE_INFO,                                     // VkStructureType                             sType;
712                 DE_NULL,                                                                                        // const void*                                 pNext;
713                 0u,                                                                                                     // uint32_t                                    waitSemaphoreCount;
714                 DE_NULL,                                                                                        // const VkSemaphore*                          pWaitSemaphores;
715                 1u,                                                                                                     // uint32_t                                    bufferBindCount;
716                 &sparseBufferMemoryBindInfo,                                            // const VkSparseBufferMemoryBindInfo*         pBufferBinds;
717                 0u,                                                                                                     // uint32_t                                    imageOpaqueBindCount;
718                 DE_NULL,                                                                                        // const VkSparseImageOpaqueMemoryBindInfo*    pImageOpaqueBinds;
719                 0u,                                                                                                     // uint32_t                                    imageBindCount;
720                 DE_NULL,                                                                                        // const VkSparseImageMemoryBindInfo*          pImageBinds;
721                 0u,                                                                                                     // uint32_t                                    signalSemaphoreCount;
722                 DE_NULL,                                                                                        // const VkSemaphore*                          pSignalSemaphores;
723         };
724
725         const Unique<VkFence> fence(createFence(vk, device));
726
727         VK_CHECK(vk.queueBindSparse(sparseQueue, 1u, &bindInfo, *fence));
728         VK_CHECK(vk.waitForFences(device, 1u, &fence.get(), VK_TRUE, ~0ull));
729 }
730
731 class SparseBufferTestInstance : public SparseResourcesBaseInstance, Renderer::Delegate
732 {
733 public:
734         SparseBufferTestInstance (Context& context, const TestFlags flags)
735                 : SparseResourcesBaseInstance   (context)
736                 , m_aliased                                             ((flags & TEST_FLAG_ALIASED)   != 0)
737                 , m_residency                                   ((flags & TEST_FLAG_RESIDENCY) != 0)
738                 , m_nonResidentStrict                   ((flags & TEST_FLAG_NON_RESIDENT_STRICT) != 0)
739                 , m_renderSize                                  (RENDER_SIZE, RENDER_SIZE)
740                 , m_colorFormat                                 (VK_FORMAT_R8G8B8A8_UNORM)
741                 , m_colorBufferSize                             (m_renderSize.x() * m_renderSize.y() * tcu::getPixelSize(mapVkFormat(m_colorFormat)))
742         {
743                 const VkPhysicalDeviceFeatures  features        = getPhysicalDeviceFeatures(m_context.getInstanceInterface(), m_context.getPhysicalDevice());
744
745                 if (!features.sparseBinding)
746                         TCU_THROW(NotSupportedError, "Missing feature: sparseBinding");
747
748                 if (m_residency && !features.sparseResidencyBuffer)
749                         TCU_THROW(NotSupportedError, "Missing feature: sparseResidencyBuffer");
750
751                 if (m_aliased && !features.sparseResidencyAliased)
752                         TCU_THROW(NotSupportedError, "Missing feature: sparseResidencyAliased");
753
754                 if (m_nonResidentStrict && !m_context.getDeviceProperties().sparseProperties.residencyNonResidentStrict)
755                         TCU_THROW(NotSupportedError, "Missing sparse property: residencyNonResidentStrict");
756
757                 {
758                         QueueRequirementsVec requirements;
759                         requirements.push_back(QueueRequirements(VK_QUEUE_SPARSE_BINDING_BIT, 1u));
760                         requirements.push_back(QueueRequirements(VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, 1u));
761
762                         createDeviceSupportingQueues(requirements);
763                 }
764
765                 const DeviceInterface& vk               = getDeviceInterface();
766                 m_sparseQueue                                   = getQueue(VK_QUEUE_SPARSE_BINDING_BIT, 0u);
767                 m_universalQueue                                = getQueue(VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, 0u);
768
769                 m_sharedQueueFamilyIndices[0]   = m_sparseQueue.queueFamilyIndex;
770                 m_sharedQueueFamilyIndices[1]   = m_universalQueue.queueFamilyIndex;
771
772                 m_colorBuffer                                   = makeBuffer(vk, getDevice(), makeBufferCreateInfo(m_colorBufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT));
773                 m_colorBufferAlloc                              = bindBuffer(vk, getDevice(), getAllocator(), *m_colorBuffer, MemoryRequirement::HostVisible);
774
775                 deMemset(m_colorBufferAlloc->getHostPtr(), 0, static_cast<std::size_t>(m_colorBufferSize));
776                 flushMappedMemoryRange(vk, getDevice(), m_colorBufferAlloc->getMemory(), m_colorBufferAlloc->getOffset(), m_colorBufferSize);
777         }
778
779 protected:
780         VkBufferCreateInfo getSparseBufferCreateInfo (const VkBufferUsageFlags usage) const
781         {
782                 VkBufferCreateFlags     flags = VK_BUFFER_CREATE_SPARSE_BINDING_BIT;
783                 if (m_residency)
784                         flags |= VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT;
785                 if (m_aliased)
786                         flags |= VK_BUFFER_CREATE_SPARSE_ALIASED_BIT;
787
788                 VkBufferCreateInfo referenceBufferCreateInfo =
789                 {
790                         VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,                           // VkStructureType        sType;
791                         DE_NULL,                                                                                        // const void*            pNext;
792                         flags,                                                                                          // VkBufferCreateFlags    flags;
793                         0u,     // override later                                                               // VkDeviceSize           size;
794                         VK_BUFFER_USAGE_TRANSFER_DST_BIT | usage,                       // VkBufferUsageFlags     usage;
795                         VK_SHARING_MODE_EXCLUSIVE,                                                      // VkSharingMode          sharingMode;
796                         0u,                                                                                                     // uint32_t               queueFamilyIndexCount;
797                         DE_NULL,                                                                                        // const uint32_t*        pQueueFamilyIndices;
798                 };
799
800                 if (m_sparseQueue.queueFamilyIndex != m_universalQueue.queueFamilyIndex)
801                 {
802                         referenceBufferCreateInfo.sharingMode                   = VK_SHARING_MODE_CONCURRENT;
803                         referenceBufferCreateInfo.queueFamilyIndexCount = DE_LENGTH_OF_ARRAY(m_sharedQueueFamilyIndices);
804                         referenceBufferCreateInfo.pQueueFamilyIndices   = m_sharedQueueFamilyIndices;
805                 }
806
807                 return referenceBufferCreateInfo;
808         }
809
810         void draw (const VkPrimitiveTopology    topology,
811                            const VkDescriptorSetLayout  descriptorSetLayout     = DE_NULL,
812                            Renderer::SpecializationMap  specMap                         = Renderer::SpecializationMap())
813         {
814                 const UniquePtr<Renderer> renderer(new Renderer(
815                         getDeviceInterface(), getDevice(), getAllocator(), m_universalQueue.queueFamilyIndex, descriptorSetLayout,
816                         m_context.getBinaryCollection(), "vert", "frag", *m_colorBuffer, m_renderSize, m_colorFormat, Vec4(1.0f, 0.0f, 0.0f, 1.0f), topology, specMap));
817
818                 renderer->draw(getDeviceInterface(), getDevice(), m_universalQueue.queueHandle, *this);
819         }
820
821         tcu::TestStatus verifyDrawResult (void) const
822         {
823                 invalidateMappedMemoryRange(getDeviceInterface(), getDevice(), m_colorBufferAlloc->getMemory(), 0ull, m_colorBufferSize);
824
825                 const tcu::ConstPixelBufferAccess resultImage (mapVkFormat(m_colorFormat), m_renderSize.x(), m_renderSize.y(), 1u, m_colorBufferAlloc->getHostPtr());
826
827                 m_context.getTestContext().getLog()
828                         << tcu::LogImageSet("Result", "Result") << tcu::LogImage("color0", "", resultImage) << tcu::TestLog::EndImageSet;
829
830                 if (imageHasErrorPixels(resultImage))
831                         return tcu::TestStatus::fail("Some buffer values were incorrect");
832                 else
833                         return tcu::TestStatus::pass("Pass");
834         }
835
836         const bool                                                      m_aliased;
837         const bool                                                      m_residency;
838         const bool                                                      m_nonResidentStrict;
839
840         Queue                                                           m_sparseQueue;
841         Queue                                                           m_universalQueue;
842
843 private:
844         const IVec2                                                     m_renderSize;
845         const VkFormat                                          m_colorFormat;
846         const VkDeviceSize                                      m_colorBufferSize;
847
848         Move<VkBuffer>                                          m_colorBuffer;
849         MovePtr<Allocation>                                     m_colorBufferAlloc;
850
851         deUint32                                                        m_sharedQueueFamilyIndices[2];
852 };
853
854 void initProgramsDrawWithUBO (vk::SourceCollections& programCollection, const TestFlags flags)
855 {
856         // Vertex shader
857         {
858                 std::ostringstream src;
859                 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
860                         << "\n"
861                         << "layout(location = 0) in vec4 in_position;\n"
862                         << "\n"
863                         << "out gl_PerVertex {\n"
864                         << "    vec4 gl_Position;\n"
865                         << "};\n"
866                         << "\n"
867                         << "void main(void)\n"
868                         << "{\n"
869                         << "    gl_Position = in_position;\n"
870                         << "}\n";
871
872                 programCollection.glslSources.add("vert") << glu::VertexSource(src.str());
873         }
874
875         // Fragment shader
876         {
877                 const bool                      aliased                         = (flags & TEST_FLAG_ALIASED) != 0;
878                 const bool                      residency                       = (flags & TEST_FLAG_RESIDENCY) != 0;
879                 const bool                      nonResidentStrict       = (flags & TEST_FLAG_NON_RESIDENT_STRICT) != 0;
880                 const std::string       valueExpr                       = (aliased ? "ivec4(3*(ndx % nonAliasedSize) ^ 127, 0, 0, 0)" : "ivec4(3*ndx ^ 127, 0, 0, 0)");
881
882                 std::ostringstream src;
883                 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
884                         << "\n"
885                         << "layout(location = 0) out vec4 o_color;\n"
886                         << "\n"
887                         << "layout(constant_id = 1) const int dataSize  = 1;\n"
888                         << "layout(constant_id = 2) const int chunkSize = 1;\n"
889                         << "\n"
890                         << "layout(set = 0, binding = 0, std140) uniform SparseBuffer {\n"
891                         << "    ivec4 data[dataSize];\n"
892                         << "} ubo;\n"
893                         << "\n"
894                         << "void main(void)\n"
895                         << "{\n"
896                         << "    const int fragNdx        = int(gl_FragCoord.x) + " << RENDER_SIZE << " * int(gl_FragCoord.y);\n"
897                         << "    const int pageSize       = " << RENDER_SIZE << " * " << RENDER_SIZE << ";\n"
898                         << "    const int numChunks      = dataSize / chunkSize;\n";
899
900                 if (aliased)
901                         src << "    const int nonAliasedSize = (numChunks > 1 ? dataSize - chunkSize : dataSize);\n";
902
903                 src << "    bool      ok             = true;\n"
904                         << "\n"
905                         << "    for (int ndx = fragNdx; ndx < dataSize; ndx += pageSize)\n"
906                         << "    {\n";
907
908                 if (residency && nonResidentStrict)
909                 {
910                         src << "        if (ndx >= chunkSize && ndx < 2*chunkSize)\n"
911                                 << "            ok = ok && (ubo.data[ndx] == ivec4(0));\n"
912                                 << "        else\n"
913                                 << "            ok = ok && (ubo.data[ndx] == " + valueExpr + ");\n";
914                 }
915                 else if (residency)
916                 {
917                         src << "        if (ndx >= chunkSize && ndx < 2*chunkSize)\n"
918                                 << "            continue;\n"
919                                 << "        ok = ok && (ubo.data[ndx] == " << valueExpr << ");\n";
920                 }
921                 else
922                         src << "        ok = ok && (ubo.data[ndx] == " << valueExpr << ");\n";
923
924                 src << "    }\n"
925                         << "\n"
926                         << "    if (ok)\n"
927                         << "        o_color = vec4(0.0, 1.0, 0.0, 1.0);\n"
928                         << "    else\n"
929                         << "        o_color = vec4(1.0, 0.0, 0.0, 1.0);\n"
930                         << "}\n";
931
932                 programCollection.glslSources.add("frag") << glu::FragmentSource(src.str());
933         }
934 }
935
936 //! Sparse buffer backing a UBO
937 class UBOTestInstance : public SparseBufferTestInstance
938 {
939 public:
940         UBOTestInstance (Context& context, const TestFlags flags)
941                 : SparseBufferTestInstance      (context, flags)
942         {
943         }
944
945         void rendererDraw (const VkPipelineLayout pipelineLayout, const VkCommandBuffer cmdBuffer) const
946         {
947                 const DeviceInterface&  vk                              = getDeviceInterface();
948                 const VkDeviceSize              vertexOffset    = 0ull;
949
950                 vk.cmdBindVertexBuffers (cmdBuffer, 0u, 1u, &m_vertexBuffer.get(), &vertexOffset);
951                 vk.cmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0u, 1u, &m_descriptorSet.get(), 0u, DE_NULL);
952                 vk.cmdDraw                              (cmdBuffer, 4u, 1u, 0u, 0u);
953         }
954
955         tcu::TestStatus iterate (void)
956         {
957                 const DeviceInterface&          vk                                      = getDeviceInterface();
958                 MovePtr<SparseAllocation>       sparseAllocation;
959                 Move<VkBuffer>                          sparseBuffer;
960                 Move<VkBuffer>                          sparseBufferAliased;
961
962                 // Set up the sparse buffer
963                 {
964                         VkBufferCreateInfo      referenceBufferCreateInfo       = getSparseBufferCreateInfo(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
965                         const VkDeviceSize      minChunkSize                            = 512u; // make sure the smallest allocation is at least this big
966                         deUint32                        numMaxChunks                            = 0u;
967
968                         // Check how many chunks we can allocate given the alignment and size requirements of UBOs
969                         {
970                                 const UniquePtr<SparseAllocation> minAllocation(SparseAllocationBuilder()
971                                         .addMemoryBind()
972                                         .build(vk, getDevice(), getAllocator(), referenceBufferCreateInfo, minChunkSize));
973
974                                 numMaxChunks = deMaxu32(static_cast<deUint32>(m_context.getDeviceProperties().limits.maxUniformBufferRange / minAllocation->resourceSize), 1u);
975                         }
976
977                         if (numMaxChunks < 4)
978                         {
979                                 sparseAllocation = SparseAllocationBuilder()
980                                         .addMemoryBind()
981                                         .build(vk, getDevice(), getAllocator(), referenceBufferCreateInfo, minChunkSize);
982                         }
983                         else
984                         {
985                                 // Try to use a non-trivial memory allocation scheme to make it different from a non-sparse binding
986                                 SparseAllocationBuilder builder;
987                                 builder.addMemoryBind();
988
989                                 if (m_residency)
990                                         builder.addResourceHole();
991
992                                 builder
993                                         .addMemoryAllocation()
994                                         .addMemoryHole()
995                                         .addMemoryBind();
996
997                                 if (m_aliased)
998                                         builder.addAliasedMemoryBind(0u, 0u);
999
1000                                 sparseAllocation = builder.build(vk, getDevice(), getAllocator(), referenceBufferCreateInfo, minChunkSize);
1001                                 DE_ASSERT(sparseAllocation->resourceSize <= m_context.getDeviceProperties().limits.maxUniformBufferRange);
1002                         }
1003
1004                         // Create the buffer
1005                         referenceBufferCreateInfo.size  = sparseAllocation->resourceSize;
1006                         sparseBuffer                                    = makeBuffer(vk, getDevice(), referenceBufferCreateInfo);
1007                         bindSparseBuffer(vk, getDevice(), m_sparseQueue.queueHandle, *sparseBuffer, *sparseAllocation);
1008
1009                         if (m_aliased)
1010                         {
1011                                 sparseBufferAliased = makeBuffer(vk, getDevice(), referenceBufferCreateInfo);
1012                                 bindSparseBuffer(vk, getDevice(), m_sparseQueue.queueHandle, *sparseBufferAliased, *sparseAllocation);
1013                         }
1014                 }
1015
1016                 // Set uniform data
1017                 {
1018                         const bool                                      hasAliasedChunk         = (m_aliased && sparseAllocation->memoryBinds.size() > 1u);
1019                         const VkDeviceSize                      chunkSize                       = sparseAllocation->resourceSize / sparseAllocation->numResourceChunks;
1020                         const VkDeviceSize                      stagingBufferSize       = sparseAllocation->resourceSize - (hasAliasedChunk ? chunkSize : 0);
1021                         const deUint32                          numBufferEntries        = static_cast<deUint32>(stagingBufferSize / sizeof(IVec4));
1022
1023                         const Unique<VkBuffer>          stagingBuffer           (makeBuffer(vk, getDevice(), makeBufferCreateInfo(stagingBufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT)));
1024                         const UniquePtr<Allocation>     stagingBufferAlloc      (bindBuffer(vk, getDevice(), getAllocator(), *stagingBuffer, MemoryRequirement::HostVisible));
1025
1026                         {
1027                                 // If aliased chunk is used, the staging buffer is smaller than the sparse buffer and we don't overwrite the last chunk
1028                                 IVec4* const pData = static_cast<IVec4*>(stagingBufferAlloc->getHostPtr());
1029                                 for (deUint32 i = 0; i < numBufferEntries; ++i)
1030                                         pData[i] = IVec4(3*i ^ 127, 0, 0, 0);
1031
1032                                 flushMappedMemoryRange(vk, getDevice(), stagingBufferAlloc->getMemory(), stagingBufferAlloc->getOffset(), stagingBufferSize);
1033
1034                                 const VkBufferCopy copyRegion =
1035                                 {
1036                                         0ull,                                           // VkDeviceSize    srcOffset;
1037                                         0ull,                                           // VkDeviceSize    dstOffset;
1038                                         stagingBufferSize,                      // VkDeviceSize    size;
1039                                 };
1040
1041                                 const Unique<VkCommandPool>             cmdPool         (makeCommandPool(vk, getDevice(), m_universalQueue.queueFamilyIndex));
1042                                 const Unique<VkCommandBuffer>   cmdBuffer       (allocateCommandBuffer(vk, getDevice(), *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
1043
1044                                 beginCommandBuffer      (vk, *cmdBuffer);
1045                                 vk.cmdCopyBuffer        (*cmdBuffer, *stagingBuffer, *sparseBuffer, 1u, &copyRegion);
1046                                 endCommandBuffer        (vk, *cmdBuffer);
1047
1048                                 submitCommandsAndWait(vk, getDevice(), m_universalQueue.queueHandle, *cmdBuffer);
1049                                 // Once the fence is signaled, the write is also available to the aliasing buffer.
1050                         }
1051                 }
1052
1053                 // Make sure that we don't try to access a larger range than is allowed. This only applies to a single chunk case.
1054                 const deUint32 maxBufferRange = deMinu32(static_cast<deUint32>(sparseAllocation->resourceSize), m_context.getDeviceProperties().limits.maxUniformBufferRange);
1055
1056                 // Descriptor sets
1057                 {
1058                         m_descriptorSetLayout = DescriptorSetLayoutBuilder()
1059                                 .addSingleBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_FRAGMENT_BIT)
1060                                 .build(vk, getDevice());
1061
1062                         m_descriptorPool = DescriptorPoolBuilder()
1063                                 .addType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)
1064                                 .build(vk, getDevice(), VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u);
1065
1066                         m_descriptorSet = makeDescriptorSet(vk, getDevice(), *m_descriptorPool, *m_descriptorSetLayout);
1067
1068                         const VkBuffer                                  buffer                          = (m_aliased ? *sparseBufferAliased : *sparseBuffer);
1069                         const VkDescriptorBufferInfo    sparseBufferInfo        = makeDescriptorBufferInfo(buffer, 0ull, maxBufferRange);
1070
1071                         DescriptorSetUpdateBuilder()
1072                                 .writeSingle(*m_descriptorSet, DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &sparseBufferInfo)
1073                                 .update(vk, getDevice());
1074                 }
1075
1076                 // Vertex data
1077                 {
1078                         const Vec4 vertexData[] =
1079                         {
1080                                 Vec4(-1.0f, -1.0f, 0.0f, 1.0f),
1081                                 Vec4(-1.0f,  1.0f, 0.0f, 1.0f),
1082                                 Vec4( 1.0f, -1.0f, 0.0f, 1.0f),
1083                                 Vec4( 1.0f,  1.0f, 0.0f, 1.0f),
1084                         };
1085
1086                         const VkDeviceSize      vertexBufferSize        = sizeof(vertexData);
1087
1088                         m_vertexBuffer          = makeBuffer(vk, getDevice(), makeBufferCreateInfo(vertexBufferSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT));
1089                         m_vertexBufferAlloc     = bindBuffer(vk, getDevice(), getAllocator(), *m_vertexBuffer, MemoryRequirement::HostVisible);
1090
1091                         deMemcpy(m_vertexBufferAlloc->getHostPtr(), &vertexData[0], vertexBufferSize);
1092                         flushMappedMemoryRange(vk, getDevice(), m_vertexBufferAlloc->getMemory(), m_vertexBufferAlloc->getOffset(), vertexBufferSize);
1093                 }
1094
1095                 // Draw
1096                 {
1097                         std::vector<deInt32> specializationData;
1098                         {
1099                                 const deUint32  numBufferEntries        = maxBufferRange / static_cast<deUint32>(sizeof(IVec4));
1100                                 const deUint32  numEntriesPerChunk      = numBufferEntries / sparseAllocation->numResourceChunks;
1101
1102                                 specializationData.push_back(numBufferEntries);
1103                                 specializationData.push_back(numEntriesPerChunk);
1104                         }
1105
1106                         const VkSpecializationMapEntry  specMapEntries[] =
1107                         {
1108                                 {
1109                                         1u,                                     // uint32_t    constantID;
1110                                         0u,                                     // uint32_t    offset;
1111                                         sizeof(deInt32),        // size_t      size;
1112                                 },
1113                                 {
1114                                         2u,                                     // uint32_t    constantID;
1115                                         sizeof(deInt32),        // uint32_t    offset;
1116                                         sizeof(deInt32),        // size_t      size;
1117                                 },
1118                         };
1119
1120                         const VkSpecializationInfo specInfo =
1121                         {
1122                                 DE_LENGTH_OF_ARRAY(specMapEntries),             // uint32_t                           mapEntryCount;
1123                                 specMapEntries,                                                 // const VkSpecializationMapEntry*    pMapEntries;
1124                                 sizeInBytes(specializationData),                // size_t                             dataSize;
1125                                 getDataOrNullptr(specializationData),   // const void*                        pData;
1126                         };
1127
1128                         Renderer::SpecializationMap     specMap;
1129                         specMap[VK_SHADER_STAGE_FRAGMENT_BIT] = &specInfo;
1130
1131                         draw(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, *m_descriptorSetLayout, specMap);
1132                 }
1133
1134                 return verifyDrawResult();
1135         }
1136
1137 private:
1138         Move<VkBuffer>                                  m_vertexBuffer;
1139         MovePtr<Allocation>                             m_vertexBufferAlloc;
1140
1141         Move<VkDescriptorSetLayout>             m_descriptorSetLayout;
1142         Move<VkDescriptorPool>                  m_descriptorPool;
1143         Move<VkDescriptorSet>                   m_descriptorSet;
1144 };
1145
1146 void initProgramsDrawGrid (vk::SourceCollections& programCollection, const TestFlags flags)
1147 {
1148         DE_UNREF(flags);
1149
1150         // Vertex shader
1151         {
1152                 std::ostringstream src;
1153                 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
1154                         << "\n"
1155                         << "layout(location = 0) in  vec4 in_position;\n"
1156                         << "layout(location = 0) out int  out_ndx;\n"
1157                         << "\n"
1158                         << "out gl_PerVertex {\n"
1159                         << "    vec4 gl_Position;\n"
1160                         << "};\n"
1161                         << "\n"
1162                         << "void main(void)\n"
1163                         << "{\n"
1164                         << "    gl_Position = in_position;\n"
1165                         << "    out_ndx     = gl_VertexIndex;\n"
1166                         << "}\n";
1167
1168                 programCollection.glslSources.add("vert") << glu::VertexSource(src.str());
1169         }
1170
1171         // Fragment shader
1172         {
1173                 std::ostringstream src;
1174                 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
1175                         << "\n"
1176                         << "layout(location = 0) flat in  int  in_ndx;\n"
1177                         << "layout(location = 0)      out vec4 o_color;\n"
1178                         << "\n"
1179                         << "void main(void)\n"
1180                         << "{\n"
1181                         << "    if (in_ndx % 2 == 0)\n"
1182                         << "        o_color = vec4(vec3(1.0), 1.0);\n"
1183                         << "    else\n"
1184                         << "        o_color = vec4(vec3(0.75), 1.0);\n"
1185                         << "}\n";
1186
1187                 programCollection.glslSources.add("frag") << glu::FragmentSource(src.str());
1188         }
1189 }
1190
1191 //! Generate vertex positions for a grid of tiles composed of two triangles each (6 vertices)
1192 void generateGrid (void* pRawData, const float step, const float ox, const float oy, const deUint32 numX, const deUint32 numY, const float z = 0.0f)
1193 {
1194         typedef Vec4 (*TilePtr)[6];
1195
1196         TilePtr const pData = static_cast<TilePtr>(pRawData);
1197         {
1198                 for (deUint32 iy = 0; iy < numY; ++iy)
1199                 for (deUint32 ix = 0; ix < numX; ++ix)
1200                 {
1201                         const deUint32  ndx     = ix + numX * iy;
1202                         const float             x       = ox + step * static_cast<float>(ix);
1203                         const float             y       = oy + step * static_cast<float>(iy);
1204
1205                         pData[ndx][0] = Vec4(x + step,  y,                      z, 1.0f);
1206                         pData[ndx][1] = Vec4(x,                 y,                      z, 1.0f);
1207                         pData[ndx][2] = Vec4(x,                 y + step,       z, 1.0f);
1208
1209                         pData[ndx][3] = Vec4(x,                 y + step,       z, 1.0f);
1210                         pData[ndx][4] = Vec4(x + step,  y + step,       z, 1.0f);
1211                         pData[ndx][5] = Vec4(x + step,  y,                      z, 1.0f);
1212                 }
1213         }
1214 }
1215
1216 //! Base test for a sparse buffer backing a vertex/index buffer
1217 class DrawGridTestInstance : public SparseBufferTestInstance
1218 {
1219 public:
1220         DrawGridTestInstance (Context& context, const TestFlags flags, const VkBufferUsageFlags usage, const VkDeviceSize minChunkSize)
1221                 : SparseBufferTestInstance      (context, flags)
1222         {
1223                 const DeviceInterface&  vk                                                      = getDeviceInterface();
1224                 VkBufferCreateInfo              referenceBufferCreateInfo       = getSparseBufferCreateInfo(usage);
1225
1226                 {
1227                         // Allocate two chunks, each covering half of the viewport
1228                         SparseAllocationBuilder builder;
1229                         builder.addMemoryBind();
1230
1231                         if (m_residency)
1232                                 builder.addResourceHole();
1233
1234                         builder
1235                                 .addMemoryAllocation()
1236                                 .addMemoryHole()
1237                                 .addMemoryBind();
1238
1239                         if (m_aliased)
1240                                 builder.addAliasedMemoryBind(0u, 0u);
1241
1242                         m_sparseAllocation      = builder.build(vk, getDevice(), getAllocator(), referenceBufferCreateInfo, minChunkSize);
1243                 }
1244
1245                 // Create the buffer
1246                 referenceBufferCreateInfo.size  = m_sparseAllocation->resourceSize;
1247                 m_sparseBuffer                                  = makeBuffer(vk, getDevice(), referenceBufferCreateInfo);
1248
1249                 // Bind the memory
1250                 bindSparseBuffer(vk, getDevice(), m_sparseQueue.queueHandle, *m_sparseBuffer, *m_sparseAllocation);
1251
1252                 m_perDrawBufferOffset   = m_sparseAllocation->resourceSize / m_sparseAllocation->numResourceChunks;
1253                 m_stagingBufferSize             = 2 * m_perDrawBufferOffset;
1254                 m_stagingBuffer                 = makeBuffer(vk, getDevice(), makeBufferCreateInfo(m_stagingBufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT));
1255                 m_stagingBufferAlloc    = bindBuffer(vk, getDevice(), getAllocator(), *m_stagingBuffer, MemoryRequirement::HostVisible);
1256         }
1257
1258         tcu::TestStatus iterate (void)
1259         {
1260                 initializeBuffers();
1261
1262                 const DeviceInterface&  vk      = getDeviceInterface();
1263
1264                 // Upload to the sparse buffer
1265                 {
1266                         flushMappedMemoryRange(vk, getDevice(), m_stagingBufferAlloc->getMemory(), m_stagingBufferAlloc->getOffset(), m_stagingBufferSize);
1267
1268                         VkDeviceSize    firstChunkOffset        = 0ull;
1269                         VkDeviceSize    secondChunkOffset       = m_perDrawBufferOffset;
1270
1271                         if (m_residency)
1272                                 secondChunkOffset += m_perDrawBufferOffset;
1273
1274                         if (m_aliased)
1275                                 firstChunkOffset = secondChunkOffset + m_perDrawBufferOffset;
1276
1277                         const VkBufferCopy copyRegions[] =
1278                         {
1279                                 {
1280                                         0ull,                                           // VkDeviceSize    srcOffset;
1281                                         firstChunkOffset,                       // VkDeviceSize    dstOffset;
1282                                         m_perDrawBufferOffset,          // VkDeviceSize    size;
1283                                 },
1284                                 {
1285                                         m_perDrawBufferOffset,          // VkDeviceSize    srcOffset;
1286                                         secondChunkOffset,                      // VkDeviceSize    dstOffset;
1287                                         m_perDrawBufferOffset,          // VkDeviceSize    size;
1288                                 },
1289                         };
1290
1291                         const Unique<VkCommandPool>             cmdPool         (makeCommandPool(vk, getDevice(), m_universalQueue.queueFamilyIndex));
1292                         const Unique<VkCommandBuffer>   cmdBuffer       (allocateCommandBuffer(vk, getDevice(), *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
1293
1294                         beginCommandBuffer      (vk, *cmdBuffer);
1295                         vk.cmdCopyBuffer        (*cmdBuffer, *m_stagingBuffer, *m_sparseBuffer, DE_LENGTH_OF_ARRAY(copyRegions), copyRegions);
1296                         endCommandBuffer        (vk, *cmdBuffer);
1297
1298                         submitCommandsAndWait(vk, getDevice(), m_universalQueue.queueHandle, *cmdBuffer);
1299                 }
1300
1301                 draw(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
1302
1303                 return verifyDrawResult();
1304         }
1305
1306 protected:
1307         virtual void                            initializeBuffers               (void) = 0;
1308
1309         VkDeviceSize                            m_perDrawBufferOffset;
1310
1311         VkDeviceSize                            m_stagingBufferSize;
1312         Move<VkBuffer>                          m_stagingBuffer;
1313         MovePtr<Allocation>                     m_stagingBufferAlloc;
1314
1315         MovePtr<SparseAllocation>       m_sparseAllocation;
1316         Move<VkBuffer>                          m_sparseBuffer;
1317 };
1318
1319 //! Sparse buffer backing a vertex input buffer
1320 class VertexBufferTestInstance : public DrawGridTestInstance
1321 {
1322 public:
1323         VertexBufferTestInstance (Context& context, const TestFlags flags)
1324                 : DrawGridTestInstance  (context,
1325                                                                  flags,
1326                                                                  VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
1327                                                                  GRID_SIZE * GRID_SIZE * 6 * sizeof(Vec4))
1328         {
1329         }
1330
1331         void rendererDraw (const VkPipelineLayout pipelineLayout, const VkCommandBuffer cmdBuffer) const
1332         {
1333                 DE_UNREF(pipelineLayout);
1334
1335                 m_context.getTestContext().getLog()
1336                         << tcu::TestLog::Message << "Drawing a grid of triangles backed by a sparse vertex buffer. There should be no red pixels visible." << tcu::TestLog::EndMessage;
1337
1338                 const DeviceInterface&  vk                              = getDeviceInterface();
1339                 const deUint32                  vertexCount             = 6 * (GRID_SIZE * GRID_SIZE) / 2;
1340                 VkDeviceSize                    vertexOffset    = 0ull;
1341
1342                 vk.cmdBindVertexBuffers (cmdBuffer, 0u, 1u, &m_sparseBuffer.get(), &vertexOffset);
1343                 vk.cmdDraw                              (cmdBuffer, vertexCount, 1u, 0u, 0u);
1344
1345                 vertexOffset += m_perDrawBufferOffset * (m_residency ? 2 : 1);
1346
1347                 vk.cmdBindVertexBuffers (cmdBuffer, 0u, 1u, &m_sparseBuffer.get(), &vertexOffset);
1348                 vk.cmdDraw                              (cmdBuffer, vertexCount, 1u, 0u, 0u);
1349         }
1350
1351         void initializeBuffers (void)
1352         {
1353                 deUint8*        pData   = static_cast<deUint8*>(m_stagingBufferAlloc->getHostPtr());
1354                 const float     step    = 2.0f / static_cast<float>(GRID_SIZE);
1355
1356                 // Prepare data for two draw calls
1357                 generateGrid(pData,                                                     step, -1.0f, -1.0f, GRID_SIZE, GRID_SIZE/2);
1358                 generateGrid(pData + m_perDrawBufferOffset,     step, -1.0f,  0.0f, GRID_SIZE, GRID_SIZE/2);
1359         }
1360 };
1361
1362 //! Sparse buffer backing an index buffer
1363 class IndexBufferTestInstance : public DrawGridTestInstance
1364 {
1365 public:
1366         IndexBufferTestInstance (Context& context, const TestFlags flags)
1367                 : DrawGridTestInstance  (context,
1368                                                                  flags,
1369                                                                  VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
1370                                                                  GRID_SIZE * GRID_SIZE * 6 * sizeof(deUint32))
1371                 , m_halfVertexCount             (6 * (GRID_SIZE * GRID_SIZE) / 2)
1372         {
1373         }
1374
1375         void rendererDraw (const VkPipelineLayout pipelineLayout, const VkCommandBuffer cmdBuffer) const
1376         {
1377                 DE_UNREF(pipelineLayout);
1378
1379                 m_context.getTestContext().getLog()
1380                         << tcu::TestLog::Message << "Drawing a grid of triangles from a sparse index buffer. There should be no red pixels visible." << tcu::TestLog::EndMessage;
1381
1382                 const DeviceInterface&  vk                              = getDeviceInterface();
1383                 const VkDeviceSize              vertexOffset    = 0ull;
1384                 VkDeviceSize                    indexOffset             = 0ull;
1385
1386                 vk.cmdBindVertexBuffers (cmdBuffer, 0u, 1u, &m_vertexBuffer.get(), &vertexOffset);
1387
1388                 vk.cmdBindIndexBuffer   (cmdBuffer, *m_sparseBuffer, indexOffset, VK_INDEX_TYPE_UINT32);
1389                 vk.cmdDrawIndexed               (cmdBuffer, m_halfVertexCount, 1u, 0u, 0, 0u);
1390
1391                 indexOffset += m_perDrawBufferOffset * (m_residency ? 2 : 1);
1392
1393                 vk.cmdBindIndexBuffer   (cmdBuffer, *m_sparseBuffer, indexOffset, VK_INDEX_TYPE_UINT32);
1394                 vk.cmdDrawIndexed               (cmdBuffer, m_halfVertexCount, 1u, 0u, 0, 0u);
1395         }
1396
1397         void initializeBuffers (void)
1398         {
1399                 // Vertex buffer
1400                 const DeviceInterface&  vk                                      = getDeviceInterface();
1401                 const VkDeviceSize              vertexBufferSize        = 2 * m_halfVertexCount * sizeof(Vec4);
1402                                                                 m_vertexBuffer          = makeBuffer(vk, getDevice(), makeBufferCreateInfo(vertexBufferSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT));
1403                                                                 m_vertexBufferAlloc     = bindBuffer(vk, getDevice(), getAllocator(), *m_vertexBuffer, MemoryRequirement::HostVisible);
1404
1405                 {
1406                         const float     step = 2.0f / static_cast<float>(GRID_SIZE);
1407
1408                         generateGrid(m_vertexBufferAlloc->getHostPtr(), step, -1.0f, -1.0f, GRID_SIZE, GRID_SIZE);
1409
1410                         flushMappedMemoryRange(vk, getDevice(), m_vertexBufferAlloc->getMemory(), m_vertexBufferAlloc->getOffset(), vertexBufferSize);
1411                 }
1412
1413                 // Sparse index buffer
1414                 for (deUint32 chunkNdx = 0u; chunkNdx < 2; ++chunkNdx)
1415                 {
1416                         deUint8* const  pData           = static_cast<deUint8*>(m_stagingBufferAlloc->getHostPtr()) + chunkNdx * m_perDrawBufferOffset;
1417                         deUint32* const pIndexData      = reinterpret_cast<deUint32*>(pData);
1418                         const deUint32  ndxBase         = chunkNdx * m_halfVertexCount;
1419
1420                         for (deUint32 i = 0u; i < m_halfVertexCount; ++i)
1421                                 pIndexData[i] = ndxBase + i;
1422                 }
1423         }
1424
1425 private:
1426         const deUint32                  m_halfVertexCount;
1427         Move<VkBuffer>                  m_vertexBuffer;
1428         MovePtr<Allocation>             m_vertexBufferAlloc;
1429 };
1430
1431 //! Draw from a sparse indirect buffer
1432 class IndirectBufferTestInstance : public DrawGridTestInstance
1433 {
1434 public:
1435         IndirectBufferTestInstance (Context& context, const TestFlags flags)
1436                 : DrawGridTestInstance  (context,
1437                                                                  flags,
1438                                                                  VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT,
1439                                                                  sizeof(VkDrawIndirectCommand))
1440         {
1441         }
1442
1443         void rendererDraw (const VkPipelineLayout pipelineLayout, const VkCommandBuffer cmdBuffer) const
1444         {
1445                 DE_UNREF(pipelineLayout);
1446
1447                 m_context.getTestContext().getLog()
1448                         << tcu::TestLog::Message << "Drawing two triangles covering the whole viewport. There should be no red pixels visible." << tcu::TestLog::EndMessage;
1449
1450                 const DeviceInterface&  vk                              = getDeviceInterface();
1451                 const VkDeviceSize              vertexOffset    = 0ull;
1452                 VkDeviceSize                    indirectOffset  = 0ull;
1453
1454                 vk.cmdBindVertexBuffers (cmdBuffer, 0u, 1u, &m_vertexBuffer.get(), &vertexOffset);
1455                 vk.cmdDrawIndirect              (cmdBuffer, *m_sparseBuffer, indirectOffset, 1u, 0u);
1456
1457                 indirectOffset += m_perDrawBufferOffset * (m_residency ? 2 : 1);
1458
1459                 vk.cmdDrawIndirect              (cmdBuffer, *m_sparseBuffer, indirectOffset, 1u, 0u);
1460         }
1461
1462         void initializeBuffers (void)
1463         {
1464                 // Vertex buffer
1465                 const DeviceInterface&  vk                                      = getDeviceInterface();
1466                 const VkDeviceSize              vertexBufferSize        = 2 * 3 * sizeof(Vec4);
1467                                                                 m_vertexBuffer          = makeBuffer(vk, getDevice(), makeBufferCreateInfo(vertexBufferSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT));
1468                                                                 m_vertexBufferAlloc     = bindBuffer(vk, getDevice(), getAllocator(), *m_vertexBuffer, MemoryRequirement::HostVisible);
1469
1470                 {
1471                         generateGrid(m_vertexBufferAlloc->getHostPtr(), 2.0f, -1.0f, -1.0f, 1, 1);
1472                         flushMappedMemoryRange(vk, getDevice(), m_vertexBufferAlloc->getMemory(), m_vertexBufferAlloc->getOffset(), vertexBufferSize);
1473                 }
1474
1475                 // Indirect buffer
1476                 for (deUint32 chunkNdx = 0u; chunkNdx < 2; ++chunkNdx)
1477                 {
1478                         deUint8* const                                  pData           = static_cast<deUint8*>(m_stagingBufferAlloc->getHostPtr()) + chunkNdx * m_perDrawBufferOffset;
1479                         VkDrawIndirectCommand* const    pCmdData        = reinterpret_cast<VkDrawIndirectCommand*>(pData);
1480
1481                         pCmdData->firstVertex   = 3u * chunkNdx;
1482                         pCmdData->firstInstance = 0u;
1483                         pCmdData->vertexCount   = 3u;
1484                         pCmdData->instanceCount = 1u;
1485                 }
1486         }
1487
1488 private:
1489         Move<VkBuffer>                  m_vertexBuffer;
1490         MovePtr<Allocation>             m_vertexBufferAlloc;
1491 };
1492
1493 //! Similar to the class in vktTestCaseUtil.hpp, but uses Arg0 directly rather than through a InstanceFunction1
1494 template<typename Arg0>
1495 class FunctionProgramsSimple1
1496 {
1497 public:
1498         typedef void    (*Function)                             (vk::SourceCollections& dst, Arg0 arg0);
1499                                         FunctionProgramsSimple1 (Function func) : m_func(func)                                                  {}
1500         void                    init                                    (vk::SourceCollections& dst, const Arg0& arg0) const    { m_func(dst, arg0); }
1501
1502 private:
1503         const Function  m_func;
1504 };
1505
1506 //! Convenience function to create a TestCase based on a freestanding initPrograms and a TestInstance implementation
1507 template<typename TestInstanceT, typename Arg0>
1508 TestCase* createTestInstanceWithPrograms (tcu::TestContext&                                                                     testCtx,
1509                                                                                   const std::string&                                                            name,
1510                                                                                   const std::string&                                                            desc,
1511                                                                                   typename FunctionProgramsSimple1<Arg0>::Function      initPrograms,
1512                                                                                   Arg0                                                                                          arg0)
1513 {
1514         return new InstanceFactory1<TestInstanceT, Arg0, FunctionProgramsSimple1<Arg0> >(
1515                 testCtx, tcu::NODETYPE_SELF_VALIDATE, name, desc, FunctionProgramsSimple1<Arg0>(initPrograms), arg0);
1516 }
1517
1518 void populateTestGroup (tcu::TestCaseGroup* parentGroup)
1519 {
1520         const struct
1521         {
1522                 std::string             name;
1523                 TestFlags               flags;
1524         } groups[] =
1525         {
1526                 { "sparse_binding",                                                     0u                                                                                                              },
1527                 { "sparse_binding_aliased",                                     TEST_FLAG_ALIASED,                                                                              },
1528                 { "sparse_residency",                                           TEST_FLAG_RESIDENCY,                                                                    },
1529                 { "sparse_residency_aliased",                           TEST_FLAG_RESIDENCY | TEST_FLAG_ALIASED,                                },
1530                 { "sparse_residency_non_resident_strict",       TEST_FLAG_RESIDENCY | TEST_FLAG_NON_RESIDENT_STRICT,    },
1531         };
1532
1533         const int numGroupsIncludingNonResidentStrict   = DE_LENGTH_OF_ARRAY(groups);
1534         const int numGroupsDefaultList                                  = numGroupsIncludingNonResidentStrict - 1;
1535
1536         // Transfer
1537         {
1538                 MovePtr<tcu::TestCaseGroup> group(new tcu::TestCaseGroup(parentGroup->getTestContext(), "transfer", ""));
1539                 {
1540                         MovePtr<tcu::TestCaseGroup> subGroup(new tcu::TestCaseGroup(parentGroup->getTestContext(), "sparse_binding", ""));
1541                         addBufferSparseBindingTests(subGroup.get());
1542                         group->addChild(subGroup.release());
1543                 }
1544                 parentGroup->addChild(group.release());
1545         }
1546
1547         // SSBO
1548         {
1549                 MovePtr<tcu::TestCaseGroup> group(new tcu::TestCaseGroup(parentGroup->getTestContext(), "ssbo", ""));
1550                 {
1551                         MovePtr<tcu::TestCaseGroup> subGroup(new tcu::TestCaseGroup(parentGroup->getTestContext(), "sparse_binding_aliased", ""));
1552                         addBufferSparseMemoryAliasingTests(subGroup.get());
1553                         group->addChild(subGroup.release());
1554                 }
1555                 {
1556                         MovePtr<tcu::TestCaseGroup> subGroup(new tcu::TestCaseGroup(parentGroup->getTestContext(), "sparse_residency", ""));
1557                         addBufferSparseResidencyTests(subGroup.get());
1558                         group->addChild(subGroup.release());
1559                 }
1560                 parentGroup->addChild(group.release());
1561         }
1562
1563         // UBO
1564         {
1565                 MovePtr<tcu::TestCaseGroup> group(new tcu::TestCaseGroup(parentGroup->getTestContext(), "ubo", ""));
1566
1567                 for (int groupNdx = 0u; groupNdx < numGroupsIncludingNonResidentStrict; ++groupNdx)
1568                         group->addChild(createTestInstanceWithPrograms<UBOTestInstance>(group->getTestContext(), groups[groupNdx].name.c_str(), "", initProgramsDrawWithUBO, groups[groupNdx].flags));
1569
1570                 parentGroup->addChild(group.release());
1571         }
1572
1573         // Vertex buffer
1574         {
1575                 MovePtr<tcu::TestCaseGroup> group(new tcu::TestCaseGroup(parentGroup->getTestContext(), "vertex_buffer", ""));
1576
1577                 for (int groupNdx = 0u; groupNdx < numGroupsDefaultList; ++groupNdx)
1578                         group->addChild(createTestInstanceWithPrograms<VertexBufferTestInstance>(group->getTestContext(), groups[groupNdx].name.c_str(), "", initProgramsDrawGrid, groups[groupNdx].flags));
1579
1580                 parentGroup->addChild(group.release());
1581         }
1582
1583         // Index buffer
1584         {
1585                 MovePtr<tcu::TestCaseGroup> group(new tcu::TestCaseGroup(parentGroup->getTestContext(), "index_buffer", ""));
1586
1587                 for (int groupNdx = 0u; groupNdx < numGroupsDefaultList; ++groupNdx)
1588                         group->addChild(createTestInstanceWithPrograms<IndexBufferTestInstance>(group->getTestContext(), groups[groupNdx].name.c_str(), "", initProgramsDrawGrid, groups[groupNdx].flags));
1589
1590                 parentGroup->addChild(group.release());
1591         }
1592
1593         // Indirect buffer
1594         {
1595                 MovePtr<tcu::TestCaseGroup> group(new tcu::TestCaseGroup(parentGroup->getTestContext(), "indirect_buffer", ""));
1596
1597                 for (int groupNdx = 0u; groupNdx < numGroupsDefaultList; ++groupNdx)
1598                         group->addChild(createTestInstanceWithPrograms<IndirectBufferTestInstance>(group->getTestContext(), groups[groupNdx].name.c_str(), "", initProgramsDrawGrid, groups[groupNdx].flags));
1599
1600                 parentGroup->addChild(group.release());
1601         }
1602 }
1603
1604 } // anonymous ns
1605
1606 tcu::TestCaseGroup* createSparseBufferTests (tcu::TestContext& testCtx)
1607 {
1608         return createTestGroup(testCtx, "buffer", "Sparse buffer usage tests", populateTestGroup);
1609 }
1610
1611 } // sparse
1612 } // vkt