update ContentGfxCache

This commit is contained in:
MihailRis 2025-07-24 22:51:53 +03:00
parent 4a6ccf4847
commit 799d712447
5 changed files with 69 additions and 35 deletions

View File

@ -53,7 +53,7 @@ public:
});
}
const T& get() const {
[[nodiscard]] const T& get() const {
return value;
}

View File

@ -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();

View File

@ -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;

View File

@ -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) {

View File

@ -477,6 +477,7 @@ glm::vec4 BlocksRenderer::pickSoftLight(
void BlocksRenderer::render(
const voxel* voxels, int beginEnds[256][2]
) {
bool denseRender = this->denseRender;
for (const auto drawGroup : *content.drawGroups) {
int begin = beginEnds[drawGroup][0];
if (begin == 0) {
@ -497,12 +498,12 @@ void BlocksRenderer::render(
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, denseRender),
cache.getRegion(id, variantId, 1, denseRender),
cache.getRegion(id, variantId, 2, denseRender),
cache.getRegion(id, variantId, 3, denseRender),
cache.getRegion(id, variantId, 4, denseRender),
cache.getRegion(id, variantId, 5, denseRender)
};
int x = i % CHUNK_W;
int y = i / (CHUNK_D * CHUNK_W);
@ -550,6 +551,8 @@ SortingMeshData BlocksRenderer::renderTranslucent(
AABB aabb {};
bool aabbInit = false;
size_t totalSize = 0;
bool denseRender = this->denseRender;
for (const auto drawGroup : *content.drawGroups) {
int begin = beginEnds[drawGroup][0];
if (begin == 0) {
@ -570,12 +573,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, denseRender),
cache.getRegion(id, variantId, 1, denseRender),
cache.getRegion(id, variantId, 2, denseRender),
cache.getRegion(id, variantId, 3, denseRender),
cache.getRegion(id, variantId, 4, denseRender),
cache.getRegion(id, variantId, 5, denseRender)
};
int x = i % CHUNK_W;
int y = i / (CHUNK_D * CHUNK_W);
@ -706,9 +709,16 @@ void BlocksRenderer::build(const Chunk* chunk, const Chunks* chunks) {
vertexCount = 0;
vertexOffset = 0;
indexCount = 0;
denseRender = settings.graphics.denseRender.get();
// denseRender = false;
render(voxels, beginEnds);
// denseRender = settings.graphics.denseRender.get();
// if (denseRender) {
// }
}
ChunkMeshData BlocksRenderer::createMesh() {