==> include/pathtrace/scene.h <==1/* @ajax/pathtrace-scene: programs/pathtrace's scene format (pathtrace-core lib.rs): a scene is u32
2 * words (floats as their bits): a 32-word header, then materials, quads, spheres and triangles. Also
3 * the image's tiles (32 x 24 pixels) and the scene words a fleet job may set. Freestanding. */
4#ifndef PATHTRACE_SCENE_H
5#define PATHTRACE_SCENE_H
6
7#include <stdbool.h>
8#include <stddef.h>
9#include <stdint.h>
10
11#define PT_MAGIC_SCENE 0x43535450u /* "PTSC" */
12#define PT_VERSION 1u
13
14/* Header words. */
15#define PT_H_MAGIC 0
16#define PT_H_VERSION 1
17#define PT_H_WIDTH 2
18#define PT_H_HEIGHT 3
19#define PT_H_SPP 4 /* samples per pixel per pass */
20#define PT_H_PASSES 5
21#define PT_H_DEPTH 6
22#define PT_H_SEED 7
23#define PT_H_NMAT 8
24#define PT_H_NQUAD 9
25#define PT_H_NSPH 10
26#define PT_H_NTRI 11
27#define PT_H_LIGHT 12 /* the quad next-event estimation samples (PT_NO_LIGHT: none) */
28#define PT_H_EXPOSURE 13
29#define PT_H_CLAMP 14 /* the per-sample radiance clamp */
30#define PT_H_WORDS 15 /* the scene's length in words */
31#define PT_H_EYE 16 /* the pinhole: eye, the image's top-left corner, horizontal and vertical spans */
32#define PT_H_CORNER 19
33#define PT_H_HORIZ 22
34#define PT_H_VERT 25
35#define PT_H_MESH 28 /* the triangles' bounding sphere: center, radius */
36#define PT_HEADER_WORDS 32
37#define PT_MAT_WORDS 8 /* kind, albedo rgb, emission rgb, param (metal: fuzz; glass: ior) */
38#define PT_QUAD_WORDS 16 /* p, u, v, n (unit), 1/u.u, 1/v.v, area, material */
39#define PT_SPH_WORDS 8 /* center, radius, 1/radius, material, 0, 0 */
40#define PT_TRI_WORDS 16 /* v0, e1, e2, n (unit), material, 0, 0, 0 */
41#define PT_SCENE_MAX_WORDS 1024
42#define PT_NO_LIGHT 0xffffffffu
43#define PT_DIFFUSE 0u
44#define PT_METAL 1u
45#define PT_GLASS 2u
46#define PT_EMIT 3u
47
48#define PT_TILE_W 32u
49#define PT_TILE_H 24u
50#define PT_TILE_PIX (PT_TILE_W * PT_TILE_H)
51#define PT_MAX_TILES 300u /* tiles the view holds: 640 x 360 */
52#define PT_FLEET_MAX_TILES 4096u /* tiles of an image rendered as pass sums only: 2048 x 1536 */
53#define PT_TONEMAP_ACES 0u
54#define PT_TONEMAP_REINHARD 1u
55#define PT_TONEMAPS 2u
56
57/* Where each part of a scene starts, from its header. */
58typedef struct {
59 uint32_t nquad, nsph, ntri;
60 size_t mat, quad, sph, tri;
61 uint32_t light;
62} pt_layout;
63
64pt_layout pt_layout_of(const uint32_t *sc);
65
66/* 0 when the scene's words are well formed (every index in range, the image a whole number of tiles
67 * within PT_MAX_TILES); else which rule failed, 1..10. len is the words available. */
68uint32_t pt_validate(const uint32_t *sc, size_t len);
69/* pt_validate with at most max_tiles tiles (PT_FLEET_MAX_TILES: pass sums only). */
70uint32_t pt_validate_in(const uint32_t *sc, size_t len, uint32_t max_tiles);
71
72/* The scene words a job's setup may set: the image size, samples per pass, passes, depth, seed. */
73#define PT_SETTABLE_COUNT 6
74extern const size_t pt_settable[PT_SETTABLE_COUNT];
75/* OP_SET's arg: word := value (< 2^24). */
76static inline uint32_t pt_set_arg(size_t word, uint32_t value) { return (uint32_t)word << 24 | (value & 0x00ffffffu); }
77/* Applies an OP_SET arg to a scene of len words; false (nothing changed) for a word not settable. */
78bool pt_set_param(uint32_t *sc, size_t len, uint32_t arg);
79
80static inline uint32_t pt_tiles_x(const uint32_t *sc) { return sc[PT_H_WIDTH] / PT_TILE_W; }
81static inline uint32_t pt_tiles(const uint32_t *sc) {
82 return (sc[PT_H_WIDTH] / PT_TILE_W) * (sc[PT_H_HEIGHT] / PT_TILE_H);
83}
84/* Tile t's top-left pixel. */
85static inline void pt_tile_origin(const uint32_t *sc, uint32_t t, uint32_t *x, uint32_t *y) {
86 uint32_t tx = pt_tiles_x(sc);
87 *x = (t % tx) * PT_TILE_W;
88 *y = (t / tx) * PT_TILE_H;
89}
90
91#endif