Skip to content

Commit

Permalink
Surface: make SurfaceWindow and SurfaceSDLWindow from the pure virtua…
Browse files Browse the repository at this point in the history
…l base class Surface
  • Loading branch information
JonathSpirit committed Sep 13, 2024
1 parent dfef65c commit e8dd2f7
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 65 deletions.
8 changes: 6 additions & 2 deletions includes/FastEngine/graphic/C_renderWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ namespace vulkan
{

class Context;
class SurfaceWindow;

} // namespace vulkan

class FGE_API RenderWindow : public fge::RenderTarget
{
public:
explicit RenderWindow(fge::vulkan::Context const& context);
explicit RenderWindow(fge::vulkan::Context const& context, fge::vulkan::SurfaceWindow& surfaceWindow);
~RenderWindow() override;

void destroy() final;
Expand All @@ -60,7 +61,7 @@ class FGE_API RenderWindow : public fge::RenderTarget
void endRenderPass() override;
void display(uint32_t imageIndex) override;

Vector2u getSize() const override;
[[nodiscard]] Vector2u getSize() const override;

void setPresentMode(VkPresentModeKHR presentMode);
[[nodiscard]] VkPresentModeKHR getPresentMode() const;
Expand All @@ -71,6 +72,7 @@ class FGE_API RenderWindow : public fge::RenderTarget
[[nodiscard]] VkExtent2D getExtent2D() const override;
[[nodiscard]] fge::vulkan::CommandBuffer& getCommandBuffer() const override;
[[nodiscard]] VkRenderPass getRenderPass() const override;
[[nodiscard]] fge::vulkan::SurfaceWindow& getSurface() const;

[[nodiscard]] VkCommandBufferInheritanceInfo getInheritanceInfo(uint32_t imageIndex) const;

Expand All @@ -87,6 +89,8 @@ class FGE_API RenderWindow : public fge::RenderTarget
void createFramebuffers();
void createSyncObjects();

fge::vulkan::SurfaceWindow* g_surfaceWindow;

fge::vulkan::SwapChain g_swapChain;

VkRenderPass g_renderPass = VK_NULL_HANDLE;
Expand Down
4 changes: 2 additions & 2 deletions includes/FastEngine/vulkan/C_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class FGE_API Context
*
* \param window The SDL window
*/
void initVulkan(SDL_Window* window);
void initVulkan(Surface const& surface);

/**
* \brief Enumerate to standard output the available extensions
Expand Down Expand Up @@ -376,7 +376,7 @@ class FGE_API Context
Instance g_instance;
PhysicalDevice g_physicalDevice;
LogicalDevice g_logicalDevice;
Surface g_surface;
Surface const* g_surface;

mutable std::map<std::string, DescriptorSetLayout, std::less<>> g_cacheLayouts;
DescriptorPool g_multiUseDescriptorPool;
Expand Down
2 changes: 1 addition & 1 deletion includes/FastEngine/vulkan/C_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class FGE_API Instance

[[nodiscard]] std::string const& getApplicationName() const;

[[nodiscard]] VkInstance getInstance() const;
[[nodiscard]] VkInstance getInstance() const; //TODO: rename to get()

/**
* \brief Get a list of physical devices
Expand Down
65 changes: 57 additions & 8 deletions includes/FastEngine/vulkan/C_surface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "volk.h"
#include "FastEngine/C_vector.hpp"
#include "SDL_vulkan.h"
#include <string_view>

namespace fge::vulkan
{
Expand All @@ -30,28 +31,76 @@ class Instance;
class FGE_API Surface
{
public:
Surface();
explicit Surface(Instance& instance);
Surface(Surface const& r) = delete;
Surface(Surface&& r) noexcept;
~Surface();
virtual ~Surface() = default;

Surface& operator=(Surface const& r) = delete;
Surface& operator=(Surface&& r) noexcept = delete;

void create(SDL_Window* window, Instance& instance);
void destroy();
virtual void destroy() = 0;

[[nodiscard]] VkSurfaceKHR getSurface() const;
[[nodiscard]] VkSurfaceKHR get() const;
[[nodiscard]] bool isCreated() const;

[[nodiscard]] Instance& getInstance();
[[nodiscard]] Instance const& getInstance() const;

[[nodiscard]] SDL_Window* getWindow() const;
[[nodiscard]] fge::Vector2i getWindowSize() const;
[[nodiscard]] virtual VkExtent2D getExtent() const;

protected:
VkSurfaceKHR _g_surface;

private:
VkSurfaceKHR g_surface;
Instance* g_instance;
};

class SurfaceWindow : public Surface
{
public:
enum class Types
{
UNKNOWN,
SDL
};

inline explicit SurfaceWindow(Instance& instance) :
Surface(instance)
{}

[[nodiscard]] inline VkExtent2D getExtent() const override
{
auto const size = this->getSize();
return {static_cast<uint32_t>(size.x), static_cast<uint32_t>(size.y)};
}

[[nodiscard]] virtual Types getType() const = 0;

[[nodiscard]] virtual fge::Vector2i getSize() const = 0;
};

class FGE_API SurfaceSDLWindow final : public SurfaceWindow
{
public:
inline explicit SurfaceSDLWindow(Instance& instance) :
SurfaceWindow(instance),
g_window(nullptr)
{}
SurfaceSDLWindow(SurfaceSDLWindow&& r) noexcept;
~SurfaceSDLWindow() final;

bool create(SDL_Window* window);
bool create(std::string_view title, fge::Vector2i const& position, fge::Vector2i const& size, uint32_t flags);
void destroy() final;

[[nodiscard]] Types getType() const override;

[[nodiscard]] fge::Vector2i getSize() const override;

[[nodiscard]] SDL_Window* getWindow() const;

private:
SDL_Window* g_window;
};

Expand Down
5 changes: 3 additions & 2 deletions sources/C_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@
#include "FastEngine/graphic/C_renderWindow.hpp"
#include "FastEngine/network/C_packet.hpp"
#include "FastEngine/vulkan/C_context.hpp"
#include "FastEngine/vulkan/C_surface.hpp"
#include "tinyutf8.h"

namespace fge
{

#ifndef FGE_DEF_SERVER
Event::Event(SDL_Window* window)
Event::Event(SDL_Window* window) //TODO: Use SurfaceWindow instead of SDL_Window
{
SDL_GetWindowSize(window, &this->g_windowSize.x, &this->g_windowSize.y);
SDL_GetWindowPosition(window, &this->g_windowPosition.x, &this->g_windowPosition.y);
}
Event::Event(fge::RenderWindow const& renderWindow) :
Event(renderWindow.getContext().getSurface().getWindow())
Event(reinterpret_cast<fge::vulkan::SurfaceSDLWindow*>(&renderWindow.getSurface())->getWindow())
{}
#endif //FGE_DEF_SERVER

Expand Down
20 changes: 13 additions & 7 deletions sources/graphic/C_renderWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "FastEngine/graphic/C_renderWindow.hpp"
#include "FastEngine/C_alloca.hpp"
#include "FastEngine/extra/extra_function.hpp"
#include "FastEngine/graphic/C_transform.hpp"
#include "FastEngine/graphic/C_transformable.hpp"
Expand Down Expand Up @@ -42,8 +43,9 @@ int ResizeCallback(void* userdata, SDL_Event* event)

} // namespace

RenderWindow::RenderWindow(fge::vulkan::Context const& context) :
RenderWindow::RenderWindow(fge::vulkan::Context const& context, fge::vulkan::SurfaceWindow& surfaceWindow) :
RenderTarget(context),
g_surfaceWindow(&surfaceWindow),
g_commandBuffers({context}),
g_imageAvailableSemaphores({VK_NULL_HANDLE}),
g_renderFinishedSemaphores({VK_NULL_HANDLE}),
Expand Down Expand Up @@ -229,9 +231,9 @@ void RenderWindow::display(uint32_t imageIndex)
this->getContext().endMainRenderTarget(*this);
}

Vector2u RenderWindow::getSize() const
[[nodiscard]] Vector2u RenderWindow::getSize() const
{
return static_cast<Vector2u>(this->getContext().getSurface().getWindowSize());
return static_cast<Vector2u>(this->g_surfaceWindow->getSize());
}

void RenderWindow::setPresentMode(VkPresentModeKHR presentMode)
Expand Down Expand Up @@ -266,6 +268,10 @@ VkRenderPass RenderWindow::getRenderPass() const
{
return this->g_renderPass;
}
fge::vulkan::SurfaceWindow& RenderWindow::getSurface() const
{
return *this->g_surfaceWindow;
}

VkCommandBufferInheritanceInfo RenderWindow::getInheritanceInfo(uint32_t imageIndex) const
{
Expand Down Expand Up @@ -296,7 +302,7 @@ void RenderWindow::init()
}
this->g_isCreated = true;

this->g_swapChain.create(this->getContext().getSurface().getWindow(), this->getContext().getLogicalDevice(),
this->g_swapChain.create(this->g_surfaceWindow->getExtent(), this->getContext().getLogicalDevice(),
this->getContext().getPhysicalDevice(), this->getContext().getSurface(),
this->g_presentMode);

Expand All @@ -317,13 +323,13 @@ void RenderWindow::init()

void RenderWindow::recreateSwapChain()
{
auto windowSize = this->getContext().getSurface().getWindowSize();
auto windowSize = this->g_surfaceWindow->getSize();

SDL_Event event;

while (windowSize.x == 0 || windowSize.y == 0)
{
windowSize = this->getContext().getSurface().getWindowSize();
windowSize = this->g_surfaceWindow->getSize();
SDL_WaitEvent(&event);
}

Expand All @@ -337,7 +343,7 @@ void RenderWindow::recreateSwapChain()

vkDestroyRenderPass(this->getContext().getLogicalDevice().getDevice(), this->g_renderPass, nullptr);

this->g_swapChain.create(this->getContext().getSurface().getWindow(), this->getContext().getLogicalDevice(),
this->g_swapChain.create(this->g_surfaceWindow->getExtent(), this->getContext().getLogicalDevice(),
this->getContext().getPhysicalDevice(), this->getContext().getSurface(),
this->g_presentMode);
this->createRenderPass();
Expand Down
16 changes: 8 additions & 8 deletions sources/vulkan/C_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void Context::destroy()

vmaDestroyAllocator(this->g_allocator);

this->g_surface.destroy();
this->g_surface = nullptr;
this->g_logicalDevice.destroy();
this->g_instance.destroy();

Expand Down Expand Up @@ -262,20 +262,20 @@ void Context::initVolk()
}
}

void Context::initVulkan(SDL_Window* window)
void Context::initVulkan(Surface const& surface)
{
this->g_isCreated = true;

this->g_instance.create("VulkanTest");
this->g_instance.create("VulkanTest"); //TODO: Add parameter for app name

this->g_surface.create(window, this->g_instance);
auto physicalDevice = this->g_instance.pickPhysicalDevice(this->g_surface.getSurface());
this->g_surface = &surface;
auto physicalDevice = this->g_instance.pickPhysicalDevice(this->g_surface->get());
if (!physicalDevice.has_value())
{
throw fge::Exception("failed to find a suitable GPU!");
}
this->g_physicalDevice = std::move(physicalDevice.value());
this->g_logicalDevice.create(this->g_physicalDevice, this->g_surface.getSurface());
this->g_logicalDevice.create(this->g_physicalDevice, this->g_surface->get());

VmaVulkanFunctions vulkanFunctions{vkGetInstanceProcAddr,
vkGetDeviceProcAddr,
Expand Down Expand Up @@ -383,7 +383,7 @@ Instance const& Context::getInstance() const
}
Surface const& Context::getSurface() const
{
return this->g_surface;
return *this->g_surface;
}
LogicalDevice const& Context::getLogicalDevice() const
{
Expand Down Expand Up @@ -561,7 +561,7 @@ void Context::GlobalTransform::update()

void Context::createCommandPool()
{
auto queueFamilyIndices = this->g_physicalDevice.findQueueFamilies(this->g_surface.getSurface());
auto queueFamilyIndices = this->g_physicalDevice.findQueueFamilies(this->g_surface->get());

VkCommandPoolCreateInfo poolInfo{};
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Expand Down
Loading

0 comments on commit e8dd2f7

Please sign in to comment.