SBgl 0.1.0
A graphics framework in C99
Loading...
Searching...
No Matches
Triangle Rendering Guide

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.
Definition sbgl_core.c:534
uint32_t sbgl_Shader
Handle for a shader module.
Definition sbgl_types.h:42
@ SBGL_SHADER_STAGE_FRAGMENT
Definition sbgl_types.h:135
@ SBGL_SHADER_STAGE_VERTEX
Definition sbgl_types.h:134

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.

sbgl_VertexAttribute attributes[] = {
{ .location = 0, .format = SBGL_FORMAT_R16G16B16A16_SNORM, .offset = offsetof(sbgl_Vertex, position) },
{ .location = 1, .format = SBGL_FORMAT_R8G8B8A8_UNORM, .offset = offsetof(sbgl_Vertex, color) }
};
.stride = sizeof(sbgl_Vertex),
.attributeCount = 2,
.attributes = attributes
};
@ SBGL_FORMAT_R8G8B8A8_UNORM
Definition sbgl_types.h:160
@ SBGL_FORMAT_R16G16B16A16_SNORM
Definition sbgl_types.h:159
Vertex attribute definition.
Definition sbgl_types.h:166
Vertex input layout definition.
Definition sbgl_types.h:186
Standard vertex structure for basic geometry rendering. Optimized for cache density (16 bytes).
Definition sbgl_types.h:28

Pipeline Creation

The graphics pipeline encapsulates the shader stages and the vertex layout.

.vertexShader = v_shd,
.fragmentShader = f_shd,
.vertexLayout = layout,
.blendMode = SBGL_BLEND_MODE_NONE // Opaque rendering (default)
};
sbgl_Pipeline sbgl_CreatePipeline(sbgl_Context *ctx, const sbgl_PipelineConfig *config)
Creates a graphics pipeline.
Definition sbgl_core.c:587
@ SBGL_BLEND_MODE_NONE
Definition sbgl_types.h:196
uint32_t sbgl_Pipeline
Handle for a graphics pipeline.
Definition sbgl_types.h:47
Configuration for creating a graphics pipeline.
Definition sbgl_types.h:204

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.

sbgl_Vertex vertices[] = {
{ .position = { 0, 16383, 0, 0 }, .color = 0xFF0000FF }, // Top (Red)
{ .position = { 16383, -16383, 0, 0 }, .color = 0xFF00FF00 }, // Bottom Right (Green)
{ .position = { -16383, -16383, 0, 0 }, .color = 0xFFFF0000 } // Bottom Left (Blue)
};
sbgl_Buffer vbo = sbgl_CreateBuffer(ctx, SBGL_BUFFER_USAGE_VERTEX, sizeof(vertices), vertices);
sbgl_Buffer sbgl_CreateBuffer(sbgl_Context *ctx, sbgl_BufferUsage usage, size_t size, const void *data)
Creates a GPU buffer.
Definition sbgl_core.c:445
@ SBGL_BUFFER_USAGE_VERTEX
Definition sbgl_types.h:123
uint32_t sbgl_Buffer
Handle for a GPU-side buffer.
Definition sbgl_types.h:37

Drawing

sbgl_Draw(ctx, 3, 0, 1); // 3 vertices, starting at 0, 1 instance
void sbgl_EndDrawing(sbgl_Context *ctx)
Finalizes the current frame and presents it to the screen.
Definition sbgl_core.c:315
void sbgl_BindPipeline(sbgl_Context *ctx, sbgl_Pipeline pipeline)
Binds a graphics pipeline for subsequent draw calls.
Definition sbgl_core.c:673
void sbgl_Draw(sbgl_Context *ctx, uint32_t vertexCount, uint32_t firstVertex, uint32_t instanceCount)
Submits a non-indexed draw command.
Definition sbgl_core.c:698
void sbgl_BeginDrawing(sbgl_Context *ctx)
Prepares the engine for a new frame of drawing.
Definition sbgl_core.c:262
void sbgl_BindBuffer(sbgl_Context *ctx, sbgl_Buffer buffer, sbgl_BufferUsage usage)
Binds a buffer to the pipeline.
Definition sbgl_core.c:681

Resource Cleanup

Resources must be destroyed in the reverse order of their creation after the GPU has finished executing all commands.

// Ensure the GPU is no longer using any resources
sbgl_DestroyShader(ctx, v_shd);
sbgl_DestroyShader(ctx, f_shd);
// Shutdown the engine and close the window
void sbgl_DestroyShader(sbgl_Context *ctx, sbgl_Shader shader)
Destroys a shader module.
Definition sbgl_core.c:565
void sbgl_DestroyBuffer(sbgl_Context *ctx, sbgl_Buffer buffer)
Destroys a GPU buffer.
Definition sbgl_core.c:457
void sbgl_DeviceWaitIdle(sbgl_Context *ctx)
Synchronizes the CPU with the GPU, waiting for all commands to complete.
Definition sbgl_core.c:391
void sbgl_Shutdown(sbgl_Context *ctx)
Gracefully shuts down the engine and releases all resources.
Definition sbgl_core.c:204
void sbgl_DestroyPipeline(sbgl_Context *ctx, sbgl_Pipeline pipeline)
Destroys a graphics pipeline.
Definition sbgl_core.c:597