load: hook vkCreateInstance as a means to setup global vtables

This commit is contained in:
Danny Robson 2019-03-03 12:59:51 +11:00
parent 45953b9dcf
commit 2828fd24f3
4 changed files with 59 additions and 18 deletions

View File

@ -66,6 +66,7 @@ list (APPEND VK_LOAD_SOURCES
${GENERATED_PREFIX}/load/vtable.hpp ${GENERATED_PREFIX}/load/vtable.hpp
${GENERATED_PREFIX}/load/dispatch.cpp ${GENERATED_PREFIX}/load/dispatch.cpp
load/dispatch.hpp load/dispatch.hpp
load/init.cpp
load/ostream.cpp load/ostream.cpp
load/ostream.hpp load/ostream.hpp
load/vendor.hpp load/vendor.hpp

50
load/init.cpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#include <cruft/vk/load/vtable.hpp>
#include <cruft/vk/load/vendor.hpp>
#include <vector>
///////////////////////////////////////////////////////////////////////////////
// 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;

View File

@ -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::vendor_table const *cruft::vk::load::v_table = nullptr;
cruft::vk::load::instance_table const *cruft::vk::load::i_table = nullptr;

View File

@ -27,22 +27,13 @@
int int
main (int, char**) 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 std::cout << "[ "
<< "{ vendor: " << i << "available_layers: [ "
<< ", available_layers: [ "
<< cruft::make_infix (cruft::vk::instance::available_layers ()) << cruft::make_infix (cruft::vk::instance::available_layers ())
<< " ] }" << " ] }"
<< ", instance: " << instance << ", instance: " << instance
<< ", devices: [ " << cruft::make_infix (cruft::vk::physical_device::find (instance)) << " ], " << ", devices: [ " << cruft::make_infix (cruft::vk::physical_device::find (instance)) << " ], "
<< " }, "; << " } ]\n";
}
std::cout << " ]\n";
} }