From b4ed5983d351382b927b91165e8f2966929c048e Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Wed, 7 Apr 2021 17:03:09 +0200 Subject: [PATCH] Some changes following the code review --- Makefile | 7 +- include/gp/ipc/filesystem.hpp | 6 +- include/gp/math/rendering/renderer.hpp | 211 +++++++++++---------- include/gp/system/platforms/gcc-x86_64.hpp | 2 +- include/gp/system/task_queue.hpp | 2 - include/gp_config.hpp | 2 +- tests/math.cpp | 11 +- 7 files changed, 125 insertions(+), 116 deletions(-) diff --git a/Makefile b/Makefile index ad08234..42b48cd 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ -CXX= clang++ -CXXFLAGS= --std=c++20 -O2 -g -pthread -DGP_TESTS -DFUZZ_STRENGTH=100000 -DNO_BENCH=0 -pedantic \ - -fprofile-instr-generate -fcoverage-mapping -Wno-unknown-attributes -fno-omit-frame-pointer \ +CXX= g++ +CXXFLAGS= --std=c++20 -O0 -g -pthread -DGP_TESTS -DFUZZ_STRENGTH=100000 -DNO_BENCH=0 -pedantic \ + +# -fprofile-instr-generate -fcoverage-mapping -Wno-unknown-attributes -fno-omit-frame-pointer \ EVERY_USEFUL_FILE= $(shell find include/ -name "*.hpp" -type "f") EVERY_TEST_FILE= $(shell find tests/ -name "*.cpp" -type "f") diff --git a/include/gp/ipc/filesystem.hpp b/include/gp/ipc/filesystem.hpp index 6771304..33cf94c 100644 --- a/include/gp/ipc/filesystem.hpp +++ b/include/gp/ipc/filesystem.hpp @@ -17,8 +17,8 @@ namespace gp{ }; class filesystem { - virtual file_description create(gp::buffer, fs_user_representation&) = 0; - virtual file_description open(gp::buffer, fs_user_representation&) = 0; - virtual file_description remove(gp::buffer, fs_user_representation&) = 0; + virtual file_description* create(gp::buffer, fs_user_representation&) = 0; + virtual file_description* open(gp::buffer, fs_user_representation&) = 0; + virtual file_description* remove(gp::buffer, fs_user_representation&) = 0; }; } \ No newline at end of file diff --git a/include/gp/math/rendering/renderer.hpp b/include/gp/math/rendering/renderer.hpp index 07bab33..b32edd1 100644 --- a/include/gp/math/rendering/renderer.hpp +++ b/include/gp/math/rendering/renderer.hpp @@ -11,107 +11,114 @@ // TODO: Namespace this correctly -using vec2 = gp::math::vec2_g<>; -using vec3 = gp::math::vec3_g<>; -using vec4 = gp::math::vec4_g<>; - -struct camera{ - vec3 position; - vec3 normal; -}; - -using index_t = size_t; -using distance_t = gp_config::rendering::default_type; -using color_t = GP_CONFIG__RENDERING__COLOR_T; - -struct render_point{ - distance_t distance; - index_t material; - - bool operator<(const render_point& rhs) { - return distance < rhs.distance; - } -}; - -using sdf_t = gp::function; -using material_t = gp::function; - - -/** - * @brief A pure ray-marching renderer. Prints pixels on order. - */ -class renderer { - using g_t = gp_config::rendering::default_type; - constexpr static auto epsilon = gp_config::rendering::epsilon; -public: - gp::indexed_array scene_elements; - gp::indexed_array materials; - gp::buddy<> allocator; - material_t sky_box; - vec2 _resolution{128,64}; - camera _camera{{0, 0, -1}, {0, 0, 0}}; - vec2 _fov{90, 45}; - distance_t projection_start = 1; - distance_t projection_end = 50; - size_t passes = 12; - - renderer(gp::buffer allocation_buffer) - : allocator(allocation_buffer.begin().data, allocation_buffer.size()) - , sky_box{gp::reference_wrapper>{allocator}} - {} - - render_point sdf(vec3& render_target) { - return gp::min_of( - scene_elements.begin(), - scene_elements.end(), - [&](auto& p){ - return p(render_target); - } - ); - } - - auto& get_allocator() { - return allocator; - } - - color_t render(vec2 pixel) { - g_t depth = projection_start; - vec3 target = _camera.normal; - auto half_res = _resolution/vec2{2.0, 2.0}; - pixel = pixel - half_res; - pixel = pixel / half_res; - pixel = pixel * _fov; - pixel = pixel / vec2{180, 180} * vec2{gp::math::pi,gp::math::pi}; - - // Y-rot (adjusts x) - target = vec3{ - target.x*gp::math::cos(pixel.x) + target.z*gp::math::sin(pixel.x), - target.y, - -target.x*gp::math::sin(pixel.x)+target.z*gp::math::cos(pixel.x) - }; - // X-rot (adjusts y) - target = vec3{ - target.x, - target.y*gp::math::cos(pixel.y) - target.z*gp::math::sin(pixel.y), - target.y*gp::math::sin(pixel.y) + target.z*gp::math::cos(pixel.y) - }; - - vec3 render_target{_camera.position}; - for(int i = 1; i < passes; ++i) { - render_point distance = sdf(render_target); - - if(distance.distance < epsilon) { - return materials[distance.material](render_target); - } - - depth += distance.distance; - - if(depth >= projection_end) { - break; - } - - render_target = _camera.position+depth*target; +namespace gp { + namespace math { + namespace rendering { + + using vec2 = gp::math::vec2_g<>; + using vec3 = gp::math::vec3_g<>; + using vec4 = gp::math::vec4_g<>; + + struct camera{ + vec3 position; + vec3 normal; + }; + + using index_t = size_t; + using distance_t = gp_config::rendering::default_type; + using color_t = GP_CONFIG__RENDERING__COLOR_T; + + struct render_point{ + distance_t distance; + index_t material; + + bool operator<(const render_point& rhs) { + return distance < rhs.distance; + } + }; + + using sdf_t = gp::function; + using material_t = gp::function; + + + /** + * @brief A pure ray-marching renderer. Prints pixels on order. + */ + class renderer { + using g_t = gp_config::rendering::default_type; + constexpr static auto epsilon = gp_config::rendering::epsilon; + public: + gp::indexed_array scene_elements; + gp::indexed_array materials; + gp::buddy<> allocator; + material_t sky_box; + vec2 _resolution{128,64}; + camera _camera{{0, 0, -5}, {0, 0, 0}}; + vec2 _fov{90, 45}; + distance_t projection_start = 1; + distance_t projection_end = 50; + size_t passes = 12; + + renderer(gp::buffer allocation_buffer) + : allocator(allocation_buffer.begin().data, allocation_buffer.size()) + , sky_box{gp::reference_wrapper>{allocator}} + {} + + render_point sdf(vec3& render_target) { + return gp::min_of( + scene_elements.begin(), + scene_elements.end(), + [&](auto& p){ + return p(render_target); + } + ); + } + + auto& get_allocator() { + return allocator; + } + + color_t render(vec2 pixel) { + g_t depth = projection_start; + vec3 target = _camera.normal; + auto half_res = _resolution/vec2{2.0, 2.0}; + pixel = pixel - half_res; + pixel = pixel / half_res; + pixel = pixel * _fov; + pixel = pixel / vec2{180, 180} * vec2{gp::math::pi,gp::math::pi}; + + // Y-rot (adjusts x) + target = vec3{ + target.x*gp::math::cos(pixel.x) + target.z*gp::math::sin(pixel.x), + target.y, + -target.x*gp::math::sin(pixel.x)+target.z*gp::math::cos(pixel.x) + }; + // X-rot (adjusts y) + target = vec3{ + target.x, + target.y*gp::math::cos(pixel.y) - target.z*gp::math::sin(pixel.y), + target.y*gp::math::sin(pixel.y) + target.z*gp::math::cos(pixel.y) + }; + + vec3 render_target{_camera.position}; + for(int i = 0; i < passes; ++i) { + render_point distance = sdf(render_target); + + if(distance.distance < epsilon) { + return materials[distance.material](render_target); + } + + depth += distance.distance; + + if(depth >= projection_end) { + break; + } + + render_target = _camera.position+depth*target; + } + return sky_box(render_target); + } + }; } - return sky_box(render_target); } -}; \ No newline at end of file +} \ No newline at end of file diff --git a/include/gp/system/platforms/gcc-x86_64.hpp b/include/gp/system/platforms/gcc-x86_64.hpp index 4be75de..c3d3205 100644 --- a/include/gp/system/platforms/gcc-x86_64.hpp +++ b/include/gp/system/platforms/gcc-x86_64.hpp @@ -4,7 +4,7 @@ #include -#define no_inline_decl(a) a __attribute__((noinline)) +#define no_inline_decl(a) __attribute__((noinline)) a namespace gp{ namespace system { diff --git a/include/gp/system/task_queue.hpp b/include/gp/system/task_queue.hpp index 241b88f..99de013 100644 --- a/include/gp/system/task_queue.hpp +++ b/include/gp/system/task_queue.hpp @@ -5,8 +5,6 @@ #include #include -// TODO: noexcept everything - namespace gp { namespace system { struct task_queue{ diff --git a/include/gp_config.hpp b/include/gp_config.hpp index 9816aef..d850838 100644 --- a/include/gp_config.hpp +++ b/include/gp_config.hpp @@ -36,7 +36,7 @@ namespace gp_config{ /** * @brief The small enough value used in signed distance function resolution */ - constexpr default_type epsilon = 0.001; + constexpr default_type epsilon = 0.01; /** * @brief The default color type diff --git a/tests/math.cpp b/tests/math.cpp index 57a7a18..af47f47 100644 --- a/tests/math.cpp +++ b/tests/math.cpp @@ -49,6 +49,8 @@ struct render_test : public test_scaffold { virtual int run() { + using namespace gp::math::rendering; + int res = 0; gp::array allocation_buffer; renderer a{allocation_buffer.as_buffer()}; @@ -85,7 +87,7 @@ struct render_test : public test_scaffold { }, a.get_allocator()) ); - volatile auto sphere = a.scene_elements.push( + auto sphere = a.scene_elements.push( sdf_t([&](vec3 pos) -> render_point { auto l_sdf = gp::math::difference_sdf( gp::math::sphere_sdf({0.0,0.0,0.0}, 1.0), @@ -98,9 +100,9 @@ struct render_test : public test_scaffold { }, a.get_allocator()) ); - volatile auto sphere2 = a.scene_elements.push( + auto sphere2 = a.scene_elements.push( sdf_t([&](vec3 pos) -> render_point { - auto l_sdf_b = gp::math::sphere_sdf({-0.75,0.0,0.0}, 1.0); + auto l_sdf_b = gp::math::sphere_sdf({-0.75,0.0,0.0}, 0.05); render_point ret; ret.distance = l_sdf_b(pos); ret.material = green; @@ -108,7 +110,7 @@ struct render_test : public test_scaffold { }, a.get_allocator()) ); - a._camera.position = vec3{0, 0, -2}; + a._camera.position = vec3{0, 0, -5}; a._camera.normal = vec3{0, 0, 1}; using pic_color = gp::math::vec4_g; @@ -159,6 +161,7 @@ struct function_test : public test_scaffold { virtual int run() { + using namespace gp::math::rendering; int res = 0; gp::array allocation_buffer;