QcChunkSection Class
16x16x16 blocks stored as a palette plus per-block palette indices. More...
| Header: | #include <common/world/QcChunkSection.h> |
Public Functions
| bool | assign(QList<QcBlockId> palette, const QList<quint16> &indices) |
| int | bitsPerIndex() const |
| QcBlockId | block(int x, int y, int z) const |
| void | compact() |
| void | fill(QcBlockId block) |
| void | forEachCell(QcBlockPos corner, const Wanted &wanted, const Visit &visit) const |
| void | forEachRun(const Visit &visit) const |
| qsizetype | heapBytes() const |
| bool | isEmpty() const |
| bool | isUniform() const |
| const QList<QcBlockId> & | palette() const |
| quint16 | paletteIndex(int cell) const |
| bool | replacePalette(QList<QcBlockId> palette) |
| void | setBlock(int x, int y, int z, QcBlockId block) |
| QList<quint16> | unpackedIndices() const |
Static Public Members
| int | index(int x, int y, int z) |
Detailed Description
A section with a single block type stores no index array, so empty (all-air) sections cost almost nothing. The indices are packed into 16-bit words with as many bits as the palette needs (decision E-844): 4 bits for up to 16 entries, 8 bits for up to 256, 16 bits beyond; compact() and assign() also pick 1 or 2 bits for palettes of 2 or 4 entries. A growing palette widens the indices (repacking all cells once); compact() narrows them again. The words are an implicitly shared QList, so a copy of a section (a chunk snapshot for mesher and light) shares them until one side writes.
Member Function Documentation
bool QcChunkSection::assign(QList<QcBlockId> palette, const QList<quint16> &indices)
Replaces the content with palette and one index per cell, indices (may be empty for a palette of one entry). Returns false when the data is inconsistent (wrong size, index out of palette).
int QcChunkSection::bitsPerIndex() const
Returns the bits per stored palette index: 0 for a uniform section, else 1, 2, 4, 8 or 16.
QcBlockId QcChunkSection::block(int x, int y, int z) const
Returns the block of the cell (x, y, z), each in 0..15.
See also setBlock().
void QcChunkSection::compact()
Drops unused palette entries, merges entries of the same block (keeping the first), packs the indices as narrow as the palette allows and collapses to uniform storage when possible. The remaining entries keep their order. Returns at once for a section not written since the last compact().
void QcChunkSection::fill(QcBlockId block)
Sets every cell to block (uniform storage).
template <typename Wanted, typename Visit> void QcChunkSection::forEachCell(QcBlockPos corner, const Wanted &wanted, const Visit &visit) const
Calls visit(pos) (visit) for every cell whose block the predicate wanted accepts, with the section's lowest corner at corner in the world.
template <typename Visit> void QcChunkSection::forEachRun(const Visit &visit) const
Calls visit(length, paletteIndex) (visit) for each maximal run of equal palette indices in the order of index(); one run of all cells for a uniform section. Compares whole words while a run lasts, so long runs cost one comparison per word (the chunk encoder).
qsizetype QcChunkSection::heapBytes() const
Returns the bytes the palette and the indices hold on the heap (their capacity; shared copies count in full for each holder), for memory statistics.
[static constexpr] int QcChunkSection::index(int x, int y, int z)
Returns the index of the cell (x, y, z): x fastest, then z, then y.
bool QcChunkSection::isEmpty() const
Returns whether every cell is air.
bool QcChunkSection::isUniform() const
Returns whether every cell holds the one palette entry (no indices stored).
const QList<QcBlockId> &QcChunkSection::palette() const
Returns the palette; the first entry in a uniform section.
quint16 QcChunkSection::paletteIndex(int cell) const
Returns the palette index of the cell cell (in the order of index()); 0 in a uniform section.
bool QcChunkSection::replacePalette(QList<QcBlockId> palette)
Replaces the palette's entries by palette of the same size, keeping every cell's index (a translation of block ids); returns false for a palette of another size.
void QcChunkSection::setBlock(int x, int y, int z, QcBlockId block)
Sets the cell (x, y, z) to block, adding it to the palette when needed.
See also block().
QList<quint16> QcChunkSection::unpackedIndices() const
Returns the palette indices of all cells in the order of index() (empty for a uniform section), for encoders and tests; the section itself keeps them packed.