From b2af56c5c6dfe5119361e492a27dcc287b6b1b11 Mon Sep 17 00:00:00 2001 From: Unknown <0.tamas.marton@gmail.com> Date: Sat, 20 Oct 2018 15:46:12 +0100 Subject: [PATCH] now images always use bound mem (eg. swapchain) --- driver/common.c | 36 --------------------------- driver/common.h | 10 +++++--- driver/draw.c | 2 +- driver/modeset.c | 2 +- driver/resource.c | 62 +++++++++++++++++++++++++++++++++++++---------- driver/sync.c | 2 +- driver/wsi.c | 36 +++++++++++++++++++++++++-- 7 files changed, 93 insertions(+), 57 deletions(-) diff --git a/driver/common.c b/driver/common.c index 75401a5..0cc4924 100644 --- a/driver/common.c +++ b/driver/common.c @@ -47,42 +47,6 @@ uint32_t packVec4IntoABGR8(const float rgba[4]) return res; } -void createImageBO(_image* i) -{ - assert(i); - assert(i->format); - assert(i->width); - assert(i->height); - - uint32_t bpp = getFormatBpp(i->format); - uint32_t pixelSizeBytes = bpp / 8; - uint32_t nonPaddedSize = i->width * i->height * pixelSizeBytes; - i->paddedWidth = i->width; - i->paddedHeight = i->height; - - //need to pad to T format, as HW automatically chooses that - if(nonPaddedSize > 4096) - { - getPaddedTextureDimensionsT(i->width, i->height, bpp, &i->paddedWidth, &i->paddedHeight); - } - - 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); - - //set tiling to T if size > 4KB - if(nonPaddedSize > 4096) - { - int ret = vc4_bo_set_tiling(controlFd, i->handle, DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED); assert(ret); - i->tiling = VC4_TILING_FORMAT_T; - } - else - { - int ret = vc4_bo_set_tiling(controlFd, i->handle, DRM_FORMAT_MOD_LINEAR); assert(ret); - i->tiling = VC4_TILING_FORMAT_LT; - } -} - /* * https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#vkCmdClearColorImage * Color and depth/stencil images can be cleared outside a render pass instance using vkCmdClearColorImage or vkCmdClearDepthStencilImage, respectively. diff --git a/driver/common.h b/driver/common.h index 30a7f3f..e9e0ba0 100644 --- a/driver/common.h +++ b/driver/common.h @@ -130,21 +130,25 @@ typedef struct VkBuffer_T typedef struct VkImage_T { - uint32_t handle; + VkImageType type; //1d, 2d, 3d uint32_t fb; //needed for swapchain uint32_t width, height, depth; uint32_t paddedWidth, paddedHeight; uint32_t miplevels, samples; uint32_t layers; //number of views for multiview/stereo - uint32_t size; //overall size including padding + uint32_t size; //overall size including padding and alignment uint32_t stride; //the number of bytes from one row of pixels in memory to the next row of pixels in memory (aka pitch) uint32_t usageBits; uint32_t format; uint32_t imageSpace; - uint32_t tiling; + uint32_t tiling; //T or LT uint32_t needToClear; uint32_t clearColor[2]; uint32_t layout; + _deviceMemory* boundMem; + uint32_t boundOffset; + uint32_t alignment; + uint32_t concurrentAccess; //TODO uint32_t numQueueFamiliesWithAccess; uint32_t* queueFamiliesWithAccess; diff --git a/driver/draw.c b/driver/draw.c index cf8f650..8e250f6 100644 --- a/driver/draw.c +++ b/driver/draw.c @@ -179,7 +179,7 @@ void vkCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t ins //Insert image handle index clFit(commandBuffer, &commandBuffer->handlesCl, 4); - uint32_t imageIdx = clGetHandleIndex(&commandBuffer->handlesCl, i->handle); + uint32_t imageIdx = clGetHandleIndex(&commandBuffer->handlesCl, i->boundMem->bo); //fill out submit cl fields commandBuffer->submitCl.color_write.hindex = imageIdx; diff --git a/driver/modeset.c b/driver/modeset.c index 9ecc571..c1a08ba 100644 --- a/driver/modeset.c +++ b/driver/modeset.c @@ -341,7 +341,7 @@ int modeset_create_fb(int fd, _image *buf) // create framebuffer object for the dumb-buffer ret = drmModeAddFB(fd, buf->width, buf->height, 24, 32, buf->stride, - buf->handle, &buf->fb); + buf->boundMem->bo, &buf->fb); if (ret) { printf("cannot create framebuffer (%d): %m\n", errno); diff --git a/driver/resource.c b/driver/resource.c index 14cb387..1b0518e 100644 --- a/driver/resource.c +++ b/driver/resource.c @@ -109,11 +109,6 @@ void vkDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbac free(buf); } -void vkDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks* pAllocator) -{ - //TODO -} - void vkDestroyImageView(VkDevice device, VkImageView imageView, const VkAllocationCallbacks* pAllocator) { assert(device); @@ -190,16 +185,29 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateImage( } //TODO flags? - //TODO? pCreateInfo->imageType - i->format = pCreateInfo->format; + i->type = pCreateInfo->imageType; + i->fb = 0; //needed for modeset i->width = pCreateInfo->extent.width; i->height = pCreateInfo->extent.height; i->depth = pCreateInfo->extent.depth; + i->paddedWidth = 0; //when format is T + i->paddedHeight = 0; i->miplevels = pCreateInfo->mipLevels; + i->samples = pCreateInfo->samples; i->layers = pCreateInfo->arrayLayers; - i->samples = pCreateInfo->samples; //TODO? - i->tiling = pCreateInfo->tiling; //TODO? + i->size = 0; + i->stride = 0; i->usageBits = pCreateInfo->usage; + i->format = pCreateInfo->format; + i->imageSpace = 0; + i->tiling = pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR ? VC4_TILING_FORMAT_LT : VC4_TILING_FORMAT_T; + i->needToClear = 0; + i->clearColor[0] = i->clearColor[1] = 0; + i->layout = pCreateInfo->initialLayout; + i->boundMem = 0; + i->boundOffset = 0; + i->alignment = ARM_PAGE_SIZE; + i->concurrentAccess = pCreateInfo->sharingMode; //TODO? i->numQueueFamiliesWithAccess = pCreateInfo->queueFamilyIndexCount; if(i->numQueueFamiliesWithAccess > 0) @@ -209,9 +217,11 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateImage( return VK_ERROR_OUT_OF_HOST_MEMORY; memcpy(i->queueFamiliesWithAccess, pCreateInfo->pQueueFamilyIndices, sizeof(uint32_t) * i->numQueueFamiliesWithAccess); } - i->layout = pCreateInfo->initialLayout; - //TODO what else memory allocation, buffer object creation etc? + i->preTransformMode = 0; + i->compositeAlpha = 0; + i->presentMode = 0; + i->clipped = 0; *pImage = i; @@ -253,7 +263,24 @@ VKAPI_ATTR void VKAPI_CALL vkGetImageMemoryRequirements( _image* i = image; - //TODO?? + uint32_t bpp = getFormatBpp(i->format); + uint32_t pixelSizeBytes = bpp / 8; + uint32_t nonPaddedSize = i->width * i->height * pixelSizeBytes; + i->paddedWidth = i->width; + i->paddedHeight = i->height; + + //need to pad to T format, as HW automatically chooses that + if(nonPaddedSize > 4096) + { + getPaddedTextureDimensionsT(i->width, i->height, bpp, &i->paddedWidth, &i->paddedHeight); + } + + i->size = getBOAlignedSize(i->paddedWidth * i->paddedHeight * pixelSizeBytes); + i->stride = i->paddedWidth * pixelSizeBytes; + + pMemoryRequirements->alignment = ARM_PAGE_SIZE; + pMemoryRequirements->memoryTypeBits = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; //TODO + pMemoryRequirements->size = i->size; } /* @@ -269,7 +296,16 @@ VKAPI_ATTR VkResult VKAPI_CALL vkBindImageMemory( assert(image); assert(memory); - //TODO + _image* i = image; + _deviceMemory* m = memory; + + assert(!i->boundMem); + assert(memoryOffset < m->size); + assert(memoryOffset % i->alignment == 0); + assert(i->size <= m->size - memoryOffset); + + i->boundMem = m; + i->boundOffset = memoryOffset; return VK_SUCCESS; } diff --git a/driver/sync.c b/driver/sync.c index c3e35e9..ba1620d 100644 --- a/driver/sync.c +++ b/driver/sync.c @@ -185,7 +185,7 @@ VKAPI_ATTR void VKAPI_CALL vkCmdPipelineBarrier( 2); //tris clFit(commandBuffer, &commandBuffer->handlesCl, 4); - uint32_t idx = clGetHandleIndex(&commandBuffer->handlesCl, i->handle); + uint32_t idx = clGetHandleIndex(&commandBuffer->handlesCl, i->boundMem->bo); commandBuffer->submitCl.color_write.hindex = idx; commandBuffer->submitCl.color_write.offset = 0; commandBuffer->submitCl.color_write.flags = 0; diff --git a/driver/wsi.c b/driver/wsi.c index b9a12e2..f315c72 100644 --- a/driver/wsi.c +++ b/driver/wsi.c @@ -1,6 +1,8 @@ #include "common.h" #include "modeset.h" +#include "kernel/vc4_packet.h" + /* * Implementation of our RPI specific "extension" */ @@ -228,7 +230,37 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR( s->images[c].presentMode = pCreateInfo->presentMode; s->images[c].clipped = pCreateInfo->clipped; - createImageBO(&s->images[c]); + + VkMemoryRequirements mr; + vkGetImageMemoryRequirements(device, &s->images[c], &mr); + + VkMemoryAllocateInfo ai; + ai.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; + ai.allocationSize = mr.size; + for(int d = 0; d < numMemoryTypes; ++d) + { + if(memoryTypes[d].propertyFlags == mr.memoryTypeBits) + { + ai.memoryTypeIndex = d; + break; + } + } + + VkDeviceMemory mem; + vkAllocateMemory(device, &ai, 0, &mem); + + vkBindImageMemory(device, &s->images[c], mem, 0); + + //set tiling to T if size > 4KB + if(s->images[c].tiling == VC4_TILING_FORMAT_T) + { + int ret = vc4_bo_set_tiling(controlFd, s->images[c].boundMem->bo, DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED); assert(ret); + } + else + { + int ret = vc4_bo_set_tiling(controlFd, s->images[c].boundMem->bo, DRM_FORMAT_MOD_LINEAR); assert(ret); + } + int res = modeset_create_fb(controlFd, &s->images[c]); assert(res == 0); } @@ -372,7 +404,7 @@ VKAPI_ATTR void VKAPI_CALL vkDestroySwapchainKHR( for(int c = 0; c < s->numImages; ++c) { - vc4_bo_free(controlFd, s->images[c].handle, 0, s->images->size); + vkFreeMemory(device, s->images[c].boundMem, 0); modeset_destroy_fb(controlFd, &s->images[c]); }