1
0
mirror of https://github.com/Yours3lf/rpi-vk-driver.git synced 2024-12-04 16:24:15 +01:00
rpi-vk-driver/driver/memory.c

222 lines
5.7 KiB
C
Raw Normal View History

2018-10-15 23:37:09 +02:00
#include "common.h"
#include "kernel/vc4_packet.h"
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkGetPhysicalDeviceMemoryProperties
*/
2019-09-30 00:52:21 +02:00
void rpi_vkGetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice, VkPhysicalDeviceMemoryProperties* pMemoryProperties)
2018-10-15 23:37:09 +02:00
{
PROFILESTART(rpi_vkGetPhysicalDeviceMemoryProperties);
2018-10-15 23:37:09 +02:00
assert(physicalDevice);
assert(pMemoryProperties);
if(memoryHeaps[0].size == 0)
{
//TODO is this the correct way of getting amount of video mem?
char buf[4096];
int fd = open("/proc/meminfo", O_RDONLY);
read(fd, buf, 4096);
close(fd);
char* cma = strstr(buf, "CmaTotal");
char* cmaend = strstr(cma, "\n");
char cmaAmount[4096];
char* cmaPtr = cmaAmount;
while(cma != cmaend)
{
if(*cma >= '0' && *cma <= '9')
{
//number
*cmaPtr = *cma; //copy char
cmaPtr++;
}
cma++;
}
*cmaPtr = '\0';
unsigned amount = atoi(cmaAmount);
//printf("%i\n", amount);
//all heaps share the same memory
for(int c = 0; c < numMemoryHeaps; ++c)
{
2019-02-09 17:18:15 +01:00
memoryHeaps[c].size = amount * 1000; //kB to B
2018-10-15 23:37:09 +02:00
}
}
pMemoryProperties->memoryTypeCount = numMemoryTypes;
for(int c = 0; c < numMemoryTypes; ++c)
{
pMemoryProperties->memoryTypes[c] = memoryTypes[c];
}
pMemoryProperties->memoryHeapCount = numMemoryHeaps;
for(int c = 0; c < numMemoryHeaps; ++c)
{
pMemoryProperties->memoryHeaps[c] = memoryHeaps[c];
}
PROFILEEND(rpi_vkGetPhysicalDeviceMemoryProperties);
2018-10-15 23:37:09 +02:00
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkAllocateMemory
*/
2019-09-30 00:52:21 +02:00
VkResult rpi_vkAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo, const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory)
2018-10-15 23:37:09 +02:00
{
PROFILESTART(rpi_vkAllocateMemory);
2018-10-15 23:37:09 +02:00
assert(device);
assert(pAllocateInfo);
assert(pMemory);
uint32_t bo = vc4_bo_alloc(controlFd, pAllocateInfo->allocationSize, "vkAllocateMemory");
2018-10-15 23:37:09 +02:00
if(!bo)
{
PROFILEEND(rpi_vkAllocateMemory);
2018-10-15 23:37:09 +02:00
return VK_ERROR_OUT_OF_DEVICE_MEMORY;
}
_deviceMemory* mem = ALLOCATE(sizeof(_deviceMemory), 1, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
2018-10-15 23:37:09 +02:00
if(!mem)
{
PROFILEEND(rpi_vkAllocateMemory);
2018-10-15 23:37:09 +02:00
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
mem->bo = bo;
mem->size = pAllocateInfo->allocationSize;
mem->memTypeIndex = pAllocateInfo->memoryTypeIndex;
mem->mappedPtr = 0;
*pMemory = mem;
//TODO max number of allocations
PROFILEEND(rpi_vkAllocateMemory);
2018-10-15 23:37:09 +02:00
return VK_SUCCESS;
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkMapMemory
*/
2019-09-30 00:52:21 +02:00
VkResult rpi_vkMapMemory(VkDevice device, VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags, void** ppData)
2018-10-15 23:37:09 +02:00
{
PROFILESTART(rpi_vkMapMemory);
2018-10-15 23:37:09 +02:00
assert(device);
assert(memory);
assert(size);
assert(ppData);
assert(memoryTypes[((_deviceMemory*)memory)->memTypeIndex].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
assert(!((_deviceMemory*)memory)->mappedPtr);
assert(offset < ((_deviceMemory*)memory)->size);
if(size != VK_WHOLE_SIZE)
{
assert(size > 0);
assert(size <= ((_deviceMemory*)memory)->size - offset);
2020-04-17 14:04:28 +02:00
}else
{
size = ((_deviceMemory*)memory)->size;
2018-10-15 23:37:09 +02:00
}
//TODO check ppdata alignment
//TODO multiple instances?
void* ptr = vc4_bo_map(controlFd, ((_deviceMemory*)memory)->bo, offset, size);
2018-10-15 23:37:09 +02:00
if(!ptr)
{
PROFILEEND(rpi_vkMapMemory);
2018-10-15 23:37:09 +02:00
return VK_ERROR_MEMORY_MAP_FAILED;
}
((_deviceMemory*)memory)->mappedPtr = ptr;
((_deviceMemory*)memory)->mappedOffset = offset;
((_deviceMemory*)memory)->mappedSize = size;
*ppData = ptr;
PROFILEEND(rpi_vkMapMemory);
2018-10-15 23:37:09 +02:00
return VK_SUCCESS;
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkUnmapMemory
*/
2019-09-30 00:52:21 +02:00
void rpi_vkUnmapMemory(VkDevice device, VkDeviceMemory memory)
2018-10-15 23:37:09 +02:00
{
PROFILESTART(rpi_vkUnmapMemory);
2018-10-15 23:37:09 +02:00
assert(device);
assert(memory);
vc4_bo_unmap_unsynchronized(controlFd, ((_deviceMemory*)memory)->mappedPtr, ((_deviceMemory*)memory)->mappedSize);
2019-02-09 17:18:15 +01:00
((_deviceMemory*)memory)->mappedPtr = 0;
((_deviceMemory*)memory)->mappedSize = 0;
((_deviceMemory*)memory)->mappedOffset = 0;
PROFILEEND(rpi_vkUnmapMemory);
2018-10-15 23:37:09 +02:00
}
2019-09-30 00:52:21 +02:00
void rpi_vkFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator)
2018-10-15 23:37:09 +02:00
{
PROFILESTART(rpi_vkFreeMemory);
2018-10-15 23:37:09 +02:00
assert(device);
_deviceMemory* mem = memory;
2019-02-09 17:18:15 +01:00
if(mem)
{
vc4_bo_free(controlFd, mem->bo, mem->mappedPtr, mem->size);
2019-02-10 00:53:32 +01:00
FREE(mem);
2019-02-09 17:18:15 +01:00
}
PROFILEEND(rpi_vkFreeMemory);
2018-10-15 23:37:09 +02:00
}
2018-10-17 22:14:49 +02:00
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkFlushMappedMemoryRanges
*/
2019-09-30 00:52:21 +02:00
VKAPI_ATTR VkResult VKAPI_CALL rpi_vkFlushMappedMemoryRanges(
2018-10-17 22:14:49 +02:00
VkDevice device,
uint32_t memoryRangeCount,
const VkMappedMemoryRange* pMemoryRanges)
{
PROFILESTART(rpi_vkFlushMappedMemoryRanges);
2018-10-17 22:14:49 +02:00
//TODO
PROFILEEND(rpi_vkFlushMappedMemoryRanges);
2018-10-17 22:14:49 +02:00
return VK_SUCCESS;
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkInvalidateMappedMemoryRanges
*/
2019-09-30 00:52:21 +02:00
VKAPI_ATTR VkResult VKAPI_CALL rpi_vkInvalidateMappedMemoryRanges(
2018-10-17 22:14:49 +02:00
VkDevice device,
uint32_t memoryRangeCount,
const VkMappedMemoryRange* pMemoryRanges)
{
//TODO
return VK_SUCCESS;
}
2019-04-22 15:58:27 +02:00
2019-09-30 00:52:21 +02:00
VKAPI_ATTR void VKAPI_CALL rpi_vkGetPhysicalDeviceMemoryProperties2(
2019-04-22 15:58:27 +02:00
VkPhysicalDevice physicalDevice,
VkPhysicalDeviceMemoryProperties2* pMemoryProperties)
{
2019-09-08 00:30:52 +02:00
assert(physicalDevice);
2019-09-30 02:13:55 +02:00
rpi_vkGetPhysicalDeviceMemoryProperties(physicalDevice, pMemoryProperties);
2019-04-22 15:58:27 +02:00
}
2019-09-30 00:52:21 +02:00
VKAPI_ATTR void VKAPI_CALL rpi_vkGetDeviceMemoryCommitment(
2019-04-22 15:58:27 +02:00
VkDevice device,
VkDeviceMemory memory,
VkDeviceSize* pCommittedMemoryInBytes)
{
2019-09-08 00:30:52 +02:00
//TODO
2019-04-22 15:58:27 +02:00
}