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