Skip to content

Commit

Permalink
fixing bugs, removing WindowRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Sep 11, 2024
1 parent 9cf3fe1 commit a13672b
Show file tree
Hide file tree
Showing 17 changed files with 605 additions and 238 deletions.
6 changes: 3 additions & 3 deletions Runtime/Includes/ImGui/ImGuiContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#include <filesystem>
#include <SDL2/SDL.h>

#include <Renderer/WindowRenderer.h>
#include <Renderer/Renderer.h>

namespace kbh
{
class ImGuiContext
{
public:
ImGuiContext(NonOwningPtr<WindowRenderer> renderer, const std::filesystem::path& assets_path);
ImGuiContext(NonOwningPtr<Renderer> renderer, const std::filesystem::path& assets_path);

void CheckEvents(const SDL_Event* event) const noexcept;
void BeginFrame() noexcept;
Expand All @@ -33,7 +33,7 @@ namespace kbh
std::vector<VkFramebuffer> m_framebuffers;
VkRenderPass m_renderpass = VK_NULL_HANDLE;
VkDescriptorPool m_pool = VK_NULL_HANDLE;
NonOwningPtr<WindowRenderer> p_renderer;
NonOwningPtr<Renderer> p_renderer;
};
}

Expand Down
4 changes: 2 additions & 2 deletions Runtime/Includes/ImGui/Panels/Render.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace kbh
class Render : public Panel
{
public:
Render(SceneDescriptor descriptor);
Render(NonOwningPtr<Renderer> renderer, SceneDescriptor descriptor);

void OnAttach() override;
void OnUpdate(ImVec2 size) override;
Expand All @@ -22,11 +22,11 @@ namespace kbh
~Render() override = default;

private:
Renderer m_renderer;
Scene m_scene;
SceneRenderer m_scene_renderer;
Texture m_render_texture;
ImTextureID m_imgui_image;
NonOwningPtr<Renderer> p_renderer;
};
}

Expand Down
34 changes: 28 additions & 6 deletions Runtime/Includes/Renderer/Renderer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef KANEL_3D_RENDERER
#define KANEL_3D_RENDERER

#include <SDL/SDLWindow.h>
#include <Utils/NonOwningPtr.h>
#include <Renderer/RenderCore.h>
#include <Renderer/Image.h>
#include <Core/EventBus.h>
Expand All @@ -15,11 +17,12 @@ namespace kbh
{
public:
Renderer() = default;
Renderer(NonOwningPtr<SDLWindow> window) { Init(window); }

virtual void Init();
void Init(NonOwningPtr<SDLWindow> window);

virtual bool BeginFrame(bool wait_for_fences = true);
virtual void EndFrame();
bool BeginFrame();
void EndFrame();

[[nodiscard]] inline VkSemaphore GetImageAvailableSemaphore(int index) const noexcept { return m_image_available_semaphores[index]; }
[[nodiscard]] inline VkSemaphore GetRenderFinishedSemaphore(int index) const noexcept { return m_render_finished_semaphores[index]; }
Expand All @@ -30,19 +33,38 @@ namespace kbh
[[nodiscard]] inline std::size_t GetCurrentFrameIndex() const noexcept { return m_current_frame_index; }
[[nodiscard]] inline VkFence GetFence(int index) const noexcept { return m_cmd_fences[index]; }
[[nodiscard]] inline VkFence GetActiveFence() const noexcept { return m_cmd_fences[m_current_frame_index]; }
[[nodiscard]] inline VkSwapchainKHR GetSwapchain() const noexcept { return m_swapchain; }
[[nodiscard]] inline VkSurfaceKHR GetSurface() const noexcept { return m_surface; }
[[nodiscard]] inline const std::vector<Image>& GetSwapchainImages() const { return m_swapchain_images; }
[[nodiscard]] inline NonOwningPtr<SDLWindow> GetWindow() const noexcept { return p_window; }
[[nodiscard]] inline std::size_t GetSwapchainImageIndex() const noexcept { return m_swapchain_image_index; }

virtual void Destroy() noexcept;
void GetDrawableSize(int& w, int& h) const noexcept;

virtual ~Renderer() = default;
constexpr inline void RequireFramebufferResize() noexcept { m_framebuffers_resize = true; }

protected:
void Destroy() noexcept;

~Renderer() = default;

private:
void CreateSwapchain();
void DestroySwapchain();

private:
std::vector<Image> m_swapchain_images;
std::array<VkSemaphore, MAX_FRAMES_IN_FLIGHT> m_image_available_semaphores;
std::array<VkSemaphore, MAX_FRAMES_IN_FLIGHT> m_render_finished_semaphores;
std::array<VkCommandBuffer, MAX_FRAMES_IN_FLIGHT> m_cmd_buffers;
std::array<VkFence, MAX_FRAMES_IN_FLIGHT> m_cmd_fences;
VkSurfaceKHR m_surface = VK_NULL_HANDLE;
VkSwapchainKHR m_swapchain = VK_NULL_HANDLE;
NonOwningPtr<SDLWindow> p_window;
std::uint32_t m_current_frame_index = 0;
std::uint32_t m_swapchain_image_index = 0;
std::size_t m_drawcalls = 0;
std::size_t m_polygons_drawn = 0;
bool m_framebuffers_resize = false;
};
}

Expand Down
60 changes: 0 additions & 60 deletions Runtime/Includes/Renderer/WindowRenderer.h

This file was deleted.

9 changes: 5 additions & 4 deletions Runtime/Sources/Core/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <ImGui/Panels/Parameters.h>
#include <ImGui/Panels/Render.h>
#include <ImGui/Panels/MainMenuBar.h>
#include <Renderer/WindowRenderer.h>
#include <Renderer/Renderer.h>
#include <Renderer/RenderCore.h>
#include <Core/EventBus.h>
#include <Graphics/Scene.h>
Expand All @@ -33,6 +33,7 @@ std::filesystem::path GetExecutablePath()
return std::string(result, (count > 0) ? count : 0);
}

// Windows shit
#if defined(_WIN32) || defined(_WIN64)
int WINAPI WinMain([[maybe_unused]]HINSTANCE hInstance, [[maybe_unused]]HINSTANCE hPrevInstance, [[maybe_unused]]LPSTR lpCmdLine, [[maybe_unused]]int nCmdShow)
#else
Expand All @@ -49,8 +50,8 @@ std::filesystem::path GetExecutablePath()
kbh::SDLContext sdl_context;
kbh::SDLWindow win("kanel 3D", WINDOW_WIDTH, WINDOW_HEIGHT);
kbh::SDLInputs inputs;
kbh::WindowRenderer renderer(&win);
kbh::ImGuiContext imgui(&renderer, GetExecutablePath().parent_path().parent_path() / "Resources");
kbh::Renderer renderer(&win);
kbh::ImGuiContext imgui(&renderer, GetExecutablePath().parent_path().parent_path().parent_path() / "Resources");

kbh::MainMenuBar menubar;

Expand All @@ -61,7 +62,7 @@ std::filesystem::path GetExecutablePath()
kbh::PanelStack stack;
stack.AddPanel(std::make_shared<kbh::Docks>(menubar));
stack.AddPanel(std::make_shared<kbh::Logger>());
stack.AddPanel(std::make_shared<kbh::Render>(std::move(scene_descriptor)));
stack.AddPanel(std::make_shared<kbh::Render>(&renderer, std::move(scene_descriptor)));
stack.AddPanel(std::make_shared<kbh::Parameters>());

while(!inputs.IsQuitResquested())
Expand Down
30 changes: 15 additions & 15 deletions Runtime/Sources/ImGui/ImGuiContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace kbh
{
ImGuiContext::ImGuiContext(NonOwningPtr<WindowRenderer> renderer, const std::filesystem::path& assets_path) : p_renderer(renderer)
ImGuiContext::ImGuiContext(NonOwningPtr<Renderer> renderer, const std::filesystem::path& assets_path) : p_renderer(renderer)
{
std::function<void(const EventBase&)> functor = [this](const EventBase& event)
{
Expand Down Expand Up @@ -55,7 +55,7 @@ namespace kbh
pool_info.maxSets = 1000 * IM_ARRAYSIZE(pool_sizes);
pool_info.poolSizeCount = (std::uint32_t)IM_ARRAYSIZE(pool_sizes);
pool_info.pPoolSizes = pool_sizes;
vkCreateDescriptorPool(RenderCore::Get().GetDevice(), &pool_info, nullptr, &m_pool);
kvfCheckVk(vkCreateDescriptorPool(RenderCore::Get().GetDevice(), &pool_info, nullptr, &m_pool));

// Setup Platform/Renderer bindings
ImGui_ImplVulkan_LoadFunctions([](const char* function_name, void* vulkan_instance) {
Expand All @@ -70,19 +70,19 @@ namespace kbh

ImGui_ImplSDL2_InitForVulkan(p_renderer->GetWindow()->GetNativeWindow());
ImGui_ImplVulkan_InitInfo init_info{};
init_info.Instance = RenderCore::Get().GetInstance();
init_info.PhysicalDevice = RenderCore::Get().GetPhysicalDevice();
init_info.Device = RenderCore::Get().GetDevice();
init_info.QueueFamily = kvfGetDeviceQueueFamily(RenderCore::Get().GetDevice(), KVF_GRAPHICS_QUEUE);
init_info.Queue = kvfGetDeviceQueue(RenderCore::Get().GetDevice(), KVF_GRAPHICS_QUEUE);
init_info.DescriptorPool = m_pool;
init_info.Allocator = nullptr;
init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
init_info.Subpass = 0;
init_info.MinImageCount = kvfGetSwapchainMinImagesCount(p_renderer->GetSwapchain());
init_info.ImageCount = p_renderer->GetSwapchainImages().size();
init_info.CheckVkResultFn = [](VkResult result){ kvfCheckVk(result); };
init_info.RenderPass = m_renderpass;
init_info.Instance = RenderCore::Get().GetInstance();
init_info.PhysicalDevice = RenderCore::Get().GetPhysicalDevice();
init_info.Device = RenderCore::Get().GetDevice();
init_info.QueueFamily = kvfGetDeviceQueueFamily(RenderCore::Get().GetDevice(), KVF_GRAPHICS_QUEUE);
init_info.Queue = kvfGetDeviceQueue(RenderCore::Get().GetDevice(), KVF_GRAPHICS_QUEUE);
init_info.DescriptorPool = m_pool;
init_info.Allocator = nullptr;
init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
init_info.Subpass = 0;
init_info.MinImageCount = kvfGetSwapchainMinImagesCount(p_renderer->GetSwapchain());
init_info.ImageCount = p_renderer->GetSwapchainImages().size();
init_info.CheckVkResultFn = &kvfCheckVk;
init_info.RenderPass = m_renderpass;
ImGui_ImplVulkan_Init(&init_info);

static const ImWchar icons_ranges[] = { KBH_ICON_MIN_MD, KBH_ICON_MAX_16_MD, 0 };
Expand Down
6 changes: 2 additions & 4 deletions Runtime/Sources/ImGui/Panel/Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

namespace kbh
{
Render::Render(SceneDescriptor descriptor) : Panel("Render"), m_scene(std::move(descriptor)) {}
Render::Render(NonOwningPtr<Renderer> renderer, SceneDescriptor descriptor) : Panel("Render"), m_scene(std::move(descriptor)), p_renderer(renderer) {}

void Render::OnAttach()
{
m_renderer.Init();
m_scene_renderer.Init();
m_scene.Init(&m_renderer);
m_scene.Init(p_renderer);
}

void Render::OnUpdate(ImVec2 size)
Expand All @@ -26,6 +25,5 @@ namespace kbh
{
m_scene.Destroy();
m_scene_renderer.Destroy();
m_renderer.Destroy();
}
}
6 changes: 4 additions & 2 deletions Runtime/Sources/Renderer/RenderCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace kbh
{
static VulkanLoader loader;
static std::unique_ptr<VulkanLoader> vulkan_loader;

void ErrorCallback(const char* message) noexcept
{
Expand All @@ -45,6 +45,7 @@ namespace kbh
if(m_instance != VK_NULL_HANDLE)
return;
SDLWindow window("hidden_shit", 1, 1);
vulkan_loader = std::make_unique<VulkanLoader>();

std::uint32_t count;
if(!SDL_Vulkan_GetInstanceExtensions(window.GetNativeWindow(), &count, nullptr))
Expand All @@ -60,7 +61,7 @@ namespace kbh
m_instance = kvfCreateInstance(extensions.data(), extensions.size());
DebugLog("Vulkan : instance created");

loader.LoadInstance(m_instance);
vulkan_loader->LoadInstance(m_instance);

VkSurfaceKHR surface = VK_NULL_HANDLE;
SDL_Vulkan_CreateSurface(window.GetNativeWindow(), m_instance, &surface);
Expand Down Expand Up @@ -168,5 +169,6 @@ namespace kbh
kvfDestroyInstance(m_instance);
m_instance = VK_NULL_HANDLE;
DebugLog("Vulkan : instance destroyed");
vulkan_loader.reset();
}
}
Loading

0 comments on commit a13672b

Please sign in to comment.