14 {
17 return 1;
19
22
27
32
33
42
43
49 };
50
51 uint32_t indices[] = {
52 0,
53 2,
54 1,
55
56
57 0,
58 3,
59 2,
60 2,
61 1,
62 0,
63 4,
64 5,
65 6,
66 6,
67 7,
68 4,
69 0,
70 4,
71 7,
72 7,
73 3,
74 0,
75 1,
76 2,
77 6,
78 6,
79 5,
80 1,
81 2,
82 3,
83 7,
84 7,
85 6,
86 2,
87 0,
88 1,
89 5,
90 5,
91 4,
92 0,
93
94
95 0,
96 2,
97 1,
98 0,
99 3,
100 2,
101 0,
102 4,
103 3,
104 0,
105 1,
106 4,
107 1,
108 2,
109 3,
110 3,
111 4,
112 1
113 };
114
117
121 };
122
124 .fragmentShader = frag,
125 .vertexLayout = {
sizeof(
sbgl_Vertex), 2, attributes } };
126
129
130 int width, height;
137
138 float pitch = -0.2f;
140 bool mouse_locked = false;
141 float move_speed = 100.0f;
142 float mouse_sensitivity = 0.005f;
143
145 double last_time = start_time;
146 float fps_timer = 0.0f;
147 int frame_count = 0;
148
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");
155
159
163 break;
164 }
165
167 float dt = (float)(current_time - last_time);
168 last_time = current_time;
169 float time = (float)(current_time - start_time);
170
171
172 frame_count++;
173 fps_timer += dt;
174 if (fps_timer >= 1.0f) {
175 printf(
176 "FPS: %d | Frame Time: %.2f ms\n",
177 frame_count,
178 (fps_timer / (float)frame_count) * 1000.0f
179 );
180 fps_timer = 0.0f;
181 frame_count = 0;
182 }
183
185 mouse_locked = !mouse_locked;
187 ctx,
189 );
190 }
191
192 if (mouse_locked) {
193 yaw += (float)input->
mouseDeltaX * mouse_sensitivity;
194 pitch -= (float)input->
mouseDeltaY * mouse_sensitivity;
195
196 if (pitch > 1.5f)
197 pitch = 1.5f;
198 if (pitch < -1.5f)
199 pitch = -1.5f;
200 }
201
203 front.
x = cosf(yaw) * cosf(pitch);
204 front.
y = sinf(pitch);
205 front.
z = sinf(yaw) * cosf(pitch);
207
210
211 float velocity = move_speed * dt;
212
225 );
230 );
231
234
236
237
238 for (int i = 0; i < 10000; i++) {
239 uint32_t meshId = i % 3;
240
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
246 );
247
252 );
253
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;
258
259
261 }
262
266
268
270 }
271
273
279
282
283 return 0;
284}
#define PACK_POS(x, y, z)
#define PACK_COL(r, g, b, a)
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.
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.
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.
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.