dEQP-VK.renderpass: Set IMAGE_USAGE_TRANSFER_SRC_BIT when needed
[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 "vktTestCase.hpp"
43
44 namespace vkt
45 {
46
47 using namespace vk;
48
49 namespace api
50 {
51
52 namespace
53 {
54
55 struct BufferCaseParameters
56 {
57         VkBufferUsageFlags      usage;
58         VkBufferCreateFlags     flags;
59         VkSharingMode           sharingMode;
60 };
61
62 class BufferTestInstance : public TestInstance
63 {
64 public:
65                                                                 BufferTestInstance                      (Context&                               ctx,
66                                                                                                                          BufferCaseParameters   testCase)
67                                                                         : TestInstance  (ctx)
68                                                                         , m_testCase    (testCase)
69                                                                 {}
70         virtual tcu::TestStatus         iterate                                         (void);
71         tcu::TestStatus                         bufferCreateAndAllocTest        (VkDeviceSize           size);
72
73 private:
74         BufferCaseParameters            m_testCase;
75 };
76
77 class BuffersTestCase : public TestCase
78 {
79 public:
80                                                         BuffersTestCase         (tcu::TestContext&              testCtx,
81                                                                                                  const std::string&             name,
82                                                                                                  const std::string&             description,
83                                                                                                  BufferCaseParameters   testCase)
84                                                                 : TestCase(testCtx, name, description)
85                                                                 , m_testCase(testCase)
86                                                         {}
87
88         virtual                                 ~BuffersTestCase        (void) {}
89         virtual TestInstance*   createInstance          (Context&                               ctx) const
90                                                         {
91                                                                 tcu::TestLog& log       = m_testCtx.getLog();
92                                                                 log << tcu::TestLog::Message << getBufferUsageFlagsStr(m_testCase.usage) << tcu::TestLog::EndMessage;
93                                                                 return new BufferTestInstance(ctx, m_testCase);
94                                                         }
95
96 private:
97         BufferCaseParameters            m_testCase;
98 };
99
100  tcu::TestStatus BufferTestInstance::bufferCreateAndAllocTest (VkDeviceSize size)
101 {
102         const VkDevice                  vkDevice        = m_context.getDevice();
103         const DeviceInterface&  vk                      = m_context.getDeviceInterface();
104         VkBuffer                                testBuffer;
105         VkMemoryRequirements    memReqs;
106         VkDeviceMemory                  memory;
107
108         // Create buffer
109         {
110                 const deUint32                                  queueFamilyIndex        = m_context.getUniversalQueueFamilyIndex();
111                 const VkBufferCreateInfo                bufferParams            =
112                 {
113                         VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
114                         DE_NULL,
115                         m_testCase.flags,
116                         size,
117                         m_testCase.usage,
118                         m_testCase.sharingMode,
119                         1u,                                                                             //      deUint32                        queueFamilyCount;
120                         &queueFamilyIndex,
121                 };
122
123                 if (vk.createBuffer(vkDevice, &bufferParams, (const VkAllocationCallbacks*)DE_NULL, &testBuffer) != VK_SUCCESS)
124                         return tcu::TestStatus::fail("Buffer creation failed! (requested memory size: " + de::toString(size) + ")");
125
126                 vk.getBufferMemoryRequirements(vkDevice, testBuffer, &memReqs);
127
128                 if (size > memReqs.size)
129                 {
130                         std::ostringstream errorMsg;
131                         errorMsg << "Requied memory size (" << memReqs.size << " bytes) smaller than the buffer's size (" << size << " bytes)!";
132                         return tcu::TestStatus::fail(errorMsg.str());
133                 }
134         }
135
136         // Allocate and bind memory
137         {
138                 const VkMemoryAllocateInfo memAlloc =
139                 {
140                         VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
141                         NULL,
142                         memReqs.size,
143                         0                                                                               //      deUint32                memoryTypeIndex
144                 };
145
146                 if (vk.allocateMemory(vkDevice, &memAlloc, (const VkAllocationCallbacks*)DE_NULL, &memory) != VK_SUCCESS)
147                         return tcu::TestStatus::fail("Alloc memory failed! (requested memory size: " + de::toString(size) + ")");
148
149                 if (vk.bindBufferMemory(vkDevice, testBuffer, memory, 0) != VK_SUCCESS)
150                         return tcu::TestStatus::fail("Bind buffer memory failed! (requested memory size: " + de::toString(size) + ")");
151         }
152
153         vk.freeMemory(vkDevice, memory, (const VkAllocationCallbacks*)DE_NULL);
154         vk.destroyBuffer(vkDevice, testBuffer, (const VkAllocationCallbacks*)DE_NULL);
155
156         return tcu::TestStatus::pass("Buffer test");
157 }
158
159 tcu::TestStatus BufferTestInstance::iterate (void)
160 {
161         const VkDeviceSize testSizes[] =
162         {
163                 1,
164                 1181,
165                 15991,
166                 16384
167         };
168         tcu::TestStatus testStatus                                      = tcu::TestStatus::pass("Buffer test");
169
170         for (int i = 0; i < DE_LENGTH_OF_ARRAY(testSizes); i++)
171         {
172                 if ((testStatus = bufferCreateAndAllocTest(testSizes[i])).getCode() != QP_TEST_RESULT_PASS)
173                         return testStatus;
174         }
175
176         if (m_testCase.usage & (VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
177         {
178                 const VkPhysicalDevice          vkPhysicalDevice        = m_context.getPhysicalDevice();
179                 const InstanceInterface&        vkInstance                      = m_context.getInstanceInterface();
180                 VkPhysicalDeviceProperties      props;
181
182                 vkInstance.getPhysicalDeviceProperties(vkPhysicalDevice, &props);
183
184                 testStatus = bufferCreateAndAllocTest(props.limits.maxTexelBufferElements);
185         }
186
187         return testStatus;
188 }
189
190 } // anonymous
191
192  tcu::TestCaseGroup* createBufferTests (tcu::TestContext& testCtx)
193 {
194         const VkBufferUsageFlags bufferUsageModes[] =
195         {
196                 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
197                 VK_BUFFER_USAGE_TRANSFER_DST_BIT,
198                 VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT,
199                 VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT,
200                 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
201                 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
202                 VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
203                 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
204                 VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT
205         };
206
207         const VkBufferCreateFlags bufferCreateFlags[] =
208         {
209                 VK_BUFFER_CREATE_SPARSE_BINDING_BIT,
210                 VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT,
211                 VK_BUFFER_CREATE_SPARSE_ALIASED_BIT
212         };
213
214         de::MovePtr<tcu::TestCaseGroup> buffersTests    (new tcu::TestCaseGroup(testCtx, "buffer", "Buffer Tests"));
215
216         deUint32        numberOfBufferUsageFlags                        = DE_LENGTH_OF_ARRAY(bufferUsageModes);
217         deUint32        numberOfBufferCreateFlags                       = DE_LENGTH_OF_ARRAY(bufferCreateFlags);
218         deUint32        maximumValueOfBufferUsageFlags          = (1 << (numberOfBufferUsageFlags - 1)) - 1;
219         deUint32        maximumValueOfBufferCreateFlags         = (1 << (numberOfBufferCreateFlags)) - 1;
220
221         for (deUint32 combinedBufferCreateFlags = 0; combinedBufferCreateFlags <= maximumValueOfBufferCreateFlags; combinedBufferCreateFlags++)
222         {
223                 for (deUint32 combinedBufferUsageFlags = 1; combinedBufferUsageFlags <= maximumValueOfBufferUsageFlags; combinedBufferUsageFlags++)
224                 {
225                         BufferCaseParameters    testParams =
226                         {
227                                 combinedBufferUsageFlags,
228                                 combinedBufferCreateFlags,
229                                 VK_SHARING_MODE_EXCLUSIVE
230                         };
231                         std::ostringstream      testName;
232                         std::ostringstream      testDescription;
233                         testName << "createBuffer_" << combinedBufferUsageFlags << "_" << combinedBufferCreateFlags;
234                         testDescription << "vkCreateBuffer test " << combinedBufferUsageFlags << " " << combinedBufferCreateFlags;
235                         buffersTests->addChild(new BuffersTestCase(testCtx, testName.str(), testDescription.str(), testParams));
236                 }
237         }
238
239         return buffersTests.release();
240 }
241
242 } // api
243 } // vk