Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace clientmap's MeshBufListList with a hashmap #14184

Merged
merged 6 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 60 additions & 38 deletions src/client/clientmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,55 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include <queue>

// struct MeshBufListList
void MeshBufListList::clear()
//FIXME: where should I move this to?
// std::hash for integral types, including ptrs, is identity, which is bad for
// aligned ptrs.
sfan5 marked this conversation as resolved.
Show resolved Hide resolved
template <typename T>
struct PtrHash
{
for (auto &list : lists)
list.clear();
}
size_t operator()(const T *p) const noexcept
{
uintptr_t v = reinterpret_cast<uintptr_t>(p);
v = (v >> 4) ^ (v & 0xf);
return std::hash<uintptr_t>{}(v);
}
};

void MeshBufListList::add(scene::IMeshBuffer *buf, v3s16 position, u8 layer)
{
// Append to the correct layer
std::vector<MeshBufList> &list = lists[layer];
const video::SMaterial &m = buf->getMaterial();
for (MeshBufList &l : list) {
// comparing a full material is quite expensive so we don't do it if
// not even first texture is equal
if (l.m.TextureLayers[0].Texture != m.TextureLayers[0].Texture)
continue;
namespace {
// A helper struct
struct MeshBufListMaps
{
struct MaterialHash
{
size_t operator()(const video::SMaterial &m) const noexcept
{
// Only hash first texture. Simple and fast.
return PtrHash<video::ITexture>{}(m.TextureLayers[0].Texture);
}
};

using MeshBufListMap = std::unordered_map<
video::SMaterial,
std::vector<std::pair<v3s16, scene::IMeshBuffer *>>,
MaterialHash>;

std::array<MeshBufListMap, MAX_TILE_LAYERS> maps;

if (l.m == m) {
l.bufs.emplace_back(position, buf);
return;
void clear()
{
for (auto &map : maps)
map.clear();
}
}
MeshBufList l;
l.m = m;
l.bufs.emplace_back(position, buf);
list.emplace_back(l);

void add(scene::IMeshBuffer *buf, v3s16 position, u8 layer)
{
Desour marked this conversation as resolved.
Show resolved Hide resolved
// Append to the correct layer
auto &map = maps[layer];
const video::SMaterial &m = buf->getMaterial();
auto &bufs = map[m]; // default constructs if non-existent
bufs.emplace_back(position, buf);
}
};
}

static void on_settings_changed(const std::string &name, void *data)
Expand Down Expand Up @@ -737,7 +759,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
Draw the selected MapBlocks
*/

MeshBufListList grouped_buffers;
MeshBufListMaps grouped_buffers;
std::vector<DrawDescriptor> draw_order;
video::SMaterial previous_material;

Expand Down Expand Up @@ -793,7 +815,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
}
else {
// otherwise, group buffers across meshes
// using MeshBufListList
// using MeshBufListMaps
for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
scene::IMesh *mesh = block_mesh->getMesh(layer);
assert(mesh);
Expand All @@ -819,11 +841,11 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
}

// Capture draw order for all solid meshes
for (auto &lists : grouped_buffers.lists) {
for (MeshBufList &list : lists) {
for (auto &map : grouped_buffers.maps) {
for (auto &list : map) {
// iterate in reverse to draw closest blocks first
for (auto it = list.bufs.rbegin(); it != list.bufs.rend(); ++it) {
draw_order.emplace_back(it->first, it->second, it != list.bufs.rbegin());
for (auto it = list.second.rbegin(); it != list.second.rend(); ++it) {
draw_order.emplace_back(it->first, it->second, it != list.second.rbegin());
}
}
}
Expand Down Expand Up @@ -1103,7 +1125,7 @@ void ClientMap::renderMapShadows(video::IVideoDriver *driver,
u32 drawcall_count = 0;
u32 vertex_count = 0;

MeshBufListList grouped_buffers;
MeshBufListMaps grouped_buffers;
std::vector<DrawDescriptor> draw_order;


Expand Down Expand Up @@ -1144,7 +1166,7 @@ void ClientMap::renderMapShadows(video::IVideoDriver *driver,
}
else {
// otherwise, group buffers across meshes
// using MeshBufListList
// using MeshBufListMaps
MapBlockMesh *mapBlockMesh = block->mesh;
assert(mapBlockMesh);

Expand All @@ -1167,18 +1189,18 @@ void ClientMap::renderMapShadows(video::IVideoDriver *driver,
}

u32 buffer_count = 0;
for (auto &lists : grouped_buffers.lists)
for (MeshBufList &list : lists)
buffer_count += list.bufs.size();
for (auto &map : grouped_buffers.maps)
for (auto &list : map)
buffer_count += list.second.size();

draw_order.reserve(draw_order.size() + buffer_count);

// Capture draw order for all solid meshes
for (auto &lists : grouped_buffers.lists) {
for (MeshBufList &list : lists) {
for (auto &map : grouped_buffers.maps) {
for (auto &list : map) {
// iterate in reverse to draw closest blocks first
for (auto it = list.bufs.rbegin(); it != list.bufs.rend(); ++it)
draw_order.emplace_back(it->first, it->second, it != list.bufs.rbegin());
for (auto it = list.second.rbegin(); it != list.second.rend(); ++it)
draw_order.emplace_back(it->first, it->second, it != list.second.rbegin());
}
}

Expand Down
19 changes: 0 additions & 19 deletions src/client/clientmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,6 @@ struct MapDrawControl
bool show_wireframe = false;
};

struct MeshBufList
{
video::SMaterial m;
std::vector<std::pair<v3s16,scene::IMeshBuffer*>> bufs;
};

struct MeshBufListList
Desour marked this conversation as resolved.
Show resolved Hide resolved
{
/*!
* Stores the mesh buffers of the world.
* The array index is the material's layer.
* The vector part groups vertices by material.
*/
std::vector<MeshBufList> lists[MAX_TILE_LAYERS];

void clear();
void add(scene::IMeshBuffer *buf, v3s16 position, u8 layer);
};

class Client;
class ITextureSource;
class PartialMeshBuffer;
Expand Down