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 vktSparseResourcesBufferSparseBinding.cpp
21 * \brief Buffer Sparse Binding tests
22 *//*--------------------------------------------------------------------*/
23
24 #include "vktSparseResourcesBufferSparseBinding.hpp"
25 #include "vktSparseResourcesTestsUtil.hpp"
26 #include "vktSparseResourcesBase.hpp"
27 #include "vktTestCaseUtil.hpp"
28
29 #include "vkDefs.hpp"
30 #include "vkRef.hpp"
31 #include "vkRefUtil.hpp"
32 #include "vkPlatform.hpp"
33 #include "vkPrograms.hpp"
34 #include "vkMemUtil.hpp"
35 #include "vkBarrierUtil.hpp"
36 #include "vkBuilderUtil.hpp"
37 #include "vkImageUtil.hpp"
38 #include "vkQueryUtil.hpp"
39 #include "vkTypeUtil.hpp"
40 #include "vkCmdUtil.hpp"
41
42 #include "deUniquePtr.hpp"
43 #include "deStringUtil.hpp"
44
45 #include <string>
46 #include <vector>
47
48 using namespace vk;
49
50 namespace vkt
51 {
52 namespace sparse
53 {
54 namespace
55 {
56
57 class BufferSparseBindingCase : public TestCase
58 {
59 public:
60 BufferSparseBindingCase (tcu::TestContext& testCtx,
61 const std::string& name,
62 const std::string& description,
63 const deUint32 bufferSize,
64 const bool useDeviceGroups);
65
66 TestInstance* createInstance (Context& context) const;
67
68 private:
69 const deUint32 m_bufferSize;
70 const bool m_useDeviceGroups;
71 };
72
BufferSparseBindingCase(tcu::TestContext & testCtx,const std::string & name,const std::string & description,const deUint32 bufferSize,const bool useDeviceGroups)73 BufferSparseBindingCase::BufferSparseBindingCase (tcu::TestContext& testCtx,
74 const std::string& name,
75 const std::string& description,
76 const deUint32 bufferSize,
77 const bool useDeviceGroups)
78 : TestCase (testCtx, name, description)
79 , m_bufferSize (bufferSize)
80 , m_useDeviceGroups (useDeviceGroups)
81 {
82 }
83
84 class BufferSparseBindingInstance : public SparseResourcesBaseInstance
85 {
86 public:
87 BufferSparseBindingInstance (Context& context,
88 const deUint32 bufferSize,
89 const bool useDeviceGroups);
90
91 tcu::TestStatus iterate (void);
92
93 private:
94 const deUint32 m_bufferSize;
95 const deUint32 m_useDeviceGroups;
96 };
97
BufferSparseBindingInstance(Context & context,const deUint32 bufferSize,const bool useDeviceGroups)98 BufferSparseBindingInstance::BufferSparseBindingInstance (Context& context,
99 const deUint32 bufferSize,
100 const bool useDeviceGroups)
101
102 : SparseResourcesBaseInstance (context, useDeviceGroups)
103 , m_bufferSize (bufferSize)
104 , m_useDeviceGroups (useDeviceGroups)
105 {
106 }
107
iterate(void)108 tcu::TestStatus BufferSparseBindingInstance::iterate (void)
109 {
110 const InstanceInterface& instance = m_context.getInstanceInterface();
111 {
112 // Create logical device supporting both sparse and compute operations
113 QueueRequirementsVec queueRequirements;
114 queueRequirements.push_back(QueueRequirements(VK_QUEUE_SPARSE_BINDING_BIT, 1u));
115 queueRequirements.push_back(QueueRequirements(VK_QUEUE_COMPUTE_BIT, 1u));
116
117 createDeviceSupportingQueues(queueRequirements);
118 }
119 const vk::VkPhysicalDevice& physicalDevice = getPhysicalDevice();
120
121 if (!getPhysicalDeviceFeatures(instance, physicalDevice).sparseBinding)
122 TCU_THROW(NotSupportedError, "Sparse binding not supported");
123
124 const DeviceInterface& deviceInterface = getDeviceInterface();
125 const Queue& sparseQueue = getQueue(VK_QUEUE_SPARSE_BINDING_BIT, 0);
126 const Queue& computeQueue = getQueue(VK_QUEUE_COMPUTE_BIT, 0);
127
128 // Go through all physical devices
129 for (deUint32 physDevID = 0; physDevID < m_numPhysicalDevices; physDevID++)
130 {
131 const deUint32 firstDeviceID = physDevID;
132 const deUint32 secondDeviceID = (firstDeviceID + 1) % m_numPhysicalDevices;
133
134 VkBufferCreateInfo bufferCreateInfo;
135
136 bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; // VkStructureType sType;
137 bufferCreateInfo.pNext = DE_NULL; // const void* pNext;
138 bufferCreateInfo.flags = VK_BUFFER_CREATE_SPARSE_BINDING_BIT; // VkBufferCreateFlags flags;
139 bufferCreateInfo.size = m_bufferSize; // VkDeviceSize size;
140 bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
141 VK_BUFFER_USAGE_TRANSFER_DST_BIT; // VkBufferUsageFlags usage;
142 bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; // VkSharingMode sharingMode;
143 bufferCreateInfo.queueFamilyIndexCount = 0u; // deUint32 queueFamilyIndexCount;
144 bufferCreateInfo.pQueueFamilyIndices = DE_NULL; // const deUint32* pQueueFamilyIndices;
145
146 const deUint32 queueFamilyIndices[] = { sparseQueue.queueFamilyIndex, computeQueue.queueFamilyIndex };
147
148 if (sparseQueue.queueFamilyIndex != computeQueue.queueFamilyIndex)
149 {
150 bufferCreateInfo.sharingMode = VK_SHARING_MODE_CONCURRENT; // VkSharingMode sharingMode;
151 bufferCreateInfo.queueFamilyIndexCount = 2u; // deUint32 queueFamilyIndexCount;
152 bufferCreateInfo.pQueueFamilyIndices = queueFamilyIndices; // const deUint32* pQueueFamilyIndices;
153 }
154
155 // Create sparse buffer
156 const Unique<VkBuffer> sparseBuffer(createBuffer(deviceInterface, getDevice(), &bufferCreateInfo));
157
158 // Create sparse buffer memory bind semaphore
159 const Unique<VkSemaphore> bufferMemoryBindSemaphore(createSemaphore(deviceInterface, getDevice()));
160
161 const VkMemoryRequirements bufferMemRequirement = getBufferMemoryRequirements(deviceInterface, getDevice(), *sparseBuffer);
162
163 if (bufferMemRequirement.size > getPhysicalDeviceProperties(instance, physicalDevice).limits.sparseAddressSpaceSize)
164 TCU_THROW(NotSupportedError, "Required memory size for sparse resources exceeds device limits");
165
166 DE_ASSERT((bufferMemRequirement.size % bufferMemRequirement.alignment) == 0);
167
168 Move<VkDeviceMemory> sparseMemoryAllocation;
169
170 {
171 std::vector<VkSparseMemoryBind> sparseMemoryBinds;
172 const deUint32 numSparseBinds = static_cast<deUint32>(bufferMemRequirement.size / bufferMemRequirement.alignment);
173 const deUint32 memoryType = findMatchingMemoryType(instance, getPhysicalDevice(secondDeviceID), bufferMemRequirement, MemoryRequirement::Any);
174
175 if (memoryType == NO_MATCH_FOUND)
176 return tcu::TestStatus::fail("No matching memory type found");
177
178 if (firstDeviceID != secondDeviceID)
179 {
180 VkPeerMemoryFeatureFlags peerMemoryFeatureFlags = (VkPeerMemoryFeatureFlags)0;
181 const deUint32 heapIndex = getHeapIndexForMemoryType(instance, getPhysicalDevice(secondDeviceID), memoryType);
182 deviceInterface.getDeviceGroupPeerMemoryFeatures(getDevice(), heapIndex, firstDeviceID, secondDeviceID, &peerMemoryFeatureFlags);
183
184 if (((peerMemoryFeatureFlags & VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT) == 0) ||
185 ((peerMemoryFeatureFlags & VK_PEER_MEMORY_FEATURE_COPY_DST_BIT) == 0))
186 {
187 TCU_THROW(NotSupportedError, "Peer memory does not support COPY_SRC and COPY_DST");
188 }
189 }
190
191 {
192 const VkMemoryAllocateInfo allocateInfo =
193 {
194 VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, // VkStructureType sType;
195 DE_NULL, // const void* pNext;
196 bufferMemRequirement.size, // VkDeviceSize allocationSize;
197 memoryType, // uint32_t memoryTypeIndex;
198 };
199
200 sparseMemoryAllocation = allocateMemory(deviceInterface, getDevice(), &allocateInfo);
201 }
202
203 for (deUint32 sparseBindNdx = 0; sparseBindNdx < numSparseBinds; ++sparseBindNdx)
204 {
205 const VkSparseMemoryBind sparseMemoryBind =
206 {
207 bufferMemRequirement.alignment * sparseBindNdx, // VkDeviceSize resourceOffset;
208 bufferMemRequirement.alignment, // VkDeviceSize size;
209 *sparseMemoryAllocation, // VkDeviceMemory memory;
210 bufferMemRequirement.alignment * sparseBindNdx, // VkDeviceSize memoryOffset;
211 (VkSparseMemoryBindFlags)0, // VkSparseMemoryBindFlags flags;
212 };
213 sparseMemoryBinds.push_back(sparseMemoryBind);
214 }
215
216 const VkSparseBufferMemoryBindInfo sparseBufferBindInfo = makeSparseBufferMemoryBindInfo(*sparseBuffer, numSparseBinds, &sparseMemoryBinds[0]);
217
218 const VkDeviceGroupBindSparseInfo devGroupBindSparseInfo =
219 {
220 VK_STRUCTURE_TYPE_DEVICE_GROUP_BIND_SPARSE_INFO_KHR, //VkStructureType sType;
221 DE_NULL, //const void* pNext;
222 firstDeviceID, //deUint32 resourceDeviceIndex;
223 secondDeviceID, //deUint32 memoryDeviceIndex;
224 };
225
226 const VkBindSparseInfo bindSparseInfo =
227 {
228 VK_STRUCTURE_TYPE_BIND_SPARSE_INFO, //VkStructureType sType;
229 m_useDeviceGroups ? &devGroupBindSparseInfo : DE_NULL, //const void* pNext;
230 0u, //deUint32 waitSemaphoreCount;
231 DE_NULL, //const VkSemaphore* pWaitSemaphores;
232 1u, //deUint32 bufferBindCount;
233 &sparseBufferBindInfo, //const VkSparseBufferMemoryBindInfo* pBufferBinds;
234 0u, //deUint32 imageOpaqueBindCount;
235 DE_NULL, //const VkSparseImageOpaqueMemoryBindInfo* pImageOpaqueBinds;
236 0u, //deUint32 imageBindCount;
237 DE_NULL, //const VkSparseImageMemoryBindInfo* pImageBinds;
238 1u, //deUint32 signalSemaphoreCount;
239 &bufferMemoryBindSemaphore.get() //const VkSemaphore* pSignalSemaphores;
240 };
241
242 // Submit sparse bind commands for execution
243 VK_CHECK(deviceInterface.queueBindSparse(sparseQueue.queueHandle, 1u, &bindSparseInfo, DE_NULL));
244 }
245
246 // Create command buffer for transfer oparations
247 const Unique<VkCommandPool> commandPool(makeCommandPool(deviceInterface, getDevice(), computeQueue.queueFamilyIndex));
248 const Unique<VkCommandBuffer> commandBuffer(allocateCommandBuffer(deviceInterface, getDevice(), *commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
249
250 // Start recording transfer commands
251 beginCommandBuffer(deviceInterface, *commandBuffer);
252
253 const VkBufferCreateInfo inputBufferCreateInfo = makeBufferCreateInfo(m_bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
254 const Unique<VkBuffer> inputBuffer(createBuffer(deviceInterface, getDevice(), &inputBufferCreateInfo));
255 const de::UniquePtr<Allocation> inputBufferAlloc(bindBuffer(deviceInterface, getDevice(), getAllocator(), *inputBuffer, MemoryRequirement::HostVisible));
256
257 std::vector<deUint8> referenceData;
258 referenceData.resize(m_bufferSize);
259
260 for (deUint32 valueNdx = 0; valueNdx < m_bufferSize; ++valueNdx)
261 {
262 referenceData[valueNdx] = static_cast<deUint8>((valueNdx % bufferMemRequirement.alignment) + 1u);
263 }
264
265 deMemcpy(inputBufferAlloc->getHostPtr(), &referenceData[0], m_bufferSize);
266
267 flushAlloc(deviceInterface, getDevice(), *inputBufferAlloc);
268
269 {
270 const VkBufferMemoryBarrier inputBufferBarrier
271 = makeBufferMemoryBarrier(VK_ACCESS_HOST_WRITE_BIT,
272 VK_ACCESS_TRANSFER_READ_BIT,
273 *inputBuffer,
274 0u,
275 m_bufferSize);
276
277 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &inputBufferBarrier, 0u, DE_NULL);
278 }
279
280 {
281 const VkBufferCopy bufferCopy = makeBufferCopy(0u, 0u, m_bufferSize);
282
283 deviceInterface.cmdCopyBuffer(*commandBuffer, *inputBuffer, *sparseBuffer, 1u, &bufferCopy);
284 }
285
286 {
287 const VkBufferMemoryBarrier sparseBufferBarrier
288 = makeBufferMemoryBarrier(VK_ACCESS_TRANSFER_WRITE_BIT,
289 VK_ACCESS_TRANSFER_READ_BIT,
290 *sparseBuffer,
291 0u,
292 m_bufferSize);
293
294 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0u, 0u, DE_NULL, 1u, &sparseBufferBarrier, 0u, DE_NULL);
295 }
296
297 const VkBufferCreateInfo outputBufferCreateInfo = makeBufferCreateInfo(m_bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT);
298 const Unique<VkBuffer> outputBuffer(createBuffer(deviceInterface, getDevice(), &outputBufferCreateInfo));
299 const de::UniquePtr<Allocation> outputBufferAlloc(bindBuffer(deviceInterface, getDevice(), getAllocator(), *outputBuffer, MemoryRequirement::HostVisible));
300
301 {
302 const VkBufferCopy bufferCopy = makeBufferCopy(0u, 0u, m_bufferSize);
303
304 deviceInterface.cmdCopyBuffer(*commandBuffer, *sparseBuffer, *outputBuffer, 1u, &bufferCopy);
305 }
306
307 {
308 const VkBufferMemoryBarrier outputBufferBarrier
309 = makeBufferMemoryBarrier(VK_ACCESS_TRANSFER_WRITE_BIT,
310 VK_ACCESS_HOST_READ_BIT,
311 *outputBuffer,
312 0u,
313 m_bufferSize);
314
315 deviceInterface.cmdPipelineBarrier(*commandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0u, 0u, DE_NULL, 1u, &outputBufferBarrier, 0u, DE_NULL);
316 }
317
318 // End recording transfer commands
319 endCommandBuffer(deviceInterface, *commandBuffer);
320
321 const VkPipelineStageFlags waitStageBits[] = { VK_PIPELINE_STAGE_TRANSFER_BIT };
322
323 // Submit transfer commands for execution and wait for completion
324 // In case of device groups, submit on the physical device with the resource
325 submitCommandsAndWait(deviceInterface, getDevice(), computeQueue.queueHandle, *commandBuffer, 1u, &bufferMemoryBindSemaphore.get(),
326 waitStageBits, 0, DE_NULL, m_useDeviceGroups, firstDeviceID);
327
328 // Retrieve data from output buffer to host memory
329 invalidateAlloc(deviceInterface, getDevice(), *outputBufferAlloc);
330
331 const deUint8* outputData = static_cast<const deUint8*>(outputBufferAlloc->getHostPtr());
332
333 // Wait for sparse queue to become idle
334 deviceInterface.queueWaitIdle(sparseQueue.queueHandle);
335
336 // Compare output data with reference data
337 if (deMemCmp(&referenceData[0], outputData, m_bufferSize) != 0)
338 return tcu::TestStatus::fail("Failed");
339 }
340 return tcu::TestStatus::pass("Passed");
341 }
342
createInstance(Context & context) const343 TestInstance* BufferSparseBindingCase::createInstance (Context& context) const
344 {
345 return new BufferSparseBindingInstance(context, m_bufferSize, m_useDeviceGroups);
346 }
347
348 } // anonymous ns
349
addBufferSparseBindingTests(tcu::TestCaseGroup * group,const bool useDeviceGroups)350 void addBufferSparseBindingTests (tcu::TestCaseGroup* group, const bool useDeviceGroups)
351 {
352 group->addChild(new BufferSparseBindingCase(group->getTestContext(), "buffer_size_2_10", "", 1 << 10, useDeviceGroups));
353 group->addChild(new BufferSparseBindingCase(group->getTestContext(), "buffer_size_2_12", "", 1 << 12, useDeviceGroups));
354 group->addChild(new BufferSparseBindingCase(group->getTestContext(), "buffer_size_2_16", "", 1 << 16, useDeviceGroups));
355 group->addChild(new BufferSparseBindingCase(group->getTestContext(), "buffer_size_2_17", "", 1 << 17, useDeviceGroups));
356 group->addChild(new BufferSparseBindingCase(group->getTestContext(), "buffer_size_2_20", "", 1 << 20, useDeviceGroups));
357 group->addChild(new BufferSparseBindingCase(group->getTestContext(), "buffer_size_2_24", "", 1 << 24, useDeviceGroups));
358 }
359
360 } // sparse
361 } // vkt
362