4#define SBL_ARENA_IMPLEMENTATION
63 if (!ctx || !ctx->
inner)
118 "sbgl_InitWithConfig: NULL config passed");
126 "sbgl_InitWithConfig: arena initialization failed");
135 "sbgl_InitWithConfig: context allocation failed");
144 "sbgl_InitWithConfig: inner context allocation failed");
148 inner->
arena = main_arena;
171 "sbgl_InitWithConfig: window creation failed");
180 "sbgl_InitWithConfig: graphics initialization failed");
187 "sbgl_InitWithConfig: initialization successful");
197 .windowTitle = title,
198 .limits = { .maxBuffers = 1024, .maxShaders = 256, .maxPipelines = 256 },
199 .enableValidation =
true
207 "sbgl_Shutdown: NULL context");
213 "sbgl_Shutdown: uninitialized context");
239 if (!ctx || !ctx->
inner)
246 if (!ctx || !ctx->
inner)
257 if (!ctx || !ctx->
inner)
263 if (!ctx || !ctx->
inner)
return;
316 if (!ctx || !ctx->
inner)
345 if (!ctx || !ctx->
inner)
392 if (!ctx || !ctx->
inner)
402 if (!ctx || !ctx->
inner)
415 if (!ctx || !ctx->
inner)
419 return &inner->
input;
423 if (!ctx || !ctx->
inner)
437 if (!ctx || !ctx->
inner) {
446 if (!ctx || !ctx->
inner)
458 if (!ctx || !ctx->
inner)
467 "Buffer destroyed while GPU is busy! Missing sbgl_DeviceWaitIdle."
481 if (!ctx || !ctx->
inner)
490 if (!ctx || !ctx->
inner)
501 if (!ctx || !ctx->
inner)
514 if (!ctx || !ctx->
inner)
524 if (!ctx || !ctx->
inner)
535 FILE* file = fopen(filename,
"rb");
541 fseek(file, 0, SEEK_END);
542 size_t size = ftell(file);
543 fseek(file, 0, SEEK_SET);
545 uint32_t* buffer = malloc(size);
551 size_t read = fread(buffer, 1, size, file);
566 if (!ctx || !ctx->
inner)
574 "Destroy Shader called while GPU is busy! Missing sbgl_DeviceWaitIdle."
588 if (!ctx || !ctx->
inner)
598 if (!ctx || !ctx->
inner)
606 "Destroy Pipeline called while GPU is busy! Missing sbgl_DeviceWaitIdle."
620 if (!ctx || !ctx->
inner)
630 if (!ctx || !ctx->
inner)
638 "Destroy Compute Pipeline called while GPU is busy! Missing sbgl_DeviceWaitIdle."
650 if (!ctx || !ctx->
inner)
658 if (!ctx || !ctx->
inner)
666 if (!ctx || !ctx->
inner)
674 if (!ctx || !ctx->
inner)
682 if (!ctx || !ctx->
inner)
690 if (!ctx || !ctx->
inner)
699 if (!ctx || !ctx->
inner)
710 int32_t vertexOffset,
711 uint32_t instanceCount
713 if (!ctx || !ctx->
inner)
726 if (!ctx || !ctx->
inner)
734 if (!ctx || !ctx->
inner)
744 if (!ctx || !arena) {
759 queue->
arena = arena;
792 uint32_t index = queue->
count++;
824 if (!ctx || !ctx->
inner || !queues || queueCount == 0) {
831 uint32_t totalPackets = 0;
832 for (uint32_t i = 0; i < queueCount; ++i) {
834 totalPackets += queues[i]->
count;
838 if (totalPackets == 0) {
855 if (!mergedPackets || !mergedInstances || !keys || !indices) {
861 uint32_t current = 0;
862 for (uint32_t i = 0; i < queueCount; ++i) {
865 for (uint32_t j = 0; j < queues[i]->
count; ++j) {
866 mergedPackets[current] = queues[i]->
packets[j];
867 mergedInstances[current] = queues[i]->
instances[j];
868 keys[current] = mergedPackets[current].
key;
869 indices[current] = current;
873 queues[i]->
count = 0;
880 if (!temp_keys || !temp_indices) {
892 if (!sortedPackets || !sortedInstances) {
897 for (uint32_t i = 0; i < totalPackets; ++i) {
898 sortedPackets[i] = mergedPackets[indices[i]];
899 sortedInstances[i] = mergedInstances[indices[i]];
910 uint32_t commandCount =
sbgl_bake_commands(sortedPackets, totalPackets, commands, totalPackets);
919 if (commandCount > 0) {
935 uint64_t instanceAddress;
936 uint64_t userAddress;
939 pc.instanceAddress = bdaAddress;
940 pc.userAddress = userAddress;
API for the SiputBiru Graphics Library (SBgl).
void sbgl_gfx_Shutdown(sbgl_GfxContext *ctx)
void sbgl_gfx_BindComputePipeline(sbgl_GfxContext *ctx, sbgl_ComputePipeline handle)
void sbgl_gfx_Draw(sbgl_GfxContext *ctx, uint32_t vertexCount, uint32_t firstVertex, uint32_t instanceCount)
uint64_t sbgl_gfx_GetBufferDeviceAddress(sbgl_GfxContext *ctx, sbgl_Buffer handle)
Retrieves the 64-bit GPU virtual address for a buffer.
sbgl_GfxContext * sbgl_gfx_Init(sbgl_Window *window, struct SblArena *arena, const sbgl_ResourceLimits *limits, bool enableValidation)
Initializes the graphics backend with configurable resource limits.
sbgl_Buffer sbgl_gfx_CreateBuffer(sbgl_GfxContext *ctx, sbgl_BufferUsage usage, size_t size, const void *data)
void sbgl_gfx_EndRenderPass(sbgl_GfxContext *ctx)
Ends the current graphics rendering pass.
void sbgl_gfx_DestroyShader(sbgl_GfxContext *ctx, sbgl_Shader handle)
void sbgl_gfx_DispatchCompute(sbgl_GfxContext *ctx, uint32_t x, uint32_t y, uint32_t z)
void sbgl_gfx_DestroyPipeline(sbgl_GfxContext *ctx, sbgl_Pipeline handle)
void sbgl_gfx_BindPipeline(sbgl_GfxContext *ctx, sbgl_Pipeline handle)
void * sbgl_gfx_MapBuffer(sbgl_GfxContext *ctx, sbgl_Buffer handle)
bool sbgl_gfx_BeginFrame(sbgl_GfxContext *ctx)
Starts a new frame, acquiring an image and starting the command buffer.
void sbgl_gfx_MemoryBarrier(sbgl_GfxContext *ctx, sbgl_BarrierType type)
float sbgl_gfx_GetGpuTime(sbgl_GfxContext *ctx)
Retrieves the elapsed GPU time for the previous frame in milliseconds.
void sbgl_gfx_UnmapBuffer(sbgl_GfxContext *ctx, sbgl_Buffer handle)
void sbgl_gfx_DrawIndexed(sbgl_GfxContext *ctx, uint32_t indexCount, uint32_t firstIndex, int32_t vertexOffset, uint32_t instanceCount)
sbgl_Shader sbgl_gfx_LoadShader(sbgl_GfxContext *ctx, sbgl_ShaderStage stage, const uint32_t *bytecode, size_t size)
sbgl_Pipeline sbgl_gfx_CreatePipeline(sbgl_GfxContext *ctx, const sbgl_PipelineConfig *config)
void sbgl_gfx_PushConstants(sbgl_GfxContext *ctx, size_t size, const void *data)
sbgl_GfxTransientAllocation sbgl_gfx_AllocateTransient(sbgl_GfxContext *ctx, size_t size, uint32_t alignment)
Allocates a slice of GPU-visible memory for transient per-frame data.
void sbgl_gfx_DestroyBuffer(sbgl_GfxContext *ctx, sbgl_Buffer handle)
void sbgl_gfx_DeviceWaitIdle(sbgl_GfxContext *ctx)
void sbgl_gfx_DrawIndirect(sbgl_GfxContext *ctx, sbgl_Buffer handle, size_t offset, uint32_t drawCount)
Submits a batch of draw calls stored in a GPU buffer.
int32_t sbgl_gfx_GetLastVkResult(sbgl_GfxContext *ctx)
Retrieves the last VkResult from the backend for error inspection.
void sbgl_gfx_FillBuffer(sbgl_GfxContext *ctx, sbgl_Buffer handle, size_t offset, size_t size, uint32_t value)
Performs a hardware-accelerated buffer fill.
sbgl_ComputePipeline sbgl_gfx_CreateComputePipeline(sbgl_GfxContext *ctx, sbgl_Shader handle)
uint32_t sbgl_gfx_GetFrameIndex(sbgl_GfxContext *ctx)
Retrieves the current backend frame index.
void sbgl_gfx_DestroyComputePipeline(sbgl_GfxContext *ctx, sbgl_ComputePipeline handle)
void sbgl_gfx_BindBuffer(sbgl_GfxContext *ctx, sbgl_Buffer handle, sbgl_BufferUsage usage)
void sbgl_gfx_EndFrame(sbgl_GfxContext *ctx)
Submits the current frame's commands and presents the image.
void sbgl_gfx_BeginRenderPass(sbgl_GfxContext *ctx, float r, float g, float b, float a)
Starts a graphics rendering pass.
uint32_t sbgl_bake_commands(const sbgl_DrawPacket *packets, uint32_t packetCount, sbgl_IndirectCommand *outCommands, uint32_t maxCommands)
The baking pipeline for compressing draw packets into indirect commands.
Internal helpers for accessing context state from library subsystems.
void sbgl_SetClearColor(sbgl_Context *ctx, float r, float g, float b, float a)
Sets the clear color for the next frame.
sbgl_Result sbgl_GetResult(sbgl_Context *ctx)
Retrieves the last result code from the context.
void sbgl_RenderQueuesEx(sbgl_Context *ctx, sbgl_RenderQueue **queues, uint32_t queueCount, const sbgl_Mat4 *viewProj, uint64_t userAddress)
Extended version of sbgl_RenderQueues with user metadata.
void sbgl_DestroyComputePipeline(sbgl_Context *ctx, sbgl_ComputePipeline pipeline)
Destroys a compute pipeline.
void sbgl_DispatchCompute(sbgl_Context *ctx, uint32_t x, uint32_t y, uint32_t z)
Dispatches a compute workload.
void sbgl_FillBuffer(sbgl_Context *ctx, sbgl_Buffer buffer, size_t offset, size_t size, uint32_t value)
Fills a region of a GPU buffer with a fixed 32-bit value.
void sbgl_DrawIndirect(sbgl_Context *ctx, sbgl_Buffer buffer, size_t offset, uint32_t drawCount)
Submits a batch of draw calls stored in a GPU buffer.
sbgl_InitResult sbgl_Init(int w, int h, const char *title)
Initializes the engine and opens a window.
sbgl_Shader sbgl_LoadShaderFromFile(sbgl_Context *ctx, sbgl_ShaderStage stage, const char *filename)
Helper function to load a shader directly from a SPIR-V file.
void sbgl_SubmitDraw(sbgl_RenderQueue *queue, uint32_t mesh, uint32_t material, uint32_t blendMode, uint32_t sidedness, uint32_t tags, sbgl_SortKey key, const sbgl_InstanceData *data)
Appends a draw command to the render queue.
void sbgl_GetWindowSize(sbgl_Context *ctx, int *w, int *h)
Retrieves the current window dimensions.
sbgl_Telemetry sbgl_GetTelemetry(sbgl_Context *ctx)
Retrieves the performance telemetry data for the previous frame.
bool sbgl_WindowShouldClose(sbgl_Context *ctx)
Checks if the user or OS has requested to close the window.
sbgl_InitResult sbgl_InitWithConfig(const sbgl_InitConfig *config)
Initializes the engine and opens a window with explicit configuration.
const sbgl_InputState * sbgl_GetInputState(sbgl_Context *ctx)
Retrieves the input state for the current frame.
sbgl_Pipeline sbgl_CreatePipeline(sbgl_Context *ctx, const sbgl_PipelineConfig *config)
Creates a graphics pipeline.
void sbgl_DestroyShader(sbgl_Context *ctx, sbgl_Shader shader)
Destroys a shader module.
sbgl_Shader sbgl_LoadShader(sbgl_Context *ctx, sbgl_ShaderStage stage, const uint32_t *bytecode, size_t size)
Loads a shader from SPIR-V bytecode.
void sbgl_SetMouseMode(sbgl_Context *ctx, sbgl_MouseMode mode)
Sets the cursor behavior and visibility.
void sbgl_PushConstants(sbgl_Context *ctx, size_t size, const void *data)
Updates push constants for the currently bound pipeline.
void sbgl_EndDrawing(sbgl_Context *ctx)
Finalizes the current frame and presents it to the screen.
void sbgl_BindComputePipeline(sbgl_Context *ctx, sbgl_ComputePipeline pipeline)
Binds a compute pipeline for subsequent dispatch calls.
void sbgl_EndCompute(sbgl_Context *ctx)
Finalizes the compute phase.
sbgl_RenderQueue * sbgl_CreateRenderQueue(sbgl_Context *ctx, SblArena *arena)
Creates a thread-local render queue for collecting draw commands.
void sbgl_BindPipeline(sbgl_Context *ctx, sbgl_Pipeline pipeline)
Binds a graphics pipeline for subsequent draw calls.
void sbgl_DestroyBuffer(sbgl_Context *ctx, sbgl_Buffer buffer)
Destroys a GPU buffer.
void sbgl_BeginCompute(sbgl_Context *ctx)
Prepares the engine for compute operations before the main drawing pass.
sbgl_ComputePipeline sbgl_CreateComputePipeline(sbgl_Context *ctx, sbgl_Shader shader)
Creates a compute pipeline.
void * sbgl_MapBuffer(sbgl_Context *ctx, sbgl_Buffer buffer)
Maps a GPU buffer into the CPU's address space.
SblArena * sbgl_GetContextArena(sbgl_Context *ctx)
Retrieves the persistent arena associated with a context.
void sbgl_ClearResult(sbgl_Context *ctx)
Clears the result code to SBGL_SUCCESS.
void sbgl_MemoryBarrier(sbgl_Context *ctx, sbgl_BarrierType type)
Injects a memory barrier to synchronize compute and graphics operations.
void sbgl_UnmapBuffer(sbgl_Context *ctx, sbgl_Buffer buffer)
Unmaps a previously mapped GPU buffer.
sbgl_ErrorDetail sbgl_GetErrorDetail(sbgl_Context *ctx)
Retrieves detailed error information including backend-specific codes.
void sbgl_DeviceWaitIdle(sbgl_Context *ctx)
Synchronizes the CPU with the GPU, waiting for all commands to complete.
uint32_t sbgl_GetFrameIndex(sbgl_Context *ctx)
Retrieves the current frame index for double/triple buffering.
void sbgl_DrawIndexed(sbgl_Context *ctx, uint32_t indexCount, uint32_t firstIndex, int32_t vertexOffset, uint32_t instanceCount)
Submits an indexed draw command.
double sbgl_GetTime(sbgl_Context *ctx)
Retrieves the current monotonic system time in seconds.
void sbgl_Draw(sbgl_Context *ctx, uint32_t vertexCount, uint32_t firstVertex, uint32_t instanceCount)
Submits a non-indexed draw command.
uint64_t sbgl_GetBufferDeviceAddress(sbgl_Context *ctx, sbgl_Buffer buffer)
Retrieves the 64-bit GPU virtual address for a buffer.
void sbgl_Shutdown(sbgl_Context *ctx)
Gracefully shuts down the engine and releases all resources.
void sbgl_BeginDrawing(sbgl_Context *ctx)
Prepares the engine for a new frame of drawing.
void sbgl_BindBuffer(sbgl_Context *ctx, sbgl_Buffer buffer, sbgl_BufferUsage usage)
Binds a buffer to the pipeline.
void sbgl_DestroyPipeline(sbgl_Context *ctx, sbgl_Pipeline pipeline)
Destroys a graphics pipeline.
sbgl_Buffer sbgl_CreateBuffer(sbgl_Context *ctx, sbgl_BufferUsage usage, size_t size, const void *data)
Creates a GPU buffer.
void sbgl_RenderQueues(sbgl_Context *ctx, sbgl_RenderQueue **queues, uint32_t queueCount, const sbgl_Mat4 *viewProj)
Merges, sorts, and submits pending draw commands to the GPU.
#define sbgl_log_impl(level, category, msg)
static sbgl_Mat4 sbgl_Mat4Identity(void)
Returns an identity matrix.
static sbgl_Vec4 sbgl_Vec4Set(float x, float y, float z, float w)
Creates a Vec4.
void sbgl_radix_sort(sbgl_SortKey *keys, uint32_t *values, uint32_t count, sbgl_SortKey *temp_keys, uint32_t *temp_values)
Performs a stable radix sort on an array of 64-bit keys and associated 32-bit values.
Sorting utilities for the SBgl batching system.
sbgl_platform_Result
Result codes for platform layer operations.
sbgl_BufferUsage
Buffer usage flags.
sbgl_Result
Result codes for engine operations.
@ SBGL_ERROR_OUT_OF_MEMORY
@ SBGL_ERROR_WINDOW_CREATION_FAILED
@ SBGL_ERROR_GRAPHICS_FAILED
@ SBGL_ERROR_INVALID_ARGUMENT
@ SBGL_ERROR_NULL_CONTEXT
sbgl_BarrierType
Memory barrier types for compute synchronization.
uint32_t sbgl_Buffer
Handle for a GPU-side buffer.
uint32_t sbgl_Shader
Handle for a shader module.
uint64_t sbgl_SortKey
Bit-packed key used for sorting draw commands to minimize state changes.
sbgl_ShaderStage
Shader stage flags.
uint32_t sbgl_ComputePipeline
Handle for a compute pipeline.
uint32_t sbgl_Pipeline
Handle for a graphics pipeline.
#define SBGL_PACK_HEADER(mesh, mat, blend, sided, tags)
#define SBGL_INVALID_HANDLE
Arena allocator implementation.
#define SBL_ARENA_PUSH_ARRAY_ZERO(arena, type, count)
SBL_ARENA_DEF void sbl_arena_reset(SblArena *arena)
#define SBL_ARENA_PUSH_STRUCT_ZERO(arena, type)
SBL_ARENA_DEF void sbl_arena_free(SblArena *arena)
SBL_ARENA_DEF bool sbl_arena_init(SblArena *arena, uint64_t initial_size)
#define SBL_ARENA_PUSH_ARRAY(arena, type, count)
void * inner
Opaque pointer to the internal engine state.
sbgl_Result result
Status of the last major operation.
Encapsulates all data required to submit a single draw call. Optimized for cache density (16 bytes).
Detailed error information for debugging.
Represents a slice of a persistent GPU buffer used for transient data.
Standard Vulkan Indirect Draw command layout.
Configuration for engine initialization.
sbgl_ResourceLimits limits
Result structure for initialization.
Per-instance data for automated batching.
Internal state for the engine context.
sbgl_Telemetry currentFrame
struct sbgl_InternalContext::@8 state
4x4 Matrix, 16-byte aligned, column-major.
Configuration for creating a graphics pipeline.
Internal storage for draw packets awaiting submission.
sbgl_InstanceData * instances
sbgl_DrawPacket * packets
Performance telemetry data for a single frame.