$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
pbr.c File Reference
#include "pbr.h"
#include "gl_common.h"
#include "perf_timer.h"
#include "render_utils.h"
#include "shader.h"
#include <math.h>
#include <stddef.h>
#include <stdint.h>
Include dependency graph for pbr.c:

Functions

void pbr_get_spec_uniforms (GLuint shader, PBRSpecUniforms *out)
 Retrieves uniform locations for the specular prefilter shader. More...
 
void pbr_get_irr_uniforms (GLuint shader, PBRIrrUniforms *out)
 Retrieves uniform locations for the irradiance convolution shader. More...
 
void pbr_get_lum_uniforms (GLuint shader, PBRLumUniforms *out)
 Retrieves uniform locations for the luminance reduction shader. More...
 
GLuint pbr_prefilter_init (int width, int height)
 Initializes a texture for progressive specular pre-filtering. More...
 
void pbr_prefilter_mip (GLuint shader, const PBRSpecUniforms *uniforms, GLuint env_hdr_tex, GLuint dest_tex, int width, int height, int level, int total_levels, int slice_index, int total_slices, float threshold)
 Computes a single slice or mip-level for progressive pre-filtering. More...
 
GLuint build_prefiltered_specular_map (GLuint shader, GLuint env_hdr_tex, int width, int height, float threshold)
 Generates a pre-filtered specular environment map in a single pass. More...
 
GLuint pbr_irradiance_init (int size)
 Initializes a texture for progressive irradiance computation. More...
 
void pbr_irradiance_slice_compute (GLuint shader, const PBRIrrUniforms *uniforms, GLuint env_hdr_tex, GLuint dest_tex, int size, int slice_index, int total_slices, float threshold)
 Computes one face/slice of the irradiance map. More...
 
GLuint build_irradiance_map (GLuint shader, GLuint env_hdr_tex, int size, float threshold)
 Generates an irradiance map (diffuse IBL) in a single pass. More...
 
void compute_mean_luminance_gpu_start (GLuint shader_pass1, GLuint shader_pass2, GLuint hdr_tex, int width, int height, GLuint ssbos[2], const PBRLumUniforms *uniforms)
 Starts the GPU computation for mean luminance (non-blocking). Uses a two-pass parallel reduction via compute shaders and SSBOs. More...
 
float compute_mean_luminance_gpu_result (GLuint ssbos[2], float clamp_multiplier)
 Reads the result of mean luminance computation from the SSBO. This function should be called only after the GPU has finished the computation started by compute_mean_luminance_gpu_start. More...
 
GLuint build_brdf_lut_map (int size)
 Generates the 2D BRDF Integration Look-Up Table. More...
 

Variables

static const uint32_t COMPUTE_GROUP_SIZE_PBR = 32
 
static const uint32_t COMPUTE_GROUP_SIZE_LUM = 16
 
static const uint32_t MAX_HDR_RESOLUTION = 4096
 
static const GLuint BINDING_ENV_MAP = 0
 
static const GLuint BINDING_DEST_TEXTURE = 1
 
static const GLuint COMPUTE_DISPATCH_Z_ONCE = 1
 

Function Documentation

◆ build_brdf_lut_map()

GLuint build_brdf_lut_map ( int  size)

Generates the 2D BRDF Integration Look-Up Table.

Parameters
sizeLUT resolution (width and height).
Returns
GLuint handle of the 2D RG texture.
Here is the call graph for this function:

◆ build_irradiance_map()

GLuint build_irradiance_map ( GLuint  shader,
GLuint  env_hdr_tex,
int  size,
float  threshold 
)

Generates an irradiance map (diffuse IBL) in a single pass.

Parameters
shaderIrradiance convolution shader.
env_hdr_texSource HDR map.
sizeDestination resolution (width/height).
thresholdLuminance threshold.
Returns
GLuint handle of the irradiance cubemap.
Here is the call graph for this function:

◆ build_prefiltered_specular_map()

GLuint build_prefiltered_specular_map ( GLuint  shader,
GLuint  env_hdr_tex,
int  width,
int  height,
float  threshold 
)

Generates a pre-filtered specular environment map in a single pass.

Parameters
shaderPrefiltering compute shader.
env_hdr_texSource HDR environment map.
widthDestination width.
heightDestination height.
thresholdLuminance threshold for importance sampling.
Returns
GLuint handle of the generated cubemap.
Here is the call graph for this function:

◆ compute_mean_luminance_gpu_result()

float compute_mean_luminance_gpu_result ( GLuint  ssbos[2],
float  clamp_multiplier 
)

Reads the result of mean luminance computation from the SSBO. This function should be called only after the GPU has finished the computation started by compute_mean_luminance_gpu_start.

Parameters
ssbosThe SSBOs used in the computation.
clamp_multiplierMultiplier to apply to the result.
Returns
The computed mean luminance.

◆ compute_mean_luminance_gpu_start()

void compute_mean_luminance_gpu_start ( GLuint  shader_pass1,
GLuint  shader_pass2,
GLuint  hdr_tex,
int  width,
int  height,
GLuint  ssbos[2],
const PBRLumUniforms uniforms 
)

Starts the GPU computation for mean luminance (non-blocking). Uses a two-pass parallel reduction via compute shaders and SSBOs.

Parameters
shader_pass1First reduction pass shader.
shader_pass2Final reduction pass shader.
hdr_texSource HDR texture.
widthTexture width.
heightTexture height.
ssbosPair of SSBO handles for intermediate and final results.
uniformsCached uniform locations for pass 2.

◆ pbr_get_irr_uniforms()

void pbr_get_irr_uniforms ( GLuint  shader,
PBRIrrUniforms out 
)

Retrieves uniform locations for the irradiance convolution shader.

Parameters
shaderThe shader program.
outPointer to the struct to populate.

◆ pbr_get_lum_uniforms()

void pbr_get_lum_uniforms ( GLuint  shader,
PBRLumUniforms out 
)

Retrieves uniform locations for the luminance reduction shader.

Parameters
shaderThe shader program.
outPointer to the struct to populate.

◆ pbr_get_spec_uniforms()

void pbr_get_spec_uniforms ( GLuint  shader,
PBRSpecUniforms out 
)

Retrieves uniform locations for the specular prefilter shader.

Parameters
shaderThe shader program.
outPointer to the struct to populate.

◆ pbr_irradiance_init()

GLuint pbr_irradiance_init ( int  size)

Initializes a texture for progressive irradiance computation.

Parameters
sizeDestination resolution.
Returns
GLuint handle of the empty cubemap.
Here is the call graph for this function:

◆ pbr_irradiance_slice_compute()

void pbr_irradiance_slice_compute ( GLuint  shader,
const PBRIrrUniforms uniforms,
GLuint  env_hdr_tex,
GLuint  dest_tex,
int  size,
int  slice_index,
int  total_slices,
float  threshold 
)

Computes one face/slice of the irradiance map.

Parameters
shaderCompute shader.
uniformsCached uniform locations.
env_hdr_texSource HDR map.
dest_texDestination cubemap.
sizeResolution.
slice_indexCurrent face.
total_slicesTotal faces.
thresholdLuminance threshold.
Note
This function does NOT issue a memory barrier. The caller must call glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT) once after all slices have been dispatched.

◆ pbr_prefilter_init()

GLuint pbr_prefilter_init ( int  width,
int  height 
)

Initializes a texture for progressive specular pre-filtering.

Parameters
widthBase level width.
heightBase level height.
Returns
GLuint handle of the empty cubemap with allocated mips.
Here is the call graph for this function:

◆ pbr_prefilter_mip()

void pbr_prefilter_mip ( GLuint  shader,
const PBRSpecUniforms uniforms,
GLuint  env_hdr_tex,
GLuint  dest_tex,
int  width,
int  height,
int  level,
int  total_levels,
int  slice_index,
int  total_slices,
float  threshold 
)

Computes a single slice or mip-level for progressive pre-filtering.

Parameters
shaderCompute shader.
uniformsCached uniform locations.
env_hdr_texSource HDR map.
dest_texDestination cubemap.
widthCurrent level width.
heightCurrent level height.
levelCurrent mip level being processed.
total_levelsTotal number of mips in the cubemap.
slice_indexCurrent face/slice being processed.
total_slicesTotal slices (usually 6).
thresholdLuminance threshold.
Note
This function does NOT issue a memory barrier. The caller must call glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT) once after all slices have been dispatched.

Variable Documentation

◆ BINDING_DEST_TEXTURE

const GLuint BINDING_DEST_TEXTURE = 1
static

◆ BINDING_ENV_MAP

const GLuint BINDING_ENV_MAP = 0
static

◆ COMPUTE_DISPATCH_Z_ONCE

const GLuint COMPUTE_DISPATCH_Z_ONCE = 1
static

◆ COMPUTE_GROUP_SIZE_LUM

const uint32_t COMPUTE_GROUP_SIZE_LUM = 16
static

◆ COMPUTE_GROUP_SIZE_PBR

const uint32_t COMPUTE_GROUP_SIZE_PBR = 32
static

◆ MAX_HDR_RESOLUTION

const uint32_t MAX_HDR_RESOLUTION = 4096
static