Skip to content

Commit

Permalink
vkvg_device_create_info wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbruyere committed Sep 21, 2023
1 parent a5c3892 commit 00d9bdf
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 155 deletions.
25 changes: 11 additions & 14 deletions include/vkvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
*
Expand Down
222 changes: 103 additions & 119 deletions src/vkvg_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
12 changes: 10 additions & 2 deletions tests/bezier.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 00d9bdf

Please sign in to comment.