$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
sphere_sorting.c File Reference
#include "sphere_sorting.h"
#include "gl_common.h"
#include "gl_debug.h"
#include "instanced_rendering.h"
#include "log.h"
#include "platform/platform_utils.h"
#include "profiler.h"
#include "shader.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for sphere_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 (SphereSorter *sorter, int count)
 Grow CPU scratchpads to hold at least count elements. More...
 
static GLuint upload_sorted_to_ssbo (SphereSorter *sorter, int count)
 Upload count sorted instances from the CPU temp buffer to the SSBO. More...
 
void sphere_sorter_init (SphereSorter *sorter, int initial_capacity)
 Allocates internal buffers for the sorter. More...
 
void sphere_sorter_cleanup (SphereSorter *sorter)
 Destroys the sorter context. More...
 
GLuint sphere_sorter_sort_gpu (SphereSorter *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 sphere_sorter_sort_cpu (SphereSorter *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 sphere_sorter_sort_cpu_radix (SphereSorter *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)"   
)

◆ compare_sphere_entries()

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

◆ ensure_cpu_capacity()

static bool ensure_cpu_capacity ( SphereSorter 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

◆ sphere_sorter_cleanup()

void sphere_sorter_cleanup ( SphereSorter sorter)

Destroys the sorter context.

Parameters
sorterPointer to the struct.

◆ sphere_sorter_init()

void sphere_sorter_init ( SphereSorter 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:

◆ sphere_sorter_sort_cpu()

GLuint sphere_sorter_sort_cpu ( SphereSorter 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:

◆ sphere_sorter_sort_cpu_radix()

GLuint sphere_sorter_sort_cpu_radix ( SphereSorter 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:

◆ sphere_sorter_sort_gpu()

GLuint sphere_sorter_sort_gpu ( SphereSorter 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:

◆ upload_sorted_to_ssbo()

static GLuint upload_sorted_to_ssbo ( SphereSorter 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).