Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ static_library(
glfw3
Vulkan
glm
stb

LINK_PACKAGES
glfw
Vulkan::Vulkan
glm::glm
stb::stb
)

target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_23)
Expand Down Expand Up @@ -57,6 +55,7 @@ target_sources(${PROJECT_NAME} PUBLIC
vulkan-cpp/descriptor_resource.cppm
vulkan-cpp/texture.cppm
vulkan-cpp/dyn/buffer.cppm
vulkan-cpp/image.cppm
)

install(
Expand Down
1 change: 0 additions & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def build_requirements(self):
def requirements(self):
self.requires("glfw/3.4")
self.requires("glm/1.0.1")
self.requires("stb/cci.20230920")

def layout(self):
cmake_layout(self)
Expand Down
1 change: 1 addition & 0 deletions demos/17-buffer-device-address/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ build_application(
tinyobjloader

LINK_PACKAGES
stb::stb
vulkan-cpp
tinyobjloader
Vulkan::Vulkan
Expand Down
93 changes: 86 additions & 7 deletions demos/17-buffer-device-address/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ import vk;
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/hash.hpp>
#include <expected>
#include <ranges>

#include <tiny_obj_loader.h>

#ifndef STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#endif

static VKAPI_ATTR VkBool32 VKAPI_CALL
debug_callback(
[[maybe_unused]] VkDebugUtilsMessageSeverityFlagBitsEXT p_message_severity,
Expand Down Expand Up @@ -240,6 +246,80 @@ struct push_constant_data {
uint64_t global_ubo_addr = 0;
};

/**
* @brief STBI-specific implementation of the vk::image interface
*/
class stb_image : public vk::image {
public:
stb_image() = delete;

stb_image(std::string_view p_path, vk::texture_params p_params) {
image_load(p_path, p_params);
}

~stb_image() = default;

protected:
bool image_load(std::string_view p_path,
vk::texture_params p_params) override {
int w = 0;
int h = 0;
int channels = 0;

stbi_uc* image_pixel_data =
stbi_load(p_path.data(), &w, &h, &channels, STBI_rgb_alpha);

if (!image_pixel_data) {
return false;
}

const VkFormat texture_format =
static_cast<VkFormat>(vk::format::r8g8b8a8_unorm);
int bytes_per_pixel = vk::bytes_per_texture_format(texture_format);

m_extent = {
.width = static_cast<uint32_t>(w),
.height = static_cast<uint32_t>(h),
};

// Retrieving total size of bytes of the dimensions of the image and
// accounting for pixels of the image
uint32_t size_bytes =
m_extent.width * m_extent.height * bytes_per_pixel;

// Retrieving total image size to the count of the image layers
uint32_t size = size_bytes * p_params.layer_count;

vk::image_params image_options = {
.extent = m_extent,
.format = texture_format,
.memory_mask = p_params.memory_mask,
.usage = static_cast<uint32_t>(vk::image_usage::transfer_dst_bit) |
static_cast<uint32_t>(vk::image_usage::sampled_bit),
.mip_levels = p_params.mip_levels,
.layer_count = p_params.layer_count,
};

m_bytes.reserve(size);
std::span<uint8_t> bytes_view =
std::span<uint8_t>(image_pixel_data, size);

m_bytes.assign(bytes_view.begin(), bytes_view.end());

stbi_image_free(image_pixel_data);

return true;
}

std::span<const uint8_t> image_read() const override { return m_bytes; }

vk::image_extent image_extent() const override { return m_extent; }

private:
vk::image_extent m_extent{};
std::vector<uint8_t> m_bytes{};
};

int
main() {
//! @note Just added the some test code to test the conan-starter setup code
Expand Down Expand Up @@ -590,14 +670,13 @@ main() {
vk::dyn::buffer(logical_device, sizeof(global_uniform), uniform_params);

vk::texture_params config_texture = {
.memory_mask =
physical_device.memory_properties(static_cast<vk::memory_property>(
vk::memory_property::host_visible_bit |
vk::memory_property::host_cached_bit)),
.memory_mask = physical_device.memory_properties(
vk::memory_property::host_visible_bit |
vk::memory_property::host_cached_bit),
};
vk::texture texture1(logical_device,
std::filesystem::path("asset_samples/viking_room.png"),
config_texture);

stb_image img = stb_image("asset_samples/viking_room.png", config_texture);
vk::texture texture1(logical_device, &img, config_texture);

// Setting the texture sampler/image view to descriptor resource
std::array<vk::write_image, 1> samplers = {
Expand Down
52 changes: 52 additions & 0 deletions vulkan-cpp/image.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module;

#include <string_view>
#include <span>

export module vk:image;

import :types;

export namespace vk {
inline namespace v1 {

/**
* @brief interface for the purpose of acting as an interface to
* different implementations for support variety of approaches in
* loading images.
*/
class image {
public:
virtual ~image() = default;

/**
* @brief Perform loading operations specific to the implementation
* of this interface
*/
bool load(std::string_view p_path, texture_params p_params) {
return image_load(p_path, p_params);
}

/**
* @brief Read you the bytes read from the specific image loading
* implementations
*/
[[nodiscard]] std::span<const uint8_t> read() const {
return image_read();
}

/**
* @return the extent that contains properties about the image
* (dimensions, depth, etc)
*/
[[nodiscard]] image_extent extent() const { return image_extent(); }

protected:
virtual bool image_load(std::string_view, texture_params) = 0;

virtual std::span<const uint8_t> image_read() const = 0;

virtual image_extent image_extent() const = 0;
};
};
};
Loading
Loading