mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
fix block wraps high draw call count
This commit is contained in:
parent
5e05b44c26
commit
21951770ea
4 changed files with 44 additions and 34 deletions
|
|
@ -8,18 +8,16 @@
|
|||
#include "graphics/core/DrawContext.hpp"
|
||||
#include "graphics/render/MainBatch.hpp"
|
||||
#include "lighting/Lightmap.hpp"
|
||||
#include "objects/Player.hpp"
|
||||
#include "voxels/Block.hpp"
|
||||
#include "voxels/Chunks.hpp"
|
||||
#include "world/Level.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
BlockWrapsRenderer::BlockWrapsRenderer(
|
||||
const Assets& assets, const Level& level, const Chunks& chunks
|
||||
const Assets& assets, const Content& content, const Chunks& chunks
|
||||
)
|
||||
: assets(assets),
|
||||
level(level),
|
||||
content(content),
|
||||
chunks(chunks),
|
||||
batch(std::make_unique<MainBatch>(1024)) {
|
||||
}
|
||||
|
|
@ -43,16 +41,14 @@ void BlockWrapsRenderer::draw(BlockWrapper& wrapper, const Texture* texture) {
|
|||
}
|
||||
|
||||
const voxel* vox = chunks.get(wrapper.position);
|
||||
if (vox == nullptr || vox->id == BLOCK_VOID) {
|
||||
if (vox == nullptr || vox->id == BLOCK_AIR) {
|
||||
return;
|
||||
}
|
||||
// one frame can be invalid due to texture change but ok
|
||||
const auto& def =
|
||||
level.content.getIndices()->blocks.require(vox->id);
|
||||
const auto& def = content.getIndices()->blocks.require(vox->id);
|
||||
|
||||
if (wrapper.modelType != def.getModel(vox->state.userbits).type) {
|
||||
wrapper.dirtySides = 0xFF;
|
||||
refreshWrapper(wrapper);
|
||||
}
|
||||
glm::vec4 light(1, 1, 1, 0);
|
||||
if (wrapper.emission < 1.0f) {
|
||||
|
|
@ -95,6 +91,8 @@ void BlockWrapsRenderer::draw(BlockWrapper& wrapper, const Texture* texture) {
|
|||
}
|
||||
|
||||
void BlockWrapsRenderer::refreshWrapper(BlockWrapper& wrapper) {
|
||||
clearOrder(&wrapper);
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if ((wrapper.cullingBits & (1 << i)) == 0) {
|
||||
continue;
|
||||
|
|
@ -104,15 +102,19 @@ void BlockWrapsRenderer::refreshWrapper(BlockWrapper& wrapper) {
|
|||
wrapper.texRegions[i] = texRegion;
|
||||
wrapper.uvRegions[i] = texRegion.region;
|
||||
|
||||
renderOrder.insert({texRegion.texture, &wrapper});
|
||||
auto& wrappers = renderOrder[texRegion.texture];
|
||||
if (std::find(wrappers.begin(), wrappers.end(), &wrapper) ==
|
||||
wrappers.end()) {
|
||||
wrappers.emplace_back(&wrapper);
|
||||
}
|
||||
}
|
||||
wrapper.dirtySides = 0x0;
|
||||
|
||||
const voxel* vox = chunks.get(wrapper.position);
|
||||
if (vox == nullptr || vox->id == BLOCK_VOID) {
|
||||
if (vox == nullptr || vox->id == BLOCK_AIR) {
|
||||
return;
|
||||
}
|
||||
const auto& def = level.content.getIndices()->blocks.require(vox->id);
|
||||
const auto& def = content.getIndices()->blocks.require(vox->id);
|
||||
wrapper.modelType = def.getModel(vox->state.userbits).type;
|
||||
switch (wrapper.modelType) {
|
||||
case BlockModelType::AABB: {
|
||||
|
|
@ -133,7 +135,7 @@ void BlockWrapsRenderer::refreshWrapper(BlockWrapper& wrapper) {
|
|||
}
|
||||
}
|
||||
|
||||
void BlockWrapsRenderer::draw(const DrawContext& pctx, const Player& player) {
|
||||
void BlockWrapsRenderer::draw(const DrawContext& pctx) {
|
||||
auto ctx = pctx.sub();
|
||||
|
||||
auto& shader = assets.require<Shader>("entity");
|
||||
|
|
@ -146,9 +148,11 @@ void BlockWrapsRenderer::draw(const DrawContext& pctx, const Player& player) {
|
|||
}
|
||||
}
|
||||
|
||||
for (const auto& [texture, wrapper] : renderOrder) {
|
||||
for (const auto& [texture, wrappersArray] : renderOrder) {
|
||||
batch->setTexture(texture);
|
||||
draw(*wrapper, texture);
|
||||
for (auto wrapper : wrappersArray) {
|
||||
draw(*wrapper, texture);
|
||||
}
|
||||
}
|
||||
batch->flush();
|
||||
}
|
||||
|
|
@ -181,15 +185,21 @@ void BlockWrapsRenderer::remove(u64id_t id) {
|
|||
if (found == wrappers.end()) {
|
||||
return;
|
||||
}
|
||||
auto wrapper = std::move(found->second);
|
||||
clearOrder(found->second.get());
|
||||
wrappers.erase(id);
|
||||
}
|
||||
|
||||
void BlockWrapsRenderer::clearOrder(const BlockWrapper* const wrapper) {
|
||||
auto it = renderOrder.begin();
|
||||
while (it != renderOrder.end()) {
|
||||
if (it->second == wrapper.get()) {
|
||||
it->second.erase(
|
||||
std::remove(it->second.begin(), it->second.end(), wrapper),
|
||||
it->second.end()
|
||||
);
|
||||
if (it->second.empty()) {
|
||||
it = renderOrder.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue