~mmach/netext73/mesa-ryzen

« back to all changes in this revision

Viewing changes to src/amd/vulkan/radv_device_memory.c

  • Committer: mmach
  • Date: 2023-11-02 21:31:35 UTC
  • Revision ID: netbit73@gmail.com-20231102213135-18d4tzh7tj0uz752
2023-11-02 22:11:57

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include "radv_private.h"
29
29
 
30
30
void
31
 
radv_device_memory_init(struct radv_device_memory *mem, struct radv_device *device,
32
 
                        struct radeon_winsys_bo *bo)
 
31
radv_device_memory_init(struct radv_device_memory *mem, struct radv_device *device, struct radeon_winsys_bo *bo)
33
32
{
34
33
   memset(mem, 0, sizeof(*mem));
35
34
   vk_object_base_init(&device->vk, &mem->base, VK_OBJECT_TYPE_DEVICE_MEMORY);
44
43
}
45
44
 
46
45
void
47
 
radv_free_memory(struct radv_device *device, const VkAllocationCallbacks *pAllocator,
48
 
                 struct radv_device_memory *mem)
 
46
radv_free_memory(struct radv_device *device, const VkAllocationCallbacks *pAllocator, struct radv_device_memory *mem)
49
47
{
50
48
   if (mem == NULL)
51
49
      return;
86
84
 
87
85
   assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
88
86
 
89
 
   const VkImportMemoryFdInfoKHR *import_info =
90
 
      vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
 
87
   const VkImportMemoryFdInfoKHR *import_info = vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
91
88
   const VkMemoryDedicatedAllocateInfo *dedicate_info =
92
89
      vk_find_struct_const(pAllocateInfo->pNext, MEMORY_DEDICATED_ALLOCATE_INFO);
93
90
   const VkExportMemoryAllocateInfo *export_info =
103
100
      vk_find_struct_const(pAllocateInfo->pNext, WSI_MEMORY_ALLOCATE_INFO_MESA);
104
101
 
105
102
   if (pAllocateInfo->allocationSize == 0 && !ahb_import_info &&
106
 
       !(export_info && (export_info->handleTypes &
107
 
                         VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID))) {
 
103
       !(export_info &&
 
104
         (export_info->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID))) {
108
105
      /* Apparently, this is allowed */
109
106
      *pMem = VK_NULL_HANDLE;
110
107
      return VK_SUCCESS;
111
108
   }
112
109
 
113
 
   mem =
114
 
      vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*mem), 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
 
110
   mem = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*mem), 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
115
111
   if (mem == NULL)
116
112
      return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
117
113
 
147
143
   if (replay_info && replay_info->opaqueCaptureAddress)
148
144
      replay_address = replay_info->opaqueCaptureAddress;
149
145
 
150
 
   unsigned priority = MIN2(RADV_BO_PRIORITY_APPLICATION_MAX - 1,
151
 
                            (int)(priority_float * RADV_BO_PRIORITY_APPLICATION_MAX));
 
146
   unsigned priority =
 
147
      MIN2(RADV_BO_PRIORITY_APPLICATION_MAX - 1, (int)(priority_float * RADV_BO_PRIORITY_APPLICATION_MAX));
152
148
 
153
149
   mem->user_ptr = NULL;
154
150
 
160
156
      result = radv_import_ahb_memory(device, mem, priority, ahb_import_info);
161
157
      if (result != VK_SUCCESS)
162
158
         goto fail;
163
 
   } else if (export_info && (export_info->handleTypes &
164
 
                              VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)) {
 
159
   } else if (export_info &&
 
160
              (export_info->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)) {
165
161
      result = radv_create_ahb_memory(device, mem, priority, pAllocateInfo);
166
162
      if (result != VK_SUCCESS)
167
163
         goto fail;
175
171
         close(import_info->fd);
176
172
      }
177
173
 
178
 
      if (mem->image && mem->image->plane_count == 1 &&
179
 
          !vk_format_is_depth_or_stencil(mem->image->vk.format) && mem->image->info.samples == 1 &&
180
 
          mem->image->vk.tiling != VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
 
174
      if (mem->image && mem->image->plane_count == 1 && !vk_format_is_depth_or_stencil(mem->image->vk.format) &&
 
175
          mem->image->vk.samples == 1 && mem->image->vk.tiling != VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
181
176
         struct radeon_bo_metadata metadata;
182
177
         device->ws->buffer_get_metadata(device->ws, mem->bo, &metadata);
183
178
 
184
 
         struct radv_image_create_info create_info = {.no_metadata_planes = true,
185
 
                                                      .bo_metadata = &metadata};
 
179
         struct radv_image_create_info create_info = {.no_metadata_planes = true, .bo_metadata = &metadata};
186
180
 
187
181
         /* This gives a basic ability to import radeonsi images
188
182
          * that don't have DCC. This is not guaranteed by any
189
183
          * spec and can be removed after we support modifiers. */
190
 
         result = radv_image_create_layout(device, create_info, NULL, mem->image);
 
184
         result = radv_image_create_layout(device, create_info, NULL, NULL, mem->image);
191
185
         if (result != VK_SUCCESS) {
192
186
            device->ws->buffer_destroy(device->ws, mem->bo);
193
187
            goto fail;
195
189
      }
196
190
   } else if (host_ptr_info) {
197
191
      assert(host_ptr_info->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT);
198
 
      result = device->ws->buffer_from_ptr(device->ws, host_ptr_info->pHostPointer,
199
 
                                           pAllocateInfo->allocationSize, priority, &mem->bo);
 
192
      result = device->ws->buffer_from_ptr(device->ws, host_ptr_info->pHostPointer, pAllocateInfo->allocationSize,
 
193
                                           priority, &mem->bo);
200
194
      if (result != VK_SUCCESS) {
201
195
         goto fail;
202
196
      } else {
206
200
      uint64_t alloc_size = align_u64(pAllocateInfo->allocationSize, 4096);
207
201
      uint32_t heap_index;
208
202
 
209
 
      heap_index =
210
 
         device->physical_device->memory_properties.memoryTypes[pAllocateInfo->memoryTypeIndex]
211
 
            .heapIndex;
 
203
      heap_index = device->physical_device->memory_properties.memoryTypes[pAllocateInfo->memoryTypeIndex].heapIndex;
212
204
      domain = device->physical_device->memory_domains[pAllocateInfo->memoryTypeIndex];
213
205
      flags |= device->physical_device->memory_flags[pAllocateInfo->memoryTypeIndex];
214
206
 
236
228
         flags |= RADEON_FLAG_ZERO_VRAM;
237
229
 
238
230
      if (device->overallocation_disallowed) {
239
 
         uint64_t total_size =
240
 
            device->physical_device->memory_properties.memoryHeaps[heap_index].size;
 
231
         uint64_t total_size = device->physical_device->memory_properties.memoryHeaps[heap_index].size;
241
232
 
242
233
         mtx_lock(&device->overallocation_mutex);
243
234
         if (device->allocated_memory_size[heap_index] + alloc_size > total_size) {
249
240
         mtx_unlock(&device->overallocation_mutex);
250
241
      }
251
242
 
252
 
      result = device->ws->buffer_create(device->ws, alloc_size,
253
 
                                         device->physical_device->rad_info.max_alignment, domain,
254
 
                                         flags, priority, replay_address, &mem->bo);
 
243
      result = device->ws->buffer_create(device->ws, alloc_size, device->physical_device->rad_info.max_alignment,
 
244
                                         domain, flags, priority, replay_address, &mem->bo);
255
245
 
256
246
      if (result != VK_SUCCESS) {
257
247
         if (device->overallocation_disallowed) {
335
325
}
336
326
 
337
327
VKAPI_ATTR VkResult VKAPI_CALL
338
 
radv_FlushMappedMemoryRanges(VkDevice _device, uint32_t memoryRangeCount,
339
 
                             const VkMappedMemoryRange *pMemoryRanges)
 
328
radv_FlushMappedMemoryRanges(VkDevice _device, uint32_t memoryRangeCount, const VkMappedMemoryRange *pMemoryRanges)
340
329
{
341
330
   return VK_SUCCESS;
342
331
}
343
332
 
344
333
VKAPI_ATTR VkResult VKAPI_CALL
345
 
radv_InvalidateMappedMemoryRanges(VkDevice _device, uint32_t memoryRangeCount,
346
 
                                  const VkMappedMemoryRange *pMemoryRanges)
 
334
radv_InvalidateMappedMemoryRanges(VkDevice _device, uint32_t memoryRangeCount, const VkMappedMemoryRange *pMemoryRanges)
347
335
{
348
336
   return VK_SUCCESS;
349
337
}
350
338
 
351
339
VKAPI_ATTR uint64_t VKAPI_CALL
352
 
radv_GetDeviceMemoryOpaqueCaptureAddress(VkDevice device,
353
 
                                         const VkDeviceMemoryOpaqueCaptureAddressInfo *pInfo)
 
340
radv_GetDeviceMemoryOpaqueCaptureAddress(VkDevice device, const VkDeviceMemoryOpaqueCaptureAddressInfo *pInfo)
354
341
{
355
342
   RADV_FROM_HANDLE(radv_device_memory, mem, pInfo->memory);
356
343
   return radv_buffer_get_va(mem->bo);
357
344
}
358
345
 
359
346
VKAPI_ATTR void VKAPI_CALL
360
 
radv_GetDeviceMemoryCommitment(VkDevice device, VkDeviceMemory memory,
361
 
                               VkDeviceSize *pCommittedMemoryInBytes)
 
347
radv_GetDeviceMemoryCommitment(VkDevice device, VkDeviceMemory memory, VkDeviceSize *pCommittedMemoryInBytes)
362
348
{
363
349
   *pCommittedMemoryInBytes = 0;
364
350
}