aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/VulkanDevice.hpp
blob: d61152df9ba0e31caa8de1e2f2b9ecfc31341ca0 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Vulkan device class
*
* Encapsulates a physical Vulkan device and it's logical representation
*
* Copyright (C) 2016-2018 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/

#pragma once

#include "vke.h"

//#include "VulkanBuffer.hpp"

namespace vke
{
    struct vkPhyInfo{
        VkPhysicalDevice                    phy;
        VkPhysicalDeviceMemoryProperties    memProps;
        VkPhysicalDeviceProperties          properties;
        VkPhysicalDeviceFeatures            features;
        VkPhysicalDeviceFeatures            enabledFeatures;
        VkQueueFamilyProperties*            queues;
        uint32_t                            queueCount;
        std::vector<int>                    cQueues;//compute, dedicated first
        std::vector<int>                    gQueues;//graphic, idem
        std::vector<int>                    tQueues;//transfer, idem
        int                                 pQueue;//presentation

        std::vector<VkDeviceQueueCreateInfo> pQueueInfos;
        std::vector<std::vector<float>>      qPriorities;

        vkPhyInfo (){}
        vkPhyInfo (VkPhysicalDevice _phy, VkSurfaceKHR surface = VK_NULL_HANDLE) {
            phy = _phy;
//            cQueues.clear();
//            gQueues.clear();
//            tQueues.clear();
            pQueue = -1;
//            pQueueInfos.clear();
//            qPriorities.clear();

            vkGetPhysicalDeviceProperties       (phy, &properties);
            vkGetPhysicalDeviceMemoryProperties (phy, &memProps);
            vkGetPhysicalDeviceFeatures         (phy, &features);

            vkGetPhysicalDeviceQueueFamilyProperties (phy, &queueCount, NULL);
            queues = (VkQueueFamilyProperties*)malloc(queueCount * sizeof(VkQueueFamilyProperties));
            vkGetPhysicalDeviceQueueFamilyProperties (phy, &queueCount, queues);

            //identify dedicated queues
            //try to find dedicated queues first
            VkBool32 present = VK_FALSE;
            for (uint j=0; j<queueCount; j++){
                switch (queues[j].queueFlags) {
                case VK_QUEUE_GRAPHICS_BIT:
                    if (surface){
                        vkGetPhysicalDeviceSurfaceSupportKHR(phy, j, surface, &present);
                        if (present && pQueue<0)
                            pQueue = j;
                    }
                    gQueues.push_back (j);
                    break;
                case VK_QUEUE_COMPUTE_BIT:
                    cQueues.push_back (j);
                    break;
                case VK_QUEUE_TRANSFER_BIT:
                    tQueues.push_back (j);
                    break;
                }
            }
            //add queues that include flag
            for (uint j=0; j<queueCount; j++){
                if (queues[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
                    if (surface) {
                        vkGetPhysicalDeviceSurfaceSupportKHR(phy, j, surface, &present);
                        if (present && pQueue<0)
                            pQueue = j;
                    }
                    if (std::find(gQueues.begin(), gQueues.end(), j) == gQueues.end())
                        gQueues.push_back(j);
                }
                if (queues[j].queueFlags & VK_QUEUE_COMPUTE_BIT){
                    if (std::find(cQueues.begin(), cQueues.end(), j) == cQueues.end())
                        cQueues.push_back(j);
                }
                if (queues[j].queueFlags & VK_QUEUE_TRANSFER_BIT){
                    if (std::find(tQueues.begin(), tQueues.end(), j) == tQueues.end())
                        tQueues.push_back(j);
                }
            }
        }

        void selectQueue (int qIndex, float priority = 0.f) {
            auto it = find_if(pQueueInfos.begin(), pQueueInfos.end(), [&qIndex](const VkDeviceQueueCreateInfo& qci)
                {return qci.queueFamilyIndex == qIndex;});
            if (it == pQueueInfos.end()) {
                VkDeviceQueueCreateInfo dqci = {VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO};
                dqci.queueCount = 1;
                dqci.queueFamilyIndex = qIndex;
                pQueueInfos.push_back (dqci);
                qPriorities.push_back ({priority});
                return;
            }

            int qciIdx = std::distance(pQueueInfos.begin(), it);
            pQueueInfos[qciIdx].queueCount ++;
            qPriorities[qciIdx].push_back(priority);
        }

    };
    struct VulkanDevice
    {
        VkPhysicalDevice            phy;
        VkDevice                    dev;
        VkPhysicalDeviceProperties  properties;
        VkPhysicalDeviceFeatures    features;
        VkPhysicalDeviceFeatures    enabledFeatures;
        VkPhysicalDeviceMemoryProperties    memoryProperties;
        std::vector<VkQueueFamilyProperties>queueFamilyProperties;

        VkCommandPool   commandPool     = VK_NULL_HANDLE;
        VkPipelineCache pipelineCache   = VK_NULL_HANDLE;
        bool            savePLCache     = true;

        VkQueue queue;

        struct {
            uint32_t graphics;
            uint32_t compute;
        } queueFamilyIndices;

        VulkanDevice(vkPhyInfo phyInfos, const std::vector<const char *> &devLayers);
        ~VulkanDevice();

        uint32_t getMemoryType(uint32_t typeBits, VkMemoryPropertyFlags properties, VkBool32 *memTypeFound = nullptr);
        VkFormat getSuitableDepthFormat ();
        uint32_t getQueueFamilyIndex(VkQueueFlagBits queueFlags);

        VkCommandPool createCommandPool(uint32_t queueFamilyIndex,
                                        VkCommandPoolCreateFlags createFlags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT);
        VkCommandBuffer createCommandBuffer(VkCommandBufferLevel level, bool begin = false, bool oneTime = false);

        void flushCommandBuffer(VkCommandBuffer commandBuffer, VkQueue queue, bool free = true);
        VkSemaphore createSemaphore ();
        VkFence createFence (bool signaled = false);
        void destroyFence (VkFence fence);
        void destroySemaphore (VkSemaphore semaphore);
    };
}