From a7595895a979010dda145016974327e16298442b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Sat, 28 May 2022 13:01:42 +0200 Subject: [PATCH] vulkan memories without VMA --- CMakeLists.txt | 39 +++++---- include/vkh.h | 218 +++++++++++++++++++++++------------------------ src/vkh_buffer.c | 34 +++++--- src/vkh_buffer.h | 2 +- src/vkh_device.c | 7 +- src/vkh_image.c | 9 +- src/vkhelpers.c | 26 +++++- 7 files changed, 188 insertions(+), 147 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8cc5fab..931e850 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,8 +14,6 @@ IF(NOT CMAKE_BUILD_TYPE) SET(CMAKE_BUILD_TYPE Release) ENDIF() -MESSAGE(STATUS "${CMAKE_BUILD_TYPE} build.") - IF (CYGWIN) SET (CMAKE_FIND_LIBRARY_PREFIXES "") SET (CMAKE_FIND_LIBRARY_SUFFIXES ".lib" ".LIB" ".dll" ".DLL") @@ -25,23 +23,22 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug") ADD_DEFINITIONS (-DDEBUG) OPTION(ENABLE_VALIDATION "enable vulkan validation layer" ON) IF (UNIX) - SET(CMAKE_${LANG}_FLAGS "-Wall -Wno-extra -Wno-unknown-pragmas") + ADD_COMPILE_OPTIONS(-Wall -Wno-extra -Wno-unknown-pragmas) ELSEIF (MSVC) - SET(CMAKE_${LANG}_FLAGS "/W4 /wd4204 /wd4221 /wd4100") + ADD_COMPILE_OPTIONS(/W4 /wd4204 /wd4221 /wd4100) ENDIF() ELSE() UNSET(ENABLE_VALIDATION CACHE) IF (UNIX) - SET(CMAKE_${LANG}_FLAGS "-w -flto -fvisibility=hidden") - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w -flto -fvisibility=hidden") + ADD_COMPILE_OPTIONS(-w -flto -fvisibility=hidden) ELSEIF(MSVC) - SET(CMAKE_${LANG}_FLAGS "/W0") + ADD_COMPILE_OPTIONS(/W0) + IF (${LANG} EQUAL "C" AND MSVC) + ADD_COMPILE_OPTIONS(/TC) + ENDIF () ENDIF() ENDIF() -if (${LANG} EQUAL "C" AND MSVC) - SET(CMAKE_C_FLAGS "/TC ${CMAKE_C_FLAGS}") -endif () IF (ENABLE_VALIDATION) ADD_DEFINITIONS (-DVKH_USE_VALIDATION) @@ -101,36 +98,38 @@ if (VKH_USE_VMA) ${CMAKE_CURRENT_SOURCE_DIR}/include ${Vulkan_INCLUDE_DIRS} ) + SET (VKH_LIB_SRC $ $) +ELSE() + SET (VKH_LIB_SRC $) ENDIF() ADD_LIBRARY(vkh_obj OBJECT ${VKH_SRC}) SET_PROPERTY(TARGET vkh_obj PROPERTY POSITION_INDEPENDENT_CODE ON) TARGET_INCLUDE_DIRECTORIES(vkh_obj - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/src - PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/include - ${Vulkan_INCLUDE_DIRS} + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/src + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/include + ${Vulkan_INCLUDE_DIRS} ) IF (vkh_has_root_project) IF (VKH_BUILD_SHARED_LIBS) - ADD_LIBRARY("${PROJECT_NAME}" SHARED $ $) + ADD_LIBRARY("${PROJECT_NAME}" SHARED ${VKH_LIB_SRC}) TARGET_COMPILE_DEFINITIONS("${PROJECT_NAME}" PUBLIC -DVKH_SHARED_BUILD) setup_lib ("${PROJECT_NAME}") ELSE() - ADD_LIBRARY("${PROJECT_NAME}" STATIC $ $) + ADD_LIBRARY("${PROJECT_NAME}" STATIC ${VKH_LIB_SRC}) setup_lib ("${PROJECT_NAME}") ENDIF() ELSE() - ADD_LIBRARY("${PROJECT_NAME}_shared" SHARED $ $) + ADD_LIBRARY("${PROJECT_NAME}_shared" SHARED ${VKH_LIB_SRC}) SET_PROPERTY(TARGET "${PROJECT_NAME}_shared" PROPERTY POSITION_INDEPENDENT_CODE ON) TARGET_COMPILE_DEFINITIONS("${PROJECT_NAME}_shared" PUBLIC -DVKH_SHARED_BUILD) setup_lib ("${PROJECT_NAME}_shared") - ADD_LIBRARY("${PROJECT_NAME}_static" STATIC $ $) + ADD_LIBRARY("${PROJECT_NAME}_static" STATIC ${VKH_LIB_SRC}) target_compile_definitions("${PROJECT_NAME}_static" PUBLIC -DVKH_STATIC_BUILD) SET_PROPERTY(TARGET "${PROJECT_NAME}_static" PROPERTY POSITION_INDEPENDENT_CODE OFF) setup_lib ("${PROJECT_NAME}_static") ENDIF() - diff --git a/include/vkh.h b/include/vkh.h index 9c693ac..279c056 100644 --- a/include/vkh.h +++ b/include/vkh.h @@ -34,69 +34,69 @@ extern "C" { typedef enum VkhMemoryUsage { - /** No intended memory usage specified. - Use other members of VmaAllocationCreateInfo to specify your requirements. - */ - VKH_MEMORY_USAGE_UNKNOWN = 0, - /** Memory will be used on device only, so fast access from the device is preferred. - It usually means device-local GPU (video) memory. - No need to be mappable on host. - It is roughly equivalent of `D3D12_HEAP_TYPE_DEFAULT`. - - Usage: - - - Resources written and read by device, e.g. images used as attachments. - - Resources transferred from host once (immutable) or infrequently and read by - device multiple times, e.g. textures to be sampled, vertex buffers, uniform - (constant) buffers, and majority of other types of resources used on GPU. - - Allocation may still end up in `HOST_VISIBLE` memory on some implementations. - In such case, you are free to map it. - You can use #VMA_ALLOCATION_CREATE_MAPPED_BIT with this usage type. - */ - VKH_MEMORY_USAGE_GPU_ONLY = 1, - /** Memory will be mappable on host. - It usually means CPU (system) memory. - Guarantees to be `HOST_VISIBLE` and `HOST_COHERENT`. - CPU access is typically uncached. Writes may be write-combined. - Resources created in this pool may still be accessible to the device, but access to them can be slow. - It is roughly equivalent of `D3D12_HEAP_TYPE_UPLOAD`. - - Usage: Staging copy of resources used as transfer source. - */ - VKH_MEMORY_USAGE_CPU_ONLY = 2, - /** - Memory that is both mappable on host (guarantees to be `HOST_VISIBLE`) and preferably fast to access by GPU. - CPU access is typically uncached. Writes may be write-combined. - - Usage: Resources written frequently by host (dynamic), read by device. E.g. textures, vertex buffers, uniform buffers updated every frame or every draw call. - */ - VKH_MEMORY_USAGE_CPU_TO_GPU = 3, - /** Memory mappable on host (guarantees to be `HOST_VISIBLE`) and cached. - It is roughly equivalent of `D3D12_HEAP_TYPE_READBACK`. - - Usage: - - - Resources written by device, read by host - results of some computations, e.g. screen capture, average scene luminance for HDR tone mapping. - - Any resources read or accessed randomly on host, e.g. CPU-side copy of vertex buffer used as source of transfer, but also used for collision detection. - */ - VKH_MEMORY_USAGE_GPU_TO_CPU = 4, - /** CPU memory - memory that is preferably not `DEVICE_LOCAL`, but also not guaranteed to be `HOST_VISIBLE`. - - Usage: Staging copy of resources moved from GPU memory to CPU memory as part - of custom paging/residency mechanism, to be moved back to GPU memory when needed. - */ - VKH_MEMORY_USAGE_CPU_COPY = 5, - /** Lazily allocated GPU memory having `VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT`. - Exists mostly on mobile platforms. Using it on desktop PC or other GPUs with no such memory type present will fail the allocation. - - Usage: Memory for transient attachment images (color attachments, depth attachments etc.), created with `VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT`. - - Allocations with this usage are always created as dedicated - it implies #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT. - */ - VKH_MEMORY_USAGE_GPU_LAZILY_ALLOCATED = 6, - - VKH_MEMORY_USAGE_MAX_ENUM = 0x7FFFFFFF + /** No intended memory usage specified. + Use other members of VmaAllocationCreateInfo to specify your requirements. + */ + VKH_MEMORY_USAGE_UNKNOWN = 0, + /** Memory will be used on device only, so fast access from the device is preferred. + It usually means device-local GPU (video) memory. + No need to be mappable on host. + It is roughly equivalent of `D3D12_HEAP_TYPE_DEFAULT`. + + Usage: + + - Resources written and read by device, e.g. images used as attachments. + - Resources transferred from host once (immutable) or infrequently and read by + device multiple times, e.g. textures to be sampled, vertex buffers, uniform + (constant) buffers, and majority of other types of resources used on GPU. + + Allocation may still end up in `HOST_VISIBLE` memory on some implementations. + In such case, you are free to map it. + You can use #VMA_ALLOCATION_CREATE_MAPPED_BIT with this usage type. + */ + VKH_MEMORY_USAGE_GPU_ONLY = 1, + /** Memory will be mappable on host. + It usually means CPU (system) memory. + Guarantees to be `HOST_VISIBLE` and `HOST_COHERENT`. + CPU access is typically uncached. Writes may be write-combined. + Resources created in this pool may still be accessible to the device, but access to them can be slow. + It is roughly equivalent of `D3D12_HEAP_TYPE_UPLOAD`. + + Usage: Staging copy of resources used as transfer source. + */ + VKH_MEMORY_USAGE_CPU_ONLY = 2, + /** + Memory that is both mappable on host (guarantees to be `HOST_VISIBLE`) and preferably fast to access by GPU. + CPU access is typically uncached. Writes may be write-combined. + + Usage: Resources written frequently by host (dynamic), read by device. E.g. textures, vertex buffers, uniform buffers updated every frame or every draw call. + */ + VKH_MEMORY_USAGE_CPU_TO_GPU = 3, + /** Memory mappable on host (guarantees to be `HOST_VISIBLE`) and cached. + It is roughly equivalent of `D3D12_HEAP_TYPE_READBACK`. + + Usage: + + - Resources written by device, read by host - results of some computations, e.g. screen capture, average scene luminance for HDR tone mapping. + - Any resources read or accessed randomly on host, e.g. CPU-side copy of vertex buffer used as source of transfer, but also used for collision detection. + */ + VKH_MEMORY_USAGE_GPU_TO_CPU = 4, + /** CPU memory - memory that is preferably not `DEVICE_LOCAL`, but also not guaranteed to be `HOST_VISIBLE`. + + Usage: Staging copy of resources moved from GPU memory to CPU memory as part + of custom paging/residency mechanism, to be moved back to GPU memory when needed. + */ + VKH_MEMORY_USAGE_CPU_COPY = 5, + /** Lazily allocated GPU memory having `VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT`. + Exists mostly on mobile platforms. Using it on desktop PC or other GPUs with no such memory type present will fail the allocation. + + Usage: Memory for transient attachment images (color attachments, depth attachments etc.), created with `VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT`. + + Allocations with this usage are always created as dedicated - it implies #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT. + */ + VKH_MEMORY_USAGE_GPU_LAZILY_ALLOCATED = 6, + + VKH_MEMORY_USAGE_MAX_ENUM = 0x7FFFFFFF } VkhMemoryUsage; #include @@ -113,25 +113,25 @@ typedef enum VkhMemoryUsage #define VKH_MO 0x00100000 #define VKH_GO 0x40000000 -#define VK_CHECK_RESULT(f) \ -{ \ - VkResult res = (f); \ - if (res != VK_SUCCESS) \ - { \ - fprintf(stderr, "Fatal : VkResult is %d in %s at line %d\n", res, __FILE__, __LINE__); \ - assert(res == VK_SUCCESS); \ - } \ +#define VK_CHECK_RESULT(f) \ +{ \ + VkResult res = (f); \ + if (res != VK_SUCCESS) \ + { \ + fprintf(stderr, "Fatal : VkResult is %d in %s at line %d\n", res, __FILE__, __LINE__); \ + assert(res == VK_SUCCESS); \ + } \ } #ifndef vkh_public - #ifdef VKH_SHARED_BUILD - #if (defined(_WIN32) || defined(_WIN64)) - #define vkh_public __declspec(dllimport) - #else - #define vkh_public __attribute__((visibility("default"))) - #endif - #else - #define vkh_public - #endif + #ifdef VKH_SHARED_BUILD + #if (defined(_WIN32) || defined(_WIN64)) + #define vkh_public __declspec(dllimport) + #else + #define vkh_public __attribute__((visibility("default"))) + #endif + #else + #define vkh_public + #endif #endif typedef struct _vkh_app_t* VkhApp; @@ -147,7 +147,7 @@ typedef struct _vkh_presenter_t* VkhPresenter; *************/ vkh_public VkhApp vkh_app_create (uint32_t version_major, uint32_t version_minor, - const char* app_name, uint32_t enabledLayersCount, const char **enabledLayers, uint32_t ext_count, const char* extentions[]); + const char* app_name, uint32_t enabledLayersCount, const char **enabledLayers, uint32_t ext_count, const char* extentions[]); vkh_public void vkh_app_destroy (VkhApp app); vkh_public @@ -159,8 +159,8 @@ vkh_public void vkh_app_free_phyinfos (uint32_t count, VkhPhyInfo* infos); vkh_public void vkh_app_enable_debug_messenger (VkhApp app, VkDebugUtilsMessageTypeFlagsEXT typeFlags, - VkDebugUtilsMessageSeverityFlagsEXT severityFlags, - PFN_vkDebugUtilsMessengerCallbackEXT callback); + VkDebugUtilsMessageSeverityFlagsEXT severityFlags, + PFN_vkDebugUtilsMessengerCallbackEXT callback); vkh_public VkPhysicalDeviceProperties vkh_app_get_phy_properties (VkhApp app, uint32_t phyIndex); @@ -184,11 +184,11 @@ vkh_public VkQueueFamilyProperties* vkh_phyinfo_get_queues_props(VkhPhyInfo phy, uint32_t* qCount); vkh_public -bool vkh_phyinfo_create_queues (VkhPhyInfo phy, int qFam, uint32_t queueCount, const float* queue_priorities, VkDeviceQueueCreateInfo* const qInfo); +bool vkh_phyinfo_create_queues (VkhPhyInfo phy, int qFam, uint32_t queueCount, const float* queue_priorities, VkDeviceQueueCreateInfo* const qInfo); vkh_public bool vkh_phyinfo_create_presentable_queues (VkhPhyInfo phy, uint32_t queueCount, const float* queue_priorities, VkDeviceQueueCreateInfo* const qInfo); vkh_public -bool phy_info_create_graphic_queues (VkhPhyInfo phy, uint32_t queueCount, const float* queue_priorities, VkDeviceQueueCreateInfo* const qInfo); +bool phy_info_create_graphic_queues (VkhPhyInfo phy, uint32_t queueCount, const float* queue_priorities, VkDeviceQueueCreateInfo* const qInfo); vkh_public bool vkh_phyinfo_create_transfer_queues (VkhPhyInfo phy, uint32_t queueCount, const float* queue_priorities, VkDeviceQueueCreateInfo* const qInfo); vkh_public @@ -200,17 +200,17 @@ bool vkh_phyinfo_try_get_extension_properties (VkhPhyInfo phy, const char* name, * VkhDevice * *************/ vkh_public -VkhDevice vkh_device_create (VkhApp app, VkhPhyInfo phyInfo, VkDeviceCreateInfo* pDevice_info); +VkhDevice vkh_device_create (VkhApp app, VkhPhyInfo phyInfo, VkDeviceCreateInfo* pDevice_info); vkh_public -VkhDevice vkh_device_import (VkInstance inst, VkPhysicalDevice phy, VkDevice vkDev); +VkhDevice vkh_device_import (VkInstance inst, VkPhysicalDevice phy, VkDevice vkDev); vkh_public -void vkh_device_destroy (VkhDevice dev); +void vkh_device_destroy (VkhDevice dev); vkh_public void vkh_device_init_debug_utils (VkhDevice dev); vkh_public -VkDevice vkh_device_get_vkdev(VkhDevice dev); +VkDevice vkh_device_get_vkdev (VkhDevice dev); vkh_public -VkPhysicalDevice vkh_device_get_phy (VkhDevice dev); +VkPhysicalDevice vkh_device_get_phy (VkhDevice dev); /** * @brief Retrieve @ref VkhApp instance used to create this VkhDevice. * @param dev @@ -224,7 +224,7 @@ void vkh_device_set_object_name (VkhDevice dev, VkObjectType objectType, uint64_ vkh_public VkSampler vkh_device_create_sampler (VkhDevice dev, VkFilter magFilter, VkFilter minFilter, - VkSamplerMipmapMode mipmapMode, VkSamplerAddressMode addressMode); + VkSamplerMipmapMode mipmapMode, VkSamplerAddressMode addressMode); vkh_public void vkh_device_destroy_sampler (VkhDevice dev, VkSampler sampler); @@ -233,8 +233,8 @@ void vkh_device_destroy_sampler (VkhDevice dev, VkSampler sampler); ****************/ vkh_public VkhPresenter vkh_presenter_create (VkhDevice dev, uint32_t presentQueueFamIdx, VkSurfaceKHR surface, - uint32_t width, uint32_t height, - VkFormat preferedFormat, VkPresentModeKHR presentMode); + uint32_t width, uint32_t height, + VkFormat preferedFormat, VkPresentModeKHR presentMode); vkh_public void vkh_presenter_destroy (VkhPresenter r); vkh_public @@ -254,29 +254,29 @@ vkh_public VkhImage vkh_image_import (VkhDevice pDev, VkImage vkImg, VkFormat format, uint32_t width, uint32_t height); vkh_public VkhImage vkh_image_create (VkhDevice pDev, VkFormat format, uint32_t width, uint32_t height, VkImageTiling tiling, - VkhMemoryUsage memprops, VkImageUsageFlags usage); + VkhMemoryUsage memprops, VkImageUsageFlags usage); vkh_public VkhImage vkh_image_ms_create (VkhDevice pDev, VkFormat format, VkSampleCountFlagBits num_samples, uint32_t width, uint32_t height, - VkhMemoryUsage memprops, VkImageUsageFlags usage); + VkhMemoryUsage memprops, VkImageUsageFlags usage); vkh_public VkhImage vkh_tex2d_array_create (VkhDevice pDev, VkFormat format, uint32_t width, uint32_t height, uint32_t layers, - VkhMemoryUsage memprops, VkImageUsageFlags usage); + VkhMemoryUsage memprops, VkImageUsageFlags usage); vkh_public void vkh_image_set_sampler (VkhImage img, VkSampler sampler); vkh_public void vkh_image_create_descriptor(VkhImage img, VkImageViewType viewType, VkImageAspectFlags aspectFlags, VkFilter magFilter, VkFilter minFilter, - VkSamplerMipmapMode mipmapMode, VkSamplerAddressMode addressMode); + VkSamplerMipmapMode mipmapMode, VkSamplerAddressMode addressMode); vkh_public void vkh_image_create_view (VkhImage img, VkImageViewType viewType, VkImageAspectFlags aspectFlags); vkh_public void vkh_image_create_sampler (VkhImage img, VkFilter magFilter, VkFilter minFilter, - VkSamplerMipmapMode mipmapMode, VkSamplerAddressMode addressMode); + VkSamplerMipmapMode mipmapMode, VkSamplerAddressMode addressMode); vkh_public void vkh_image_set_layout (VkCommandBuffer cmdBuff, VkhImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout, - VkImageLayout new_image_layout, VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages); + VkImageLayout new_image_layout, VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages); vkh_public void vkh_image_set_layout_subres(VkCommandBuffer cmdBuff, VkhImage image, VkImageSubresourceRange subresourceRange, VkImageLayout old_image_layout, - VkImageLayout new_image_layout, VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages); + VkImageLayout new_image_layout, VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages); vkh_public void vkh_image_destroy_sampler (VkhImage img); vkh_public @@ -308,14 +308,14 @@ VkDescriptorImageInfo vkh_image_get_descriptor(VkhImage img, VkImageLayout ima *************/ vkh_public void vkh_buffer_init (VkhDevice pDev, VkBufferUsageFlags usage, - VkhMemoryUsage memprops, VkDeviceSize size, VkhBuffer buff, bool mapped); + VkhMemoryUsage memprops, VkDeviceSize size, VkhBuffer buff, bool mapped); vkh_public VkhBuffer vkh_buffer_create (VkhDevice pDev, VkBufferUsageFlags usage, - VkhMemoryUsage memprops, VkDeviceSize size); + VkhMemoryUsage memprops, VkDeviceSize size); vkh_public void vkh_buffer_destroy (VkhBuffer buff); vkh_public -void vkh_buffer_resize (VkhBuffer buff, VkDeviceSize newSize); +void vkh_buffer_resize (VkhBuffer buff, VkDeviceSize newSize, bool mapped); vkh_public void vkh_buffer_reset (VkhBuffer buff); vkh_public @@ -342,10 +342,10 @@ vkh_public VkResult vkh_timeline_wait (VkhDevice dev, VkSemaphore timeline, const uint64_t wait); vkh_public void vkh_cmd_submit_timelined (VkhQueue queue, VkCommandBuffer *pCmdBuff, VkSemaphore timeline, - const uint64_t wait, const uint64_t signal); + const uint64_t wait, const uint64_t signal); vkh_public void vkh_cmd_submit_timelined2 (VkhQueue queue, VkCommandBuffer *pCmdBuff, VkSemaphore timelines[2], - const uint64_t waits[2], const uint64_t signals[2]); + const uint64_t waits[2], const uint64_t signals[2]); vkh_public VkEvent vkh_event_create (VkhDevice dev); @@ -363,7 +363,7 @@ vkh_public void vkh_cmd_submit (VkhQueue queue, VkCommandBuffer *pCmdBuff, VkFence fence); vkh_public void vkh_cmd_submit_with_semaphores(VkhQueue queue, VkCommandBuffer *pCmdBuff, VkSemaphore waitSemaphore, - VkSemaphore signalSemaphore, VkFence fence); + VkSemaphore signalSemaphore, VkFence fence); vkh_public void vkh_cmd_label_start (VkCommandBuffer cmd, const char* name, const float color[4]); @@ -377,7 +377,7 @@ VkShaderModule vkh_load_module(VkDevice dev, const char* path); vkh_public bool vkh_memory_type_from_properties(VkPhysicalDeviceMemoryProperties* memory_properties, uint32_t typeBits, - VkFlags requirements_mask, uint32_t *typeIndex); + VkhMemoryUsage requirements_mask, uint32_t *typeIndex); vkh_public char * read_spv(const char *filename, size_t *psize); vkh_public @@ -388,10 +388,10 @@ void dumpLayerExts (); vkh_public void set_image_layout(VkCommandBuffer cmdBuff, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout, - VkImageLayout new_image_layout, VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages); + VkImageLayout new_image_layout, VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages); vkh_public void set_image_layout_subres(VkCommandBuffer cmdBuff, VkImage image, VkImageSubresourceRange subresourceRange, VkImageLayout old_image_layout, - VkImageLayout new_image_layout, VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages); + VkImageLayout new_image_layout, VkPipelineStageFlags src_stages, VkPipelineStageFlags dest_stages); ///////////////////// vkh_public VkhQueue vkh_queue_create (VkhDevice dev, uint32_t familyIndex, uint32_t qIndex); diff --git a/src/vkh_buffer.c b/src/vkh_buffer.c index 69e3fbc..3595076 100644 --- a/src/vkh_buffer.c +++ b/src/vkh_buffer.c @@ -23,21 +23,20 @@ #include "vkh_device.h" #ifndef VKH_USE_VMA -void _set_size_and_map(VkhDevice pDev, VkBufferUsageFlags usage, VkMemoryPropertyFlags memoryPropertyFlags, VkDeviceSize size, VkhBuffer buff){ +void _set_size_and_bind(VkhDevice pDev, VkBufferUsageFlags usage, VkhMemoryUsage memoryUsage, VkDeviceSize size, VkhBuffer buff){ VkMemoryRequirements memReq; vkGetBufferMemoryRequirements(pDev->dev, buff->buffer, &memReq); VkMemoryAllocateInfo memAllocInfo = { .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = memReq.size }; - assert(vkh_memory_type_from_properties(&pDev->phyMemProps, memReq.memoryTypeBits,memoryPropertyFlags, &memAllocInfo.memoryTypeIndex)); + vkh_memory_type_from_properties(&pDev->phyMemProps, memReq.memoryTypeBits, memoryUsage, &memAllocInfo.memoryTypeIndex); VK_CHECK_RESULT(vkAllocateMemory(pDev->dev, &memAllocInfo, NULL, &buff->memory)); buff->alignment = memReq.alignment; buff->size = memAllocInfo.allocationSize; buff->usageFlags = usage; - buff->memprops = memoryPropertyFlags; + buff->memprops = memoryUsage; VK_CHECK_RESULT(vkBindBufferMemory(buff->pDev->dev, buff->buffer, buff->memory, 0)); - VK_CHECK_RESULT(vkMapMemory(buff->pDev->dev, buff->memory, 0, VK_WHOLE_SIZE, 0, &buff->mapped)); } #endif @@ -54,7 +53,11 @@ void vkh_buffer_init(VkhDevice pDev, VkBufferUsageFlags usage, VkhMemoryUsage me buff->allocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT; VK_CHECK_RESULT(vmaCreateBuffer(pDev->allocator, pInfo, &buff->allocCreateInfo, &buff->buffer, &buff->alloc, &buff->allocInfo)); #else + VK_CHECK_RESULT(vkCreateBuffer(pDev->dev, pInfo, NULL, &buff->buffer)); + _set_size_and_bind (pDev, usage, memprops, size, buff); buff->memprops = memprops; + if (mapped) + VK_CHECK_RESULT(vkMapMemory(buff->pDev->dev, buff->memory, 0, VK_WHOLE_SIZE, 0, &buff->mapped)); #endif } @@ -69,6 +72,9 @@ void vkh_buffer_reset(VkhBuffer buff){ #ifdef VKH_USE_VMA vmaDestroyBuffer(buff->pDev->allocator, buff->buffer, buff->alloc); #else + vkDestroyBuffer(buff->pDev->dev, buff->buffer, NULL); + if (buff->memory) + vkFreeMemory(buff->pDev->dev, buff->memory, NULL); #endif } void vkh_buffer_destroy(VkhBuffer buff){ @@ -76,22 +82,24 @@ void vkh_buffer_destroy(VkhBuffer buff){ #ifdef VKH_USE_VMA vmaDestroyBuffer(buff->pDev->allocator, buff->buffer, buff->alloc); #else + vkDestroyBuffer(buff->pDev->dev, buff->buffer, NULL); + if (buff->memory) + vkFreeMemory(buff->pDev->dev, buff->memory, NULL); #endif free(buff); buff = NULL; } -void vkh_buffer_resize(VkhBuffer buff, VkDeviceSize newSize){ - if (buff->buffer) +void vkh_buffer_resize(VkhBuffer buff, VkDeviceSize newSize, bool mapped){ + vkh_buffer_reset(buff); + buff->infos.size = newSize; #ifdef VKH_USE_VMA - vmaDestroyBuffer(buff->pDev->allocator, buff->buffer, buff->alloc); VK_CHECK_RESULT(vmaCreateBuffer(buff->pDev->allocator, &buff->infos, &buff->allocCreateInfo, &buff->buffer, &buff->alloc, &buff->allocInfo)); #else + VK_CHECK_RESULT(vkCreateBuffer(buff->pDev->dev, &buff->infos, NULL, &buff->buffer)); + _set_size_and_bind (buff->pDev, buff->usageFlags, buff->memprops, buff->infos.size, buff); + if (mapped) + VK_CHECK_RESULT(vkMapMemory(buff->pDev->dev, buff->memory, 0, VK_WHOLE_SIZE, 0, &buff->mapped)); #endif -#ifdef VKH_USE_VMA -#else - buff->memprops = memprops; -#endif - } VkDescriptorBufferInfo vkh_buffer_get_descriptor (VkhBuffer buff){ @@ -107,6 +115,7 @@ VkResult vkh_buffer_map(VkhBuffer buff){ #ifdef VKH_USE_VMA return vmaMapMemory(buff->pDev->allocator, buff->alloc, &buff->mapped); #else + return vkMapMemory(buff->pDev->dev, buff->memory, 0, VK_WHOLE_SIZE, 0, &buff->mapped); #endif } void vkh_buffer_unmap(VkhBuffer buff){ @@ -115,6 +124,7 @@ void vkh_buffer_unmap(VkhBuffer buff){ #else if (!buff->mapped) return; + vkUnmapMemory (buff->pDev->dev, buff->memory); buff->mapped = NULL; #endif } diff --git a/src/vkh_buffer.h b/src/vkh_buffer.h index 9b5c0ff..08486a5 100644 --- a/src/vkh_buffer.h +++ b/src/vkh_buffer.h @@ -44,7 +44,7 @@ typedef struct _vkh_buffer_t { VkDeviceMemory memory; VkDeviceSize size; VkBufferUsageFlags usageFlags; - VkMemoryPropertyFlags memprops; + VkhMemoryUsage memprops; #endif VkDescriptorBufferInfo descriptor; VkDeviceSize alignment; diff --git a/src/vkh_device.c b/src/vkh_device.c index 9f5bfcb..98ec48c 100644 --- a/src/vkh_device.c +++ b/src/vkh_device.c @@ -46,12 +46,14 @@ VkhDevice vkh_device_import (VkInstance inst, VkPhysicalDevice phy, VkDevice vkD dev->instance = inst; vkGetPhysicalDeviceMemoryProperties (phy, &dev->phyMemProps); - +#ifdef VKH_USE_VMA VmaAllocatorCreateInfo allocatorInfo = { .physicalDevice = phy, .device = vkDev }; vmaCreateAllocator(&allocatorInfo, &dev->allocator); +#else +#endif return dev; } @@ -94,7 +96,10 @@ void vkh_device_destroy_sampler (VkhDevice dev, VkSampler sampler) { vkDestroySampler (dev->dev, sampler, NULL); } void vkh_device_destroy (VkhDevice dev) { +#ifdef VKH_USE_VMA vmaDestroyAllocator (dev->allocator); +#else +#endif vkDestroyDevice (dev->dev, NULL); free (dev); } diff --git a/src/vkh_image.c b/src/vkh_image.c index c2ff84c..b795a31 100644 --- a/src/vkh_image.c +++ b/src/vkh_image.c @@ -57,11 +57,12 @@ VkhImage _vkh_image_create (VkhDevice pDev, VkImageType imageType, VmaAllocationCreateInfo allocInfo = { .usage = (VmaMemoryUsage)memprops }; VK_CHECK_RESULT(vmaCreateImage (pDev->allocator, pInfo, &allocInfo, &img->image, &img->alloc, &img->allocInfo)); #else + VK_CHECK_RESULT(vkCreateImage(pDev->dev, pInfo, NULL, &img->image)); VkMemoryRequirements memReq; vkGetImageMemoryRequirements(pDev->dev, img->image, &memReq); VkMemoryAllocateInfo memAllocInfo = { .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = memReq.size }; - assert(vkh_memory_type_from_properties(&pDev->phyMemProps, memReq.memoryTypeBits, memprops,&memAllocInfo.memoryTypeIndex)); + vkh_memory_type_from_properties(&pDev->phyMemProps, memReq.memoryTypeBits, memprops,&memAllocInfo.memoryTypeIndex); VK_CHECK_RESULT(vkAllocateMemory(pDev->dev, &memAllocInfo, NULL, &img->memory)); VK_CHECK_RESULT(vkBindImageMemory(pDev->dev, img->image, img->memory, 0)); #endif @@ -91,11 +92,15 @@ void vkh_image_destroy(VkhImage img) if(img->sampler != VK_NULL_HANDLE) vkDestroySampler (img->pDev->dev,img->sampler, NULL); - if (!img->imported) + if (!img->imported) { #ifdef VKH_USE_VMA vmaDestroyImage (img->pDev->allocator, img->image, img->alloc); #else + vkDestroyImage (img->pDev->dev, img->image, NULL); + vkFreeMemory (img->pDev->dev, img->memory, NULL); + #endif + } free(img); diff --git a/src/vkhelpers.c b/src/vkhelpers.c index e110015..3dff6e1 100644 --- a/src/vkhelpers.c +++ b/src/vkhelpers.c @@ -254,12 +254,34 @@ void set_image_layout_subres(VkCommandBuffer cmdBuff, VkImage image, VkImageSubr vkCmdPipelineBarrier(cmdBuff, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); } -bool vkh_memory_type_from_properties(VkPhysicalDeviceMemoryProperties* memory_properties, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) { +bool vkh_memory_type_from_properties(VkPhysicalDeviceMemoryProperties* memory_properties, uint32_t typeBits, VkhMemoryUsage memUsage, uint32_t *typeIndex) { + VkMemoryPropertyFlagBits memFlags = 0; + switch(memUsage) { + case VKH_MEMORY_USAGE_GPU_ONLY: + memFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; + break; + case VKH_MEMORY_USAGE_CPU_ONLY: + memFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + break; + case VKH_MEMORY_USAGE_CPU_TO_GPU: + memFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + break; + case VKH_MEMORY_USAGE_GPU_TO_CPU: + memFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT; + break; + case VKH_MEMORY_USAGE_CPU_COPY: + memFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; + break; + case VKH_MEMORY_USAGE_GPU_LAZILY_ALLOCATED: + memFlags = VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT; + break; + } + // Search memtypes to find first index with those properties for (uint32_t i = 0; i < memory_properties->memoryTypeCount; i++) { if (CHECK_BIT(typeBits, i)) { // Type is available, does it match user properties? - if ((memory_properties->memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) { + if ((memory_properties->memoryTypes[i].propertyFlags & memFlags) == memFlags) { *typeIndex = i; return true; } -- 2.47.3