optimize (part 2)
This commit is contained in:
parent
99f1594645
commit
1ff89491e6
@ -590,7 +590,7 @@ SortingMeshData BlocksRenderer::renderTranslucent(
|
|||||||
|
|
||||||
// additional powerful optimization
|
// additional powerful optimization
|
||||||
auto size = aabb.size();
|
auto size = aabb.size();
|
||||||
if (glm::abs(size.y) < 0.01f && sortingMesh.entries.size() > 1 && false) {
|
if (glm::abs(size.y) < 0.01f && sortingMesh.entries.size() > 1) {
|
||||||
SortingMeshEntry newEntry {
|
SortingMeshEntry newEntry {
|
||||||
sortingMesh.entries[0].position,
|
sortingMesh.entries[0].position,
|
||||||
util::Buffer<float>(totalSize)
|
util::Buffer<float>(totalSize)
|
||||||
|
|||||||
@ -49,6 +49,8 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const vattr ATTRS[]{ {3}, {2}, {1}, {0} };
|
||||||
|
|
||||||
ChunksRenderer::ChunksRenderer(
|
ChunksRenderer::ChunksRenderer(
|
||||||
const Level* level,
|
const Level* level,
|
||||||
const Assets& assets,
|
const Assets& assets,
|
||||||
@ -80,9 +82,8 @@ ChunksRenderer::ChunksRenderer(
|
|||||||
);
|
);
|
||||||
logger.info() << "created " << threadPool.getWorkersCount() << " workers";
|
logger.info() << "created " << threadPool.getWorkersCount() << " workers";
|
||||||
|
|
||||||
const vattr attrs[]{ {3}, {2}, {1}, {0} };
|
|
||||||
float buf[1]{};
|
float buf[1]{};
|
||||||
sortedMesh = std::make_unique<Mesh>(buf, 0, attrs);
|
sortedMesh = std::make_unique<Mesh>(buf, 0, ATTRS);
|
||||||
}
|
}
|
||||||
|
|
||||||
ChunksRenderer::~ChunksRenderer() {
|
ChunksRenderer::~ChunksRenderer() {
|
||||||
@ -93,7 +94,7 @@ std::shared_ptr<Mesh> ChunksRenderer::render(const std::shared_ptr<Chunk>& chunk
|
|||||||
if (important) {
|
if (important) {
|
||||||
auto mesh = renderer->render(chunk.get(), level.chunks.get());
|
auto mesh = renderer->render(chunk.get(), level.chunks.get());
|
||||||
meshes[glm::ivec2(chunk->x, chunk->z)] = ChunkMesh {
|
meshes[glm::ivec2(chunk->x, chunk->z)] = ChunkMesh {
|
||||||
std::move(mesh.mesh), std::move(mesh.sortingMesh)
|
std::move(mesh.mesh), std::move(mesh.sortingMeshData)
|
||||||
};
|
};
|
||||||
return meshes[glm::ivec2(chunk->x, chunk->z)].mesh;
|
return meshes[glm::ivec2(chunk->x, chunk->z)].mesh;
|
||||||
}
|
}
|
||||||
@ -219,14 +220,13 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) {
|
|||||||
|
|
||||||
atlas.getTexture()->bind();
|
atlas.getTexture()->bind();
|
||||||
|
|
||||||
std::vector<const SortingMeshEntry*> entries;
|
|
||||||
|
|
||||||
const auto& chunks = level.chunks->getChunks();
|
const auto& chunks = level.chunks->getChunks();
|
||||||
|
|
||||||
auto pposition = camera.position;
|
auto pposition = camera.position;
|
||||||
|
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
bool culling = settings.graphics.frustumCulling.get();
|
bool culling = settings.graphics.frustumCulling.get();
|
||||||
|
shader.uniformMatrix("u_model", glm::mat4(1.0f));
|
||||||
|
|
||||||
for (const auto& index : indices) {
|
for (const auto& index : indices) {
|
||||||
const auto& chunk = chunks[index.index];
|
const auto& chunk = chunks[index.index];
|
||||||
@ -238,43 +238,50 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 min(chunk->x * CHUNK_W, chunk->bottom, chunk->z * CHUNK_D);
|
glm::vec3 min(
|
||||||
|
chunk->x * CHUNK_W - CHUNK_W, 0, chunk->z * CHUNK_D - CHUNK_D
|
||||||
|
);
|
||||||
glm::vec3 max(
|
glm::vec3 max(
|
||||||
chunk->x * CHUNK_W + CHUNK_W,
|
chunk->x * CHUNK_W + CHUNK_W*2,
|
||||||
chunk->top,
|
CHUNK_H,
|
||||||
chunk->z * CHUNK_D + CHUNK_D
|
chunk->z * CHUNK_D + CHUNK_D*2
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!frustum.isBoxVisible(min, max)) continue;
|
if (!frustum.isBoxVisible(min, max)) continue;
|
||||||
|
|
||||||
auto& chunkEntries = found->second.sortingMesh.entries;
|
auto& chunkEntries = found->second.sortingMeshData.entries;
|
||||||
|
|
||||||
for (auto& entry : chunkEntries) {
|
for (auto& entry : chunkEntries) {
|
||||||
entry.distance = static_cast<long long>(glm::distance2(entry.position, pposition));
|
entry.distance = static_cast<long long>(glm::distance2(entry.position, pposition));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (chunkEntries.size() == 1) {
|
||||||
|
auto& entry = chunkEntries.at(0);
|
||||||
|
if (found->second.planesMesh == nullptr) {
|
||||||
|
found->second.planesMesh = std::make_shared<Mesh>(
|
||||||
|
entry.vertexData.data(), entry.vertexData.size() / 6, ATTRS
|
||||||
|
);
|
||||||
|
}
|
||||||
|
found->second.planesMesh->draw();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
std::sort(chunkEntries.begin(), chunkEntries.end());
|
std::sort(chunkEntries.begin(), chunkEntries.end());
|
||||||
|
size_t size = 0;
|
||||||
for (const auto& entry : chunkEntries) {
|
for (const auto& entry : chunkEntries) {
|
||||||
size += entry.vertexData.size();
|
size += entry.vertexData.size();
|
||||||
entries.push_back(&entry);
|
|
||||||
}
|
}
|
||||||
|
util::Buffer<float> buffer(size);
|
||||||
|
size_t offset = 0;
|
||||||
|
for (const auto& entry : chunkEntries) {
|
||||||
|
std::memcpy(
|
||||||
|
(buffer.data() + offset),
|
||||||
|
entry.vertexData.data(),
|
||||||
|
entry.vertexData.size() * sizeof(float)
|
||||||
|
);
|
||||||
|
offset += entry.vertexData.size();
|
||||||
|
}
|
||||||
|
sortedMesh->reload(buffer.data(), size / 6);
|
||||||
|
sortedMesh->draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
static util::Buffer<float> buffer;
|
|
||||||
|
|
||||||
if (buffer.size() < size) {
|
|
||||||
buffer = util::Buffer<float>(size);
|
|
||||||
}
|
|
||||||
size_t offset = 0;
|
|
||||||
for (const auto& entry : entries) {
|
|
||||||
std::memcpy(
|
|
||||||
(buffer.data() + offset),
|
|
||||||
entry->vertexData.data(),
|
|
||||||
entry->vertexData.size() * sizeof(float)
|
|
||||||
);
|
|
||||||
offset += entry->vertexData.size();
|
|
||||||
}
|
|
||||||
sortedMesh->reload(buffer.data(), size / 6);
|
|
||||||
|
|
||||||
shader.uniformMatrix("u_model", glm::mat4(1.0f));
|
|
||||||
sortedMesh->draw();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,5 +30,6 @@ struct ChunkMeshData {
|
|||||||
|
|
||||||
struct ChunkMesh {
|
struct ChunkMesh {
|
||||||
std::shared_ptr<Mesh> mesh;
|
std::shared_ptr<Mesh> mesh;
|
||||||
SortingMeshData sortingMesh;
|
SortingMeshData sortingMeshData;
|
||||||
|
std::shared_ptr<Mesh> planesMesh = nullptr;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user