aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/phyInfos.h
blob: db89cbdb6da77b922fd7ab24e807448d6f6760e9 (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
#pragma once

#include "vke.h"

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);
		}

	};
}