$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
light_probes.h
Go to the documentation of this file.
1 #ifndef LIGHT_PROBES_H
2 #define LIGHT_PROBES_H
3 
4 #include "sh_math.h"
5 #include <cglm/cglm.h>
6 #include <pthread.h>
7 
8 typedef struct Shader Shader;
9 typedef struct SphereInstance SphereInstance;
10 
13 enum { SH_TEXTURE_COUNT = 7 };
14 
16 enum { TEXTURE_UNIT_SH_START = 8 };
17 
18 typedef struct {
19  SH9 sh_data; /* 9 coefficients, each aligned to vec4 (16 bytes) for
20  std430 */
21 } LightProbe;
22 
23 typedef struct {
24  /* Grid Data */
25  LightProbe* probes; /* CPU Buffer */
26  unsigned int ssbo; /* Legacy GPU Buffer Handle (kept for debug view) */
27  unsigned int sh_textures[SH_TEXTURE_COUNT]; /* 3D Textures for hardware
28  interpolation */
29 
30  /* Spatial Info */
31  vec3 aabb_min;
32  vec3 aabb_max;
33  ivec3 grid_dim;
34  vec3 cell_size;
36 
37  /* Scene Copy for Async Update */
40 
41  /* Threading */
42  pthread_t worker_thread;
43  pthread_mutex_t mutex;
44  pthread_cond_t cond;
45  volatile int running;
46  volatile int update_pending;
47  volatile int results_ready;
48 
49  /* Debug Resources */
52  unsigned int dummy_vao;
53  unsigned int aabb_vao;
54  unsigned int aabb_vbo;
56 
66 void light_probe_grid_compute_aabb(LightProbeGrid* grid, const void* spheres,
67  int count, size_t stride, float padding);
68 
76 void light_probe_grid_init_cpu(LightProbeGrid* grid, int dim_x, int dim_y,
77  int dim_z);
78 
83 void light_probe_grid_init(LightProbeGrid* grid, int dim_x, int dim_y,
84  int dim_z);
85 
89 void light_probe_grid_set_bounds(LightProbeGrid* grid, vec3 aabb_min,
90  vec3 aabb_max);
91 
95 void light_probe_grid_set_scene(LightProbeGrid* grid, const void* spheres,
96  int count, size_t stride);
97 
102 
108 
115 void light_probe_grid_render_debug(LightProbeGrid* grid, mat4 view, mat4 proj);
116 
121 
126 
127 #endif /* LIGHT_PROBES_H */
void light_probe_grid_compute_aabb(LightProbeGrid *grid, const void *spheres, int count, size_t stride, float padding)
Computes the AABB for the grid based on sphere instances.
Definition: light_probes.c:84
void light_probe_grid_init(LightProbeGrid *grid, int dim_x, int dim_y, int dim_z)
Initializes the grid structure, creates SSBO, and starts worker thread.
Definition: light_probes.c:424
void light_probe_grid_init_cpu(LightProbeGrid *grid, int dim_x, int dim_y, int dim_z)
Initializes the grid structure (CPU only).
Definition: light_probes.c:137
void light_probe_grid_render_debug(LightProbeGrid *grid, mat4 view, mat4 proj)
Renders debug spheres for the probes.
Definition: light_probes.c:670
void light_probe_grid_cleanup(LightProbeGrid *grid)
Cleans up all resources (CPU, GPU, Threads).
Definition: light_probes.c:620
void light_probe_grid_set_scene(LightProbeGrid *grid, const void *spheres, int count, size_t stride)
Updates the scene data used by the grid (copies it).
Definition: light_probes.c:496
void light_probe_grid_sync(LightProbeGrid *grid)
Checks if results are ready and uploads to GPU. Should be called on main thread.
Definition: light_probes.c:537
void light_probe_grid_update_async(LightProbeGrid *grid)
Signals the worker thread to perform an SH update.
Definition: light_probes.c:524
void light_probe_grid_set_bounds(LightProbeGrid *grid, vec3 aabb_min, vec3 aabb_max)
Sets the grid AABB and computes cell sizes from dimensions.
Definition: light_probes.c:473
@ TEXTURE_UNIT_SH_START
Definition: light_probes.h:16
@ SH_TEXTURE_COUNT
Definition: light_probes.h:13
void light_probe_grid_free_cpu(LightProbeGrid *grid)
Cleans up CPU resources.
Definition: light_probes.c:163
Definition: light_probes.h:23
SphereInstance * scene_copy
Definition: light_probes.h:38
pthread_t worker_thread
Definition: light_probes.h:42
Shader * aabb_shader
Definition: light_probes.h:51
pthread_mutex_t mutex
Definition: light_probes.h:43
int scene_count
Definition: light_probes.h:39
Shader * debug_shader
Definition: light_probes.h:50
int total_probes
Definition: light_probes.h:35
unsigned int aabb_vao
Definition: light_probes.h:53
unsigned int aabb_vbo
Definition: light_probes.h:54
unsigned int ssbo
Definition: light_probes.h:26
ivec3 grid_dim
Definition: light_probes.h:33
unsigned int dummy_vao
Definition: light_probes.h:52
pthread_cond_t cond
Definition: light_probes.h:44
volatile int update_pending
Definition: light_probes.h:46
LightProbe * probes
Definition: light_probes.h:25
vec3 aabb_min
Definition: light_probes.h:31
volatile int results_ready
Definition: light_probes.h:47
vec3 cell_size
Definition: light_probes.h:34
volatile int running
Definition: light_probes.h:45
vec3 aabb_max
Definition: light_probes.h:32
Definition: light_probes.h:18
SH9 sh_data
Definition: light_probes.h:19
Definition: sh_math.h:10
Wrapper for an OpenGL program with uniform caching and automatic cleanup.
Definition: shader.h:71
Per-instance data sent to the shader via an instanced VBO.
Definition: sphere_types.h:25