13 #include "effects/fx_auto_exposure.h"
14 #include "effects/fx_bloom.h"
15 #include "effects/fx_dof.h"
16 #include "effects/fx_lut3d.h"
17 #include "effects/fx_lut_viz.h"
18 #include "effects/fx_motion_blur.h"
22 #include <cglm/cglm.h>
23 #include <cglm/types.h>
26 #define POSTPROCESS_HISTOGRAM_BUCKETS 256
28 #define DEFAULT_VIGNETTE_INTENSITY 0.8F
29 #define DEFAULT_VIGNETTE_SMOOTHNESS 0.5F
30 #define DEFAULT_VIGNETTE_ROUNDNESS 1.0F
31 #define DEFAULT_GRAIN_INTENSITY 0.02F
32 #define DEFAULT_GRAIN_SHADOWS_MAX 0.09F
33 #define DEFAULT_GRAIN_HIGHLIGHTS_MIN 0.5F
34 #define DEFAULT_GRAIN_TEXEL_SIZE 1.0F
35 #define DEFAULT_EXPOSURE 1.00F
36 #define DEFAULT_CHROM_ABBR_STRENGTH 0.005F
37 #define DEFAULT_BLOOM_INTENSITY 0.0F
38 #define DEFAULT_BLOOM_THRESHOLD 1.0F
39 #define DEFAULT_BLOOM_SOFT_THRESHOLD 0.5F
40 #define DEFAULT_BLOOM_RADIUS 1.0F
43 #define DEFAULT_FXAA_SUBPIX 0.75F
44 #define DEFAULT_FXAA_EDGE_THRESHOLD 0.125F
45 #define DEFAULT_FXAA_EDGE_THRESHOLD_MIN 0.063F
48 #define DEFAULT_DOF_FOCAL_DISTANCE 20.0F
49 #define DEFAULT_DOF_FOCAL_RANGE 5.0F
50 #define DEFAULT_DOF_BOKEH_SCALE 10.0F
51 #define DEFAULT_DOF_ANAMORPHIC_RATIO \
55 #define DEFAULT_BANDING_LEVELS 256.0F
58 #define DEFAULT_FOG_DENSITY 0.0F
59 #define DEFAULT_FOG_START 10.0F
60 #define DEFAULT_FOG_HEIGHT_FALLOFF 0.1F
61 #define DEFAULT_FOG_COLOR_R 0.5F
62 #define DEFAULT_FOG_COLOR_G 0.6F
63 #define DEFAULT_FOG_COLOR_B 0.7F
66 #define DEFAULT_WB_TEMP 6500.0F
67 #define DEFAULT_WB_TINT 0.0F
70 #define DEFAULT_FILMIC_SLOPE 1.0F
71 #define DEFAULT_FILMIC_TOE 0.0F
72 #define DEFAULT_FILMIC_SHOULDER 0.0F
73 #define DEFAULT_FILMIC_BLACK_CLIP 0.0F
74 #define DEFAULT_FILMIC_WHITE_CLIP 0.0F
111 #define DEFAULT_ACTIVE_EFFECTS \
112 ((unsigned int)POSTFX_EXPOSURE | (unsigned int)POSTFX_COLOR_GRADING | \
113 (unsigned int)POSTFX_FXAA)
254 float screen_texel_size[2];
331 float banding_channel_levels[3];
453 GPUProfiler* external_profiler,
int width,
int height);
467 unsigned int static_flags);
503 float temperature,
float tint);
505 float saturation,
float contrast,
506 float gamma,
float gain,
float offset,
509 float toe,
float shoulder,
float black_clip,
513 float smoothness,
float roundness);
518 float threshold,
float soft_threshold);
520 float focal_range,
float bokeh_scale);
522 float anamorphic_ratio);
525 float min_luminance,
float max_luminance,
526 float speed_up,
float speed_down,
529 float edge_threshold,
float edge_threshold_min);
537 float green,
float blue);
539 float start,
float height_falloff,
float fog_r,
540 float fog_g,
float fog_b);
617 uint64_t frame_count);
630 uint64_t frame_count,
int* buckets,
631 int size,
float* min_lum,
Common OpenGL definitions, RAII helpers, and utilities.
#define GL_ASSERT_UBO_ALIGNMENT(type)
Compile-time assertion that a UBO struct meets AVX alignment. Place after the typedef to catch miscon...
Definition: gl_common.h:153
#define GL_UBO_ALIGNED
Attribute to apply on UBO typedef to enforce AVX alignment. Usage: } GL_UBO_ALIGNED MyUBOType;.
Definition: gl_common.h:146
PostProcessEffect
Bitmask flags for enabling/disabling individual effects.
Definition: postprocess.h:80
@ POSTFX_BANDING
Definition: postprocess.h:99
@ POSTFX_BLOOM_DEBUG
Definition: postprocess.h:103
@ POSTFX_AUTO_EXPOSURE
Definition: postprocess.h:90
@ POSTFX_MOTION_BLUR
Definition: postprocess.h:94
@ POSTFX_DOF_DEBUG
Definition: postprocess.h:89
@ POSTFX_COLOR_GRADING
Definition: postprocess.h:86
@ POSTFX_EXPOSURE_DEBUG
Definition: postprocess.h:92
@ POSTFX_GRAIN
Definition: postprocess.h:82
@ POSTFX_MOTION_BLUR_DEBUG
Definition: postprocess.h:95
@ POSTFX_LUT_VIZ
Definition: postprocess.h:107
@ POSTFX_VECTOR_FIELD_DEBUG
Definition: postprocess.h:100
@ POSTFX_FXAA
Definition: postprocess.h:97
@ POSTFX_DOF
Definition: postprocess.h:88
@ POSTFX_STENCIL_DEBUG
Definition: postprocess.h:102
@ POSTFX_BLOOM
Definition: postprocess.h:85
@ POSTFX_FXAA_DEBUG
Definition: postprocess.h:98
@ POSTFX_LUT3D
Definition: postprocess.h:106
@ POSTFX_FOG_DEBUG
Definition: postprocess.h:105
@ POSTFX_EXPOSURE
Definition: postprocess.h:83
@ POSTFX_FOG
Definition: postprocess.h:104
@ POSTFX_VIGNETTE
Definition: postprocess.h:81
@ POSTFX_CHROM_ABBR
Definition: postprocess.h:84
GLuint postprocess_get_histogram_pbo(PostProcess *post_processing, int index)
Definition: postprocess.c:1040
void postprocess_set_color_grading(PostProcess *post_processing, float saturation, float contrast, float gamma, float gain, float offset, float lift)
Definition: postprocess.c:511
BandingMode
Styles of color quantization.
Definition: postprocess.h:204
@ BANDING_MODE_DITHERED
Definition: postprocess.h:206
@ BANDING_MODE_LINEAR
Definition: postprocess.h:205
@ BANDING_MODE_PERCEPTUAL
Definition: postprocess.h:207
@ BANDING_MODE_LUMINANCE
Definition: postprocess.h:209
@ BANDING_MODE_CHANNEL
Definition: postprocess.h:208
int postprocess_init(PostProcess *post_processing, GPUProfiler *external_profiler, int width, int height)
Initializes the post-processing pipeline.
Definition: postprocess.c:42
void postprocess_set_white_balance(PostProcess *post_processing, float temperature, float tint)
Definition: postprocess.c:503
void postprocess_set_banding_perceptual(PostProcess *post_processing, float gamma)
Definition: postprocess.c:610
void postprocess_use_dynamic(PostProcess *post_processing)
Switches back to the dynamic/generic Uber-shader.
Definition: postprocess.c:1513
void postprocess_set_grain(PostProcess *post_processing, float intensity)
Definition: postprocess.c:485
void postprocess_set_banding_dither(PostProcess *post_processing, float strength)
Definition: postprocess.c:603
void postprocess_set_dummy_textures(PostProcess *post_processing, GLuint dummy_black)
Internal helper to set fallback textures.
Definition: postprocess.c:258
void postprocess_set_banding_channels(PostProcess *post_processing, float red, float green, float blue)
Definition: postprocess.c:617
void postprocess_set_banding(PostProcess *post_processing, BandingMode mode, float levels)
Definition: postprocess.c:595
void postprocess_update_readbacks(PostProcess *post_processing, uint64_t frame_count)
Updates all async GPU readbacks (Exposure, Histogram). Handles PBO mapping and Sync management intern...
Definition: postprocess.c:1070
void postprocess_set_chrom_abbr(PostProcess *post_processing, float strength)
Definition: postprocess.c:497
void postprocess_set_lut3d(PostProcess *post_processing, float intensity, GLuint texture)
Definition: postprocess.c:1527
int postprocess_compute_luminance_histogram(PostProcess *post_processing, uint64_t frame_count, int *buckets, int size, float *min_lum, float *max_lum)
Computes the luminance histogram from the GPU readback.
Definition: postprocess.c:1148
GLsync postprocess_get_histogram_sync(PostProcess *post_processing, int index)
Definition: postprocess.c:1050
void postprocess_apply_preset(PostProcess *post_processing, const PostProcessPreset *preset)
Applies all settings from a preset atomically.
Definition: postprocess.c:658
void postprocess_set_auto_exposure(PostProcess *post_processing, float min_luminance, float max_luminance, float speed_up, float speed_down, float key_value)
Definition: postprocess.c:573
void postprocess_set_exposure_sync(PostProcess *post_processing, int index, GLsync sync)
Definition: postprocess.c:1055
void postprocess_set_exposure(PostProcess *post_processing, float exposure)
Definition: postprocess.c:491
#define POSTPROCESS_HISTOGRAM_BUCKETS
Definition: postprocess.h:26
void postprocess_set_tonemapper(PostProcess *post_processing, float slope, float toe, float shoulder, float black_clip, float white_clip)
Definition: postprocess.c:525
void postprocess_update_matrices(PostProcess *post_processing, mat4 view_proj)
Updates view-projection matrices for effects requiring depth-reconstruction.
Definition: postprocess.c:1225
void postprocess_enable(PostProcess *post_processing, PostProcessEffect effect)
Enables a specific effect.
Definition: postprocess.c:452
void postprocess_set_bloom(PostProcess *post_processing, float intensity, float threshold, float soft_threshold)
Definition: postprocess.c:537
void postprocess_begin(PostProcess *post_processing)
Binds the HDR FBO and prepares for scene rendering. Should be called BEFORE the main render loop.
Definition: postprocess.c:680
void postprocess_toggle(PostProcess *post_processing, PostProcessEffect effect)
Toggles the current state of an effect.
Definition: postprocess.c:464
void postprocess_cleanup(PostProcess *post_processing)
Releases all GPU and CPU resources.
Definition: postprocess.c:319
void postprocess_set_fog(PostProcess *post_processing, float density, float start, float height_falloff, float fog_r, float fog_g, float fog_b)
Definition: postprocess.c:626
GLuint postprocess_get_exposure_pbo(PostProcess *post_processing, int index)
Definition: postprocess.c:1035
void postprocess_set_grading_ue_default(PostProcess *post_processing)
Definition: postprocess.c:639
void postprocess_disable(PostProcess *post_processing, PostProcessEffect effect)
Disables a specific effect.
Definition: postprocess.c:458
void postprocess_set_histogram_sync(PostProcess *post_processing, int index, GLsync sync)
Definition: postprocess.c:1061
void postprocess_end(PostProcess *post_processing)
Processes the HDR scene and renders the final LDR result to screen. Should be called AFTER the main r...
Definition: postprocess.c:690
GLsync postprocess_get_exposure_sync(PostProcess *post_processing, int index)
Definition: postprocess.c:1045
void postprocess_set_dof(PostProcess *post_processing, float focal_distance, float focal_range, float bokeh_scale)
Definition: postprocess.c:546
int postprocess_load_lut3d(PostProcess *post_processing, const char *path)
Definition: postprocess.c:1535
float postprocess_get_exposure(PostProcess *post_processing)
Definition: postprocess.c:563
void postprocess_set_dof_anamorphic(PostProcess *post_processing, float anamorphic_ratio)
Definition: postprocess.c:556
@ SHADER_CACHE_SIZE
Definition: postprocess.h:244
void postprocess_set_vignette(PostProcess *post_processing, float intensity, float smoothness, float roundness)
Definition: postprocess.c:476
void postprocess_update_time(PostProcess *post_processing, float delta_time)
Increments internal clocks.
Definition: postprocess.c:1028
void postprocess_resize(PostProcess *post_processing, int width, int height)
Recreates all internal buffers for a new resolution.
Definition: postprocess.c:358
void postprocess_set_fxaa(PostProcess *post_processing, float subpix, float edge_threshold, float edge_threshold_min)
Definition: postprocess.c:586
void postprocess_set_exposure_target(PostProcess *post_processing, float threshold)
Updates the target exposure threshold for AE.
Definition: postprocess.c:1100
void postprocess_compile_optimized(PostProcess *post_processing, unsigned int static_flags)
Compiles a specialized Uber-shader for maximum performance.
Definition: postprocess.c:1436
int postprocess_is_enabled(PostProcess *post_processing, PostProcessEffect effect)
Returns true if an effect is currently active.
Definition: postprocess.c:470
High-level OpenGL shader management with metadata and uniform caching.
Controls for color banding/quantization.
Definition: postprocess.h:216
float dither_strength
Definition: postprocess.h:219
float perceptual_gamma
Definition: postprocess.h:220
vec3 channel_levels
Definition: postprocess.h:221
int32_t mode
Definition: postprocess.h:217
float levels
Definition: postprocess.h:218
Focal-length distortion simulation.
Definition: postprocess.h:164
float strength
Definition: postprocess.h:165
Unreal-style color grading parameters.
Definition: postprocess.h:119
float saturation
Definition: postprocess.h:120
float lift
Definition: postprocess.h:125
float gamma
Definition: postprocess.h:122
float gain
Definition: postprocess.h:123
float contrast
Definition: postprocess.h:121
float offset
Definition: postprocess.h:124
Manual exposure tuning.
Definition: postprocess.h:156
float exposure
Definition: postprocess.h:157
Parameters for Fast Approximate Anti-Aliasing.
Definition: postprocess.h:193
float edge_threshold_min
Definition: postprocess.h:196
float edge_threshold
Definition: postprocess.h:195
float subpix
Definition: postprocess.h:194
Depth-based atmospheric fog parameters.
Definition: postprocess.h:228
float density
Definition: postprocess.h:229
float height_falloff
Definition: postprocess.h:231
float start
Definition: postprocess.h:230
Manages GPU timing using double-buffered queries to avoid stalls.
Definition: gpu_profiler.h:86
Fine-grained controls for film noise.
Definition: postprocess.h:142
float intensity_highlights
Definition: postprocess.h:146
float shadows_max
Definition: postprocess.h:147
float intensity_midtones
Definition: postprocess.h:145
float highlights_min
Definition: postprocess.h:148
float intensity
Definition: postprocess.h:143
float intensity_shadows
Definition: postprocess.h:144
float texel_size
Definition: postprocess.h:149
Snapshot of every configurable parameter in the pipeline.
Definition: postprocess.h:556
DoFParams dof
Definition: postprocess.h:566
BloomParams bloom
Definition: postprocess.h:565
TonemapParams tonemapper
Definition: postprocess.h:564
WhiteBalanceParams white_balance
Definition: postprocess.h:562
BandingParams banding
Definition: postprocess.h:568
ExposureParams exposure
Definition: postprocess.h:560
unsigned int active_effects
Definition: postprocess.h:557
VignetteParams vignette
Definition: postprocess.h:558
ColorGradingParams color_grading
Definition: postprocess.h:563
ChromAbberationParams chrom_abbr
Definition: postprocess.h:561
FXAAParams fxaa
Definition: postprocess.h:567
FogParams fog
Definition: postprocess.h:569
GrainParams grain
Definition: postprocess.h:559
LUT3DParams lut3d
Definition: postprocess.h:570
Shared Uniform Buffer structure for shaders.
Definition: postprocess.h:251
float tonemap_slope
Definition: postprocess.h:295
float fog_height_falloff
Definition: postprocess.h:337
float fxaa_quality_edge_threshold_min
Definition: postprocess.h:323
float _pad13
Definition: postprocess.h:340
float grain_texel_size
Definition: postprocess.h:269
float grain_intensity
Definition: postprocess.h:263
float vignette_intensity
Definition: postprocess.h:257
float dof_focal_distance
Definition: postprocess.h:309
float exposure_manual
Definition: postprocess.h:273
float banding_perceptual_gamma
Definition: postprocess.h:330
float fxaa_quality_subpix
Definition: postprocess.h:321
float bloom_radius
Definition: postprocess.h:306
float dof_anamorphic_ratio
Definition: postprocess.h:312
float grading_lift
Definition: postprocess.h:291
float tonemap_toe
Definition: postprocess.h:296
float grain_highlights_min
Definition: postprocess.h:268
float banding_levels
Definition: postprocess.h:328
float bloom_soft_threshold
Definition: postprocess.h:305
uint32_t active_effects
Definition: postprocess.h:252
float tonemap_white_clip
Definition: postprocess.h:299
float fxaa_quality_edge_threshold
Definition: postprocess.h:322
float grading_gamma
Definition: postprocess.h:288
float lut3d_intensity
Definition: postprocess.h:343
float dof_focal_range
Definition: postprocess.h:310
float _pad9
Definition: postprocess.h:318
float _pad1
Definition: postprocess.h:260
float grain_shadows_max
Definition: postprocess.h:267
float fog_start
Definition: postprocess.h:336
int32_t banding_mode
Definition: postprocess.h:327
float _pad12
Definition: postprocess.h:338
float grading_contrast
Definition: postprocess.h:287
float tonemap_black_clip
Definition: postprocess.h:298
float grain_intensity_shadows
Definition: postprocess.h:264
float banding_dither_strength
Definition: postprocess.h:329
float tonemap_shoulder
Definition: postprocess.h:297
float vignette_smoothness
Definition: postprocess.h:258
float wb_tint
Definition: postprocess.h:282
float grading_saturation
Definition: postprocess.h:286
float _pad10
Definition: postprocess.h:324
float wb_temperature
Definition: postprocess.h:281
float mb_max_velocity
Definition: postprocess.h:316
float grading_offset
Definition: postprocess.h:290
float fog_density
Definition: postprocess.h:335
float bloom_intensity
Definition: postprocess.h:303
vec3 fog_color
Definition: postprocess.h:339
int32_t mb_samples
Definition: postprocess.h:317
float _pad2
Definition: postprocess.h:270
float _pad11
Definition: postprocess.h:332
float bloom_threshold
Definition: postprocess.h:304
float grain_intensity_highlights
Definition: postprocess.h:266
float mb_intensity
Definition: postprocess.h:315
float grading_gain
Definition: postprocess.h:289
float time
Definition: postprocess.h:253
float vignette_roundness
Definition: postprocess.h:259
float grain_intensity_midtones
Definition: postprocess.h:265
float chrom_abbr_strength
Definition: postprocess.h:277
float dof_bokeh_scale
Definition: postprocess.h:311
Main pipeline state for post-processing.
Definition: postprocess.h:353
int height
Definition: postprocess.h:381
GLuint scene_stencil_view
Definition: postprocess.h:359
FogParams fog
Definition: postprocess.h:399
GLsync exposure_sync[2]
Definition: postprocess.h:425
int last_histogram_updated
Definition: postprocess.h:437
GLuint histogram_pbo[2]
Definition: postprocess.h:423
GLsync histogram_sync[2]
Definition: postprocess.h:427
int banding_preset_idx
Definition: postprocess.h:418
unsigned int active_effects
Definition: postprocess.h:383
Shader * neighbor_max_shader
Definition: postprocess.h:378
AutoExposureFX auto_exposure_fx
Definition: postprocess.h:364
ExposureParams exposure
Definition: postprocess.h:388
GLuint scene_fbo
Definition: postprocess.h:355
Shader * tile_max_shader
Definition: postprocess.h:377
DoFFX dof_fx
Definition: postprocess.h:363
float auto_threshold
Definition: postprocess.h:431
int width
Definition: postprocess.h:380
bool ubo_dirty
Definition: postprocess.h:409
Shader * postprocess_shader
Definition: postprocess.h:376
GLuint dummy_black_tex
Definition: postprocess.h:404
GLuint velocity_tex
Definition: postprocess.h:357
GLuint dummy_uint_tex
Definition: postprocess.h:405
AutoExposureParams auto_exposure
Definition: postprocess.h:395
int last_buckets[256]
Definition: postprocess.h:434
TonemapParams tonemapper
Definition: postprocess.h:392
LUTVizFX lut_viz_fx
Definition: postprocess.h:367
ChromAbberationParams chrom_abbr
Definition: postprocess.h:389
LUT3DFX lut3d_fx
Definition: postprocess.h:366
float current_exposure
Definition: postprocess.h:430
uint64_t frame_count
Definition: postprocess.h:439
GLuint screen_quad_vbo
Definition: postprocess.h:371
LUT3DParams lut3d
Definition: postprocess.h:400
WhiteBalanceParams white_balance
Definition: postprocess.h:390
DoFParams dof
Definition: postprocess.h:394
int shader_cache_count
Definition: postprocess.h:415
FXAAParams fxaa
Definition: postprocess.h:397
GLuint settings_ubo
Definition: postprocess.h:373
GrainParams grain
Definition: postprocess.h:387
MotionBlurParams motion_blur
Definition: postprocess.h:396
unsigned int compiled_flags
Definition: postprocess.h:412
GLuint exposure_pbo[2]
Definition: postprocess.h:422
GPUProfiler * gpu_profiler
Definition: postprocess.h:417
BloomFX bloom_fx
Definition: postprocess.h:362
BloomParams bloom
Definition: postprocess.h:393
GLuint scene_color_tex
Definition: postprocess.h:356
ShaderCacheEntry shader_cache[SHADER_CACHE_SIZE]
Definition: postprocess.h:414
float last_min_lum
Definition: postprocess.h:435
VignetteParams vignette
Definition: postprocess.h:386
GLuint screen_quad_vao
Definition: postprocess.h:370
GLuint scene_depth_tex
Definition: postprocess.h:358
bool is_optimized
Definition: postprocess.h:407
BandingParams banding
Definition: postprocess.h:398
MotionBlurFX motion_blur_fx
Definition: postprocess.h:365
float delta_time
Definition: postprocess.h:403
float time
Definition: postprocess.h:402
ColorGradingParams color_grading
Definition: postprocess.h:391
float last_max_lum
Definition: postprocess.h:436
Cache entry for optimized shaders.
Definition: postprocess.h:239
Shader * shader
Definition: postprocess.h:241
unsigned int flags
Definition: postprocess.h:240
Wrapper for an OpenGL program with uniform caching and automatic cleanup.
Definition: shader.h:71
ACES-like filmic tonemapping curve parameters.
Definition: postprocess.h:181
float white_clip
Definition: postprocess.h:186
float black_clip
Definition: postprocess.h:185
float toe
Definition: postprocess.h:183
float slope
Definition: postprocess.h:182
float shoulder
Definition: postprocess.h:184
Controls for the screen-edge darkening effect.
Definition: postprocess.h:132
float smoothness
Definition: postprocess.h:134
float roundness
Definition: postprocess.h:135
float intensity
Definition: postprocess.h:133
Temperature and tint correction.
Definition: postprocess.h:172
float tint
Definition: postprocess.h:174
float temperature
Definition: postprocess.h:173