This modular guide details the rendering of a triangle.
Shader Loading
SBgl provides a helper to load shaders directly from SPIR-V files.
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.
uint32_t sbgl_Shader
Handle for a shader module.
@ SBGL_SHADER_STAGE_FRAGMENT
@ SBGL_SHADER_STAGE_VERTEX
Vertex Layout
A vertex layout describes how the GPU should interpret raw vertex data. The system utilizes the sbgl_Vertex structure, which is optimized for cache density (16 bytes). It consists of a 4D position (3D + padding) using 16-bit signed normalized integers (SNORM) and a packed RGBA8 color.
};
.attributeCount = 2,
.attributes = attributes
};
@ SBGL_FORMAT_R8G8B8A8_UNORM
@ SBGL_FORMAT_R16G16B16A16_SNORM
Vertex attribute definition.
Vertex input layout definition.
Standard vertex structure for basic geometry rendering. Optimized for cache density (16 bytes).
Pipeline Creation
The graphics pipeline encapsulates the shader stages and the vertex layout.
.vertexShader = v_shd,
.fragmentShader = f_shd,
.vertexLayout = layout,
};
sbgl_Pipeline sbgl_CreatePipeline(sbgl_Context *ctx, const sbgl_PipelineConfig *config)
Creates a graphics pipeline.
uint32_t sbgl_Pipeline
Handle for a graphics pipeline.
Configuration for creating a graphics pipeline.
Vertex Buffer
Vertices are initialized using the sbgl_Vertex structure. Positions are quantized to the 16-bit range (-32767 to 32767), representing -1.0 to 1.0.
{ .position = { 0, 16383, 0, 0 }, .color = 0xFF0000FF },
{ .position = { 16383, -16383, 0, 0 }, .color = 0xFF00FF00 },
{ .position = { -16383, -16383, 0, 0 }, .color = 0xFFFF0000 }
};
sbgl_Buffer sbgl_CreateBuffer(sbgl_Context *ctx, sbgl_BufferUsage usage, size_t size, const void *data)
Creates a GPU buffer.
@ SBGL_BUFFER_USAGE_VERTEX
uint32_t sbgl_Buffer
Handle for a GPU-side buffer.
Drawing
void sbgl_EndDrawing(sbgl_Context *ctx)
Finalizes the current frame and presents it to the screen.
void sbgl_BindPipeline(sbgl_Context *ctx, sbgl_Pipeline pipeline)
Binds a graphics pipeline for subsequent draw calls.
void sbgl_Draw(sbgl_Context *ctx, uint32_t vertexCount, uint32_t firstVertex, uint32_t instanceCount)
Submits a non-indexed draw command.
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.
Resource Cleanup
Resources must be destroyed in the reverse order of their creation after the GPU has finished executing all commands.
void sbgl_DestroyShader(sbgl_Context *ctx, sbgl_Shader shader)
Destroys a shader module.
void sbgl_DestroyBuffer(sbgl_Context *ctx, sbgl_Buffer buffer)
Destroys a GPU buffer.
void sbgl_DeviceWaitIdle(sbgl_Context *ctx)
Synchronizes the CPU with the GPU, waiting for all commands to complete.
void sbgl_Shutdown(sbgl_Context *ctx)
Gracefully shuts down the engine and releases all resources.
void sbgl_DestroyPipeline(sbgl_Context *ctx, sbgl_Pipeline pipeline)
Destroys a graphics pipeline.