Skip to content
Merged
24 changes: 9 additions & 15 deletions demos/17-buffer-device-address/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import vk;
#include <glm/gtc/matrix_transform.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/hash.hpp>

#include <stdio.h>
#include <expected>

#include <tiny_obj_loader.h>

Expand Down Expand Up @@ -301,12 +300,10 @@ main() {
}

// setting up physical device
vk::physical_enumeration enumerate_devices{
.device_type = vk::physical_gpu::integrated,
};
vk::physical_device physical_device(api_instance, enumerate_devices);

vk::queue_indices queue_indices = physical_device.family_indices();
std::expected<vk::physical_device, VkResult> physical_device_expected =
api_instance.enumerate_physical_device(vk::physical_gpu::integrated);
vk::physical_device physical_device = physical_device_expected.value();

// setting up logical device
std::array<float, 1> priorities = { 0.f };
Expand Down Expand Up @@ -353,7 +350,8 @@ main() {
.features = device_features.data(),
.queue_priorities = priorities,
.extensions = extensions,
.queue_family_index = queue_indices.graphics,
// .queue_family_index = queue_indices.graphics,
.queue_family_index = 0,
};

vk::device logical_device(physical_device, logical_device_params);
Expand All @@ -366,9 +364,7 @@ main() {
vk::swapchain_params enumerate_swapchain_settings = {
.width = static_cast<uint32_t>(width),
.height = static_cast<uint32_t>(height),
.present_index =
physical_device.family_indices()
.graphics, // presentation index just uses the graphics index
.present_index = 0,
};

vk::swapchain main_swapchain(logical_device,
Expand All @@ -383,8 +379,6 @@ main() {

// querying presentable images
std::span<const VkImage> images = main_swapchain.get_images();

std::println("span<const VkImage>::size() = {}", images.size());
uint32_t image_count = static_cast<uint32_t>(images.size());

// Creating Images
Expand Down Expand Up @@ -434,7 +428,7 @@ main() {
for (size_t i = 0; i < swapchain_command_buffers.size(); i++) {
vk::command_params settings = {
.levels = vk::command_levels::primary,
.queue_index = enumerate_swapchain_settings.present_index,
.queue_index = 0,
.flags = vk::command_pool_flags::reset,
};

Expand Down Expand Up @@ -807,6 +801,6 @@ main() {
logical_device.destroy();
window_surface.destroy();
glfwDestroyWindow(window);
api_instance.destroy();
// api_instance.destruct();
return 0;
}
100 changes: 79 additions & 21 deletions vulkan-cpp/instance.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module;
#include <span>
#include <vector>
#include <vulkan/vulkan.h>
#include <expected>

export module vk:instance;

export import :types;
export import :utilities;
import :types;
import :utilities;
import :physical_device;

export namespace vk {
inline namespace v1 {
Expand All @@ -20,6 +22,8 @@ export namespace vk {
*/
class instance {
public:
instance() = delete("Disallow constructing empty vk::instance");

/**
* @param p_config sets the application information that vulkan has
* optionally.
Expand All @@ -44,23 +48,6 @@ export namespace vk {
.pApplicationInfo = &app_info
};

// Setting up validation layers properties
uint32_t layer_count = 0;
std::vector<VkLayerProperties> layer_properties;
vkEnumerateInstanceLayerProperties(&layer_count, nullptr);
// std::vector<VkLayerProperties> layer_properties(layer_count);
layer_properties.resize(layer_count);
vkEnumerateInstanceLayerProperties(&layer_count,
layer_properties.data());

for (const VkLayerProperties property : layer_properties) {
m_layer_properties.emplace_back(
property.layerName,
property.specVersion,
property.implementationVersion,
property.description);
}

// Setting up instance extensions
instance_ci.enabledExtensionCount =
static_cast<uint32_t>(p_config.extensions.size());
Expand Down Expand Up @@ -123,14 +110,81 @@ export namespace vk {
#endif
}

~instance() = default;

//! @return true if a valid VkInstance
[[nodiscard]] bool alive() const { return !m_instance; }

//! @return available validation layers
//! @return Requesting available validation layers
std::span<const layer_properties> validation() {
uint32_t layer_count = 0;
std::vector<VkLayerProperties> layer_properties;
vkEnumerateInstanceLayerProperties(&layer_count, nullptr);

layer_properties.resize(layer_count);
vkEnumerateInstanceLayerProperties(&layer_count,
layer_properties.data());

for (const VkLayerProperties property : layer_properties) {
m_layer_properties.emplace_back(
property.layerName,
property.specVersion,
property.implementationVersion,
property.description);
}

return m_layer_properties;
}

/**
* @brief Enumerate physical devices and select specific physical
* device to report properties from.
*
* @return vk::physical_device if successful
* @return VkResult if an unexpected error occurs
*
*
* ```C++
*
* vk::instance api_instance = ...;
* std::expected<vk::physical_device, VkResult> expected =
* api_instance.enumerate_physical_device(vk::physical_device_type::integrated);
*
* vk::physical_device physical_device = expected.value();
*
* ```
*/
std::expected<physical_device, VkResult> enumerate_physical_device(
physical_gpu p_device_type) {
uint32_t device_count = 0;
VkResult res = vkEnumeratePhysicalDevices(
m_instance, &device_count, nullptr);

if (res != VK_SUCCESS) {
return std::unexpected(res);
}

std::vector<VkPhysicalDevice> physical_devices(device_count);
res = vkEnumeratePhysicalDevices(
m_instance, &device_count, physical_devices.data());

if (res != VK_SUCCESS) {
return std::unexpected(res);
}

for (const auto& device : physical_devices) {
VkPhysicalDeviceProperties device_properties;
vkGetPhysicalDeviceProperties(device, &device_properties);

if (device_properties.deviceType ==
static_cast<VkPhysicalDeviceType>(p_device_type)) {
return physical_device(device);
}
}

return std::unexpected(res);
}

/**
* @brief returns function pointer to allow for setting debug object
* name
Expand All @@ -154,7 +208,11 @@ export namespace vk {
operator VkInstance() const { return m_instance; }

//! @brief Invokes the destruction of the VkInstance.
void destroy() {}
void destruct() {
if (m_instance != nullptr) {
vkDestroyInstance(m_instance, nullptr);
}
}

private:
VkInstance m_instance = nullptr;
Expand Down
Loading
Loading