Skip to content

Commit

Permalink
Merge pull request #352 from oddkiva/enh-wrap-vulkan-image
Browse files Browse the repository at this point in the history
ENH: wrap vulkan image
  • Loading branch information
oddkiva authored Oct 17, 2023
2 parents 7142333 + 89aa681 commit f6c0bf9
Show file tree
Hide file tree
Showing 14 changed files with 925 additions and 38 deletions.
8 changes: 4 additions & 4 deletions cpp/examples/Shakti/Vulkan/hello_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class VulkanTriangleRenderer : public kvk::GraphicsBackend
svk::record_copy_buffer(vbo_staging, _vbo, copy_cmd_buf);

SARA_DEBUG << "Submitting data transfer command...\n";
_graphics_queue.submit_copy_commands(copy_cmd_bufs);
_graphics_queue.submit_commands(copy_cmd_bufs);
_graphics_queue.wait();
}

Expand Down Expand Up @@ -198,7 +198,7 @@ class VulkanTriangleRenderer : public kvk::GraphicsBackend
svk::record_copy_buffer(ebo_staging, _ebo, copy_cmd_buf);

SARA_DEBUG << "Submitting data transfer command...\n";
_graphics_queue.submit_copy_commands(copy_cmd_bufs);
_graphics_queue.submit_commands(copy_cmd_bufs);
_graphics_queue.wait();
}

Expand Down Expand Up @@ -379,8 +379,8 @@ class VulkanTriangleRenderer : public kvk::GraphicsBackend

// Reset the command buffer associated to the current frame.
SARA_DEBUG << "[VK] Resetting for the command buffer...\n";
vkResetCommandBuffer(_graphics_cmd_bufs[_current_frame],
/*VkCommandBufferResetFlagBits*/ 0);
_graphics_cmd_bufs.reset(_current_frame,
/*VkCommandBufferResetFlagBits*/ 0);

// Record the draw command to be performed on this swapchain image.
SARA_CHECK(_framebuffers.fbs.size());
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/DO/Shakti/Vulkan/Buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ namespace DO::Shakti::Vulkan {
std::swap(_size, other._size);
}

auto get_memory_requirements() const -> VkMemoryRequirements
auto memory_requirements() const -> VkMemoryRequirements
{
auto mem_requirements = VkMemoryRequirements{};
vkGetBufferMemoryRequirements(_device, _handle, &mem_requirements);
Expand Down
9 changes: 7 additions & 2 deletions cpp/src/DO/Shakti/Vulkan/CommandBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ namespace DO::Shakti::Vulkan {

auto clear() -> void
{
if (_device == nullptr || _command_pool == nullptr)
if (_device == nullptr || _command_pool == nullptr ||
_command_buffers.empty())
return;

SARA_DEBUG << fmt::format(
Expand Down Expand Up @@ -107,7 +108,11 @@ namespace DO::Shakti::Vulkan {

auto reset(int i, VkCommandBufferResetFlags flags = 0) const -> void
{
vkResetCommandBuffer(_command_buffers[i], flags);
const auto status = vkResetCommandBuffer(_command_buffers[i], flags);
if (status != VK_SUCCESS)
throw std::runtime_error{fmt::format(
"[VK] Failed to reset to reset command buffer {}! Error code: {}",
i, static_cast<int>(status))};
}

auto data() const -> const VkCommandBuffer*
Expand Down
81 changes: 63 additions & 18 deletions cpp/src/DO/Shakti/Vulkan/DescriptorSet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ namespace DO::Shakti::Vulkan {
class DescriptorSetLayout
{
public:
class Builder;
friend class Builder;

DescriptorSetLayout() = default;

DescriptorSetLayout(const DescriptorSetLayout&) = delete;
Expand Down Expand Up @@ -63,43 +66,85 @@ namespace DO::Shakti::Vulkan {
std::swap(_handle, other._handle);
}

static auto create_for_single_ubo(VkDevice device) -> DescriptorSetLayout
private:
VkDevice _device = nullptr;
VkDescriptorSetLayout _handle = nullptr;
};

class DescriptorSetLayout::Builder
{
public:
explicit Builder(VkDevice device)
: _device{device}
{
}

auto push_uniform_buffer_layout_binding() -> DescriptorSetLayout::Builder&
{
// UBO object: matrix-view-projection matrix stack
auto ubo_layout_binding = VkDescriptorSetLayoutBinding{};

// In the vertex shader code, we have something like:
// layout(binding = 0) uniform UBO { ... } ubo;
ubo_layout_binding.binding = 0;
ubo_layout_binding.descriptorCount = 1;
ubo_layout_binding.binding = static_cast<std::uint32_t>(_bindings.size());
ubo_layout_binding.descriptorCount = 1; // TODO: see if this ever
// needs to change.
ubo_layout_binding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
ubo_layout_binding.pImmutableSamplers = nullptr;

// Accessible from the vertex shader only.
ubo_layout_binding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;

// We only need 1 set of descriptors for the MVP UBO.
_bindings.emplace_back(ubo_layout_binding);

return *this;
}

auto push_sampler_layout_binding() -> DescriptorSetLayout::Builder&
{
auto sampler_layout_binding = VkDescriptorSetLayoutBinding{};

sampler_layout_binding.binding =
static_cast<std::uint32_t>(_bindings.size());
sampler_layout_binding.descriptorCount = 1; // TODO: see if this ever
// needs to change.
sampler_layout_binding.descriptorType =
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
sampler_layout_binding.pImmutableSamplers = nullptr;

// Accessible from the fragment shader only.
sampler_layout_binding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;

_bindings.emplace_back(sampler_layout_binding);

return *this;
}

auto create() const -> DescriptorSetLayout
{
auto create_info = VkDescriptorSetLayoutCreateInfo{};
create_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
create_info.bindingCount = 1;
create_info.pBindings = &ubo_layout_binding;

// Finally really create the descriptor set layout.
auto ubo_set_layout = DescriptorSetLayout{};
ubo_set_layout._device = device;
const auto status = vkCreateDescriptorSetLayout(
device, &create_info, nullptr, &ubo_set_layout._handle);
create_info.bindingCount = static_cast<std::uint32_t>(_bindings.size());
create_info.pBindings = _bindings.data();

auto desc_set_layout = DescriptorSetLayout{};
desc_set_layout._device = _device;
const auto status = vkCreateDescriptorSetLayout( //
_device, &create_info, nullptr, //
&desc_set_layout._handle //
);
if (status != VK_SUCCESS)
throw std::runtime_error{fmt::format(
"[VK] Error: failed to create UBO set layout! Error code: {}",
static_cast<int>(status))};
throw std::runtime_error{
fmt::format("[VK] Error: failed to create descriptor set layout! "
"Error code: {}",
static_cast<int>(status))};

return ubo_set_layout;
return desc_set_layout;
}

private:
VkDevice _device = nullptr;
VkDescriptorSetLayout _handle = nullptr;
VkDevice _device = VK_NULL_HANDLE;
std::vector<VkDescriptorSetLayoutBinding> _bindings;
};


Expand Down
19 changes: 16 additions & 3 deletions cpp/src/DO/Shakti/Vulkan/DeviceMemory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#pragma once

#include <DO/Shakti/Vulkan/Buffer.hpp>
#include <DO/Shakti/Vulkan/Image.hpp>
#include <DO/Shakti/Vulkan/PhysicalDevice.hpp>

#include <fmt/format.h>
Expand Down Expand Up @@ -135,7 +136,7 @@ namespace DO::Shakti::Vulkan {
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT //
};

const auto mem_reqs = buffer.get_memory_requirements();
const auto mem_reqs = buffer.memory_requirements();
const auto mem_type =
_physical_device.find_memory_type(mem_reqs.memoryTypeBits, mem_props);

Expand All @@ -149,7 +150,7 @@ namespace DO::Shakti::Vulkan {
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT //
};

const auto mem_reqs = buffer.get_memory_requirements();
const auto mem_reqs = buffer.memory_requirements();
const auto mem_type =
_physical_device.find_memory_type(mem_reqs.memoryTypeBits, mem_props);

Expand All @@ -161,7 +162,19 @@ namespace DO::Shakti::Vulkan {
static constexpr auto mem_props =
VkMemoryPropertyFlags{VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT};

const auto mem_reqs = buffer.get_memory_requirements();
const auto mem_reqs = buffer.memory_requirements();
const auto mem_type =
_physical_device.find_memory_type(mem_reqs.memoryTypeBits, mem_props);

return {_device, mem_reqs.size, mem_type};
}

auto allocate_for_device_image(const Image& image) const -> DeviceMemory
{
static constexpr auto mem_props =
VkMemoryPropertyFlags{VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT};

const auto mem_reqs = image.memory_requirements();
const auto mem_type =
_physical_device.find_memory_type(mem_reqs.memoryTypeBits, mem_props);

Expand Down
7 changes: 5 additions & 2 deletions cpp/src/DO/Shakti/Vulkan/GraphicsPipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ namespace DO::Kalpana::Vulkan {
return _device;
}

auto model_view_projection_layout() const -> const Shakti::Vulkan::DescriptorSetLayout&
auto model_view_projection_layout() const
-> const Shakti::Vulkan::DescriptorSetLayout&
{
return _mvp_layout;
}
Expand Down Expand Up @@ -210,7 +211,9 @@ namespace DO::Kalpana::Vulkan {
graphics_pipeline._device = device;

graphics_pipeline._mvp_layout =
Shakti::Vulkan::DescriptorSetLayout::create_for_single_ubo(device);
Shakti::Vulkan::DescriptorSetLayout::Builder{device}
.push_uniform_buffer_layout_binding()
.create();

// Initialize the graphics pipeline layout.
SARA_DEBUG << "Initializing the graphics pipeline layout...\n";
Expand Down
Loading

0 comments on commit f6c0bf9

Please sign in to comment.