Optimize glyph calculation

This commit is contained in:
2026-03-22 14:13:00 +05:45
parent 9294a1699f
commit 157f04060e
+17 -25
View File
@@ -3,11 +3,11 @@ package e2glyph
/* /*
Provides text rendering using glfw, stb_truetype for the engine Provides text rendering using glfw, stb_truetype for the engine
TODO:
Optimize for only calculate when there's changes
*/ */
import "core:math" import "core:math"
import os "core:os" import os "core:os"
import "core:mem" import "core:mem"
@@ -52,16 +52,10 @@ GlyphState :: struct {
} }
glyph_init :: proc(glyph: ^GlyphState) { glyph_init :: proc(glyph: ^GlyphState, filepath : string) {
ok : bool ok : bool
glyph.program_id, ok = gl.load_shaders_source( glyph.program_id, ok = gl.load_shaders_source(#load("./shaders/font_vert.glsl"), #load("./shaders/font_frag.glsl"), )
#load("./shaders/font_vert.glsl"),
#load("./shaders/font_frag.glsl"),
)
//gl.UseProgram(glyph.program_id)
gl.CreateBuffers(1, &glyph.rect_instances_vbo) gl.CreateBuffers(1, &glyph.rect_instances_vbo)
gl.CreateVertexArrays(1, &glyph.vao) gl.CreateVertexArrays(1, &glyph.vao)
@@ -88,19 +82,7 @@ glyph_init :: proc(glyph: ^GlyphState) {
gl.CreateTextures(gl.TEXTURE_RECTANGLE, 1, &glyph.atlas_texture) gl.CreateTextures(gl.TEXTURE_RECTANGLE, 1, &glyph.atlas_texture)
gl.TextureStorage2D(glyph.atlas_texture, 1, gl.RGB8, glyph.atlas_width, glyph.atlas_height) gl.TextureStorage2D(glyph.atlas_texture, 1, gl.RGB8, glyph.atlas_width, glyph.atlas_height)
font_data, _ := os.read_entire_file_from_path(filepath, context.allocator)
/*
gl.BindVertexArray(0)
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
gl.BindTexture(gl.TEXTURE_RECTANGLE, 0)
gl.BindTexture(gl.TEXTURE_2D, 0)
gl.DisableVertexArrayAttrib(glyph.vao, 0)
gl.DisableVertexArrayAttrib(glyph.vao, 1)
gl.DisableVertexArrayAttrib(glyph.vao, 2)
gl.DisableVertexArrayAttrib(glyph.vao, 3)
*/
font_data, _ := os.read_entire_file_from_path("./Ubuntu-Regular.ttf", context.allocator)
stbtt.InitFont(&glyph.font_info, &font_data[0], 0) stbtt.InitFont(&glyph.font_info, &font_data[0], 0)
@@ -160,10 +142,13 @@ glyph_draw_font :: proc(glyph_state: ^GlyphState, text: string, pos : [2]f32, co
glyph_state.curr.x += 2 * glyph_state.font_size_pt glyph_state.curr.x += 2 * glyph_state.font_size_pt
} else { } else {
horizontal_filter_padding, subpixel_positioning_left_padding: i32 = 1, 1 horizontal_filter_padding, subpixel_positioning_left_padding: i32 = 1, 1
assert(codepoint <= 127) assert(codepoint <= 127)
glyph_atlas := glyph_state.atlas_items[codepoint] glyph_atlas := glyph_state.atlas_items[codepoint]
if !glyph_atlas.filled
{
glyph_index := stbtt.FindGlyphIndex(&glyph_state.font_info, c) glyph_index := stbtt.FindGlyphIndex(&glyph_state.font_info, c)
x0, y0, x1, y1: i32 = 0, 0, 0, 0 x0, y0, x1, y1: i32 = 0, 0, 0, 0
@@ -193,7 +178,14 @@ glyph_draw_font :: proc(glyph_state: ^GlyphState, text: string, pos : [2]f32, co
glyph_bitmap, _ := mem.alloc_bytes(int(bitmap_size)) glyph_bitmap, _ := mem.alloc_bytes(int(bitmap_size))
glyph_offset_x := (subpixel_positioning_left_padding + horizontal_filter_padding) * horizontal_resolution glyph_offset_x := (subpixel_positioning_left_padding + horizontal_filter_padding) * horizontal_resolution
stbtt.MakeGlyphBitmap(&glyph_state.font_info, &glyph_bitmap[glyph_offset_x], atlas_item_width * horizontal_resolution, atlas_item_height, bitmap_stride, font_scale * f32(horizontal_resolution), font_scale, glyph_index) stbtt.MakeGlyphBitmap(
&glyph_state.font_info,
&glyph_bitmap[glyph_offset_x],
atlas_item_width * horizontal_resolution,
atlas_item_height, bitmap_stride,
font_scale * f32(horizontal_resolution),
font_scale, glyph_index
)
atlas_item_bitmap, _ := mem.alloc_bytes(int(bitmap_size)) atlas_item_bitmap, _ := mem.alloc_bytes(int(bitmap_size))
@@ -245,6 +237,7 @@ glyph_draw_font :: proc(glyph_state: ^GlyphState, text: string, pos : [2]f32, co
glyph_atlas.distance_b2t = distance_from_baseline_to_top_px glyph_atlas.distance_b2t = distance_from_baseline_to_top_px
glyph_atlas.filled = true glyph_atlas.filled = true
glyph_state.atlas_items[codepoint] = glyph_atlas glyph_state.atlas_items[codepoint] = glyph_atlas
}
glyph_advance_width, glyph_left_side_bearing: i32 = 0, 0 glyph_advance_width, glyph_left_side_bearing: i32 = 0, 0
@@ -274,7 +267,6 @@ glyph_draw_font :: proc(glyph_state: ^GlyphState, text: string, pos : [2]f32, co
r.color = text_color r.color = text_color
r.index = u32(i + 1) r.index = u32(i + 1)
append(&glyph_state.rect_buffer, r) append(&glyph_state.rect_buffer, r)
} }