From 00d9bdf568af94c7b7c03a4861a4b7acf0afa249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Thu, 21 Sep 2023 07:15:37 +0200 Subject: [PATCH] vkvg_device_create_info wip --- include/vkvg.h | 25 ++-- src/vkvg_device.c | 222 +++++++++++++++++------------------- tests/bezier.c | 12 +- tests/common/test.c | 42 +++++-- tests/inverse_colinear.c | 15 ++- tests/offscreen.c | 5 +- tests/path_extents.c | 13 ++- tests/vkvg-svg/svg-viewer.c | 13 ++- 8 files changed, 192 insertions(+), 155 deletions(-) diff --git a/include/vkvg.h b/include/vkvg.h index ba719c6e..2ac2c31c 100644 --- a/include/vkvg.h +++ b/include/vkvg.h @@ -550,8 +550,6 @@ void vkvg_matrix_get_scale (const vkvg_matrix_t *matrix, float *sx, float *sy); * Structure used to pass parameter to the device creation method. * * @code - * x_new = xx * x + xy * y + x0; - * y_new = yx * x + yy * y + y0; * @endcode * * @samples: sample count. @@ -560,14 +558,18 @@ void vkvg_matrix_get_scale (const vkvg_matrix_t *matrix, float *sx, float *sy); * @vkdev: vulkan logical device, may be null to create a new one. * @qFamIdx: graphic queue family index, ignored if vkdev is NULL. * @qIndex: queue index, ignored if vkdev is NULL. + * @deferredResolve: If true, the final simple sampled image of the surface will only be resolved on demand + * when calling @ref vkvg_surface_get_vk_image or by explicitly calling @ref vkvg_multisample_surface_resolve. + * If false, multisampled image is resolved on each draw operation. */ typedef struct { - VkSampleCountFlags samples; - VkInstance inst; + VkSampleCountFlags samples; + bool deferredResolve; + VkInstance inst; VkPhysicalDevice phy; VkDevice vkdev; uint32_t qFamIdx; - uint32_t qIndex; + uint32_t qIndex; }vkvg_device_create_info_t; /** * @brief Set device ready for multithreading. @@ -629,10 +631,7 @@ VkvgDevice vkvg_device_create (vkvg_device_create_info_t* info); * @param qFamIdx Queue family Index of the graphic queue used for drawing operations. * @param qIndex Index of the queue into the choosen familly, 0 in general. * @return The handle of the created vkvg device, or null if an error occured. - */ -vkvg_public -VkvgDevice vkvg_device_create_from_vk (VkInstance inst, VkPhysicalDevice phy, VkDevice vkdev, uint32_t qFamIdx, uint32_t qIndex); -/** + * @brief Create a new multisampled vkvg device. * * This function allows to create vkvg device for working with multisampled surfaces. @@ -646,12 +645,10 @@ VkvgDevice vkvg_device_create_from_vk (VkInstance inst, VkPhysicalDevice phy, Vk * @param qFamIdx Queue family Index of the graphic queue used for drawing operations. * @param qIndex Index of the queue into the choosen familly, 0 in general. * @param samples The sample count that will be setup for the surfaces created by this device. - * @param deferredResolve If true, the final simple sampled image of the surface will only be resolved on demand - * when calling @ref vkvg_surface_get_vk_image or by explicitly calling @ref vkvg_multisample_surface_resolve. If false, multisampled image is resolved on each draw operation. + * @return The handle of the created vkvg device, or null if an error occured. - */ -vkvg_public -VkvgDevice vkvg_device_create_from_vk_multisample (VkInstance inst, VkPhysicalDevice phy, VkDevice vkdev, uint32_t qFamIdx, uint32_t qIndex, VkSampleCountFlags samples, bool deferredResolve); + + /** * @brief Decrement the reference count of the device by 1. Release all its resources if count reaches 0. * diff --git a/src/vkvg_device.c b/src/vkvg_device.c index b5e5b459..7783fb5f 100644 --- a/src/vkvg_device.c +++ b/src/vkvg_device.c @@ -47,17 +47,17 @@ void vkvg_device_set_context_cache_size (VkvgDevice dev, uint32_t maxCount) { } dev->cachedContextLast = cur; } -void _device_init (VkvgDevice dev, VkInstance inst, VkPhysicalDevice phy, VkDevice vkdev, uint32_t qFamIdx, uint32_t qIndex, VkSampleCountFlags samples, bool deferredResolve) { - dev->vkDev = vkdev; - dev->phy = phy; - dev->instance = inst; +void _device_init (VkvgDevice dev, const vkvg_device_create_info_t* info) { + dev->vkDev = info->vkdev; + dev->phy = info->phy; + dev->instance = info->inst; dev->hdpi = 72; - dev->vdpi = 72; - dev->samples= samples; + dev->vdpi = 72; + dev->samples= info->samples; if (dev->samples == VK_SAMPLE_COUNT_1_BIT) dev->deferredResolve = false; - else - dev->deferredResolve = deferredResolve; + else + dev->deferredResolve = info->deferredResolve; dev->cachedContextMaxCount = VKVG_MAX_CACHED_CONTEXT_COUNT; @@ -78,16 +78,16 @@ void _device_init (VkvgDevice dev, VkInstance inst, VkPhysicalDevice phy, VkDevi VkhPhyInfo phyInfos = vkh_phyinfo_create (dev->phy, NULL); - dev->phyMemProps = phyInfos->memProps; - dev->gQueue = vkh_queue_create ((VkhDevice)dev, qFamIdx, qIndex); + dev->phyMemProps = phyInfos->memProps; + dev->gQueue = vkh_queue_create ((VkhDevice)dev, info->qFamIdx, info->qIndex); //mtx_init (&dev->gQMutex, mtx_plain); vkh_phyinfo_destroy (phyInfos); #ifdef VKH_USE_VMA VmaAllocatorCreateInfo allocatorInfo = { - .physicalDevice = phy, - .device = vkdev + .physicalDevice = info->phy, + .device = info->vkdev }; vmaCreateAllocator(&allocatorInfo, (VmaAllocator*)&dev->allocator); #endif @@ -279,114 +279,98 @@ VkvgDevice vkvg_device_create (vkvg_device_create_info_t* info) { dev->references = 1; - const char* enabledExts [10]; - const char* enabledLayers[10]; - uint32_t enabledExtsCount = 0, enabledLayersCount = 0, phyCount = 0; + if (!info->vkdev) { + const char* enabledExts [10]; + const char* enabledLayers[10]; + uint32_t enabledExtsCount = 0, enabledLayersCount = 0, phyCount = 0; + + vkh_layers_check_init(); + + #ifdef VKVG_USE_VALIDATION + if (vkh_layer_is_present("VK_LAYER_KHRONOS_validation")) + enabledLayers[enabledLayersCount++] = "VK_LAYER_KHRONOS_validation"; + #endif + + #ifdef VKVG_USE_RENDERDOC + if (vkh_layer_is_present("VK_LAYER_RENDERDOC_Capture")) + enabledLayers[enabledLayersCount++] = "VK_LAYER_RENDERDOC_Capture"; + #endif + vkh_layers_check_release(); + + vkvg_get_required_instance_extensions (enabledExts, &enabledExtsCount); + + #ifdef VK_VERSION_1_2 + VkhApp app = vkh_app_create(1, 2, "vkvg", enabledLayersCount, enabledLayers, enabledExtsCount, enabledExts); + #else + VkhApp app = vkh_app_create(1, 1, "vkvg", enabledLayersCount, enabledLayers, enabledExtsCount, enabledExts); + #endif + + #if defined(DEBUG) && defined (VKVG_DBG_UTILS) + vkh_app_enable_debug_messenger(app + , VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT + , VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT + , NULL); + #endif + + VkhPhyInfo* phys = vkh_app_get_phyinfos (app, &phyCount, VK_NULL_HANDLE); + if (phyCount == 0) { + dev->status = VKVG_STATUS_DEVICE_ERROR; + vkh_app_destroy (app); + return dev; + } + + VkhPhyInfo pi = 0; + if (!_device_try_get_phyinfo(phys, phyCount, VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU, &pi)) + if (!_device_try_get_phyinfo(phys, phyCount, VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU, &pi)) + pi = phys[0]; + + if (!(pi->properties.limits.framebufferColorSampleCounts & info->samples)) { + LOG(VKVG_LOG_ERR, "CREATE Device failed: sample count not supported: %d\n", info->samples); + dev->status = VKVG_STATUS_DEVICE_ERROR; + vkh_app_free_phyinfos (phyCount, phys); + vkh_app_destroy (app); + return dev; + } + + uint32_t qCount = 0; + float qPriorities[] = {0.0}; + VkDeviceQueueCreateInfo pQueueInfos[] = { {0},{0},{0} }; + + if (vkh_phyinfo_create_queues (pi, pi->gQueue, 1, qPriorities, &pQueueInfos[qCount])) + qCount++; + + enabledExtsCount=0; + + if (vkvg_get_required_device_extensions (pi->phy, enabledExts, &enabledExtsCount) != VKVG_STATUS_SUCCESS){ + dev->status = VKVG_STATUS_DEVICE_ERROR; + vkh_app_free_phyinfos (phyCount, phys); + vkh_app_destroy (app); + return dev; + } + + VkPhysicalDeviceFeatures enabledFeatures = {0}; + const void* pNext = vkvg_get_device_requirements (&enabledFeatures); + + VkDeviceCreateInfo device_info = { .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, + .queueCreateInfoCount = qCount, + .pQueueCreateInfos = (VkDeviceQueueCreateInfo*)&pQueueInfos, + .enabledExtensionCount = enabledExtsCount, + .ppEnabledExtensionNames = enabledExts, + .pEnabledFeatures = &enabledFeatures, + .pNext = pNext}; + dev->vkhDev = vkh_device_create(app, pi, &device_info); + + vkh_app_free_phyinfos (phyCount, phys); + + info->inst = vkh_app_get_inst(app); + info->phy = vkh_device_get_phy(dev->vkhDev); + info->vkdev = vkh_device_get_vkdev(dev->vkhDev); + info->qFamIdx = pi->gQueue; + info->qIndex = 0; + } + + _device_init (dev, info); - vkh_layers_check_init(); - -#ifdef VKVG_USE_VALIDATION - if (vkh_layer_is_present("VK_LAYER_KHRONOS_validation")) - enabledLayers[enabledLayersCount++] = "VK_LAYER_KHRONOS_validation"; -#endif - -#ifdef VKVG_USE_RENDERDOC - if (vkh_layer_is_present("VK_LAYER_RENDERDOC_Capture")) - enabledLayers[enabledLayersCount++] = "VK_LAYER_RENDERDOC_Capture"; -#endif - vkh_layers_check_release(); - - vkvg_get_required_instance_extensions (enabledExts, &enabledExtsCount); - -#ifdef VK_VERSION_1_2 - VkhApp app = vkh_app_create(1, 2, "vkvg", enabledLayersCount, enabledLayers, enabledExtsCount, enabledExts); -#else - VkhApp app = vkh_app_create(1, 1, "vkvg", enabledLayersCount, enabledLayers, enabledExtsCount, enabledExts); -#endif - -#if defined(DEBUG) && defined (VKVG_DBG_UTILS) - vkh_app_enable_debug_messenger(app - , VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT - , VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT - , NULL); -#endif - - VkhPhyInfo* phys = vkh_app_get_phyinfos (app, &phyCount, VK_NULL_HANDLE); - if (phyCount == 0) { - dev->status = VKVG_STATUS_DEVICE_ERROR; - vkh_app_destroy (app); - return dev; - } - - VkhPhyInfo pi = 0; - if (!_device_try_get_phyinfo(phys, phyCount, VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU, &pi)) - if (!_device_try_get_phyinfo(phys, phyCount, VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU, &pi)) - pi = phys[0]; - - if (!(pi->properties.limits.framebufferColorSampleCounts & info->samples)) { - LOG(VKVG_LOG_ERR, "CREATE Device failed: sample count not supported: %d\n", info->samples); - dev->status = VKVG_STATUS_DEVICE_ERROR; - vkh_app_free_phyinfos (phyCount, phys); - vkh_app_destroy (app); - return dev; - } - - uint32_t qCount = 0; - float qPriorities[] = {0.0}; - VkDeviceQueueCreateInfo pQueueInfos[] = { {0},{0},{0} }; - - if (vkh_phyinfo_create_queues (pi, pi->gQueue, 1, qPriorities, &pQueueInfos[qCount])) - qCount++; - - enabledExtsCount=0; - - if (vkvg_get_required_device_extensions (pi->phy, enabledExts, &enabledExtsCount) != VKVG_STATUS_SUCCESS){ - dev->status = VKVG_STATUS_DEVICE_ERROR; - vkh_app_free_phyinfos (phyCount, phys); - vkh_app_destroy (app); - return dev; - } - - VkPhysicalDeviceFeatures enabledFeatures = {0}; - const void* pNext = vkvg_get_device_requirements (&enabledFeatures); - - VkDeviceCreateInfo device_info = { .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, - .queueCreateInfoCount = qCount, - .pQueueCreateInfos = (VkDeviceQueueCreateInfo*)&pQueueInfos, - .enabledExtensionCount = enabledExtsCount, - .ppEnabledExtensionNames = enabledExts, - .pEnabledFeatures = &enabledFeatures, - .pNext = pNext}; - - VkhDevice vkhd = vkh_device_create(app, pi, &device_info); - - _device_init (dev, - vkh_app_get_inst(app), - vkh_device_get_phy(vkhd), - vkh_device_get_vkdev(vkhd), - pi->gQueue, 0, - info->samples, deferredResolve); - - dev->vkhDev = vkhd; - - vkh_app_free_phyinfos (phyCount, phys); - - return dev; -} -VkvgDevice vkvg_device_create_from_vk(VkInstance inst, VkPhysicalDevice phy, VkDevice vkdev, uint32_t qFamIdx, uint32_t qIndex) -{ - return vkvg_device_create_from_vk_multisample (inst,phy,vkdev,qFamIdx,qIndex, VK_SAMPLE_COUNT_1_BIT, false); -} -VkvgDevice vkvg_device_create_from_vk_multisample(VkInstance inst, VkPhysicalDevice phy, VkDevice vkdev, uint32_t qFamIdx, uint32_t qIndex, VkSampleCountFlags samples, bool deferredResolve) -{ - LOG(VKVG_LOG_INFO, "CREATE Device from vk: qFam = %d; qIdx = %d\n", qFamIdx, qIndex); - VkvgDevice dev = (vkvg_device*)calloc(1,sizeof(vkvg_device)); - if (!dev) { - LOG(VKVG_LOG_ERR, "CREATE Device failed, no memory\n"); - exit(-1); - } - dev->references = 1; - _device_init(dev, inst, phy, vkdev, qFamIdx, qIndex, samples, deferredResolve); return dev; } diff --git a/tests/bezier.c b/tests/bezier.c index 756a3c7f..0ac57d31 100644 --- a/tests/bezier.c +++ b/tests/bezier.c @@ -189,9 +189,17 @@ int main(int argc, char* argv[]) { vkengine_set_cursor_pos_callback(e, mouse_move_callback); vkengine_set_scroll_callback(e, scroll_callback); - bool deferredResolve = false; + vkvg_device_create_info_t info = { + samples, + false, + vkh_app_get_inst(e->app), + r->dev->phy, + r->dev->dev, + r->qFam, + 0 + }; + device = vkvg_device_create(&info); - device = vkvg_device_create_from_vk_multisample(vkh_app_get_inst(e->app), r->dev->phy, r->dev->dev, r->qFam, 0, samples, deferredResolve); surf = vkvg_surface_create(device, test_width, test_height); vkh_presenter_build_blit_cmd (r, vkvg_surface_get_vk_image(surf), test_width, test_height); diff --git a/tests/common/test.c b/tests/common/test.c index 595f71a2..c583f8fb 100644 --- a/tests/common/test.c +++ b/tests/common/test.c @@ -161,9 +161,17 @@ void init_test (uint32_t width, uint32_t height){ vkengine_set_cursor_pos_callback(e, mouse_move_callback); vkengine_set_scroll_callback(e, scroll_callback); - bool deferredResolve = false; - - device = vkvg_device_create_from_vk_multisample(vkh_app_get_inst(e->app), r->dev->phy, r->dev->dev, r->qFam, 0, samples, deferredResolve); + vkvg_device_create_info_t info = { + samples, + false, + vkh_app_get_inst(e->app), + r->dev->phy, + r->dev->dev, + r->qFam, + 0 + }; + + device = vkvg_device_create(&info); surf = vkvg_surface_create(device, width, height); vkh_presenter_build_blit_cmd (r, vkvg_surface_get_vk_image(surf), width, height); @@ -496,8 +504,17 @@ void perform_test_offscreen (void(*testfunc)(void), const char *testName, int ar VkhDevice dev = vkh_device_create(app, pi, &device_info); - - device = vkvg_device_create_from_vk_multisample(vkh_app_get_inst(app), dev->phy, dev->dev, pi->gQueue, 0, samples, deferredResolve); + vkvg_device_create_info_t info = { + samples, + deferredResolve, + vkh_app_get_inst(e->app), + dev->phy, + dev->dev, + pi->gQueue, + 0 + }; + + device = vkvg_device_create(&info); //vkvg_device_set_dpy(device, 96, 96); vkh_app_free_phyinfos (phyCount, phys); @@ -559,9 +576,16 @@ void perform_test_onscreen (void(*testfunc)(void), const char *testName, int arg vkengine_set_cursor_pos_callback (e, mouse_move_callback); vkengine_set_scroll_callback (e, scroll_callback); - bool deferredResolve = false; - - device = vkvg_device_create_from_vk_multisample (vkh_app_get_inst (e->app), r->dev->phy, r->dev->dev, r->qFam, 0, samples, deferredResolve); + vkvg_device_create_info_t info = { + samples, + false, + vkh_app_get_inst(e->app), + r->dev->phy, + r->dev->dev, + r->qFam, + 0 + }; + device = vkvg_device_create(&info); vkvg_device_set_dpy (device, 96, 96); if (threadAware) @@ -619,7 +643,7 @@ void perform_test_onscreen (void(*testfunc)(void), const char *testName, int arg testfunc(); - if (deferredResolve) + if (info.deferredResolve) vkvg_surface_resolve(surf); if (!vkh_presenter_draw (r)){ vkh_presenter_get_size (r, &test_width, &test_height); diff --git a/tests/inverse_colinear.c b/tests/inverse_colinear.c index da4069f3..38b4adc6 100644 --- a/tests/inverse_colinear.c +++ b/tests/inverse_colinear.c @@ -187,10 +187,17 @@ int main(int argc, char* argv[]) { vkengine_set_cursor_pos_callback(e, mouse_move_callback); vkengine_set_scroll_callback(e, scroll_callback); - bool deferredResolve = false; - - device = vkvg_device_create_from_vk_multisample(vkh_app_get_inst(e->app), r->dev->phy, r->dev->dev, r->qFam, 0, samples, deferredResolve); - surf = vkvg_surface_create(device, test_width, test_height); + vkvg_device_create_info_t info = { + samples, + false, + vkh_app_get_inst(e->app), + r->dev->phy, + r->dev->dev, + r->qFam, + 0 + }; + device = vkvg_device_create(&info); + surf = vkvg_surface_create(device, test_width, test_height); vkh_presenter_build_blit_cmd (r, vkvg_surface_get_vk_image(surf), test_width, test_height); diff --git a/tests/offscreen.c b/tests/offscreen.c index 095baa0b..895d1e0d 100644 --- a/tests/offscreen.c +++ b/tests/offscreen.c @@ -1,7 +1,10 @@ #include "vkvg.h" int main(int argc, char *argv[]) { - VkvgDevice dev = vkvg_device_create(VK_SAMPLE_COUNT_1_BIT, false); + vkvg_device_create_info_t info = { + VK_SAMPLE_COUNT_1_BIT, false + }; + VkvgDevice dev = vkvg_device_create(&info); VkvgSurface surf = vkvg_surface_create(dev, 512,512); VkvgContext ctx = vkvg_create(surf); diff --git a/tests/path_extents.c b/tests/path_extents.c index 3011f1e3..55c69de4 100644 --- a/tests/path_extents.c +++ b/tests/path_extents.c @@ -151,9 +151,16 @@ int main(int argc, char* argv[]) { vkengine_set_cursor_pos_callback(e, mouse_move_callback); vkengine_set_scroll_callback(e, scroll_callback); - bool deferredResolve = false; - - device = vkvg_device_create_from_vk_multisample(vkh_app_get_inst(e->app), r->dev->phy, r->dev->dev, r->qFam, 0, samples, deferredResolve); + vkvg_device_create_info_t info = { + samples, + false, + vkh_app_get_inst(e->app), + r->dev->phy, + r->dev->dev, + r->qFam, + 0 + }; + device = vkvg_device_create(&info); surf = vkvg_surface_create(device, test_width, test_height); vkh_presenter_build_blit_cmd (r, vkvg_surface_get_vk_image(surf), test_width, test_height); diff --git a/tests/vkvg-svg/svg-viewer.c b/tests/vkvg-svg/svg-viewer.c index 6d39baba..de18cde3 100644 --- a/tests/vkvg-svg/svg-viewer.c +++ b/tests/vkvg-svg/svg-viewer.c @@ -270,9 +270,16 @@ int main (int argc, char *argv[]){ VkEngine e = vkengine_create (VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU,VK_PRESENT_MODE_FIFO_KHR, width, height); vkengine_set_key_callback (e, key_callback); vkengine_set_scroll_callback(e, scroll_callback); - - dev = vkvg_device_create_from_vk_multisample (vkh_app_get_inst(e->app), - vkengine_get_physical_device(e), vkengine_get_device(e), vkengine_get_queue_fam_idx(e), 0, samples, false); + vkvg_device_create_info_t info = { + samples, + false, + vkh_app_get_inst(e->app), + vkengine_get_physical_device(e), + vkengine_get_device(e), + vkengine_get_queue_fam_idx(e), + 0 + }; + dev = vkvg_device_create(&info); VkvgSurface surf = NULL;