diff --git a/include/Nazara/Renderer/RenderTexture.hpp b/include/Nazara/Renderer/RenderTexture.hpp index 1856d012b..6130e4184 100644 --- a/include/Nazara/Renderer/RenderTexture.hpp +++ b/include/Nazara/Renderer/RenderTexture.hpp @@ -26,8 +26,8 @@ struct NzRenderTextureImpl; class NAZARA_RENDERER_API NzRenderTexture : public NzRenderTarget, NzNonCopyable { public: - NzRenderTexture() = default; - ~NzRenderTexture(); + inline NzRenderTexture(); + inline ~NzRenderTexture(); bool AttachBuffer(nzAttachmentPoint attachmentPoint, nzUInt8 index, NzRenderBuffer* buffer); bool AttachBuffer(nzAttachmentPoint attachmentPoint, nzUInt8 index, nzPixelFormat format, unsigned int width, unsigned int height); @@ -38,18 +38,18 @@ class NAZARA_RENDERER_API NzRenderTexture : public NzRenderTarget, NzNonCopyable void Detach(nzAttachmentPoint attachmentPoint, nzUInt8 index); - unsigned int GetHeight() const; + unsigned int GetHeight() const override; NzRenderTargetParameters GetParameters() const; NzVector2ui GetSize() const; - unsigned int GetWidth() const; + unsigned int GetWidth() const override; bool IsComplete() const; bool IsRenderable() const; - bool IsValid() const; + inline bool IsValid() const; bool Lock() const; - void SetColorTarget(nzUInt8 target) const; + inline void SetColorTarget(nzUInt8 target) const; void SetColorTargets(const nzUInt8* targets, unsigned int targetCount) const; void SetColorTargets(const std::initializer_list& targets) const; @@ -59,7 +59,7 @@ class NAZARA_RENDERER_API NzRenderTexture : public NzRenderTarget, NzNonCopyable unsigned int GetOpenGLID() const; bool HasContext() const override; - static void Blit(NzRenderTexture* src, NzRenderTexture* dst, nzUInt32 buffers = nzRendererBuffer_Color | nzRendererBuffer_Depth | nzRendererBuffer_Stencil, bool bilinearFilter = false); + static inline void Blit(NzRenderTexture* src, NzRenderTexture* dst, nzUInt32 buffers = nzRendererBuffer_Color | nzRendererBuffer_Depth | nzRendererBuffer_Stencil, bool bilinearFilter = false); static void Blit(NzRenderTexture* src, NzRectui srcRect, NzRenderTexture* dst, NzRectui dstRect, nzUInt32 buffers = nzRendererBuffer_Color | nzRendererBuffer_Depth | nzRendererBuffer_Stencil, bool bilinearFilter = false); static bool IsSupported(); @@ -69,6 +69,9 @@ class NAZARA_RENDERER_API NzRenderTexture : public NzRenderTarget, NzNonCopyable void EnsureTargetUpdated() const override; private: + inline void InvalidateDrawBuffers() const; + inline void InvalidateSize() const; + inline void InvalidateTargets() const; void OnContextDestroy(const NzContext* context); void OnRenderBufferDestroy(const NzRenderBuffer* renderBuffer, unsigned int attachmentIndex); void OnTextureDestroy(const NzTexture* texture, unsigned int attachmentIndex); @@ -76,7 +79,13 @@ class NAZARA_RENDERER_API NzRenderTexture : public NzRenderTarget, NzNonCopyable void UpdateSize() const; void UpdateTargets() const; - NzRenderTextureImpl* m_impl = nullptr; + NzRenderTextureImpl* m_impl; + mutable bool m_checked ; + mutable bool m_drawBuffersUpdated; + mutable bool m_sizeUpdated; + mutable bool m_targetsUpdated; }; +#include + #endif // NAZARA_RENDERTEXTURE_HPP diff --git a/include/Nazara/Renderer/RenderTexture.inl b/include/Nazara/Renderer/RenderTexture.inl new file mode 100644 index 000000000..d104eab39 --- /dev/null +++ b/include/Nazara/Renderer/RenderTexture.inl @@ -0,0 +1,53 @@ +// 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 +#include +#include + +inline NzRenderTexture::NzRenderTexture() : +m_impl(nullptr) +{ +} + +inline NzRenderTexture::~NzRenderTexture() +{ + Destroy(); +} + +inline bool NzRenderTexture::IsValid() const +{ + return m_impl != nullptr; +} + +inline void NzRenderTexture::SetColorTarget(nzUInt8 target) const +{ + SetColorTargets(&target, 1); +} + +inline void NzRenderTexture::Blit(NzRenderTexture* src, NzRenderTexture* dst, nzUInt32 buffers, bool bilinearFilter) +{ + Blit(src, src->GetSize(), dst, dst->GetSize(), buffers, bilinearFilter); +} + +inline void NzRenderTexture::InvalidateDrawBuffers() const +{ + m_drawBuffersUpdated = false; +} + +inline void NzRenderTexture::InvalidateSize() const +{ + m_sizeUpdated = false; + + OnRenderTargetSizeChange(this); +} + +inline void NzRenderTexture::InvalidateTargets() const +{ + m_checked = false; + m_drawBuffersUpdated = false; + m_targetsUpdated = false; +} + +#include diff --git a/src/Nazara/Renderer/RenderTexture.cpp b/src/Nazara/Renderer/RenderTexture.cpp index f2dc61bfc..b574de8d9 100644 --- a/src/Nazara/Renderer/RenderTexture.cpp +++ b/src/Nazara/Renderer/RenderTexture.cpp @@ -63,21 +63,12 @@ struct NzRenderTextureImpl std::vector colorTargets; mutable std::vector drawBuffers; const NzContext* context; - bool checked = false; bool complete = false; bool userDefinedTargets = false; - mutable bool drawBuffersUpdated = true; - mutable bool sizeUpdated = false; - mutable bool targetsUpdated = true; unsigned int height; unsigned int width; }; -NzRenderTexture::~NzRenderTexture() -{ - Destroy(); -} - bool NzRenderTexture::AttachBuffer(nzAttachmentPoint attachmentPoint, nzUInt8 index, NzRenderBuffer* buffer) { #if NAZARA_RENDERER_SAFE @@ -148,6 +139,9 @@ bool NzRenderTexture::AttachBuffer(nzAttachmentPoint attachmentPoint, nzUInt8 in Unlock(); unsigned int attachIndex = attachmentIndex[attachmentPoint] + index; + if (attachIndex >= m_impl->attachments.size()) + m_impl->attachments.resize(attachIndex+1); + Attachment& attachment = m_impl->attachments[attachIndex]; attachment.attachmentPoint = attachmentPoint; attachment.buffer = buffer; @@ -157,15 +151,11 @@ bool NzRenderTexture::AttachBuffer(nzAttachmentPoint attachmentPoint, nzUInt8 in attachment.height = buffer->GetHeight(); attachment.width = buffer->GetWidth(); - m_impl->checked = false; - m_impl->sizeUpdated = false; + InvalidateSize(); + InvalidateTargets(); if (attachmentPoint == nzAttachmentPoint_Color && !m_impl->userDefinedTargets) - { m_impl->colorTargets.push_back(index); - m_impl->drawBuffersUpdated = false; - m_impl->targetsUpdated = false; - } return true; } @@ -286,6 +276,9 @@ bool NzRenderTexture::AttachTexture(nzAttachmentPoint attachmentPoint, nzUInt8 i Unlock(); unsigned int attachIndex = attachmentIndex[attachmentPoint] + index; + if (attachIndex >= m_impl->attachments.size()) + m_impl->attachments.resize(attachIndex+1); + Attachment& attachment = m_impl->attachments[attachIndex]; attachment.attachmentPoint = attachmentPoint; attachment.isBuffer = false; @@ -295,15 +288,11 @@ bool NzRenderTexture::AttachTexture(nzAttachmentPoint attachmentPoint, nzUInt8 i attachment.textureDestroySlot.Connect(texture->OnTextureDestroy, std::bind(&NzRenderTexture::OnTextureDestroy, this, std::placeholders::_1, attachIndex)); attachment.width = texture->GetWidth(); - m_impl->checked = false; - m_impl->sizeUpdated = false; + InvalidateSize(); + InvalidateTargets(); if (attachmentPoint == nzAttachmentPoint_Color && !m_impl->userDefinedTargets) - { m_impl->colorTargets.push_back(index); - m_impl->drawBuffersUpdated = false; - m_impl->targetsUpdated = false; - } return true; } @@ -341,6 +330,11 @@ bool NzRenderTexture::Create(bool lock) m_impl->context = NzContext::GetCurrent(); m_impl->contextDestroySlot.Connect(m_impl->context->OnContextDestroy, this, &NzRenderTexture::OnContextDestroy); + m_checked = false; + m_drawBuffersUpdated = true; + m_sizeUpdated = false; + m_targetsUpdated = true; + if (lock) { // En cas d'exception, la ressource sera quand même libérée @@ -430,30 +424,21 @@ void NzRenderTexture::Detach(nzAttachmentPoint attachmentPoint, nzUInt8 index) attachement.textureDestroySlot.Disconnect(); } - m_impl->sizeUpdated = false; + InvalidateSize(); if (attachement.attachmentPoint == nzAttachmentPoint_Color) - { - m_impl->drawBuffersUpdated = false; - m_impl->targetsUpdated = false; - } + InvalidateTargets(); Unlock(); - m_impl->checked = false; + m_checked = false; } unsigned int NzRenderTexture::GetHeight() const { - #if NAZARA_RENDERER_SAFE - if (!m_impl) - { - NazaraError("Render texture not created"); - return 0; - } - #endif + NazaraAssert(m_impl, "Invalid render texture"); - if (!m_impl->sizeUpdated) + if (!m_sizeUpdated) UpdateSize(); return m_impl->height; @@ -461,13 +446,7 @@ unsigned int NzRenderTexture::GetHeight() const NzRenderTargetParameters NzRenderTexture::GetParameters() const { - #if NAZARA_RENDERER_SAFE - if (!m_impl) - { - NazaraError("Render texture not created"); - return NzRenderTargetParameters(); - } - #endif + NazaraAssert(m_impl, "Invalid render texture"); ///TODO return NzRenderTargetParameters(); @@ -475,15 +454,9 @@ NzRenderTargetParameters NzRenderTexture::GetParameters() const NzVector2ui NzRenderTexture::GetSize() const { - #if NAZARA_RENDERER_SAFE - if (!m_impl) - { - NazaraError("Render texture not created"); - return 0; - } - #endif + NazaraAssert(m_impl, "Invalid render texture"); - if (!m_impl->sizeUpdated) + if (!m_sizeUpdated) UpdateSize(); return NzVector2ui(m_impl->width, m_impl->height); @@ -491,15 +464,9 @@ NzVector2ui NzRenderTexture::GetSize() const unsigned int NzRenderTexture::GetWidth() const { - #if NAZARA_RENDERER_SAFE - if (!m_impl) - { - NazaraError("Render texture not created"); - return 0; - } - #endif + NazaraAssert(m_impl, "Invalid render texture"); - if (!m_impl->sizeUpdated) + if (!m_sizeUpdated) UpdateSize(); return m_impl->width; @@ -507,15 +474,9 @@ unsigned int NzRenderTexture::GetWidth() const bool NzRenderTexture::IsComplete() const { - #if NAZARA_RENDERER_SAFE - if (!m_impl) - { - NazaraError("Render texture not created"); - return false; - } - #endif + NazaraAssert(m_impl, "Invalid render texture"); - if (!m_impl->checked) + if (!m_checked) { if (!Lock()) { @@ -566,7 +527,7 @@ bool NzRenderTexture::IsComplete() const NazaraInternalError("Unknown error"); } - m_impl->checked = true; + m_checked = true; } return m_impl->complete; @@ -577,20 +538,11 @@ bool NzRenderTexture::IsRenderable() const return IsComplete() && !m_impl->attachments.empty(); } -bool NzRenderTexture::IsValid() const -{ - return m_impl != nullptr; -} - bool NzRenderTexture::Lock() const { - #if NAZARA_RENDERER_SAFE - if (!m_impl) - { - NazaraError("Render texture not created"); - return false; - } + NazaraAssert(m_impl, "Invalid render texture"); + #if NAZARA_RENDERER_SAFE if (NzContext::GetCurrent() != m_impl->context) { NazaraError("RenderTexture cannot be used with this context"); @@ -612,20 +564,11 @@ bool NzRenderTexture::Lock() const return true; } -void NzRenderTexture::SetColorTarget(nzUInt8 target) const -{ - SetColorTargets(&target, 1); -} - void NzRenderTexture::SetColorTargets(const nzUInt8* targets, unsigned int targetCount) const { - #if NAZARA_RENDERER_SAFE - if (!m_impl) - { - NazaraError("Render texture not created"); - return; - } + NazaraAssert(m_impl, "Invalid render texture"); + #if NAZARA_RENDERER_SAFE for (unsigned int i = 0; i < targetCount; ++i) { unsigned int index = attachmentIndex[nzAttachmentPoint_Color] + targets[i]; @@ -640,20 +583,15 @@ void NzRenderTexture::SetColorTargets(const nzUInt8* targets, unsigned int targe m_impl->colorTargets.resize(targetCount); std::memcpy(&m_impl->colorTargets[0], targets, targetCount*sizeof(nzUInt8)); - m_impl->drawBuffersUpdated = false; - m_impl->targetsUpdated = false; m_impl->userDefinedTargets = true; + InvalidateDrawBuffers(); } void NzRenderTexture::SetColorTargets(const std::initializer_list& targets) const { - #if NAZARA_RENDERER_SAFE - if (!m_impl) - { - NazaraError("Render texture not created"); - return; - } + NazaraAssert(m_impl, "Invalid render texture"); + #if NAZARA_RENDERER_SAFE for (nzUInt8 target : targets) { unsigned int index = attachmentIndex[nzAttachmentPoint_Color] + target; @@ -671,20 +609,15 @@ void NzRenderTexture::SetColorTargets(const std::initializer_list& targ for (nzUInt8 index : targets) *ptr++ = index; - m_impl->drawBuffersUpdated = false; - m_impl->targetsUpdated = false; m_impl->userDefinedTargets = true; + InvalidateDrawBuffers(); } void NzRenderTexture::Unlock() const { - #if NAZARA_RENDERER_SAFE - if (!m_impl) - { - NazaraError("Render texture not created"); - return; - } + NazaraAssert(m_impl, "Invalid render texture"); + #if NAZARA_RENDERER_SAFE if (NzContext::GetCurrent() != m_impl->context) { NazaraError("RenderTexture cannot be used with this context"); @@ -704,13 +637,9 @@ void NzRenderTexture::Unlock() const unsigned int NzRenderTexture::GetOpenGLID() const { - #if NAZARA_RENDERER_SAFE - if (!m_impl) - { - NazaraError("Render texture not created"); - return 0; - } + NazaraAssert(m_impl, "Invalid render texture"); + #if NAZARA_RENDERER_SAFE if (NzContext::GetCurrent() != m_impl->context) { NazaraError("RenderTexture cannot be used with this context"); @@ -731,46 +660,18 @@ bool NzRenderTexture::IsSupported() return NzOpenGL::IsSupported(nzOpenGLExtension_FrameBufferObject); } -void NzRenderTexture::Blit(NzRenderTexture* src, NzRenderTexture* dst, nzUInt32 buffers, bool bilinearFilter) -{ - #if NAZARA_RENDERER_SAFE - if (!src) - { - NazaraError("Invalid source render texture"); - return; - } - - if (!dst) - { - NazaraError("Invalid source render texture"); - return; - } - #endif - - Blit(src, src->GetSize(), dst, dst->GetSize(), buffers, bilinearFilter); -} - void NzRenderTexture::Blit(NzRenderTexture* src, NzRectui srcRect, NzRenderTexture* dst, NzRectui dstRect, nzUInt32 buffers, bool bilinearFilter) { - #if NAZARA_RENDERER_SAFE - if (!src || !src->IsValid()) - { - NazaraError("Invalid source render texture"); - return; - } + NazaraAssert(src && src->IsValid(), "Invalid source render texture"); + NazaraAssert(dst && dst->IsValid(), "Invalid destination render texture"); + #if NAZARA_RENDERER_SAFE if (srcRect.x+srcRect.width > src->GetWidth() || srcRect.y+srcRect.height > src->GetHeight()) { NazaraError("Source rectangle dimensions are out of bounds"); return; } - if (!dst || !dst->IsValid()) - { - NazaraError("Invalid source render texture"); - return; - } - if (dstRect.x+dstRect.width > dst->GetWidth() || dstRect.y+dstRect.height > dst->GetHeight()) { NazaraError("Destination rectangle dimensions are out of bounds"); @@ -811,13 +712,9 @@ void NzRenderTexture::Blit(NzRenderTexture* src, NzRectui srcRect, NzRenderTextu bool NzRenderTexture::Activate() const { - #if NAZARA_RENDERER_SAFE - if (!m_impl) - { - NazaraError("Render texture not created"); - return false; - } + NazaraAssert(m_impl, "Invalid render texture"); + #if NAZARA_RENDERER_SAFE if (NzContext::GetCurrent() != m_impl->context) { NazaraError("RenderTexture cannot be used with this context"); @@ -827,20 +724,16 @@ bool NzRenderTexture::Activate() const glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_impl->fbo); - m_impl->drawBuffersUpdated = false; + m_drawBuffersUpdated = false; return true; } void NzRenderTexture::Desactivate() const { - #if NAZARA_RENDERER_SAFE - if (!m_impl) - { - NazaraError("Render texture not created"); - return; - } + NazaraAssert(m_impl, "Invalid render texture"); + #if NAZARA_RENDERER_SAFE if (NzContext::GetCurrent() != m_impl->context) { NazaraError("RenderTexture cannot be used with this context"); @@ -855,7 +748,7 @@ void NzRenderTexture::Desactivate() const void NzRenderTexture::EnsureTargetUpdated() const { - if (!m_impl->drawBuffersUpdated) + if (!m_drawBuffersUpdated) UpdateDrawBuffers(); for (nzUInt8 index : m_impl->colorTargets) @@ -894,8 +787,7 @@ void NzRenderTexture::OnRenderBufferDestroy(const NzRenderBuffer* renderBuffer, attachment.isUsed = false; attachment.renderBufferDestroySlot.Disconnect(); - m_impl->checked = false; - m_impl->targetsUpdated = false; + InvalidateTargets(); } void NzRenderTexture::OnTextureDestroy(const NzTexture* texture, unsigned int attachmentIndex) @@ -905,23 +797,17 @@ void NzRenderTexture::OnTextureDestroy(const NzTexture* texture, unsigned int at NazaraAssert(!m_impl->attachments[attachmentIndex].isBuffer, "Invalid attachment state"); NazaraUnused(texture); - Attachment& attachment = m_impl->attachments[attachmentIndex]; - attachment.isUsed = false; - attachment.texture = nullptr; - attachment.textureDestroySlot.Disconnect(); - - m_impl->checked = false; - m_impl->targetsUpdated = false; + InvalidateTargets(); } void NzRenderTexture::UpdateDrawBuffers() const { - if (!m_impl->targetsUpdated) + if (!m_targetsUpdated) UpdateTargets(); glDrawBuffers(m_impl->drawBuffers.size(), &m_impl->drawBuffers[0]); - m_impl->drawBuffersUpdated = true; + m_drawBuffersUpdated = true; } void NzRenderTexture::UpdateSize() const @@ -937,7 +823,7 @@ void NzRenderTexture::UpdateSize() const } } - m_impl->sizeUpdated = true; + m_sizeUpdated = true; } void NzRenderTexture::UpdateTargets() const @@ -955,5 +841,5 @@ void NzRenderTexture::UpdateTargets() const *ptr++ = GL_COLOR_ATTACHMENT0 + index; } - m_impl->targetsUpdated = true; + m_targetsUpdated = true; }