Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
Loading...
Searching...
No Matches
billboard_sorter.c File Reference

Back-to-front sorting for transparent billboard geometry. More...

#include "billboard_sorter.h"
#include "gl_common.h"
#include "gl_debug.h"
#include "log.h"
#include "platform/platform_utils.h"
#include "profiler.h"
#include "scene_config.h"
#include "shader.h"
#include "sphere_types.h"
#include <cglm/types.h>
#include <cglm/vec3.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
Include dependency graph for billboard_sorter.c:

Enumerations

enum  { EXPECTED_SPHERE_INSTANCE_SIZE = 128 }
 
enum  {
  WORKGROUP_SIZE = 1024 , DEFAULT_MIN_CAPACITY = 64 , MAX_SINGLE_PASS_COUNT = 1024 , RADIX_BITS_PER_PASS = 8 ,
  RADIX_BUCKETS = 256 , RADIX_SHIFT_LIMIT = 32 , RADIX_MASK = 0xFF
}
 

Functions

static int next_pow2 (int val)
 
static bool ensure_cpu_capacity (BillboardSorter *sorter, int count)
 Grow CPU scratchpads to hold at least count elements.
 
static GLuint upload_sorted_to_ssbo (BillboardSorter *sorter, int count)
 Upload count sorted instances from the CPU temp buffer to the SSBO.
 
void billboard_sorter_init (BillboardSorter *sorter, int initial_capacity)
 Allocates internal buffers for the sorter.
 
void billboard_sorter_cleanup (BillboardSorter *sorter)
 Destroys the sorter context.
 
GLuint billboard_sorter_sort_gpu (BillboardSorter *sorter, const SphereInstance *instances, int count, const vec3 camera_pos)
 Sorts the array of instances Back-to-Front (descending depth) on GPU.
 
static int compare_sphere_entries (const void *lhs, const void *rhs)
 
GLuint billboard_sorter_sort_cpu (BillboardSorter *sorter, const SphereInstance *instances, int count, const vec3 camera_pos)
 Sorts the array of instances Back-to-Front (descending depth) on CPU.
 
static uint32_t float_to_sortable_uint (float f_val)
 Converts a float to a uint32_t that preserves order (IEEE 754). This allows sorting floats using integer radix sort.
 
GLuint billboard_sorter_sort_cpu_radix (BillboardSorter *sorter, const SphereInstance *instances, int count, const vec3 camera_pos)
 Sorts the array of instances Back-to-Front (descending depth) using Radix Sort.
 
GLuint billboard_sorter_sort (BillboardSorter *sorter, const SphereInstance *instances, int count, const vec3 camera_pos, SortingMode mode)
 Dispatches the sorting task using the specified SortingMode.
 

Variables

static const uint32_t FLOAT_SIGN_MASK = 0x80000000U
 
static const uint32_t FLOAT_COMPLEMENT_MASK = 0xFFFFFFFFU
 
static const unsigned int BIT_SHIFT_8 = 8U
 
static const unsigned int BIT_SHIFT_16 = 16U
 
static const size_t GPU_ENTRY_SIZE = 8
 

Detailed Description

Back-to-front sorting for transparent billboard geometry.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
EXPECTED_SPHERE_INSTANCE_SIZE 

◆ anonymous enum

anonymous enum
Enumerator
WORKGROUP_SIZE 
DEFAULT_MIN_CAPACITY 
MAX_SINGLE_PASS_COUNT 
RADIX_BITS_PER_PASS 
RADIX_BUCKETS 
RADIX_SHIFT_LIMIT 
RADIX_MASK 

Function Documentation

◆ billboard_sorter_cleanup()

void billboard_sorter_cleanup ( BillboardSorter *  sorter)

Destroys the sorter context.

Parameters
sorterPointer to the struct.

◆ billboard_sorter_init()

void billboard_sorter_init ( BillboardSorter *  sorter,
int  initial_capacity 
)

Allocates internal buffers for the sorter.

Parameters
sorterPointer to the struct.
initial_capacityExpected number of instances.
Here is the call graph for this function:

◆ billboard_sorter_sort()

GLuint billboard_sorter_sort ( BillboardSorter *  sorter,
const SphereInstance *  instances,
int  count,
const vec3  camera_pos,
SortingMode  mode 
)

Dispatches the sorting task using the specified SortingMode.

Parameters
sorterMemory context.
instancesArray of instances.
countActive element count.
camera_posWorld-space viewer position.
modeSorting algorithm choice.
Returns
The SSBO handle containing the sorted instances.
Here is the call graph for this function:

◆ billboard_sorter_sort_cpu()

GLuint billboard_sorter_sort_cpu ( BillboardSorter *  sorter,
const SphereInstance *  instances,
int  count,
const vec3  camera_pos 
)

Sorts the array of instances Back-to-Front (descending depth) on CPU.

Parameters
sorterMemory context.
instancesArray of instances (will be copied and sorted internally).
countActive element count.
camera_posWorld-space viewer position.
Returns
The SSBO handle containing the sorted instances.
Here is the call graph for this function:

◆ billboard_sorter_sort_cpu_radix()

GLuint billboard_sorter_sort_cpu_radix ( BillboardSorter *  sorter,
const SphereInstance *  instances,
int  count,
const vec3  camera_pos 
)

Sorts the array of instances Back-to-Front (descending depth) using Radix Sort.

Parameters
sorterMemory context.
instancesArray of instances.
countActive element count.
camera_posWorld-space viewer position (for depth).
Returns
The SSBO handle containing the sorted instances.
Here is the call graph for this function:

◆ billboard_sorter_sort_gpu()

GLuint billboard_sorter_sort_gpu ( BillboardSorter *  sorter,
const SphereInstance *  instances,
int  count,
const vec3  camera_pos 
)

Sorts the array of instances Back-to-Front (descending depth) on GPU.

Parameters
sorterMemory context.
instancesPointer to the array of instances to upload.
countActive element count.
camera_posWorld-space viewer position.
Returns
The SSBO handle containing the sorted instances.
Here is the call graph for this function:

◆ compare_sphere_entries()

static int compare_sphere_entries ( const void *  lhs,
const void *  rhs 
)
static

◆ ensure_cpu_capacity()

static bool ensure_cpu_capacity ( BillboardSorter *  sorter,
int  count 
)
static

Grow CPU scratchpads to hold at least count elements.

Returns
true on success, false on allocation failure.

◆ float_to_sortable_uint()

static uint32_t float_to_sortable_uint ( float  f_val)
inlinestatic

Converts a float to a uint32_t that preserves order (IEEE 754). This allows sorting floats using integer radix sort.

◆ next_pow2()

static int next_pow2 ( int  val)
static

◆ upload_sorted_to_ssbo()

static GLuint upload_sorted_to_ssbo ( BillboardSorter *  sorter,
int  count 
)
static

Upload count sorted instances from the CPU temp buffer to the SSBO.

Returns
The SSBO handle containing the data.

Variable Documentation

◆ BIT_SHIFT_16

const unsigned int BIT_SHIFT_16 = 16U
static

◆ BIT_SHIFT_8

const unsigned int BIT_SHIFT_8 = 8U
static

Bit-shift constants for next_pow2 (named to satisfy linter).

◆ FLOAT_COMPLEMENT_MASK

const uint32_t FLOAT_COMPLEMENT_MASK = 0xFFFFFFFFU
static

◆ FLOAT_SIGN_MASK

const uint32_t FLOAT_SIGN_MASK = 0x80000000U
static

◆ GPU_ENTRY_SIZE

const size_t GPU_ENTRY_SIZE = 8
static

Size of the Entry struct in the compute shader: int (4) + float (4).