Skip to content

Commit

Permalink
WIP: save work.
Browse files Browse the repository at this point in the history
  • Loading branch information
Odd Kiva committed Dec 8, 2023
1 parent 3f792b6 commit b5ebe51
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
3 changes: 1 addition & 2 deletions cpp/examples/Shakti/Vulkan/Common/HostUniforms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ struct 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;
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
};
71 changes: 49 additions & 22 deletions cpp/examples/Shakti/Vulkan/hello_vulkan_image/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
#include <DO/Shakti/Vulkan/ImageView.hpp>
#include <DO/Shakti/Vulkan/Sampler.hpp>

#include <DO/Kalpana/Math/Projection.hpp>

#include <DO/Sara/Core/Image.hpp>
#include <DO/Sara/Core/TicToc.hpp>
#include <DO/Sara/VideoIO.hpp>


namespace k = DO::Kalpana;
namespace sara = DO::Sara;
namespace glfw = DO::Kalpana::GLFW;
namespace kvk = DO::Kalpana::Vulkan;
Expand All @@ -37,11 +41,11 @@ namespace fs = std::filesystem;

//! @brief The 4 vertices of the square.
// clang-format off
static const auto vertices = std::vector<Vertex>{
static auto vertices = std::vector<Vertex>{
{.pos = {-0.5f, -0.5f}, .color = {1.0f, 0.0f, 0.0f}, .uv = {0.f, 0.f}},
{.pos = { 0.5f, -0.5f}, .color = {0.0f, 1.0f, 0.0f}, .uv = {1.f, 0.f}},
{.pos = { 0.5f, 0.5f}, .color = {0.0f, 0.0f, 1.0f}, .uv = {1.f, 1.f}},
{.pos = {-0.5f, 0.5f}, .color = {1.0f, 1.0f, 1.0f}, .uv = {0.f, 1.f}}
{.pos = { 0.5f, -0.5f}, .color = {0.0f, 1.0f, 0.0f}, .uv = {1.f, 0.f}},
{.pos = { 0.5f, 0.5f}, .color = {0.0f, 0.0f, 1.0f}, .uv = {1.f, 1.f}},
{.pos = {-0.5f, 0.5f}, .color = {1.0f, 1.0f, 1.0f}, .uv = {0.f, 1.f}}
};
// clang-format on

Expand Down Expand Up @@ -104,6 +108,13 @@ class VulkanImageRenderer : public kvk::GraphicsBackend
glfwSetFramebufferSizeCallback(window, framebuffer_resize_callback);
}

_vstream.open(_vpath);
const auto image_host = _vstream.frame().convert<sara::Rgba8>();

const auto aspect_ratio = static_cast<float>(image_host.width()) / image_host.height();
for (auto& vertex: vertices)
vertex.pos.x() *= aspect_ratio;

// General vulkan context objects.
init_instance(app_name, debug_vulkan);
init_surface(window);
Expand All @@ -130,8 +141,6 @@ class VulkanImageRenderer : public kvk::GraphicsBackend
init_ebos(indices);

// Device memory for image data..
_vstream.open(_vpath);
const auto image_host = _vstream.frame().convert<sara::Rgba8>();
init_vulkan_image_objects(image_host);
init_image_copy_command_buffers();
// Initialize the image data on the device side.
Expand All @@ -147,7 +156,7 @@ class VulkanImageRenderer : public kvk::GraphicsBackend
// 1. Model-view-projection matrices
init_mvp_ubos();
// 2. Image sampler objects
init_image_sampler();
init_image_view_and_sampler();

define_descriptor_set_types();
}
Expand All @@ -160,9 +169,18 @@ class VulkanImageRenderer : public kvk::GraphicsBackend

if (_vstream.read())
{
if (_verbose)
sara::tic();
const auto image_host = _vstream.frame().convert<sara::Rgba8>();
if (_verbose)
sara::toc("RGB to RGBA");

if (_verbose)
sara::tic();
copy_image_data_from_host_to_staging_buffer(image_host);
copy_image_data_from_staging_to_device_buffer();
if (_verbose)
sara::toc("Transfer from host to device image");
}

draw_frame();
Expand Down Expand Up @@ -439,21 +457,19 @@ class VulkanImageRenderer : public kvk::GraphicsBackend
//! @brief Every time we render the image frame through the method
//! `VulkanImageRenderer::draw_frame`, we update the model-view-projection
//! matrix stack by calling this method and therefore animate the square.
auto update_mvp_uniform(const std::uint32_t swapchain_image_index) -> void
auto update_mvp_ubo(const std::uint32_t swapchain_image_index) -> void
{
static auto start_time = std::chrono::high_resolution_clock::now();

const auto current_time = std::chrono::high_resolution_clock::now();
const auto time =
std::chrono::duration<float, std::chrono::seconds::period>(
current_time - start_time)
.count();
// static auto start_time = std::chrono::high_resolution_clock::now();

auto mvp = ModelViewProjectionStack{};
// const auto current_time = std::chrono::high_resolution_clock::now();
// const auto time =
// std::chrono::duration<float, std::chrono::seconds::period>(
// current_time - start_time)
// .count();

mvp.model.rotate(Eigen::AngleAxisf{time, Eigen::Vector3f::UnitZ()});
// _mvp.model.rotate(Eigen::AngleAxisf{time, Eigen::Vector3f::UnitZ()});

memcpy(_mvp_ubo_ptrs[swapchain_image_index], &mvp, sizeof(mvp));
memcpy(_mvp_ubo_ptrs[swapchain_image_index], &_mvp, sizeof(_mvp));
}

private: /* Methods to initialize image data */
Expand Down Expand Up @@ -489,7 +505,7 @@ class VulkanImageRenderer : public kvk::GraphicsBackend
_image.bind(_image_dmem, 0);
}

auto init_image_sampler() -> void
auto init_image_view_and_sampler() -> void
{
// To use the image resource from a shader:
// 1. Create an image view
Expand Down Expand Up @@ -699,7 +715,7 @@ class VulkanImageRenderer : public kvk::GraphicsBackend
if (_verbose)
SARA_CHECK(index_of_next_image_to_render);

update_mvp_uniform(index_of_next_image_to_render);
update_mvp_ubo(index_of_next_image_to_render);

// Reset the signaled fence associated to the current frame to an
// unsignaled state. So that the GPU can reuse it to signal.
Expand Down Expand Up @@ -854,6 +870,16 @@ class VulkanImageRenderer : public kvk::GraphicsBackend
"dimensions)...\n";
init_swapchain(_window);
init_swapchain_fbos();

const auto fb_aspect_ratio = static_cast<float>(w) / h;
_mvp.projection = k::orthographic( //
-0.5f * fb_aspect_ratio, 0.5f * fb_aspect_ratio, //
-0.5f, 0.5f, //
-0.5f, 0.5f);

SARA_CHECK(_mvp.model.matrix());
SARA_CHECK(_mvp.view.matrix());
SARA_CHECK(_mvp.projection);
}

private:
Expand All @@ -875,6 +901,7 @@ class VulkanImageRenderer : public kvk::GraphicsBackend
// Model-view-projection matrix
//
// 1. UBO and device memory objects.
ModelViewProjectionStack _mvp;
std::vector<svk::Buffer> _mvp_ubos;
std::vector<svk::DeviceMemory> _mvp_dmems;
std::vector<void*> _mvp_ubo_ptrs;
Expand Down Expand Up @@ -915,11 +942,11 @@ auto main(int argc, char** argv) -> int
app.init_for_vulkan_rendering();

const auto app_name = "Vulkan Image";
auto window = glfw::Window{300, 300, app_name};
auto window = glfw::Window{800, 600, app_name};

const auto program_dir_path = fs::absolute(fs::path(argv[0])).parent_path();
const auto video_path = fs::path(argv[1]);
static constexpr auto debug = true;
static constexpr auto debug = false;
auto triangle_renderer = VulkanImageRenderer{
window, //
app_name, //
Expand Down
2 changes: 1 addition & 1 deletion cpp/examples/Shakti/Vulkan/hello_vulkan_image/shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ layout(location = 0) out vec3 frag_color;
layout(location = 1) out vec2 tex_coords;

void main() {
gl_Position = mvp.model * mvp.view * mvp.projection * vec4(in_position, 0.0, 1.0);
gl_Position = mvp.projection * mvp.view * mvp.model * vec4(in_position, 0.0, 1.0);
frag_color = in_color;
tex_coords = in_tex_coords;
}
3 changes: 0 additions & 3 deletions cpp/src/DO/Kalpana/EasyGL/Objects/TexturedQuad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
#include <DO/Sara/Core/DebugUtilities.hpp>


namespace sara = DO::Sara;


using namespace DO::Kalpana::GL;


Expand Down

0 comments on commit b5ebe51

Please sign in to comment.