$darkmode
#include "postprocess.h"#include "app_settings.h"#include "effects/fx_auto_exposure.h"#include "effects/fx_bloom.h"#include "effects/fx_dof.h"#include "effects/fx_motion_blur.h"#include "gl_common.h"#include "gl_debug.h"#include "log.h"#include "render_utils.h"#include "shader.h"#include "utils.h"#include <cglm/types.h>#include <stdbool.h>#include <stdint.h>#include <string.h>#include "profiler.h"Data Structures | |
| struct | EffectMetadata |
Macros | |
| #define | EFFECT_COUNT (sizeof(ALL_EFFECTS) / sizeof(ALL_EFFECTS[0])) |
Enumerations | |
| enum | { POSTPROCESS_TEX_UNIT_SCENE = 0 , POSTPROCESS_TEX_UNIT_BLOOM = 1 , POSTPROCESS_TEX_UNIT_DEPTH = 2 , POSTPROCESS_TEX_UNIT_EXPOSURE = 3 , POSTPROCESS_TEX_UNIT_VELOCITY = 4 , POSTPROCESS_TEX_UNIT_NEIGHBOR_MAX = 5 , POSTPROCESS_TEX_UNIT_DOF_BLUR = 6 , POSTPROCESS_TEX_UNIT_STENCIL = 7 , POSTPROCESS_TEX_UNIT_LUT3D = 8 } |
| enum | { POSTPROCESS_COMPUTE_GROUP_SIZE = 16 } |
| enum | { MAX_SHADER_DEFINES = 32 , MAX_DEFINE_LENGTH = 64 } |
Functions | |
| static int | create_framebuffer (PostProcess *post_processing) |
| static void | destroy_framebuffer (PostProcess *post_processing) |
| static void | destroy_screen_quad (PostProcess *post_processing) |
| static bool | is_shader_in_cache (PostProcess *post_processing, Shader *shader) |
| int | postprocess_init (PostProcess *post_processing, GPUProfiler *external_profiler, int width, int height) |
| Initializes the post-processing pipeline. More... | |
| void | postprocess_set_dummy_textures (PostProcess *post_processing, GLuint dummy_black) |
| Internal helper to set fallback textures. More... | |
| static void | destroy_readback_buffers (PostProcess *post_processing) |
| static void | destroy_cached_shaders (PostProcess *post_processing) |
| void | postprocess_cleanup (PostProcess *post_processing) |
| Releases all GPU and CPU resources. More... | |
| void | postprocess_resize (PostProcess *post_processing, int width, int height) |
| Recreates all internal buffers for a new resolution. More... | |
| static void | postprocess_on_state_change (PostProcess *post_processing) |
| void | postprocess_enable (PostProcess *post_processing, PostProcessEffect effect) |
| Enables a specific effect. More... | |
| void | postprocess_disable (PostProcess *post_processing, PostProcessEffect effect) |
| Disables a specific effect. More... | |
| void | postprocess_toggle (PostProcess *post_processing, PostProcessEffect effect) |
| Toggles the current state of an effect. More... | |
| int | postprocess_is_enabled (PostProcess *post_processing, PostProcessEffect effect) |
| Returns true if an effect is currently active. More... | |
| void | postprocess_set_vignette (PostProcess *post_processing, float intensity, float smoothness, float roundness) |
| void | postprocess_set_grain (PostProcess *post_processing, float intensity) |
| void | postprocess_set_exposure (PostProcess *post_processing, float exposure) |
| void | postprocess_set_chrom_abbr (PostProcess *post_processing, float strength) |
| void | postprocess_set_white_balance (PostProcess *post_processing, float temperature, float tint) |
| void | postprocess_set_color_grading (PostProcess *post_processing, float saturation, float contrast, float gamma, float gain, float offset, float lift) |
| void | postprocess_set_tonemapper (PostProcess *post_processing, float slope, float toe, float shoulder, float black_clip, float white_clip) |
| void | postprocess_set_bloom (PostProcess *post_processing, float intensity, float threshold, float soft_threshold) |
| void | postprocess_set_dof (PostProcess *post_processing, float focal_distance, float focal_range, float bokeh_scale) |
| void | postprocess_set_dof_anamorphic (PostProcess *post_processing, float anamorphic_ratio) |
| float | postprocess_get_exposure (PostProcess *post_processing) |
| void | postprocess_set_auto_exposure (PostProcess *post_processing, float min_luminance, float max_luminance, float speed_up, float speed_down, float key_value) |
| void | postprocess_set_fxaa (PostProcess *post_processing, float subpix, float edge_threshold, float edge_threshold_min) |
| void | postprocess_set_banding (PostProcess *post_processing, BandingMode mode, float levels) |
| void | postprocess_set_banding_dither (PostProcess *post_processing, float strength) |
| void | postprocess_set_banding_perceptual (PostProcess *post_processing, float gamma) |
| void | postprocess_set_banding_channels (PostProcess *post_processing, float red, float green, float blue) |
| void | postprocess_set_fog (PostProcess *post_processing, float density, float start, float height_falloff, float fog_r, float fog_g, float fog_b) |
| void | postprocess_set_grading_ue_default (PostProcess *post_processing) |
| void | postprocess_apply_preset (PostProcess *post_processing, const PostProcessPreset *preset) |
| Applies all settings from a preset atomically. More... | |
| void | postprocess_begin (PostProcess *post_processing) |
| Binds the HDR FBO and prepares for scene rendering. Should be called BEFORE the main render loop. More... | |
| void | postprocess_end (PostProcess *post_processing) |
| Processes the HDR scene and renders the final LDR result to screen. Should be called AFTER the main render loop. More... | |
| void | postprocess_update_time (PostProcess *post_processing, float delta_time) |
| Increments internal clocks. More... | |
| GLuint | postprocess_get_exposure_pbo (PostProcess *post_processing, int index) |
| GLuint | postprocess_get_histogram_pbo (PostProcess *post_processing, int index) |
| GLsync | postprocess_get_exposure_sync (PostProcess *post_processing, int index) |
| GLsync | postprocess_get_histogram_sync (PostProcess *post_processing, int index) |
| void | postprocess_set_exposure_sync (PostProcess *post_processing, int index, GLsync sync) |
| void | postprocess_set_histogram_sync (PostProcess *post_processing, int index, GLsync sync) |
| void | postprocess_update_readbacks (PostProcess *post_processing, uint64_t frame_count) |
| Updates all async GPU readbacks (Exposure, Histogram). Handles PBO mapping and Sync management internally to avoid CPU stalls. More... | |
| void | postprocess_set_exposure_target (PostProcess *post_processing, float threshold) |
| Updates the target exposure threshold for AE. More... | |
| static void | fill_histogram_buckets (const float *lum_data, int *buckets, int size, float *min_lum, float *max_lum) |
| static void | trigger_histogram_readback (PostProcess *post_processing, int write_idx) |
| 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. More... | |
| void | postprocess_update_matrices (PostProcess *post_processing, mat4 view_proj) |
| Updates view-projection matrices for effects requiring depth-reconstruction. More... | |
| static void | setup_sampler_uniforms (PostProcess *post_processing) |
| Sets sampler uniform → texture unit bindings on the postprocess shader. These are program state (not context state) and only need to be set once per shader program, not every frame. More... | |
| static void | log_optimized_effects (unsigned int flags) |
| static Shader * | find_shader_in_cache (PostProcess *post_processing, unsigned int static_flags) |
| static void | update_current_shader (PostProcess *post_processing, Shader *new_shader, bool is_optimized) |
| void | postprocess_compile_optimized (PostProcess *post_processing, unsigned int static_flags) |
| Compiles a specialized Uber-shader for maximum performance. More... | |
| void | postprocess_use_dynamic (PostProcess *post_processing) |
| Switches back to the dynamic/generic Uber-shader. More... | |
| void | postprocess_set_lut3d (PostProcess *post_processing, float intensity, GLuint texture) |
| int | postprocess_load_lut3d (PostProcess *post_processing, const char *path) |
Variables | |
| static const float | LUM_MIN_EXTREME = 1e30F |
| static const float | LUM_MAX_EXTREME = -1e30F |
| static const EffectMetadata | ALL_EFFECTS [] |
| #define EFFECT_COUNT (sizeof(ALL_EFFECTS) / sizeof(ALL_EFFECTS[0])) |
| anonymous enum |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
| void postprocess_apply_preset | ( | PostProcess * | post_processing, |
| const PostProcessPreset * | preset | ||
| ) |
Applies all settings from a preset atomically.
| post_processing | Pointer to the struct. |
| preset | Pointer to the preset values. |
| void postprocess_begin | ( | PostProcess * | post_processing | ) |
Binds the HDR FBO and prepares for scene rendering. Should be called BEFORE the main render loop.
| void postprocess_cleanup | ( | PostProcess * | post_processing | ) |
Releases all GPU and CPU resources.
| post_processing | Pointer to the struct. |
| void postprocess_compile_optimized | ( | PostProcess * | post_processing, |
| unsigned int | static_flags | ||
| ) |
Compiles a specialized Uber-shader for maximum performance.
| post_processing | Pointer to the struct. |
| static_flags | Bitmask of effects to bake into the shader. |
| 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.
| void postprocess_disable | ( | PostProcess * | post_processing, |
| PostProcessEffect | effect | ||
| ) |
Disables a specific effect.
| void postprocess_enable | ( | PostProcess * | post_processing, |
| PostProcessEffect | effect | ||
| ) |
Enables a specific effect.
| void postprocess_end | ( | PostProcess * | post_processing | ) |
Processes the HDR scene and renders the final LDR result to screen. Should be called AFTER the main render loop.
| float postprocess_get_exposure | ( | PostProcess * | post_processing | ) |
| GLuint postprocess_get_exposure_pbo | ( | PostProcess * | post_processing, |
| int | index | ||
| ) |
| GLsync postprocess_get_exposure_sync | ( | PostProcess * | post_processing, |
| int | index | ||
| ) |
| GLuint postprocess_get_histogram_pbo | ( | PostProcess * | post_processing, |
| int | index | ||
| ) |
| GLsync postprocess_get_histogram_sync | ( | PostProcess * | post_processing, |
| int | index | ||
| ) |
| int postprocess_init | ( | PostProcess * | post_processing, |
| GPUProfiler * | external_profiler, | ||
| int | width, | ||
| int | height | ||
| ) |
Initializes the post-processing pipeline.
| post_processing | Pointer to the struct. |
| external_profiler | Pointer to the GPU profiler for timing stages. |
| width | Initial resolution width. |
| height | Initial resolution height. |
| int postprocess_is_enabled | ( | PostProcess * | post_processing, |
| PostProcessEffect | effect | ||
| ) |
Returns true if an effect is currently active.
| int postprocess_load_lut3d | ( | PostProcess * | post_processing, |
| const char * | path | ||
| ) |
|
static |
| void postprocess_resize | ( | PostProcess * | post_processing, |
| int | width, | ||
| int | height | ||
| ) |
Recreates all internal buffers for a new resolution.
| post_processing | Pointer to the struct. |
| width | New width. |
| height | New height. |
| void postprocess_set_auto_exposure | ( | PostProcess * | post_processing, |
| float | min_luminance, | ||
| float | max_luminance, | ||
| float | speed_up, | ||
| float | speed_down, | ||
| float | key_value | ||
| ) |
| void postprocess_set_banding | ( | PostProcess * | post_processing, |
| BandingMode | mode, | ||
| float | levels | ||
| ) |
| void postprocess_set_banding_channels | ( | PostProcess * | post_processing, |
| float | red, | ||
| float | green, | ||
| float | blue | ||
| ) |
| void postprocess_set_banding_dither | ( | PostProcess * | post_processing, |
| float | strength | ||
| ) |
| void postprocess_set_banding_perceptual | ( | PostProcess * | post_processing, |
| float | gamma | ||
| ) |
| void postprocess_set_bloom | ( | PostProcess * | post_processing, |
| float | intensity, | ||
| float | threshold, | ||
| float | soft_threshold | ||
| ) |
| void postprocess_set_chrom_abbr | ( | PostProcess * | post_processing, |
| float | strength | ||
| ) |
| void postprocess_set_color_grading | ( | PostProcess * | post_processing, |
| float | saturation, | ||
| float | contrast, | ||
| float | gamma, | ||
| float | gain, | ||
| float | offset, | ||
| float | lift | ||
| ) |
| void postprocess_set_dof | ( | PostProcess * | post_processing, |
| float | focal_distance, | ||
| float | focal_range, | ||
| float | bokeh_scale | ||
| ) |
| void postprocess_set_dof_anamorphic | ( | PostProcess * | post_processing, |
| float | anamorphic_ratio | ||
| ) |
| void postprocess_set_dummy_textures | ( | PostProcess * | post_processing, |
| GLuint | dummy_black | ||
| ) |
Internal helper to set fallback textures.
| void postprocess_set_exposure | ( | PostProcess * | post_processing, |
| float | exposure | ||
| ) |
| void postprocess_set_exposure_sync | ( | PostProcess * | post_processing, |
| int | index, | ||
| GLsync | sync | ||
| ) |
| void postprocess_set_exposure_target | ( | PostProcess * | post_processing, |
| float | threshold | ||
| ) |
Updates the target exposure threshold for AE.
| void postprocess_set_fog | ( | PostProcess * | post_processing, |
| float | density, | ||
| float | start, | ||
| float | height_falloff, | ||
| float | fog_r, | ||
| float | fog_g, | ||
| float | fog_b | ||
| ) |
| void postprocess_set_fxaa | ( | PostProcess * | post_processing, |
| float | subpix, | ||
| float | edge_threshold, | ||
| float | edge_threshold_min | ||
| ) |
| void postprocess_set_grading_ue_default | ( | PostProcess * | post_processing | ) |
| void postprocess_set_grain | ( | PostProcess * | post_processing, |
| float | intensity | ||
| ) |
| void postprocess_set_histogram_sync | ( | PostProcess * | post_processing, |
| int | index, | ||
| GLsync | sync | ||
| ) |
| void postprocess_set_lut3d | ( | PostProcess * | post_processing, |
| float | intensity, | ||
| GLuint | texture | ||
| ) |
| void postprocess_set_tonemapper | ( | PostProcess * | post_processing, |
| float | slope, | ||
| float | toe, | ||
| float | shoulder, | ||
| float | black_clip, | ||
| float | white_clip | ||
| ) |
| void postprocess_set_vignette | ( | PostProcess * | post_processing, |
| float | intensity, | ||
| float | smoothness, | ||
| float | roundness | ||
| ) |
| void postprocess_set_white_balance | ( | PostProcess * | post_processing, |
| float | temperature, | ||
| float | tint | ||
| ) |
| void postprocess_toggle | ( | PostProcess * | post_processing, |
| PostProcessEffect | effect | ||
| ) |
Toggles the current state of an effect.
| void postprocess_update_matrices | ( | PostProcess * | post_processing, |
| mat4 | view_proj | ||
| ) |
Updates view-projection matrices for effects requiring depth-reconstruction.
| post_processing | Pointer to the struct. |
| view_proj | The current frame's View-Proj matrix. |
| void postprocess_update_readbacks | ( | PostProcess * | post_processing, |
| uint64_t | frame_count | ||
| ) |
Updates all async GPU readbacks (Exposure, Histogram). Handles PBO mapping and Sync management internally to avoid CPU stalls.
| void postprocess_update_time | ( | PostProcess * | post_processing, |
| float | delta_time | ||
| ) |
Increments internal clocks.
| post_processing | Pointer to the struct. |
| delta_time | SECONDS elapsed since last frame. |
| void postprocess_use_dynamic | ( | PostProcess * | post_processing | ) |
Switches back to the dynamic/generic Uber-shader.
| post_processing | Pointer to the struct. |
|
static |
Sets sampler uniform → texture unit bindings on the postprocess shader. These are program state (not context state) and only need to be set once per shader program, not every frame.
|
static |
|
static |
|
static |
|
static |
|
static |