• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2016 Red Hat.
3  * Copyright © 2016 Bas Nieuwenhuizen
4  *
5  * based in part on anv driver which is:
6  * Copyright © 2015 Intel Corporation
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25  * IN THE SOFTWARE.
26  */
27 
28 #include "radv_private.h"
29 
30 #include "vk_buffer.h"
31 #include "vk_common_entrypoints.h"
32 
33 void
radv_buffer_init(struct radv_buffer * buffer,struct radv_device * device,struct radeon_winsys_bo * bo,uint64_t size,uint64_t offset)34 radv_buffer_init(struct radv_buffer *buffer, struct radv_device *device, struct radeon_winsys_bo *bo, uint64_t size,
35                  uint64_t offset)
36 {
37    VkBufferCreateInfo createInfo = {
38       .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
39       .size = size,
40    };
41 
42    vk_buffer_init(&device->vk, &buffer->vk, &createInfo);
43 
44    buffer->bo = bo;
45    buffer->offset = offset;
46 }
47 
48 void
radv_buffer_finish(struct radv_buffer * buffer)49 radv_buffer_finish(struct radv_buffer *buffer)
50 {
51    vk_buffer_finish(&buffer->vk);
52 }
53 
54 static void
radv_destroy_buffer(struct radv_device * device,const VkAllocationCallbacks * pAllocator,struct radv_buffer * buffer)55 radv_destroy_buffer(struct radv_device *device, const VkAllocationCallbacks *pAllocator, struct radv_buffer *buffer)
56 {
57    if ((buffer->vk.create_flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) && buffer->bo)
58       device->ws->buffer_destroy(device->ws, buffer->bo);
59 
60    radv_rmv_log_resource_destroy(device, (uint64_t)radv_buffer_to_handle(buffer));
61    radv_buffer_finish(buffer);
62    vk_free2(&device->vk.alloc, pAllocator, buffer);
63 }
64 
65 VkResult
radv_create_buffer(struct radv_device * device,const VkBufferCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkBuffer * pBuffer,bool is_internal)66 radv_create_buffer(struct radv_device *device, const VkBufferCreateInfo *pCreateInfo,
67                    const VkAllocationCallbacks *pAllocator, VkBuffer *pBuffer, bool is_internal)
68 {
69    struct radv_buffer *buffer;
70 
71    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
72 
73 #if DETECT_OS_ANDROID
74    /* reject buffers that are larger than maxBufferSize on Android, which
75     * might not have VK_KHR_maintenance4
76     */
77    if (pCreateInfo->size > RADV_MAX_MEMORY_ALLOCATION_SIZE)
78       return vk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY);
79 #endif
80 
81    buffer = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*buffer), 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
82    if (buffer == NULL)
83       return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
84 
85    vk_buffer_init(&device->vk, &buffer->vk, pCreateInfo);
86    buffer->bo = NULL;
87    buffer->offset = 0;
88 
89    if (pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) {
90       enum radeon_bo_flag flags = RADEON_FLAG_VIRTUAL;
91       if (pCreateInfo->flags & VK_BUFFER_CREATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT)
92          flags |= RADEON_FLAG_REPLAYABLE;
93       if (pCreateInfo->usage & VK_BUFFER_USAGE_2_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT)
94          flags |= RADEON_FLAG_32BIT;
95 
96       uint64_t replay_address = 0;
97       const VkBufferOpaqueCaptureAddressCreateInfo *replay_info =
98          vk_find_struct_const(pCreateInfo->pNext, BUFFER_OPAQUE_CAPTURE_ADDRESS_CREATE_INFO);
99       if (replay_info && replay_info->opaqueCaptureAddress)
100          replay_address = replay_info->opaqueCaptureAddress;
101 
102       VkResult result = device->ws->buffer_create(device->ws, align64(buffer->vk.size, 4096), 4096, 0, flags,
103                                                   RADV_BO_PRIORITY_VIRTUAL, replay_address, &buffer->bo);
104       if (result != VK_SUCCESS) {
105          radv_destroy_buffer(device, pAllocator, buffer);
106          return vk_error(device, result);
107       }
108       radv_rmv_log_bo_allocate(device, buffer->bo, buffer->vk.size, true);
109    }
110 
111    *pBuffer = radv_buffer_to_handle(buffer);
112    vk_rmv_log_buffer_create(&device->vk, false, *pBuffer);
113    if (buffer->bo)
114       radv_rmv_log_buffer_bind(device, *pBuffer);
115    return VK_SUCCESS;
116 }
117 
118 VKAPI_ATTR VkResult VKAPI_CALL
radv_CreateBuffer(VkDevice _device,const VkBufferCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkBuffer * pBuffer)119 radv_CreateBuffer(VkDevice _device, const VkBufferCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator,
120                   VkBuffer *pBuffer)
121 {
122    RADV_FROM_HANDLE(radv_device, device, _device);
123    return radv_create_buffer(device, pCreateInfo, pAllocator, pBuffer, false);
124 }
125 
126 VKAPI_ATTR void VKAPI_CALL
radv_DestroyBuffer(VkDevice _device,VkBuffer _buffer,const VkAllocationCallbacks * pAllocator)127 radv_DestroyBuffer(VkDevice _device, VkBuffer _buffer, const VkAllocationCallbacks *pAllocator)
128 {
129    RADV_FROM_HANDLE(radv_device, device, _device);
130    RADV_FROM_HANDLE(radv_buffer, buffer, _buffer);
131 
132    if (!buffer)
133       return;
134 
135    radv_destroy_buffer(device, pAllocator, buffer);
136 }
137 
138 VKAPI_ATTR VkResult VKAPI_CALL
radv_BindBufferMemory2(VkDevice _device,uint32_t bindInfoCount,const VkBindBufferMemoryInfo * pBindInfos)139 radv_BindBufferMemory2(VkDevice _device, uint32_t bindInfoCount, const VkBindBufferMemoryInfo *pBindInfos)
140 {
141    RADV_FROM_HANDLE(radv_device, device, _device);
142 
143    for (uint32_t i = 0; i < bindInfoCount; ++i) {
144       RADV_FROM_HANDLE(radv_device_memory, mem, pBindInfos[i].memory);
145       RADV_FROM_HANDLE(radv_buffer, buffer, pBindInfos[i].buffer);
146       VkBindMemoryStatusKHR *status = (void *)vk_find_struct_const(&pBindInfos[i], BIND_MEMORY_STATUS_KHR);
147 
148       if (status)
149          *status->pResult = VK_SUCCESS;
150 
151       if (mem->alloc_size) {
152          VkBufferMemoryRequirementsInfo2 info = {
153             .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2,
154             .buffer = pBindInfos[i].buffer,
155          };
156          VkMemoryRequirements2 reqs = {
157             .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
158          };
159 
160          vk_common_GetBufferMemoryRequirements2(_device, &info, &reqs);
161 
162          if (pBindInfos[i].memoryOffset + reqs.memoryRequirements.size > mem->alloc_size) {
163             if (status)
164                *status->pResult = VK_ERROR_UNKNOWN;
165             return vk_errorf(device, VK_ERROR_UNKNOWN, "Device memory object too small for the buffer.\n");
166          }
167       }
168 
169       buffer->bo = mem->bo;
170       buffer->offset = pBindInfos[i].memoryOffset;
171       radv_rmv_log_buffer_bind(device, pBindInfos[i].buffer);
172    }
173    return VK_SUCCESS;
174 }
175 
176 static void
radv_get_buffer_memory_requirements(struct radv_device * device,VkDeviceSize size,VkBufferCreateFlags flags,VkBufferUsageFlags2KHR usage,VkMemoryRequirements2 * pMemoryRequirements)177 radv_get_buffer_memory_requirements(struct radv_device *device, VkDeviceSize size, VkBufferCreateFlags flags,
178                                     VkBufferUsageFlags2KHR usage, VkMemoryRequirements2 *pMemoryRequirements)
179 {
180    pMemoryRequirements->memoryRequirements.memoryTypeBits =
181       ((1u << device->physical_device->memory_properties.memoryTypeCount) - 1u) &
182       ~device->physical_device->memory_types_32bit;
183 
184    /* Allow 32-bit address-space for DGC usage, as this buffer will contain
185     * cmd buffer upload buffers, and those get passed to shaders through 32-bit
186     * pointers.
187     *
188     * We only allow it with this usage set, to "protect" the 32-bit address space
189     * from being overused. The actual requirement is done as part of
190     * vkGetGeneratedCommandsMemoryRequirementsNV. (we have to make sure their
191     * intersection is non-zero at least)
192     */
193    if ((usage & VK_BUFFER_USAGE_2_INDIRECT_BUFFER_BIT_KHR) && radv_uses_device_generated_commands(device))
194       pMemoryRequirements->memoryRequirements.memoryTypeBits |= device->physical_device->memory_types_32bit;
195 
196    /* Force 32-bit address-space for descriptor buffers usage because they are passed to shaders
197     * through 32-bit pointers.
198     */
199    if (usage &
200        (VK_BUFFER_USAGE_2_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT | VK_BUFFER_USAGE_2_SAMPLER_DESCRIPTOR_BUFFER_BIT_EXT))
201       pMemoryRequirements->memoryRequirements.memoryTypeBits = device->physical_device->memory_types_32bit;
202 
203    if (flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT)
204       pMemoryRequirements->memoryRequirements.alignment = 4096;
205    else
206       pMemoryRequirements->memoryRequirements.alignment = 16;
207 
208    /* Top level acceleration structures need the bottom 6 bits to store
209     * the root ids of instances. The hardware also needs bvh nodes to
210     * be 64 byte aligned.
211     */
212    if (usage & VK_BUFFER_USAGE_2_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR)
213       pMemoryRequirements->memoryRequirements.alignment = MAX2(pMemoryRequirements->memoryRequirements.alignment, 64);
214 
215    pMemoryRequirements->memoryRequirements.size = align64(size, pMemoryRequirements->memoryRequirements.alignment);
216 
217    vk_foreach_struct (ext, pMemoryRequirements->pNext) {
218       switch (ext->sType) {
219       case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
220          VkMemoryDedicatedRequirements *req = (VkMemoryDedicatedRequirements *)ext;
221          req->requiresDedicatedAllocation = false;
222          req->prefersDedicatedAllocation = req->requiresDedicatedAllocation;
223          break;
224       }
225       default:
226          break;
227       }
228    }
229 }
230 
231 static const VkBufferUsageFlagBits2KHR
radv_get_buffer_usage_flags(const VkBufferCreateInfo * pCreateInfo)232 radv_get_buffer_usage_flags(const VkBufferCreateInfo *pCreateInfo)
233 {
234    const VkBufferUsageFlags2CreateInfoKHR *flags2 =
235       vk_find_struct_const(pCreateInfo->pNext, BUFFER_USAGE_FLAGS_2_CREATE_INFO_KHR);
236    return flags2 ? flags2->usage : pCreateInfo->usage;
237 }
238 
239 VKAPI_ATTR void VKAPI_CALL
radv_GetDeviceBufferMemoryRequirements(VkDevice _device,const VkDeviceBufferMemoryRequirements * pInfo,VkMemoryRequirements2 * pMemoryRequirements)240 radv_GetDeviceBufferMemoryRequirements(VkDevice _device, const VkDeviceBufferMemoryRequirements *pInfo,
241                                        VkMemoryRequirements2 *pMemoryRequirements)
242 {
243    RADV_FROM_HANDLE(radv_device, device, _device);
244    const VkBufferUsageFlagBits2KHR usage_flags = radv_get_buffer_usage_flags(pInfo->pCreateInfo);
245 
246    radv_get_buffer_memory_requirements(device, pInfo->pCreateInfo->size, pInfo->pCreateInfo->flags, usage_flags,
247                                        pMemoryRequirements);
248 }
249 
250 VKAPI_ATTR VkDeviceAddress VKAPI_CALL
radv_GetBufferDeviceAddress(VkDevice device,const VkBufferDeviceAddressInfo * pInfo)251 radv_GetBufferDeviceAddress(VkDevice device, const VkBufferDeviceAddressInfo *pInfo)
252 {
253    RADV_FROM_HANDLE(radv_buffer, buffer, pInfo->buffer);
254    return radv_buffer_get_va(buffer->bo) + buffer->offset;
255 }
256 
257 VKAPI_ATTR uint64_t VKAPI_CALL
radv_GetBufferOpaqueCaptureAddress(VkDevice device,const VkBufferDeviceAddressInfo * pInfo)258 radv_GetBufferOpaqueCaptureAddress(VkDevice device, const VkBufferDeviceAddressInfo *pInfo)
259 {
260    RADV_FROM_HANDLE(radv_buffer, buffer, pInfo->buffer);
261    return buffer->bo ? radv_buffer_get_va(buffer->bo) + buffer->offset : 0;
262 }
263