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

View File

@ -21,6 +21,7 @@ namespace scene
CMeshBuffer()
: ChangedID_Vertex(1), ChangedID_Index(1)
, MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER)
, HWBuffer(NULL)
, PrimitiveType(EPT_TRIANGLES)
{
#ifdef _DEBUG
@ -286,12 +287,22 @@ namespace scene
/** This shouldn't be used for anything outside the VideoDriver. */
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_Index;
//! hardware mapping hint
E_HARDWARE_MAPPING MappingHint_Vertex;
E_HARDWARE_MAPPING MappingHint_Index;
mutable void *HWBuffer;
//! Material for this meshbuffer.
video::SMaterial Material;

View File

@ -145,6 +145,10 @@ namespace scene
/** This shouldn't be used for anything outside the VideoDriver. */
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
/** Note: Default is EPT_TRIANGLES. Using other types is fine for rendering.
But meshbuffer manipulation functions might expect type EPT_TRIANGLES

View File

@ -23,6 +23,7 @@ struct SSkinMeshBuffer : public IMeshBuffer
ChangedID_Vertex(1), ChangedID_Index(1), VertexType(vt),
PrimitiveType(EPT_TRIANGLES),
MappingHint_Vertex(EHM_NEVER), MappingHint_Index(EHM_NEVER),
HWBuffer(NULL),
BoundingBoxNeedsRecalculated(true)
{
#ifdef _DEBUG
@ -383,6 +384,15 @@ struct SSkinMeshBuffer : public IMeshBuffer
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.
void boundingBoxNeedsRecalculated(void) { BoundingBoxNeedsRecalculated = true; }
@ -409,6 +419,8 @@ struct SSkinMeshBuffer : public IMeshBuffer
E_HARDWARE_MAPPING MappingHint_Vertex:3;
E_HARDWARE_MAPPING MappingHint_Index:3;
mutable void *HWBuffer;
bool BoundingBoxNeedsRecalculated:1;
};

View File

@ -1689,9 +1689,9 @@ CNullDriver::SHWBufferLink *CNullDriver::getBufferLink(const scene::IMeshBuffer*
return 0;
//search for hardware links
core::map< const scene::IMeshBuffer*,SHWBufferLink* >::Node* node = HWBufferMap.find(mb);
if (node)
return node->getValue();
SHWBufferLink *HWBuffer = reinterpret_cast<SHWBufferLink*>(mb->getHWBuffer());
if (HWBuffer)
return HWBuffer;
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
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++)
{
SHWBufferLink *Link=Iterator.getNode()->getValue();
Link->LastUsed++;
if (Link->LastUsed>20000)
{
if (!Link->MeshBuffer || Link->MeshBuffer->getReferenceCount() == 1)
deleteHardwareBuffer(Link);
// todo: needs better fix
Iterator = HWBufferMap.getParentFirstIterator();
}
}
}
@ -1722,7 +1715,7 @@ void CNullDriver::deleteHardwareBuffer(SHWBufferLink *HWBuffer)
{
if (!HWBuffer)
return;
HWBufferMap.remove(HWBuffer->MeshBuffer);
HWBufferList.erase(HWBuffer->listPosition);
delete HWBuffer;
}
@ -1730,17 +1723,19 @@ void CNullDriver::deleteHardwareBuffer(SHWBufferLink *HWBuffer)
//! Remove hardware buffer
void CNullDriver::removeHardwareBuffer(const scene::IMeshBuffer* mb)
{
core::map<const scene::IMeshBuffer*,SHWBufferLink*>::Node* node = HWBufferMap.find(mb);
if (node)
deleteHardwareBuffer(node->getValue());
if (!mb)
return;
SHWBufferLink *HWBuffer = reinterpret_cast<SHWBufferLink*>(mb->getHWBuffer());
if (HWBuffer)
deleteHardwareBuffer(HWBuffer);
}
//! Remove all hardware buffers
void CNullDriver::removeAllHardwareBuffers()
{
while (HWBufferMap.size())
deleteHardwareBuffer(HWBufferMap.getRoot()->getValue());
while (!HWBufferList.empty())
deleteHardwareBuffer(HWBufferList.front());
}

View File

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

View File

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

View File

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

View File

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