remove unnecessary shared_ptrs from ChunksRenderer and ChunkMesh
This commit is contained in:
@@ -666,7 +666,7 @@ ChunkMesh BlocksRenderer::render(const Chunk* chunk, const Chunks* chunks) {
|
|||||||
|
|
||||||
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;
|
||||||
return ChunkMesh{std::make_shared<Mesh>(
|
return ChunkMesh{std::make_unique<Mesh>(
|
||||||
vertexBuffer.get(), vcount, indexBuffer.get(), indexSize, attrs
|
vertexBuffer.get(), vcount, indexBuffer.get(), indexSize, attrs
|
||||||
), std::move(sortingMesh)};
|
), std::move(sortingMesh)};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ ChunksRenderer::ChunksRenderer(
|
|||||||
if (!result.cancelled) {
|
if (!result.cancelled) {
|
||||||
auto meshData = std::move(result.meshData);
|
auto meshData = std::move(result.meshData);
|
||||||
meshes[result.key] = ChunkMesh {
|
meshes[result.key] = ChunkMesh {
|
||||||
std::make_shared<Mesh>(meshData.mesh),
|
std::make_unique<Mesh>(meshData.mesh),
|
||||||
std::move(meshData.sortingMesh)
|
std::move(meshData.sortingMesh)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -90,7 +90,7 @@ ChunksRenderer::ChunksRenderer(
|
|||||||
ChunksRenderer::~ChunksRenderer() {
|
ChunksRenderer::~ChunksRenderer() {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Mesh> ChunksRenderer::render(
|
const Mesh* ChunksRenderer::render(
|
||||||
const std::shared_ptr<Chunk>& chunk, bool important
|
const std::shared_ptr<Chunk>& chunk, bool important
|
||||||
) {
|
) {
|
||||||
chunk->flags.modified = false;
|
chunk->flags.modified = false;
|
||||||
@@ -99,7 +99,7 @@ std::shared_ptr<Mesh> ChunksRenderer::render(
|
|||||||
meshes[glm::ivec2(chunk->x, chunk->z)] = ChunkMesh {
|
meshes[glm::ivec2(chunk->x, chunk->z)] = ChunkMesh {
|
||||||
std::move(mesh.mesh), std::move(mesh.sortingMeshData)
|
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.get();
|
||||||
}
|
}
|
||||||
glm::ivec2 key(chunk->x, chunk->z);
|
glm::ivec2 key(chunk->x, chunk->z);
|
||||||
if (inwork.find(key) != inwork.end()) {
|
if (inwork.find(key) != inwork.end()) {
|
||||||
@@ -123,7 +123,7 @@ void ChunksRenderer::clear() {
|
|||||||
threadPool.clearQueue();
|
threadPool.clearQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Mesh> ChunksRenderer::getOrRender(
|
const Mesh* ChunksRenderer::getOrRender(
|
||||||
const std::shared_ptr<Chunk>& chunk, bool important
|
const std::shared_ptr<Chunk>& chunk, bool important
|
||||||
) {
|
) {
|
||||||
auto found = meshes.find(glm::ivec2(chunk->x, chunk->z));
|
auto found = meshes.find(glm::ivec2(chunk->x, chunk->z));
|
||||||
@@ -133,7 +133,7 @@ std::shared_ptr<Mesh> ChunksRenderer::getOrRender(
|
|||||||
if (chunk->flags.modified) {
|
if (chunk->flags.modified) {
|
||||||
render(chunk, important);
|
render(chunk, important);
|
||||||
}
|
}
|
||||||
return found->second.mesh;
|
return found->second.mesh.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChunksRenderer::update() {
|
void ChunksRenderer::update() {
|
||||||
@@ -225,16 +225,12 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) {
|
|||||||
static int frameid = 0;
|
static int frameid = 0;
|
||||||
frameid++;
|
frameid++;
|
||||||
|
|
||||||
|
bool culling = settings.graphics.frustumCulling.get();
|
||||||
|
const auto& chunks = level.chunks->getChunks();
|
||||||
|
const auto& cameraPos = camera.position;
|
||||||
const auto& atlas = assets.require<Atlas>("blocks");
|
const auto& atlas = assets.require<Atlas>("blocks");
|
||||||
|
|
||||||
atlas.getTexture()->bind();
|
atlas.getTexture()->bind();
|
||||||
|
|
||||||
const auto& chunks = level.chunks->getChunks();
|
|
||||||
|
|
||||||
auto pposition = camera.position;
|
|
||||||
|
|
||||||
size_t size = 0;
|
|
||||||
bool culling = settings.graphics.frustumCulling.get();
|
|
||||||
shader.uniformMatrix("u_model", glm::mat4(1.0f));
|
shader.uniformMatrix("u_model", glm::mat4(1.0f));
|
||||||
|
|
||||||
for (const auto& index : indices) {
|
for (const auto& index : indices) {
|
||||||
@@ -259,16 +255,13 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) {
|
|||||||
if (!frustum.isBoxVisible(min, max)) continue;
|
if (!frustum.isBoxVisible(min, max)) continue;
|
||||||
|
|
||||||
auto& chunkEntries = found->second.sortingMeshData.entries;
|
auto& chunkEntries = found->second.sortingMeshData.entries;
|
||||||
|
|
||||||
for (auto& entry : chunkEntries) {
|
|
||||||
entry.distance =
|
|
||||||
static_cast<long long>(glm::distance2(entry.position, pposition));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (chunkEntries.size() == 1) {
|
if (chunkEntries.empty()) {
|
||||||
|
continue;
|
||||||
|
} else if (chunkEntries.size() == 1) {
|
||||||
auto& entry = chunkEntries.at(0);
|
auto& entry = chunkEntries.at(0);
|
||||||
if (found->second.sortedMesh == nullptr) {
|
if (found->second.sortedMesh == nullptr) {
|
||||||
found->second.sortedMesh = std::make_shared<Mesh>(
|
found->second.sortedMesh = std::make_unique<Mesh>(
|
||||||
entry.vertexData.data(),
|
entry.vertexData.data(),
|
||||||
entry.vertexData.size() / VERTEX_SIZE,
|
entry.vertexData.size() / VERTEX_SIZE,
|
||||||
ATTRS
|
ATTRS
|
||||||
@@ -277,11 +270,10 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) {
|
|||||||
found->second.sortedMesh->draw();
|
found->second.sortedMesh->draw();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
for (auto& entry : chunkEntries) {
|
||||||
if (chunkEntries.empty()) {
|
entry.distance =
|
||||||
continue;
|
static_cast<long long>(glm::distance2(entry.position, cameraPos));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (found->second.sortedMesh == nullptr ||
|
if (found->second.sortedMesh == nullptr ||
|
||||||
(frameid + chunk->x) % sortInterval == 0) {
|
(frameid + chunk->x) % sortInterval == 0) {
|
||||||
std::sort(chunkEntries.begin(), chunkEntries.end());
|
std::sort(chunkEntries.begin(), chunkEntries.end());
|
||||||
@@ -302,7 +294,7 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) {
|
|||||||
);
|
);
|
||||||
offset += entry.vertexData.size();
|
offset += entry.vertexData.size();
|
||||||
}
|
}
|
||||||
found->second.sortedMesh = std::make_shared<Mesh>(
|
found->second.sortedMesh = std::make_unique<Mesh>(
|
||||||
buffer.data(), size / VERTEX_SIZE, ATTRS
|
buffer.data(), size / VERTEX_SIZE, ATTRS
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,13 +65,13 @@ public:
|
|||||||
);
|
);
|
||||||
virtual ~ChunksRenderer();
|
virtual ~ChunksRenderer();
|
||||||
|
|
||||||
std::shared_ptr<Mesh> render(
|
const Mesh* render(
|
||||||
const std::shared_ptr<Chunk>& chunk, bool important
|
const std::shared_ptr<Chunk>& chunk, bool important
|
||||||
);
|
);
|
||||||
void unload(const Chunk* chunk);
|
void unload(const Chunk* chunk);
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
std::shared_ptr<Mesh> getOrRender(
|
const Mesh* getOrRender(
|
||||||
const std::shared_ptr<Chunk>& chunk, bool important
|
const std::shared_ptr<Chunk>& chunk, bool important
|
||||||
);
|
);
|
||||||
void drawChunks(const Camera& camera, Shader& shader);
|
void drawChunks(const Camera& camera, Shader& shader);
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ struct ChunkMeshData {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct ChunkMesh {
|
struct ChunkMesh {
|
||||||
std::shared_ptr<Mesh> mesh;
|
std::unique_ptr<Mesh> mesh;
|
||||||
SortingMeshData sortingMeshData;
|
SortingMeshData sortingMeshData;
|
||||||
std::shared_ptr<Mesh> sortedMesh = nullptr;
|
std::unique_ptr<Mesh> sortedMesh = nullptr;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user