Quellcode durchsuchen

Some changes following the code review

channel
Ludovic 'Archivist' Lagouardette vor 3 Jahren
Ursprung
Commit
b4ed5983d3
7 geänderte Dateien mit 125 neuen und 116 gelöschten Zeilen
  1. +4
    -3
      Makefile
  2. +3
    -3
      include/gp/ipc/filesystem.hpp
  3. +109
    -102
      include/gp/math/rendering/renderer.hpp
  4. +1
    -1
      include/gp/system/platforms/gcc-x86_64.hpp
  5. +0
    -2
      include/gp/system/task_queue.hpp
  6. +1
    -1
      include/gp_config.hpp
  7. +7
    -4
      tests/math.cpp

+ 4
- 3
Makefile Datei anzeigen

@ -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")

+ 3
- 3
include/gp/ipc/filesystem.hpp Datei anzeigen

@ -17,8 +17,8 @@ namespace gp{
};
class filesystem {
virtual file_description create(gp::buffer<file_char>, fs_user_representation&) = 0;
virtual file_description open(gp::buffer<file_char>, fs_user_representation&) = 0;
virtual file_description remove(gp::buffer<file_char>, fs_user_representation&) = 0;
virtual file_description* create(gp::buffer<file_char>, fs_user_representation&) = 0;
virtual file_description* open(gp::buffer<file_char>, fs_user_representation&) = 0;
virtual file_description* remove(gp::buffer<file_char>, fs_user_representation&) = 0;
};
}

+ 109
- 102
include/gp/math/rendering/renderer.hpp Datei anzeigen

@ -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<render_point(vec3&)>;
using material_t = gp::function<color_t(vec3&)>;
/**
* @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<sdf_t, 4096> scene_elements;
gp::indexed_array<material_t, 4096> 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<char> allocation_buffer)
: allocator(allocation_buffer.begin().data, allocation_buffer.size())
, sky_box{gp::reference_wrapper<gp::buddy<>>{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<g_t>,gp::math::pi<g_t>};
// 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<render_point(vec3&)>;
using material_t = gp::function<color_t(vec3&)>;
/**
* @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<sdf_t, 4096> scene_elements;
gp::indexed_array<material_t, 4096> 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<char> allocation_buffer)
: allocator(allocation_buffer.begin().data, allocation_buffer.size())
, sky_box{gp::reference_wrapper<gp::buddy<>>{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<g_t>,gp::math::pi<g_t>};
// 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);
}
};
}

+ 1
- 1
include/gp/system/platforms/gcc-x86_64.hpp Datei anzeigen

@ -4,7 +4,7 @@
#include <cstdint>
#define no_inline_decl(a) a __attribute__((noinline))
#define no_inline_decl(a) __attribute__((noinline)) a
namespace gp{
namespace system {

+ 0
- 2
include/gp/system/task_queue.hpp Datei anzeigen

@ -5,8 +5,6 @@
#include <atomic>
#include <new>
// TODO: noexcept everything
namespace gp {
namespace system {
struct task_queue{

+ 1
- 1
include/gp_config.hpp Datei anzeigen

@ -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

+ 7
- 4
tests/math.cpp Datei anzeigen

@ -49,6 +49,8 @@ struct render_test : public test_scaffold {
virtual int run() {
using namespace gp::math::rendering;
int res = 0;
gp::array<char, 2048> 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<float>(
gp::math::sphere_sdf<float>({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<float>({-0.75,0.0,0.0}, 1.0);
auto l_sdf_b = gp::math::sphere_sdf<float>({-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<uint8_t>;
@ -159,6 +161,7 @@ struct function_test : public test_scaffold {
virtual int run() {
using namespace gp::math::rendering;
int res = 0;
gp::array<char, 2048> allocation_buffer;

Laden…
Abbrechen
Speichern