Skip to content
Open
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: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@
"xstring": "cpp",
"xtr1common": "cpp",
"xutility": "cpp",
"queue": "cpp"
"queue": "cpp",
"stdfloat": "cpp"
},
"C_Cpp.clang_format_style": "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 80, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false, PointerAlignment: Left, Cpp11BracedListStyle: true, AlignAfterOpenBracket: AlwaysBreak}"
}
28 changes: 25 additions & 3 deletions Application.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
#include "Application.hpp"
#include "imgui/imgui_impl_sdl2.h"
#include "imgui/imgui_impl_vulkan.h"
#include <SDL2/SDL.h>

Application::Application() {}
Application::Application()
{
ImGui::CreateContext();
ImGui::StyleColorsDark();
m_Video.InitImGui();
}

Application::~Application()
{
}

void Application::Run()
{
m_Running = true;
while (m_Running)
{
ImGui_ImplVulkan_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
// ImGui::ShowDemoWindow();
ImGui::Begin("Hello, world!");
ImGui::SliderFloat("color", &m_Color, 0.0f, 360.0f);
ImGui::SliderFloat("theta", &m_Theta, 0.0f, 360.0f);
ImGui::SliderFloat("x", &m_RotationAxis.x, -1.0f, 1.0f);
ImGui::SliderFloat("y", &m_RotationAxis.y, -1.0f, 1.0f);
Update();
ImGui::End();

m_Video.Render();
}
}
Expand All @@ -17,6 +39,7 @@ void Application::Update()
SDL_Event event;
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL2_ProcessEvent(&event);
switch (event.type)
{
case SDL_WINDOWEVENT:
Expand All @@ -28,6 +51,5 @@ void Application::Update()
}
}
}
m_Video.UpdateUnformBuffers(m_Theta);
m_Theta += 0.1f;
m_Video.UpdateUnformBuffers(m_Color, m_Theta, m_RotationAxis);
}
4 changes: 4 additions & 0 deletions Application.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#pragma once

#include "Video.hpp"
#include <glm/vec2.hpp>

class Application
{
public:
Application();
~Application();
void Run();
void Update();

private:
Video m_Video;
bool m_Running;
float m_Color;
float m_Theta;
glm::vec2 m_RotationAxis;
};
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ include_directories(/usr/local/include)
endif()

# add_subdirectory(fmt)
add_subdirectory(imgui)

file(GLOB source_files CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/*.cpp" )

Expand All @@ -44,6 +45,7 @@ target_link_libraries(UntitledGame SDL2::SDL2main)
target_link_libraries(UntitledGame SDL2::SDL2)
target_link_libraries(UntitledGame SDL2_image::SDL2_image)
target_link_libraries(UntitledGame Vulkan::Vulkan)
target_link_libraries(UntitledGame imgui)
# message(STATUS ${Vulkan_LIBRARY})
if (WIN32)
target_link_libraries(UntitledGame glm)
Expand Down
5 changes: 5 additions & 0 deletions CommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ CommandBuffer::CommandBuffer(
{
}

vk::raii::CommandBuffers& CommandBuffer::Get()
{
return m_CommandBuffers;
}

vk::raii::CommandBuffer& CommandBuffer::operator[](size_t index)
{
return m_CommandBuffers.at(index);
Expand Down
1 change: 1 addition & 0 deletions CommandBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CommandBuffer
public:
CommandBuffer(Device& device, uint32_t queueIndex, size_t bufferCount = 1);

vk::raii::CommandBuffers& Get();
vk::raii::CommandBuffer& operator[](size_t index);

private:
Expand Down
15 changes: 9 additions & 6 deletions Descriptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ Descriptors::Descriptors(
device.Get().updateDescriptorSets(descriptorWriteSets, nullptr);
}

vk::raii::DescriptorPool
Descriptors::CreateDescriptorPool(Device& device, size_t imageCount)
vk::raii::DescriptorPool Descriptors::CreateDescriptorPool(
Device& device, size_t imageCount) // maxSets needs to be revisited later
{
vk::DescriptorPoolSize discriptorPoolSize(
vk::DescriptorType::eUniformBuffer, imageCount);
std::array<vk::DescriptorPoolSize, 2> discriptorPoolSizes{
{{vk::DescriptorType::eUniformBuffer,
static_cast<uint32_t>(imageCount)},
{vk::DescriptorType::eCombinedImageSampler, /*For ImGui*/
static_cast<uint32_t>(imageCount)}}};
vk::DescriptorPoolCreateInfo createInfo(
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, imageCount,
discriptorPoolSize);
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet,
discriptorPoolSizes.size() * imageCount, discriptorPoolSizes);
return device.Get().createDescriptorPool(createInfo);
}

Expand Down
1 change: 0 additions & 1 deletion Log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <fmt/ostream.h>
#include <iostream>
#include <stdexcept>
#include <vulkan/vulkan.h>

void LogError(const std::string& message);

Expand Down
5 changes: 2 additions & 3 deletions UniformBuffer.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#pragma once
#include "glm/mat2x4.hpp"
#include "glm/mat3x4.hpp"

struct UniformBufferObject
{
glm::mat2x4 rotation;
float colorRotation;
glm::mat3x4 rotation;
float color;
};
61 changes: 54 additions & 7 deletions Video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "Pipeline.hpp"
#include "UniformBuffer.hpp"
#include "Vertex.hpp"
#include "imgui/imgui_impl_sdl2.h"
#include "imgui/imgui_impl_vulkan.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_vulkan.h>
Expand All @@ -17,7 +19,7 @@
Video::Video()
: m_Window(
"Untitled Game", {SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED},
{1200, 800}, SDL_WINDOW_SHOWN | SDL_WINDOW_VULKAN),
{800, 800}, SDL_WINDOW_SHOWN | SDL_WINDOW_VULKAN),
m_Instance(m_Window, m_Context), m_Surface(m_Window, m_Instance),
m_Device(m_Instance, m_Surface), m_Swapchain(m_Device, m_Surface),
m_Queue(m_Device.Get(), m_QueueFamilyIndex, 0),
Expand All @@ -39,6 +41,9 @@ Video::~Video()
{
m_Device.Get().waitIdle();
m_Queue.waitIdle();
ImGui_ImplVulkan_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
}

void Video::Render()
Expand All @@ -48,6 +53,10 @@ void Video::Render()
*m_SyncObjects.inFlightFences.at(m_CurrentImage), VK_TRUE,
std::numeric_limits<uint64_t>::max());

// Render ImGui frame
ImGui::Render();
ImDrawData* draw_data = ImGui::GetDrawData();

device.resetFences(*m_SyncObjects.inFlightFences.at(m_CurrentImage));
auto [result, imageIndex] = m_Swapchain.Get().acquireNextImage(
std::numeric_limits<uint64_t>::max(),
Expand Down Expand Up @@ -80,6 +89,7 @@ void Video::Render()
*m_Descriptors.GetSets().at(m_CurrentImage), nullptr);

commandBuffer.draw(static_cast<uint32_t>(vertices.size()), 1, 0, 0);
ImGui_ImplVulkan_RenderDrawData(draw_data, *commandBuffer);
commandBuffer.endRenderPass();
commandBuffer.end();

Expand All @@ -100,15 +110,26 @@ void Video::Render()
m_CurrentImage = (m_CurrentImage + 1) % m_Swapchain.GetImageCount();
}

void Video::UpdateUnformBuffers(float theta)
void Video::UpdateUnformBuffers(
float color, float theta, glm::vec2 rotationAxis)
{
UniformBufferObject& buffer =
m_UniformBuffers.at(m_CurrentImage).GetMemory().front();
buffer.rotation[0].x = cos(theta * M_PI / 180);
buffer.rotation[0].y = -sin(theta * M_PI / 180);
buffer.rotation[1].x = sin(theta * M_PI / 180);
buffer.rotation[1].y = cos(theta * M_PI / 180);
buffer.colorRotation = theta;
glm::mat3x3 rotationMatrix(
cos(theta * M_PI / 180.0f), -sin(theta * M_PI / 180.0f), 0.0f,
sin(theta * M_PI / 180.0f), cos(theta * M_PI / 180.0f), 0.0f, 0.0f,
0.0f, 1.0f);

glm::mat3x3 translationMatrix(
1.0f, 0.0f, rotationAxis.x, 0.0f, 1.0f, rotationAxis.y, 0.0f, 0.0f,
1.0f);
buffer.rotation =
(glm::inverse(translationMatrix) * rotationMatrix * translationMatrix);
ImGui::Separator();
ImGui::InputFloat4("row1", &buffer.rotation[0].x);
ImGui::InputFloat4("row2", &buffer.rotation[1].x);
ImGui::InputFloat4("row3", &buffer.rotation[2].x);
buffer.color = color;
}

void Video::FillVertexBuffer()
Expand All @@ -118,6 +139,32 @@ void Video::FillVertexBuffer()
std::copy_n(vertices.begin(), vertices.size(), memorySpan.begin());
}

void Video::InitImGui()
{
ImGui_ImplSDL2_InitForVulkan(m_Window.Get());
ImGui_ImplVulkan_InitInfo init_info = {};
init_info.Instance = *m_Instance.Get();
init_info.PhysicalDevice = *m_Device.GetPhysicalDevice();
init_info.Device = *m_Device.Get();
init_info.QueueFamily = m_QueueFamilyIndex;
init_info.Queue = *m_Queue;
init_info.DescriptorPool = *m_Descriptors.GetPool();
init_info.Subpass = 0;
init_info.MinImageCount = m_Swapchain.GetImageCount();
init_info.ImageCount = m_Swapchain.GetImageCount();
init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
init_info.Allocator = nullptr;
auto check_vulkan_err = [](VkResult err)
{
if (vk::Result(err) != vk::Result::eSuccess)
{
throw vk::SystemError(std::error_code(vk::Result(err)));
}
};
init_info.CheckVkResultFn = check_vulkan_err;
ImGui_ImplVulkan_Init(&init_info, *m_RenderPass.Get());
}

std::vector<Buffer<UniformBufferObject>> Video::ConstructUniformBuffers()
{
std::vector<Buffer<UniformBufferObject>> uniformBuffers;
Expand Down
5 changes: 3 additions & 2 deletions Video.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class Video
~Video();

void Render();
void UpdateUnformBuffers(float theta);

void UpdateUnformBuffers(float color, float theta, glm::vec2 rotationAxis);
void InitImGui();

private:
void FillVertexBuffer();
std::vector<Buffer<UniformBufferObject>> ConstructUniformBuffers();
Expand Down
7 changes: 7 additions & 0 deletions imgui/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
file(GLOB imgui_source CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" )
find_package(SDL2)
find_package(Vulkan)
add_library(imgui ${imgui_source})
target_link_libraries(imgui SDL2::SDL2)
target_link_libraries(imgui Vulkan::Vulkan)
target_include_directories(imgui PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
Loading