Add Vulkan function dispatcher which queries pointers through vkGet*ProcAddress (#176)

This commit is contained in:
Markus Tavenrath 2018-01-29 23:39:35 +08:00 committed by Andreas Süßenbach
parent 07b4e69c9d
commit 4b77709e49
4 changed files with 1221 additions and 536 deletions

View File

@ -323,6 +323,23 @@ std::vector<LayerProperties, MyCustomAllocator> properties = physicalDevice.enum
All over vulkan.hpp, there are a couple of calls to an assert function. By defining VULKAN_HPP_ASSERT, you can specifiy your own custom assert function to be called instead. All over vulkan.hpp, there are a couple of calls to an assert function. By defining VULKAN_HPP_ASSERT, you can specifiy your own custom assert function to be called instead.
### Extensions / Per Device function pointers
The Vulkan loader exposes only the Vulkan core functions and a limited number of extensions. To use Vulkan-Hpp with extensions it's required to have either a library which provides stubs to all used Vulkan
functions or to tell Vulkan-Hpp to dispatch those functions pointers. Vulkan-Hpp provides a per-function dispatch mechanism by accepting a dispatch class as last parameter in each function call. The dispatch
class must provide a callable type for each used Vulkan function. Vulkan-Hpp provides one implementation, ```DispatchLoaderDynamic```, which fetches all function pointers known to the library.
```c++
// This dispatch class will fetch all function pointers through the passed instance
vk::DispatchLoaderDynamic dldi(instance);
// This dispatch class will fetch function pointers for the passed device if possible, else for the passed instance
vk::DispatchLoaderDynamic dldid(instance, device);
// Pass dispatch class to function call as last parameter
device.getQueue(graphics_queue_family_index, 0, &graphics_queue, dldid);
```
## See Also ## See Also
Feel free to submit a PR to add to this list. Feel free to submit a PR to add to this list.

View File

@ -3936,7 +3936,7 @@ void VulkanHppGenerator::writeFunctionHeaderReturnType(std::ostream & os, std::s
void VulkanHppGenerator::writeFunctionHeaderTemplate(std::ostream & os, std::string const& indentation, CommandData const& commandData, bool enhanced, bool withDefault, bool isStructureChain) void VulkanHppGenerator::writeFunctionHeaderTemplate(std::ostream & os, std::string const& indentation, CommandData const& commandData, bool enhanced, bool withDefault, bool isStructureChain)
{ {
std::string dispatch = withDefault ? std::string("typename Dispatch = DispatchLoader") : std::string("typename Dispatch"); std::string dispatch = withDefault ? std::string("typename Dispatch = DispatchLoaderStatic") : std::string("typename Dispatch");
if (enhanced && isStructureChain) if (enhanced && isStructureChain)
{ {
os << indentation << "template <typename ...T, " << dispatch << ">" << std::endl; os << indentation << "template <typename ...T, " << dispatch << ">" << std::endl;
@ -4818,9 +4818,9 @@ void VulkanHppGenerator::EnumData::addEnumValue(std::string const &name, std::st
} }
} }
void VulkanHppGenerator::writeDelegationClass(std::ostream &os) void VulkanHppGenerator::writeDelegationClassStatic(std::ostream &os)
{ {
os << "class DispatchLoader" << std::endl os << "class DispatchLoaderStatic" << std::endl
<< "{" << std::endl << "{" << std::endl
<< "public:\n"; << "public:\n";
@ -4860,6 +4860,49 @@ void VulkanHppGenerator::writeDelegationClass(std::ostream &os)
os << "};\n"; os << "};\n";
} }
void VulkanHppGenerator::writeDelegationClassDynamic(std::ostream &os)
{
os << " class DispatchLoaderDynamic" << std::endl
<< " {" << std::endl
<< " public:" << std::endl;
for (auto command : m_commands)
{
enterProtect(os, command.second.protect);
os << " PFN_vk" << startUpperCase(command.second.fullName) << " vk" << startUpperCase(command.second.fullName) << " = 0;" << std::endl;
leaveProtect(os, command.second.protect);
}
// write initialization function to fetch function pointers
os << " public:" << std::endl
<< " DispatchLoaderDynamic(Instance instance = Instance(), Device device = Device())" << std::endl
<< " {" << std::endl
<< " if (instance)" << std::endl
<< " {" << std::endl
<< " init(instance, device);" << std::endl
<< " }" << std::endl
<< " }" << std::endl << std::endl
<< " void init(Instance instance, Device device = Device())" << std::endl
<< " {" << std::endl;
for (auto command : m_commands)
{
enterProtect(os, command.second.protect);
if (!command.second.params.empty()
&& m_handles.find(command.second.params[0].type) != m_handles.end()
&& command.second.params[0].type != "Instance")
{
os << " vk" << startUpperCase(command.second.fullName) << " = PFN_vk" << startUpperCase(command.second.fullName)
<< "(device ? device.getProcAddr( \"vk" << startUpperCase(command.second.fullName) << "\") : instance.getProcAddr( \"vk" << startUpperCase(command.second.fullName) << "\"));" << std::endl;
}
else {
os << " vk" << startUpperCase(command.second.fullName) << " = PFN_vk" << startUpperCase(command.second.fullName) << "(instance.getProcAddr( \"vk" << startUpperCase(command.second.fullName) << "\"));" << std::endl;
}
leaveProtect(os, command.second.protect);
}
os << " }" << std::endl;
os << " };\n";
}
int main( int argc, char **argv ) int main( int argc, char **argv )
{ {
@ -4992,12 +5035,14 @@ int main( int argc, char **argv )
<< resultValueHeader << resultValueHeader
<< createResultValueHeader; << createResultValueHeader;
generator.writeDelegationClass(ofs); generator.writeDelegationClassStatic(ofs);
generator.writeTypes(ofs, defaultValues); generator.writeTypes(ofs, defaultValues);
generator.writeStructureChainValidation(ofs); generator.writeStructureChainValidation(ofs);
generator.writeToStringFunctions(ofs); generator.writeToStringFunctions(ofs);
generator.writeDelegationClassDynamic(ofs);
ofs << "} // namespace VULKAN_HPP_NAMESPACE" << std::endl ofs << "} // namespace VULKAN_HPP_NAMESPACE" << std::endl
<< std::endl << std::endl
<< "#endif" << std::endl; << "#endif" << std::endl;

View File

@ -46,7 +46,8 @@ class VulkanHppGenerator
void writeStructureChainValidation(std::ostream & os); void writeStructureChainValidation(std::ostream & os);
void writeToStringFunctions(std::ostream & os); void writeToStringFunctions(std::ostream & os);
void writeTypes(std::ostream & os, std::map<std::string, std::string> const& defaultValues); void writeTypes(std::ostream & os, std::map<std::string, std::string> const& defaultValues);
void writeDelegationClass(std::ostream &os); void writeDelegationClassStatic(std::ostream &os); // use exported symbols from loader
void writeDelegationClassDynamic(std::ostream &os); // use vkGet*ProcAddress to get function pointers
#if !defined(NDEBUG) #if !defined(NDEBUG)
void checkExtensionRequirements(); void checkExtensionRequirements();
void skipVendorIDs(tinyxml2::XMLElement const* element); void skipVendorIDs(tinyxml2::XMLElement const* element);

File diff suppressed because it is too large Load Diff