Toast Engine
Loading...
Searching...
No Matches
voxel_volume.hpp
Go to the documentation of this file.
1
6
7#pragma once
8#include "brick.hpp"
9#include "brick_pool.hpp"
10#include "voxel_constants.hpp"
11
12#include <cstdint>
13#include <glm/glm.hpp>
14#include <optional>
15#include <span>
16#include <toast/export.hpp>
17#include <vector>
18
19namespace voxel {
20
21class TOAST_API Volume {
22public:
23 struct VoxelWrite {
24 uint8_t previous_material = k_empty_palette_index;
25
26 bool changed = false;
27
28 bool brick_became_occupied = false;
29
30 bool brick_became_empty = false;
31 };
32
33 Volume(BrickPool& pool, glm::uvec3 brick_dims);
34
35 ~Volume();
36
37 Volume(const Volume&) = delete;
38 auto operator=(const Volume&) -> Volume& = delete;
39 Volume(Volume&& other) noexcept;
40 auto operator=(Volume&& other) noexcept -> Volume&;
41
43 [[nodiscard]]
44 static auto instanceOf(const Volume& source) -> Volume;
45
46 [[nodiscard]]
47 static auto adoptResized(Volume& source, glm::ivec3 brick_offset, glm::uvec3 brick_dims) -> Volume;
48
49 [[nodiscard]]
50 auto brickDims() const noexcept -> glm::uvec3 {
51 return m_brick_dims;
52 }
53
54 [[nodiscard]]
55 auto voxelDims() const noexcept -> glm::uvec3 {
56 return m_brick_dims * k_brick_dim;
57 }
58
59 [[nodiscard]]
60 auto brickCount() const noexcept -> uint32_t {
61 return static_cast<uint32_t>(m_entries.size());
62 }
63
64 [[nodiscard]]
65 auto containsBrick(glm::ivec3 brick) const noexcept -> bool;
66
67 [[nodiscard]]
68 auto containsVoxel(glm::ivec3 voxel) const noexcept -> bool;
69
70 [[nodiscard]]
71 auto entryAt(glm::ivec3 brick) const noexcept -> BrickEntry;
72
73 [[nodiscard]]
74 auto materialAt(glm::ivec3 voxel) const noexcept -> uint8_t;
75
76 [[nodiscard]]
77 auto isSolidAt(glm::ivec3 voxel) const noexcept -> bool;
78
79 auto setVoxel(glm::ivec3 voxel, uint8_t material) -> VoxelWrite;
80
81 void setBrickUniform(glm::ivec3 brick, uint8_t material);
82
83 auto tryCollapseUniform(glm::ivec3 brick) -> bool;
84
86 auto setBrickMaterial(glm::ivec3 brick, std::span<const uint8_t, k_brick_material_bytes> material) -> bool;
87
89 [[nodiscard]]
90 auto takeBrick(glm::ivec3 brick) -> BrickEntry;
91
93 void adoptBrick(glm::ivec3 brick, BrickEntry entry);
94
96 auto clearVoxels(glm::ivec3 brick, const BrickOccupancy& mask) -> bool;
97
102 auto copyBrickFrom(glm::ivec3 brick, const Volume& source, glm::ivec3 source_brick) -> bool;
103
105 std::span<uint8_t, k_brick_material_bytes> material;
106 BrickOccupancy* occupancy = nullptr;
107 };
108
109 auto beginBrickWrite(glm::ivec3 brick) -> std::optional<WritableBrick>;
110 void finishBrickWrite(glm::ivec3 brick, bool changed);
111
113 [[nodiscard]]
114 auto occupancyPointer(glm::ivec3 brick) const noexcept -> const BrickOccupancy*;
115
116 [[nodiscard]]
117 auto neighbourhoodOf(glm::ivec3 brick) const noexcept -> BrickNeighbourhood;
118
119 [[nodiscard]]
120 auto solidVoxelCount() const -> uint32_t;
121
122 [[nodiscard]]
123 auto ownedBrickCount() const -> uint32_t;
124
125 [[nodiscard]]
126 auto sharedBrickCount() const -> uint32_t;
127
129 [[nodiscard]]
130 auto revision() const noexcept -> uint32_t {
131 return m_revision;
132 }
133
134 [[nodiscard]]
135 auto pool() const noexcept -> BrickPool* {
136 return m_pool;
137 }
138
140 [[nodiscard]]
141 auto id() const noexcept -> uint64_t {
142 return m_id;
143 }
144
146 [[nodiscard]]
147 auto dirtyBricksSince(uint32_t revision) const noexcept -> std::optional<std::span<const uint32_t>>;
148
149 [[nodiscard]]
150 auto brickAtIndex(uint32_t index) const noexcept -> glm::ivec3;
151
153 void discardDirty() const noexcept;
154
155private:
156 void markDirty(glm::ivec3 brick);
157
158 [[nodiscard]]
159 auto entryIndex(glm::ivec3 brick) const noexcept -> uint32_t;
160
161 auto makeWritable(uint32_t entry_index) -> uint32_t;
162
163 void releaseOwned();
164
165 BrickPool* m_pool = nullptr;
166 glm::uvec3 m_brick_dims {0};
167
168 std::vector<BrickEntry> m_entries;
169
170 uint32_t m_revision = 0;
171
172 uint64_t m_id = 0;
173
175 mutable std::vector<uint64_t> m_dirty_mask;
176 mutable std::vector<uint32_t> m_dirty_list;
177 mutable uint32_t m_dirty_since = 0;
178};
179
180}
auto occupancyPointer(glm::ivec3 brick) const noexcept -> const BrickOccupancy *
Null when the brick holds nothing.
Definition voxel_volume.cpp:169
static auto instanceOf(const Volume &source) -> Volume
Shares every brick with source until written.
Definition voxel_volume.cpp:79
Definition brick_pool.hpp:26
auto id() const noexcept -> uint64_t
Unique to this content so a replaced volume never matches the id of the one before it.
Definition voxel_volume.hpp:141
auto revision() const noexcept -> uint32_t
Bumped by writes that change a voxel and by a collapse since that changes the packed form.
Definition voxel_volume.hpp:130
Definition voxel_volume.hpp:23
Definition voxel_volume.hpp:104