$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
billboard_sorting.c File Reference
#include "billboard_sorting.h"
#include "gl_common.h"
#include "gl_debug.h"
#include "log.h"
#include "platform/platform_utils.h"
#include "profiler.h"
#include "shader.h"
#include <stdbool.h>
#include <stdlib.h>
Include dependency graph for billboard_sorting.c:

Enumerations

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

Functions

 _Static_assert (sizeof(SphereInstance)==EXPECTED_SPHERE_INSTANCE_SIZE, "SphereInstance size must match GLSL std430 layout (128 B)")
 
static int next_pow2 (int val)
 
static bool ensure_cpu_capacity (BillboardSorter *sorter, int count)
 Grow CPU scratchpads to hold at least count elements. More...
 
static GLuint upload_sorted_to_ssbo (BillboardSorter *sorter, int count)
 Upload count sorted instances from the CPU temp buffer to the SSBO. More...
 
void billboard_sorter_init (BillboardSorter *sorter, int initial_capacity)
 Allocates internal buffers for the sorter. More...
 
void billboard_sorter_cleanup (BillboardSorter *sorter)
 Destroys the sorter context. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 

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
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
EXPECTED_SPHERE_INSTANCE_SIZE 

◆ anonymous enum

anonymous enum
Enumerator
WORKGROUP_SIZE 

◆ anonymous enum

anonymous enum
Enumerator
DEFAULT_MIN_CAPACITY 

◆ anonymous enum

anonymous enum
Enumerator
MAX_SINGLE_PASS_COUNT 

◆ anonymous enum

anonymous enum
Enumerator
RADIX_BITS_PER_PASS 

◆ anonymous enum

anonymous enum
Enumerator
RADIX_BUCKETS 

◆ anonymous enum

anonymous enum
Enumerator
RADIX_SHIFT_LIMIT 

◆ anonymous enum

anonymous enum
Enumerator
RADIX_MASK 

Function Documentation

◆ _Static_assert()

_Static_assert ( sizeof(SphereInstance)  = =EXPECTED_SPHERE_INSTANCE_SIZE,
"SphereInstance size must match GLSL std430 layout (128 B)"   
)

◆ 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_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.

Uses qsort on the host and uploads the result to the SSBO.

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.

Uploads instances to an SSBO, sorts them using a compute shader, and prepares them for rendering.

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).