1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2015 The Khronos Group Inc.
6 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 * \brief Vulkan Buffers Tests
23 *//*--------------------------------------------------------------------*/
25 #include "vktApiBufferTests.hpp"
27 #include "deStringUtil.hpp"
28 #include "gluVarType.hpp"
29 #include "tcuTestLog.hpp"
30 #include "vkPrograms.hpp"
31 #include "vkQueryUtil.hpp"
32 #include "vkRefUtil.hpp"
33 #include "vktTestCase.hpp"
46 static const deUint32 MAX_BUFFER_SIZE_DIVISOR = 16;
48 struct BufferCaseParameters
50 VkBufferUsageFlags usage;
51 VkBufferCreateFlags flags;
52 VkSharingMode sharingMode;
55 class BufferTestInstance : public TestInstance
58 BufferTestInstance (Context& ctx,
59 BufferCaseParameters testCase)
61 , m_testCase (testCase)
63 virtual tcu::TestStatus iterate (void);
64 tcu::TestStatus bufferCreateAndAllocTest (VkDeviceSize size);
67 BufferCaseParameters m_testCase;
70 class BuffersTestCase : public TestCase
73 BuffersTestCase (tcu::TestContext& testCtx,
74 const std::string& name,
75 const std::string& description,
76 BufferCaseParameters testCase)
77 : TestCase(testCtx, name, description)
78 , m_testCase(testCase)
81 virtual ~BuffersTestCase (void) {}
82 virtual TestInstance* createInstance (Context& ctx) const
84 tcu::TestLog& log = m_testCtx.getLog();
85 log << tcu::TestLog::Message << getBufferUsageFlagsStr(m_testCase.usage) << tcu::TestLog::EndMessage;
86 return new BufferTestInstance(ctx, m_testCase);
90 BufferCaseParameters m_testCase;
93 tcu::TestStatus BufferTestInstance::bufferCreateAndAllocTest (VkDeviceSize size)
95 const VkPhysicalDevice vkPhysicalDevice = m_context.getPhysicalDevice();
96 const InstanceInterface& vkInstance = m_context.getInstanceInterface();
97 const VkDevice vkDevice = m_context.getDevice();
98 const DeviceInterface& vk = m_context.getDeviceInterface();
99 Move<VkBuffer> testBuffer;
100 VkMemoryRequirements memReqs;
101 Move<VkDeviceMemory> memory;
102 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
103 const VkPhysicalDeviceMemoryProperties memoryProperties = getPhysicalDeviceMemoryProperties(vkInstance, vkPhysicalDevice);
107 VkBufferCreateInfo bufferParams =
109 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
114 m_testCase.sharingMode,
115 1u, // deUint32 queueFamilyCount;
121 testBuffer = createBuffer(vk, vkDevice, &bufferParams);
123 catch (const vk::Error& error)
125 return tcu::TestStatus::fail("Buffer creation failed! (requested memory size: " + de::toString(size) + ", Error code: " + de::toString(error.getMessage()) + ")");
128 vk.getBufferMemoryRequirements(vkDevice, *testBuffer, &memReqs);
130 const deUint32 heapTypeIndex = (deUint32)deCtz32(memReqs.memoryTypeBits);
131 const VkMemoryType memoryType = memoryProperties.memoryTypes[heapTypeIndex];
132 const VkMemoryHeap memoryHeap = memoryProperties.memoryHeaps[memoryType.heapIndex];
133 const VkDeviceSize maxBufferSize = memoryHeap.size / MAX_BUFFER_SIZE_DIVISOR;
134 // If the requested size is too large, clamp it based on the selected heap size
135 if (size > maxBufferSize)
137 size = maxBufferSize;
138 bufferParams.size = size;
141 // allocate a new buffer with the adjusted size, the old one will be destroyed by the smart pointer
142 testBuffer = createBuffer(vk, vkDevice, &bufferParams);
144 catch (const vk::Error& error)
146 return tcu::TestStatus::fail("Buffer creation failed! (requested memory size: " + de::toString(size) + ", Error code: " + de::toString(error.getMessage()) + ")");
148 vk.getBufferMemoryRequirements(vkDevice, *testBuffer, &memReqs);
151 if (size > memReqs.size)
153 std::ostringstream errorMsg;
154 errorMsg << "Requied memory size (" << memReqs.size << " bytes) smaller than the buffer's size (" << size << " bytes)!";
155 return tcu::TestStatus::fail(errorMsg.str());
159 // Allocate and bind memory
161 const VkMemoryAllocateInfo memAlloc =
163 VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
166 (deUint32)deCtz32(memReqs.memoryTypeBits) // deUint32 memoryTypeIndex
171 memory = allocateMemory(vk, vkDevice, &memAlloc, (const VkAllocationCallbacks*)DE_NULL);
173 catch (const vk::Error& error)
175 return tcu::TestStatus::fail("Alloc memory failed! (requested memory size: " + de::toString(size) + ", Error code: " + de::toString(error.getMessage()) + ")");
178 if ((m_testCase.flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) ||
179 (m_testCase.flags & VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT) ||
180 (m_testCase.flags & VK_BUFFER_CREATE_SPARSE_ALIASED_BIT))
184 vk.getDeviceQueue(vkDevice, queueFamilyIndex, 0, &queue);
186 const VkSparseMemoryBind sparseMemoryBind =
188 0, // VkDeviceSize resourceOffset;
189 memReqs.size, // VkDeviceSize size;
190 *memory, // VkDeviceMemory memory;
191 0, // VkDeviceSize memoryOffset;
192 0 // VkSparseMemoryBindFlags flags;
195 const VkSparseBufferMemoryBindInfo sparseBufferMemoryBindInfo =
197 *testBuffer, // VkBuffer buffer;
198 1u, // deUint32 bindCount;
199 &sparseMemoryBind // const VkSparseMemoryBind* pBinds;
202 const VkBindSparseInfo bindSparseInfo =
204 VK_STRUCTURE_TYPE_BIND_SPARSE_INFO, // VkStructureType sType;
205 DE_NULL, // const void* pNext;
206 0, // deUint32 waitSemaphoreCount;
207 DE_NULL, // const VkSemaphore* pWaitSemaphores;
208 1u, // deUint32 bufferBindCount;
209 &sparseBufferMemoryBindInfo, // const VkSparseBufferMemoryBindInfo* pBufferBinds;
210 0, // deUint32 imageOpaqueBindCount;
211 DE_NULL, // const VkSparseImageOpaqueMemoryBindInfo* pImageOpaqueBinds;
212 0, // deUint32 imageBindCount;
213 DE_NULL, // const VkSparseImageMemoryBindInfo* pImageBinds;
214 0, // deUint32 signalSemaphoreCount;
215 DE_NULL, // const VkSemaphore* pSignalSemaphores;
218 const VkFenceCreateInfo fenceParams =
220 VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, // VkStructureType sType;
221 DE_NULL, // const void* pNext;
222 0u // VkFenceCreateFlags flags;
225 const vk::Unique<vk::VkFence> fence(vk::createFence(vk, vkDevice, &fenceParams));
227 VK_CHECK(vk.resetFences(vkDevice, 1, &fence.get()));
228 if (vk.queueBindSparse(queue, 1, &bindSparseInfo, *fence) != VK_SUCCESS)
229 return tcu::TestStatus::fail("Bind sparse buffer memory failed! (requested memory size: " + de::toString(size) + ")");
231 VK_CHECK(vk.waitForFences(vkDevice, 1, &fence.get(), VK_TRUE, ~(0ull) /* infinity */));
233 if (vk.bindBufferMemory(vkDevice, *testBuffer, *memory, 0) != VK_SUCCESS)
234 return tcu::TestStatus::fail("Bind buffer memory failed! (requested memory size: " + de::toString(size) + ")");
237 return tcu::TestStatus::pass("Buffer test");
240 tcu::TestStatus BufferTestInstance::iterate (void)
242 const VkPhysicalDeviceFeatures& physicalDeviceFeatures = m_context.getDeviceFeatures();
244 if ((m_testCase.flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT ) && !physicalDeviceFeatures.sparseBinding)
245 TCU_THROW(NotSupportedError, "Sparse bindings feature is not supported");
247 if ((m_testCase.flags & VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT ) && !physicalDeviceFeatures.sparseResidencyBuffer)
248 TCU_THROW(NotSupportedError, "Sparse buffer residency feature is not supported");
250 if ((m_testCase.flags & VK_BUFFER_CREATE_SPARSE_ALIASED_BIT ) && !physicalDeviceFeatures.sparseResidencyAliased)
251 TCU_THROW(NotSupportedError, "Sparse aliased residency feature is not supported");
253 const VkDeviceSize testSizes[] =
260 tcu::TestStatus testStatus = tcu::TestStatus::pass("Buffer test");
262 for (int i = 0; i < DE_LENGTH_OF_ARRAY(testSizes); i++)
264 if ((testStatus = bufferCreateAndAllocTest(testSizes[i])).getCode() != QP_TEST_RESULT_PASS)
268 if (m_testCase.usage & (VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
270 const VkPhysicalDevice vkPhysicalDevice = m_context.getPhysicalDevice();
271 const InstanceInterface& vkInstance = m_context.getInstanceInterface();
272 VkPhysicalDeviceProperties props;
274 vkInstance.getPhysicalDeviceProperties(vkPhysicalDevice, &props);
275 testStatus = bufferCreateAndAllocTest((VkDeviceSize) props.limits.maxTexelBufferElements);
283 tcu::TestCaseGroup* createBufferTests (tcu::TestContext& testCtx)
285 const VkBufferUsageFlags bufferUsageModes[] =
287 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
288 VK_BUFFER_USAGE_TRANSFER_DST_BIT,
289 VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT,
290 VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT,
291 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
292 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
293 VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
294 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
295 VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT
298 const VkBufferCreateFlags bufferCreateFlags[] =
300 VK_BUFFER_CREATE_SPARSE_BINDING_BIT,
301 VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT,
302 VK_BUFFER_CREATE_SPARSE_ALIASED_BIT
305 de::MovePtr<tcu::TestCaseGroup> buffersTests (new tcu::TestCaseGroup(testCtx, "buffer", "Buffer Tests"));
307 deUint32 numberOfBufferUsageFlags = DE_LENGTH_OF_ARRAY(bufferUsageModes);
308 deUint32 numberOfBufferCreateFlags = DE_LENGTH_OF_ARRAY(bufferCreateFlags);
309 deUint32 maximumValueOfBufferUsageFlags = (1 << (numberOfBufferUsageFlags - 1)) - 1;
310 deUint32 maximumValueOfBufferCreateFlags = (1 << (numberOfBufferCreateFlags)) - 1;
312 for (deUint32 combinedBufferCreateFlags = 0; combinedBufferCreateFlags <= maximumValueOfBufferCreateFlags; combinedBufferCreateFlags++)
314 for (deUint32 combinedBufferUsageFlags = 1; combinedBufferUsageFlags <= maximumValueOfBufferUsageFlags; combinedBufferUsageFlags++)
316 if (combinedBufferCreateFlags == VK_BUFFER_CREATE_SPARSE_ALIASED_BIT)
318 // spec says: If flags contains VK_BUFFER_CREATE_SPARSE_ALIASED_BIT, it must also contain at least one of
319 // VK_BUFFER_CREATE_SPARSE_BINDING_BIT or VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT
322 BufferCaseParameters testParams =
324 combinedBufferUsageFlags,
325 combinedBufferCreateFlags,
326 VK_SHARING_MODE_EXCLUSIVE
328 std::ostringstream testName;
329 std::ostringstream testDescription;
330 testName << "createBuffer_" << combinedBufferUsageFlags << "_" << combinedBufferCreateFlags;
331 testDescription << "vkCreateBuffer test " << combinedBufferUsageFlags << " " << combinedBufferCreateFlags;
332 buffersTests->addChild(new BuffersTestCase(testCtx, testName.str(), testDescription.str(), testParams));
336 return buffersTests.release();