Skip to content

Commit

Permalink
MAINT: refactor code.
Browse files Browse the repository at this point in the history
  • Loading branch information
oddkiva committed Nov 30, 2023
1 parent 57e5ac7 commit 0d15d79
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 212 deletions.
1 change: 1 addition & 0 deletions cpp/examples/Shakti/Vulkan/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(Common)
add_subdirectory(hello_vulkan_triangle)
add_subdirectory(hello_vulkan_image)
5 changes: 5 additions & 0 deletions cpp/examples/Shakti/Vulkan/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_library(
SignalHandler #
HostUniforms.hpp #
SignalHandler.hpp SignalHandler.cpp)
target_include_directories(SignalHandler PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)
18 changes: 18 additions & 0 deletions cpp/examples/Shakti/Vulkan/Common/HostUniforms.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <Eigen/Geometry>


struct ModelViewProjectionStack
{
ModelViewProjectionStack()
{
model.setIdentity();
view.setIdentity();
projection.setIdentity();
}

Eigen::Transform<float, 3, Eigen::Projective> model;
Eigen::Transform<float, 3, Eigen::Projective> view;
Eigen::Transform<float, 3, Eigen::Projective> projection;
};
10 changes: 10 additions & 0 deletions cpp/examples/Shakti/Vulkan/Common/SignalHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include "SignalHandler.hpp"


bool SignalHandler::initialized = false;
std::atomic_bool SignalHandler::ctrl_c_hit = false;
#if !defined(_WIN32)
struct sigaction SignalHandler::sigint_handler = {};
#endif
39 changes: 39 additions & 0 deletions cpp/examples/Shakti/Vulkan/Common/SignalHandler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <signal.h>

#include <atomic>
#include <iostream>


struct SignalHandler
{
static bool initialized;
static std::atomic_bool ctrl_c_hit;
static struct sigaction sigint_handler;

static auto stop_render_loop(int) -> void
{
std::cout << "[CTRL+C HIT] Preparing to close the program!" << std::endl;
ctrl_c_hit = true;
}

static auto init() -> void
{
if (initialized)
return;

#if defined(_WIN32)
signal(SIGINT, stop_render_loop);
signal(SIGTERM, stop_render_loop);
signal(SIGABRT, stop_render_loop);
#else
sigint_handler.sa_handler = SignalHandler::stop_render_loop;
sigemptyset(&sigint_handler.sa_mask);
sigint_handler.sa_flags = 0;
sigaction(SIGINT, &sigint_handler, nullptr);
#endif

initialized = true;
}
};
31 changes: 19 additions & 12 deletions cpp/examples/Shakti/Vulkan/hello_vulkan_image/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
add_executable(hello_vulkan_image main.cpp)
target_link_libraries(
hello_vulkan_image #
PRIVATE DO::Sara::Core #
hello_vulkan_image
PRIVATE SignalHandler #
DO::Sara::Core #
DO::Shakti::Vulkan)
set_target_properties(hello_vulkan_image PROPERTIES FOLDER "Examples/Shakti/Vulkan")
set_target_properties(hello_vulkan_image PROPERTIES FOLDER
"Examples/Shakti/Vulkan")

add_custom_target(compile_hello_vulkan_image_shaders)
set_target_properties(compile_hello_vulkan_image_shaders PROPERTIES FOLDER "Examples/Shakti/Vulkan")
set_target_properties(compile_hello_vulkan_image_shaders
PROPERTIES FOLDER "Examples/Shakti/Vulkan")
add_custom_command(
TARGET compile_hello_vulkan_image_shaders
COMMAND ${CMAKE_COMMAND} -E make_directory
$<TARGET_FILE_DIR:hello_vulkan_image>/hello_vulkan_image_shaders
COMMAND ${GLSLC_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/shader.vert -o
$<TARGET_FILE_DIR:hello_vulkan_image>/hello_vulkan_image_shaders/vert.spv
COMMAND ${GLSLC_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/shader.frag -o
$<TARGET_FILE_DIR:hello_vulkan_image>/hello_vulkan_image_shaders/frag.spv)
COMMAND
${GLSLC_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/shader.vert -o
$<TARGET_FILE_DIR:hello_vulkan_image>/hello_vulkan_image_shaders/vert.spv
COMMAND
${GLSLC_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/shader.frag -o
$<TARGET_FILE_DIR:hello_vulkan_image>/hello_vulkan_image_shaders/frag.spv)

# file(GLOB SHADER_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.vert *.frag)
add_custom_command(
TARGET hello_vulkan_image
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
$<TARGET_FILE_DIR:hello_vulkan_image>/hello_vulkan_image_shaders
COMMAND ${GLSLC_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/shader.vert -o
$<TARGET_FILE_DIR:hello_vulkan_image>/hello_vulkan_image_shaders/vert.spv
COMMAND ${GLSLC_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/shader.frag -o
$<TARGET_FILE_DIR:hello_vulkan_image>/hello_vulkan_image_shaders/frag.spv)
COMMAND
${GLSLC_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/shader.vert -o
$<TARGET_FILE_DIR:hello_vulkan_image>/hello_vulkan_image_shaders/vert.spv
COMMAND
${GLSLC_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/shader.frag -o
$<TARGET_FILE_DIR:hello_vulkan_image>/hello_vulkan_image_shaders/frag.spv)
187 changes: 99 additions & 88 deletions cpp/examples/Shakti/Vulkan/hello_vulkan_image/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

#define GLFW_INCLUDE_VULKAN

#include "Geometry.hpp"

#include "Common/HostUniforms.hpp"
#include "Common/SignalHandler.hpp"

#include <DO/Shakti/Vulkan/Buffer.hpp>
#include <DO/Shakti/Vulkan/CommandBuffer.hpp>
#include <DO/Shakti/Vulkan/DescriptorSet.hpp>
Expand All @@ -22,75 +27,17 @@

#include <Eigen/Geometry>

#include <signal.h>

#include <atomic>
#include <filesystem>
#include <limits>
#include <stdexcept>

#include "Geometry.hpp"


namespace glfw = DO::Kalpana::GLFW;
namespace kvk = DO::Kalpana::Vulkan;
namespace svk = DO::Shakti::Vulkan;
namespace fs = std::filesystem;


struct SignalHandler
{
static bool initialized;
static std::atomic_bool ctrl_c_hit;
static struct sigaction sigint_handler;

static auto stop_render_loop(int) -> void
{
std::cout << "[CTRL+C HIT] Preparing to close the program!" << std::endl;
ctrl_c_hit = true;
}

static auto init() -> void
{
if (initialized)
return;

#if defined(_WIN32)
signal(SIGINT, stop_render_loop);
signal(SIGTERM, stop_render_loop);
signal(SIGABRT, stop_render_loop);
#else
sigint_handler.sa_handler = SignalHandler::stop_render_loop;
sigemptyset(&sigint_handler.sa_mask);
sigint_handler.sa_flags = 0;
sigaction(SIGINT, &sigint_handler, nullptr);
#endif

initialized = true;
}
};

bool SignalHandler::initialized = false;
std::atomic_bool SignalHandler::ctrl_c_hit = false;
#if !defined(_WIN32)
struct sigaction SignalHandler::sigint_handler = {};
#endif


struct ModelViewProjectionStack
{
ModelViewProjectionStack()
{
model.setIdentity();
view.setIdentity();
projection.setIdentity();
}

Eigen::Transform<float, 3, Eigen::Projective> model;
Eigen::Transform<float, 3, Eigen::Projective> view;
Eigen::Transform<float, 3, Eigen::Projective> projection;
};

// clang-format off
static const auto vertices = std::vector<Vertex>{
{{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.f, 0.f}},
Expand All @@ -106,6 +53,95 @@ static const auto indices = std::vector<uint16_t>{
};


class VulkanImagePipelineBuilder : public kvk::GraphicsPipeline::Builder
{
public:
auto create() -> kvk::GraphicsPipeline override
{
load_shaders();
initialize_fixed_functions();

auto graphics_pipeline = kvk::GraphicsPipeline{};

graphics_pipeline.device = device;

graphics_pipeline.desc_set_layout =
svk::DescriptorSetLayout::Builder{device}
.push_uniform_buffer_layout_binding()
.push_image_sampler_layout_binding()
.create();

// Initialize the graphics pipeline layout.
SARA_DEBUG << "Initializing the graphics pipeline layout...\n";
pipeline_layout_info = VkPipelineLayoutCreateInfo{};
{
pipeline_layout_info.sType =
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipeline_layout_info.setLayoutCount = 1;
pipeline_layout_info.pSetLayouts = &static_cast<VkDescriptorSetLayout&>(
graphics_pipeline.desc_set_layout);
pipeline_layout_info.pushConstantRangeCount = 0;
};
auto status = vkCreatePipelineLayout( //
device, //
&pipeline_layout_info, //
nullptr, //
&graphics_pipeline.pipeline_layout //
);
if (status != VK_SUCCESS)
throw std::runtime_error{fmt::format(
"Failed to create the graphics pipeline layout! Error code: {}",
static_cast<int>(status))};

// Initialize the graphics pipeline.
SARA_DEBUG << "Initializing the graphics pipeline...\n";
pipeline_info = {};
{
pipeline_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;

// - Vertex and fragment shaders.
pipeline_info.stageCount =
static_cast<std::uint32_t>(shader_stage_infos.size());
pipeline_info.pStages = shader_stage_infos.data();

// - Vertex buffer data format.
pipeline_info.pVertexInputState = &vertex_input_info;
// - We enumerate triangle vertices.
pipeline_info.pInputAssemblyState = &input_assembly;
pipeline_info.pViewportState = &viewport_state;

// The rasterization state.
pipeline_info.pRasterizationState = &rasterization_state;

// Rendering policy.
pipeline_info.pMultisampleState = &multisampling;
pipeline_info.pColorBlendState = &color_blend;

pipeline_info.layout = graphics_pipeline.pipeline_layout;
pipeline_info.renderPass = render_pass.handle;
pipeline_info.subpass = 0;
pipeline_info.basePipelineHandle = VK_NULL_HANDLE;
pipeline_info.basePipelineIndex = -1;
};

status = vkCreateGraphicsPipelines( //
device, //
VK_NULL_HANDLE, //
1, //
&pipeline_info, //
nullptr, //
&graphics_pipeline.pipeline //
);
if (status != VK_SUCCESS)
throw std::runtime_error{
fmt::format("Failed to create graphics pipeline! Error code: {}",
static_cast<int>(status))};

return graphics_pipeline;
}
};


class VulkanImageRenderer : public kvk::GraphicsBackend
{
public:
Expand All @@ -130,7 +166,7 @@ class VulkanImageRenderer : public kvk::GraphicsBackend
transfer_element_data_to_vulkan(indices);

make_descriptor_pool();
make_ubo_descriptor_sets();
make_descriptor_sets();
initialize_model_view_projection_ubos();
}

Expand Down Expand Up @@ -256,32 +292,7 @@ class VulkanImageRenderer : public kvk::GraphicsBackend
_desc_pool = desc_pool_builder.create();
}

auto make_ubo_descriptor_sets() -> void
{
// The number of frames in flight is the number of swapchain images.
// Let's say there are 3 frames in flight.
//
// We will construct 3 sets of descriptors, that is, we need one for each
// swapchain image.
const auto num_frames_in_flight =
static_cast<std::uint32_t>(_swapchain.images.size());

// Each descriptor set has the same uniform descriptor layout.
const auto& ubo_layout = _graphics_pipeline.descriptor_set_layout();
const auto ubo_layout_handle =
static_cast<VkDescriptorSetLayout>(ubo_layout);

const auto desc_set_layouts = std::vector<VkDescriptorSetLayout>(
num_frames_in_flight, ubo_layout_handle);

_desc_sets = svk::DescriptorSets{
desc_set_layouts.data(), //
num_frames_in_flight, //
_desc_pool //
};
}

auto make_image_descriptor_sets() -> void
auto make_descriptor_sets() -> void
{
// The number of frames in flight is the number of swapchain images.
// Let's say there are 3 frames in flight.
Expand All @@ -292,9 +303,9 @@ class VulkanImageRenderer : public kvk::GraphicsBackend
static_cast<std::uint32_t>(_swapchain.images.size());

// Each descriptor set has the same uniform descriptor layout.
const auto& ubo_layout = _graphics_pipeline.descriptor_set_layout();
const auto& desc_set_layout = _graphics_pipeline.desc_set_layout;
const auto ubo_layout_handle =
static_cast<VkDescriptorSetLayout>(ubo_layout);
static_cast<VkDescriptorSetLayout>(desc_set_layout);

const auto desc_set_layouts = std::vector<VkDescriptorSetLayout>(
num_frames_in_flight, ubo_layout_handle);
Expand Down Expand Up @@ -631,7 +642,7 @@ class VulkanImageRenderer : public kvk::GraphicsBackend

// Pass the UBO to the graphics pipeline.
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
_graphics_pipeline.pipeline_layout(), //
_graphics_pipeline.pipeline_layout, //
0, 1, // Find out later about this.
&descriptor_set, //
0, nullptr); // Find out later about this.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ add_executable(hello_vulkan_triangle main.cpp)
target_link_libraries(
hello_vulkan_triangle #
PRIVATE DO::Sara::Core #
DO::Shakti::Vulkan)
DO::Shakti::Vulkan #
SignalHandler)
set_target_properties(hello_vulkan_triangle PROPERTIES FOLDER
"Examples/Shakti/Vulkan")

Expand Down
Loading

0 comments on commit 0d15d79

Please sign in to comment.