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