Skip to content

Commit 38c3c1d

Browse files
committed
Implement more device initialisation
1 parent 6fa6efb commit 38c3c1d

File tree

10 files changed

+308
-8
lines changed

10 files changed

+308
-8
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
===========================================================================
3+
4+
Daemon BSD Source Code
5+
Copyright (c) 2025 Daemon Developers
6+
All rights reserved.
7+
8+
This file is part of the Daemon BSD Source Code (Daemon Source Code).
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are met:
12+
* Redistributions of source code must retain the above copyright
13+
notice, this list of conditions and the following disclaimer.
14+
* Redistributions in binary form must reproduce the above copyright
15+
notice, this list of conditions and the following disclaimer in the
16+
documentation and/or other materials provided with the distribution.
17+
* Neither the name of the Daemon developers nor the
18+
names of its contributors may be used to endorse or promote products
19+
derived from this software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
25+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
===========================================================================
33+
*/
34+
// GraphicsCoreStore.h
35+
36+
#include "Vulkan.h"
37+
38+
#include "EngineConfig.h"
39+
#include "QueuesConfig.h"
40+
#include "Queue.h"
41+
42+
#include "GraphicsCoreStore.h"
43+
44+
EngineConfig engineConfig;
45+
QueuesConfig queuesConfig;
46+
47+
VkPhysicalDevice physicalDevice;
48+
49+
VkDevice device;
50+
51+
GraphicsQueueRingBuffer graphicsQueue;
52+
GraphicsQueueRingBuffer computeQueue;
53+
GraphicsQueueRingBuffer transferQueue;
54+
GraphicsQueueRingBuffer sparseQueue;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
===========================================================================
3+
4+
Daemon BSD Source Code
5+
Copyright (c) 2025 Daemon Developers
6+
All rights reserved.
7+
8+
This file is part of the Daemon BSD Source Code (Daemon Source Code).
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are met:
12+
* Redistributions of source code must retain the above copyright
13+
notice, this list of conditions and the following disclaimer.
14+
* Redistributions in binary form must reproduce the above copyright
15+
notice, this list of conditions and the following disclaimer in the
16+
documentation and/or other materials provided with the distribution.
17+
* Neither the name of the Daemon developers nor the
18+
names of its contributors may be used to endorse or promote products
19+
derived from this software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
25+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
===========================================================================
33+
*/
34+
// GraphicsCoreStore.h
35+
36+
#ifndef GRAPHICS_CORE_STORE_H
37+
#define GRAPHICS_CORE_STORE_H
38+
39+
struct GraphicsQueueRingBuffer;
40+
struct EngineConfig;
41+
struct QueuesConfig;
42+
43+
extern EngineConfig engineConfig;
44+
extern QueuesConfig queuesConfig;
45+
46+
extern VkPhysicalDevice physicalDevice;
47+
48+
extern VkDevice device;
49+
50+
extern GraphicsQueueRingBuffer graphicsQueue;
51+
extern GraphicsQueueRingBuffer computeQueue;
52+
extern GraphicsQueueRingBuffer transferQueue;
53+
extern GraphicsQueueRingBuffer sparseQueue;
54+
55+
#endif // GRAPHICS_CORE_STORE_H

src/engine/renderer-vulkan/GraphicsCore/Instance.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4545

4646
#include "CapabilityPack.h"
4747
#include "EngineConfig.h"
48+
#include "QueuesConfig.h"
4849
#include "PhysicalDevice.h"
50+
#include "Queue.h"
51+
52+
#include "GraphicsCoreStore.h"
4953

5054
#include "Instance.h"
5155

@@ -109,9 +113,28 @@ void Instance::Init( const char* engineName, const char* appName ) {
109113
vkGetPhysicalDeviceFeatures2( availableDevices[1], &f );
110114
vkGetPhysicalDeviceProperties2( availableDevices[1], &p );
111115

112-
EngineConfig cfg;
113-
114-
if ( !SelectPhysicalDevice( availableDevices, &cfg ) ) {
116+
if ( !SelectPhysicalDevice( availableDevices, &engineConfig, &physicalDevice ) ) {
115117
return;
116118
}
119+
120+
QueuesConfig queuesConfig = GetQueuesConfigForDevice( physicalDevice );
121+
122+
CreateDevice( physicalDevice, engineConfig, queuesConfig,
123+
capabilityPackMinimal.requiredExtensions.memory, capabilityPackMinimal.requiredExtensions.size, &device );
124+
125+
VulkanLoadDeviceFunctions( device, instance );
126+
127+
graphicsQueue.Init( device, queuesConfig.graphicsQueue.id, queuesConfig.graphicsQueue.queues );
128+
129+
if( queuesConfig.computeQueue.queues ) {
130+
computeQueue.Init( device, queuesConfig.computeQueue.id, queuesConfig.computeQueue.queues );
131+
}
132+
133+
if ( queuesConfig.transferQueue.queues ) {
134+
transferQueue.Init( device, queuesConfig.transferQueue.id, queuesConfig.transferQueue.queues );
135+
}
136+
137+
if ( queuesConfig.sparseQueue.queues ) {
138+
sparseQueue.Init( device, queuesConfig.sparseQueue.id, queuesConfig.sparseQueue.queues );
139+
}
117140
}

src/engine/renderer-vulkan/GraphicsCore/PhysicalDevice.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3434
// PhysicalDevice.cpp
3535

3636
#include "../Math/NumberTypes.h"
37+
#include "../Memory/Array.h"
38+
#include "../Memory/DynamicArray.h"
3739
#include "../Error.h"
3840

3941
#include "GraphicsCoreCVars.h"
@@ -58,7 +60,7 @@ static void PrintDeviceInfo( const EngineConfig& config ) {
5860
config.conformanceVersion.major, config.conformanceVersion.minor, config.conformanceVersion.patch );
5961
}
6062

61-
bool SelectPhysicalDevice( const DynamicArray<VkPhysicalDevice>& devices, EngineConfig* config ) {
63+
bool SelectPhysicalDevice( const DynamicArray<VkPhysicalDevice>& devices, EngineConfig* config, VkPhysicalDevice* deviceOut ) {
6264
if ( !devices.size ) {
6365
Err( "No Vulkan devices found" );
6466
return false;
@@ -109,7 +111,45 @@ bool SelectPhysicalDevice( const DynamicArray<VkPhysicalDevice>& devices, Engine
109111

110112
*config = bestCFG;
111113

112-
QueuesConfig queuesConfig = GetQueuesConfigForDevice( *bestDevice );
114+
*deviceOut = *bestDevice;
113115

114116
return true;
115117
}
118+
119+
void CreateDevice( const VkPhysicalDevice& physicalDevice, EngineConfig& config, QueuesConfig& queuesConfig,
120+
const char* const* requiredExtensions, const uint32 extensionCount,
121+
VkDevice* device ) {
122+
VkPhysicalDeviceVulkan12Features features12 {};
123+
VkPhysicalDeviceVulkan13Features features13 { .pNext = &features12 };
124+
VkPhysicalDeviceFeatures2 features { .pNext = &features13 };
125+
126+
vkGetPhysicalDeviceFeatures2( physicalDevice, &features );
127+
128+
DynamicArray<VkDeviceQueueCreateInfo> queueInfos;
129+
queueInfos.Resize( queuesConfig.count );
130+
131+
for ( uint32 i = 0; i < queuesConfig.count; i++ ) {
132+
DynamicArray<float> priorities;
133+
priorities.Resize( queuesConfig[i].queues );
134+
135+
for ( float* value = priorities.memory; value < priorities.memory + priorities.size; value++ ) {
136+
*value = 1.0f;
137+
}
138+
139+
VkDeviceQueueCreateInfo& queueInfo = queueInfos[i];
140+
141+
queueInfo.queueFamilyIndex = i;
142+
queueInfo.queueCount = queuesConfig[i].queues;
143+
queueInfo.pQueuePriorities = priorities.memory;
144+
}
145+
146+
VkDeviceCreateInfo info {
147+
.pNext = &features,
148+
.queueCreateInfoCount = queuesConfig.count,
149+
.pQueueCreateInfos = queueInfos.memory,
150+
.enabledExtensionCount = extensionCount,
151+
.ppEnabledExtensionNames = requiredExtensions
152+
};
153+
154+
VkResult res = vkCreateDevice( physicalDevice, &info, nullptr, device );
155+
}

src/engine/renderer-vulkan/GraphicsCore/PhysicalDevice.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4141
#include "../Memory/DynamicArray.h"
4242

4343
struct EngineConfig;
44+
struct QueuesConfig;
4445

4546
class PhysicalDevice {
4647
public:
@@ -49,6 +50,10 @@ class PhysicalDevice {
4950
PhysicalDevice( const VkPhysicalDeviceProperties2& properties, const VkPhysicalDeviceFeatures2& features );
5051
};
5152

52-
bool SelectPhysicalDevice( const DynamicArray<VkPhysicalDevice>& devices, EngineConfig* config );
53+
bool SelectPhysicalDevice( const DynamicArray<VkPhysicalDevice>& devices, EngineConfig* config, VkPhysicalDevice* deviceOut );
54+
55+
void CreateDevice( const VkPhysicalDevice& physicalDevice, EngineConfig& config, QueuesConfig& queuesConfig,
56+
const char* const* requiredExtensions, const uint32 extensionCount,
57+
VkDevice* device );
5358

5459
#endif // PHYSICAL_DEVICE_H
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
===========================================================================
3+
4+
Daemon BSD Source Code
5+
Copyright (c) 2025 Daemon Developers
6+
All rights reserved.
7+
8+
This file is part of the Daemon BSD Source Code (Daemon Source Code).
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are met:
12+
* Redistributions of source code must retain the above copyright
13+
notice, this list of conditions and the following disclaimer.
14+
* Redistributions in binary form must reproduce the above copyright
15+
notice, this list of conditions and the following disclaimer in the
16+
documentation and/or other materials provided with the distribution.
17+
* Neither the name of the Daemon developers nor the
18+
names of its contributors may be used to endorse or promote products
19+
derived from this software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
25+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
===========================================================================
33+
*/
34+
// Queue.h
35+
36+
#include "Vulkan.h"
37+
38+
#include "Queue.h"
39+
40+
void GraphicsQueueRingBuffer::Init( const VkDevice device, const uint32 queueGroup, uint32 count ) {
41+
count = count > maxQueues ? maxQueues : count;
42+
43+
for ( GraphicsQueue* queue = queues; queue < queues + count; queue++ ) {
44+
VkDeviceQueueInfo2 info {
45+
.queueFamilyIndex = queueGroup,
46+
.queueIndex = ( uint32 ) ( queue - queues )
47+
};
48+
49+
vkGetDeviceQueue2( device, &info, &queue->queue );
50+
}
51+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
===========================================================================
3+
4+
Daemon BSD Source Code
5+
Copyright (c) 2025 Daemon Developers
6+
All rights reserved.
7+
8+
This file is part of the Daemon BSD Source Code (Daemon Source Code).
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are met:
12+
* Redistributions of source code must retain the above copyright
13+
notice, this list of conditions and the following disclaimer.
14+
* Redistributions in binary form must reproduce the above copyright
15+
notice, this list of conditions and the following disclaimer in the
16+
documentation and/or other materials provided with the distribution.
17+
* Neither the name of the Daemon developers nor the
18+
names of its contributors may be used to endorse or promote products
19+
derived from this software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
25+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
32+
===========================================================================
33+
*/
34+
// Queue.h
35+
36+
#ifndef QUEUE_H
37+
#define QUEUE_H
38+
39+
#include "Vulkan.h"
40+
41+
#include "../Math/NumberTypes.h"
42+
43+
#include "QueuesConfig.h"
44+
45+
struct GraphicsQueue {
46+
QueueType type;
47+
uint32 index;
48+
VkQueue queue;
49+
};
50+
51+
struct GraphicsQueueRingBuffer {
52+
static constexpr uint32 maxQueues = 64;
53+
54+
GraphicsQueue queues[maxQueues] {};
55+
56+
void Init( const VkDevice device, const uint32 queueGroup, uint32 count );
57+
};
58+
59+
#endif // QUEUE_H

src/engine/renderer-vulkan/GraphicsCore/QueuesConfig.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,25 @@ QueuesConfig GetQueuesConfigForDevice( const VkPhysicalDevice& device ) {
5959
QueueConfig* cfg = &config.queues[i];
6060
VkQueueFamilyProperties& coreProperties = propertiesArray[i].queueFamilyProperties;
6161

62+
cfg->id = i;
63+
6264
cfg->type = ( QueueType ) coreProperties.queueFlags;
6365
cfg->queues = coreProperties.queueCount;
6466
cfg->timestampValidBits = coreProperties.timestampValidBits;
6567
cfg->minImageTransferGranularity = coreProperties.minImageTransferGranularity;
6668

6769
if ( cfg->type & GRAPHICS ) {
6870
config.graphicsQueue = *cfg;
71+
config.graphicsQueue.unique = true;
6972
} else if ( cfg->type & COMPUTE ) {
7073
config.computeQueue = *cfg;
74+
config.computeQueue.unique = true;
7175
} else if ( ( cfg->type & TRANSFER ) && cfg->queues > config.transferQueue.queues ) {
7276
config.transferQueue = *cfg;
77+
config.transferQueue.unique = true;
7378
} else if ( ( cfg->type & SPARSE ) && cfg->queues > config.sparseQueue.queues ) {
7479
config.sparseQueue = *cfg;
80+
config.sparseQueue.unique = true;
7581
}
7682
}
7783

src/engine/renderer-vulkan/GraphicsCore/QueuesConfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ enum QueueType {
4949
};
5050

5151
struct QueueConfig {
52+
uint32 id;
53+
bool unique;
54+
5255
QueueType type;
5356
uint32 queues = 0;
5457
uint32 timestampValidBits;

0 commit comments

Comments
 (0)