|
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
|
#include "async_loader.h"#include "asset_manager.h"#include "async_backend.h"#include "log.h"#include "perf_timer.h"#include "profiler.h"#include "tracy_manager.h"#include "utils.h"#include <pthread.h>#include <stdlib.h>#include <string.h>#include "app.h"#include "app_profiling.h"Data Structures | |
| struct | AsyncLoader |
Macros | |
| #define | transition_tracy_state(s) tracy_manager_async_transition(loader->tracy_mgr, s) |
| #define | cleanup_tracy_states() tracy_manager_async_end(loader->tracy_mgr) |
Functions | |
| static void | async_perform_conversion (AsyncLoader *loader) |
| Performs the actual format conversion (e.g., float -> half float) into the PBO. | |
| static void | async_cleanup_payload (AsyncLoader *loader) |
| Destroys backend-specific payload allocations. | |
| static void | async_wait_and_convert (AsyncLoader *loader) |
| Worker thread wait loop for PBO allocation from the main thread. | |
| static void | async_handle_io (AsyncLoader *loader, const char *path_to_load) |
| Heavy-lifting disk IO loading phase. | |
| static void * | async_worker_func (void *arg) |
| Thread entry point for the background worker. | |
| AsyncLoader * | async_loader_create (struct TracyManager *mgr) |
| Initialize the asynchronous loader subsystem. | |
| void | async_loader_destroy (AsyncLoader *loader) |
| Tear down the asynchronous loader subsystem. | |
| bool | async_loader_request (AsyncLoader *loader, const AssetHandle *asset) |
| Request a new asset to be loaded asynchronously. | |
| bool | async_loader_poll (AsyncLoader *loader, AsyncRequest *out_req) |
| Poll the status of the current request. | |
| void | async_loader_provide_pbo (AsyncLoader *loader, void *mapped_ptr, GLuint pbo_id) |
| Provide the mapped memory address and GL buffer ID for PBO writing. | |
| void | async_loader_cancel (AsyncLoader *loader) |
| Cancel a loading request waiting for PBO input. | |
| int | async_loader_subsys_init (App *app) |
| void | async_loader_subsys_cleanup (App *app) |
| #define cleanup_tracy_states | ( | ) | tracy_manager_async_end(loader->tracy_mgr) |
| #define transition_tracy_state | ( | s | ) | tracy_manager_async_transition(loader->tracy_mgr, s) |
|
static |
Destroys backend-specific payload allocations.
Invokes the active format backend's cleanup function to free CPU allocations loaded during the IO phase.
| loader | Pointer to the AsyncLoader instance. |
Concurrency constraints:
|
static |
Heavy-lifting disk IO loading phase.
Loads the asset payload from disk into temporary CPU memory. This is executed entirely outside the request_mutex to keep disk latency from blocking the main thread.
| loader | Pointer to the AsyncLoader instance. |
| path_to_load | Absolute or relative path to the asset on disk. |
Concurrency constraints:
| void async_loader_cancel | ( | AsyncLoader * | loader | ) |
Cancel a loading request waiting for PBO input.
Aborts the current request if it is still waiting for the main thread to provide a PBO.
| loader | Loader instance. |
State transitions:
Concurrency: Thread-safe. Acquires request_mutex.
| AsyncLoader * async_loader_create | ( | struct TracyManager * | mgr | ) |
Initialize the asynchronous loader subsystem.
Allocates resources, initializes synchronization primitives (mutex, condvar), and spawns the background worker thread.
| mgr | Tracy profiler manager instance. |
Concurrency: Thread-safe. Lock initialized inside. Initial state: ASYNC_IDLE.
| void async_loader_destroy | ( | AsyncLoader * | loader | ) |
Tear down the asynchronous loader subsystem.
Signals the worker thread to exit, waits for its completion (joins it), releases any pending resources/requests, and destroys synchronization primitives.
| loader | Loader instance to destroy. Can be NULL (no-op). |
Concurrency: Safe to call from the main thread during cleanup. Post-condition: Mutex/condvar are destroyed and memory is freed.
| bool async_loader_poll | ( | AsyncLoader * | loader, |
| AsyncRequest * | out_req | ||
| ) |
Poll the status of the current request.
Checks if the request is ready for GPU upload, waiting for PBO allocation, or has failed. This is meant to be called periodically from the main thread.
| loader | Loader instance. |
| out_req | Destination structure to copy the request snapshot if ready/waiting. |
State transitions:
Concurrency: Thread-safe. Acquires request_mutex.
| void async_loader_provide_pbo | ( | AsyncLoader * | loader, |
| void * | mapped_ptr, | ||
| GLuint | pbo_id | ||
| ) |
Provide the mapped memory address and GL buffer ID for PBO writing.
Called by the main thread after detecting ASYNC_WAITING_FOR_PBO during poll. Maps the CPU pointer to the PBO and allows the worker to perform the conversion.
| loader | Loader instance. |
| mapped_ptr | Pointer to the mapped buffer memory (CPU visible). |
| pbo_id | OpenGL buffer object ID (or 0 for CPU-only malloc fallback). |
State transitions:
Concurrency: Thread-safe. Acquires request_mutex.
| bool async_loader_request | ( | AsyncLoader * | loader, |
| const AssetHandle * | asset | ||
| ) |
Request a new asset to be loaded asynchronously.
Accepts a loading task if the loader is idle or finished with the previous one.
| loader | Loader instance. |
| asset | Path and format metadata of the asset. |
State transitions:
Concurrency: Thread-safe. Acquires request_mutex.
| void async_loader_subsys_cleanup | ( | App * | app | ) |
| int async_loader_subsys_init | ( | App * | app | ) |
|
static |
Performs the actual format conversion (e.g., float -> half float) into the PBO.
Runs the format-specific backend conversion. Since writing to mapped PBO memory can be slow, the request mutex is released during the conversion process to keep the main thread from stalling.
| loader | Pointer to the AsyncLoader instance. |
Concurrency constraints:
|
static |
Worker thread wait loop for PBO allocation from the main thread.
Blocks the worker thread until the main thread allocates a PBO, maps it, and calls async_loader_provide_pbo() (which transitions state to ASYNC_CONVERTING and signals request_cond).
| loader | Pointer to the AsyncLoader instance. |
Concurrency constraints:
|
static |
Thread entry point for the background worker.
Handles the main execution loop, waiting passively for submitted requests, coordinating IO and conversion task dispatches.
| arg | Opaque pointer to the AsyncLoader instance. |