other optimizations

This commit is contained in:
MihailRis 2024-05-17 20:00:24 +03:00
parent 8c918ff136
commit b399679c31
6 changed files with 21 additions and 21 deletions

View File

@ -458,14 +458,13 @@ void BlocksRenderer::build(const Chunk* chunk, const ChunksStorage* chunks) {
render(voxels);
}
Mesh* BlocksRenderer::createMesh() {
std::shared_ptr<Mesh> BlocksRenderer::createMesh() {
const vattr attrs[]{ {3}, {2}, {1}, {0} };
size_t vcount = vertexOffset / BlocksRenderer::VERTEX_SIZE;
Mesh* mesh = new Mesh(vertexBuffer, vcount, indexBuffer, indexSize, attrs);
return mesh;
return std::make_shared<Mesh>(vertexBuffer, vcount, indexBuffer, indexSize, attrs);
}
Mesh* BlocksRenderer::render(const Chunk* chunk, const ChunksStorage* chunks) {
std::shared_ptr<Mesh> BlocksRenderer::render(const Chunk* chunk, const ChunksStorage* chunks) {
build(chunk, chunks);
return createMesh();
}

View File

@ -94,8 +94,8 @@ public:
virtual ~BlocksRenderer();
void build(const Chunk* chunk, const ChunksStorage* chunks);
Mesh* render(const Chunk* chunk, const ChunksStorage* chunks);
Mesh* createMesh();
std::shared_ptr<Mesh> render(const Chunk* chunk, const ChunksStorage* chunks);
std::shared_ptr<Mesh> createMesh();
VoxelsVolume* getVoxelsBuffer() const;
};

View File

@ -41,7 +41,7 @@ ChunksRenderer::ChunksRenderer(
"chunks-render-pool",
[=](){return std::make_shared<RendererWorker>(level, cache, settings);},
[=](RendererResult& mesh){
meshes[mesh.key].reset(mesh.renderer->createMesh());
meshes[mesh.key] = mesh.renderer->createMesh();
inwork.erase(mesh.key);
})
{
@ -59,7 +59,7 @@ std::shared_ptr<Mesh> ChunksRenderer::render(std::shared_ptr<Chunk> chunk, bool
chunk->setModified(false);
if (important) {
std::shared_ptr<Mesh> mesh (renderer->render(chunk.get(), level->chunksStorage.get()));
auto mesh = renderer->render(chunk.get(), level->chunksStorage.get());
meshes[glm::ivec2(chunk->x, chunk->z)] = mesh;
return mesh;
}

View File

@ -121,8 +121,8 @@ void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) {
continue;
indices.push_back(i);
}
int px = camera->position.x / (float)CHUNK_W - 0.5f;
int pz = camera->position.z / (float)CHUNK_D - 0.5f;
float px = camera->position.x / (float)CHUNK_W - 0.5f;
float pz = camera->position.z / (float)CHUNK_D - 0.5f;
std::sort(indices.begin(), indices.end(), [chunks, px, pz](auto i, auto j) {
const auto& a = chunks->chunks[i];
const auto& b = chunks->chunks[j];

View File

@ -39,7 +39,7 @@ voxel* Chunks::get(int32_t x, int32_t y, int32_t z) {
int cz = floordiv(z, CHUNK_D);
if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d))
return nullptr;
std::shared_ptr<Chunk> chunk = chunks[cz * w + cx];
auto& chunk = chunks[cz * w + cx]; // not thread safe
if (chunk == nullptr)
return nullptr;
int lx = x - cx * CHUNK_W;
@ -103,7 +103,7 @@ ubyte Chunks::getLight(int32_t x, int32_t y, int32_t z, int channel){
int cz = floordiv(z, CHUNK_D);
if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d))
return 0;
auto chunk = chunks[(cy * d + cz) * w + cx];
const auto& chunk = chunks[(cy * d + cz) * w + cx];
if (chunk == nullptr)
return 0;
int lx = x - cx * CHUNK_W;
@ -120,7 +120,7 @@ light_t Chunks::getLight(int32_t x, int32_t y, int32_t z){
int cz = floordiv(z, CHUNK_D);
if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d))
return 0;
auto chunk = chunks[(cy * d + cz) * w + cx];
const auto& chunk = chunks[(cy * d + cz) * w + cx];
if (chunk == nullptr)
return 0;
int lx = x - cx * CHUNK_W;

View File

@ -1,8 +1,5 @@
#include "ChunksStorage.hpp"
#include <assert.h>
#include <iostream>
#include "VoxelsVolume.hpp"
#include "Chunk.hpp"
#include "Block.hpp"
@ -14,6 +11,9 @@
#include "../lighting/Lightmap.hpp"
#include "../items/Inventories.hpp"
#include "../typedefs.hpp"
#include "../debug/Logger.hpp"
static debug::Logger logger("chunks-storage");
ChunksStorage::ChunksStorage(Level* level) : level(level) {
}
@ -41,10 +41,11 @@ static void verifyLoadedChunk(ContentIndices* indices, Chunk* chunk) {
for (size_t i = 0; i < CHUNK_VOL; i++) {
blockid_t id = chunk->voxels[i].id;
if (indices->getBlockDef(id) == nullptr) {
std::cout << "corruped block detected at " << i << " of chunk ";
std::cout << chunk->x << "x" << chunk->z;
std::cout << " -> " << (int)id << std::endl;
chunk->voxels[i].id = 11;
auto logline = logger.error();
logline << "corruped block detected at " << i << " of chunk ";
logline << chunk->x << "x" << chunk->z;
logline << " -> " << id;
chunk->voxels[i].id = BLOCK_AIR;
}
}
}
@ -101,7 +102,7 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const {
// cw*ch chunks will be scanned
for (int cz = scz; cz < scz + ch; cz++) {
for (int cx = scx; cx < scx + cw; cx++) {
auto found = chunksMap.find(glm::ivec2(cx, cz));
const auto& found = chunksMap.find(glm::ivec2(cx, cz));
if (found == chunksMap.end()) {
// no chunk loaded -> filling with BLOCK_VOID
for (int ly = y; ly < y + h; ly++) {