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