Replace RenderWindow with swapchains

This commit is contained in:
SirLynix
2023-01-21 12:02:34 +01:00
committed by Jérôme Leclercq
parent 8db1c04568
commit 18851c9185
66 changed files with 1404 additions and 2048 deletions

View File

@@ -1,11 +0,0 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/RenderSurface.hpp>
#include <Nazara/Renderer/Debug.hpp>
namespace Nz
{
RenderSurface::~RenderSurface() = default;
}

View File

@@ -1,91 +0,0 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/RenderWindow.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Renderer/Renderer.hpp>
#include <Nazara/Renderer/RendererImpl.hpp>
#include <chrono>
#include <thread>
#include <Nazara/Renderer/Debug.hpp>
namespace Nz
{
RenderFrame RenderWindow::AcquireFrame()
{
if (!m_impl)
{
NazaraError("window is not created");
return RenderFrame{};
}
if (m_framerateLimit > 0)
{
int remainingTime = 1000 / static_cast<int>(m_framerateLimit) - static_cast<int>(m_clock.GetElapsedTime().AsMilliseconds());
if (remainingTime > 0)
std::this_thread::sleep_for(std::chrono::milliseconds(remainingTime));
m_clock.Restart();
}
return m_impl->Acquire();
}
bool RenderWindow::Create(std::shared_ptr<RenderDevice> renderDevice, VideoMode mode, const std::string& title, WindowStyleFlags style, const RenderWindowParameters& parameters)
{
m_parameters = parameters;
m_renderDevice = std::move(renderDevice);
return Window::Create(mode, title, style);
}
bool RenderWindow::Create(std::shared_ptr<RenderDevice> renderDevice, void* handle, const RenderWindowParameters& parameters)
{
m_parameters = parameters;
m_renderDevice = std::move(renderDevice);
return Window::Create(handle);
}
void RenderWindow::EnableVerticalSync(bool enabled)
{
///TODO
}
bool RenderWindow::OnWindowCreated()
{
RendererImpl* rendererImpl = Renderer::Instance()->GetRendererImpl();
auto surface = rendererImpl->CreateRenderSurfaceImpl();
if (!surface->Create(GetSystemHandle()))
{
NazaraError("Failed to create render surface: " + Error::GetLastError());
return false;
}
auto impl = rendererImpl->CreateRenderWindowImpl(*this);
if (!impl->Create(rendererImpl, surface.get(), m_parameters))
{
NazaraError("Failed to create render window implementation: " + Error::GetLastError());
return false;
}
m_impl = std::move(impl);
m_surface = std::move(surface);
m_clock.Restart();
return true;
}
void RenderWindow::OnWindowDestroy()
{
m_impl.reset();
m_renderDevice.reset();
m_surface.reset();
}
void RenderWindow::OnWindowResized()
{
}
}

View File

@@ -2,14 +2,14 @@
// This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/RenderWindowImpl.hpp>
#include <Nazara/Renderer/Swapchain.hpp>
#include <Nazara/Renderer/Debug.hpp>
namespace Nz
{
RenderWindowImpl::~RenderWindowImpl() = default;
Swapchain::~Swapchain() = default;
void RenderWindowImpl::BuildRenderPass(PixelFormat colorFormat, PixelFormat depthFormat, std::vector<RenderPass::Attachment>& attachments, std::vector<RenderPass::SubpassDescription>& subpassDescriptions, std::vector<RenderPass::SubpassDependency>& subpassDependencies)
void Swapchain::BuildRenderPass(PixelFormat colorFormat, PixelFormat depthFormat, std::vector<RenderPass::Attachment>& attachments, std::vector<RenderPass::SubpassDescription>& subpassDescriptions, std::vector<RenderPass::SubpassDependency>& subpassDependencies)
{
assert(colorFormat != PixelFormat::Undefined);

View File

@@ -0,0 +1,72 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Renderer module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/WindowSwapchain.hpp>
#include <Nazara/Renderer/RenderDevice.hpp>
#include <Nazara/Platform/Window.hpp>
#include <Nazara/Renderer/Debug.hpp>
namespace Nz
{
WindowSwapchain::WindowSwapchain(std::shared_ptr<RenderDevice> renderDevice, Window& window, SwapchainParameters parameters) :
m_renderDevice(std::move(renderDevice)),
m_window(&window),
m_parameters(std::move(parameters)),
m_renderOnlyIfFocused(false)
{
NazaraAssert(m_renderDevice, "invalid render device");
if (m_window->IsValid())
{
m_swapchain = m_renderDevice->InstantiateSwapchain(window.GetHandle(), window.GetSize(), m_parameters);
m_isMinimized = window.IsMinimized();
}
else
m_isMinimized = true; //< consider it minimized so AcquireFrame returns no frame
ConnectSignals();
}
void WindowSwapchain::ConnectSignals()
{
WindowEventHandler& windowEvents = m_window->GetEventHandler();
m_onCreated.Connect(windowEvents.OnCreated, [this](const WindowEventHandler* /*eventHandler*/)
{
// Recreate swapchain
m_swapchain = m_renderDevice->InstantiateSwapchain(m_window->GetHandle(), m_window->GetSize(), m_parameters);
m_isMinimized = m_window->IsMinimized();
});
m_onDestruction.Connect(windowEvents.OnDestruction, [this](const WindowEventHandler* /*eventHandler*/)
{
m_swapchain.reset();
m_isMinimized = true;
});
m_onGainedFocus.Connect(windowEvents.OnGainedFocus, [this](const WindowEventHandler* /*eventHandler*/)
{
m_hasFocus = true;
});
m_onLostFocus.Connect(windowEvents.OnLostFocus, [this](const WindowEventHandler* /*eventHandler*/)
{
m_hasFocus = false;
});
m_onMinimized.Connect(windowEvents.OnMinimized, [this](const WindowEventHandler* /*eventHandler*/)
{
m_isMinimized = true;
});
m_onResized.Connect(windowEvents.OnResized, [this](const WindowEventHandler* /*eventHandler*/, const WindowEvent::SizeEvent& event)
{
m_swapchain->NotifyResize({ event.width, event.height });
});
m_onRestored.Connect(windowEvents.OnRestored, [this](const WindowEventHandler* /*eventHandler*/)
{
m_isMinimized = false;
});
}
}