$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
adaptive_sampler.c File Reference
#include "adaptive_sampler.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
Include dependency graph for adaptive_sampler.c:

Functions

static void pcg32_seed (Pcg32 *rng, uint64_t initstate, uint64_t initseq)
 
static uint32_t pcg32_random (Pcg32 *rng)
 
static float pcg32_random_float (Pcg32 *rng)
 
void adaptive_sampler_init (AdaptiveSampler *sampler, float window_duration, size_t target_samples, float initial_fps_guess)
 Initializes the adaptive sampler. More...
 
void adaptive_sampler_reset (AdaptiveSampler *sampler, double current_time)
 Resets the sampler state for a new window. More...
 
int adaptive_sampler_should_sample (AdaptiveSampler *sampler, float delta_time, double current_time, uint64_t frame_index)
 Determines if a sample should be taken this frame based on probability. More...
 
void adaptive_sampler_add (AdaptiveSampler *sampler, float value, uint64_t frame_index)
 Manually adds a sample to the sampler (e.g. from external source like GPU profiler). More...
 
int adaptive_sampler_is_finished (const AdaptiveSampler *sampler, double current_time)
 Checks if the current sampling window has concluded. More...
 
float adaptive_sampler_get_average (const AdaptiveSampler *sampler)
 Calculates the arithmetic mean of all samples in the current window. More...
 
size_t adaptive_sampler_get_sample_count (const AdaptiveSampler *sampler)
 Returns the number of samples currently in the buffer. More...
 
void adaptive_sampler_get_window_range (const AdaptiveSampler *sampler, uint64_t *start_frame, uint64_t *end_frame)
 Retrieves the frame index range covered by the current window. More...
 
size_t adaptive_sampler_get_sample_indices (const AdaptiveSampler *sampler, uint64_t *out_indices, size_t max_count)
 Retrieves the list of frame indices for all collected samples. More...
 
void adaptive_sampler_cleanup (AdaptiveSampler *sampler)
 Frees all dynamic memory associated with the sampler. More...
 

Variables

static const uint64_t PCG_MULTIPLIER = 6364136223846793005ULL
 
static const unsigned int PCG_SHIFT_1 = 18U
 
static const unsigned int PCG_SHIFT_2 = 27U
 
static const unsigned int PCG_IS_3 = 59U
 
static const unsigned int PCG_IS_4 = 31U
 
static const size_t DEFAULT_INITIAL_CAPACITY = 64
 

Function Documentation

◆ adaptive_sampler_add()

void adaptive_sampler_add ( AdaptiveSampler sampler,
float  value,
uint64_t  frame_index 
)

Manually adds a sample to the sampler (e.g. from external source like GPU profiler).

Parameters
samplerPointer to the sampler.
valueThe value to add.
frame_indexThe frame index associated with this value.

◆ adaptive_sampler_cleanup()

void adaptive_sampler_cleanup ( AdaptiveSampler sampler)

Frees all dynamic memory associated with the sampler.

Parameters
samplerPointer to the sampler.

◆ adaptive_sampler_get_average()

float adaptive_sampler_get_average ( const AdaptiveSampler sampler)

Calculates the arithmetic mean of all samples in the current window.

Parameters
samplerPointer to the sampler.
Returns
The average value.

◆ adaptive_sampler_get_sample_count()

size_t adaptive_sampler_get_sample_count ( const AdaptiveSampler sampler)

Returns the number of samples currently in the buffer.

Parameters
samplerPointer to the sampler.
Returns
Sample count.

◆ adaptive_sampler_get_sample_indices()

size_t adaptive_sampler_get_sample_indices ( const AdaptiveSampler sampler,
uint64_t *  out_indices,
size_t  max_count 
)

Retrieves the list of frame indices for all collected samples.

Parameters
samplerPointer to the sampler.
out_indicesBuffer to store the frame indices.
max_countSize of the output buffer.
Returns
Number of indices written to the buffer.

◆ adaptive_sampler_get_window_range()

void adaptive_sampler_get_window_range ( const AdaptiveSampler sampler,
uint64_t *  start_frame,
uint64_t *  end_frame 
)

Retrieves the frame index range covered by the current window.

Parameters
samplerPointer to the sampler.
start_frameOutput for start frame index (can be NULL).
end_frameOutput for end frame index (can be NULL).

◆ adaptive_sampler_init()

void adaptive_sampler_init ( AdaptiveSampler sampler,
float  window_duration,
size_t  target_samples,
float  initial_fps_guess 
)

Initializes the adaptive sampler.

Parameters
samplerPointer to the sampler.
window_durationLength of the window in seconds.
target_samplesApproximate number of samples to collect.
initial_fps_guessStarting value for EMA to avoid cold-start bias.
Here is the call graph for this function:

◆ adaptive_sampler_is_finished()

int adaptive_sampler_is_finished ( const AdaptiveSampler sampler,
double  current_time 
)

Checks if the current sampling window has concluded.

Parameters
samplerPointer to the sampler.
current_timeAbsolute time in seconds.
Returns
1 if the window duration has elapsed, 0 otherwise.

◆ adaptive_sampler_reset()

void adaptive_sampler_reset ( AdaptiveSampler sampler,
double  current_time 
)

Resets the sampler state for a new window.

Parameters
samplerPointer to the sampler.
current_timeNew window start time.

◆ adaptive_sampler_should_sample()

int adaptive_sampler_should_sample ( AdaptiveSampler sampler,
float  delta_time,
double  current_time,
uint64_t  frame_index 
)

Determines if a sample should be taken this frame based on probability.

Parameters
samplerPointer to the sampler.
delta_timeCurrent frame duration.
current_timeAbsolute time in seconds.
frame_indexCurrent frame index.
Returns
1 if a sample should be recorded, 0 otherwise.
Here is the call graph for this function:

◆ pcg32_random()

static uint32_t pcg32_random ( Pcg32 rng)
static

◆ pcg32_random_float()

static float pcg32_random_float ( Pcg32 rng)
static
Here is the call graph for this function:

◆ pcg32_seed()

static void pcg32_seed ( Pcg32 rng,
uint64_t  initstate,
uint64_t  initseq 
)
static

Variable Documentation

◆ DEFAULT_INITIAL_CAPACITY

const size_t DEFAULT_INITIAL_CAPACITY = 64
static

◆ PCG_IS_3

const unsigned int PCG_IS_3 = 59U
static

◆ PCG_IS_4

const unsigned int PCG_IS_4 = 31U
static

◆ PCG_MULTIPLIER

const uint64_t PCG_MULTIPLIER = 6364136223846793005ULL
static

◆ PCG_SHIFT_1

const unsigned int PCG_SHIFT_1 = 18U
static

◆ PCG_SHIFT_2

const unsigned int PCG_SHIFT_2 = 27U
static