8#include "batching_vert.h"
9#include "batching_frag.h"
11#define PACK_POS(x, y, z) { (int16_t)((x) * 32767.0f), (int16_t)((y) * 32767.0f), (int16_t)((z) * 32767.0f), 32767 }
12#define PACK_COL(r, g, b, a) (uint32_t)(((uint32_t)((r)*255.0f) << 0) | ((uint32_t)((g)*255.0f) << 8) | ((uint32_t)((b)*255.0f) << 16) | ((uint32_t)((a)*255.0f) << 24))
51 uint32_t indices[] = {
124 .fragmentShader = frag,
125 .vertexLayout = {
sizeof(
sbgl_Vertex), 2, attributes } };
140 bool mouse_locked =
false;
141 float move_speed = 100.0f;
142 float mouse_sensitivity = 0.005f;
145 double last_time = start_time;
146 float fps_timer = 0.0f;
149 printf(
"--- Batching Controls ---\n");
150 printf(
"W/A/S/D: Move\n");
151 printf(
"Q/E: Vertical Move\n");
152 printf(
"TAB: Lock/Unlock Mouse\n");
153 printf(
"ESC: Exit\n");
154 printf(
"-------------------------\n");
167 float dt = (float)(current_time - last_time);
168 last_time = current_time;
169 float time = (float)(current_time - start_time);
174 if (fps_timer >= 1.0f) {
176 "FPS: %d | Frame Time: %.2f ms\n",
178 (fps_timer / (
float)frame_count) * 1000.0f
185 mouse_locked = !mouse_locked;
193 yaw += (float)input->
mouseDeltaX * mouse_sensitivity;
194 pitch -= (float)input->
mouseDeltaY * mouse_sensitivity;
203 front.
x = cosf(yaw) * cosf(pitch);
204 front.
y = sinf(pitch);
205 front.
z = sinf(yaw) * cosf(pitch);
211 float velocity = move_speed * dt;
238 for (
int i = 0; i < 10000; i++) {
239 uint32_t meshId = i % 3;
241 float angle = time + (float)i;
243 ((
float)(i % 100) - 50.0f) * 10.0f,
244 ((
float)(i / 100 % 100) - 50.0f) * 10.0f,
245 ((
float)(i / 10000) - 0.5f) * 10.0f
254 float r = (float)(i % 255) / 255.0f;
255 float g = (float)((i * 7) % 255) / 255.0f;
256 float b = (float)((i * 13) % 255) / 255.0f;
#define PACK_POS(x, y, z)
#define PACK_COL(r, g, b, a)
API for the SiputBiru Graphics Library (SBgl).
sbgl_InitResult sbgl_Init(int w, int h, const char *title)
Initializes the engine and opens a window.
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.
bool sbgl_WindowShouldClose(sbgl_Context *ctx)
Checks if the user or OS has requested to close the window.
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_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.
#define sbgl_Clear
Backward compatibility alias for sbgl_SetClearColor.
void sbgl_DestroyBuffer(sbgl_Context *ctx, sbgl_Buffer buffer)
Destroys a GPU buffer.
sbgl_RenderQueue * sbgl_CreateRenderQueue(sbgl_Context *ctx, struct SblArena *arena)
Creates a thread-local render queue for collecting draw commands.
void sbgl_DeviceWaitIdle(sbgl_Context *ctx)
Synchronizes the CPU with the GPU, waiting for all commands to complete.
double sbgl_GetTime(sbgl_Context *ctx)
Retrieves the current monotonic system time in seconds.
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.
Camera system and batch collision math for SBgl.
static sbgl_Camera sbgl_CameraPerspective(float fov_y_rad, float aspect, float near_p, float far_p)
Initializes a camera with perspective projection.
static sbgl_Mat4 sbgl_CameraGetProjection(const sbgl_Camera *cam)
Computes the projection matrix for the given camera.
static sbgl_Mat4 sbgl_CameraGetView(const sbgl_Camera *cam)
Computes the view matrix for the given camera.
Single-header math library for SBgl.
static sbgl_Vec3 sbgl_Vec3Set(float x, float y, float z)
Creates a Vec3, correctly padded.
static sbgl_Vec3 sbgl_Vec3Add(sbgl_Vec3 a, sbgl_Vec3 b)
Adds two Vec3 vectors.
static sbgl_Vec3 sbgl_Vec3Mul(sbgl_Vec3 a, float s)
Multiplies a Vec3 by a scalar.
static sbgl_Mat4 sbgl_Mat4Translate(sbgl_Vec3 v)
Creates a translation matrix.
static sbgl_Mat4 sbgl_Mat4Rotate(float angle_rad, sbgl_Vec3 axis)
Creates a rotation matrix from axis and angle.
static sbgl_Mat4 sbgl_Mat4Mul(sbgl_Mat4 a, sbgl_Mat4 b)
Multiplies two matrices.
static sbgl_Vec3 sbgl_Vec3Cross(sbgl_Vec3 a, sbgl_Vec3 b)
Computes the cross product of two Vec3 vectors.
static sbgl_Vec3 sbgl_Vec3Normalize(sbgl_Vec3 v)
Normalizes a Vec3.
static sbgl_Vec4 sbgl_Vec4Set(float x, float y, float z, float w)
Creates a Vec4.
static sbgl_Vec3 sbgl_Vec3Sub(sbgl_Vec3 a, sbgl_Vec3 b)
Subtracts b from a.
@ SBGL_BUFFER_USAGE_INDEX
@ SBGL_BUFFER_USAGE_VERTEX
@ SBGL_FORMAT_R8G8B8A8_UNORM
@ SBGL_FORMAT_R16G16B16A16_SNORM
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_SHADER_STAGE_FRAGMENT
@ SBGL_SHADER_STAGE_VERTEX
uint32_t sbgl_Pipeline
Handle for a graphics pipeline.
Arena allocator implementation.
SBL_ARENA_DEF void sbl_arena_free(SblArena *arena)
SBL_ARENA_DEF bool sbl_arena_init(SblArena *arena, uint64_t initial_size)
Stateful camera representation.
Result structure for initialization.
Per-instance data for automated batching.
4x4 Matrix, 16-byte aligned, column-major.
Configuration for creating a graphics pipeline.
Internal storage for draw packets awaiting submission.
Vertex attribute definition.
Standard vertex structure for basic geometry rendering. Optimized for cache density (16 bytes).
3D Vector, 16-byte aligned and padded for SIMD safety.