Merge branch 'jekstrand_renderpass_transfer_bit_fix' into 'master'
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / api / vktApiBufferTests.cpp
1 /*------------------------------------------------------------------------
2  *  Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 The Khronos Group Inc.
6  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and/or associated documentation files (the
10  * "Materials"), to deal in the Materials without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Materials, and to
13  * permit persons to whom the Materials are furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice(s) and this permission notice shall be included
17  * in all copies or substantial portions of the Materials.
18  *
19  * The Materials are Confidential Information as defined by the
20  * Khronos Membership Agreement until designated non-confidential by Khronos,
21  * at which point this condition clause shall be removed.
22  *
23  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
27  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
30  *
31  *//*!
32  * \file
33  * \brief Vulkan Buffers Tests
34  *//*--------------------------------------------------------------------*/
35
36 #include "vktApiBufferTests.hpp"
37
38 #include "deStringUtil.hpp"
39 #include "gluVarType.hpp"
40 #include "tcuTestLog.hpp"
41 #include "vkPrograms.hpp"
42 #include "vkQueryUtil.hpp"
43 #include "vktTestCase.hpp"
44
45 namespace vkt
46 {
47
48 using namespace vk;
49
50 namespace api
51 {
52
53 namespace
54 {
55
56 struct BufferCaseParameters
57 {
58         VkBufferUsageFlags      usage;
59         VkBufferCreateFlags     flags;
60         VkSharingMode           sharingMode;
61 };
62
63 class BufferTestInstance : public TestInstance
64 {
65 public:
66                                                                 BufferTestInstance                      (Context&                               ctx,
67                                                                                                                          BufferCaseParameters   testCase)
68                                                                         : TestInstance  (ctx)
69                                                                         , m_testCase    (testCase)
70                                                                 {}
71         virtual tcu::TestStatus         iterate                                         (void);
72         tcu::TestStatus                         bufferCreateAndAllocTest        (VkDeviceSize           size);
73
74 private:
75         BufferCaseParameters            m_testCase;
76 };
77
78 class BuffersTestCase : public TestCase
79 {
80 public:
81                                                         BuffersTestCase         (tcu::TestContext&              testCtx,
82                                                                                                  const std::string&             name,
83                                                                                                  const std::string&             description,
84                                                                                                  BufferCaseParameters   testCase)
85                                                                 : TestCase(testCtx, name, description)
86                                                                 , m_testCase(testCase)
87                                                         {}
88
89         virtual                                 ~BuffersTestCase        (void) {}
90         virtual TestInstance*   createInstance          (Context&                               ctx) const
91                                                         {
92                                                                 tcu::TestLog& log       = m_testCtx.getLog();
93                                                                 log << tcu::TestLog::Message << getBufferUsageFlagsStr(m_testCase.usage) << tcu::TestLog::EndMessage;
94                                                                 return new BufferTestInstance(ctx, m_testCase);
95                                                         }
96
97 private:
98         BufferCaseParameters            m_testCase;
99 };
100
101  tcu::TestStatus BufferTestInstance::bufferCreateAndAllocTest (VkDeviceSize size)
102 {
103         const VkDevice                  vkDevice                        = m_context.getDevice();
104         const DeviceInterface&  vk                                      = m_context.getDeviceInterface();
105         VkBuffer                                testBuffer;
106         VkMemoryRequirements    memReqs;
107         VkDeviceMemory                  memory;
108         const deUint32                  queueFamilyIndex        = m_context.getUniversalQueueFamilyIndex();
109
110         // Create buffer
111         {
112                 const VkBufferCreateInfo                bufferParams            =
113                 {
114                         VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
115                         DE_NULL,
116                         m_testCase.flags,
117                         size,
118                         m_testCase.usage,
119                         m_testCase.sharingMode,
120                         1u,                                                                             //      deUint32                        queueFamilyCount;
121                         &queueFamilyIndex,
122                 };
123
124                 if (vk.createBuffer(vkDevice, &bufferParams, (const VkAllocationCallbacks*)DE_NULL, &testBuffer) != VK_SUCCESS)
125                         return tcu::TestStatus::fail("Buffer creation failed! (requested memory size: " + de::toString(size) + ")");
126
127                 vk.getBufferMemoryRequirements(vkDevice, testBuffer, &memReqs);
128
129                 if (size > memReqs.size)
130                 {
131                         std::ostringstream errorMsg;
132                         errorMsg << "Requied memory size (" << memReqs.size << " bytes) smaller than the buffer's size (" << size << " bytes)!";
133                         return tcu::TestStatus::fail(errorMsg.str());
134                 }
135         }
136
137         // Allocate and bind memory
138         {
139                 const VkMemoryAllocateInfo memAlloc =
140                 {
141                         VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
142                         NULL,
143                         memReqs.size,
144                         0                                                                               //      deUint32                memoryTypeIndex
145                 };
146
147                 if (vk.allocateMemory(vkDevice, &memAlloc, (const VkAllocationCallbacks*)DE_NULL, &memory) != VK_SUCCESS)
148                         return tcu::TestStatus::fail("Alloc memory failed! (requested memory size: " + de::toString(size) + ")");
149
150
151                 if ((m_testCase.flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) ||
152                         (m_testCase.flags & VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT) ||
153                         (m_testCase.flags & VK_BUFFER_CREATE_SPARSE_ALIASED_BIT))
154                 {
155                         VkQueue queue                                                                                           = 0;
156
157                         vk.getDeviceQueue(vkDevice, queueFamilyIndex, 0, &queue);
158
159                         const VkSparseMemoryBind                        sparseMemoryBind                =
160                         {
161                                 0,                                                                              // VkDeviceSize                                                         resourceOffset;
162                                 memReqs.size,                                                   // VkDeviceSize                                                         size;
163                                 memory,                                                                 // VkDeviceMemory                                                       memory;
164                                 0,                                                                              // VkDeviceSize                                                         memoryOffset;
165                                 0                                                                               // VkSparseMemoryBindFlags                                      flags;
166                         };
167
168                         const VkSparseBufferMemoryBindInfo      sparseBufferMemoryBindInfo      =
169                         {
170                                 testBuffer,                                                             // VkBuffer                                                                     buffer;
171                                 1u,                                                                             // deUint32                                                                     bindCount;
172                                 &sparseMemoryBind                                               // const VkSparseMemoryBind*                            pBinds;
173                         };
174
175                         const VkBindSparseInfo                          bindSparseInfo                  =
176                         {
177                                 VK_STRUCTURE_TYPE_BIND_SPARSE_INFO,             // VkStructureType                                                      sType;
178                                 DE_NULL,                                                                // const void*                                                          pNext;
179                                 0,                                                                              // deUint32                                                                     waitSemaphoreCount;
180                                 DE_NULL,                                                                // const VkSemaphore*                                           pWaitSemaphores;
181                                 1u,                                                                             // deUint32                                                                     bufferBindCount;
182                                 &sparseBufferMemoryBindInfo,                    // const VkSparseBufferMemoryBindInfo*          pBufferBinds;
183                                 0,                                                                              // deUint32                                                                     imageOpaqueBindCount;
184                                 DE_NULL,                                                                // const VkSparseImageOpaqueMemoryBindInfo*     pImageOpaqueBinds;
185                                 0,                                                                              // deUint32                                                                     imageBindCount;
186                                 DE_NULL,                                                                // const VkSparseImageMemoryBindInfo*           pImageBinds;
187                                 0,                                                                              // deUint32                                                                     signalSemaphoreCount;
188                                 DE_NULL,                                                                // const VkSemaphore*                                           pSignalSemaphores;
189                         };
190
191                         if (vk.queueBindSparse(queue, 1, &bindSparseInfo, DE_NULL) != VK_SUCCESS)
192                                 return tcu::TestStatus::fail("Bind sparse buffer memory failed! (requested memory size: " + de::toString(size) + ")");
193                 } else
194                         if (vk.bindBufferMemory(vkDevice, testBuffer, memory, 0) != VK_SUCCESS)
195                                 return tcu::TestStatus::fail("Bind buffer memory failed! (requested memory size: " + de::toString(size) + ")");
196         }
197
198         vk.freeMemory(vkDevice, memory, (const VkAllocationCallbacks*)DE_NULL);
199         vk.destroyBuffer(vkDevice, testBuffer, (const VkAllocationCallbacks*)DE_NULL);
200
201         return tcu::TestStatus::pass("Buffer test");
202 }
203
204 tcu::TestStatus BufferTestInstance::iterate (void)
205 {
206         const VkPhysicalDeviceFeatures& physicalDeviceFeatures  = m_context.getDeviceFeatures();
207
208         if ((m_testCase.flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT ) && !physicalDeviceFeatures.sparseBinding)
209                 TCU_THROW(NotSupportedError, "Sparse bindings feature is not supported");
210
211         if ((m_testCase.flags & VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT ) && !physicalDeviceFeatures.sparseResidencyBuffer)
212                 TCU_THROW(NotSupportedError, "Sparse buffer residency feature is not supported");
213
214         if ((m_testCase.flags & VK_BUFFER_CREATE_SPARSE_ALIASED_BIT ) && !physicalDeviceFeatures.sparseResidencyAliased)
215                 TCU_THROW(NotSupportedError, "Sparse aliased residency feature is not supported");
216
217         const VkDeviceSize testSizes[] =
218         {
219                 1,
220                 1181,
221                 15991,
222                 16384
223         };
224         tcu::TestStatus                                 testStatus                      = tcu::TestStatus::pass("Buffer test");
225
226         for (int i = 0; i < DE_LENGTH_OF_ARRAY(testSizes); i++)
227         {
228                 if ((testStatus = bufferCreateAndAllocTest(testSizes[i])).getCode() != QP_TEST_RESULT_PASS)
229                         return testStatus;
230         }
231
232         if (m_testCase.usage & (VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
233         {
234                 const VkPhysicalDevice                                  vkPhysicalDevice        = m_context.getPhysicalDevice();
235                 const InstanceInterface&                                vkInstance                      = m_context.getInstanceInterface();
236                 const VkPhysicalDeviceMemoryProperties  memoryProperties        = getPhysicalDeviceMemoryProperties(vkInstance, vkPhysicalDevice);
237                 VkPhysicalDeviceProperties      props;
238
239                 vkInstance.getPhysicalDeviceProperties(vkPhysicalDevice, &props);
240
241                 const VkDeviceSize maxTestBufferSize = de::min((VkDeviceSize) props.limits.maxTexelBufferElements, memoryProperties.memoryHeaps[memoryProperties.memoryTypes[0].heapIndex].size / 16);
242
243                 testStatus = bufferCreateAndAllocTest(maxTestBufferSize);
244         }
245
246         return testStatus;
247 }
248
249 } // anonymous
250
251  tcu::TestCaseGroup* createBufferTests (tcu::TestContext& testCtx)
252 {
253         const VkBufferUsageFlags bufferUsageModes[] =
254         {
255                 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
256                 VK_BUFFER_USAGE_TRANSFER_DST_BIT,
257                 VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT,
258                 VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT,
259                 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
260                 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
261                 VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
262                 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
263                 VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT
264         };
265
266         const VkBufferCreateFlags bufferCreateFlags[] =
267         {
268                 VK_BUFFER_CREATE_SPARSE_BINDING_BIT,
269                 VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT,
270                 VK_BUFFER_CREATE_SPARSE_ALIASED_BIT
271         };
272
273         de::MovePtr<tcu::TestCaseGroup> buffersTests    (new tcu::TestCaseGroup(testCtx, "buffer", "Buffer Tests"));
274
275         deUint32        numberOfBufferUsageFlags                        = DE_LENGTH_OF_ARRAY(bufferUsageModes);
276         deUint32        numberOfBufferCreateFlags                       = DE_LENGTH_OF_ARRAY(bufferCreateFlags);
277         deUint32        maximumValueOfBufferUsageFlags          = (1 << (numberOfBufferUsageFlags - 1)) - 1;
278         deUint32        maximumValueOfBufferCreateFlags         = (1 << (numberOfBufferCreateFlags)) - 1;
279
280         for (deUint32 combinedBufferCreateFlags = 0; combinedBufferCreateFlags <= maximumValueOfBufferCreateFlags; combinedBufferCreateFlags++)
281         {
282                 for (deUint32 combinedBufferUsageFlags = 1; combinedBufferUsageFlags <= maximumValueOfBufferUsageFlags; combinedBufferUsageFlags++)
283                 {
284                         BufferCaseParameters    testParams =
285                         {
286                                 combinedBufferUsageFlags,
287                                 combinedBufferCreateFlags,
288                                 VK_SHARING_MODE_EXCLUSIVE
289                         };
290                         std::ostringstream      testName;
291                         std::ostringstream      testDescription;
292                         testName << "createBuffer_" << combinedBufferUsageFlags << "_" << combinedBufferCreateFlags;
293                         testDescription << "vkCreateBuffer test " << combinedBufferUsageFlags << " " << combinedBufferCreateFlags;
294                         buffersTests->addChild(new BuffersTestCase(testCtx, testName.str(), testDescription.str(), testParams));
295                 }
296         }
297
298         return buffersTests.release();
299 }
300
301 } // api
302 } // vk