15#include "voxel_mesh.h"
16#include "voxel_cull.h"
19#define SBGL_VOXEL_CHUNK_SIZE 256.0f
222 for (
int i = 0; i < 2; ++i) {
269 memset(mappedShell, 0, config->
max_slots *
sizeof(uint32_t));
273 for (
int i = 0; i < 2; ++i) {
316 if (abs(p.
x - camCX) <= radius && abs(p.
z - camCZ) <= radius && p.
y >= (camCY - 1) && p.
y <= (camCY + 2)) {
339 for (
int dz = -radius; dz <= radius; dz++) {
340 for (
int dx = -radius; dx <= radius; dx++) {
341 for (
int dy = -1; dy <= 2; dy++) {
342 sbgl_ivec3 pos = { camCX + dx, camCY + dy, camCZ + dz };
348 uint64_t maskAddr = maskBaseAddr + (slot * 65536);
349 uint64_t instAddr = instBaseAddr + (slot * 65536 * 8);
350 uint64_t countAddr = shellCountsBaseAddr + (slot * 4);
363 .maskAddress = maskAddr,
374 .maskAddress = maskAddr,
375 .instanceAddress = instAddr,
376 .counterAddress = countAddr,
385 if (aabbPtr) memcpy(aabbPtr + slot, &aabb,
sizeof(
sbgl_AABB));
397 if (abs(p.
x - camCX) > radius || abs(p.
z - camCZ) > radius || p.
y < (camCY - 1) || p.
y > (camCY + 2)) {
500 uint32_t visibleCount = 0;
501 uint32_t totalInstances = 0;
505 if (cmds[i].instanceCount > 0) {
513 printf(
"FPS: %u | GPU: %.2fms | Visible: %u/%u | Instances: %u\n",
544 for (
int i = 0; i < 2; ++i) {
549 for (
int i = 0; i < 2; ++i) {
void sbgl_DestroyComputePipeline(sbgl_Context *ctx, sbgl_ComputePipeline pipeline)
Destroys a compute pipeline.
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_Telemetry sbgl_GetTelemetry(sbgl_Context *ctx)
Retrieves the performance telemetry data for the previous frame.
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_PushConstants(sbgl_Context *ctx, size_t size, const void *data)
Updates push constants for the currently bound pipeline.
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.
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.
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.
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.
double sbgl_GetTime(sbgl_Context *ctx)
Retrieves the current monotonic system time in seconds.
uint64_t sbgl_GetBufferDeviceAddress(sbgl_Context *ctx, sbgl_Buffer buffer)
Retrieves the 64-bit GPU virtual address for a buffer.
sbgl_Buffer sbgl_CreateBuffer(sbgl_Context *ctx, sbgl_BufferUsage usage, size_t size, const void *data)
Creates a GPU buffer.
void sbgl_DispatchCompute(sbgl_Context *ctx, uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ)
Dispatches a compute workload.
Internal helpers for accessing context state from library subsystems.
SblArena * sbgl_GetContextArena(sbgl_Context *ctx)
Retrieves the persistent arena associated with a context.
static sbgl_Vec4 sbgl_Vec4Set(float x, float y, float z, float w)
Creates a Vec4.
void VoxelPool_UpdateFrame(VoxelPool *pool, uint64_t frame)
Advances the pool's internal frame counter for LRU tracking.
VoxelPool * VoxelPool_Init(SblArena *arena, uint32_t capacity)
Initializes a VoxelPool on the provided arena.
int32_t VoxelPool_AcquireSlot(VoxelPool *pool, sbgl_ivec3 pos, bool *is_new)
Acquires a slot for a given chunk position. Returns the slot index (0 to capacity-1)....
@ SBGL_BUFFER_USAGE_INDIRECT
@ SBGL_BUFFER_USAGE_TRANSFER_DST
@ SBGL_BUFFER_USAGE_STORAGE
@ SBGL_BARRIER_COMPUTE_TO_INDIRECT
@ SBGL_BARRIER_COMPUTE_TO_COMPUTE
@ SBGL_BARRIER_HOST_TO_GRAPHICS
@ SBGL_BARRIER_HOST_TO_COMPUTE
@ SBGL_BARRIER_COMPUTE_TO_GRAPHICS
uint32_t sbgl_Buffer
Handle for a GPU-side buffer.
uint32_t sbgl_Shader
Handle for a shader module.
@ SBGL_SHADER_STAGE_COMPUTE
uint32_t sbgl_ComputePipeline
Handle for a compute pipeline.
#define SBGL_INVALID_HANDLE
uint64_t sbgl_Voxel_GetInstanceAddress(sbgl_VoxelSystem *sys)
Returns the device address of the instance buffer.
void sbgl_Voxel_Cull(sbgl_VoxelSystem *sys, sbgl_Mat4 view_proj)
Performs GPU-driven frustum culling on voxel chunks. MUST be called BEFORE sbgl_BeginDrawing.
uint64_t sbgl_Voxel_GetPaletteAddress(sbgl_VoxelSystem *sys)
Returns the device address of the material palette buffer.
uint64_t sbgl_Voxel_GetAABBAddress(sbgl_VoxelSystem *sys)
Returns the device address of the AABB buffer.
void sbgl_Voxel_Update(sbgl_VoxelSystem *sys, sbgl_Vec3 camera_pos)
Updates the voxel system state based on the current camera position. This function handles chunk gene...
void sbgl_Voxel_Render(sbgl_VoxelSystem *sys)
Issues the indirect draw calls for visible voxel chunks. MUST be called BETWEEN sbgl_BeginDrawing and...
#define SBGL_VOXEL_CHUNK_SIZE
World-space size of a single voxel chunk in meters.
void sbgl_Voxel_Destroy(sbgl_VoxelSystem *sys)
Destroys the voxel system and releases all associated resources.
sbgl_VoxelSystem * sbgl_Voxel_Create(sbgl_Context *ctx, const sbgl_VoxelConfig *config)
Creates and initializes a new voxel system. This operation allocates GPU resources and internal track...
Public API for the SBgl voxel rendering system.
Arena allocator implementation.
#define SBL_ARENA_PUSH_ARRAY_ZERO(arena, type, count)
#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)
VoxelPool manages a fixed number of slots for voxel chunks. It uses a flat array layout to maintain c...
uint64_t * last_used_frames
Axis-Aligned Bounding Box (AABB).
Push constants for the frustum culling and indirect draw compute shader.
Push constants for the voxel generation compute shader.
Standard Vulkan Indirect Draw command layout.
4x4 Matrix, 16-byte aligned, column-major.
Push constants for the shell extraction compute shader.
Configuration parameters for initializing the voxel system.
bool enable_telemetry
Enables console output for performance metrics and visible chunk counts.
uint32_t max_slots
Maximum number of chunk slots to manage in the GPU-side pool. This value determines the total memory ...
uint32_t chunk_radius
Radius (in chunks) around the camera that should be maintained. Chunks outside this radius are candid...
Internal state for the voxel system. This structure adheres to Data-Oriented Design principles by mai...
sbgl_Buffer materialPaletteBuf
GPU buffer storing the material palette attributes.
sbgl_AABB * cpuAABB
CPU-side mirror of AABB data to avoid mapping the previous frame's GPU buffer.
sbgl_ComputePipeline shellPipe
Compute pipeline for shell/boundary processing.
sbgl_Vec3 camera_pos
Current world-space position of the camera.
sbgl_Buffer shellCountsBuf
GPU storage for shell/active instance counts.
sbgl_Buffer maskBuf
GPU buffer storing the presence mask for all active chunks.
uint32_t chunk_radius
Radius (in chunks) for visibility and generation logic.
uint32_t frame_idx
Monotonically increasing index for double-buffer rotation.
SblArena poolArena
Arena for the CPU-side management pool for chunk slots.
sbgl_ivec3 last_cam_chunk
World-space chunk coordinates of the camera in the previous frame.
sbgl_Context * ctx
Pointer to the active engine context.
double last_update_time
Time of the previous update for delta calculation.
sbgl_Buffer indirectCmdBuf[2]
Double-buffered GPU storage for indirect draw commands. Double buffering prevents CPU-GPU synchroniza...
sbgl_Buffer aabbBuf[2]
GPU buffer storing Axis-Aligned Bounding Boxes for culling. Double-buffered.
sbgl_Buffer instBuf
GPU buffer containing per-instance data for visible chunks.
sbgl_ComputePipeline genPipe
Compute pipeline for initial chunk generation.
sbgl_ComputePipeline cullPipe
Compute pipeline for frustum culling and indirect command generation.
float telemetry_timer
Timer for periodic telemetry reporting.
VoxelPool * pool
Pointer to the CPU-side management pool for chunk slots.
uint64_t total_frames
Total number of frames processed for LRU tracking.
bool enable_telemetry
Enables console output for performance metrics.
uint32_t fps_frames
Frame counter for FPS calculation.
3D integer vector for chunk coordinates.
3D Vector, 16-byte aligned and padded for SIMD safety.
4D Vector, 16-byte aligned.