Skip to content

Commit

Permalink
testing context dumps per frame data to files
Browse files Browse the repository at this point in the history
  • Loading branch information
JungerBoyo authored and JungerBoyo committed Dec 13, 2023
1 parent 50908ae commit e457b53
Show file tree
Hide file tree
Showing 20 changed files with 427 additions and 252 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ conanprofile.dbg.txt
.ninja_log
ve001_log
maps/
ve001_frame_samples.csv
ve001_meshing_samples.csv
6 changes: 1 addition & 5 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ class VE001Recipe(ConanFile):
build_policy = "missing"

settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps"

def generate(self):
tc = CMakeToolchain(self, generator="Ninja")
tc.generate()
generators = "CMakeDeps, CMakeToolchain"

def config_options(self):
self.options["glad"].spec = "gl"
Expand Down
Binary file modified shaders/bin/basic_test_shader/frag.spv
Binary file not shown.
13 changes: 13 additions & 0 deletions shaders/src/basic_test_shader/shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ layout(std140, binding = 0) uniform General {
vec3 camera_pos;
};

const vec3 color_palette[9] = {
vec3(0.0, 0.0, 0.5),
vec3(0.0, 0.0, 1.0),
vec3(0.0, 0.5, 1.0),
vec3(0.0, 1.0, 1.0),
vec3(0.5, 1.0, 0.5),
vec3(1.0, 1.0, 0.0),
vec3(1.0, 0.5, 0.0),
vec3(1.0, 0.0, 0.0),
vec3(0.5, 0.0, 0.0),
};
/*
const vec3 color_palette[257] = {
vec3 (0.2298057, 0.298717966, 0.753683153),
vec3 (0.234299935, 0.305559204, 0.759874796),
Expand Down Expand Up @@ -272,6 +284,7 @@ const vec3 color_palette[257] = {
vec3 (0.711724383, 0.043755173, 0.154324339),
vec3 (0.705673158, 0.01555616, 0.150232812),
};
*/

const vec3 LIGHT_DIR = vec3(0.5, -0.5, 0.5);
const vec3 AMBIENT = vec3(0.33, 0.3, 0.3);
Expand Down
6 changes: 3 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ find_package(glad REQUIRED)
find_package(spdlog REQUIRED)
find_package(FastNoise2 REQUIRED)
find_package(CLI11 REQUIRED)

link_libraries(FastNoise2::FastNoise)

find_package(glfw3 REQUIRED)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
Expand All @@ -17,6 +14,9 @@ add_subdirectory(logger)
add_library(gl_context STATIC gl_context.h gl_context.cpp)
target_link_libraries(gl_context PUBLIC glad::glad)
target_link_libraries(gl_context PRIVATE glfw)
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
target_compile_definitions(gl_context PRIVATE DEBUG)
endif()

target_link_libraries(gl_context PRIVATE logger)

Expand Down
1 change: 1 addition & 0 deletions src/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ add_library(ve001 STATIC
world_grid.cpp
world_grid.h
)
target_compile_definitions(ve001 PUBLIC ENGINE_TEST)
target_link_libraries(ve001 PRIVATE
glad::glad
vmath
Expand Down
11 changes: 10 additions & 1 deletion src/engine/chunk_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ vmath::u32 ChunkPool::allocateChunk(std::span<const vmath::u16> src, Vec3i32 pos

std::memcpy(static_cast<void*>(free_chunk.cpu_region.data()), static_cast<const void*>(src.data()), src.size() * sizeof(u16));

#ifdef ENGINE_TEST
cpu_memory_usage += src.size() * sizeof(u16);
#endif

auto& chunk = _chunks.emplace_back(Chunk{
Vec3f32::cast(Vec3i32::sub(Vec3i32::mul(position, _engine_context.chunk_size), _engine_context.half_chunk_size)),
Expand All @@ -123,7 +125,9 @@ vmath::u32 ChunkPool::allocateChunk(std::span<const vmath::u16> src, Vec3i32 pos

_meshing_engine.issueMeshingCommand(chunk.chunk_id, chunk.position, chunk.cpu_region);

#ifdef ENGINE_TEST
++chunks_used;
#endif

return chunk.chunk_id;
}
Expand Down Expand Up @@ -159,7 +163,9 @@ void ChunkPool::completeChunk(MeshingEngine::Result result) {
.orientation = static_cast<Face>(i),
.chunk_id = result.chunk_id
});
#ifdef ENGINE_TEST
gpu_memory_usage += static_cast<u64>(draw_cmd.count/6) * sizeof(Vertex) * 4;
#endif
}
_draw_cmds_dirty = true;
}
Expand Down Expand Up @@ -274,14 +280,17 @@ void ChunkPool::deallocateChunk(u32 chunk_id) noexcept {
.cpu_region = chunk.cpu_region
});
_chunks.pop_back();

#ifdef ENGINE_TEST
--chunks_used;
#endif
}
void ChunkPool::deallocateChunkDrawCommands(ChunkId chunk_id) {
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];
#ifdef ENGINE_TEST
gpu_memory_usage -= static_cast<u64>(_draw_cmds[draw_cmd_index].count/6) * sizeof(Vertex) * 4;
#endif
if (draw_cmd_index != _draw_cmds.size() - 1U) {
auto& last_draw_cmd = _draw_cmds.back();
const auto last_draw_cmd_chunk_index = _chunk_id_to_index[last_draw_cmd.chunk_id];
Expand Down
4 changes: 3 additions & 1 deletion src/engine/chunk_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ struct ChunkPool {
/// METADATA (DEBUG/INFO) ///
////////////////////////////////////////

#ifdef ENGINE_TEST
/// @brief gpu memory usage in bytes (mesh)
vmath::u64 gpu_memory_usage{ 0UL };
/// @brief cpu memory usage in bytes (voxel values)
vmath::u64 cpu_memory_usage{ 0UL };
/// @brief gpu region usage
vmath::u64 chunks_used{ 0UL };

#endif

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

/// @brief context holds common data to all engine components
Expand Down
70 changes: 39 additions & 31 deletions src/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void Engine::applyFrustumCullingPartition(
_world_grid._chunk_pool.partitionDrawCommands(
frustumCullingUnaryOp,
use_last_partition,
Vec3f32::cast(_engine_context.chunk_size),
Vec3f32::cast(_engine_context.half_chunk_size),
z_near,
z_far,
x_near,
Expand All @@ -80,7 +80,7 @@ void Engine::draw() {
bool frustumCullingUnaryOp(
Face orientation,
Vec3f32 position,
Vec3f32 chunk_size,
Vec3f32 half_chunk_size,
f32 z_near,
f32 z_far,
f32 x_near,
Expand All @@ -90,11 +90,21 @@ bool frustumCullingUnaryOp(
// center position in view space
const auto position_in_view_space = Mat4f32::mulVec(view_matrix, {position[0], position[1], position[2], 1.F});

const Vec4f32 axes[] = {
Mat4f32::mulVec(view_matrix, {1.F, 0.F, 0.F, 0.F}),
Mat4f32::mulVec(view_matrix, {0.F, 1.F, 0.F, 0.F}),
Mat4f32::mulVec(view_matrix, {0.F, 0.F, 1.F, 0.F})
};
const auto chunk_min = Vec3f32::add(position, half_chunk_size);
const auto chunk_max = Vec3f32::sub(position, half_chunk_size);

const std::array<Vec4f32, 4> corners{{
Mat4f32::mulVec(view_matrix, {chunk_min[0], chunk_min[1], chunk_min[2], 1.F}),
Mat4f32::mulVec(view_matrix, {chunk_max[0], chunk_min[1], chunk_min[2], 1.F}),
Mat4f32::mulVec(view_matrix, {chunk_min[0], chunk_max[1], chunk_min[2], 1.F}),
Mat4f32::mulVec(view_matrix, {chunk_min[0], chunk_min[1], chunk_max[2], 1.F}),
}};

const std::array<Vec3f32, 3> axes{{
Vec3f32::normalize(Vec3f32::sub({corners[1][0], corners[1][1], corners[1][2]}, {corners[0][0], corners[0][1], corners[0][2]})),
Vec3f32::normalize(Vec3f32::sub({corners[2][0], corners[2][1], corners[2][2]}, {corners[0][0], corners[0][1], corners[0][2]})),
Vec3f32::normalize(Vec3f32::sub({corners[3][0], corners[3][1], corners[3][2]}, {corners[0][0], corners[0][1], corners[0][2]})),
}};

// determine if chunk is behind z_near plane or beyond z_far plane
{
Expand All @@ -103,18 +113,17 @@ bool frustumCullingUnaryOp(
const auto projected_center = position_in_view_space[2];

const auto radius =
fabsf(axes[0][2]) * (chunk_size[0]/2.F) +
fabsf(axes[1][2]) * (chunk_size[1]/2.F) +
fabsf(axes[2][2]) * (chunk_size[2]/2.F);
fabsf(axes[0][2]) * (half_chunk_size[0]) +
fabsf(axes[1][2]) * (half_chunk_size[1]) +
fabsf(axes[2][2]) * (half_chunk_size[2]);

const auto min = projected_center - radius;
const auto max = projected_center + radius;

if (max > z_near || min < z_far) {
if (min > z_near || max < z_far) {
return false;
}
}

// determine if chunk is beyond the rest of the frustum planes
{
const Vec3f32 projection_lines[4] {
Expand All @@ -126,9 +135,9 @@ bool frustumCullingUnaryOp(
for (std::size_t i{ 0U }; i < 4U; ++i) {
const auto projected_center = Vec3f32::dot(projection_lines[i], { position_in_view_space[0], position_in_view_space[1], position_in_view_space[2] });
const auto radius =
fabsf(Vec3f32::dot(projection_lines[i], {axes[0][0], axes[0][1], axes[0][2]})) * (chunk_size[0]/2.F) +
fabsf(Vec3f32::dot(projection_lines[i], {axes[1][0], axes[1][1], axes[1][2]})) * (chunk_size[1]/2.F) +
fabsf(Vec3f32::dot(projection_lines[i], {axes[2][0], axes[2][1], axes[2][2]})) * (chunk_size[2]/2.F);
fabsf(Vec3f32::dot(projection_lines[i], axes[0])) * half_chunk_size[0] +
fabsf(Vec3f32::dot(projection_lines[i], axes[1])) * half_chunk_size[1] +
fabsf(Vec3f32::dot(projection_lines[i], axes[2])) * half_chunk_size[2];

const auto min = projected_center - radius;
const auto max = projected_center + radius;
Expand All @@ -152,8 +161,8 @@ bool frustumCullingUnaryOp(
}
// determine if frustum is inside of the chunk
for (std::size_t i{ 0U }; i < 3U; ++i) {
const auto projected_center = Vec4f32::dot(axes[i], position_in_view_space);
const auto radius = chunk_size[i]/2.F;
const auto projected_center = Vec3f32::dot(axes[i], { position_in_view_space[0], position_in_view_space[1], position_in_view_space[2] });
const auto radius = half_chunk_size[i];

const auto min = projected_center - radius;
const auto max = projected_center + radius;
Expand Down Expand Up @@ -186,9 +195,9 @@ bool frustumCullingUnaryOp(
projection_line[2] * position_in_view_space[2];

const auto radius =
fabsf(Vec3f32::dot(projection_line, {axes[0][0], axes[0][1], axes[0][2]})) * (chunk_size[0]/2.F) +
fabsf(Vec3f32::dot(projection_line, {axes[1][0], axes[1][1], axes[1][2]})) * (chunk_size[1]/2.F) +
fabsf(Vec3f32::dot(projection_line, {axes[2][0], axes[2][1], axes[2][2]})) * (chunk_size[0]/2.F);
fabsf(Vec3f32::dot(projection_line, axes[0])) * half_chunk_size[0] +
fabsf(Vec3f32::dot(projection_line, axes[1])) * half_chunk_size[1] +
fabsf(Vec3f32::dot(projection_line, axes[2])) * half_chunk_size[0];

const auto min = projected_center - radius;
const auto max = projected_center + radius;
Expand Down Expand Up @@ -218,9 +227,9 @@ bool frustumCullingUnaryOp(
projection_line[2] * position_in_view_space[2];

const auto radius =
fabsf(Vec3f32::dot(projection_line, {axes[0][0], axes[0][1], axes[0][2]})) * (chunk_size[0]/2.F) +
fabsf(Vec3f32::dot(projection_line, {axes[1][0], axes[1][1], axes[1][2]})) * (chunk_size[1]/2.F) +
fabsf(Vec3f32::dot(projection_line, {axes[2][0], axes[2][1], axes[2][2]})) * (chunk_size[0]/2.F);
fabsf(Vec3f32::dot(projection_line, axes[0])) * half_chunk_size[0] +
fabsf(Vec3f32::dot(projection_line, axes[1])) * half_chunk_size[1] +
fabsf(Vec3f32::dot(projection_line, axes[2])) * half_chunk_size[0];

const auto min = projected_center - radius;
const auto max = projected_center + radius;
Expand All @@ -242,12 +251,11 @@ bool frustumCullingUnaryOp(

// cross(frustum edges, axes[0,1,2])
for (std::size_t i{ 0U }; i < 3; ++i) {
const auto axis = Vec3f32{axes[i][0], axes[i][1], axes[i][2]};
const Vec3f32 projection_lines[4] = {
vmath::cross({-x_near, 0.F, z_near }, axis), // Left Plane
vmath::cross({ x_near, 0.F, z_near }, axis), // Right plane
vmath::cross({ 0.F, y_near, z_near }, axis), // Top plane
vmath::cross({ 0.F,-y_near, z_near }, axis) // Bottom plane
vmath::cross({-x_near, 0.F, z_near }, axes[i]), // Left Plane
vmath::cross({ x_near, 0.F, z_near }, axes[i]), // Right plane
vmath::cross({ 0.F, y_near, z_near }, axes[i]), // Top plane
vmath::cross({ 0.F,-y_near, z_near }, axes[i]) // Bottom plane
};

for (std::size_t j{ 0U }; j < 4; ++j) {
Expand All @@ -261,9 +269,9 @@ bool frustumCullingUnaryOp(
const auto projected_center = Vec3f32::dot(projection_lines[j], {position_in_view_space[0], position_in_view_space[1], position_in_view_space[2]});

const auto radius =
fabsf(Vec3f32::dot(projection_lines[j], { axes[0][0], axes[0][1], axes[0][2] })) * (chunk_size[0]/2.F) +
fabsf(Vec3f32::dot(projection_lines[j], { axes[1][0], axes[1][1], axes[1][2] })) * (chunk_size[1]/2.F) +
fabsf(Vec3f32::dot(projection_lines[j], { axes[2][0], axes[2][1], axes[2][2] })) * (chunk_size[2]/2.F);
fabsf(Vec3f32::dot(projection_lines[j], axes[0])) * half_chunk_size[0] +
fabsf(Vec3f32::dot(projection_lines[j], axes[1])) * half_chunk_size[1] +
fabsf(Vec3f32::dot(projection_lines[j], axes[2])) * half_chunk_size[2];

const auto min = projected_center - radius;
const auto max = projected_center + radius;
Expand Down
4 changes: 2 additions & 2 deletions src/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ struct Engine {
/// @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 applyCustomPartition(bool(*unary_op)(Face orientation, vmath::Vec3f32 position, Args... args), bool use_last_partition, Args&&... args) {
_world_grid._chunk_pool.partitionDrawCommands(unary_op, use_last_partition, std::forward<Args>(args)...);
void applyCustomPartition(bool(*unary_op)(Face orientation, vmath::Vec3f32 position, Args... args), bool use_last_partition, Args... args) {
_world_grid._chunk_pool.partitionDrawCommands(unary_op, use_last_partition, args...);
}

/// @brief initalization of the opengl resources
Expand Down
17 changes: 17 additions & 0 deletions src/engine/meshing_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ void MeshingEngine::init(u32 vbo_id) {
} else {
_meshing_shader.attach(_engine_context.meshing_shader_src_path, false);
}
#ifdef ENGINE_TEST
glCreateQueries(GL_TIMESTAMP, 1, &meshing_time_query);
#endif
}

void MeshingEngine::issueMeshingCommand(u32 chunk_id, Vec3f32 chunk_position, std::span<const u16> voxel_data) {
Expand Down Expand Up @@ -125,6 +128,12 @@ bool MeshingEngine::pollMeshingCommand(Result& result) {
return false;
}

#ifdef ENGINE_TEST
glQueryCounter(meshing_time_query, GL_TIMESTAMP);
glGetQueryObjectui64v(meshing_time_query, GL_QUERY_RESULT, &end_meshing_time_ns);
result_meshing_time_ns = end_meshing_time_ns - begin_meshing_time_ns;
#endif

glDeleteSync(static_cast<GLsync>(_active_command.fence));
_active_command.fence = nullptr;

Expand Down Expand Up @@ -197,6 +206,11 @@ void MeshingEngine::updateMetadata(vmath::u32 new_vbo_id) {
}

void MeshingEngine::firstCommandExec(Command& command) {
#ifdef ENGINE_TEST
glQueryCounter(meshing_time_query, GL_TIMESTAMP);
glGetQueryObjectui64v(meshing_time_query, GL_QUERY_RESULT, &begin_meshing_time_ns);
#endif

std::memcpy(_ssbo_voxel_data_ptr, static_cast<const void*>(command.voxel_data.data()), _engine_context.chunk_voxel_data_size);

Temp meshing_temp{};
Expand Down Expand Up @@ -233,6 +247,9 @@ void MeshingEngine::subsequentCommandExec(Command& command) {
}

void MeshingEngine::deinit() {
#ifdef ENGINE_TEST
glDeleteQueries(1, &meshing_time_query);
#endif
_meshing_shader.deinit();
if (_ssbo_voxel_data_ptr != nullptr) {
glUnmapNamedBuffer(_ssbo_voxel_data_id);
Expand Down
7 changes: 7 additions & 0 deletions src/engine/meshing_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ struct MeshingEngine {
/// @brief greedy meshing shader handle
Shader _meshing_shader;

#ifdef ENGINE_TEST
vmath::u64 begin_meshing_time_ns{ 0UL };
vmath::u64 end_meshing_time_ns{ 0UL };
vmath::u64 result_meshing_time_ns{ 0UL };
vmath::u32 meshing_time_query{ 0U };
#endif

const EngineContext& _engine_context;

MeshingEngine(const EngineContext& engine_context, vmath::u32 max_chunks)
Expand Down
3 changes: 0 additions & 3 deletions src/engine/world_grid.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include "world_grid.h"

#include <iostream>
#include <FastNoise/FastNoise.h>

using namespace vmath;
using namespace ve001;

Expand Down
7 changes: 1 addition & 6 deletions src/gl_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,4 @@ void ve001::glInit() {
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(glDebugCallback, nullptr);
#endif
}

void ve001::setGLDebugCallback(void (*gl_error_callback)(unsigned int, unsigned int, unsigned int, unsigned int, int, const char *, const void *)) {
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(gl_error_callback, nullptr);
}
}
13 changes: 0 additions & 13 deletions src/gl_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,6 @@ namespace ve001 {

void glInit();

#ifndef DEBUG
void setGLDebugCallback(
void (*gl_error_callback)(
unsigned int,
unsigned int,
unsigned int,
unsigned int,
int,
const char *,
const void *)
);
#endif

}

#endif
Loading

0 comments on commit e457b53

Please sign in to comment.