aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/vkh_device.c
blob: 8659ef35b08627ed6efdfe129e3deb2883ec8cac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (c) 2018-2022 Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
 * Software, and to permit persons to whom the Software is furnished to do so, subject
 * to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "vkh_device.h"
#include "vkh_phyinfo.h"
#include "vkh_app.h"

#include <stdlib.h>
#include <string.h>

static PFN_vkSetDebugUtilsObjectNameEXT   SetDebugUtilsObjectNameEXT;
static PFN_vkQueueBeginDebugUtilsLabelEXT QueueBeginDebugUtilsLabelEXT;
static PFN_vkQueueEndDebugUtilsLabelEXT   QueueEndDebugUtilsLabelEXT;
static PFN_vkCmdBeginDebugUtilsLabelEXT   CmdBeginDebugUtilsLabelEXT;
static PFN_vkCmdEndDebugUtilsLabelEXT     CmdEndDebugUtilsLabelEXT;
static PFN_vkCmdInsertDebugUtilsLabelEXT  CmdInsertDebugUtilsLabelEXT;

VkhDevice vkh_device_create(VkhApp app, VkhPhyInfo phyInfo, VkDeviceCreateInfo *pDevice_info) {
    VkDevice dev;
    VK_CHECK_RESULT(vkCreateDevice(phyInfo->phy, pDevice_info, NULL, &dev));
    VkhDevice vkhd       = vkh_device_import(app->inst, phyInfo->phy, dev);
    vkhd->vkhApplication = app;
    return vkhd;
}
VkhDevice vkh_device_import(VkInstance inst, VkPhysicalDevice phy, VkDevice vkDev) {
    VkhDevice dev = (vkh_device_t *)calloc(1, sizeof(vkh_device_t));
    dev->dev      = vkDev;
    dev->phy      = phy;
    dev->instance = inst;

    vkGetPhysicalDeviceMemoryProperties(phy, &dev->phyMemProps);
#ifdef VKH_USE_VMA
    if (!dev->allocator) {
        VmaAllocatorCreateInfo allocatorInfo = {.physicalDevice = phy, .device = vkDev};
        vmaCreateAllocator(&allocatorInfo, &dev->allocator);
    }
#else
#endif

    return dev;
}
VkResult         vkh_device_status(VkhDevice dev) { return dev == NULL ? VK_ERROR_UNKNOWN : dev->status; }
VkDevice         vkh_device_get_vkdev(VkhDevice dev) { return vkh_device_status(dev) ? VK_NULL_HANDLE : dev->dev; }
VkPhysicalDevice vkh_device_get_phy(VkhDevice dev) { return vkh_device_status(dev) ? VK_NULL_HANDLE : dev->phy; }
VkhApp           vkh_device_get_app(VkhDevice dev) { return vkh_device_status(dev) ? NULL : dev->vkhApplication; }
/**
 * @brief get instance proc addresses for debug utils (name, label,...)
 * @param vkh device
 */
void vkh_device_init_debug_utils(VkhDevice dev) {
    SetDebugUtilsObjectNameEXT =
        (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(dev->instance, "vkSetDebugUtilsObjectNameEXT");
    QueueBeginDebugUtilsLabelEXT =
        (PFN_vkQueueBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(dev->instance, "vkQueueBeginDebugUtilsLabelEXT");
    QueueEndDebugUtilsLabelEXT =
        (PFN_vkQueueEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(dev->instance, "vkQueueEndDebugUtilsLabelEXT");
    CmdBeginDebugUtilsLabelEXT =
        (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(dev->instance, "vkCmdBeginDebugUtilsLabelEXT");
    CmdEndDebugUtilsLabelEXT =
        (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(dev->instance, "vkCmdEndDebugUtilsLabelEXT");
    CmdInsertDebugUtilsLabelEXT =
        (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(dev->instance, "vkCmdInsertDebugUtilsLabelEXT");
}
VkSampler vkh_device_create_sampler(VkhDevice dev, VkFilter magFilter, VkFilter minFilter,
                                    VkSamplerMipmapMode mipmapMode, VkSamplerAddressMode addressMode) {
    VkSampler           sampler           = VK_NULL_HANDLE;
    VkSamplerCreateInfo samplerCreateInfo = {.sType         = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
                                             .maxAnisotropy = 1.0,
                                             .addressModeU  = addressMode,
                                             .addressModeV  = addressMode,
                                             .addressModeW  = addressMode,
                                             .magFilter     = magFilter,
                                             .minFilter     = minFilter,
                                             .mipmapMode    = mipmapMode};
    VK_CHECK_RESULT(vkCreateSampler(dev->dev, &samplerCreateInfo, NULL, &sampler));
    return sampler;
}
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);
}

void vkh_device_set_object_name(VkhDevice dev, VkObjectType objectType, uint64_t handle, const char *name) {
    const VkDebugUtilsObjectNameInfoEXT info = {.sType        = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
                                                .pNext        = 0,
                                                .objectType   = objectType,
                                                .objectHandle = handle,
                                                .pObjectName  = name};
    SetDebugUtilsObjectNameEXT(dev->dev, &info);
}
void vkh_cmd_label_start(VkCommandBuffer cmd, const char *name, const float color[4]) {
    const VkDebugUtilsLabelEXT info = {
        .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT, .pNext = 0, .pLabelName = name};
    memcpy((void *)info.color, (void *)color, 4 * sizeof(float));
    CmdBeginDebugUtilsLabelEXT(cmd, &info);
}
void vkh_cmd_label_insert(VkCommandBuffer cmd, const char *name, const float color[4]) {
    const VkDebugUtilsLabelEXT info = {
        .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT, .pNext = 0, .pLabelName = name};
    memcpy((void *)info.color, (void *)color, 4 * sizeof(float));
    CmdInsertDebugUtilsLabelEXT(cmd, &info);
}
void vkh_cmd_label_end(VkCommandBuffer cmd) { CmdEndDebugUtilsLabelEXT(cmd); }