Replace HWBufferMap with a list and back pointers (#99)

This commit is contained in:
paradust7 2022-04-29 03:04:22 -07:00 committed by GitHub
parent c9b66c8c58
commit ba0396e93d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 56 additions and 38 deletions

@ -21,6 +21,7 @@ namespace scene
CMeshBuffer() CMeshBuffer()
: ChangedID_Vertex(1), ChangedID_Index(1) : ChangedID_Vertex(1), ChangedID_Index(1)
, MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER) , MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER)
, HWBuffer(NULL)
, PrimitiveType(EPT_TRIANGLES) , PrimitiveType(EPT_TRIANGLES)
{ {
#ifdef _DEBUG #ifdef _DEBUG
@ -286,12 +287,22 @@ namespace scene
/** This shouldn't be used for anything outside the VideoDriver. */ /** This shouldn't be used for anything outside the VideoDriver. */
virtual u32 getChangedID_Index() const _IRR_OVERRIDE_ {return ChangedID_Index;} virtual u32 getChangedID_Index() const _IRR_OVERRIDE_ {return ChangedID_Index;}
virtual void setHWBuffer(void *ptr) const _IRR_OVERRIDE_ {
HWBuffer = ptr;
}
virtual void *getHWBuffer() const _IRR_OVERRIDE_ {
return HWBuffer;
}
u32 ChangedID_Vertex; u32 ChangedID_Vertex;
u32 ChangedID_Index; u32 ChangedID_Index;
//! hardware mapping hint //! hardware mapping hint
E_HARDWARE_MAPPING MappingHint_Vertex; E_HARDWARE_MAPPING MappingHint_Vertex;
E_HARDWARE_MAPPING MappingHint_Index; E_HARDWARE_MAPPING MappingHint_Index;
mutable void *HWBuffer;
//! Material for this meshbuffer. //! Material for this meshbuffer.
video::SMaterial Material; video::SMaterial Material;

@ -145,6 +145,10 @@ namespace scene
/** This shouldn't be used for anything outside the VideoDriver. */ /** This shouldn't be used for anything outside the VideoDriver. */
virtual u32 getChangedID_Index() const = 0; virtual u32 getChangedID_Index() const = 0;
//! Used by the VideoDriver to remember the buffer link.
virtual void setHWBuffer(void *ptr) const = 0;
virtual void *getHWBuffer() const = 0;
//! Describe what kind of primitive geometry is used by the meshbuffer //! Describe what kind of primitive geometry is used by the meshbuffer
/** Note: Default is EPT_TRIANGLES. Using other types is fine for rendering. /** Note: Default is EPT_TRIANGLES. Using other types is fine for rendering.
But meshbuffer manipulation functions might expect type EPT_TRIANGLES But meshbuffer manipulation functions might expect type EPT_TRIANGLES

@ -23,6 +23,7 @@ struct SSkinMeshBuffer : public IMeshBuffer
ChangedID_Vertex(1), ChangedID_Index(1), VertexType(vt), ChangedID_Vertex(1), ChangedID_Index(1), VertexType(vt),
PrimitiveType(EPT_TRIANGLES), PrimitiveType(EPT_TRIANGLES),
MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER), MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER),
HWBuffer(NULL),
BoundingBoxNeedsRecalculated(true) BoundingBoxNeedsRecalculated(true)
{ {
#ifdef _DEBUG #ifdef _DEBUG
@ -383,6 +384,15 @@ struct SSkinMeshBuffer : public IMeshBuffer
virtual u32 getChangedID_Index() const _IRR_OVERRIDE_ {return ChangedID_Index;} virtual u32 getChangedID_Index() const _IRR_OVERRIDE_ {return ChangedID_Index;}
virtual void setHWBuffer(void *ptr) const _IRR_OVERRIDE_ {
HWBuffer = ptr;
}
virtual void *getHWBuffer() const _IRR_OVERRIDE_ {
return HWBuffer;
}
//! Call this after changing the positions of any vertex. //! Call this after changing the positions of any vertex.
void boundingBoxNeedsRecalculated(void) { BoundingBoxNeedsRecalculated = true; } void boundingBoxNeedsRecalculated(void) { BoundingBoxNeedsRecalculated = true; }
@ -409,6 +419,8 @@ struct SSkinMeshBuffer : public IMeshBuffer
E_HARDWARE_MAPPING MappingHint_Vertex:3; E_HARDWARE_MAPPING MappingHint_Vertex:3;
E_HARDWARE_MAPPING MappingHint_Index:3; E_HARDWARE_MAPPING MappingHint_Index:3;
mutable void *HWBuffer;
bool BoundingBoxNeedsRecalculated:1; bool BoundingBoxNeedsRecalculated:1;
}; };

@ -1689,9 +1689,9 @@ CNullDriver::SHWBufferLink *CNullDriver::getBufferLink(const scene::IMeshBuffer*
return 0; return 0;
//search for hardware links //search for hardware links
core::map< const scene::IMeshBuffer*,SHWBufferLink* >::Node* node = HWBufferMap.find(mb); SHWBufferLink *HWBuffer = reinterpret_cast<SHWBufferLink*>(mb->getHWBuffer());
if (node) if (HWBuffer)
return node->getValue(); return HWBuffer;
return createHardwareBuffer(mb); //no hardware links, and mesh wants one, create it return createHardwareBuffer(mb); //no hardware links, and mesh wants one, create it
} }
@ -1700,20 +1700,13 @@ CNullDriver::SHWBufferLink *CNullDriver::getBufferLink(const scene::IMeshBuffer*
//! Update all hardware buffers, remove unused ones //! Update all hardware buffers, remove unused ones
void CNullDriver::updateAllHardwareBuffers() void CNullDriver::updateAllHardwareBuffers()
{ {
core::map<const scene::IMeshBuffer*,SHWBufferLink*>::ParentFirstIterator Iterator=HWBufferMap.getParentFirstIterator(); auto it = HWBufferList.begin();
while (it != HWBufferList.end()) {
SHWBufferLink *Link = *it;
++it;
for (;!Iterator.atEnd();Iterator++) if (!Link->MeshBuffer || Link->MeshBuffer->getReferenceCount() == 1)
{
SHWBufferLink *Link=Iterator.getNode()->getValue();
Link->LastUsed++;
if (Link->LastUsed>20000)
{
deleteHardwareBuffer(Link); deleteHardwareBuffer(Link);
// todo: needs better fix
Iterator = HWBufferMap.getParentFirstIterator();
}
} }
} }
@ -1722,7 +1715,7 @@ void CNullDriver::deleteHardwareBuffer(SHWBufferLink *HWBuffer)
{ {
if (!HWBuffer) if (!HWBuffer)
return; return;
HWBufferMap.remove(HWBuffer->MeshBuffer); HWBufferList.erase(HWBuffer->listPosition);
delete HWBuffer; delete HWBuffer;
} }
@ -1730,17 +1723,19 @@ void CNullDriver::deleteHardwareBuffer(SHWBufferLink *HWBuffer)
//! Remove hardware buffer //! Remove hardware buffer
void CNullDriver::removeHardwareBuffer(const scene::IMeshBuffer* mb) void CNullDriver::removeHardwareBuffer(const scene::IMeshBuffer* mb)
{ {
core::map<const scene::IMeshBuffer*,SHWBufferLink*>::Node* node = HWBufferMap.find(mb); if (!mb)
if (node) return;
deleteHardwareBuffer(node->getValue()); SHWBufferLink *HWBuffer = reinterpret_cast<SHWBufferLink*>(mb->getHWBuffer());
if (HWBuffer)
deleteHardwareBuffer(HWBuffer);
} }
//! Remove all hardware buffers //! Remove all hardware buffers
void CNullDriver::removeAllHardwareBuffers() void CNullDriver::removeAllHardwareBuffers()
{ {
while (HWBufferMap.size()) while (!HWBufferList.empty())
deleteHardwareBuffer(HWBufferMap.getRoot()->getValue()); deleteHardwareBuffer(HWBufferList.front());
} }

@ -21,6 +21,7 @@
#include "SVertexIndex.h" #include "SVertexIndex.h"
#include "SLight.h" #include "SLight.h"
#include "SExposedVideoData.h" #include "SExposedVideoData.h"
#include <list>
namespace irr namespace irr
{ {
@ -388,25 +389,29 @@ namespace video
{ {
SHWBufferLink(const scene::IMeshBuffer *_MeshBuffer) SHWBufferLink(const scene::IMeshBuffer *_MeshBuffer)
:MeshBuffer(_MeshBuffer), :MeshBuffer(_MeshBuffer),
ChangedID_Vertex(0),ChangedID_Index(0),LastUsed(0), ChangedID_Vertex(0),ChangedID_Index(0),
Mapped_Vertex(scene::EHM_NEVER),Mapped_Index(scene::EHM_NEVER) Mapped_Vertex(scene::EHM_NEVER),Mapped_Index(scene::EHM_NEVER)
{ {
if (MeshBuffer) if (MeshBuffer) {
MeshBuffer->grab(); MeshBuffer->grab();
MeshBuffer->setHWBuffer(reinterpret_cast<void*>(this));
}
} }
virtual ~SHWBufferLink() virtual ~SHWBufferLink()
{ {
if (MeshBuffer) if (MeshBuffer) {
MeshBuffer->setHWBuffer(NULL);
MeshBuffer->drop(); MeshBuffer->drop();
} }
}
const scene::IMeshBuffer *MeshBuffer; const scene::IMeshBuffer *MeshBuffer;
u32 ChangedID_Vertex; u32 ChangedID_Vertex;
u32 ChangedID_Index; u32 ChangedID_Index;
u32 LastUsed;
scene::E_HARDWARE_MAPPING Mapped_Vertex; scene::E_HARDWARE_MAPPING Mapped_Vertex;
scene::E_HARDWARE_MAPPING Mapped_Index; scene::E_HARDWARE_MAPPING Mapped_Index;
std::list<SHWBufferLink*>::iterator listPosition;
}; };
//! Gets hardware buffer link from a meshbuffer (may create or update buffer) //! Gets hardware buffer link from a meshbuffer (may create or update buffer)
@ -818,8 +823,7 @@ namespace video
core::array<SLight> Lights; core::array<SLight> Lights;
core::array<SMaterialRenderer> MaterialRenderers; core::array<SMaterialRenderer> MaterialRenderers;
//core::array<SHWBufferLink*> HWBufferLinks; std::list<SHWBufferLink*> HWBufferList;
core::map< const scene::IMeshBuffer* , SHWBufferLink* > HWBufferMap;
io::IFileSystem* FileSystem; io::IFileSystem* FileSystem;

@ -589,13 +589,12 @@ COGLES2Driver::~COGLES2Driver()
SHWBufferLink_opengl *HWBuffer = new SHWBufferLink_opengl(mb); SHWBufferLink_opengl *HWBuffer = new SHWBufferLink_opengl(mb);
//add to map //add to map
HWBufferMap.insert(HWBuffer->MeshBuffer, HWBuffer); HWBuffer->listPosition = HWBufferList.insert(HWBufferList.end(), HWBuffer);
HWBuffer->ChangedID_Vertex = HWBuffer->MeshBuffer->getChangedID_Vertex(); HWBuffer->ChangedID_Vertex = HWBuffer->MeshBuffer->getChangedID_Vertex();
HWBuffer->ChangedID_Index = HWBuffer->MeshBuffer->getChangedID_Index(); HWBuffer->ChangedID_Index = HWBuffer->MeshBuffer->getChangedID_Index();
HWBuffer->Mapped_Vertex = mb->getHardwareMappingHint_Vertex(); HWBuffer->Mapped_Vertex = mb->getHardwareMappingHint_Vertex();
HWBuffer->Mapped_Index = mb->getHardwareMappingHint_Index(); HWBuffer->Mapped_Index = mb->getHardwareMappingHint_Index();
HWBuffer->LastUsed = 0;
HWBuffer->vbo_verticesID = 0; HWBuffer->vbo_verticesID = 0;
HWBuffer->vbo_indicesID = 0; HWBuffer->vbo_indicesID = 0;
HWBuffer->vbo_verticesSize = 0; HWBuffer->vbo_verticesSize = 0;
@ -642,8 +641,6 @@ COGLES2Driver::~COGLES2Driver()
updateHardwareBuffer(HWBuffer); //check if update is needed updateHardwareBuffer(HWBuffer); //check if update is needed
HWBuffer->LastUsed = 0;//reset count
const scene::IMeshBuffer* mb = HWBuffer->MeshBuffer; const scene::IMeshBuffer* mb = HWBuffer->MeshBuffer;
const void *vertices = mb->getVertices(); const void *vertices = mb->getVertices();
const void *indexList = mb->getIndices(); const void *indexList = mb->getIndices();

@ -456,13 +456,12 @@ COGLES1Driver::SHWBufferLink *COGLES1Driver::createHardwareBuffer(const scene::I
SHWBufferLink_opengl *HWBuffer=new SHWBufferLink_opengl(mb); SHWBufferLink_opengl *HWBuffer=new SHWBufferLink_opengl(mb);
//add to map //add to map
HWBufferMap.insert(HWBuffer->MeshBuffer, HWBuffer); HWBuffer->listPosition = HWBufferList.insert(HWBufferList.end(), HWBuffer);
HWBuffer->ChangedID_Vertex=HWBuffer->MeshBuffer->getChangedID_Vertex(); HWBuffer->ChangedID_Vertex=HWBuffer->MeshBuffer->getChangedID_Vertex();
HWBuffer->ChangedID_Index=HWBuffer->MeshBuffer->getChangedID_Index(); HWBuffer->ChangedID_Index=HWBuffer->MeshBuffer->getChangedID_Index();
HWBuffer->Mapped_Vertex=mb->getHardwareMappingHint_Vertex(); HWBuffer->Mapped_Vertex=mb->getHardwareMappingHint_Vertex();
HWBuffer->Mapped_Index=mb->getHardwareMappingHint_Index(); HWBuffer->Mapped_Index=mb->getHardwareMappingHint_Index();
HWBuffer->LastUsed=0;
HWBuffer->vbo_verticesID=0; HWBuffer->vbo_verticesID=0;
HWBuffer->vbo_indicesID=0; HWBuffer->vbo_indicesID=0;
HWBuffer->vbo_verticesSize=0; HWBuffer->vbo_verticesSize=0;
@ -509,8 +508,6 @@ void COGLES1Driver::drawHardwareBuffer(SHWBufferLink *_HWBuffer)
updateHardwareBuffer(HWBuffer); //check if update is needed updateHardwareBuffer(HWBuffer); //check if update is needed
HWBuffer->LastUsed=0;//reset count
const scene::IMeshBuffer* mb = HWBuffer->MeshBuffer; const scene::IMeshBuffer* mb = HWBuffer->MeshBuffer;
const void *vertices=mb->getVertices(); const void *vertices=mb->getVertices();
const void *indexList=mb->getIndices(); const void *indexList=mb->getIndices();

@ -571,13 +571,12 @@ COpenGLDriver::SHWBufferLink *COpenGLDriver::createHardwareBuffer(const scene::I
SHWBufferLink_opengl *HWBuffer=new SHWBufferLink_opengl(mb); SHWBufferLink_opengl *HWBuffer=new SHWBufferLink_opengl(mb);
//add to map //add to map
HWBufferMap.insert(HWBuffer->MeshBuffer, HWBuffer); HWBuffer->listPosition = HWBufferList.insert(HWBufferList.end(), HWBuffer);
HWBuffer->ChangedID_Vertex=HWBuffer->MeshBuffer->getChangedID_Vertex(); HWBuffer->ChangedID_Vertex=HWBuffer->MeshBuffer->getChangedID_Vertex();
HWBuffer->ChangedID_Index=HWBuffer->MeshBuffer->getChangedID_Index(); HWBuffer->ChangedID_Index=HWBuffer->MeshBuffer->getChangedID_Index();
HWBuffer->Mapped_Vertex=mb->getHardwareMappingHint_Vertex(); HWBuffer->Mapped_Vertex=mb->getHardwareMappingHint_Vertex();
HWBuffer->Mapped_Index=mb->getHardwareMappingHint_Index(); HWBuffer->Mapped_Index=mb->getHardwareMappingHint_Index();
HWBuffer->LastUsed=0;
HWBuffer->vbo_verticesID=0; HWBuffer->vbo_verticesID=0;
HWBuffer->vbo_indicesID=0; HWBuffer->vbo_indicesID=0;
HWBuffer->vbo_verticesSize=0; HWBuffer->vbo_verticesSize=0;
@ -626,7 +625,6 @@ void COpenGLDriver::drawHardwareBuffer(SHWBufferLink *_HWBuffer)
return; return;
updateHardwareBuffer(_HWBuffer); //check if update is needed updateHardwareBuffer(_HWBuffer); //check if update is needed
_HWBuffer->LastUsed=0; //reset count
#if defined(GL_ARB_vertex_buffer_object) #if defined(GL_ARB_vertex_buffer_object)
SHWBufferLink_opengl *HWBuffer=(SHWBufferLink_opengl*)_HWBuffer; SHWBufferLink_opengl *HWBuffer=(SHWBufferLink_opengl*)_HWBuffer;