Skip to content

Commit

Permalink
Add Vulkan texture
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Sep 6, 2024
1 parent 6021c2d commit 95992c4
Show file tree
Hide file tree
Showing 11 changed files with 581 additions and 22 deletions.
1 change: 1 addition & 0 deletions source/vulkan_renderer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ find_package(Vulkan REQUIRED)
find_package(VulkanMemoryAllocator CONFIG REQUIRED)
find_package(magic_enum CONFIG REQUIRED)
find_package(imgui CONFIG REQUIRED)
find_package(Stb REQUIRED)

add_subdirectory("lib")

Expand Down
7 changes: 7 additions & 0 deletions source/vulkan_renderer/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ target_sources(tactile-vulkan-renderer
"src/vulkan_shader_module.cpp"
"src/vulkan_surface.cpp"
"src/vulkan_swapchain.cpp"
"src/vulkan_texture.cpp"
"src/vulkan_util.cpp"

PUBLIC FILE_SET "HEADERS" BASE_DIRS "inc" FILES
"inc/tactile/vulkan_renderer/api.hpp"
"inc/tactile/vulkan_renderer/imgui_shader_code.hpp"
"inc/tactile/vulkan_renderer/vulkan_allocator.hpp"
"inc/tactile/vulkan_renderer/vulkan_buffer.hpp"
"inc/tactile/vulkan_renderer/vulkan_command_buffer.hpp"
Expand All @@ -57,6 +59,7 @@ target_sources(tactile-vulkan-renderer
"inc/tactile/vulkan_renderer/vulkan_shader_module.hpp"
"inc/tactile/vulkan_renderer/vulkan_surface.hpp"
"inc/tactile/vulkan_renderer/vulkan_swapchain.hpp"
"inc/tactile/vulkan_renderer/vulkan_texture.hpp"
"inc/tactile/vulkan_renderer/vulkan_util.hpp"
)

Expand All @@ -67,6 +70,10 @@ target_compile_definitions(tactile-vulkan-renderer
"TACTILE_BUILDING_VULKAN_RENDERER"
)

target_include_directories(tactile-vulkan-renderer
PRIVATE
${Stb_INCLUDE_DIR})

target_link_libraries(tactile-vulkan-renderer
PUBLIC
tactile::base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ TACTILE_VULKAN_API auto create_vulkan_buffer(VmaAllocator allocator,
const VmaAllocationCreateInfo& allocation_info)
-> std::expected<VulkanBuffer, VkResult>;

[[nodiscard]]
TACTILE_VULKAN_API auto create_vulkan_staging_buffer(VmaAllocator allocator,
std::uint64_t size,
VkBufferUsageFlags usage_flags)
-> std::expected<VulkanBuffer, VkResult>;

[[nodiscard]]
TACTILE_VULKAN_API auto set_buffer_data(VulkanBuffer& buffer,
const void* data,
std::uint64_t data_size) -> VkResult;

} // namespace tactile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#pragma once

#include <cstdint> // uint32_t
#include <expected> // expected
#include <algorithm> // max
#include <cmath> // floor, log2
#include <cstdint> // uint32_t, uint64_t
#include <expected> // expected

#include <vk_mem_alloc.h>
#include <vulkan/vulkan.h>
Expand Down Expand Up @@ -38,10 +40,39 @@ class TACTILE_VULKAN_API VulkanImage final
VmaAllocation allocation {VK_NULL_HANDLE};
VulkanImageParams params {};

[[nodiscard]]
auto change_layout(VkDevice device,
VkQueue queue,
VkCommandPool command_pool,
VkImageLayout new_layout) -> VkResult;

[[nodiscard]]
auto set_data(VkDevice device,
VkQueue queue,
VkCommandPool command_pool,
const void* data,
std::uint64_t data_size) -> VkResult;

[[nodiscard]]
auto copy_buffer(VkDevice device, VkQueue queue, VkCommandPool command_pool, VkBuffer buffer)
-> VkResult;

[[nodiscard]]
auto generate_mipmaps(VkDevice device,
VkQueue queue,
VkCommandPool command_pool) -> VkResult;

private:
void _destroy() noexcept;
};

[[nodiscard]]
constexpr auto calculate_vulkan_mip_levels(const VkExtent2D extent) -> std::uint32_t
{
const auto max_extent = std::max(extent.width, extent.height);
return 1u + static_cast<std::uint32_t>(std::floor(std::log2(max_extent)));
}

[[nodiscard]]
TACTILE_VULKAN_API auto create_vulkan_image(VmaAllocator allocator,
const VulkanImageParams& params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

#pragma once

#include <cstddef> // size_t
#include <vector> // vector
#include <cstddef> // size_t
#include <unordered_map> // unordered_map
#include <vector> // vector

#include <vulkan/vulkan.h>

#include "tactile/base/id.hpp"
#include "tactile/base/prelude.hpp"
#include "tactile/base/render/renderer.hpp"
#include "tactile/vulkan_renderer/api.hpp"
Expand All @@ -22,6 +24,7 @@
#include "tactile/vulkan_renderer/vulkan_semaphore.hpp"
#include "tactile/vulkan_renderer/vulkan_surface.hpp"
#include "tactile/vulkan_renderer/vulkan_swapchain.hpp"
#include "tactile/vulkan_renderer/vulkan_texture.hpp"

namespace tactile {

Expand Down Expand Up @@ -97,8 +100,9 @@ class TACTILE_VULKAN_API VulkanRenderer final : public IRenderer
PFN_vkCmdEndRenderingKHR m_vkCmdEndRendering {};
std::vector<VulkanFrame> m_frames {};
std::size_t m_frame_index {0};

VulkanImGuiContext m_imgui_context {};
std::unordered_map<TextureID, VulkanTexture> m_textures {};
TextureID m_next_texture_id {1};

void _record_commands() const;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

#pragma once

#include <expected> // expected
#include <filesystem> // path

#include <vk_mem_alloc.h>
#include <vulkan/vulkan.h>

#include "tactile/base/prelude.hpp"
#include "tactile/base/render/texture.hpp"
#include "tactile/vulkan_renderer/api.hpp"
#include "tactile/vulkan_renderer/vulkan_image.hpp"
#include "vulkan_image_view.hpp"

namespace tactile {

class TACTILE_VULKAN_API VulkanTexture final : public ITexture
{
public:
[[nodiscard]]
auto get_handle() const -> void* override;

[[nodiscard]]
auto get_size() const -> TextureSize override;

[[nodiscard]]
auto get_path() const -> const std::filesystem::path& override;

VulkanImage image {};
VulkanImageView view {};
std::filesystem::path path {};
void* imgui_handle {};
};

[[nodiscard]]
TACTILE_VULKAN_API auto load_vulkan_texture(VkDevice device,
VkQueue queue,
VkCommandPool command_pool,
VmaAllocator allocator,
VkSampler sampler,
const std::filesystem::path& image_path)
-> std::expected<VulkanTexture, VkResult>;

} // namespace tactile
57 changes: 56 additions & 1 deletion source/vulkan_renderer/lib/src/vulkan_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include "tactile/vulkan_renderer/vulkan_buffer.hpp"

#include <utility> // exchange
#include <algorithm> // min
#include <cstring> // memcpy
#include <utility> // exchange

#include "tactile/runtime/logging.hpp"
#include "tactile/vulkan_renderer/vulkan_util.hpp"
Expand Down Expand Up @@ -64,4 +66,57 @@ auto create_vulkan_buffer(VmaAllocator allocator,
return buffer;
}

auto create_vulkan_staging_buffer(VmaAllocator allocator,
const std::uint64_t size,
const VkBufferUsageFlags usage_flags)
-> std::expected<VulkanBuffer, VkResult>
{
const VkBufferCreateInfo buffer_info {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = size,
.usage = usage_flags | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.queueFamilyIndexCount = 0,
.pQueueFamilyIndices = nullptr,
};

constexpr VmaAllocationCreateInfo allocation_info {
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST,
.requiredFlags =
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
.preferredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
.memoryTypeBits = 0,
.pool = nullptr,
.pUserData = nullptr,
.priority = 0,
};

return create_vulkan_buffer(allocator, buffer_info, allocation_info);
}

auto set_buffer_data(VulkanBuffer& buffer,
const void* data,
const uint64 data_size) -> VkResult
{
void* mapped_data = nullptr;
const auto result = vmaMapMemory(buffer.allocator, buffer.allocation, &mapped_data);

if (result != VK_SUCCESS) {
return result;
}

VmaAllocationInfo allocation_info {};
vmaGetAllocationInfo(buffer.allocator, buffer.allocation, &allocation_info);

const auto allocation_size = static_cast<std::uint64_t>(allocation_info.size);
std::memcpy(mapped_data, data, std::min(data_size, allocation_size));

vmaUnmapMemory(buffer.allocator, buffer.allocation);

return result;
}

} // namespace tactile
4 changes: 2 additions & 2 deletions source/vulkan_renderer/lib/src/vulkan_descriptor_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ auto create_vulkan_imgui_descriptor_pool(VkDevice device)
constexpr VkDescriptorPoolSize pool_sizes[] = {
VkDescriptorPoolSize {
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = 1,
.descriptorCount = 8,
},
};

const VkDescriptorPoolCreateInfo descriptor_pool_info {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.pNext = nullptr,
.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT,
.maxSets = 5,
.maxSets = 64,
.poolSizeCount = 1,
.pPoolSizes = pool_sizes,
};
Expand Down
Loading

0 comments on commit 95992c4

Please sign in to comment.