src/internal.h
 1#ifndef INTERNAL_H
 2#define INTERNAL_H
 3
 4#include "alba.h"
 5
 6#include <pthread.h>
 7
 8#include "hashmap.h"
 9
10#define NO_TEXTURE {-1, -1}
11
12typedef struct RenderPassDataImpl
13{
14    // if set, the next frame will update the buffers
15    int dirty;
16    AlbaDynArray new_vertices;
17    WGPUBuffer vertices;
18    AlbaDynArray new_attributes;
19    WGPUBuffer attributes;
20    AlbaDynArray new_indices;
21    WGPUBuffer indices;
22    WGPUBuffer uniforms;
23    WGPUTexture texture;
24    WGPURenderPipeline pipeline;
25    WGPUBindGroup bind_group;
26} RenderPassData;
27
28typedef struct AlbaWindowImpl
29{
30    AlbaWindowOptions options;
31    GLFWwindow* glfw_window;
32    AlbaVector size;
33    AlbaVector scale;
34    WGPUInstance instance;
35    WGPUSurface surface;
36    WGPUAdapter adapter;
37    WGPUDevice device;
38    WGPUQueue queue;
39    WGPUShaderModule shaders;
40    RenderPassData drawing;
41    RenderPassData text;
42} AlbaWindow;
43
44// Atlas
45typedef struct
46{
47    uint32_t character;
48    FT_Long glyph_index;
49    FT_Glyph_Metrics metrics;
50    FT_Bitmap bitmap;
51    AlbaVector start;
52    AlbaVector end;
53} AlbaGlyph;
54
55typedef struct
56{
57    struct hashmap* glyphs;
58    int dirty;
59    AlbaDynArray glyph_sizes; // in w, h order
60    AlbaVector size;
61    uint8_t* image;
62} AlbaAtlas;
63
64AlbaAtlas atlas_new(uint32_t capacity);
65void atlas_reserve(AlbaAtlas* atlas, uint32_t capacity);
66void atlas_append(AlbaAtlas* atlas, FT_Face face, uint32_t character);
67void atlas_generate_image(AlbaAtlas* atlas);
68void atlas_release(const AlbaAtlas* atlas);
69
70// Helpers
71WGPUTexture create_texture(
72    const AlbaWindow* window,
73    WGPUTextureUsage usage,
74    AlbaVector size,
75    WGPUTextureFormat format, uint8_t samples,
76    const void* data
77);
78
79// Drawng
80void draw_triangles_indexed_render_pass(
81    RenderPassData* data,
82    uint32_t num_vertices,
83    const AlbaVector* vertices,
84    const AlbaAttribute* attributes,
85    uint32_t num_indices,
86    uint32_t* indices
87);
88
89// Globals
90static FT_Library freetype = NULL;
91static FT_Face roboto = NULL;
92static AlbaAtlas atlas;
93static pthread_mutex_t freetype_mutex = PTHREAD_MUTEX_INITIALIZER;
94
95#endif // INTERNAL_H