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{
 17    AlbaString string; // needed?
 18    AlbaVector position;
 19    // Make part of style?
 20    FT_Face face; // needed?
 21    AlbaTextStyle style;
 22    uint32_t glyph_count;
 23    hb_glyph_info_t* glyphs_infos;
 24    hb_glyph_position_t* glyph_positions;
 25} Text;
 26
 27typedef struct
 28{
 29    int buffers_dirty;
 30    AlbaArray new_vertices;
 31    AlbaArray new_attributes;
 32    AlbaArray new_indices;
 33    WGPUBuffer vertices;
 34    WGPUBuffer attributes;
 35    WGPUBuffer indices;
 36
 37    int text_dirty;
 38    AlbaArray new_texts;
 39
 40    int bind_group_dirty;
 41    int texture_set;
 42    WGPUTexture texture;
 43    WGPUTextureView view;
 44    WGPUSampler sampler;
 45    WGPUBindGroup bind_group;
 46} DrawCall;
 47
 48void draw_call_generate_text(const AlbaWindow* window, DrawCall* draw_call);
 49void draw_call_set_texture(DrawCall* draw_call, WGPUTexture texture);
 50void draw_call_update_bind_group(const AlbaWindow* window, DrawCall* draw_call);
 51void draw_call_release(const DrawCall* draw_call);
 52
 53typedef struct AlbaWindowImpl
 54{
 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} AlbaWindow;
 72
 73DrawCall* window_create_draw_call(AlbaWindow* window);
 74void window_set_texture(AlbaWindow* window, WGPUTexture texture);
 75void window_clear(AlbaWindow* window);
 76
 77// Atlas
 78typedef struct
 79{
 80    // Key properties
 81    uint32_t codepoint;
 82    AlbaTextStyle style;
 83    // Other properties used during atlas creation
 84    // and text rendering
 85    FT_Long glyph_index;
 86    FT_Glyph_Metrics metrics;
 87    FT_Bitmap bitmap;
 88    AlbaVector start;
 89    AlbaVector end;
 90} AlbaGlyph;
 91
 92typedef struct
 93{
 94    struct hashmap* glyphs;
 95    int dirty;
 96    AlbaArray glyph_sizes; // in w, h order
 97    AlbaVector size;
 98    uint8_t* image;
 99} Atlas;
100
101Atlas create_atlas(uint32_t capacity);
102void atlas_reserve(Atlas* atlas, uint32_t capacity);
103void atlas_append(Atlas* atlas, FT_Face face, uint32_t codepoint, AlbaTextStyle style);
104void atlas_generate_image(Atlas* atlas);
105void atlas_release(const Atlas* atlas);
106
107// Helpers
108WGPUSurface get_window_surface(WGPUInstance instance, AlbaWindow* window);
109
110WGPUBuffer create_buffer(
111    const AlbaWindow* window,
112    uint64_t size, const void* data,
113    WGPUBufferUsageFlags flags
114);
115void release_buffer(WGPUBuffer buffer);
116WGPUBuffer update_buffer(
117    const AlbaWindow* window, WGPUBuffer buffer, WGPUBufferUsage usage, AlbaArray data
118);
119
120WGPUTexture create_texture(
121    const AlbaWindow* window,
122    WGPUTextureUsage usage,
123    AlbaVector size,
124    WGPUTextureFormat format, uint8_t samples,
125    const void* data
126);
127void release_texture(WGPUTexture texture);
128
129// Global variables
130extern int glfw_initialized;
131extern pthread_mutex_t glfw_mutex;
132
133extern FT_Library freetype;
134extern FT_Face roboto;
135extern Atlas atlas;
136extern pthread_mutex_t freetype_mutex;
137
138#endif // INTERNAL_H