Added ResourceListenerWrapper
This class wraps the call to Resource::AddResourceListener/RemoveResourceListener using RAII and help a lot with some of the dependencies. Thanks to this, the render queues now handle their resources listening properly. Former-commit-id: 7f215ffa4ccadcc4f44f777656970e92ce01087a
This commit is contained in:
@@ -19,8 +19,17 @@ namespace
|
||||
{
|
||||
struct Attachment
|
||||
{
|
||||
Attachment(NzResourceListener* listener, int bufferIndex = 0, int textureIndex = 0) :
|
||||
bufferListener(listener, bufferIndex),
|
||||
textureListener(listener, textureIndex)
|
||||
{
|
||||
}
|
||||
|
||||
NzRenderBufferRef buffer;
|
||||
NzTextureRef texture;
|
||||
// Les listeners doivent se trouver après les références (pour être libérés avant elles)
|
||||
NzRenderBufferListener bufferListener;
|
||||
NzTextureListener textureListener;
|
||||
|
||||
nzAttachmentPoint attachmentPoint;
|
||||
bool isBuffer;
|
||||
@@ -51,11 +60,16 @@ namespace
|
||||
|
||||
struct NzRenderTextureImpl
|
||||
{
|
||||
NzRenderTextureImpl(NzResourceListener* listener, int contextIndex = 0) :
|
||||
context(listener, contextIndex)
|
||||
{
|
||||
}
|
||||
|
||||
GLuint fbo;
|
||||
std::vector<Attachment> attachments;
|
||||
std::vector<nzUInt8> colorTargets;
|
||||
mutable std::vector<GLenum> drawBuffers;
|
||||
const NzContext* context;
|
||||
NzContextConstListener context;
|
||||
bool checked = false;
|
||||
bool complete = false;
|
||||
bool userDefinedTargets = false;
|
||||
@@ -139,20 +153,23 @@ bool NzRenderTexture::AttachBuffer(nzAttachmentPoint attachmentPoint, nzUInt8 in
|
||||
|
||||
Unlock();
|
||||
|
||||
unsigned int attachIndex = attachmentIndex[attachmentPoint]+index;
|
||||
if (m_impl->attachments.size() <= attachIndex)
|
||||
m_impl->attachments.resize(attachIndex+1);
|
||||
unsigned int attachIndex = attachmentIndex[attachmentPoint] + index;
|
||||
// On créé les attachements si ça n'a pas déjà été fait
|
||||
for (unsigned int i = m_impl->attachments.size(); i <= attachIndex; ++i)
|
||||
{
|
||||
Attachment attachment(this, attachIndex, attachIndex);
|
||||
m_impl->attachments.emplace_back(std::move(attachment));
|
||||
}
|
||||
|
||||
Attachment& attachment = m_impl->attachments[attachIndex];
|
||||
attachment.attachmentPoint = attachmentPoint;
|
||||
attachment.buffer = buffer;
|
||||
attachment.bufferListener = buffer;
|
||||
attachment.isBuffer = true;
|
||||
attachment.isUsed = true;
|
||||
attachment.height = buffer->GetHeight();
|
||||
attachment.width = buffer->GetWidth();
|
||||
|
||||
buffer->AddResourceListener(this, attachIndex);
|
||||
|
||||
m_impl->checked = false;
|
||||
|
||||
if (attachmentPoint == nzAttachmentPoint_Color && !m_impl->userDefinedTargets)
|
||||
@@ -283,9 +300,14 @@ bool NzRenderTexture::AttachTexture(nzAttachmentPoint attachmentPoint, nzUInt8 i
|
||||
|
||||
Unlock();
|
||||
|
||||
unsigned int attachIndex = attachmentIndex[attachmentPoint]+index;
|
||||
if (m_impl->attachments.size() <= attachIndex)
|
||||
m_impl->attachments.resize(attachIndex+1);
|
||||
unsigned int attachIndex = attachmentIndex[attachmentPoint] + index;
|
||||
|
||||
// On créé les attachements si ça n'a pas déjà été fait
|
||||
for (unsigned int i = m_impl->attachments.size(); i <= attachIndex; ++i)
|
||||
{
|
||||
Attachment attachment(this, attachIndex, attachIndex);
|
||||
m_impl->attachments.emplace_back(std::move(attachment));
|
||||
}
|
||||
|
||||
Attachment& attachment = m_impl->attachments[attachIndex];
|
||||
attachment.attachmentPoint = attachmentPoint;
|
||||
@@ -293,10 +315,9 @@ bool NzRenderTexture::AttachTexture(nzAttachmentPoint attachmentPoint, nzUInt8 i
|
||||
attachment.isUsed = true;
|
||||
attachment.height = texture->GetHeight();
|
||||
attachment.texture = texture;
|
||||
attachment.textureListener = texture;
|
||||
attachment.width = texture->GetWidth();
|
||||
|
||||
texture->AddResourceListener(this, attachIndex);
|
||||
|
||||
m_impl->checked = false;
|
||||
|
||||
if (attachmentPoint == nzAttachmentPoint_Color && !m_impl->userDefinedTargets)
|
||||
@@ -327,7 +348,7 @@ bool NzRenderTexture::Create(bool lock)
|
||||
}
|
||||
#endif
|
||||
|
||||
std::unique_ptr<NzRenderTextureImpl> impl(new NzRenderTextureImpl);
|
||||
std::unique_ptr<NzRenderTextureImpl> impl(new NzRenderTextureImpl(this));
|
||||
|
||||
impl->fbo = 0;
|
||||
glGenFramebuffers(1, &impl->fbo);
|
||||
@@ -340,7 +361,6 @@ bool NzRenderTexture::Create(bool lock)
|
||||
|
||||
m_impl = impl.release();
|
||||
m_impl->context = NzContext::GetCurrent();
|
||||
m_impl->context->AddResourceListener(this);
|
||||
|
||||
if (lock)
|
||||
{
|
||||
@@ -372,19 +392,6 @@ void NzRenderTexture::Destroy()
|
||||
if (IsActive())
|
||||
NzRenderer::SetTarget(nullptr);
|
||||
|
||||
m_impl->context->RemoveResourceListener(this);
|
||||
|
||||
for (const Attachment& attachment : m_impl->attachments)
|
||||
{
|
||||
if (attachment.isUsed)
|
||||
{
|
||||
if (attachment.isBuffer)
|
||||
attachment.buffer->RemoveResourceListener(this);
|
||||
else
|
||||
attachment.texture->RemoveResourceListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
// Le FBO devant être supprimé dans son contexte d'origine, nous déléguons sa suppression à la classe OpenGL
|
||||
// Celle-ci va libérer le FBO dès que possible (la prochaine fois que son contexte d'origine sera actif)
|
||||
NzOpenGL::DeleteFrameBuffer(m_impl->context, m_impl->fbo);
|
||||
@@ -410,7 +417,7 @@ void NzRenderTexture::Detach(nzAttachmentPoint attachmentPoint, nzUInt8 index)
|
||||
}
|
||||
#endif
|
||||
|
||||
unsigned int attachIndex = attachmentIndex[attachmentPoint]+index;
|
||||
unsigned int attachIndex = attachmentIndex[attachmentPoint] + index;
|
||||
if (attachIndex >= m_impl->attachments.size())
|
||||
return;
|
||||
|
||||
@@ -430,7 +437,7 @@ void NzRenderTexture::Detach(nzAttachmentPoint attachmentPoint, nzUInt8 index)
|
||||
{
|
||||
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, NzOpenGL::Attachment[attachmentPoint]+index, GL_RENDERBUFFER, 0);
|
||||
|
||||
attachement.buffer->RemoveResourceListener(this);
|
||||
attachement.bufferListener = nullptr;
|
||||
attachement.buffer = nullptr;
|
||||
}
|
||||
else
|
||||
@@ -440,7 +447,7 @@ void NzRenderTexture::Detach(nzAttachmentPoint attachmentPoint, nzUInt8 index)
|
||||
else
|
||||
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, NzOpenGL::Attachment[attachmentPoint]+index, 0, 0, 0);
|
||||
|
||||
attachement.texture->RemoveResourceListener(this);
|
||||
attachement.textureListener = nullptr;
|
||||
attachement.texture = nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -66,10 +66,38 @@ namespace
|
||||
bool samplerUpdated = false;
|
||||
};
|
||||
|
||||
using VAO_Key = std::tuple<const NzIndexBuffer*, const NzVertexBuffer*, const NzVertexDeclaration*, const NzVertexDeclaration*>;
|
||||
using VAO_Map = std::map<VAO_Key, GLuint>;
|
||||
struct VAO_Entry
|
||||
{
|
||||
VAO_Entry(NzResourceListener* listener, int indexBufferIndex, int vertexBufferIndex, int vertexDeclarationIndex, int instancingDeclarationIndex) :
|
||||
indexBufferListener(listener, indexBufferIndex),
|
||||
vertexBufferListener(listener, vertexBufferIndex),
|
||||
instancingDeclarationListener(listener, instancingDeclarationIndex),
|
||||
vertexDeclarationListener(listener, vertexDeclarationIndex)
|
||||
{
|
||||
}
|
||||
|
||||
using Context_Map = std::unordered_map<const NzContext*, VAO_Map>;
|
||||
GLuint vao;
|
||||
NzIndexBufferConstListener indexBufferListener;
|
||||
NzVertexBufferConstListener vertexBufferListener;
|
||||
NzVertexDeclarationConstListener instancingDeclarationListener;
|
||||
NzVertexDeclarationConstListener vertexDeclarationListener;
|
||||
};
|
||||
|
||||
using VAO_Key = std::tuple<const NzIndexBuffer*, const NzVertexBuffer*, const NzVertexDeclaration*, const NzVertexDeclaration*>;
|
||||
using VAO_Map = std::map<VAO_Key, VAO_Entry>;
|
||||
|
||||
struct Context_Entry
|
||||
{
|
||||
Context_Entry(NzResourceListener* listener, int index) :
|
||||
contextListener(listener, index)
|
||||
{
|
||||
}
|
||||
|
||||
NzContextConstListener contextListener;
|
||||
VAO_Map vaoMap;
|
||||
};
|
||||
|
||||
using Context_Map = std::unordered_map<const NzContext*, Context_Entry>;
|
||||
|
||||
Context_Map s_vaos;
|
||||
std::vector<unsigned int> s_dirtyTextureUnits;
|
||||
@@ -116,7 +144,7 @@ namespace
|
||||
for (auto& pair : s_vaos)
|
||||
{
|
||||
const NzContext* context = pair.first;
|
||||
VAO_Map& vaos = pair.second;
|
||||
VAO_Map& vaos = pair.second.vaoMap;
|
||||
|
||||
auto it = vaos.begin();
|
||||
while (it != vaos.end())
|
||||
@@ -131,7 +159,7 @@ namespace
|
||||
// son contexte d'origine est actif, sinon il faudra le mettre en file d'attente
|
||||
// Ceci est géré par la méthode OpenGL::DeleteVertexArray
|
||||
|
||||
NzOpenGL::DeleteVertexArray(context, it->second);
|
||||
NzOpenGL::DeleteVertexArray(context, it->second.vao);
|
||||
vaos.erase(it++);
|
||||
}
|
||||
else
|
||||
@@ -147,7 +175,7 @@ namespace
|
||||
for (auto& pair : s_vaos)
|
||||
{
|
||||
const NzContext* context = pair.first;
|
||||
VAO_Map& vaos = pair.second;
|
||||
VAO_Map& vaos = pair.second.vaoMap;
|
||||
|
||||
auto it = vaos.begin();
|
||||
while (it != vaos.end())
|
||||
@@ -162,7 +190,7 @@ namespace
|
||||
// son contexte d'origine est actif, sinon il faudra le mettre en file d'attente
|
||||
// Ceci est géré par la méthode OpenGL::DeleteVertexArray
|
||||
|
||||
NzOpenGL::DeleteVertexArray(context, it->second);
|
||||
NzOpenGL::DeleteVertexArray(context, it->second.vao);
|
||||
vaos.erase(it++);
|
||||
}
|
||||
else
|
||||
@@ -178,7 +206,7 @@ namespace
|
||||
for (auto& pair : s_vaos)
|
||||
{
|
||||
const NzContext* context = pair.first;
|
||||
VAO_Map& vaos = pair.second;
|
||||
VAO_Map& vaos = pair.second.vaoMap;
|
||||
|
||||
auto it = vaos.begin();
|
||||
while (it != vaos.end())
|
||||
@@ -194,7 +222,7 @@ namespace
|
||||
// son contexte d'origine est actif, sinon il faudra le mettre en file d'attente
|
||||
// Ceci est géré par la méthode OpenGL::DeleteVertexArray
|
||||
|
||||
NzOpenGL::DeleteVertexArray(context, it->second);
|
||||
NzOpenGL::DeleteVertexArray(context, it->second.vao);
|
||||
vaos.erase(it++);
|
||||
}
|
||||
else
|
||||
@@ -1533,28 +1561,14 @@ void NzRenderer::Uninitialize()
|
||||
for (auto& pair : s_vaos)
|
||||
{
|
||||
const NzContext* context = pair.first;
|
||||
const Context_Entry& contextEntry = pair.second;
|
||||
|
||||
for (auto& pair2 : pair.second)
|
||||
for (auto& pair2 : contextEntry.vaoMap)
|
||||
{
|
||||
const VAO_Key& key = pair2.first;
|
||||
const NzIndexBuffer* indexBuffer = std::get<0>(key);
|
||||
const NzVertexBuffer* vertexBuffer = std::get<1>(key);
|
||||
const NzVertexDeclaration* vertexDeclaration = std::get<2>(key);
|
||||
const NzVertexDeclaration* instancingDeclaration = std::get<3>(key);
|
||||
|
||||
if (indexBuffer)
|
||||
indexBuffer->RemoveResourceListener(&s_listener);
|
||||
|
||||
vertexBuffer->RemoveResourceListener(&s_listener);
|
||||
vertexDeclaration->RemoveResourceListener(&s_listener);
|
||||
|
||||
if (instancingDeclaration)
|
||||
instancingDeclaration->RemoveResourceListener(&s_listener);
|
||||
|
||||
NzOpenGL::DeleteVertexArray(context, pair2.second);
|
||||
const VAO_Entry& entry = pair2.second;
|
||||
NzOpenGL::DeleteVertexArray(context, entry.vao);
|
||||
}
|
||||
}
|
||||
|
||||
s_vaos.clear();
|
||||
|
||||
NzOpenGL::Uninitialize();
|
||||
@@ -1719,16 +1733,16 @@ bool NzRenderer::EnsureStateUpdate()
|
||||
// Note: Les VAOs ne sont pas partagés entre les contextes, nous avons donc un tableau de VAOs par contexte
|
||||
const NzContext* context = NzContext::GetCurrent();
|
||||
|
||||
VAO_Map* vaos;
|
||||
auto it = s_vaos.find(context);
|
||||
if (it == s_vaos.end())
|
||||
{
|
||||
context->AddResourceListener(&s_listener, ResourceType_Context);
|
||||
auto pair = s_vaos.insert(std::make_pair(context, Context_Map::mapped_type()));
|
||||
vaos = &pair.first->second;
|
||||
Context_Entry entry(&s_listener, ResourceType_Context);
|
||||
entry.contextListener = context;
|
||||
|
||||
it = s_vaos.insert(std::make_pair(context, std::move(entry))).first;
|
||||
}
|
||||
else
|
||||
vaos = &it->second;
|
||||
|
||||
VAO_Map& vaoMap = it->second.vaoMap;
|
||||
|
||||
// Notre clé est composée de ce qui définit un VAO
|
||||
const NzVertexDeclaration* vertexDeclaration = s_vertexBuffer->GetVertexDeclaration();
|
||||
@@ -1736,23 +1750,22 @@ bool NzRenderer::EnsureStateUpdate()
|
||||
VAO_Key key(s_indexBuffer, s_vertexBuffer, vertexDeclaration, instancingDeclaration);
|
||||
|
||||
// On recherche un VAO existant avec notre configuration
|
||||
vaoIt = vaos->find(key);
|
||||
if (vaoIt == vaos->end())
|
||||
vaoIt = vaoMap.find(key);
|
||||
if (vaoIt == vaoMap.end())
|
||||
{
|
||||
// On créé notre VAO
|
||||
glGenVertexArrays(1, &s_currentVAO);
|
||||
glBindVertexArray(s_currentVAO);
|
||||
|
||||
// On l'ajoute à notre liste
|
||||
vaoIt = vaos->insert(std::make_pair(key, s_currentVAO)).first;
|
||||
if (s_indexBuffer)
|
||||
s_indexBuffer->AddResourceListener(&s_listener, ResourceType_IndexBuffer);
|
||||
VAO_Entry entry(&s_listener, ResourceType_IndexBuffer, ResourceType_VertexBuffer, ResourceType_VertexDeclaration, ResourceType_VertexDeclaration);
|
||||
entry.indexBufferListener = std::get<0>(key);
|
||||
entry.instancingDeclarationListener = std::get<3>(key);
|
||||
entry.vertexBufferListener = std::get<1>(key);
|
||||
entry.vertexDeclarationListener = std::get<2>(key);
|
||||
entry.vao = s_currentVAO;
|
||||
|
||||
s_vertexBuffer->AddResourceListener(&s_listener, ResourceType_VertexBuffer);
|
||||
vertexDeclaration->AddResourceListener(&s_listener, ResourceType_VertexDeclaration);
|
||||
|
||||
if (instancingDeclaration)
|
||||
instancingDeclaration->AddResourceListener(&s_listener, ResourceType_VertexDeclaration);
|
||||
vaoIt = vaoMap.insert(std::make_pair(key, std::move(entry))).first;
|
||||
|
||||
// Et on indique qu'on veut le programmer
|
||||
update = true;
|
||||
@@ -1760,7 +1773,7 @@ bool NzRenderer::EnsureStateUpdate()
|
||||
else
|
||||
{
|
||||
// Notre VAO existe déjà, il est donc inutile de le reprogrammer
|
||||
s_currentVAO = vaoIt->second;
|
||||
s_currentVAO = vaoIt->second.vao;
|
||||
|
||||
update = false;
|
||||
}
|
||||
@@ -1911,8 +1924,8 @@ bool NzRenderer::EnsureStateUpdate()
|
||||
if (updateFailed)
|
||||
{
|
||||
// La création de notre VAO a échoué, libérons-le et marquons-le comme problématique
|
||||
glDeleteVertexArrays(1, &vaoIt->second);
|
||||
vaoIt->second = 0;
|
||||
glDeleteVertexArrays(1, &vaoIt->second.vao);
|
||||
vaoIt->second.vao = 0;
|
||||
s_currentVAO = 0;
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user