skip to content
==> include/pathtrace/consts.h <==
1/* @ajax/pathtrace-math: the constants of programs/pathtrace as f32 bits (program.metal has the same),
2 * the vector type, and bit casts. Freestanding. */
3#ifndef PATHTRACE_CONSTS_H
4#define PATHTRACE_CONSTS_H
5
6#include <stdint.h>
7
8#define PT_C_ONE 0x3f800000u
9#define PT_C_HALF 0x3f000000u
10#define PT_C_TWO 0x40000000u
11#define PT_C_ZERO 0u
12#define PT_C_U24 0x33800000u     /* 2^-24: a 24-bit integer to [0, 1) */
13#define PT_C_HALF_PI 0x3fc90fdbu
14#define PT_C_INV_PI 0x3ea2f983u
15/* Ray offset and the nearest hit (1e-4), the far plane (1e30), the parallel tests, the shadow margin. */
16#define PT_C_EPS 0x38d1b717u
17#define PT_C_BIG 0x7149f2cau
18#define PT_C_DET_EPS 0x3089705fu
19#define PT_C_PAR_EPS 0x322bcc77u
20#define PT_C_SHADOW_EPS 0x3a83126fu
21/* Russian roulette's survival probability bounds (0.05, 0.95), from depth PT_RR_DEPTH. */
22#define PT_C_RR_LO 0x3d4ccccdu
23#define PT_C_RR_HI 0x3f733333u
24#define PT_RR_DEPTH 3u
25/* Hit ids: 0 none, 1 + i quad i, ID_SPH + i sphere i, ID_TRI + i triangle i. */
26#define PT_ID_SPH 0x100u
27#define PT_ID_TRI 0x200u
28
29/* sin: a (1 + a2 (S1 + a2 (S2 + ..))), cos: 1 + a2 (C1 + a2 (C2 + ..)) (Taylor, |a| <= pi/2). */
30static const uint32_t pt_c_sin[5] = {0xbe2aaaab, 0x3c088889, 0xb9500d01, 0x3638ef1d, 0xb2d7322b};
31static const uint32_t pt_c_cos[6] = {0xbf000000, 0x3d2aaaab, 0xbab60b61, 0x37d00d01, 0xb493f27e, 0x310f76c7};
32/* ACES filmic fit (Narkowicz): x (A x + B) / (x (C x + D) + E). */
33static const uint32_t pt_c_aces[5] = {0x4020a3d7, 0x3cf5c28f, 0x401b851f, 0x3f170a3d, 0x3e0f5c29};
34
35typedef struct {
36    float x, y, z;
37} pt_v;
38
39typedef union {
40    float f;
41    uint32_t u;
42} pt_pun;
43
44static inline float pt_from_bits(uint32_t b) {
45    pt_pun p;
46    p.u = b;
47    return p.f;
48}
49
50static inline uint32_t pt_to_bits(float x) {
51    pt_pun p;
52    p.f = x;
53    return p.u;
54}
55
56#endif