From 6b8ff8926e2569d7a4eab2f1c9a997699765fb16 Mon Sep 17 00:00:00 2001 From: Kevin Trogant Date: Tue, 23 May 2023 13:20:59 +0200 Subject: [PATCH] dump state --- bin/defocus.c | 19 ++++++++++++++++++- include/defocus/intrinsic_helper.h | 18 ++++++++++++++++++ include/defocus/raytracing.h | 4 +++- lib/camera.c | 4 ++++ lib/raytracing.c | 28 +++++++++++++++++++++------- subprojects/.gitignore | 1 + 6 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 subprojects/.gitignore diff --git a/bin/defocus.c b/bin/defocus.c index 07e2ad2..0641921 100644 --- a/bin/defocus.c +++ b/bin/defocus.c @@ -233,7 +233,24 @@ int test_fn( int argc, char **argv ) { df_camera_i cam = df_create_perspective_camera(1024.0, 1024.0, 1024.0, 1024.0, 2.e-4f, 1000.f, 350.0, 21.0); df_ray_packet packet = cam.build_ray_packet(cam.o); - df_evaluate_ray_packet(&packet); + + int w = 1024; + int h = 1024; + df_image *out_image; + df_result res = df_create_image(w, h, &out_image); + if (res != df_result_success) { + fprintf(stderr, "Failed to create output image (%d)\n", (int)res); + return 1; + } + + + df_evaluate_ray_packet(&packet, out_image); + + const char *out_path = "out.png"; + res = df_write_image(out_image, out_path); + if (res != df_result_success) { + fprintf(stderr, "Failed to write to output image %s (%d)\n", out_path, (int)res); + } df_release_ray_packet(&packet); diff --git a/include/defocus/intrinsic_helper.h b/include/defocus/intrinsic_helper.h index c24b01f..996354b 100644 --- a/include/defocus/intrinsic_helper.h +++ b/include/defocus/intrinsic_helper.h @@ -35,4 +35,22 @@ /* returns [vec1[2], vec1[3], vec2[2], vec2[3]] */ #define DF_VEC_SHUFFLE_2323(vec1, vec2) _mm_movehl_ps(vec2, vec1) +/* returns the n-th float inside the vector */ +inline float extract_float(__m128 v, unsigned int n) +{ + /* _mm_shuffle_ps expects a constant */ + switch (n) { + case 0: + return _mm_cvtss_f32(_mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 0, 0, 0))); + case 1: + return _mm_cvtss_f32(_mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 0, 0, 1))); + case 2: + return _mm_cvtss_f32(_mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 0, 0, 2))); + case 3: + return _mm_cvtss_f32(_mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 0, 0, 3))); + } + int nan = 0x7F800001; + return *(float *)&nan; +} + #endif diff --git a/include/defocus/raytracing.h b/include/defocus/raytracing.h index 9406173..3a10d95 100644 --- a/include/defocus/raytracing.h +++ b/include/defocus/raytracing.h @@ -1,6 +1,8 @@ #ifndef DF_RAYTRACING_H #define DF_RAYTRACING_H +#include "image.h" + /** @brief Stores information for rays in a SIMD friendly way */ typedef struct { @@ -24,6 +26,6 @@ typedef struct /** @brief Free ray packet memory */ DF_API void df_release_ray_packet(df_ray_packet *rays); -DF_API void df_evaluate_ray_packet(const df_ray_packet *rays); +DF_API void df_evaluate_ray_packet(const df_ray_packet *rays, df_image *output); #endif diff --git a/lib/camera.c b/lib/camera.c index 6790920..6ac9cd9 100644 --- a/lib/camera.c +++ b/lib/camera.c @@ -16,7 +16,11 @@ DF_API void df_release_ray_packet(df_ray_packet *rays) { +#ifdef _MSC_VER + _aligned_free(rays->simd_mem); +#else free(rays->simd_mem); +#endif free(rays->ray_uvs); memset(rays, 0, sizeof(*rays)); diff --git a/lib/raytracing.c b/lib/raytracing.c index 79901d0..8208ce4 100644 --- a/lib/raytracing.c +++ b/lib/raytracing.c @@ -1,8 +1,12 @@ #include +#include #include -DF_API void df_evaluate_ray_packet(const df_ray_packet *rays) { +#include + +DF_API void df_evaluate_ray_packet(const df_ray_packet *rays, df_image *output) +{ const __m128 *base_x = (const __m128 *)rays->base_x; const __m128 *base_y = (const __m128 *)rays->base_y; const __m128 *base_z = (const __m128 *)rays->base_z; @@ -21,12 +25,14 @@ DF_API void df_evaluate_ray_packet(const df_ray_packet *rays) { /* TODO(kevin): divide to multiple threads */ for (size_t i = 0; i < ray_count; i += 4) { - __m128 rays_base_x = base_x[i]; - __m128 rays_base_y = base_y[i]; - __m128 rays_base_z = base_z[i]; - __m128 rays_dir_x = dir_x[i]; - __m128 rays_dir_y = dir_y[i]; - __m128 rays_dir_z = dir_z[i]; + size_t index = i / 4; + + __m128 rays_base_x = base_x[index]; + __m128 rays_base_y = base_y[index]; + __m128 rays_base_z = base_z[index]; + __m128 rays_dir_x = dir_x[index]; + __m128 rays_dir_y = dir_y[index]; + __m128 rays_dir_z = dir_z[index]; /* Solve for t: base.z + t * dir.z = plane_z * t = (plane_z - base.z) / dir.z @@ -42,6 +48,14 @@ DF_API void df_evaluate_ray_packet(const df_ray_packet *rays) { sample_p_y = _mm_add_ps(sample_p_y, rays_base_y); sample_p_z = _mm_add_ps(sample_p_z, rays_base_z); + /* Map the sample position to a 2d (uv) point on the plane, so that we can sample a color */ + df_color color[4]; + for (int j = 0; j < 4; ++j) { + float r = extract_float(sample_p_x, j); + float g = extract_float(sample_p_y, j); + + printf("%f %f\n", r, g); + } } /* Handle remaining (< 4) rays */ diff --git a/subprojects/.gitignore b/subprojects/.gitignore new file mode 100644 index 0000000..71db7c4 --- /dev/null +++ b/subprojects/.gitignore @@ -0,0 +1 @@ +munit \ No newline at end of file