VulkanRenderer: Add RenderWindow wrapper
Former-commit-id: 8046b7ecb5a71ba64bb5d51faaa2d2946318e6f1 [formerly adbc30c0ed533b63c445125f013384412f9272bd] [formerly eaa0c2a91e13440ee9b869d5fd2faad08d682879 [formerly 67381c6dbe3b960b1ab23bbe18c0a6193286f330]] Former-commit-id: f4716a44444383d107f44265b5720490e141e4d0 [formerly 49d667d30dda623e6192853573efe05aa589035c] Former-commit-id: fd3718fac5bb6d896d7cfd350807bbc1c0af309f
This commit is contained in:
@@ -1,138 +1,21 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// 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/Core/ErrorFlags.hpp>
|
||||
#include <Nazara/Core/Thread.hpp>
|
||||
#include <Nazara/Renderer/Context.hpp>
|
||||
#include <Nazara/Renderer/OpenGL.hpp>
|
||||
#include <Nazara/Renderer/Renderer.hpp>
|
||||
#include <Nazara/Renderer/Texture.hpp>
|
||||
#include <stdexcept>
|
||||
#include <Nazara/Renderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
RenderWindow::RenderWindow(VideoMode mode, const String& title, UInt32 style, const ContextParameters& parameters) :
|
||||
RenderTarget(), Window()
|
||||
{
|
||||
ErrorFlags flags(ErrorFlag_ThrowException, true);
|
||||
Create(mode, title, style, parameters);
|
||||
}
|
||||
|
||||
RenderWindow::RenderWindow(WindowHandle handle, const ContextParameters& parameters) :
|
||||
RenderTarget(), Window()
|
||||
{
|
||||
ErrorFlags flags(ErrorFlag_ThrowException, true);
|
||||
Create(handle, parameters);
|
||||
}
|
||||
|
||||
RenderWindow::~RenderWindow()
|
||||
{
|
||||
// Nécessaire si Window::Destroy est appelé par son destructeur
|
||||
OnWindowDestroy();
|
||||
}
|
||||
|
||||
bool RenderWindow::CopyToImage(AbstractImage* image, const Vector3ui& dstPos) const
|
||||
{
|
||||
#if NAZARA_RENDERER_SAFE
|
||||
if (!m_context)
|
||||
{
|
||||
NazaraError("Window has not been created");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
return CopyToImage(image, Rectui(Vector2ui(0U), GetSize()), dstPos);
|
||||
}
|
||||
|
||||
bool RenderWindow::CopyToImage(AbstractImage* image, const Rectui& rect, const Vector3ui& dstPos) const
|
||||
{
|
||||
#if NAZARA_RENDERER_SAFE
|
||||
if (!m_context)
|
||||
{
|
||||
NazaraError("Window has not been created");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
Vector2ui windowSize = GetSize();
|
||||
|
||||
#if NAZARA_RENDERER_SAFE
|
||||
if (!image)
|
||||
{
|
||||
NazaraError("Image must be valid");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (image->GetFormat() != PixelFormatType_RGBA8)
|
||||
{
|
||||
// Pour plus de facilité, évidemment on peut faire sauter cette règle avec un peu de gestion
|
||||
NazaraError("Image must be RGBA8-formatted");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (rect.x + rect.width > windowSize.x || rect.y + rect.height > windowSize.y)
|
||||
{
|
||||
NazaraError("Rectangle dimensions are out of window's bounds");
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector3ui imageSize = image->GetSize();
|
||||
if (dstPos.x + rect.width > imageSize.x || dstPos.y + rect.height > imageSize.y || dstPos.z > imageSize.z)
|
||||
{
|
||||
NazaraError("Cube dimensions are out of image's bounds");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
const Context* currentContext = Context::GetCurrent();
|
||||
if (m_context != currentContext)
|
||||
{
|
||||
if (!m_context->SetActive(true))
|
||||
{
|
||||
NazaraError("Failed to activate context");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
///TODO: Fast-path pour les images en cas de copie du buffer entier
|
||||
|
||||
m_buffer.resize(rect.width*rect.height*4);
|
||||
glReadPixels(rect.x, windowSize.y - rect.height - rect.y, rect.width, rect.height, GL_RGBA, GL_UNSIGNED_BYTE, m_buffer.data());
|
||||
|
||||
// Les pixels sont retournés, nous devons envoyer les pixels par rangée
|
||||
for (unsigned int i = 0; i < rect.height; ++i)
|
||||
image->Update(&m_buffer[rect.width*4*i], Boxui(dstPos.x, rect.height - i - 1, dstPos.z, rect.width, 1, 1), rect.width);
|
||||
|
||||
if (m_context != currentContext)
|
||||
{
|
||||
if (currentContext)
|
||||
{
|
||||
if (!currentContext->SetActive(true))
|
||||
NazaraWarning("Failed to reset old context");
|
||||
}
|
||||
else
|
||||
m_context->SetActive(false);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RenderWindow::Create(VideoMode mode, const String& title, UInt32 style, const ContextParameters& parameters)
|
||||
{
|
||||
m_parameters = parameters;
|
||||
return Window::Create(mode, title, style);
|
||||
}
|
||||
|
||||
bool RenderWindow::Create(WindowHandle handle, const ContextParameters& parameters)
|
||||
{
|
||||
m_parameters = parameters;
|
||||
return Window::Create(handle);
|
||||
}
|
||||
|
||||
void RenderWindow::Display()
|
||||
{
|
||||
if (m_framerateLimit > 0)
|
||||
@@ -143,129 +26,23 @@ namespace Nz
|
||||
|
||||
m_clock.Restart();
|
||||
}
|
||||
|
||||
if (m_context && m_parameters.doubleBuffered)
|
||||
m_context->SwapBuffers();
|
||||
}
|
||||
|
||||
void RenderWindow::EnableVerticalSync(bool enabled)
|
||||
{
|
||||
if (m_context)
|
||||
{
|
||||
if (!m_context->SetActive(true))
|
||||
{
|
||||
NazaraError("Failed to activate context");
|
||||
return;
|
||||
}
|
||||
|
||||
m_context->EnableVerticalSync(enabled);
|
||||
}
|
||||
else
|
||||
NazaraError("No context");
|
||||
}
|
||||
|
||||
unsigned int RenderWindow::GetHeight() const
|
||||
{
|
||||
return Window::GetHeight();
|
||||
}
|
||||
|
||||
RenderTargetParameters RenderWindow::GetParameters() const
|
||||
{
|
||||
if (m_context)
|
||||
{
|
||||
const ContextParameters& parameters = m_context->GetParameters();
|
||||
return RenderTargetParameters(parameters.antialiasingLevel, parameters.depthBits, parameters.stencilBits);
|
||||
}
|
||||
else
|
||||
{
|
||||
NazaraError("Window not created/context not initialized");
|
||||
return RenderTargetParameters();
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int RenderWindow::GetWidth() const
|
||||
{
|
||||
return Window::GetWidth();
|
||||
}
|
||||
|
||||
bool RenderWindow::IsRenderable() const
|
||||
{
|
||||
return m_impl != nullptr; // Si m_impl est valide, alors m_context l'est aussi
|
||||
}
|
||||
|
||||
bool RenderWindow::IsValid() const
|
||||
{
|
||||
return m_impl != nullptr;
|
||||
}
|
||||
|
||||
void RenderWindow::SetFramerateLimit(unsigned int limit)
|
||||
{
|
||||
m_framerateLimit = limit;
|
||||
}
|
||||
|
||||
ContextParameters RenderWindow::GetContextParameters() const
|
||||
{
|
||||
if (m_context)
|
||||
return m_context->GetParameters();
|
||||
else
|
||||
{
|
||||
NazaraError("Window not created/context not initialized");
|
||||
return ContextParameters();
|
||||
}
|
||||
}
|
||||
|
||||
bool RenderWindow::HasContext() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RenderWindow::Activate() const
|
||||
{
|
||||
if (m_context->SetActive(true))
|
||||
{
|
||||
glDrawBuffer((m_parameters.doubleBuffered) ? GL_BACK : GL_FRONT);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
NazaraError("Failed to activate window's context");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void RenderWindow::EnsureTargetUpdated() const
|
||||
{
|
||||
// Rien à faire
|
||||
///TODO
|
||||
}
|
||||
|
||||
bool RenderWindow::OnWindowCreated()
|
||||
{
|
||||
m_parameters.doubleBuffered = true;
|
||||
m_parameters.window = GetHandle();
|
||||
|
||||
std::unique_ptr<Context> context(new Context);
|
||||
if (!context->Create(m_parameters))
|
||||
auto impl = Renderer::GetRendererImpl()->CreateRenderWindowImpl();
|
||||
if (!impl->Create(GetHandle(), Vector2ui(GetWidth(), GetHeight()), m_parameters))
|
||||
{
|
||||
NazaraError("Failed to create context");
|
||||
NazaraError("Failed to create render window implementation: " + Error::GetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
m_context = context.release();
|
||||
|
||||
if (!SetActive(true)) // Les fenêtres s'activent à la création
|
||||
NazaraWarning("Failed to activate window");
|
||||
|
||||
EnableVerticalSync(false);
|
||||
|
||||
Vector2ui size = GetSize();
|
||||
|
||||
// Le scissorBox/viewport (à la création) est de la taille de la fenêtre
|
||||
// https://www.opengl.org/sdk/docs/man/xhtml/glGet.xml
|
||||
OpenGL::SetScissorBox(Recti(0, 0, size.x, size.y));
|
||||
OpenGL::SetViewport(Recti(0, 0, size.x, size.y));
|
||||
|
||||
OnRenderTargetParametersChange(this);
|
||||
OnRenderTargetSizeChange(this);
|
||||
m_impl = std::move(impl);
|
||||
|
||||
m_clock.Restart();
|
||||
|
||||
@@ -274,19 +51,10 @@ namespace Nz
|
||||
|
||||
void RenderWindow::OnWindowDestroy()
|
||||
{
|
||||
if (m_context)
|
||||
{
|
||||
if (IsActive())
|
||||
Renderer::SetTarget(nullptr);
|
||||
|
||||
delete m_context;
|
||||
m_context = nullptr;
|
||||
}
|
||||
m_impl.reset();
|
||||
}
|
||||
|
||||
void RenderWindow::OnWindowResized()
|
||||
{
|
||||
OnRenderTargetSizeChange(this);
|
||||
}
|
||||
}
|
||||
*/
|
||||
11
src/Nazara/Renderer/RendererWindowImpl.cpp
Normal file
11
src/Nazara/Renderer/RendererWindowImpl.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// 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/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
RenderWindowImpl::~RenderWindowImpl() = default;
|
||||
}
|
||||
Reference in New Issue
Block a user