src/internal.h
  1#ifndef INTERNAL_H
  2#define INTERNAL_H
  3
  4#include "alba.h"
  5
  6#include <pthread.h>
  7
  8#include "GLFW/glfw3.h"
  9#include "hashmap.h"
 10#include "hb.h"
 11
 12#define NO_TEXTURE {-1, -1}
 13
 14// Window
 15typedef struct {
 16    AlbaString string; // needed?
 17    AlbaVector position;
 18    // Make part of style?
 19    FT_Face face; // needed?
 20    AlbaTextStyle style;
 21    uint32_t glyph_count;
 22    hb_glyph_info_t *glyphs_infos;
 23    hb_glyph_position_t *glyph_positions;
 24} Text;
 25
 26typedef struct {
 27    int buffers_dirty;
 28    AlbaArray new_vertices;
 29    AlbaArray new_attributes;
 30    AlbaArray new_indices;
 31    WGPUBuffer vertices;
 32    WGPUBuffer attributes;
 33    WGPUBuffer indices;
 34
 35    int text_dirty;
 36    AlbaArray new_texts;
 37
 38    int bind_group_dirty;
 39    int texture_set;
 40    WGPUTexture texture;
 41    WGPUTextureView view;
 42    WGPUSampler sampler;
 43    WGPUBindGroup bind_group;
 44} DrawCall;
 45
 46void draw_call_generate_text(const AlbaWindow *window, DrawCall *draw_call);
 47
 48void draw_call_set_texture(DrawCall *draw_call, WGPUTexture texture);
 49
 50void draw_call_update_bind_group(const AlbaWindow *window, DrawCall *draw_call);
 51
 52void draw_call_release(const DrawCall *draw_call);
 53
 54typedef struct AlbaWindowImpl {
 55    AlbaWindowOptions options;
 56    GLFWwindow *glfw_window;
 57    AlbaVector size;
 58    AlbaVector scale;
 59    WGPUInstance instance;
 60    WGPUSurface surface;
 61    WGPUAdapter adapter;
 62    WGPUDevice device;
 63    WGPUQueue queue;
 64    WGPUCommandEncoder command_encoder;
 65    WGPUShaderModule shaders;
 66    WGPURenderPipeline pipeline;
 67    WGPUBuffer uniforms;
 68    WGPUBindGroup uniforms_bind_group;
 69    WGPUBindGroupLayout texture_bind_group_layout;
 70    AlbaArray draw_calls;
 71    WGPUTextureFormat surface_format;
 72} AlbaWindow;
 73
 74DrawCall *window_create_draw_call(AlbaWindow *window);
 75
 76void window_set_texture(AlbaWindow *window, WGPUTexture texture);
 77
 78void window_clear(AlbaWindow *window);
 79
 80// Atlas
 81typedef struct {
 82    // Key properties
 83    uint32_t codepoint;
 84    AlbaTextStyle style;
 85    // Other properties used during atlas creation
 86    // and text rendering
 87    FT_Long glyph_index;
 88    FT_Glyph_Metrics metrics;
 89    FT_Bitmap bitmap;
 90    AlbaVector start;
 91    AlbaVector end;
 92} AlbaGlyph;
 93
 94typedef struct {
 95    struct hashmap *glyphs;
 96    int dirty;
 97    AlbaArray glyph_sizes; // in w, h order
 98    AlbaVector size;
 99    uint8_t *image;
100} Atlas;
101
102Atlas create_atlas(uint32_t capacity);
103
104void atlas_reserve(Atlas *atlas, uint32_t capacity);
105
106void atlas_append(Atlas *atlas, FT_Face face, uint32_t codepoint, AlbaTextStyle style);
107
108void atlas_generate_image(Atlas *atlas);
109
110void atlas_release(const Atlas *atlas);
111
112// Helpers
113WGPUSurface get_window_surface(WGPUInstance instance, AlbaWindow *window);
114
115WGPUBuffer create_buffer(
116    const AlbaWindow *window,
117    uint64_t size, const void *data,
118    WGPUBufferUsageFlags flags
119);
120
121void release_buffer(WGPUBuffer buffer);
122
123WGPUBuffer update_buffer(
124    const AlbaWindow *window, WGPUBuffer buffer, WGPUBufferUsage usage, AlbaArray data
125);
126
127WGPUTexture create_texture(
128    const AlbaWindow *window,
129    WGPUTextureUsage usage,
130    AlbaVector size,
131    WGPUTextureFormat format, uint8_t samples,
132    const void *data
133);
134
135void release_texture(WGPUTexture texture);
136
137// Global variables
138extern int glfw_initialized;
139extern pthread_mutex_t glfw_mutex;
140
141extern FT_Library freetype;
142extern FT_Face roboto;
143extern Atlas atlas;
144extern pthread_mutex_t freetype_mutex;
145
146#endif // INTERNAL_H