other optimizations
This commit is contained in:
@@ -458,14 +458,13 @@ void BlocksRenderer::build(const Chunk* chunk, const ChunksStorage* chunks) {
|
|||||||
render(voxels);
|
render(voxels);
|
||||||
}
|
}
|
||||||
|
|
||||||
Mesh* BlocksRenderer::createMesh() {
|
std::shared_ptr<Mesh> BlocksRenderer::createMesh() {
|
||||||
const vattr attrs[]{ {3}, {2}, {1}, {0} };
|
const vattr attrs[]{ {3}, {2}, {1}, {0} };
|
||||||
size_t vcount = vertexOffset / BlocksRenderer::VERTEX_SIZE;
|
size_t vcount = vertexOffset / BlocksRenderer::VERTEX_SIZE;
|
||||||
Mesh* mesh = new Mesh(vertexBuffer, vcount, indexBuffer, indexSize, attrs);
|
return std::make_shared<Mesh>(vertexBuffer, vcount, indexBuffer, indexSize, attrs);
|
||||||
return mesh;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Mesh* BlocksRenderer::render(const Chunk* chunk, const ChunksStorage* chunks) {
|
std::shared_ptr<Mesh> BlocksRenderer::render(const Chunk* chunk, const ChunksStorage* chunks) {
|
||||||
build(chunk, chunks);
|
build(chunk, chunks);
|
||||||
return createMesh();
|
return createMesh();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,8 +94,8 @@ public:
|
|||||||
virtual ~BlocksRenderer();
|
virtual ~BlocksRenderer();
|
||||||
|
|
||||||
void build(const Chunk* chunk, const ChunksStorage* chunks);
|
void build(const Chunk* chunk, const ChunksStorage* chunks);
|
||||||
Mesh* render(const Chunk* chunk, const ChunksStorage* chunks);
|
std::shared_ptr<Mesh> render(const Chunk* chunk, const ChunksStorage* chunks);
|
||||||
Mesh* createMesh();
|
std::shared_ptr<Mesh> createMesh();
|
||||||
VoxelsVolume* getVoxelsBuffer() const;
|
VoxelsVolume* getVoxelsBuffer() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ ChunksRenderer::ChunksRenderer(
|
|||||||
"chunks-render-pool",
|
"chunks-render-pool",
|
||||||
[=](){return std::make_shared<RendererWorker>(level, cache, settings);},
|
[=](){return std::make_shared<RendererWorker>(level, cache, settings);},
|
||||||
[=](RendererResult& mesh){
|
[=](RendererResult& mesh){
|
||||||
meshes[mesh.key].reset(mesh.renderer->createMesh());
|
meshes[mesh.key] = mesh.renderer->createMesh();
|
||||||
inwork.erase(mesh.key);
|
inwork.erase(mesh.key);
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
@@ -59,7 +59,7 @@ std::shared_ptr<Mesh> ChunksRenderer::render(std::shared_ptr<Chunk> chunk, bool
|
|||||||
chunk->setModified(false);
|
chunk->setModified(false);
|
||||||
|
|
||||||
if (important) {
|
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;
|
meshes[glm::ivec2(chunk->x, chunk->z)] = mesh;
|
||||||
return mesh;
|
return mesh;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -121,8 +121,8 @@ void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) {
|
|||||||
continue;
|
continue;
|
||||||
indices.push_back(i);
|
indices.push_back(i);
|
||||||
}
|
}
|
||||||
int px = camera->position.x / (float)CHUNK_W - 0.5f;
|
float px = camera->position.x / (float)CHUNK_W - 0.5f;
|
||||||
int pz = camera->position.z / (float)CHUNK_D - 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) {
|
std::sort(indices.begin(), indices.end(), [chunks, px, pz](auto i, auto j) {
|
||||||
const auto& a = chunks->chunks[i];
|
const auto& a = chunks->chunks[i];
|
||||||
const auto& b = chunks->chunks[j];
|
const auto& b = chunks->chunks[j];
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ voxel* Chunks::get(int32_t x, int32_t y, int32_t z) {
|
|||||||
int cz = floordiv(z, CHUNK_D);
|
int cz = floordiv(z, CHUNK_D);
|
||||||
if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d))
|
if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
std::shared_ptr<Chunk> chunk = chunks[cz * w + cx];
|
auto& chunk = chunks[cz * w + cx]; // not thread safe
|
||||||
if (chunk == nullptr)
|
if (chunk == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
int lx = x - cx * CHUNK_W;
|
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);
|
int cz = floordiv(z, CHUNK_D);
|
||||||
if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d))
|
if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d))
|
||||||
return 0;
|
return 0;
|
||||||
auto chunk = chunks[(cy * d + cz) * w + cx];
|
const auto& chunk = chunks[(cy * d + cz) * w + cx];
|
||||||
if (chunk == nullptr)
|
if (chunk == nullptr)
|
||||||
return 0;
|
return 0;
|
||||||
int lx = x - cx * CHUNK_W;
|
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);
|
int cz = floordiv(z, CHUNK_D);
|
||||||
if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d))
|
if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d))
|
||||||
return 0;
|
return 0;
|
||||||
auto chunk = chunks[(cy * d + cz) * w + cx];
|
const auto& chunk = chunks[(cy * d + cz) * w + cx];
|
||||||
if (chunk == nullptr)
|
if (chunk == nullptr)
|
||||||
return 0;
|
return 0;
|
||||||
int lx = x - cx * CHUNK_W;
|
int lx = x - cx * CHUNK_W;
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
#include "ChunksStorage.hpp"
|
#include "ChunksStorage.hpp"
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include "VoxelsVolume.hpp"
|
#include "VoxelsVolume.hpp"
|
||||||
#include "Chunk.hpp"
|
#include "Chunk.hpp"
|
||||||
#include "Block.hpp"
|
#include "Block.hpp"
|
||||||
@@ -14,6 +11,9 @@
|
|||||||
#include "../lighting/Lightmap.hpp"
|
#include "../lighting/Lightmap.hpp"
|
||||||
#include "../items/Inventories.hpp"
|
#include "../items/Inventories.hpp"
|
||||||
#include "../typedefs.hpp"
|
#include "../typedefs.hpp"
|
||||||
|
#include "../debug/Logger.hpp"
|
||||||
|
|
||||||
|
static debug::Logger logger("chunks-storage");
|
||||||
|
|
||||||
ChunksStorage::ChunksStorage(Level* level) : level(level) {
|
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++) {
|
for (size_t i = 0; i < CHUNK_VOL; i++) {
|
||||||
blockid_t id = chunk->voxels[i].id;
|
blockid_t id = chunk->voxels[i].id;
|
||||||
if (indices->getBlockDef(id) == nullptr) {
|
if (indices->getBlockDef(id) == nullptr) {
|
||||||
std::cout << "corruped block detected at " << i << " of chunk ";
|
auto logline = logger.error();
|
||||||
std::cout << chunk->x << "x" << chunk->z;
|
logline << "corruped block detected at " << i << " of chunk ";
|
||||||
std::cout << " -> " << (int)id << std::endl;
|
logline << chunk->x << "x" << chunk->z;
|
||||||
chunk->voxels[i].id = 11;
|
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
|
// cw*ch chunks will be scanned
|
||||||
for (int cz = scz; cz < scz + ch; cz++) {
|
for (int cz = scz; cz < scz + ch; cz++) {
|
||||||
for (int cx = scx; cx < scx + cw; cx++) {
|
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()) {
|
if (found == chunksMap.end()) {
|
||||||
// no chunk loaded -> filling with BLOCK_VOID
|
// no chunk loaded -> filling with BLOCK_VOID
|
||||||
for (int ly = y; ly < y + h; ly++) {
|
for (int ly = y; ly < y + h; ly++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user