Skip to content

Commit

Permalink
start config extension implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LDAP committed Nov 8, 2024
1 parent 5eab4e5 commit ed8876a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
4 changes: 3 additions & 1 deletion include/merian/vk/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Context : public std::enable_shared_from_this<Context> {
create(const std::vector<std::shared_ptr<Extension>>& extensions,
const std::string& application_name = "",
uint32_t application_vk_version = VK_MAKE_VERSION(1, 0, 0),
uint32_t vk_api_version = VK_API_VERSION_1_3,
uint32_t preffered_number_compute_queues = 1, // Additionally to the GCT queue
uint32_t filter_vendor_id = -1,
uint32_t filter_device_id = -1,
Expand All @@ -103,6 +104,7 @@ class Context : public std::enable_shared_from_this<Context> {
Context(const std::vector<std::shared_ptr<Extension>>& extensions,
const std::string& application_name,
uint32_t application_vk_version,
uint32_t vk_api_version,
uint32_t preffered_number_compute_queues,
uint32_t filter_vendor_id,
uint32_t filter_device_id,
Expand Down Expand Up @@ -207,7 +209,7 @@ class Context : public std::enable_shared_from_this<Context> {

public:
const std::string application_name;
const uint32_t vk_api_version = VK_API_VERSION_1_3;
const uint32_t vk_api_version;
const uint32_t application_vk_version;

// in create_instance
Expand Down
20 changes: 20 additions & 0 deletions include/merian/vk/extension/extension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,20 @@ class Extension {
virtual void* pnext_instance_create_info(void* const p_next) {
return p_next;
}

virtual void on_instance_created(const vk::Instance& /*unused*/) {}

/**
* Vote against a physical device by returning false. The context attemps to select a physical
* device that is accepted by most extensions, meaning this is not a hard criteria and you
* should check support in "extension_supported" again!
*/
virtual bool
accept_physical_device([[maybe_unused]] const vk::PhysicalDevice& physical_device,
[[maybe_unused]] const vk::PhysicalDeviceProperties2& props) {
return true;
}

/* Called after the physical device was select and before extensions are checked for
* compativility and check_support is called.*/
virtual void on_physical_device_selected(const Context::PhysicalDeviceContainer& /*unused*/) {}
Expand All @@ -80,6 +93,7 @@ class Extension {
virtual bool extension_supported(const Context::PhysicalDeviceContainer& /*unused*/) {
return true;
}

/* E.g. to dismiss a queue that does not support present-to-surface. */
virtual bool accept_graphics_queue([[maybe_unused]] const vk::Instance& instance,
[[maybe_unused]] const vk::PhysicalDevice& physical_device,
Expand All @@ -99,15 +113,21 @@ class Extension {
/* Do not change pNext. You can use pnext_device_create_info for that. */
virtual void enable_device_features(const Context::FeaturesContainer& /*supportedFeatures*/,
Context::FeaturesContainer& /*enableFeatures*/) {}

virtual void on_device_created(const vk::Device& /*unused*/) {}

/* Called right before context constructor returns. */
virtual void on_context_created(const ContextHandle& /*unused*/) {}

/* Called after device is idle and before context is destroyed. */
virtual void on_destroy_context() {}

/* Called right before device is destroyed. */
virtual void on_destroy_device(const vk::Device& /*unused*/) {}

/* Called before the instance is destroyed or if the extension is determined as unsupported. */
virtual void on_destroy_instance(const vk::Instance& /*unused*/) {}

// Called by context if extension was determined as unsupported. The extension might not receive
// further callbacks.
virtual void on_unsupported([[maybe_unused]] const std::string& reason) {
Expand Down
4 changes: 2 additions & 2 deletions include/merian/vk/extension/extension_resources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class ExtensionResources : public Extension {
ExtensionResources() : Extension("ExtensionResources") {}
~ExtensionResources() {}

void on_physical_device_selected(const Context::PhysicalDeviceContainer&) override;
void on_physical_device_selected(const Context::PhysicalDeviceContainer& /*unused*/) override;

std::vector<const char*> required_device_extension_names(vk::PhysicalDevice) const override;
std::vector<const char*> required_device_extension_names(vk::PhysicalDevice /*unused*/) const override;

void enable_device_features(const Context::FeaturesContainer& supported,
Context::FeaturesContainer& enable) override;
Expand Down
30 changes: 23 additions & 7 deletions src/merian/vk/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ namespace merian {
ContextHandle Context::create(const std::vector<std::shared_ptr<Extension>>& extensions,
const std::string& application_name,
uint32_t application_vk_version,
uint32_t vk_api_version,
uint32_t preffered_number_compute_queues,
uint32_t filter_vendor_id,
uint32_t filter_device_id,
const std::string& filter_device_name) {

const ContextHandle context = std::shared_ptr<Context>(new Context(
extensions, application_name, application_vk_version, preffered_number_compute_queues,
filter_vendor_id, filter_device_id, filter_device_name));
extensions, application_name, application_vk_version, vk_api_version,
preffered_number_compute_queues, filter_vendor_id, filter_device_id, filter_device_name));

for (auto& ext : context->extensions) {
ext.second->on_context_created(context);
Expand All @@ -36,11 +37,13 @@ ContextHandle Context::create(const std::vector<std::shared_ptr<Extension>>& ext
Context::Context(const std::vector<std::shared_ptr<Extension>>& desired_extensions,
const std::string& application_name,
uint32_t application_vk_version,
uint32_t vk_api_version,
uint32_t preffered_number_compute_queues,
uint32_t filter_vendor_id,
uint32_t filter_device_id,
const std::string& filter_device_name)
: application_name(application_name), application_vk_version(application_vk_version) {
: application_name(application_name), vk_api_version(vk_api_version),
application_vk_version(application_vk_version) {
SPDLOG_INFO("\n\n\
__ __ ___ ___ ___ _ _ _ \n\
| \\/ | __| _ \\_ _| /_\\ | \\| |\n\
Expand Down Expand Up @@ -173,7 +176,8 @@ void Context::prepare_physical_device(uint32_t filter_vendor_id,
filter_device_name = env_device_name;
}

std::vector<std::tuple<vk::PhysicalDevice, vk::PhysicalDeviceProperties2>> matches;
// (device, props, number_of_accept_votes)
std::vector<std::tuple<vk::PhysicalDevice, vk::PhysicalDeviceProperties2, uint32_t>> matches;
for (std::size_t i = 0; i < devices.size(); i++) {
vk::PhysicalDeviceProperties2 props = devices[i].getProperties2();
SPDLOG_INFO("found physical device {}, vendor id: {}, device id: {}, Vulkan: {}.{}.{}",
Expand All @@ -184,7 +188,14 @@ void Context::prepare_physical_device(uint32_t filter_vendor_id,
if ((filter_vendor_id == (uint32_t)-1 || filter_vendor_id == props.properties.vendorID) &&
(filter_device_id == (uint32_t)-1 || filter_device_id == props.properties.deviceID) &&
(filter_device_name == "" || filter_device_name == props.properties.deviceName)) {
matches.emplace_back(devices[i], props);

uint32_t number_of_accept_votes = 0;
for (auto& ext : extensions) {
number_of_accept_votes +=
static_cast<uint32_t>(ext.second->accept_physical_device(devices[i], props));
}

matches.emplace_back(devices[i], props, number_of_accept_votes);
}
}

Expand All @@ -196,8 +207,13 @@ void Context::prepare_physical_device(uint32_t filter_vendor_id,
filter_device_name.empty() ? "any" : filter_device_name));
}
std::sort(matches.begin(), matches.end(),
[](std::tuple<vk::PhysicalDevice, vk::PhysicalDeviceProperties2>& a,
std::tuple<vk::PhysicalDevice, vk::PhysicalDeviceProperties2>& b) {
[](std::tuple<vk::PhysicalDevice, vk::PhysicalDeviceProperties2, uint32_t>& a,
std::tuple<vk::PhysicalDevice, vk::PhysicalDeviceProperties2, uint32_t>& b) {
// compare number of accept votes.
if (std::get<2>(a) != std::get<2>(b)) {
return std::get<2>(a) < std::get<2>(b);
}

const vk::PhysicalDeviceProperties& props_a = std::get<1>(a).properties;
const vk::PhysicalDeviceProperties& props_b = std::get<1>(b).properties;
if (props_a.deviceType != props_b.deviceType) {
Expand Down
4 changes: 2 additions & 2 deletions src/merian/vk/extension/extension_resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ ExtensionResources::required_device_extension_names(vk::PhysicalDevice) const {

void ExtensionResources::enable_device_features(const Context::FeaturesContainer& supported,
Context::FeaturesContainer& enable) {
if (supported.physical_device_features_v12.bufferDeviceAddress) {
if (supported.physical_device_features_v12.bufferDeviceAddress == VK_TRUE) {
SPDLOG_DEBUG("bufferDeviceAddress supported. Enabling feature");
enable.physical_device_features_v12.bufferDeviceAddress = true;
enable.physical_device_features_v12.bufferDeviceAddress = VK_TRUE;
flags |= VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT;
}
}
Expand Down

0 comments on commit ed8876a

Please sign in to comment.