summaryrefslogtreecommitdiff
path: root/src/atlas.cc
diff options
context:
space:
mode:
authorFabrice <fabrice@schaub-dev.xyz>2026-02-13 07:09:38 +0100
committerFabrice <fabrice@schaub-dev.xyz>2026-02-13 07:09:38 +0100
commite40037fd7a922132015340a758468cb5dec71096 (patch)
tree5f57004ffc0bed7c0d1b2e3ae5a45df104d9eabd /src/atlas.cc
parent2600ac4737d0098d650b792a30ccc557f6d76d16 (diff)
rasterizing and caching
Diffstat (limited to 'src/atlas.cc')
-rw-r--r--src/atlas.cc53
1 files changed, 52 insertions, 1 deletions
diff --git a/src/atlas.cc b/src/atlas.cc
index ad697f1..5e7d5c2 100644
--- a/src/atlas.cc
+++ b/src/atlas.cc
@@ -2,6 +2,7 @@
#include <cstring>
+#include "cglm/types.h"
#include "mimalloc.h"
#include "utils.h"
@@ -55,12 +56,62 @@ enum atlas_error_e wayc_atlas_init(struct atlas_s* atlas, u32 width, u32 height,
atlas->format = format;
atlas->cpu_atlas = cpu_atlas;
atlas->gpu_atlas = gpu_atlas;
- atlas->cpu_dirty = false;
success = true;
return ATLAS_ERROR_NONE;
}
+enum atlas_error_e wayc_atlas_insert(struct atlas_s* atlas, u32 width,
+ u32 height, const u8* data, vec2* uv0,
+ vec2* uv1) {
+ wayc_notnull(atlas);
+ wayc_notnull(data);
+ wayc_notnull(uv0);
+ wayc_notnull(uv1);
+
+ struct atlas_packer_s* packer = &atlas->packer;
+
+ u32 x = WAYC_X(packer->cursor);
+ u32 y = WAYC_Y(packer->cursor);
+
+ // wrap
+ if (x + width > atlas->width) {
+ x = 0;
+ y += packer->row_height;
+ packer->row_height = 0;
+ }
+
+ if (y + height > atlas->height) return ATLAS_ERROR_ATLAS_FULL;
+
+ const int atlas_pitch =
+ sg_query_surface_pitch(atlas->format, atlas->width, 1, 1);
+ const int bpp = sg_query_surface_pitch(atlas->format, 1, 1, 1);
+ const int src_pitch = width * bpp;
+
+ for (u32 row = 0; row < height; row++) {
+ const u8* src_row = data + row * src_pitch;
+ u8* dst_row = atlas->cpu_atlas + (y + row) * atlas_pitch;
+ u8* dst_px = dst_row + x * bpp;
+
+ memcpy(dst_px, src_row, src_pitch);
+ }
+
+ const f32 W = (f32)atlas->width;
+ const f32 H = (f32)atlas->height;
+
+ WAYC_X(*uv0) = (f32)x / W;
+ WAYC_Y(*uv0) = (f32)y / H;
+ WAYC_X(*uv1) = (f32)(x + width) / W;
+ WAYC_Y(*uv1) = (f32)(y + height) / H;
+
+ WAYC_X(packer->cursor) = x + width;
+ WAYC_Y(packer->cursor) = y;
+ packer->row_height = wayc_max(packer->row_height, height);
+ atlas->cpu_dirty = true;
+
+ return ATLAS_ERROR_NONE;
+}
+
enum atlas_error_e wayc_atlas_flush(struct atlas_s* atlas) {
wayc_notnull(atlas);
if (!atlas->cpu_dirty) return ATLAS_ERROR_NONE;