1
0
mirror of https://github.com/Yours3lf/rpi-vk-driver.git synced 2025-02-20 17:54:17 +01:00

added some of the implementation for the triangle example

This commit is contained in:
Unknown 2018-09-09 15:45:07 +01:00
parent b0a6927058
commit c7988b5c09
5 changed files with 407 additions and 71 deletions

View File

@ -21,7 +21,7 @@ void createImageBO(_image* i)
getPaddedTextureDimensionsT(i->width, i->height, bpp, &i->paddedWidth, &i->paddedHeight);
}
i->size = i->paddedWidth * i->paddedHeight * pixelSizeBytes;
i->size = getBOAlignedSize(i->paddedWidth * i->paddedHeight * pixelSizeBytes);
i->stride = i->paddedWidth * pixelSizeBytes;
i->handle = vc4_bo_alloc(controlFd, i->size, "swapchain image"); assert(i->handle);
@ -327,6 +327,332 @@ uint32_t packVec4IntoABGR8(const float rgba[4])
}
}*/
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCmdBeginRenderPass
*/
void vkCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin, VkSubpassContents contents)
{
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCmdBindPipeline
*/
void vkCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline)
{
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCmdSetViewport
*/
void vkCmdSetViewport(VkCommandBuffer commandBuffer, uint32_t firstViewport, uint32_t viewportCount, const VkViewport* pViewports)
{
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCmdSetScissor
*/
void vkCmdSetScissor(VkCommandBuffer commandBuffer, uint32_t firstScissor, uint32_t scissorCount, const VkRect2D* pScissors)
{
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCmdDraw
*/
void vkCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance)
{
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCmdEndRenderPass
*/
void vkCmdEndRenderPass(VkCommandBuffer commandBuffer)
{
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCreateRenderPass
*/
VkResult vkCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass)
{
assert(device);
assert(pCreateInfo);
assert(pRenderPass);
assert(pAllocator == 0); //TODO allocators not supported yet
_renderpass* rp = malloc(sizeof(_renderpass));
if(!rp)
{
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
*pRenderPass = rp;
return VK_SUCCESS;
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCreateImageView
*/
VkResult vkCreateImageView(VkDevice device, const VkImageViewCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkImageView* pView)
{
return VK_SUCCESS;
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCreateFramebuffer
*/
VkResult vkCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer)
{
return VK_SUCCESS;
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCreateShaderModule
*/
VkResult vkCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkShaderModule* pShaderModule)
{
return VK_SUCCESS;
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCreateGraphicsPipelines
*/
VkResult vkCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount, const VkGraphicsPipelineCreateInfo* pCreateInfos, const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines)
{
return VK_SUCCESS;
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkGetPhysicalDeviceMemoryProperties
*/
void vkGetPhysicalDeviceMemoryProperties(VkPhysicalDevice physicalDevice, VkPhysicalDeviceMemoryProperties* pMemoryProperties)
{
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)
{
memoryHeaps[c].size = amount;
}
}
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];
}
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCmdBindVertexBuffers
*/
void vkCmdBindVertexBuffers(VkCommandBuffer commandBuffer, uint32_t firstBinding, uint32_t bindingCount, const VkBuffer* pBuffers, const VkDeviceSize* pOffsets)
{
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCreateBuffer
*/
VkResult vkCreateBuffer(VkDevice device, const VkBufferCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer)
{
assert(device);
assert(pCreateInfo);
assert(pBuffer);
assert(pAllocator == 0); //TODO
_buffer* buf = malloc(sizeof(_buffer));
if(!buf)
{
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
buf->size = pCreateInfo->size;
buf->usage = pCreateInfo->usage;
buf->boundMem = 0;
buf->alignment = ARM_PAGE_SIZE; //TODO
buf->alignedSize = getBOAlignedSize(buf->size);
*pBuffer = buf;
return VK_SUCCESS;
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkGetBufferMemoryRequirements
*/
void vkGetBufferMemoryRequirements(VkDevice device, VkBuffer buffer, VkMemoryRequirements* pMemoryRequirements)
{
assert(device);
assert(buffer);
assert(pMemoryRequirements);
pMemoryRequirements->alignment = ((_buffer*)buffer)->alignment;
pMemoryRequirements->size = ((_buffer*)buffer)->alignedSize;
pMemoryRequirements->memoryTypeBits = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; //TODO
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkAllocateMemory
*/
VkResult vkAllocateMemory(VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo, const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory)
{
assert(device);
assert(pAllocateInfo);
assert(pMemory);
assert(pAllocator == 0); //TODO
uint32_t bo = vc4_bo_alloc(controlFd, pAllocateInfo->allocationSize, "vkAllocateMemory");
if(!bo)
{
return VK_ERROR_OUT_OF_DEVICE_MEMORY;
}
_deviceMemory* mem = malloc(sizeof(_deviceMemory));
if(!mem)
{
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
return VK_SUCCESS;
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkMapMemory
*/
VkResult vkMapMemory(VkDevice device, VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags, void** ppData)
{
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);
}
//TODO check ppdata alignment
//TODO multiple instances?
void* ptr = vc4_bo_map(controlFd, ((_deviceMemory*)memory)->bo, offset, size);
if(!ptr)
{
return VK_ERROR_MEMORY_MAP_FAILED;
}
((_deviceMemory*)memory)->mappedPtr = ptr;
((_deviceMemory*)memory)->mappedOffset = offset;
((_deviceMemory*)memory)->mappedSize = size;
*ppData = ptr;
return VK_SUCCESS;
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkUnmapMemory
*/
void vkUnmapMemory(VkDevice device, VkDeviceMemory memory)
{
assert(device);
assert(memory);
vc4_bo_unmap_unsynchronized(controlFd, ((_deviceMemory*)memory)->mappedPtr, ((_deviceMemory*)memory)->mappedSize);
}
/*
* https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkBindBufferMemory
*/
VkResult vkBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize memoryOffset)
{
assert(device);
assert(buffer);
assert(memory);
_buffer* buf = buffer;
_deviceMemory* mem = memory;
assert(!buf->boundMem);
assert(memoryOffset < mem->size);
assert(memoryOffset % buf->alignment == 0);
assert(buf->alignedSize <= mem->size - memoryOffset);
buf->boundMem = mem;
buf->boundOffset = memoryOffset;
return VK_SUCCESS;
}
void vkDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks* pAllocator)
{
}
void vkFreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator)
{
}
void vkDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks* pAllocator)
{
@ -344,7 +670,14 @@ void vkDestroyFramebuffer(VkDevice device, VkFramebuffer framebuffer, const VkAl
void vkDestroyRenderPass(VkDevice device, VkRenderPass renderPass, const VkAllocationCallbacks* pAllocator)
{
assert(device);
assert(renderPass);
assert(pAllocator == 0); //TODO
//TODO?
free(renderPass);
}
void vkDestroyShaderModule(VkDevice device, VkShaderModule shaderModule, const VkAllocationCallbacks* pAllocator)
@ -356,58 +689,3 @@ void vkDestroyPipeline(VkDevice device, VkPipeline pipeline, const VkAllocationC
{
}
void vkCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo* pRenderPassBegin, VkSubpassContents contents)
{
}
void vkCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline)
{
}
void vkCmdSetViewport(VkCommandBuffer commandBuffer, uint32_t firstViewport, uint32_t viewportCount, const VkViewport* pViewports)
{
}
void vkCmdSetScissor(VkCommandBuffer commandBuffer, uint32_t firstScissor, uint32_t scissorCount, const VkRect2D* pScissors)
{
}
void vkCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance)
{
}
void vkCmdEndRenderPass(VkCommandBuffer commandBuffer)
{
}
VkResult vkCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass)
{
return VK_SUCCESS;
}
VkResult vkCreateImageView(VkDevice device, const VkImageViewCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkImageView* pView)
{
return VK_SUCCESS;
}
VkResult vkCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkFramebuffer* pFramebuffer)
{
return VK_SUCCESS;
}
VkResult vkCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkShaderModule* pShaderModule)
{
return VK_SUCCESS;
}
VkResult vkCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t createInfoCount, const VkGraphicsPipelineCreateInfo* pCreateInfos, const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines)
{
return VK_SUCCESS;
}

View File

@ -1,4 +1,4 @@
#pragma once
#pragma once
#include <drm/drm.h>
#include <drm/drm_fourcc.h>
@ -112,6 +112,30 @@ typedef struct VkSwapchain_T
VkSurfaceKHR surface;
} _swapchain;
typedef struct VkRenderPass_T
{
//TODO
} _renderpass;
typedef struct VkDeviceMemory_T
{
uint32_t size;
uint32_t bo;
uint32_t memTypeIndex;
void* mappedPtr;
uint32_t mappedOffset, mappedSize;
} _deviceMemory;
typedef struct VkBuffer_T
{
uint32_t size;
VkBufferUsageFlags usage;
_deviceMemory* boundMem;
uint32_t boundOffset;
uint32_t alignment;
uint32_t alignedSize;
} _buffer;
void getPaddedTextureDimensionsT(uint32_t width, uint32_t height, uint32_t bpp, uint32_t* paddedWidth, uint32_t* paddedHeight);
uint32_t getFormatBpp(VkFormat f);
uint32_t packVec4IntoABGR8(const float rgba[4]);

View File

@ -169,13 +169,12 @@ int vc4_bo_set_tiling(int fd, uint32_t bo, uint64_t mod)
return 1;
}
void* vc4_bo_map_unsynchronized(int fd, uint32_t bo, uint32_t size)
void* vc4_bo_map_unsynchronized(int fd, uint32_t bo, uint32_t offset, uint32_t size)
{
assert(fd);
assert(bo);
assert(size);
uint64_t offset;
int ret;
//if (bo->map)
@ -185,17 +184,16 @@ void* vc4_bo_map_unsynchronized(int fd, uint32_t bo, uint32_t size)
memset(&map, 0, sizeof(map));
map.handle = bo;
ret = drmIoctl(fd, DRM_IOCTL_VC4_MMAP_BO, &map);
offset = map.offset;
if (ret != 0) {
printf("Couldn't map unsync: %s\n", strerror(errno));
return 0;
}
void* mapPtr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
fd, offset);
fd, map.offset + offset);
if (mapPtr == MAP_FAILED) {
printf("mmap of bo %d (offset 0x%016llx, size %d) failed\n",
bo, (long long)offset, size);
bo, (long long)map.offset + offset, size);
return 0;
}
//VG(VALGRIND_MALLOCLIKE_BLOCK(bo->map, bo->size, 0, false));
@ -203,6 +201,15 @@ void* vc4_bo_map_unsynchronized(int fd, uint32_t bo, uint32_t size)
return mapPtr;
}
void vc4_bo_unmap_unsynchronized(int fd, void* ptr, uint32_t size)
{
assert(fd);
assert(ptr);
assert(size);
munmap(ptr, size);
}
int vc4_bo_wait(int fd, uint32_t bo, uint64_t timeout_ns)
{
assert(fd);
@ -281,6 +288,11 @@ int vc4_bo_flink(int fd, uint32_t bo, uint32_t *name)
return 1;
}
uint32_t getBOAlignedSize(uint32_t size)
{
return align(size, ARM_PAGE_SIZE);
}
uint32_t vc4_bo_alloc_shader(int fd, const void *data, uint32_t* size)
{
assert(fd);
@ -289,7 +301,7 @@ uint32_t vc4_bo_alloc_shader(int fd, const void *data, uint32_t* size)
int ret;
uint32_t alignedSize = align(*size, ARM_PAGE_SIZE);
uint32_t alignedSize = getBOAlignedSize(*size);
struct drm_vc4_create_shader_bo create = {
.size = alignedSize,
@ -336,8 +348,6 @@ uint32_t vc4_bo_alloc(int fd, uint32_t size, const char *name)
struct drm_vc4_create_bo create;
int ret;
uint32_t alignedSize = align(size, ARM_PAGE_SIZE);
/*bo = vc4_bo_from_cache(screen, size, name);
if (bo) {
if (dump_stats) {
@ -349,7 +359,7 @@ uint32_t vc4_bo_alloc(int fd, uint32_t size, const char *name)
}*/
memset(&create, 0, sizeof(create));
create.size = alignedSize;
create.size = size;
ret = drmIoctl(fd, DRM_IOCTL_VC4_CREATE_BO, &create);
uint32_t handle = create.handle;
@ -381,7 +391,7 @@ void vc4_bo_free(int fd, uint32_t bo, void* mappedAddr, uint32_t size)
assert(size);
if (mappedAddr) {
munmap(mappedAddr, size);
vc4_bo_unmap_unsynchronized(fd, mappedAddr, size);
//VG(VALGRIND_FREELIKE_BLOCK(bo->map, 0));
}
@ -479,13 +489,13 @@ int vc4_bo_get_dmabuf(int fd, uint32_t bo)
return boFd;
}
void* vc4_bo_map(int fd, uint32_t bo, uint32_t size)
void* vc4_bo_map(int fd, uint32_t bo, uint32_t offset, uint32_t size)
{
assert(fd);
assert(bo);
assert(size);
void* map = vc4_bo_map_unsynchronized(fd, bo, size);
void* map = vc4_bo_map_unsynchronized(fd, bo, offset, size);
//wait infinitely
int ok = vc4_bo_wait(fd, bo, WAIT_TIMEOUT_INFINITE);

View File

@ -45,7 +45,8 @@ int vc4_has_feature(int fd, uint32_t feature);
int vc4_test_tiling(int fd);
uint64_t vc4_bo_get_tiling(int fd, uint32_t bo, uint64_t mod);
int vc4_bo_set_tiling(int fd, uint32_t bo, uint64_t mod);
void* vc4_bo_map_unsynchronized(int fd, uint32_t bo, uint32_t size);
void* vc4_bo_map_unsynchronized(int fd, uint32_t bo, uint32_t offset, uint32_t size);
void vc4_bo_unmap_unsynchronized(int fd, void* ptr, uint32_t size);
//int vc4_bo_wait_ioctl(int fd, uint32_t handle, uint64_t timeout_ns);
int vc4_bo_wait(int fd, uint32_t bo, uint64_t timeout_ns);
//int vc4_seqno_wait_ioctl(int fd, uint64_t seqno, uint64_t timeout_ns);
@ -59,8 +60,9 @@ int vc4_bo_unpurgeable(int fd, uint32_t bo, int hasMadvise);
void vc4_bo_purgeable(int fd, uint32_t bo, int hasMadvise);
void vc4_bo_label(int fd, uint32_t bo, const char* name);
int vc4_bo_get_dmabuf(int fd, uint32_t bo);
void* vc4_bo_map(int fd, uint32_t bo, uint32_t size);
void* vc4_bo_map(int fd, uint32_t bo, uint32_t offset, uint32_t size);
void vc4_cl_submit(int fd, struct drm_vc4_submit_cl* submit, uint64_t* lastEmittedSeqno, uint64_t* lastFinishedSeqno);
uint32_t getBOAlignedSize(uint32_t size);
//TODO perfmon

View File

@ -259,3 +259,25 @@ static VkExtensionProperties deviceExtensions[] =
}
};
#define numDeviceExtensions (sizeof(deviceExtensions) / sizeof(VkExtensionProperties))
static VkMemoryType memoryTypes[] =
{
{
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
0
},
{
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
0
},
};
#define numMemoryTypes (sizeof(memoryTypes) / sizeof(VkMemoryType))
static VkMemoryHeap memoryHeaps[] =
{
{
0, //will be filled from /proc/meminfo
VK_MEMORY_HEAP_DEVICE_LOCAL_BIT
}
};
#define numMemoryHeaps (sizeof(memoryHeaps) / sizeof(VkMemoryHeap))