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
 13{
 14    // if set, the next frame will update the buffers
 15    int dirty;
 16    AlbaArray new_vertices;
 17    AlbaArray new_attributes;
 18    AlbaArray new_indices;
 19    WGPUBuffer vertices;
 20    WGPUBuffer attributes;
 21    WGPUBuffer indices;
 22    WGPUTexture texture;
 23    WGPUTextureView view;
 24    WGPUBindGroup bind_group;
 25} DrawCall;
 26
 27void create_draw_call();
 28void draw_call_release(const DrawCall* draw_call);
 29
 30typedef struct AlbaWindowImpl
 31{
 32    AlbaWindowOptions options;
 33    GLFWwindow* glfw_window;
 34    AlbaVector size;
 35    AlbaVector scale;
 36    WGPUInstance instance;
 37    WGPUSurface surface;
 38    WGPUAdapter adapter;
 39    WGPUDevice device;
 40    WGPUQueue queue;
 41    WGPUShaderModule shaders;
 42    WGPURenderPipeline pipeline;
 43    WGPUBuffer uniforms;
 44    WGPUBindGroup uniforms_bind_group;
 45    AlbaArray draw_calls;
 46} AlbaWindow;
 47
 48// Atlas
 49typedef struct
 50{
 51    uint32_t character;
 52    FT_Long glyph_index;
 53    FT_Glyph_Metrics metrics;
 54    FT_Bitmap bitmap;
 55    AlbaVector start;
 56    AlbaVector end;
 57} AlbaGlyph;
 58
 59typedef struct
 60{
 61    struct hashmap* glyphs;
 62    int dirty;
 63    AlbaArray glyph_sizes; // in w, h order
 64    AlbaVector size;
 65    uint8_t* image;
 66} AlbaAtlas;
 67
 68AlbaAtlas create_atlas(uint32_t capacity);
 69void atlas_reserve(AlbaAtlas* atlas, uint32_t capacity);
 70void atlas_append(AlbaAtlas* atlas, FT_Face face, uint32_t character);
 71void atlas_generate_image(AlbaAtlas* atlas);
 72void atlas_release(const AlbaAtlas* atlas);
 73
 74// Helpers
 75WGPUSurface get_window_surface(WGPUInstance instance, AlbaWindow* window);
 76WGPUTexture create_texture(
 77    const AlbaWindow* window,
 78    WGPUTextureUsage usage,
 79    AlbaVector size,
 80    WGPUTextureFormat format, uint8_t samples,
 81    const void* data
 82);
 83
 84// Drawng
 85void draw_triangles_indexed_render_pass(
 86    DrawCall* data,
 87    uint32_t num_vertices,
 88    const AlbaVector* vertices,
 89    const AlbaAttribute* attributes,
 90    uint32_t num_indices,
 91    uint32_t* indices
 92);
 93
 94// Global variables
 95static int glfw_initialized = 0;
 96static pthread_mutex_t glfw_mutex = PTHREAD_MUTEX_INITIALIZER;
 97
 98static FT_Library freetype = NULL;
 99static FT_Face roboto = NULL;
100static AlbaAtlas atlas;
101static pthread_mutex_t freetype_mutex = PTHREAD_MUTEX_INITIALIZER;
102
103#endif // INTERNAL_H