1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2017 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
21  * \brief Implementation of utility classes for various kinds of
22  * allocation memory for buffers and images
23  *//*--------------------------------------------------------------------*/
24 
25 #include "vktApiBufferAndImageAllocationUtil.hpp"
26 
27 #include "deInt32.h"
28 #include "tcuVectorUtil.hpp"
29 #include "vkQueryUtil.hpp"
30 #include "vkMemUtil.hpp"
31 #include "vkRefUtil.hpp"
32 
33 #include <sstream>
34 
35 namespace vkt
36 {
37 
38 namespace api
39 {
40 
createTestBuffer(const DeviceInterface & vk,VkDevice vkDevice,uint32_t queueFamilyIndex,VkDeviceSize size,VkBufferUsageFlags usage,Context & context,Allocator & allocator,Move<VkBuffer> & buffer,const MemoryRequirement & requirement,de::MovePtr<Allocation> & memory) const41 void BufferSuballocation::createTestBuffer(const DeviceInterface &vk, VkDevice vkDevice, uint32_t queueFamilyIndex,
42                                            VkDeviceSize size, VkBufferUsageFlags usage, Context &context,
43                                            Allocator &allocator, Move<VkBuffer> &buffer,
44                                            const MemoryRequirement &requirement, de::MovePtr<Allocation> &memory) const
45 {
46     DE_UNREF(context);
47 
48     const VkBufferCreateInfo bufferParams{
49         VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
50         DE_NULL,                              // const void* pNext;
51         0u,                                   // VkBufferCreateFlags flags;
52         size,                                 // VkDeviceSize size;
53         usage,                                // VkBufferUsageFlags usage;
54         VK_SHARING_MODE_EXCLUSIVE,            // VkSharingMode sharingMode;
55         1u,                                   // uint32_t queueFamilyCount;
56         &queueFamilyIndex,                    // const uint32_t* pQueueFamilyIndices;
57     };
58 
59     buffer = vk::createBuffer(vk, vkDevice, &bufferParams, (const VkAllocationCallbacks *)DE_NULL);
60     memory = allocator.allocate(getBufferMemoryRequirements(vk, vkDevice, *buffer), requirement);
61     VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, memory->getMemory(), 0));
62 }
63 
createTestBuffer(const DeviceInterface & vk,VkDevice vkDevice,uint32_t queueFamilyIndex,VkDeviceSize size,VkBufferUsageFlags usage,Context & context,Allocator & allocator,Move<VkBuffer> & buffer,const MemoryRequirement & requirement,de::MovePtr<Allocation> & memory) const64 void BufferDedicatedAllocation::createTestBuffer(const DeviceInterface &vk, VkDevice vkDevice,
65                                                  uint32_t queueFamilyIndex, VkDeviceSize size, VkBufferUsageFlags usage,
66                                                  Context &context, Allocator &allocator, Move<VkBuffer> &buffer,
67                                                  const MemoryRequirement &requirement,
68                                                  de::MovePtr<Allocation> &memory) const
69 {
70     DE_UNREF(allocator);
71     if (!context.isDeviceFunctionalitySupported("VK_KHR_dedicated_allocation"))
72         TCU_THROW(NotSupportedError, "Not supported");
73 
74     const InstanceInterface &vkInstance     = context.getInstanceInterface();
75     const VkPhysicalDevice vkPhysicalDevice = context.getPhysicalDevice();
76 
77     const VkBufferCreateInfo bufferParams{
78         VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
79         DE_NULL,                              // const void* pNext;
80         0u,                                   // VkBufferCreateFlags flags;
81         size,                                 // VkDeviceSize size;
82         usage,                                // VkBufferUsageFlags usage;
83         VK_SHARING_MODE_EXCLUSIVE,            // VkSharingMode sharingMode;
84         1u,                                   // uint32_t queueFamilyCount;
85         &queueFamilyIndex,                    // const uint32_t* pQueueFamilyIndices;
86     };
87 
88     buffer = vk::createBuffer(vk, vkDevice, &bufferParams, (const VkAllocationCallbacks *)DE_NULL);
89     memory = allocateDedicated(vkInstance, vk, vkPhysicalDevice, vkDevice, buffer.get(), requirement);
90     VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, memory->getMemory(), memory->getOffset()));
91 }
92 
createTestImage(tcu::IVec2 size,VkFormat format,Context & context,Allocator & allocator,Move<VkImage> & image,const MemoryRequirement & requirement,de::MovePtr<Allocation> & memory,VkImageTiling tiling) const93 void ImageSuballocation::createTestImage(tcu::IVec2 size, VkFormat format, Context &context, Allocator &allocator,
94                                          Move<VkImage> &image, const MemoryRequirement &requirement,
95                                          de::MovePtr<Allocation> &memory, VkImageTiling tiling) const
96 {
97     const VkDevice vkDevice         = context.getDevice();
98     const DeviceInterface &vk       = context.getDeviceInterface();
99     const uint32_t queueFamilyIndex = context.getUniversalQueueFamilyIndex();
100 
101     const VkImageCreateInfo colorImageParams = {
102         VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,          // VkStructureType sType;
103         DE_NULL,                                      // const void* pNext;
104         0u,                                           // VkImageCreateFlags flags;
105         VK_IMAGE_TYPE_2D,                             // VkImageType imageType;
106         format,                                       // VkFormat format;
107         {(uint32_t)size.x(), (uint32_t)size.y(), 1u}, // VkExtent3D extent;
108         1u,                                           // uint32_t mipLevels;
109         1u,                                           // uint32_t arraySize;
110         VK_SAMPLE_COUNT_1_BIT,                        // uint32_t samples;
111         tiling,                                       // VkImageTiling tiling;
112         (vk::VkImageUsageFlags)((tiling == VK_IMAGE_TILING_LINEAR) ?
113                                     VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT :
114                                     VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
115                                         VK_IMAGE_USAGE_TRANSFER_SRC_BIT), // VkImageUsageFlags usage;
116         VK_SHARING_MODE_EXCLUSIVE,                                        // VkSharingMode sharingMode;
117         1u,                                                               // uint32_t queueFamilyCount;
118         &queueFamilyIndex,                                                // const uint32_t* pQueueFamilyIndices;
119         VK_IMAGE_LAYOUT_UNDEFINED,                                        // VkImageLayout initialLayout;
120     };
121 
122     image  = createImage(vk, vkDevice, &colorImageParams);
123     memory = allocator.allocate(getImageMemoryRequirements(vk, vkDevice, *image), requirement);
124     VK_CHECK(vk.bindImageMemory(vkDevice, *image, memory->getMemory(), memory->getOffset()));
125 }
126 
createTestImage(tcu::IVec2 size,VkFormat format,Context & context,Allocator & allocator,Move<VkImage> & image,const MemoryRequirement & requirement,de::MovePtr<Allocation> & memory,VkImageTiling tiling) const127 void ImageDedicatedAllocation::createTestImage(tcu::IVec2 size, VkFormat format, Context &context, Allocator &allocator,
128                                                Move<VkImage> &image, const MemoryRequirement &requirement,
129                                                de::MovePtr<Allocation> &memory, VkImageTiling tiling) const
130 {
131     DE_UNREF(allocator);
132     if (!context.isDeviceFunctionalitySupported("VK_KHR_dedicated_allocation"))
133         TCU_THROW(NotSupportedError, "Not supported");
134 
135     const InstanceInterface &vkInstance     = context.getInstanceInterface();
136     const VkDevice vkDevice                 = context.getDevice();
137     const VkPhysicalDevice vkPhysicalDevice = context.getPhysicalDevice();
138     const DeviceInterface &vk               = context.getDeviceInterface();
139     const uint32_t queueFamilyIndex         = context.getUniversalQueueFamilyIndex();
140 
141     const VkImageCreateInfo colorImageParams = {
142         VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,          // VkStructureType sType;
143         DE_NULL,                                      // const void* pNext;
144         0u,                                           // VkImageCreateFlags flags;
145         VK_IMAGE_TYPE_2D,                             // VkImageType imageType;
146         format,                                       // VkFormat format;
147         {(uint32_t)size.x(), (uint32_t)size.y(), 1u}, // VkExtent3D extent;
148         1u,                                           // uint32_t mipLevels;
149         1u,                                           // uint32_t arraySize;
150         VK_SAMPLE_COUNT_1_BIT,                        // uint32_t samples;
151         tiling,                                       // VkImageTiling tiling;
152         (vk::VkImageUsageFlags)((tiling == VK_IMAGE_TILING_LINEAR) ?
153                                     VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT :
154                                     VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
155                                         VK_IMAGE_USAGE_TRANSFER_SRC_BIT), // VkImageUsageFlags usage;
156         VK_SHARING_MODE_EXCLUSIVE,                                        // VkSharingMode sharingMode;
157         1u,                                                               // uint32_t queueFamilyCount;
158         &queueFamilyIndex,                                                // const uint32_t* pQueueFamilyIndices;
159         VK_IMAGE_LAYOUT_UNDEFINED,                                        // VkImageLayout initialLayout;
160     };
161 
162     image  = createImage(vk, vkDevice, &colorImageParams);
163     memory = allocateDedicated(vkInstance, vk, vkPhysicalDevice, vkDevice, image.get(), requirement);
164     VK_CHECK(vk.bindImageMemory(vkDevice, *image, memory->getMemory(), memory->getOffset()));
165 }
166 
167 } // namespace api
168 } // namespace vkt
169