Skip to content

Commit

Permalink
delete assets noexcept migrate turn off engine test
Browse files Browse the repository at this point in the history
  • Loading branch information
JungerBoyo authored and JungerBoyo committed Jan 6, 2024
1 parent 240772c commit 3f67479
Show file tree
Hide file tree
Showing 31 changed files with 288 additions and 188 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ ve001_log
maps/
ve001_frame_samples.csv
ve001_meshing_samples.csv
data/
data/
_assets/
Empty file removed assets/CMakeLists.txt
Empty file.
Binary file removed assets/textures/mcgrasstexture.png
Binary file not shown.
Binary file removed assets/textures/mcgrasstexture_medium.png
Binary file not shown.
Binary file removed assets/textures/mcgrasstexture_small.png
Binary file not shown.
Binary file removed assets/textures/mcstonetexture.png
Binary file not shown.
Binary file removed assets/textures/mcstonetexture_small.png
Binary file not shown.
14 changes: 13 additions & 1 deletion src/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ add_library(ve001 STATIC
world_grid.h
)

target_compile_definitions(ve001 PUBLIC ENGINE_TEST)
if (DEFINED ENGINE_TEST)
target_compile_definitions(ve001 PUBLIC ENGINE_TEST)
endif()

target_compile_definitions(ve001 PRIVATE
VE001_SH_CONFIG_ATTRIB_INDEX_POSITION=0
VE001_SH_CONFIG_ATTRIB_INDEX_TEXCOORD=1
VE001_SH_CONFIG_UBO_BINDING_MESHING_DESCRIPTOR=2
VE001_SH_CONFIG_SSBO_BINDING_VOXEL_DATA=5
VE001_SH_CONFIG_SSBO_BINDING_MESHING_TEMP=6
VE001_SH_CONFIG_SSBO_BINDING_MESH_DATA=7
)

target_link_libraries(ve001 PRIVATE
glad::glad
vmath
Expand Down
38 changes: 32 additions & 6 deletions src/engine/chunk_data_streamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
using namespace ve001;
using namespace vmath;

ChunkDataStreamer::ChunkDataStreamer(u32 threads_count, std::unique_ptr<ChunkGenerator> chunk_generator, std::size_t capacity)
: _chunk_generator(std::move(chunk_generator)), _gen_promises(capacity) {
ChunkDataStreamer::ChunkDataStreamer(EngineContext& engine_context, u32 threads_count, std::unique_ptr<ChunkGenerator> chunk_generator, std::size_t capacity) noexcept
: _engine_context(engine_context), _chunk_generator(std::move(chunk_generator)) {

try {
_gen_promises.resize(capacity);
} catch(const std::exception&) {
_engine_context.error |= Error::CPU_ALLOCATION_FAILED;
return;
}

try {
auto final_threads_count = threads_count;
if (final_threads_count == 0U) {
Expand All @@ -14,26 +22,44 @@ ChunkDataStreamer::ChunkDataStreamer(u32 threads_count, std::unique_ptr<ChunkGen
_threads.push_back(std::jthread(&ChunkDataStreamer::thread, this));
}
} catch(const std::exception&) {
_engine_context.error |= Error::CHUNK_DATA_STREAMER_THREAD_ALLOCATION_FAILED;
_done = true;
}
}

void ChunkDataStreamer::thread() {
_chunk_generator->threadInit();
static std::mutex _m;

void ChunkDataStreamer::thread() noexcept {
if (_done) {
return;
}
if (_chunk_generator->threadInit()) {
_done = true;

std::lock_guard<std::mutex> lock(_m);
_engine_context.error |= Error::CHUNK_DATA_STREAMER_THREAD_INITIALIZATION_FAILED;
return;
}

++_ready_counter;

while (!_done) {
Promise promise;
if (_gen_promises.read(promise)) {
// can throw but will never happen in practice
promise.value.set_value(_chunk_generator->gen(promise.position));
} else {
std::this_thread::yield();
}
}
}

std::future<std::optional<std::span<const vmath::u16>>> ChunkDataStreamer::gen(Vec3i32 chunk_position) {
std::future<std::optional<std::span<const vmath::u16>>> ChunkDataStreamer::gen(Vec3i32 chunk_position) noexcept {
// can throw but will never happen in practice
std::promise<std::optional<std::span<const vmath::u16>>> promise;
std::future<std::optional<std::span<const vmath::u16>>> result(promise.get_future());


/// will never return false since size == max chunks count
_gen_promises.write({std::move(promise), chunk_position});

return result;
Expand Down
31 changes: 23 additions & 8 deletions src/engine/chunk_data_streamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
#include <memory>

#include <vmath/vmath.h>
#include "threadsafe_ringbuffer.h"

#include "threadsafe_ringbuffer.h"
#include "engine_context.h"
#include "chunk_generator.h"

namespace ve001 {
Expand All @@ -20,24 +21,38 @@ struct ChunkDataStreamer {
std::promise<std::optional<std::span<const vmath::u16>>> value;
vmath::Vec3i32 position;
};
/// @brief counts number of threads' which are already
/// initialized
std::atomic_int32_t _ready_counter{ 0 };

std::atomic_bool _done;
std::vector<std::jthread> _threads;
ThreadSafeRingBuffer<Promise> _gen_promises;
EngineContext& _engine_context;

/// @brief threads' exit condition
std::atomic_bool _done{ false };
/// @brief allocated threads
std::vector<std::jthread> _threads;
/// @brief promise queue for generation task
ThreadSafeRingBuffer<Promise> _gen_promises{};
/// @brief used chunk data generator ptr
std::unique_ptr<ChunkGenerator> _chunk_generator;

ChunkDataStreamer(vmath::u32 threads_count, std::unique_ptr<ChunkGenerator> chunk_generator, std::size_t capacity);
ChunkDataStreamer(EngineContext& engine_context, vmath::u32 threads_count, std::unique_ptr<ChunkGenerator> chunk_generator, std::size_t capacity) noexcept;

/// @brief threads' entry point
void thread() noexcept;

/// @brief signals if chunk data streamer was fully initialized
/// @return true if chunk data streamer is initialzed
bool ready() const noexcept { return _ready_counter == _threads.size(); }

void thread();
/**
* @brief generates chunk
* @param chunk_position discrete position of the chunk in chunk extents
* @return future to pointer to generated chunk, nullopt if chunk is all 0
*/
std::future<std::optional<std::span<const vmath::u16>>> gen(vmath::Vec3i32 chunk_position);
std::future<std::optional<std::span<const vmath::u16>>> gen(vmath::Vec3i32 chunk_position) noexcept;

~ChunkDataStreamer() { _done = true; }
~ChunkDataStreamer() noexcept { _done = true; }

};

Expand Down
8 changes: 6 additions & 2 deletions src/engine/chunk_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
namespace ve001 {

struct ChunkGenerator {
virtual void threadInit() = 0;
virtual std::optional<std::span<const vmath::u16>> gen(vmath::Vec3i32 chunk_position) = 0;
/// @brief initializes data of the generator thread-wise
/// @return true if initialization failed
virtual bool threadInit() noexcept = 0;
/// @brief generates data at <chunk_position>
/// @return generated data, if fails just return std::nullopt
virtual std::optional<std::span<const vmath::u16>> gen(vmath::Vec3i32 chunk_position) noexcept = 0;
};

}
Expand Down
21 changes: 9 additions & 12 deletions src/engine/chunk_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
using namespace ve001;
using namespace vmath;

#define VE001_SH_CONFIG_ATTRIB_INDEX_POSITION 0
#define VE001_SH_CONFIG_ATTRIB_INDEX_TEXCOORD 1

static void rebindVaoToVbo(u32 vao, u32 vbo) {
static void rebindVaoToVbo(u32 vao, u32 vbo) noexcept {
constexpr u32 vertex_attrib_binding = 0U;

glVertexArrayVertexBuffer(vao, vertex_attrib_binding, vbo, 0, sizeof(Vertex));
}

static void setVertexLayout(u32 vao, u32 vbo) {
static void setVertexLayout(u32 vao, u32 vbo) noexcept {
constexpr u32 vertex_attrib_binding = 0U;

glEnableVertexArrayAttrib(vao, VE001_SH_CONFIG_ATTRIB_INDEX_POSITION);
Expand Down Expand Up @@ -150,7 +147,7 @@ vmath::u32 ChunkPool::allocateChunk(std::span<const vmath::u16> src, Vec3i32 pos
return chunk.chunk_id;
}

void ChunkPool::completeChunk(MeshingEngine::Result result) {
void ChunkPool::completeChunk(MeshingEngine::Result result) noexcept {
const auto chunk_index = _chunk_id_to_index[result.chunk_id];
if (chunk_index == INVALID_CHUNK_INDEX) {
return;
Expand Down Expand Up @@ -188,7 +185,7 @@ void ChunkPool::completeChunk(MeshingEngine::Result result) {
_draw_cmds_dirty = true;
}

void ChunkPool::update(bool use_partition) {
void ChunkPool::update(bool use_partition) noexcept {
if (_draw_cmds.size() > 0U) {
if (use_partition && _draw_cmds_parition_size == 0U) {
return;
Expand All @@ -205,7 +202,7 @@ void ChunkPool::update(bool use_partition) {
}
}

void ChunkPool::recreatePool(MeshingEngine::Result overflow_result) {
void ChunkPool::recreatePool(MeshingEngine::Result overflow_result) noexcept {
const auto max_written_indices_x_axis = std::max(overflow_result.written_indices[X_POS], overflow_result.written_indices[X_NEG]);
const auto max_written_indices_y_axis = std::max(overflow_result.written_indices[Y_POS], overflow_result.written_indices[Y_NEG]);
const auto max_written_indices_z_axis = std::max(overflow_result.written_indices[Z_POS], overflow_result.written_indices[Z_NEG]);
Expand Down Expand Up @@ -252,7 +249,7 @@ void ChunkPool::recreatePool(MeshingEngine::Result overflow_result) {
_meshing_engine.issueMeshingCommand(chunk.chunk_id, chunk.position, chunk.cpu_region);
}

void ChunkPool::drawAll(bool use_partition) {
void ChunkPool::drawAll(bool use_partition) noexcept {
if (_draw_cmds.size() > 0U) {
if (use_partition && _draw_cmds_parition_size == 0U) {
return;
Expand All @@ -266,7 +263,7 @@ void ChunkPool::drawAll(bool use_partition) {
);
}
}
bool ChunkPool::poll() {
bool ChunkPool::poll() noexcept {
if (MeshingEngine::Result result{}; _meshing_engine.pollMeshingCommand(result)) {
completeChunk(result);
return true;
Expand Down Expand Up @@ -310,7 +307,7 @@ void ChunkPool::deallocateChunk(u32 chunk_id) noexcept {
--chunks_used;
#endif
}
void ChunkPool::deallocateChunkDrawCommands(ChunkId chunk_id) {
void ChunkPool::deallocateChunkDrawCommands(ChunkId chunk_id) noexcept {
const auto& chunk = _chunks[_chunk_id_to_index[chunk_id]];
for (u32 i{ 0U }; i < 6; ++i) {
const auto draw_cmd_index = chunk.draw_cmd_indices[i];
Expand All @@ -332,7 +329,7 @@ void ChunkPool::deallocateChunkDrawCommands(ChunkId chunk_id) {
_draw_cmds_dirty = true;
}

void ChunkPool::deinit() {
void ChunkPool::deinit() noexcept {
_meshing_engine.deinit();

glBindVertexArray(0);
Expand Down
19 changes: 9 additions & 10 deletions src/engine/chunk_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,14 @@ struct ChunkPool {

////////////////////////////////////////


/// @brief context holds common data to all engine components
EngineContext& _engine_context;

/// @brief meshing engine of the chunk pool. It schedules meshing
/// tasks on the GPU
MeshingEngine _meshing_engine{ _engine_context, _chunks_count };

ChunkPool(EngineContext& engine_context, vmath::u32 max_chunks) :
ChunkPool(EngineContext& engine_context, vmath::u32 max_chunks) noexcept :
_chunks_count(max_chunks), _engine_context(engine_context) {}
/// @brief initializes chunk pool
/// @param max_chunks number of chunks in a pool
Expand All @@ -178,29 +177,29 @@ struct ChunkPool {
/// @brief completes chunk eg. chunk starts to be drawn by the drawAll command
/// called by poll() function if chunk's mesh is finished
/// @param result holds data from meshing_engine with which to update the chunk
void completeChunk(MeshingEngine::Result result);
void completeChunk(MeshingEngine::Result result) noexcept;
/// @brief deallocates chunk
/// @param chunk_id chunk's id to deallocate
void deallocateChunk(ChunkId chunk_id) noexcept;
/// @brief deallocates draw commands of the chunk. Called by deallocateChunk only
/// if deallocated chunk is complete
/// @param chunk_id chunk's id from which to deallocate draw commands
void deallocateChunkDrawCommands(ChunkId chunk_id);
void deallocateChunkDrawCommands(ChunkId chunk_id) noexcept;
/// @brief updates the state. Update draw command buffer binds vbo as vertex buffer, binds vao
/// @param use_partition commands will be supplied based on last paritioning call (paritionDrawCmds)
void update(bool use_partition);
void update(bool use_partition) noexcept;
/// @brief draws all chunks
/// @param use_partition number of draw commands will be based on last paritioning call (paritionDrawCmds)
void drawAll(bool use_partition);
void drawAll(bool use_partition) noexcept;
/// @brief recreates chunk pool based on the meshing result which caused overflow
/// @param overflow_result meshing result which contains info about overflow
void recreatePool(MeshingEngine::Result overflow_result);
void recreatePool(MeshingEngine::Result overflow_result) noexcept;

/// @brief polls for chunks that are meshed and are ready to be completed (one at a time)
/// @return true if chunk was completed false otherwise
bool poll();
bool poll() noexcept;
/// @brief deinitializes chunk pool
void deinit();
void deinit() noexcept;


/// @brief function paritions the _draw_cmds based on <unary_op> setting the <_draw_cmds_parition_size> member
Expand All @@ -209,7 +208,7 @@ struct ChunkPool {
/// @param use_last_partition If true then the previous parition will be paritioned again
/// @param args Aux arguments to pass to unary_op function
template<typename ...Args>
void partitionDrawCommands(bool(*unary_op)(Face orientation, vmath::Vec3f32 position, Args... args), bool use_last_partition, Args... args) {
void partitionDrawCommands(bool(*unary_op)(Face orientation, vmath::Vec3f32 position, Args... args), bool use_last_partition, Args... args) noexcept {
std::size_t begin{ 0UL };
std::size_t end{ use_last_partition ? _draw_cmds_parition_size - 1UL : _draw_cmds.size() - 1UL };

Expand Down
20 changes: 10 additions & 10 deletions src/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ static bool frustumCullingUnaryOp(
f32 x_near,
f32 y_near,
Mat4f32 view_matrix
);
) noexcept ;


Engine::Engine(Config config) : _engine_context(EngineContext{
Engine::Engine(Config config) noexcept : _engine_context(EngineContext{
.error = this->error,
.chunk_size = config.chunk_size,
.half_chunk_size = Vec3i32::divScalar(config.chunk_size, 2),
Expand All @@ -38,11 +38,11 @@ Engine::Engine(Config config) : _engine_context(EngineContext{
_world_grid(_engine_context, config.world_size, config.initial_position, config.chunk_data_streamer_threads_count, std::move(config.chunk_data_generator))
{}

bool Engine::init() {
bool Engine::init() noexcept {
_world_grid.init();
return (error != Error::NO_ERROR);
}
void Engine::deinit() {
void Engine::deinit() noexcept {
_world_grid.deinit();
}

Expand All @@ -52,7 +52,7 @@ void Engine::applyFrustumCullingPartition(
f32 z_far,
f32 x_near,
f32 y_near,
Mat4f32 view_matrix) {
Mat4f32 view_matrix) noexcept {

_world_grid._chunk_pool.partitionDrawCommands(
frustumCullingUnaryOp,
Expand All @@ -66,16 +66,16 @@ void Engine::applyFrustumCullingPartition(
);
}

void Engine::updateCameraPosition(Vec3f32 position) {
void Engine::updateCameraPosition(Vec3f32 position) noexcept {
_world_grid.update(position);
}
bool Engine::pollChunksUpdates() {
bool Engine::pollChunksUpdates() noexcept {
return _world_grid._chunk_pool.poll();
}
void Engine::updateDrawState() {
void Engine::updateDrawState() noexcept {
_world_grid._chunk_pool.update(partitioning);
}
void Engine::draw() {
void Engine::draw() noexcept {
_world_grid._chunk_pool.drawAll(partitioning);
}

Expand All @@ -88,7 +88,7 @@ bool frustumCullingUnaryOp(
f32 x_near,
f32 y_near,
Mat4f32 view_matrix
) {
) noexcept {
// center position in view space
const auto position_in_view_space = Mat4f32::mulVec(view_matrix, {position[0], position[1], position[2], 1.F});

Expand Down
Loading

0 comments on commit 3f67479

Please sign in to comment.