dump state

This commit is contained in:
Kevin Trogant 2023-05-23 13:20:59 +02:00
parent 583cd9b417
commit 6b8ff8926e
6 changed files with 65 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -1,8 +1,12 @@
#include <defocus/camera.h>
#include <defocus/intrinsic_helper.h>
#include <immintrin.h>
DF_API void df_evaluate_ray_packet(const df_ray_packet *rays) {
#include <stdio.h>
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 */

1
subprojects/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
munit