1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2016 The Khronos Group Inc.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * \file vktSparseResourcesBufferSparseBinding.cpp
21 * \brief Buffer Sparse Binding tests
22 *//*--------------------------------------------------------------------*/
24 #include "vktSparseResourcesBufferSparseBinding.hpp"
25 #include "vktSparseResourcesTestsUtil.hpp"
26 #include "vktSparseResourcesBase.hpp"
27 #include "vktTestCaseUtil.hpp"
31 #include "vkRefUtil.hpp"
32 #include "vkPlatform.hpp"
33 #include "vkPrograms.hpp"
34 #include "vkMemUtil.hpp"
35 #include "vkBuilderUtil.hpp"
36 #include "vkImageUtil.hpp"
37 #include "vkQueryUtil.hpp"
38 #include "vkTypeUtil.hpp"
40 #include "deUniquePtr.hpp"
41 #include "deStringUtil.hpp"
55 class BufferSparseBindingCase : public TestCase
58 BufferSparseBindingCase (tcu::TestContext& testCtx,
59 const std::string& name,
60 const std::string& description,
61 const deUint32 bufferSize);
63 TestInstance* createInstance (Context& context) const;
66 const deUint32 m_bufferSize;
69 BufferSparseBindingCase::BufferSparseBindingCase (tcu::TestContext& testCtx,
70 const std::string& name,
71 const std::string& description,
72 const deUint32 bufferSize)
73 : TestCase (testCtx, name, description)
74 , m_bufferSize (bufferSize)
78 class BufferSparseBindingInstance : public SparseResourcesBaseInstance
81 BufferSparseBindingInstance (Context& context,
82 const deUint32 bufferSize);
84 tcu::TestStatus iterate (void);
87 const deUint32 m_bufferSize;
90 BufferSparseBindingInstance::BufferSparseBindingInstance (Context& context,
91 const deUint32 bufferSize)
93 : SparseResourcesBaseInstance (context)
94 , m_bufferSize (bufferSize)
98 tcu::TestStatus BufferSparseBindingInstance::iterate (void)
100 const InstanceInterface& instance = m_context.getInstanceInterface();
101 const VkPhysicalDevice physicalDevice = m_context.getPhysicalDevice();
103 if (!getPhysicalDeviceFeatures(instance, physicalDevice).sparseBinding)
104 TCU_THROW(NotSupportedError, "Sparse binding not supported");
107 // Create logical device supporting both sparse and compute operations
108 QueueRequirementsVec queueRequirements;
109 queueRequirements.push_back(QueueRequirements(VK_QUEUE_SPARSE_BINDING_BIT, 1u));
110 queueRequirements.push_back(QueueRequirements(VK_QUEUE_COMPUTE_BIT, 1u));
112 createDeviceSupportingQueues(queueRequirements);
115 const DeviceInterface& deviceInterface = getDeviceInterface();
116 const Queue& sparseQueue = getQueue(VK_QUEUE_SPARSE_BINDING_BIT, 0);
117 const Queue& computeQueue = getQueue(VK_QUEUE_COMPUTE_BIT, 0);
119 VkBufferCreateInfo bufferCreateInfo;
121 bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; // VkStructureType sType;
122 bufferCreateInfo.pNext = DE_NULL; // const void* pNext;
123 bufferCreateInfo.flags = VK_BUFFER_CREATE_SPARSE_BINDING_BIT; // VkBufferCreateFlags flags;
124 bufferCreateInfo.size = m_bufferSize; // VkDeviceSize size;
125 bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
126 VK_BUFFER_USAGE_TRANSFER_DST_BIT; // VkBufferUsageFlags usage;
127 bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; // VkSharingMode sharingMode;
128 bufferCreateInfo.queueFamilyIndexCount = 0u; // deUint32 queueFamilyIndexCount;
129 bufferCreateInfo.pQueueFamilyIndices = DE_NULL; // const deUint32* pQueueFamilyIndices;
131 const deUint32 queueFamilyIndices[] = { sparseQueue.queueFamilyIndex, computeQueue.queueFamilyIndex };
133 if (sparseQueue.queueFamilyIndex != computeQueue.queueFamilyIndex)
135 bufferCreateInfo.sharingMode = VK_SHARING_MODE_CONCURRENT; // VkSharingMode sharingMode;
136 bufferCreateInfo.queueFamilyIndexCount = 2u; // deUint32 queueFamilyIndexCount;
137 bufferCreateInfo.pQueueFamilyIndices = queueFamilyIndices; // const deUint32* pQueueFamilyIndices;
140 // Create sparse buffer
141 const Unique<VkBuffer> sparseBuffer(createBuffer(deviceInterface, getDevice(), &bufferCreateInfo));
143 // Create sparse buffer memory bind semaphore
144 const Unique<VkSemaphore> bufferMemoryBindSemaphore(createSemaphore(deviceInterface, getDevice()));
146 const VkMemoryRequirements bufferMemRequirement = getBufferMemoryRequirements(deviceInterface, getDevice(), *sparseBuffer);
148 if (bufferMemRequirement.size > getPhysicalDeviceProperties(instance, physicalDevice).limits.sparseAddressSpaceSize)
149 TCU_THROW(NotSupportedError, "Required memory size for sparse resources exceeds device limits");
151 DE_ASSERT((bufferMemRequirement.size % bufferMemRequirement.alignment) == 0);
153 std::vector<DeviceMemorySp> deviceMemUniquePtrVec;
156 std::vector<VkSparseMemoryBind> sparseMemoryBinds;
157 const deUint32 numSparseBinds = static_cast<deUint32>(bufferMemRequirement.size / bufferMemRequirement.alignment);
158 const deUint32 memoryType = findMatchingMemoryType(instance, physicalDevice, bufferMemRequirement, MemoryRequirement::Any);
160 if (memoryType == NO_MATCH_FOUND)
161 return tcu::TestStatus::fail("No matching memory type found");
163 for (deUint32 sparseBindNdx = 0; sparseBindNdx < numSparseBinds; ++sparseBindNdx)
165 const VkSparseMemoryBind sparseMemoryBind = makeSparseMemoryBind(deviceInterface, getDevice(), bufferMemRequirement.alignment, memoryType, bufferMemRequirement.alignment * sparseBindNdx);
167 deviceMemUniquePtrVec.push_back(makeVkSharedPtr(Move<VkDeviceMemory>(check<VkDeviceMemory>(sparseMemoryBind.memory), Deleter<VkDeviceMemory>(deviceInterface, getDevice(), DE_NULL))));
169 sparseMemoryBinds.push_back(sparseMemoryBind);
172 const VkSparseBufferMemoryBindInfo sparseBufferBindInfo = makeSparseBufferMemoryBindInfo(*sparseBuffer, numSparseBinds, &sparseMemoryBinds[0]);
174 const VkBindSparseInfo bindSparseInfo =
176 VK_STRUCTURE_TYPE_BIND_SPARSE_INFO, //VkStructureType sType;
177 DE_NULL, //const void* pNext;
178 0u, //deUint32 waitSemaphoreCount;
179 DE_NULL, //const VkSemaphore* pWaitSemaphores;
180 1u, //deUint32 bufferBindCount;
181 &sparseBufferBindInfo, //const VkSparseBufferMemoryBindInfo* pBufferBinds;
182 0u, //deUint32 imageOpaqueBindCount;
183 DE_NULL, //const VkSparseImageOpaqueMemoryBindInfo* pImageOpaqueBinds;
184 0u, //deUint32 imageBindCount;
185 DE_NULL, //const VkSparseImageMemoryBindInfo* pImageBinds;
186 1u, //deUint32 signalSemaphoreCount;
187 &bufferMemoryBindSemaphore.get() //const VkSemaphore* pSignalSemaphores;
190 // Submit sparse bind commands for execution
191 VK_CHECK(deviceInterface.queueBindSparse(sparseQueue.queueHandle, 1u, &bindSparseInfo, DE_NULL));
194 // Create command buffer for transfer oparations
195 const Unique<VkCommandPool> commandPool(makeCommandPool(deviceInterface, getDevice(), computeQueue.queueFamilyIndex));
196 const Unique<VkCommandBuffer> commandBuffer(allocateCommandBuffer(deviceInterface, getDevice(), *commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
198 // Start recording transfer commands
199 beginCommandBuffer(deviceInterface, *commandBuffer);
201 const VkBufferCreateInfo inputBufferCreateInfo = makeBufferCreateInfo(m_bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
202 const Unique<VkBuffer> inputBuffer (createBuffer(deviceInterface, getDevice(), &inputBufferCreateInfo));
203 const de::UniquePtr<Allocation> inputBufferAlloc (bindBuffer(deviceInterface, getDevice(), getAllocator(), *inputBuffer, MemoryRequirement::HostVisible));
205 std::vector<deUint8> referenceData;
206 referenceData.resize(m_bufferSize);
208 for (deUint32 valueNdx = 0; valueNdx < m_bufferSize; ++valueNdx)
210 referenceData[valueNdx] = static_cast<deUint8>((valueNdx % bufferMemRequirement.alignment) + 1u);
213 deMemcpy(inputBufferAlloc->getHostPtr(), &referenceData[0], m_bufferSize);
215 flushMappedMemoryRange(deviceInterface, getDevice(), inputBufferAlloc->getMemory(), inputBufferAlloc->getOffset(), m_bufferSize);
218 const VkBufferMemoryBarrier inputBufferBarrier
219 = makeBufferMemoryBarrier( VK_ACCESS_HOST_WRITE_BIT,
220 VK_ACCESS_TRANSFER_READ_BIT,
225 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &inputBufferBarrier, 0u, DE_NULL);
229 const VkBufferCopy bufferCopy = makeBufferCopy(0u, 0u, m_bufferSize);
231 deviceInterface.cmdCopyBuffer(*commandBuffer, *inputBuffer, *sparseBuffer, 1u, &bufferCopy);
235 const VkBufferMemoryBarrier sparseBufferBarrier
236 = makeBufferMemoryBarrier( VK_ACCESS_TRANSFER_WRITE_BIT,
237 VK_ACCESS_TRANSFER_READ_BIT,
242 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &sparseBufferBarrier, 0u, DE_NULL);
245 const VkBufferCreateInfo outputBufferCreateInfo = makeBufferCreateInfo(m_bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
246 const Unique<VkBuffer> outputBuffer (createBuffer(deviceInterface, getDevice(), &outputBufferCreateInfo));
247 const de::UniquePtr<Allocation> outputBufferAlloc (bindBuffer(deviceInterface, getDevice(), getAllocator(), *outputBuffer, MemoryRequirement::HostVisible));
250 const VkBufferCopy bufferCopy = makeBufferCopy(0u, 0u, m_bufferSize);
252 deviceInterface.cmdCopyBuffer(*commandBuffer, *sparseBuffer, *outputBuffer, 1u, &bufferCopy);
256 const VkBufferMemoryBarrier outputBufferBarrier
257 = makeBufferMemoryBarrier( VK_ACCESS_TRANSFER_WRITE_BIT,
258 VK_ACCESS_HOST_READ_BIT,
263 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 1u, &outputBufferBarrier, 0u, DE_NULL);
266 // End recording transfer commands
267 endCommandBuffer(deviceInterface, *commandBuffer);
269 const VkPipelineStageFlags waitStageBits[] = { VK_PIPELINE_STAGE_TRANSFER_BIT };
271 // Submit transfer commands for execution and wait for completion
272 submitCommandsAndWait(deviceInterface, getDevice(), computeQueue.queueHandle, *commandBuffer, 1u, &bufferMemoryBindSemaphore.get(), waitStageBits);
274 // Retrieve data from output buffer to host memory
275 invalidateMappedMemoryRange(deviceInterface, getDevice(), outputBufferAlloc->getMemory(), outputBufferAlloc->getOffset(), m_bufferSize);
277 const deUint8* outputData = static_cast<const deUint8*>(outputBufferAlloc->getHostPtr());
279 // Wait for sparse queue to become idle
280 deviceInterface.queueWaitIdle(sparseQueue.queueHandle);
282 // Compare output data with reference data
283 if (deMemCmp(&referenceData[0], outputData, m_bufferSize) != 0)
284 return tcu::TestStatus::fail("Failed");
286 return tcu::TestStatus::pass("Passed");
289 TestInstance* BufferSparseBindingCase::createInstance (Context& context) const
291 return new BufferSparseBindingInstance(context, m_bufferSize);
296 void addBufferSparseBindingTests (tcu::TestCaseGroup* group)
298 group->addChild(new BufferSparseBindingCase(group->getTestContext(), "buffer_size_2_10", "", 1 << 10));
299 group->addChild(new BufferSparseBindingCase(group->getTestContext(), "buffer_size_2_12", "", 1 << 12));
300 group->addChild(new BufferSparseBindingCase(group->getTestContext(), "buffer_size_2_16", "", 1 << 16));
301 group->addChild(new BufferSparseBindingCase(group->getTestContext(), "buffer_size_2_17", "", 1 << 17));
302 group->addChild(new BufferSparseBindingCase(group->getTestContext(), "buffer_size_2_20", "", 1 << 20));
303 group->addChild(new BufferSparseBindingCase(group->getTestContext(), "buffer_size_2_24", "", 1 << 24));