Split CIndexBuffer from CMeshBuffer

This commit is contained in:
sfan5 2024-08-27 17:40:29 +02:00
parent 5d6e15bc49
commit 47e4c33a50
13 changed files with 197 additions and 104 deletions

View File

@ -0,0 +1,89 @@
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#pragma once
#include <vector>
#include "IIndexBuffer.h"
namespace irr
{
namespace scene
{
//! Template implementation of the IIndexBuffer interface
template <class T>
class CIndexBuffer : public IIndexBuffer
{
public:
//! Default constructor for empty buffer
CIndexBuffer()
{
#ifdef _DEBUG
setDebugName("CIndexBuffer");
#endif
}
video::E_INDEX_TYPE getType() const
{
static_assert(sizeof(T) == 2 || sizeof(T) == 4, "invalid index type");
return sizeof(T) == 2 ? video::EIT_16BIT : video::EIT_32BIT;
}
const void *getData() const override
{
return Data.data();
}
void *getData() override
{
return Data.data();
}
u32 getCount() const override
{
return static_cast<u32>(Data.size());
}
E_HARDWARE_MAPPING getHardwareMappingHint() const override
{
return MappingHint;
}
void setHardwareMappingHint(E_HARDWARE_MAPPING NewMappingHint) override
{
MappingHint = NewMappingHint;
}
void setDirty() override
{
++ChangedID;
}
u32 getChangedID() const override { return ChangedID; }
void setHWBuffer(void *ptr) const override
{
HWBuffer = ptr;
}
void *getHWBuffer() const override
{
return HWBuffer;
}
u32 ChangedID = 1;
//! hardware mapping hint
E_HARDWARE_MAPPING MappingHint = EHM_NEVER;
mutable void *HWBuffer = nullptr;
//! Indices of this buffer
std::vector<T> Data;
};
//! Standard 16-bit buffer
typedef CIndexBuffer<u16> SIndexBuffer;
} // end namespace scene
} // end namespace irr

View File

@ -7,6 +7,7 @@
#include <vector>
#include "IMeshBuffer.h"
#include "CVertexBuffer.h"
#include "CIndexBuffer.h"
namespace irr
{
@ -19,17 +20,19 @@ class CMeshBuffer : public IMeshBuffer
public:
//! Default constructor for empty meshbuffer
CMeshBuffer() :
ChangedID_Index(1), MappingHint_Index(EHM_NEVER), HWBuffer(NULL), PrimitiveType(EPT_TRIANGLES)
HWBuffer(NULL), PrimitiveType(EPT_TRIANGLES)
{
#ifdef _DEBUG
setDebugName("CMeshBuffer");
#endif
Vertices = new CVertexBuffer<T>();
Indices = new SIndexBuffer();
}
~CMeshBuffer()
{
Vertices->drop();
Indices->drop();
}
//! Get material of this meshbuffer
@ -77,28 +80,34 @@ public:
/** \return Index type of this buffer. */
video::E_INDEX_TYPE getIndexType() const override
{
return video::EIT_16BIT;
return Indices->getType();
}
//! Get pointer to indices
/** \return Pointer to indices. */
const u16 *getIndices() const override
{
return Indices.data();
return static_cast<const u16*>(Indices->getData());
}
//! Get pointer to indices
/** \return Pointer to indices. */
u16 *getIndices() override
{
return Indices.data();
return static_cast<u16*>(Indices->getData());
}
//! Get number of indices
/** \return Number of indices. */
u32 getIndexCount() const override
{
return static_cast<u32>(Indices.size());
return Indices->getCount();
}
// TEMPORARY helper for direct buffer acess
inline auto &IndexBuffer()
{
return Indices->Data;
}
//! Get the axis aligned bounding box
@ -186,10 +195,10 @@ public:
for (u32 i = vertexCount; i < getVertexCount(); i++)
BoundingBox.addInternalPoint(Vertices->getPosition(i));
Indices.insert(Indices.end(), indices, indices + numIndices);
Indices->Data.insert(Indices->Data.end(), indices, indices + numIndices);
if (vertexCount != 0) {
for (u32 i = indexCount; i < getIndexCount(); i++)
Indices[i] += vertexCount;
Indices->Data[i] += vertexCount;
}
}
@ -202,7 +211,7 @@ public:
//! get the current hardware mapping hint
E_HARDWARE_MAPPING getHardwareMappingHint_Index() const override
{
return MappingHint_Index;
return Indices->getHardwareMappingHint();
}
//! set the hardware mapping hint, for driver
@ -211,7 +220,7 @@ public:
if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_VERTEX)
Vertices->setHardwareMappingHint(NewMappingHint);
if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_INDEX)
MappingHint_Index = NewMappingHint;
Indices->setHardwareMappingHint(NewMappingHint);
}
//! Describe what kind of primitive geometry is used by the meshbuffer
@ -232,7 +241,7 @@ public:
if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_VERTEX)
Vertices->setDirty();
if (Buffer == EBT_VERTEX_AND_INDEX || Buffer == EBT_INDEX)
++ChangedID_Index;
Indices->setDirty();
}
//! Get the currently used ID for identification of changes.
@ -241,7 +250,7 @@ public:
//! Get the currently used ID for identification of changes.
/** This shouldn't be used for anything outside the VideoDriver. */
u32 getChangedID_Index() const override { return ChangedID_Index; }
u32 getChangedID_Index() const override { return Indices->getChangedID(); }
void setHWBuffer(void *ptr) const override
{
@ -253,17 +262,14 @@ public:
return HWBuffer;
}
u32 ChangedID_Index;
E_HARDWARE_MAPPING MappingHint_Index;
mutable void *HWBuffer;
//! Material for this meshbuffer.
video::SMaterial Material;
//! Vertex buffer
CVertexBuffer<T> *Vertices;
//! Indices into the vertices of this buffer.
std::vector<u16> Indices;
//! Index buffer
SIndexBuffer *Indices;
//! Bounding box of this meshbuffer.
core::aabbox3d<f32> BoundingBox;
//! Primitive type used for rendering (triangles, lines, ...)

View File

@ -12,40 +12,33 @@
namespace irr
{
namespace video
{
}
namespace scene
{
class IIndexBuffer : public virtual IReferenceCounted
{
public:
//! Get type of index data which is stored in this meshbuffer.
/** \return Index type of this buffer. */
virtual video::E_INDEX_TYPE getType() const = 0;
//! Get access to indices.
/** \return Pointer to indices array. */
virtual const void *getData() const = 0;
//! Get access to indices.
/** \return Pointer to indices array. */
virtual void *getData() = 0;
virtual video::E_INDEX_TYPE getType() const = 0;
virtual void setType(video::E_INDEX_TYPE IndexType) = 0;
virtual u32 stride() const = 0;
virtual u32 size() const = 0;
virtual void push_back(const u32 &element) = 0;
virtual u32 operator[](u32 index) const = 0;
virtual u32 getLast() = 0;
virtual void setValue(u32 index, u32 value) = 0;
virtual void set_used(u32 usedNow) = 0;
virtual void reallocate(u32 new_size) = 0;
virtual u32 allocated_size() const = 0;
virtual void *pointer() = 0;
//! Get amount of indices in this meshbuffer.
/** \return Number of indices in this buffer. */
virtual u32 getCount() const = 0;
//! get the current hardware mapping hint
virtual E_HARDWARE_MAPPING getHardwareMappingHint() const = 0;
//! set the hardware mapping hint, for driver
virtual void setHardwareMappingHint(E_HARDWARE_MAPPING NewMappingHint) = 0;
virtual void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint) = 0;
//! flags the meshbuffer as changed, reloads hardware buffers
virtual void setDirty() = 0;
@ -53,6 +46,10 @@ public:
//! Get the currently used ID for identification of changes.
/** This shouldn't be used for anything outside the VideoDriver. */
virtual u32 getChangedID() const = 0;
//! Used by the VideoDriver to remember the buffer link.
virtual void setHWBuffer(void *ptr) const = 0;
virtual void *getHWBuffer() const = 0;
};
} // end namespace scene

View File

@ -27,16 +27,17 @@ CBillboardSceneNode::CBillboardSceneNode(ISceneNode *parent, ISceneManager *mgr,
setSize(size);
auto &Vertices = Buffer->Vertices->Data;
auto &Indices = Buffer->Indices->Data;
Vertices.resize(4);
Buffer->Indices.resize(6);
Indices.resize(6);
Buffer->Indices[0] = 0;
Buffer->Indices[1] = 2;
Buffer->Indices[2] = 1;
Buffer->Indices[3] = 0;
Buffer->Indices[4] = 3;
Buffer->Indices[5] = 2;
Indices[0] = 0;
Indices[1] = 2;
Indices[2] = 1;
Indices[3] = 0;
Indices[4] = 3;
Indices[5] = 2;
Vertices[0].TCoords.set(1.0f, 1.0f);
Vertices[0].Color = colorBottom;

View File

@ -135,7 +135,7 @@ SMesh *CMeshManipulator::createMeshCopy(scene::IMesh *mesh) const
auto *vt = static_cast<const video::S3DVertex*>(mb->getVertices());
buffer->VertexBuffer().insert(buffer->VertexBuffer().end(), vt, vt + mb->getVertexCount());
auto *indices = mb->getIndices();
buffer->Indices.insert(buffer->Indices.end(), indices, indices + mb->getIndexCount());
buffer->IndexBuffer().insert(buffer->IndexBuffer().end(), indices, indices + mb->getIndexCount());
clone->addMeshBuffer(buffer);
buffer->drop();
} break;
@ -145,7 +145,7 @@ SMesh *CMeshManipulator::createMeshCopy(scene::IMesh *mesh) const
auto *vt = static_cast<const video::S3DVertex2TCoords*>(mb->getVertices());
buffer->VertexBuffer().insert(buffer->VertexBuffer().end(), vt, vt + mb->getVertexCount());
auto *indices = mb->getIndices();
buffer->Indices.insert(buffer->Indices.end(), indices, indices + mb->getIndexCount());
buffer->IndexBuffer().insert(buffer->IndexBuffer().end(), indices, indices + mb->getIndexCount());
clone->addMeshBuffer(buffer);
buffer->drop();
} break;
@ -155,7 +155,7 @@ SMesh *CMeshManipulator::createMeshCopy(scene::IMesh *mesh) const
auto *vt = static_cast<const video::S3DVertexTangents*>(mb->getVertices());
buffer->VertexBuffer().insert(buffer->VertexBuffer().end(), vt, vt + mb->getVertexCount());
auto *indices = mb->getIndices();
buffer->Indices.insert(buffer->Indices.end(), indices, indices + mb->getIndexCount());
buffer->IndexBuffer().insert(buffer->IndexBuffer().end(), indices, indices + mb->getIndexCount());
clone->addMeshBuffer(buffer);
buffer->drop();
} break;

View File

@ -247,15 +247,16 @@ IAnimatedMesh *COBJMeshFileLoader::createMesh(io::IReadFile *file)
}
// triangulate the face
auto &Indices = currMtl->Meshbuffer->IndexBuffer();
const int c = faceCorners[0];
for (u32 i = 1; i < faceCorners.size() - 1; ++i) {
// Add a triangle
const int a = faceCorners[i + 1];
const int b = faceCorners[i];
if (a != b && a != c && b != c) { // ignore degenerated faces. We can get them when we merge vertices above in the VertMap.
currMtl->Meshbuffer->Indices.push_back(a);
currMtl->Meshbuffer->Indices.push_back(b);
currMtl->Meshbuffer->Indices.push_back(c);
Indices.push_back(a);
Indices.push_back(b);
Indices.push_back(c);
} else {
++degeneratedFaces;
}

View File

@ -1358,11 +1358,8 @@ video::SMaterial &ClientMap::DrawDescriptor::getMaterial()
u32 ClientMap::DrawDescriptor::draw(video::IVideoDriver* driver)
{
if (m_use_partial_buffer) {
m_partial_buffer->beforeDraw();
driver->drawMeshBuffer(m_partial_buffer->getBuffer());
auto count = m_partial_buffer->getBuffer()->getVertexCount();
m_partial_buffer->afterDraw();
return count;
m_partial_buffer->draw(driver);
return m_partial_buffer->getBuffer()->getVertexCount();
} else {
driver->drawMeshBuffer(m_buffer);
return m_buffer->getVertexCount();

View File

@ -179,6 +179,7 @@ void Clouds::updateMesh()
auto *mb = m_meshbuffer.get();
auto &vertices = mb->Vertices->Data;
auto &indices = mb->Indices->Data;
{
const u32 vertex_count = num_faces_to_draw * 16 * m_cloud_radius_i * m_cloud_radius_i;
const u32 quad_count = vertex_count / 4;
@ -186,7 +187,7 @@ void Clouds::updateMesh()
// reserve memory
vertices.reserve(vertex_count);
mb->Indices.reserve(index_count);
indices.reserve(index_count);
}
#define GETINDEX(x, z, radius) (((z)+(radius))*(radius)*2 + (x)+(radius))
@ -323,18 +324,18 @@ void Clouds::updateMesh()
const u32 index_count = quad_count * 6;
// rewrite index array as needed
if (mb->getIndexCount() > index_count) {
mb->Indices.resize(index_count);
indices.resize(index_count);
mb->setDirty(scene::EBT_INDEX);
} else if (mb->getIndexCount() < index_count) {
const u32 start = mb->getIndexCount() / 6;
assert(start * 6 == mb->getIndexCount());
for (u32 k = start; k < quad_count; k++) {
mb->Indices.push_back(4 * k + 0);
mb->Indices.push_back(4 * k + 1);
mb->Indices.push_back(4 * k + 2);
mb->Indices.push_back(4 * k + 2);
mb->Indices.push_back(4 * k + 3);
mb->Indices.push_back(4 * k + 0);
indices.push_back(4 * k + 0);
indices.push_back(4 * k + 1);
indices.push_back(4 * k + 2);
indices.push_back(4 * k + 2);
indices.push_back(4 * k + 3);
indices.push_back(4 * k + 0);
}
mb->setDirty(scene::EBT_INDEX);
}

View File

@ -135,8 +135,9 @@ Hud::Hud(Client *client, LocalPlayer *player,
// Prepare mesh for compass drawing
auto &b = m_rotation_mesh_buffer;
auto &vertices = b.Vertices->Data;
auto &indices = b.Indices->Data;
vertices.resize(4);
b.Indices.resize(6);
indices.resize(6);
video::SColor white(255, 255, 255, 255);
v3f normal(0.f, 0.f, 1.f);
@ -146,12 +147,12 @@ Hud::Hud(Client *client, LocalPlayer *player,
vertices[2] = video::S3DVertex(v3f( 1.f, 1.f, 0.f), normal, white, v2f(1.f, 0.f));
vertices[3] = video::S3DVertex(v3f( 1.f, -1.f, 0.f), normal, white, v2f(1.f, 1.f));
b.Indices[0] = 0;
b.Indices[1] = 1;
b.Indices[2] = 2;
b.Indices[3] = 2;
b.Indices[4] = 3;
b.Indices[5] = 0;
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 2;
indices[4] = 3;
indices[5] = 0;
b.getMaterial().Lighting = false;
b.getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;

View File

@ -592,17 +592,14 @@ void MapBlockBspTree::traverse(s32 node, v3f viewpoint, std::vector<s32> &output
PartialMeshBuffer
*/
void PartialMeshBuffer::beforeDraw() const
void PartialMeshBuffer::draw(video::IVideoDriver *driver) const
{
// Patch the indexes in the mesh buffer before draw
m_buffer->Indices = std::move(m_vertex_indexes);
m_buffer->setDirty(scene::EBT_INDEX);
}
void PartialMeshBuffer::afterDraw() const
{
// Take the data back
m_vertex_indexes = std::move(m_buffer->Indices);
// Swap out the index buffer before drawing the mesh
auto *old = m_buffer->Indices;
m_buffer->Indices = m_indices.get();
m_buffer->setDirty(scene::EBT_INDEX); // TODO remove
driver->drawMeshBuffer(m_buffer);
m_buffer->Indices = old;
}
/*
@ -789,6 +786,7 @@ MapBlockMesh::MapBlockMesh(Client *client, MeshMakeData *data, v3s16 camera_offs
}
// Transparent parts have changing indices
// TODO: remove
for (auto &it : m_transparent_triangles)
it.buffer->setHardwareMappingHint(scene::EHM_STREAM, scene::EBT_INDEX);

View File

@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include "irrlichttypes_extrabloated.h"
#include "irr_ptr.h"
#include "util/numeric.h"
#include "client/tile.h"
#include "voxel.h"
@ -145,25 +146,24 @@ private:
* Attach alternate `Indices` to an existing mesh buffer, to make it possible to use different
* indices with the same vertex buffer.
*
* Irrlicht does not currently support this: `CMeshBuffer` ties together a single vertex buffer
* and a single index buffer. There's no way to share these between mesh buffers.
*
*/
class PartialMeshBuffer
{
public:
PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector<u16> &&vertex_indexes) :
m_buffer(buffer), m_vertex_indexes(std::move(vertex_indexes))
{}
PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector<u16> &&vertex_indices) :
m_buffer(buffer)
{
m_indices.reset(new scene::SIndexBuffer());
m_indices->Data = std::move(vertex_indices);
}
scene::IMeshBuffer *getBuffer() const { return m_buffer; }
const std::vector<u16> &getVertexIndexes() const { return m_vertex_indexes; }
void beforeDraw() const;
void afterDraw() const;
void draw(video::IVideoDriver *driver) const;
private:
scene::SMeshBuffer *m_buffer;
mutable std::vector<u16> m_vertex_indexes;
irr_ptr<scene::SIndexBuffer> m_indices;
};
/*

View File

@ -556,8 +556,9 @@ scene::SMeshBuffer *Minimap::getMinimapMeshBuffer()
{
scene::SMeshBuffer *buf = new scene::SMeshBuffer();
auto &vertices = buf->Vertices->Data;
auto &indices = buf->Indices->Data;
vertices.resize(4);
buf->Indices.resize(6);
indices.resize(6);
static const video::SColor c(255, 255, 255, 255);
vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
@ -565,12 +566,12 @@ scene::SMeshBuffer *Minimap::getMinimapMeshBuffer()
vertices[2] = video::S3DVertex( 1, 1, 0, 0, 0, 1, c, 1, 0);
vertices[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c, 1, 1);
buf->Indices[0] = 0;
buf->Indices[1] = 1;
buf->Indices[2] = 2;
buf->Indices[3] = 2;
buf->Indices[4] = 3;
buf->Indices[5] = 0;
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 2;
indices[4] = 3;
indices[5] = 0;
buf->setHardwareMappingHint(scene::EHM_STATIC);
return buf;

View File

@ -831,8 +831,9 @@ void Sky::updateStars()
m_star_params.count = 0x4000;
}
auto &vertices = m_stars->Vertices->Data;
auto &indices = m_stars->Indices->Data;
vertices.reserve(4 * m_star_params.count);
m_stars->Indices.reserve(6 * m_star_params.count);
indices.reserve(6 * m_star_params.count);
video::SColor fallback_color = m_star_params.starcolor; // used on GLES 2 “without shaders”
PcgRandom rgen(m_seed);
@ -859,12 +860,12 @@ void Sky::updateStars()
vertices.push_back(video::S3DVertex(p3, {}, fallback_color, {}));
}
for (u16 i = 0; i < m_star_params.count; i++) {
m_stars->Indices.push_back(i * 4 + 0);
m_stars->Indices.push_back(i * 4 + 1);
m_stars->Indices.push_back(i * 4 + 2);
m_stars->Indices.push_back(i * 4 + 2);
m_stars->Indices.push_back(i * 4 + 3);
m_stars->Indices.push_back(i * 4 + 0);
indices.push_back(i * 4 + 0);
indices.push_back(i * 4 + 1);
indices.push_back(i * 4 + 2);
indices.push_back(i * 4 + 2);
indices.push_back(i * 4 + 3);
indices.push_back(i * 4 + 0);
}
m_stars->setHardwareMappingHint(scene::EHM_STATIC);
}