diff --git a/CMakeLists.txt b/CMakeLists.txt index e880a53..45b9e9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,7 @@ list (APPEND VK_LOAD_SOURCES ${GENERATED_PREFIX}/load/vtable.hpp ${GENERATED_PREFIX}/load/dispatch.cpp load/dispatch.hpp + load/init.cpp load/ostream.cpp load/ostream.hpp load/vendor.hpp diff --git a/load/init.cpp b/load/init.cpp new file mode 100644 index 0000000..ff44534 --- /dev/null +++ b/load/init.cpp @@ -0,0 +1,50 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2019 Danny Robson + */ + + +#include +#include + +#include + + +/////////////////////////////////////////////////////////////////////////////// +// We need to ensure the tables are loaded before anything much gets called. +// +// Given we have a global vtable we'll install a default table that hooks the +// function(s) that a client will call first off. This function will: +// * find the various ICDs on the system, +// * install a vtable that forwards calls by default, +// * and then forwards the current call on to this newly installed vtable + + +/////////////////////////////////////////////////////////////////////////////// +static VkResult setup_vtables ( + const VkInstanceCreateInfo* pCreateInfo, + const VkAllocationCallbacks* pAllocator, + VkInstance* pInstance +) { + static auto libraries = cruft::vk::load::enumerate (); + static cruft::vk::load::vendor v (libraries[0]); + + cruft::vk::load::i_table = &v.itable; + cruft::vk::load::v_table = &v.vtable; + + return cruft::vk::load::i_table->vkCreateInstance (pCreateInfo, pAllocator, pInstance); +} + + +/////////////////////////////////////////////////////////////////////////////// +static cruft::vk::load::instance_table const initialiser { + .vkCreateInstance = setup_vtables, +}; + + +//----------------------------------------------------------------------------- +cruft::vk::load::instance_table const *cruft::vk::load::i_table = &initialiser; + diff --git a/load/vtable.cpp b/load/vtable.cpp index 994ef1b..745448a 100644 --- a/load/vtable.cpp +++ b/load/vtable.cpp @@ -5,4 +5,3 @@ using cruft::vk::load::instance_table; /////////////////////////////////////////////////////////////////////////////// cruft::vk::load::vendor_table const *cruft::vk::load::v_table = nullptr; -cruft::vk::load::instance_table const *cruft::vk::load::i_table = nullptr; diff --git a/tools/info.cpp b/tools/info.cpp index 02692c7..6a91290 100644 --- a/tools/info.cpp +++ b/tools/info.cpp @@ -27,22 +27,13 @@ int main (int, char**) { - std::cout << "[ "; - for (auto const &i: cruft::vk::load::enumerate ()) { - cruft::vk::load::vendor v (i); - cruft::vk::load::i_table = &v.itable; - cruft::vk::load::v_table = &v.vtable; + cruft::vk::instance instance; - cruft::vk::instance instance; - - std::cout - << "{ vendor: " << i - << ", available_layers: [ " - << cruft::make_infix (cruft::vk::instance::available_layers ()) - << " ] }" - << ", instance: " << instance - << ", devices: [ " << cruft::make_infix (cruft::vk::physical_device::find (instance)) << " ], " - << " }, "; - } - std::cout << " ]\n"; + std::cout << "[ " + << "available_layers: [ " + << cruft::make_infix (cruft::vk::instance::available_layers ()) + << " ] }" + << ", instance: " << instance + << ", devices: [ " << cruft::make_infix (cruft::vk::physical_device::find (instance)) << " ], " + << " } ]\n"; } \ No newline at end of file