Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
VkCV Framework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Vulkan2021
VkCV Framework
Commits
0e8cdfb5
Commit
0e8cdfb5
authored
3 years ago
by
Vanessa Karolek
Browse files
Options
Downloads
Patches
Plain Diff
[
#7
][Fix] Simplify and correct Context class
The changes are based on review
!2 (comment 114205)
parent
47fe8a97
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!2
Resolve "Context Functionality"
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/vkcv/Context.cpp
+74
-76
74 additions, 76 deletions
src/vkcv/Context.cpp
src/vkcv/Context.hpp
+1
-6
1 addition, 6 deletions
src/vkcv/Context.hpp
with
75 additions
and
82 deletions
src/vkcv/Context.cpp
+
74
−
76
View file @
0e8cdfb5
#include
"Context.hpp"
std
::
vector
<
const
char
*>
validationLayers
=
{
"VK_LAYER_KHRONOS_validation"
};
namespace
vkcv
{
Context
::
Context
(
vk
::
Instance
instance
,
vk
::
PhysicalDevice
physicalDevice
,
vk
::
Device
device
)
:
m_instance
(
instance
),
m_physicalDevice
(
physicalDevice
),
m_device
(
device
)
:
m_instance
(
instance
),
m_physicalDevice
(
physicalDevice
),
m_device
(
device
)
{}
Context
::~
Context
()
{
...
...
@@ -19,49 +14,64 @@ namespace vkcv {
Context
Context
::
create
(
const
char
*
applicationName
,
uint32_t
applicationVersion
,
uint32_t
queueCount
,
std
::
vector
<
vk
::
QueueFlagBits
>
queueFlags
,
std
::
vector
<
const
char
*>
instanceExtensions
,
std
::
vector
<
const
char
*>
deviceExtensions
)
{
glfwInit
();
// check for layer support
uint32_t
layerCount
=
0
;
vk
::
enumerateInstanceLayerProperties
(
&
layerCount
,
nullptr
);
std
::
vector
<
vk
::
LayerProperties
>
layerProperties
(
layerCount
);
vk
::
enumerateInstanceLayerProperties
(
&
layerCount
,
layerProperties
.
data
());
std
::
vector
<
const
char
*>
supportedLayers
;
for
(
auto
&
elem
:
layerProperties
)
for
(
auto
&
elem
:
layerProperties
)
{
supportedLayers
.
push_back
(
elem
.
layerName
);
}
// if in debug mode, check if validation layers are supported. Enable them if supported
if
(
enableValidationLayers
&&
!
Context
::
checkSupport
(
supportedLayers
,
validationLayers
))
// if in debug mode, check if validation layers are supported. Enable them if supported
#if _DEBUG
std
::
vector
<
const
char
*>
validationLayers
=
{
"VK_LAYER_KHRONOS_validation"
};
if
(
!
Context
::
checkSupport
(
supportedLayers
,
validationLayers
))
{
throw
std
::
runtime_error
(
"Validation layers requested but not available!"
);
}
#endif
// check for extension support
std
::
vector
<
vk
::
ExtensionProperties
>
instanceExtensionProperties
=
vk
::
enumerateInstanceExtensionProperties
();
std
::
vector
<
const
char
*>
supportedExtensions
;
for
(
auto
&
elem
:
instanceExtensionProperties
)
for
(
auto
&
elem
:
instanceExtensionProperties
)
{
supportedExtensions
.
push_back
(
elem
.
extensionName
);
if
(
!
checkSupport
(
supportedExtensions
,
instanceExtensions
))
}
if
(
!
checkSupport
(
supportedExtensions
,
instanceExtensions
))
{
throw
std
::
runtime_error
(
"The requested instance extensions are not supported!"
);
}
// for GLFW: get all required extensions
std
::
vector
<
const
char
*>
requiredExtensions
=
Context
::
getRequiredExtensions
();
instanceExtensions
.
insert
(
instanceExtensions
.
end
(),
requiredExtensions
.
begin
(),
requiredExtensions
.
end
());
const
vk
::
ApplicationInfo
applicationInfo
(
applicationName
,
applicationVersion
,
"vkCV"
,
VK_MAKE_VERSION
(
0
,
0
,
1
),
VK_HEADER_VERSION_COMPLETE
const
vk
::
ApplicationInfo
applicationInfo
(
applicationName
,
applicationVersion
,
"vkCV"
,
VK_MAKE_VERSION
(
0
,
0
,
1
),
VK_HEADER_VERSION_COMPLETE
);
const
vk
::
InstanceCreateInfo
instanceCreateInfo
(
vk
::
InstanceCreateInfo
instanceCreateInfo
(
vk
::
InstanceCreateFlags
(),
&
applicationInfo
,
(
enableValidationLayers
)
?
static_cast
<
uint32_t
>
(
validationLayers
.
size
())
:
0
,
(
enableValidationLayers
)
?
validationLayers
.
data
()
:
nullptr
,
0
,
nullptr
,
static_cast
<
uint32_t
>
(
instanceExtensions
.
size
()),
instanceExtensions
.
data
()
);
#if _DEBUG
instanceCreateInfo
.
enabledLayerCount
=
static_cast
<
uint32_t
>
(
validationLayers
.
size
());
instanceCreateInfo
.
ppEnabledLayerNames
=
validationLayers
.
data
();
#endif
vk
::
Instance
instance
=
vk
::
createInstance
(
instanceCreateInfo
);
std
::
vector
<
vk
::
PhysicalDevice
>
physicalDevices
=
instance
.
enumeratePhysicalDevices
();
...
...
@@ -70,28 +80,36 @@ namespace vkcv {
// check for physical device extension support
std
::
vector
<
vk
::
ExtensionProperties
>
deviceExtensionProperties
=
physicalDevice
.
enumerateDeviceExtensionProperties
();
supportedExtensions
.
clear
();
for
(
auto
&
elem
:
deviceExtensionProperties
)
for
(
auto
&
elem
:
deviceExtensionProperties
)
{
supportedExtensions
.
push_back
(
elem
.
extensionName
);
if
(
!
checkSupport
(
supportedExtensions
,
deviceExtensions
))
}
if
(
!
checkSupport
(
supportedExtensions
,
deviceExtensions
))
{
throw
std
::
runtime_error
(
"The requested device extensions are not supported by the physical device!"
);
}
// create required queues
std
::
vector
<
vk
::
DeviceQueueCreateInfo
>
qCreateInfos
=
getQueueCreateInfos
(
physicalDevice
,
queueCount
,
queueFlags
);
const
vk
::
DeviceCreateInfo
deviceCreateInfo
(
vk
::
DeviceCreateFlags
(),
qCreateInfos
.
size
(),
qCreateInfos
.
data
(),
(
enableValidationLayers
)
?
static_cast
<
uint32_t
>
(
validationLayers
.
size
())
:
0
,
(
enableValidationLayers
)
?
validationLayers
.
data
()
:
nullptr
,
deviceExtensions
.
size
(),
deviceExtensions
.
data
(),
nullptr
// Should our device use some features??? If yes: TODO
vk
::
DeviceCreateInfo
deviceCreateInfo
(
vk
::
DeviceCreateFlags
(),
qCreateInfos
.
size
(),
qCreateInfos
.
data
(),
0
,
nullptr
,
deviceExtensions
.
size
(),
deviceExtensions
.
data
(),
nullptr
// Should our device use some features??? If yes: TODO
);
#if _DEBUG
deviceCreateInfo
.
enabledLayerCount
=
static_cast
<
uint32_t
>
(
validationLayers
.
size
());
deviceCreateInfo
.
ppEnabledLayerNames
=
validationLayers
.
data
();
#endif
vk
::
Device
device
=
physicalDevice
.
createDevice
(
deviceCreateInfo
);
// TODO: implement device.getQueue() to access the queues, if needed
return
Context
(
instance
,
physicalDevice
,
device
);
}
...
...
@@ -115,13 +133,12 @@ namespace vkcv {
/// <seealso cref="Context.deviceScore">
vk
::
PhysicalDevice
Context
::
pickPhysicalDevice
(
vk
::
Instance
&
instance
)
{
vk
::
PhysicalDevice
phyDevice
;
uint32_t
deviceCount
=
0
;
instance
.
enumeratePhysicalDevices
(
&
deviceCount
,
nullptr
);
if
(
device
Count
==
0
)
{
std
::
vector
<
vk
::
PhysicalDevice
>
devices
=
instance
.
enumeratePhysicalDevices
()
;
if
(
device
s
.
size
()
==
0
)
{
throw
std
::
runtime_error
(
"failed to find GPUs with Vulkan support!"
);
}
std
::
vector
<
vk
::
PhysicalDevice
>
devices
(
deviceCount
);
instance
.
enumeratePhysicalDevices
(
&
deviceCount
,
devices
.
data
());
int
max_score
=
-
1
;
for
(
const
auto
&
device
:
devices
)
{
int
score
=
deviceScore
(
device
);
...
...
@@ -131,7 +148,7 @@ namespace vkcv {
}
}
if
(
&
phyDevice
==
nullptr
)
{
if
(
max_score
==
-
1
)
{
throw
std
::
runtime_error
(
"failed to find a suitable GPU!"
);
}
...
...
@@ -165,15 +182,10 @@ namespace vkcv {
score
*=
vram
;
if
(
properties
.
deviceType
==
vk
::
PhysicalDeviceType
::
eDiscreteGpu
)
{
// nice!
score
*=
2
;
}
else
if
(
properties
.
deviceType
==
vk
::
PhysicalDeviceType
::
eIntegratedGpu
)
{
// not perfect but ok
}
else
{
// not so nice
score
*=
-
1
;
else
if
(
properties
.
deviceType
!=
vk
::
PhysicalDeviceType
::
eIntegratedGpu
)
{
score
=
-
1
;
}
return
score
;
...
...
@@ -206,31 +218,17 @@ namespace vkcv {
uint32_t
create
=
queueCount
;
for
(
int
i
=
0
;
i
<
qFamilyCandidates
.
size
()
&&
create
>
0
;
i
++
)
{
const
int
availableQueues
=
qFamilyCandidates
[
i
].
queueCount
;
if
(
create
>=
availableQueues
)
{
float
*
qPriorities
=
new
float
[
availableQueues
];
std
::
fill_n
(
qPriorities
,
availableQueues
,
1.
f
);
// all queues have the same priorities
vk
::
DeviceQueueCreateInfo
qCreateInfo
(
vk
::
DeviceQueueCreateFlags
(),
i
,
qFamilyCandidates
[
i
].
queueCount
,
qPriorities
);
queueCreateInfos
.
push_back
(
qCreateInfo
);
create
-=
qFamilyCandidates
[
i
].
queueCount
;
}
else
{
float
*
qPriorities
=
new
float
[
create
];
std
::
fill_n
(
qPriorities
,
create
,
1.
f
);
// all queues have the same priorities
vk
::
DeviceQueueCreateInfo
qCreateInfo
(
vk
::
DeviceQueueCreateFlags
(),
i
,
create
,
qPriorities
);
queueCreateInfos
.
push_back
(
qCreateInfo
);
create
-=
create
;
}
const
int
maxCreatableQueues
=
std
::
min
(
create
,
qFamilyCandidates
[
i
].
queueCount
);
float
*
qPriorities
=
new
float
[
maxCreatableQueues
];
// TO CHECK: this seems to solve validation layer errors but the array pointer will not be deleted
std
::
fill_n
(
qPriorities
,
maxCreatableQueues
,
1.
f
);
// all queues have the same priorities
vk
::
DeviceQueueCreateInfo
qCreateInfo
(
vk
::
DeviceQueueCreateFlags
(),
i
,
maxCreatableQueues
,
qPriorities
);
queueCreateInfos
.
push_back
(
qCreateInfo
);
create
-=
maxCreatableQueues
;
}
return
queueCreateInfos
;
...
...
@@ -267,9 +265,9 @@ namespace vkcv {
const
char
**
glfwExtensions
=
glfwGetRequiredInstanceExtensions
(
&
glfwExtensionCount
);
std
::
vector
<
const
char
*>
extensions
(
glfwExtensions
,
glfwExtensions
+
glfwExtensionCount
);
if
(
enableValidationLayers
)
{
extensions
.
push_back
(
VK_EXT_DEBUG_UTILS_EXTENSION_NAME
);
}
#
if
_DEBUG
extensions
.
push_back
(
VK_EXT_DEBUG_UTILS_EXTENSION_NAME
);
#endif
return
extensions
;
}
...
...
This diff is collapsed.
Click to expand it.
src/vkcv/Context.hpp
+
1
−
6
View file @
0e8cdfb5
...
...
@@ -3,11 +3,6 @@
#include
<GLFW/glfw3.h>
#include
<iostream>
#ifdef NDEBUG
const
bool
enableValidationLayers
=
false
;
#else
const
bool
enableValidationLayers
=
true
;
#endif
namespace
vkcv
{
...
...
@@ -41,7 +36,7 @@ namespace vkcv {
static
Context
create
(
const
char
*
applicationName
,
uint32_t
applicationVersion
,
uint32_t
queueCount
=
1
,
const
std
::
vector
<
vk
::
QueueFlagBits
>
queueFlags
=
{},
std
::
vector
<
const
char
*>
instanceExtensions
=
{},
std
::
vector
<
const
char
*>
deviceExtensions
=
{});
static
bool
checkSupport
(
std
::
vector
<
const
char
*>
&
supported
,
std
::
vector
<
const
char
*>
&
check
);
static
std
::
vector
<
const
char
*>
getRequiredExtensions
();
static
vk
::
PhysicalDevice
Context
::
pickPhysicalDevice
(
vk
::
Instance
&
instance
);
static
vk
::
PhysicalDevice
pickPhysicalDevice
(
vk
::
Instance
&
instance
);
static
int
deviceScore
(
const
vk
::
PhysicalDevice
&
physicalDevice
);
static
std
::
vector
<
vk
::
DeviceQueueCreateInfo
>
getQueueCreateInfos
(
vk
::
PhysicalDevice
&
physicalDevice
,
uint32_t
queueCount
,
std
::
vector
<
vk
::
QueueFlagBits
>
&
queueFlags
);
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment