Merge pull request #564 from MihailRis/small-optimizations
optimize shadow maps generation a bit
This commit is contained in:
@@ -53,7 +53,7 @@ public:
|
||||
});
|
||||
}
|
||||
|
||||
const T& get() const {
|
||||
[[nodiscard]] const T& get() const {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,9 +9,11 @@
|
||||
#include "graphics/core/Atlas.hpp"
|
||||
#include "maths/UVRegion.hpp"
|
||||
#include "voxels/Block.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
#include "core_defs.hpp"
|
||||
#include "settings.hpp"
|
||||
|
||||
static debug::Logger logger("content-gfx-cache");
|
||||
|
||||
ContentGfxCache::ContentGfxCache(
|
||||
const Content& content,
|
||||
@@ -22,27 +24,29 @@ ContentGfxCache::ContentGfxCache(
|
||||
refresh();
|
||||
}
|
||||
|
||||
static void refresh_variant(
|
||||
const Assets& assets,
|
||||
void ContentGfxCache::refreshVariant(
|
||||
const Block& def,
|
||||
const Variant& variant,
|
||||
uint8_t variantIndex,
|
||||
std::unique_ptr<UVRegion[]>& sideregions,
|
||||
const Atlas& atlas,
|
||||
const GraphicsSettings& settings,
|
||||
std::unordered_map<blockid_t, model::Model>& models
|
||||
const Atlas& atlas
|
||||
) {
|
||||
bool denseRender = settings.denseRender.get();
|
||||
for (uint side = 0; side < 6; side++) {
|
||||
std::string tex = variant.textureFaces[side];
|
||||
if (variant.culling == CullingMode::OPTIONAL &&
|
||||
!settings.denseRender.get() && atlas.has(tex + "_opaque")) {
|
||||
tex = tex + "_opaque";
|
||||
std::string texOpaque = tex + "_opaque";
|
||||
|
||||
if (!atlas.has(tex)) {
|
||||
tex = TEXTURE_NOTFOUND;
|
||||
}
|
||||
if (atlas.has(tex)) {
|
||||
sideregions[(def.rt.id * 6 + side) * MAX_VARIANTS + variantIndex] = atlas.get(tex);
|
||||
} else if (atlas.has(TEXTURE_NOTFOUND)) {
|
||||
sideregions[(def.rt.id * 6 + side) * MAX_VARIANTS + variantIndex] = atlas.get(TEXTURE_NOTFOUND);
|
||||
|
||||
if (!atlas.has(texOpaque)) {
|
||||
texOpaque = tex;
|
||||
} else if (variant.culling == CullingMode::OPTIONAL && !denseRender) {
|
||||
tex = texOpaque;
|
||||
}
|
||||
size_t index = getRegionIndex(def.rt.id, variantIndex, side, false);
|
||||
sideregions[index] = atlas.get(tex);
|
||||
sideregions[index + 1] = atlas.get(texOpaque);
|
||||
}
|
||||
if (variant.model.type == BlockModelType::CUSTOM) {
|
||||
auto model = assets.require<model::Model>(variant.model.name);
|
||||
@@ -63,11 +67,11 @@ static void refresh_variant(
|
||||
}
|
||||
|
||||
void ContentGfxCache::refresh(const Block& def, const Atlas& atlas) {
|
||||
refresh_variant(assets, def, def.defaults, 0, sideregions, atlas, settings, models);
|
||||
refreshVariant(def, def.defaults, 0, atlas);
|
||||
if (def.variants) {
|
||||
const auto& variants = def.variants->variants;
|
||||
for (int i = 1; i < variants.size() - 1; i++) {
|
||||
refresh_variant(assets, def, variants[i], i, sideregions, atlas, settings, models);
|
||||
refreshVariant(def, variants[i], i, atlas);
|
||||
}
|
||||
def.variants->variants.at(0) = def.defaults;
|
||||
}
|
||||
@@ -75,7 +79,11 @@ void ContentGfxCache::refresh(const Block& def, const Atlas& atlas) {
|
||||
|
||||
void ContentGfxCache::refresh() {
|
||||
auto indices = content.getIndices();
|
||||
sideregions = std::make_unique<UVRegion[]>(indices->blocks.count() * 6 * MAX_VARIANTS);
|
||||
size_t size = indices->blocks.count() * GFXC_SIDES * GFXC_MAX_VARIANTS * 2;
|
||||
|
||||
logger.info() << "uv cache size is " << (sizeof(UVRegion) * size) << " B";
|
||||
|
||||
sideregions = std::make_unique<UVRegion[]>(size);
|
||||
const auto& atlas = assets.require<Atlas>("blocks");
|
||||
|
||||
const auto& blocks = indices->blocks.getIterable();
|
||||
|
||||
@@ -14,9 +14,11 @@ class Assets;
|
||||
class Atlas;
|
||||
class Block;
|
||||
struct UVRegion;
|
||||
struct Variant;
|
||||
struct GraphicsSettings;
|
||||
|
||||
inline constexpr int MAX_VARIANTS = 16;
|
||||
inline constexpr int GFXC_MAX_VARIANTS = 16;
|
||||
inline constexpr int GFXC_SIDES = 6;
|
||||
|
||||
class ContentGfxCache {
|
||||
const Content& content;
|
||||
@@ -26,6 +28,13 @@ class ContentGfxCache {
|
||||
// array of block sides uv regions (6 per block)
|
||||
std::unique_ptr<UVRegion[]> sideregions;
|
||||
std::unordered_map<blockid_t, model::Model> models;
|
||||
|
||||
void refreshVariant(
|
||||
const Block& def,
|
||||
const Variant& variant,
|
||||
uint8_t variantIndex,
|
||||
const Atlas& atlas
|
||||
);
|
||||
public:
|
||||
ContentGfxCache(
|
||||
const Content& content,
|
||||
@@ -34,8 +43,14 @@ public:
|
||||
);
|
||||
~ContentGfxCache();
|
||||
|
||||
inline const UVRegion& getRegion(blockid_t id, uint8_t variant, int side) const {
|
||||
return sideregions[(id * 6 + side) * MAX_VARIANTS + variant];
|
||||
static inline size_t getRegionIndex(
|
||||
blockid_t id, uint8_t variant, int side, bool opaque
|
||||
) {
|
||||
return ((id * GFXC_SIDES + side) * GFXC_MAX_VARIANTS + variant) * 2 + opaque;
|
||||
}
|
||||
|
||||
inline const UVRegion& getRegion(blockid_t id, uint8_t variant, int side, bool dense) const {
|
||||
return sideregions[getRegionIndex(id, variant, side, !dense)];
|
||||
}
|
||||
|
||||
const model::Model& getModel(blockid_t id) const;
|
||||
|
||||
+23
-11
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "MeshData.hpp"
|
||||
|
||||
|
||||
@@ -8,25 +10,32 @@ struct MeshStats {
|
||||
static int drawCalls;
|
||||
};
|
||||
|
||||
struct IndexBufferData {
|
||||
const uint32_t* indices;
|
||||
size_t indicesCount;
|
||||
};
|
||||
|
||||
template <typename VertexStructure>
|
||||
class Mesh {
|
||||
struct IndexBuffer {
|
||||
unsigned int ibo;
|
||||
size_t indexCount;
|
||||
};
|
||||
unsigned int vao;
|
||||
unsigned int vbo;
|
||||
unsigned int ibo;
|
||||
std::vector<IndexBuffer> ibos;
|
||||
size_t vertexCount;
|
||||
size_t indexCount;
|
||||
public:
|
||||
explicit Mesh(const MeshData<VertexStructure>& data);
|
||||
|
||||
Mesh(
|
||||
const VertexStructure* vertexBuffer,
|
||||
size_t vertices,
|
||||
const uint32_t* indexBuffer,
|
||||
size_t indices
|
||||
std::vector<IndexBufferData> indices
|
||||
);
|
||||
|
||||
Mesh(const VertexStructure* vertexBuffer, size_t vertices)
|
||||
: Mesh<VertexStructure>(vertexBuffer, vertices, nullptr, 0) {};
|
||||
: Mesh<VertexStructure>(vertexBuffer, vertices, {}) {};
|
||||
|
||||
~Mesh();
|
||||
|
||||
@@ -34,18 +43,21 @@ public:
|
||||
/// attributes
|
||||
/// @param vertexBuffer vertex data buffer
|
||||
/// @param vertexCount number of vertices in new buffer
|
||||
/// @param indexBuffer indices buffer
|
||||
/// @param indexCount number of values in indices buffer
|
||||
/// @param indices indices buffer
|
||||
void reload(
|
||||
const VertexStructure* vertexBuffer,
|
||||
size_t vertexCount,
|
||||
const uint32_t* indexBuffer = nullptr,
|
||||
size_t indexCount = 0
|
||||
const std::vector<IndexBufferData>& indices
|
||||
);
|
||||
|
||||
void reload(const VertexStructure* vertexBuffer, size_t vertexCount) {
|
||||
static const std::vector<IndexBufferData> indices {};
|
||||
reload(vertexBuffer, vertexCount, indices);
|
||||
}
|
||||
|
||||
/// @brief Draw mesh with specified primitives type
|
||||
/// @param primitive primitives type
|
||||
void draw(unsigned int primitive) const;
|
||||
/// @param iboIndex index of used element buffer
|
||||
void draw(unsigned int primitive, int iboIndex = 0) const;
|
||||
|
||||
/// @brief Draw mesh as triangles
|
||||
void draw() const;
|
||||
|
||||
+45
-24
@@ -11,13 +11,21 @@ inline constexpr size_t calc_size(const VertexAttribute attrs[]) {
|
||||
return vertexSize;
|
||||
}
|
||||
|
||||
template <typename VertexStructure>
|
||||
inline std::vector<IndexBufferData> convert_to_ibd(const MeshData<VertexStructure>& data) {
|
||||
std::vector<IndexBufferData> indices;
|
||||
for (const auto& buffer : data.indices) {
|
||||
indices.push_back(IndexBufferData {buffer.data(), buffer.size()});
|
||||
}
|
||||
return indices;
|
||||
}
|
||||
|
||||
template <typename VertexStructure>
|
||||
Mesh<VertexStructure>::Mesh(const MeshData<VertexStructure>& data)
|
||||
: Mesh(
|
||||
data.vertices.data(),
|
||||
data.vertices.size(),
|
||||
data.indices.data(),
|
||||
data.indices.size()
|
||||
convert_to_ibd<VertexStructure>(data)
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -25,10 +33,9 @@ template <typename VertexStructure>
|
||||
Mesh<VertexStructure>::Mesh(
|
||||
const VertexStructure* vertexBuffer,
|
||||
size_t vertices,
|
||||
const uint32_t* indexBuffer,
|
||||
size_t indices
|
||||
std::vector<IndexBufferData> indices
|
||||
)
|
||||
: vao(0), vbo(0), ibo(0), vertexCount(0), indexCount(0) {
|
||||
: vao(0), vbo(0), ibos(), vertexCount(0) {
|
||||
static_assert(
|
||||
calc_size(VertexStructure::ATTRIBUTES) == sizeof(VertexStructure)
|
||||
);
|
||||
@@ -39,8 +46,9 @@ Mesh<VertexStructure>::Mesh(
|
||||
glGenVertexArrays(1, &vao);
|
||||
glGenBuffers(1, &vbo);
|
||||
|
||||
reload(vertexBuffer, vertices, indexBuffer, indices);
|
||||
reload(vertexBuffer, vertices, std::move(indices));
|
||||
|
||||
glBindVertexArray(vao);
|
||||
// attributes
|
||||
int offset = 0;
|
||||
for (int i = 0; attrs[i].count; i++) {
|
||||
@@ -65,8 +73,8 @@ Mesh<VertexStructure>::~Mesh() {
|
||||
MeshStats::meshesCount--;
|
||||
glDeleteVertexArrays(1, &vao);
|
||||
glDeleteBuffers(1, &vbo);
|
||||
if (ibo != 0) {
|
||||
glDeleteBuffers(1, &ibo);
|
||||
for (int i = ibos.size() - 1; i >= 0; i--) {
|
||||
glDeleteBuffers(1, &ibos[i].ibo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,11 +82,9 @@ template <typename VertexStructure>
|
||||
void Mesh<VertexStructure>::reload(
|
||||
const VertexStructure* vertexBuffer,
|
||||
size_t vertexCount,
|
||||
const uint32_t* indexBuffer,
|
||||
size_t indexCount
|
||||
const std::vector<IndexBufferData>& indices
|
||||
) {
|
||||
this->vertexCount = vertexCount;
|
||||
this->indexCount = indexCount;
|
||||
glBindVertexArray(vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
if (vertexBuffer != nullptr && vertexCount != 0) {
|
||||
@@ -92,30 +98,45 @@ void Mesh<VertexStructure>::reload(
|
||||
glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STREAM_DRAW);
|
||||
}
|
||||
|
||||
if (indexBuffer != nullptr && indexCount != 0) {
|
||||
if (ibo == 0) glGenBuffers(1, &ibo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
|
||||
for (int i = indices.size(); i < ibos.size(); i++) {
|
||||
glDeleteBuffers(1, &ibos[i].ibo);
|
||||
}
|
||||
ibos.clear();
|
||||
|
||||
for (int i = 0; i < indices.size(); i++) {
|
||||
const auto& indexBuffer = indices[i];
|
||||
ibos.push_back(IndexBuffer {0, 0});
|
||||
glGenBuffers(1, &ibos[i].ibo);
|
||||
ibos[i].indexCount = indexBuffer.indicesCount;
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibos[i].ibo);
|
||||
glBufferData(
|
||||
GL_ELEMENT_ARRAY_BUFFER,
|
||||
sizeof(uint32_t) * indexCount,
|
||||
indexBuffer,
|
||||
sizeof(uint32_t) * indexBuffer.indicesCount,
|
||||
indexBuffer.indices,
|
||||
GL_STATIC_DRAW
|
||||
);
|
||||
} else if (ibo != 0) {
|
||||
glDeleteBuffers(1, &ibo);
|
||||
}
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
template <typename VertexStructure>
|
||||
void Mesh<VertexStructure>::draw(unsigned int primitive) const {
|
||||
void Mesh<VertexStructure>::draw(unsigned int primitive, int iboIndex) const {
|
||||
MeshStats::drawCalls++;
|
||||
glBindVertexArray(vao);
|
||||
if (ibo != 0) {
|
||||
glDrawElements(primitive, indexCount, GL_UNSIGNED_INT, nullptr);
|
||||
} else {
|
||||
|
||||
if (!ibos.empty()) {
|
||||
if (iboIndex < ibos.size()) {
|
||||
glBindVertexArray(vao);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibos[iboIndex].ibo);
|
||||
glDrawElements(
|
||||
primitive, ibos.at(iboIndex).indexCount, GL_UNSIGNED_INT, nullptr
|
||||
);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
} else if (vertexCount > 0) {
|
||||
glBindVertexArray(vao);
|
||||
glDrawArrays(primitive, 0, vertexCount);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
template <typename VertexStructure>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "typedefs.hpp"
|
||||
@@ -40,7 +41,7 @@ struct VertexAttribute {
|
||||
template<typename VertexStructure>
|
||||
struct MeshData {
|
||||
util::Buffer<VertexStructure> vertices;
|
||||
util::Buffer<uint32_t> indices;
|
||||
std::vector<util::Buffer<uint32_t>> indices;
|
||||
util::Buffer<VertexAttribute> attrs;
|
||||
|
||||
MeshData() = default;
|
||||
@@ -50,7 +51,7 @@ struct MeshData {
|
||||
/// @param attrs vertex attribute sizes (must be null-terminated)
|
||||
MeshData(
|
||||
util::Buffer<VertexStructure> vertices,
|
||||
util::Buffer<uint32_t> indices,
|
||||
std::vector<util::Buffer<uint32_t>> indices,
|
||||
util::Buffer<VertexAttribute> attrs
|
||||
) : vertices(std::move(vertices)),
|
||||
indices(std::move(indices)),
|
||||
|
||||
@@ -27,9 +27,10 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
|
||||
){
|
||||
display::clear();
|
||||
blockid_t id = def.rt.id;
|
||||
const UVRegion texfaces[6]{cache.getRegion(id, 0, 0), cache.getRegion(id, 0, 1),
|
||||
cache.getRegion(id, 0, 2), cache.getRegion(id, 0, 3),
|
||||
cache.getRegion(id, 0, 4), cache.getRegion(id, 0, 5)};
|
||||
const UVRegion texfaces[6] {
|
||||
cache.getRegion(id, 0, 0, true), cache.getRegion(id, 0, 1, true),
|
||||
cache.getRegion(id, 0, 2, true), cache.getRegion(id, 0, 3, true),
|
||||
cache.getRegion(id, 0, 4, true), cache.getRegion(id, 0, 5, true)};
|
||||
|
||||
glm::vec3 offset(0.1f, 0.5f, 0.1f);
|
||||
switch (def.defaults.model.type) {
|
||||
|
||||
@@ -20,6 +20,7 @@ BlocksRenderer::BlocksRenderer(
|
||||
) : content(content),
|
||||
vertexBuffer(std::make_unique<ChunkVertex[]>(capacity)),
|
||||
indexBuffer(std::make_unique<uint32_t[]>(capacity)),
|
||||
denseIndexBuffer(std::make_unique<uint32_t[]>(capacity)),
|
||||
vertexCount(0),
|
||||
vertexOffset(0),
|
||||
indexCount(0),
|
||||
@@ -475,8 +476,10 @@ glm::vec4 BlocksRenderer::pickSoftLight(
|
||||
}
|
||||
|
||||
void BlocksRenderer::render(
|
||||
const voxel* voxels, int beginEnds[256][2]
|
||||
const voxel* voxels, const int beginEnds[256][2]
|
||||
) {
|
||||
bool denseRender = this->denseRender;
|
||||
bool densePass = this->densePass;
|
||||
for (const auto drawGroup : *content.drawGroups) {
|
||||
int begin = beginEnds[drawGroup][0];
|
||||
if (begin == 0) {
|
||||
@@ -493,16 +496,19 @@ void BlocksRenderer::render(
|
||||
if (id == 0 || variant.drawGroup != drawGroup || state.segment) {
|
||||
continue;
|
||||
}
|
||||
if (denseRender != (variant.culling == CullingMode::OPTIONAL)) {
|
||||
continue;
|
||||
}
|
||||
if (def.translucent) {
|
||||
continue;
|
||||
}
|
||||
const UVRegion texfaces[6] {
|
||||
cache.getRegion(id, variantId, 0),
|
||||
cache.getRegion(id, variantId, 1),
|
||||
cache.getRegion(id, variantId, 2),
|
||||
cache.getRegion(id, variantId, 3),
|
||||
cache.getRegion(id, variantId, 4),
|
||||
cache.getRegion(id, variantId, 5)
|
||||
cache.getRegion(id, variantId, 0, densePass),
|
||||
cache.getRegion(id, variantId, 1, densePass),
|
||||
cache.getRegion(id, variantId, 2, densePass),
|
||||
cache.getRegion(id, variantId, 3, densePass),
|
||||
cache.getRegion(id, variantId, 4, densePass),
|
||||
cache.getRegion(id, variantId, 5, densePass)
|
||||
};
|
||||
int x = i % CHUNK_W;
|
||||
int y = i / (CHUNK_D * CHUNK_W);
|
||||
@@ -513,16 +519,19 @@ void BlocksRenderer::render(
|
||||
def.ambientOcclusion);
|
||||
break;
|
||||
case BlockModelType::XSPRITE: {
|
||||
if (!denseRender)
|
||||
blockXSprite(x, y, z, glm::vec3(1.0f),
|
||||
texfaces[FACE_MX], texfaces[FACE_MZ], 1.0f);
|
||||
break;
|
||||
}
|
||||
case BlockModelType::AABB: {
|
||||
if (!denseRender)
|
||||
blockAABB({x, y, z}, texfaces, &def, vox.state.rotation,
|
||||
!def.shadeless, def.ambientOcclusion);
|
||||
break;
|
||||
}
|
||||
case BlockModelType::CUSTOM: {
|
||||
if (!denseRender)
|
||||
blockCustomModel(
|
||||
{x, y, z},
|
||||
def,
|
||||
@@ -550,6 +559,8 @@ SortingMeshData BlocksRenderer::renderTranslucent(
|
||||
AABB aabb {};
|
||||
bool aabbInit = false;
|
||||
size_t totalSize = 0;
|
||||
|
||||
bool densePass = this->densePass;
|
||||
for (const auto drawGroup : *content.drawGroups) {
|
||||
int begin = beginEnds[drawGroup][0];
|
||||
if (begin == 0) {
|
||||
@@ -570,12 +581,12 @@ SortingMeshData BlocksRenderer::renderTranslucent(
|
||||
continue;
|
||||
}
|
||||
const UVRegion texfaces[6] {
|
||||
cache.getRegion(id, variantId, 0),
|
||||
cache.getRegion(id, variantId, 1),
|
||||
cache.getRegion(id, variantId, 2),
|
||||
cache.getRegion(id, variantId, 3),
|
||||
cache.getRegion(id, variantId, 4),
|
||||
cache.getRegion(id, variantId, 5)
|
||||
cache.getRegion(id, variantId, 0, densePass),
|
||||
cache.getRegion(id, variantId, 1, densePass),
|
||||
cache.getRegion(id, variantId, 2, densePass),
|
||||
cache.getRegion(id, variantId, 3, densePass),
|
||||
cache.getRegion(id, variantId, 4, densePass),
|
||||
cache.getRegion(id, variantId, 5, densePass)
|
||||
};
|
||||
int x = i % CHUNK_W;
|
||||
int y = i / (CHUNK_D * CHUNK_W);
|
||||
@@ -700,21 +711,45 @@ void BlocksRenderer::build(const Chunk* chunk, const Chunks* chunks) {
|
||||
vertexCount = 0;
|
||||
vertexOffset = indexCount = 0;
|
||||
|
||||
denseRender = false;
|
||||
densePass = false;
|
||||
|
||||
sortingMesh = renderTranslucent(voxels, beginEnds);
|
||||
|
||||
overflow = false;
|
||||
vertexCount = 0;
|
||||
vertexOffset = 0;
|
||||
indexCount = 0;
|
||||
denseIndexCount = 0;
|
||||
|
||||
denseRender = false; //settings.graphics.denseRender.get();
|
||||
densePass = false;
|
||||
render(voxels, beginEnds);
|
||||
|
||||
size_t endIndex = indexCount;
|
||||
|
||||
denseRender = true;
|
||||
densePass = true;
|
||||
render(voxels, beginEnds);
|
||||
|
||||
denseIndexCount = indexCount;
|
||||
for (size_t i = 0; i < denseIndexCount; i++) {
|
||||
denseIndexBuffer[i] = indexBuffer[i];
|
||||
}
|
||||
|
||||
indexCount = endIndex;
|
||||
densePass = false;
|
||||
render(voxels, beginEnds);
|
||||
}
|
||||
|
||||
ChunkMeshData BlocksRenderer::createMesh() {
|
||||
return ChunkMeshData{
|
||||
return ChunkMeshData {
|
||||
MeshData(
|
||||
util::Buffer(vertexBuffer.get(), vertexCount),
|
||||
util::Buffer(indexBuffer.get(), indexCount),
|
||||
std::vector<util::Buffer<uint32_t>> {
|
||||
util::Buffer(indexBuffer.get(), indexCount),
|
||||
util::Buffer(denseIndexBuffer.get(), denseIndexCount),
|
||||
},
|
||||
util::Buffer(
|
||||
ChunkVertex::ATTRIBUTES, sizeof(ChunkVertex::ATTRIBUTES) / sizeof(VertexAttribute)
|
||||
)
|
||||
@@ -727,10 +762,19 @@ ChunkMesh BlocksRenderer::render(const Chunk *chunk, const Chunks *chunks) {
|
||||
build(chunk, chunks);
|
||||
|
||||
return ChunkMesh{std::make_unique<Mesh<ChunkVertex>>(
|
||||
vertexBuffer.get(), vertexCount, indexBuffer.get(), indexCount
|
||||
vertexBuffer.get(), vertexCount,
|
||||
std::vector<IndexBufferData> {
|
||||
IndexBufferData {indexBuffer.get(), indexCount},
|
||||
IndexBufferData {denseIndexBuffer.get(), denseIndexCount},
|
||||
}
|
||||
), std::move(sortingMesh)};
|
||||
}
|
||||
|
||||
VoxelsVolume* BlocksRenderer::getVoxelsBuffer() const {
|
||||
return voxelsBuffer.get();
|
||||
}
|
||||
|
||||
size_t BlocksRenderer::getMemoryConsumption() const {
|
||||
size_t volume = voxelsBuffer->getW() * voxelsBuffer->getH() * voxelsBuffer->getD();
|
||||
return capacity * (sizeof(ChunkVertex) + sizeof(uint32_t) * 2) + volume * (sizeof(voxel) + sizeof(light_t));
|
||||
}
|
||||
|
||||
@@ -26,13 +26,17 @@ class BlocksRenderer {
|
||||
const Content& content;
|
||||
std::unique_ptr<ChunkVertex[]> vertexBuffer;
|
||||
std::unique_ptr<uint32_t[]> indexBuffer;
|
||||
std::unique_ptr<uint32_t[]> denseIndexBuffer;
|
||||
size_t vertexCount;
|
||||
size_t vertexOffset;
|
||||
size_t indexCount;
|
||||
size_t denseIndexCount;
|
||||
size_t capacity;
|
||||
int voxelBufferPadding = 2;
|
||||
bool overflow = false;
|
||||
bool cancelled = false;
|
||||
bool densePass = false;
|
||||
bool denseRender = false;
|
||||
const Chunk* chunk = nullptr;
|
||||
std::unique_ptr<VoxelsVolume> voxelsBuffer;
|
||||
|
||||
@@ -135,10 +139,12 @@ class BlocksRenderer {
|
||||
if ((otherDrawGroup && (otherDrawGroup != variant.drawGroup)) || !blockVariant.rt.solid) {
|
||||
return true;
|
||||
}
|
||||
if ((variant.culling == CullingMode::DISABLED ||
|
||||
(variant.culling == CullingMode::OPTIONAL &&
|
||||
settings.graphics.denseRender.get())) &&
|
||||
vox.id == def.rt.id) {
|
||||
if (densePass) {
|
||||
return variant.culling == CullingMode::OPTIONAL;
|
||||
} else if (variant.culling == CullingMode::OPTIONAL) {
|
||||
return false;
|
||||
}
|
||||
if (variant.culling == CullingMode::DISABLED && vox.id == def.rt.id) {
|
||||
return true;
|
||||
}
|
||||
return !vox.id;
|
||||
@@ -149,7 +155,7 @@ class BlocksRenderer {
|
||||
glm::vec4 pickSoftLight(const glm::ivec3& coord, const glm::ivec3& right, const glm::ivec3& up) const;
|
||||
glm::vec4 pickSoftLight(float x, float y, float z, const glm::ivec3& right, const glm::ivec3& up) const;
|
||||
|
||||
void render(const voxel* voxels, int beginEnds[256][2]);
|
||||
void render(const voxel* voxels, const int beginEnds[256][2]);
|
||||
SortingMeshData renderTranslucent(const voxel* voxels, int beginEnds[256][2]);
|
||||
public:
|
||||
BlocksRenderer(
|
||||
@@ -165,6 +171,8 @@ public:
|
||||
ChunkMeshData createMesh();
|
||||
VoxelsVolume* getVoxelsBuffer() const;
|
||||
|
||||
size_t getMemoryConsumption() const;
|
||||
|
||||
bool isCancelled() const {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@@ -87,10 +87,12 @@ ChunksRenderer::ChunksRenderer(
|
||||
level->content, cache, settings
|
||||
);
|
||||
logger.info() << "created " << threadPool.getWorkersCount() << " workers";
|
||||
logger.info() << "memory consumption is "
|
||||
<< renderer->getMemoryConsumption() * threadPool.getWorkersCount()
|
||||
<< " B";
|
||||
}
|
||||
|
||||
ChunksRenderer::~ChunksRenderer() {
|
||||
}
|
||||
ChunksRenderer::~ChunksRenderer() = default;
|
||||
|
||||
const Mesh<ChunkVertex>* ChunksRenderer::render(
|
||||
const std::shared_ptr<Chunk>& chunk, bool important
|
||||
@@ -183,29 +185,47 @@ const Mesh<ChunkVertex>* ChunksRenderer::retrieveChunk(
|
||||
}
|
||||
|
||||
void ChunksRenderer::drawChunksShadowsPass(
|
||||
const Camera& camera, Shader& shader
|
||||
const Camera& camera, Shader& shader, const Camera& playerCamera
|
||||
) {
|
||||
Frustum frustum;
|
||||
frustum.update(camera.getProjView());
|
||||
|
||||
const auto& atlas = assets.require<Atlas>("blocks");
|
||||
|
||||
atlas.getTexture()->bind();
|
||||
|
||||
auto denseDistance = settings.graphics.denseRenderDistance.get();
|
||||
auto denseDistance2 = denseDistance * denseDistance;
|
||||
|
||||
for (const auto& chunk : chunks.getChunks()) {
|
||||
if (chunk == nullptr) {
|
||||
continue;
|
||||
}
|
||||
glm::ivec2 pos {chunk->x, chunk->z};
|
||||
const auto& found = meshes.find({chunk->x, chunk->z});
|
||||
if (found == meshes.end()) {
|
||||
continue;
|
||||
}
|
||||
auto mesh = found->second.mesh.get();
|
||||
if (mesh) {
|
||||
glm::vec3 coord(
|
||||
chunk->x * CHUNK_W + 0.5f, 0.5f, chunk->z * CHUNK_D + 0.5f
|
||||
);
|
||||
glm::mat4 model = glm::translate(glm::mat4(1.0f), coord);
|
||||
shader.uniformMatrix("u_model", model);
|
||||
mesh->draw();
|
||||
|
||||
glm::vec3 coord(
|
||||
pos.x * CHUNK_W + 0.5f, 0.5f, pos.y * CHUNK_D + 0.5f
|
||||
);
|
||||
|
||||
glm::vec3 min(chunk->x * CHUNK_W, chunk->bottom, chunk->z * CHUNK_D);
|
||||
glm::vec3 max(
|
||||
chunk->x * CHUNK_W + CHUNK_W,
|
||||
chunk->top,
|
||||
chunk->z * CHUNK_D + CHUNK_D
|
||||
);
|
||||
|
||||
if (!frustum.isBoxVisible(min, max)) {
|
||||
continue;
|
||||
}
|
||||
glm::mat4 model = glm::translate(glm::mat4(1.0f), coord);
|
||||
shader.uniformMatrix("u_model", model);
|
||||
found->second.mesh->draw(GL_TRIANGLES,
|
||||
glm::distance2(playerCamera.position * glm::vec3(1, 0, 1),
|
||||
(min + max) * 0.5f * glm::vec3(1, 0, 1)) < denseDistance2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,6 +262,9 @@ void ChunksRenderer::drawChunks(
|
||||
visibleChunks = 0;
|
||||
shader.uniform1i("u_alphaClip", true);
|
||||
|
||||
auto denseDistance = settings.graphics.denseRenderDistance.get();
|
||||
auto denseDistance2 = denseDistance * denseDistance;
|
||||
|
||||
// TODO: minimize draw calls number
|
||||
for (int i = indices.size()-1; i >= 0; i--) {
|
||||
auto& chunk = chunks.getChunks()[indices[i].index];
|
||||
@@ -253,7 +276,8 @@ void ChunksRenderer::drawChunks(
|
||||
);
|
||||
glm::mat4 model = glm::translate(glm::mat4(1.0f), coord);
|
||||
shader.uniformMatrix("u_model", model);
|
||||
mesh->draw();
|
||||
mesh->draw(GL_TRIANGLES, glm::distance2(camera.position * glm::vec3(1, 0, 1),
|
||||
(coord + glm::vec3(CHUNK_W * 0.5f, 0.0f, CHUNK_D * 0.5f))) < denseDistance2);
|
||||
visibleChunks++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,9 @@ public:
|
||||
const std::shared_ptr<Chunk>& chunk, bool important
|
||||
);
|
||||
|
||||
void drawChunksShadowsPass(const Camera& camera, Shader& shader);
|
||||
void drawChunksShadowsPass(
|
||||
const Camera& camera, Shader& shader, const Camera& playerCamera
|
||||
);
|
||||
|
||||
void drawChunks(const Camera& camera, Shader& shader);
|
||||
|
||||
|
||||
@@ -220,8 +220,6 @@ void WorldRenderer::renderLevel(
|
||||
if (hudVisible) {
|
||||
renderLines(camera, linesShader, ctx);
|
||||
}
|
||||
shader.use();
|
||||
chunks->drawSortedMeshes(camera, shader);
|
||||
|
||||
if (!pause) {
|
||||
scripting::on_frontend_render();
|
||||
@@ -400,14 +398,13 @@ void WorldRenderer::generateShadowsMap(
|
||||
shadowCamera.setProjection(glm::ortho(min.x, max.x, min.y, max.y, 0.1f, 1000.0f));
|
||||
|
||||
{
|
||||
frustumCulling->update(shadowCamera.getProjView());
|
||||
auto sctx = pctx.sub();
|
||||
sctx.setDepthTest(true);
|
||||
sctx.setCullFace(true);
|
||||
sctx.setViewport({resolution, resolution});
|
||||
shadowMap.bind();
|
||||
setupWorldShader(shadowsShader, shadowCamera, settings, 0.0f);
|
||||
chunks->drawChunksShadowsPass(shadowCamera, shadowsShader);
|
||||
chunks->drawChunksShadowsPass(shadowCamera, shadowsShader, camera);
|
||||
shadowMap.unbind();
|
||||
}
|
||||
}
|
||||
@@ -432,6 +429,7 @@ void WorldRenderer::draw(
|
||||
|
||||
auto& mainShader = assets.require<Shader>("main");
|
||||
auto& entityShader = assets.require<Shader>("entity");
|
||||
auto& translucentShader = assets.require<Shader>("translucent");
|
||||
auto& deferredShader = assets.require<PostEffect>("deferred_lighting").getShader();
|
||||
const auto& settings = engine.getSettings();
|
||||
|
||||
@@ -464,6 +462,7 @@ void WorldRenderer::draw(
|
||||
mainShader.recompile();
|
||||
entityShader.recompile();
|
||||
deferredShader.recompile();
|
||||
translucentShader.recompile();
|
||||
prevCTShaderSettings = currentSettings;
|
||||
}
|
||||
|
||||
@@ -526,6 +525,7 @@ void WorldRenderer::draw(
|
||||
{
|
||||
DrawContext ctx = pctx.sub();
|
||||
ctx.setDepthTest(true);
|
||||
|
||||
if (gbufferPipeline) {
|
||||
postProcessing.bindDepthBuffer();
|
||||
} else {
|
||||
@@ -534,6 +534,16 @@ void WorldRenderer::draw(
|
||||
// Drawing background sky plane
|
||||
skybox->draw(ctx, camera, assets, worldInfo.daytime, clouds);
|
||||
|
||||
{
|
||||
auto sctx = ctx.sub();
|
||||
sctx.setCullFace(true);
|
||||
skybox->bind();
|
||||
translucentShader.use();
|
||||
setupWorldShader(translucentShader, camera, settings, fogFactor);
|
||||
chunks->drawSortedMeshes(camera, translucentShader);
|
||||
skybox->unbind();
|
||||
}
|
||||
|
||||
entityShader.use();
|
||||
setupWorldShader(entityShader, camera, settings, fogFactor);
|
||||
|
||||
@@ -553,8 +563,7 @@ void WorldRenderer::draw(
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
postProcessing.render(pctx, assets, timer, camera);
|
||||
|
||||
skybox->unbind();
|
||||
|
||||
if (player.currentCamera == player.fpCamera) {
|
||||
DrawContext ctx = pctx.sub();
|
||||
ctx.setDepthTest(true);
|
||||
|
||||
@@ -47,7 +47,7 @@ struct ChunkMeshData {
|
||||
};
|
||||
|
||||
struct ChunkMesh {
|
||||
std::unique_ptr<Mesh<ChunkVertex> > mesh;
|
||||
std::unique_ptr<Mesh<ChunkVertex>> mesh;
|
||||
SortingMeshData sortingMeshData;
|
||||
std::unique_ptr<Mesh<ChunkVertex> > sortedMesh = nullptr;
|
||||
};
|
||||
|
||||
@@ -77,6 +77,7 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) {
|
||||
builder.add("advanced-render", &settings.graphics.advancedRender);
|
||||
builder.add("ssao", &settings.graphics.ssao);
|
||||
builder.add("shadows-quality", &settings.graphics.shadowsQuality);
|
||||
builder.add("dense-render-distance", &settings.graphics.denseRenderDistance);
|
||||
|
||||
builder.section("ui");
|
||||
builder.add("language", &settings.ui.language);
|
||||
|
||||
@@ -81,6 +81,8 @@ struct GraphicsSettings {
|
||||
FlagSetting ssao {true};
|
||||
/// @brief Shadows quality
|
||||
IntegerSetting shadowsQuality {0, 0, 3};
|
||||
/// @brief Dense render distance
|
||||
IntegerSetting denseRenderDistance {56, 0, 10'000};
|
||||
};
|
||||
|
||||
struct DebugSettings {
|
||||
|
||||
Reference in New Issue
Block a user