Replaced std::string with const char *

Now checks extensions being valid on the instance.
This commit is contained in:
Charles Giessen 2020-02-11 17:51:03 -07:00
parent 2b711f30d5
commit cf8411ae78
3 changed files with 58 additions and 59 deletions

View File

@ -172,17 +172,17 @@ detail::Expected<Instance, detail::Error<InstanceError>> InstanceBuilder::build
VkApplicationInfo app_info = {}; VkApplicationInfo app_info = {};
app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
app_info.pNext = nullptr; app_info.pNext = nullptr;
app_info.pApplicationName = info.app_name.c_str (); app_info.pApplicationName = info.app_name;
app_info.applicationVersion = info.application_version; app_info.applicationVersion = info.application_version;
app_info.pEngineName = info.engine_name.c_str (); app_info.pEngineName = info.engine_name;
app_info.engineVersion = info.engine_version; app_info.engineVersion = info.engine_version;
app_info.apiVersion = info.api_version; app_info.apiVersion = info.api_version;
std::vector<const char*> extensions; std::vector<const char*> extensions;
for (auto& ext : info.extensions) for (auto& ext : info.extensions)
extensions.push_back (ext.c_str ()); extensions.push_back (ext);
if (info.debug_callback != nullptr) { if (info.debug_callback != nullptr) {
extensions.push_back ("VK_EXT_debug_utils"); extensions.push_back (VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
} }
if (!info.headless_context) { if (!info.headless_context) {
@ -208,7 +208,7 @@ detail::Expected<Instance, detail::Error<InstanceError>> InstanceBuilder::build
std::vector<const char*> layers; std::vector<const char*> layers;
for (auto& layer : info.layers) for (auto& layer : info.layers)
layers.push_back (layer.c_str ()); layers.push_back (layer);
if (info.enable_validation_layers) { if (info.enable_validation_layers) {
layers.push_back ("VK_LAYER_KHRONOS_validation"); layers.push_back ("VK_LAYER_KHRONOS_validation");
@ -280,11 +280,11 @@ detail::Expected<Instance, detail::Error<InstanceError>> InstanceBuilder::build
return instance; return instance;
} }
InstanceBuilder& InstanceBuilder::set_app_name (std::string app_name) { InstanceBuilder& InstanceBuilder::set_app_name (const char* app_name) {
info.app_name = app_name; info.app_name = app_name;
return *this; return *this;
} }
InstanceBuilder& InstanceBuilder::set_engine_name (std::string engine_name) { InstanceBuilder& InstanceBuilder::set_engine_name (const char* engine_name) {
info.engine_name = engine_name; info.engine_name = engine_name;
return *this; return *this;
} }
@ -300,11 +300,11 @@ InstanceBuilder& InstanceBuilder::set_api_version (uint32_t major, uint32_t mino
info.api_version = VK_MAKE_VERSION (major, minor, patch); info.api_version = VK_MAKE_VERSION (major, minor, patch);
return *this; return *this;
} }
InstanceBuilder& InstanceBuilder::add_layer (std::string layer_name) { InstanceBuilder& InstanceBuilder::add_layer (const char* layer_name) {
info.layers.push_back (layer_name); info.layers.push_back (layer_name);
return *this; return *this;
} }
InstanceBuilder& InstanceBuilder::add_extension (std::string extension_name) { InstanceBuilder& InstanceBuilder::add_extension (const char* extension_name) {
info.extensions.push_back (extension_name); info.extensions.push_back (extension_name);
return *this; return *this;
} }
@ -412,13 +412,13 @@ VkFormat find_supported_format (VkPhysicalDevice physical_device,
return VK_FORMAT_UNDEFINED; return VK_FORMAT_UNDEFINED;
} }
std::vector<std::string> check_device_extension_support ( std::vector<const char*> check_device_extension_support (
VkPhysicalDevice device, std::vector<std::string> desired_extensions) { VkPhysicalDevice device, std::vector<const char*> desired_extensions) {
auto available_extensions = auto available_extensions =
detail::get_vector<VkExtensionProperties> (vkEnumerateDeviceExtensionProperties, device, nullptr); detail::get_vector<VkExtensionProperties> (vkEnumerateDeviceExtensionProperties, device, nullptr);
if (!available_extensions.has_value ()) return {}; if (!available_extensions.has_value ()) return {};
std::vector<std::string> extensions_to_enable; std::vector<const char*> extensions_to_enable;
for (const auto& extension : available_extensions.value ()) { for (const auto& extension : available_extensions.value ()) {
for (auto& req_ext : desired_extensions) { for (auto& req_ext : desired_extensions) {
if (req_ext == extension.extensionName) extensions_to_enable.push_back (req_ext); if (req_ext == extension.extensionName) extensions_to_enable.push_back (req_ext);
@ -701,20 +701,20 @@ PhysicalDeviceSelector& PhysicalDeviceSelector::desired_device_memory_size (VkDe
criteria.desired_mem_size = size; criteria.desired_mem_size = size;
return *this; return *this;
} }
PhysicalDeviceSelector& PhysicalDeviceSelector::add_required_extension (std::string extension) { PhysicalDeviceSelector& PhysicalDeviceSelector::add_required_extension (const char* extension) {
criteria.required_extensions.push_back (extension); criteria.required_extensions.push_back (extension);
return *this; return *this;
} }
PhysicalDeviceSelector& PhysicalDeviceSelector::add_required_extensions (std::vector<std::string> extensions) { PhysicalDeviceSelector& PhysicalDeviceSelector::add_required_extensions (std::vector<const char*> extensions) {
criteria.required_extensions.insert ( criteria.required_extensions.insert (
criteria.required_extensions.end (), extensions.begin (), extensions.end ()); criteria.required_extensions.end (), extensions.begin (), extensions.end ());
return *this; return *this;
} }
PhysicalDeviceSelector& PhysicalDeviceSelector::add_desired_extension (std::string extension) { PhysicalDeviceSelector& PhysicalDeviceSelector::add_desired_extension (const char* extension) {
criteria.desired_extensions.push_back (extension); criteria.desired_extensions.push_back (extension);
return *this; return *this;
} }
PhysicalDeviceSelector& PhysicalDeviceSelector::add_desired_extensions (std::vector<std::string> extensions) { PhysicalDeviceSelector& PhysicalDeviceSelector::add_desired_extensions (std::vector<const char*> extensions) {
criteria.desired_extensions.insert ( criteria.desired_extensions.insert (
criteria.desired_extensions.end (), extensions.begin (), extensions.end ()); criteria.desired_extensions.end (), extensions.begin (), extensions.end ());
return *this; return *this;
@ -787,7 +787,7 @@ detail::Expected<Device, detail::Error<DeviceError>> DeviceBuilder::build () {
std::vector<const char*> extensions; std::vector<const char*> extensions;
for (auto& ext : info.extensions) for (auto& ext : info.extensions)
extensions.push_back (ext.c_str ()); extensions.push_back (ext);
if (info.physical_device.surface != VK_NULL_HANDLE) if (info.physical_device.surface != VK_NULL_HANDLE)
extensions.push_back ({ VK_KHR_SWAPCHAIN_EXTENSION_NAME }); extensions.push_back ({ VK_KHR_SWAPCHAIN_EXTENSION_NAME });

View File

@ -2,8 +2,6 @@
#include <cassert> #include <cassert>
#include <array>
#include <string>
#include <vector> #include <vector>
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
@ -130,18 +128,18 @@ class InstanceBuilder {
public: public:
detail::Expected<Instance, detail::Error<InstanceError>> build (); // use builder pattern detail::Expected<Instance, detail::Error<InstanceError>> build (); // use builder pattern
InstanceBuilder& set_app_name (std::string app_name); InstanceBuilder& set_app_name (const char* app_name);
InstanceBuilder& set_engine_name (std::string engine_name); InstanceBuilder& set_engine_name (const char* engine_name);
InstanceBuilder& set_app_version (uint32_t major, uint32_t minor, uint32_t patch); InstanceBuilder& set_app_version (uint32_t major, uint32_t minor, uint32_t patch);
InstanceBuilder& set_engine_version (uint32_t major, uint32_t minor, uint32_t patch); InstanceBuilder& set_engine_version (uint32_t major, uint32_t minor, uint32_t patch);
InstanceBuilder& set_api_version (uint32_t major, uint32_t minor, uint32_t patch); InstanceBuilder& set_api_version (uint32_t major, uint32_t minor, uint32_t patch);
InstanceBuilder& add_layer (std::string app_name); InstanceBuilder& add_layer (const char* app_name);
InstanceBuilder& add_extension (std::string app_name); InstanceBuilder& add_extension (const char* app_name);
bool check_and_add_layer (std::string app_name); bool check_and_add_layer (const char* app_name);
bool check_and_add_extension (std::string app_name); bool check_and_add_extension (const char* app_name);
InstanceBuilder& setup_validation_layers (bool enable_validation = true); InstanceBuilder& setup_validation_layers (bool enable_validation = true);
InstanceBuilder& set_headless (bool headless = false); InstanceBuilder& set_headless (bool headless = false);
@ -162,15 +160,15 @@ class InstanceBuilder {
private: private:
struct InstanceInfo { struct InstanceInfo {
// VkApplicationInfo // VkApplicationInfo
std::string app_name; const char* app_name;
std::string engine_name; const char* engine_name;
uint32_t application_version = 0; uint32_t application_version = 0;
uint32_t engine_version = 0; uint32_t engine_version = 0;
uint32_t api_version = VK_MAKE_VERSION (1, 0, 0); uint32_t api_version = VK_MAKE_VERSION (1, 0, 0);
// VkInstanceCreateInfo // VkInstanceCreateInfo
std::vector<std::string> layers; std::vector<const char*> layers;
std::vector<std::string> extensions; std::vector<const char*> extensions;
VkInstanceCreateFlags flags = 0; VkInstanceCreateFlags flags = 0;
std::vector<VkBaseOutStructure*> pNext_elements; std::vector<VkBaseOutStructure*> pNext_elements;
@ -228,7 +226,7 @@ struct PhysicalDevice {
private: private:
VkPhysicalDeviceFeatures features{}; VkPhysicalDeviceFeatures features{};
std::vector<std::string> extensions_to_enable; std::vector<const char*> extensions_to_enable;
std::vector<VkQueueFamilyProperties> queue_families; std::vector<VkQueueFamilyProperties> queue_families;
friend class PhysicalDeviceSelector; friend class PhysicalDeviceSelector;
friend class DeviceBuilder; friend class DeviceBuilder;
@ -253,11 +251,11 @@ struct PhysicalDeviceSelector {
PhysicalDeviceSelector& required_device_memory_size (VkDeviceSize size); PhysicalDeviceSelector& required_device_memory_size (VkDeviceSize size);
PhysicalDeviceSelector& desired_device_memory_size (VkDeviceSize size); PhysicalDeviceSelector& desired_device_memory_size (VkDeviceSize size);
PhysicalDeviceSelector& add_required_extension (std::string extension); PhysicalDeviceSelector& add_required_extension (const char* extension);
PhysicalDeviceSelector& add_required_extensions (std::vector<std::string> extensions); PhysicalDeviceSelector& add_required_extensions (std::vector<const char*> extensions);
PhysicalDeviceSelector& add_desired_extension (std::string extension); PhysicalDeviceSelector& add_desired_extension (const char* extension);
PhysicalDeviceSelector& add_desired_extensions (std::vector<std::string> extensions); PhysicalDeviceSelector& add_desired_extensions (std::vector<const char*> extensions);
PhysicalDeviceSelector& set_desired_version (uint32_t major, uint32_t minor); PhysicalDeviceSelector& set_desired_version (uint32_t major, uint32_t minor);
PhysicalDeviceSelector& set_minimum_version (uint32_t major = 1, uint32_t minor = 0); PhysicalDeviceSelector& set_minimum_version (uint32_t major = 1, uint32_t minor = 0);
@ -292,8 +290,8 @@ struct PhysicalDeviceSelector {
VkDeviceSize required_mem_size = 0; VkDeviceSize required_mem_size = 0;
VkDeviceSize desired_mem_size = 0; VkDeviceSize desired_mem_size = 0;
std::vector<std::string> required_extensions; std::vector<const char*> required_extensions;
std::vector<std::string> desired_extensions; std::vector<const char*> desired_extensions;
uint32_t required_version = VK_MAKE_VERSION (1, 0, 0); uint32_t required_version = VK_MAKE_VERSION (1, 0, 0);
uint32_t desired_version = VK_MAKE_VERSION (1, 0, 0); uint32_t desired_version = VK_MAKE_VERSION (1, 0, 0);
@ -346,7 +344,7 @@ class DeviceBuilder {
VkDeviceCreateFlags flags = 0; VkDeviceCreateFlags flags = 0;
std::vector<VkBaseOutStructure*> pNext_chain; std::vector<VkBaseOutStructure*> pNext_chain;
PhysicalDevice physical_device; PhysicalDevice physical_device;
std::vector<std::string> extensions; std::vector<const char*> extensions;
std::vector<VkQueueFamilyProperties> queue_families; std::vector<VkQueueFamilyProperties> queue_families;
std::vector<CustomQueueDescription> queue_descriptions; std::vector<CustomQueueDescription> queue_descriptions;
bool request_compute_queue = true; bool request_compute_queue = true;

View File

@ -1,6 +1,8 @@
#include "common.h" #include "common.h"
int test_happy_path () { int test_happy_path () {
printf ("happy path\n");
auto window = create_window_glfw (); auto window = create_window_glfw ();
vkb::InstanceBuilder instance_builder; vkb::InstanceBuilder instance_builder;
@ -36,6 +38,7 @@ int test_happy_path () {
int test_instance_basic () { int test_instance_basic () {
printf ("\nbasic instance\n");
vkb::InstanceBuilder builder; vkb::InstanceBuilder builder;
@ -56,10 +59,13 @@ int test_instance_basic () {
if (!instance_ret.has_value ()) { if (!instance_ret.has_value ()) {
return 1; return 1;
} }
vkb::destroy_instance (instance_ret.value ());
return 0; return 0;
} }
int test_instance_headless () { int test_instance_headless () {
printf ("\nheadless instance\n");
vkb::InstanceBuilder builder; vkb::InstanceBuilder builder;
@ -74,10 +80,13 @@ int test_instance_headless () {
if (!instance_ret.has_value ()) { if (!instance_ret.has_value ()) {
return 1; return 1;
} }
vkb::destroy_instance (instance_ret.value ());
return 0; return 0;
} }
int test_physical_device_selection () { int test_physical_device_selection () {
printf ("\nphysical device selection\n");
vkb::InstanceBuilder instance_builder; vkb::InstanceBuilder instance_builder;
auto instance_ret = instance_builder.set_default_debug_messenger ().build (); auto instance_ret = instance_builder.set_default_debug_messenger ().build ();
auto instance = instance_ret.value (); auto instance = instance_ret.value ();
@ -100,15 +109,25 @@ int test_physical_device_selection () {
} }
int test_device_creation () { int test_device_creation () {
printf ("\ndevice creation\n");
vkb::InstanceBuilder instance_builder; vkb::InstanceBuilder instance_builder;
auto instance_ret = instance_builder.set_default_debug_messenger ().build (); auto instance_ret = instance_builder.set_default_debug_messenger ().build ();
if (!instance_ret.has_value ()) {
printf ("couldn't create instance %i\n", static_cast<uint32_t> (instance_ret.error ().type));
return -1;
}
auto instance = instance_ret.value (); auto instance = instance_ret.value ();
auto window = create_window_glfw (); auto window = create_window_glfw ();
auto surface = create_surface_glfw (instance.instance, window); auto surface = create_surface_glfw (instance.instance, window);
vkb::PhysicalDeviceSelector selector (instance); vkb::PhysicalDeviceSelector selector (instance);
auto phys_dev_ret = selector.set_surface (surface).select (); auto phys_dev_ret = selector.set_surface (surface).select ();
auto phys_dev = phys_dev_ret.value (); auto phys_dev = phys_dev_ret.value ();
if (!phys_dev_ret.has_value ()) {
printf ("couldn't select device %i\n", static_cast<uint32_t> (phys_dev_ret.error ().type));
return -1;
}
vkb::DeviceBuilder device_builder (phys_dev); vkb::DeviceBuilder device_builder (phys_dev);
auto dev_ret = device_builder.build (); auto dev_ret = device_builder.build ();
@ -124,27 +143,9 @@ int test_device_creation () {
} }
int main () { int main () {
printf ("happy path\n");
test_happy_path (); test_happy_path ();
test_instance_basic ();
printf ("\nbasic instance\n"); test_instance_headless ();
{ test_physical_device_selection ();
auto ret = test_instance_basic (); test_device_creation ();
if (ret != 0) printf ("test_failed\n");
}
printf ("\nheadless instance\n");
{
auto ret = test_instance_headless ();
if (ret != 0) printf ("test_failed\n");
}
printf ("\nphysical device selection\n");
{
auto ret = test_physical_device_selection ();
if (ret != 0) printf ("test_failed\n");
}
printf ("\ndevice creation\n");
{
auto ret = test_device_creation ();
if (ret != 0) printf ("test_failed\n");
}
} }