VEFontCache: runtime bugfixes
This commit is contained in:
		| @@ -118,7 +118,7 @@ LRU_init :: proc( cache : ^LRU_Cache, capacity : u32 ) { | ||||
| 	error : AllocatorError | ||||
| 	cache.capacity     = capacity | ||||
| 	cache.table, error = make( HMapChained(LRU_Link), hmap_closest_prime( uint(capacity)) ) | ||||
| 	assert( error != .None, "VEFontCache.LRU_init : Failed to allocate cache's table") | ||||
| 	assert( error == .None, "VEFontCache.LRU_init : Failed to allocate cache's table") | ||||
|  | ||||
| 	pool_list_init( & cache.key_queue, capacity ) | ||||
| } | ||||
|   | ||||
| @@ -298,14 +298,18 @@ init :: proc( ctx : ^Context, | ||||
| 	atlas.region_d.offset.x = atlas.width / 2 | ||||
|  | ||||
| 	LRU_init( & shape_cache.state, shape_cache_params.capacity ) | ||||
|  | ||||
| 	shape_cache.storage, error = make( Array(ShapedText), u64(shape_cache_params.reserve_length) ) | ||||
| 	assert(error == .None, "VEFontCache.init : Failed to allocate shape_cache.storage") | ||||
|  | ||||
| 	for idx : u32 = 0; idx < shape_cache_params.capacity; idx += 1 { | ||||
| 		stroage_entry := & shape_cache.storage.data[idx] | ||||
| 		using stroage_entry | ||||
| 		glyphs, error = make( Array(Glyph), cast(u64) shape_cache_params.reserve_length ) | ||||
| 		assert( error != .None, "VEFontCache.init : Failed to allocate glyphs array for shape cache storage" ) | ||||
| 		assert( error == .None, "VEFontCache.init : Failed to allocate glyphs array for shape cache storage" ) | ||||
|  | ||||
| 		positions, error = make( Array(Vec2), cast(u64) shape_cache_params.reserve_length ) | ||||
| 		assert( error != .None, "VEFontCache.init : Failed to allocate positions array for shape cache storage" ) | ||||
| 		assert( error == .None, "VEFontCache.init : Failed to allocate positions array for shape cache storage" ) | ||||
| 	} | ||||
|  | ||||
| 	// Note(From original author): We can actually go over VE_FONTCACHE_GLYPHDRAW_BUFFER_BATCH batches due to smart packing! | ||||
| @@ -318,22 +322,22 @@ init :: proc( ctx : ^Context, | ||||
| 		draw_padding  = glyph_draw_params.draw_padding | ||||
|  | ||||
| 		draw_list.calls, error = make( Array(DrawCall), cast(u64) glyph_draw_params.buffer_batch * 2 ) | ||||
| 		assert( error != .None, "VEFontCache.init : Failed to allocate calls for draw_list" ) | ||||
| 		assert( error == .None, "VEFontCache.init : Failed to allocate calls for draw_list" ) | ||||
|  | ||||
| 		draw_list.indices, error = make( Array(u32), cast(u64) glyph_draw_params.buffer_batch * 2 * 6 ) | ||||
| 		assert( error != .None, "VEFontCache.init : Failed to allocate indices array for draw_list" ) | ||||
| 		assert( error == .None, "VEFontCache.init : Failed to allocate indices array for draw_list" ) | ||||
|  | ||||
| 		draw_list.vertices, error = make( Array(Vertex), cast(u64) glyph_draw_params.buffer_batch * 2 * 4 ) | ||||
| 		assert( error != .None, "VEFontCache.init : Failed to allocate vertices array for draw_list" ) | ||||
| 		assert( error == .None, "VEFontCache.init : Failed to allocate vertices array for draw_list" ) | ||||
|  | ||||
| 		clear_draw_list.calls, error = make( Array(DrawCall), cast(u64) glyph_draw_params.buffer_batch * 2 ) | ||||
| 		assert( error != .None, "VEFontCache.init : Failed to allocate calls for calls for clear_draw_list" ) | ||||
| 		assert( error == .None, "VEFontCache.init : Failed to allocate calls for calls for clear_draw_list" ) | ||||
|  | ||||
| 		clear_draw_list.indices, error = make( Array(u32), cast(u64) glyph_draw_params.buffer_batch * 2 * 4 ) | ||||
| 		assert( error != .None, "VEFontCache.init : Failed to allocate calls for indices array for clear_draw_list" ) | ||||
| 		assert( error == .None, "VEFontCache.init : Failed to allocate calls for indices array for clear_draw_list" ) | ||||
|  | ||||
| 		clear_draw_list.vertices, error = make( Array(Vertex), cast(u64) glyph_draw_params.buffer_batch * 2 * 4 ) | ||||
| 		assert( error != .None, "VEFontCache.init : Failed to allocate vertices array for clear_draw_list" ) | ||||
| 		assert( error == .None, "VEFontCache.init : Failed to allocate vertices array for clear_draw_list" ) | ||||
| 	} | ||||
|  | ||||
| 	parser_init( & parser_ctx ) | ||||
| @@ -360,6 +364,7 @@ load_font :: proc( ctx : ^Context, label : string, data : []byte, size_px : f32 | ||||
| 	assert( ctx != nil ) | ||||
| 	assert( len(data) > 0 ) | ||||
| 	using ctx | ||||
| 	context.allocator = backing | ||||
|  | ||||
| 	id : i32 = -1 | ||||
| 	for index : i32 = 0; index < i32(entries.num); index += 1 { | ||||
| @@ -398,6 +403,7 @@ unload_font :: proc( ctx : ^Context, font : FontID ) | ||||
| { | ||||
| 	assert( ctx != nil ) | ||||
| 	assert( font >= 0 && u64(font) < ctx.entries.num ) | ||||
| 	context.allocator = ctx.backing | ||||
|  | ||||
| 	using ctx | ||||
| 	entry     := & entries.data[ font ] | ||||
| @@ -642,65 +648,10 @@ cache_glyph_to_atlas :: proc( ctx : ^Context, font : FontID, glyph_index : Glyph | ||||
| 		append( & atlas.draw_list.calls, call ) | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	// Render glyph to glyph_update_FBO | ||||
| 	cache_glyph( ctx, font, glyph_index, glyph_draw_scale, glyph_draw_translate ) | ||||
| } | ||||
|  | ||||
| directly_draw_massive_glyph :: proc( ctx : ^Context, entry : ^Entry, glyph : Glyph, bounds_0 : Vec2i, bounds_width, bounds_height : u32, over_sample, position, scale : Vec2 ) | ||||
| { | ||||
| 	flush_glyph_buffer_to_atlas( ctx ) | ||||
|  | ||||
| 	// Draw un-antialiased glyph to update FBO. | ||||
| 	glyph_draw_scale     := over_sample * entry.size_scale | ||||
| 	glyph_draw_translate := - Vec2{ f32(bounds_0.x), f32(bounds_0.y)} * glyph_draw_scale + Vec2{ f32(ctx.atlas.glyph_padding), f32(ctx.atlas.glyph_padding) } | ||||
| 	screenspace_x_form( & glyph_draw_translate, & glyph_draw_scale, f32(ctx.atlas.buffer_width), f32(ctx.atlas.buffer_height) ) | ||||
|  | ||||
| 	cache_glyph( ctx, entry.id, glyph, glyph_draw_scale, glyph_draw_translate ) | ||||
|  | ||||
| 	// Figure out the source rect. | ||||
| 	glyph_position   := Vec2 {} | ||||
| 	glyph_width      := f32(bounds_width)  * entry.size_scale * over_sample.x | ||||
| 	glyph_height     := f32(bounds_height) * entry.size_scale * over_sample.y | ||||
| 	glyph_dst_width  := f32(bounds_width)  * entry.size_scale | ||||
| 	glyph_dst_height := f32(bounds_height) * entry.size_scale | ||||
| 	glyph_width      += f32(2 * ctx.atlas.glyph_padding) | ||||
| 	glyph_height     += f32(2 * ctx.atlas.glyph_padding) | ||||
|  | ||||
| 	// Figure out the destination rect. | ||||
| 	bounds_scaled := Vec2 { | ||||
| 		cast(f32) i32(f32(bounds_0.x) * entry.size_scale - 0.5), | ||||
| 		cast(f32) i32(f32(bounds_0.y) * entry.size_scale - 0.5), | ||||
| 	} | ||||
| 	dst        := position + scale * bounds_scaled | ||||
| 	dst_width  := scale.x * glyph_dst_width | ||||
| 	dst_height := scale.y * glyph_dst_height | ||||
| 	dst.x      -= scale.x * f32(ctx.atlas.glyph_padding) | ||||
| 	dst.y      -= scale.y * f32(ctx.atlas.glyph_padding) | ||||
|  | ||||
| 	glyph_size := Vec2 { glyph_width, glyph_height } | ||||
| 	textspace_x_form( & glyph_position, & glyph_size, f32(ctx.atlas.buffer_width), f32(ctx.atlas.buffer_height) ) | ||||
|  | ||||
| 	// Add the glyph drawcall. | ||||
| 	call : DrawCall | ||||
| 	{ | ||||
| 		using call | ||||
| 		pass        = .Target_Unchanged | ||||
| 		colour      = ctx.colour | ||||
| 		start_index = u32(ctx.draw_list.indices.num) | ||||
| 		blit_quad( & ctx.draw_list, dst, dst + { dst_width, dst_height }, glyph_position, glyph_position + glyph_size ) | ||||
| 		end_index   = u32(ctx.draw_list.indices.num) | ||||
| 		append( & ctx.draw_list.calls, call ) | ||||
| 	} | ||||
|  | ||||
| 	// Clear glyph_update_FBO. | ||||
| 	call.pass              = .Glyph | ||||
| 	call.start_index       = 0 | ||||
| 	call.end_index         = 0 | ||||
| 	call.clear_before_draw = true | ||||
| 	append( & ctx.draw_list.calls, call ) | ||||
| } | ||||
|  | ||||
| is_empty :: proc( ctx : ^Context, entry : ^Entry, glyph_index : Glyph ) -> b32 | ||||
| { | ||||
| 	if glyph_index == 0 do return true | ||||
|   | ||||
| @@ -89,6 +89,60 @@ clear_draw_list :: proc( draw_list : ^DrawList ) { | ||||
| 	clear( draw_list.vertices ) | ||||
| } | ||||
|  | ||||
| directly_draw_massive_glyph :: proc( ctx : ^Context, entry : ^Entry, glyph : Glyph, bounds_0 : Vec2i, bounds_width, bounds_height : u32, over_sample, position, scale : Vec2 ) | ||||
| { | ||||
| 	flush_glyph_buffer_to_atlas( ctx ) | ||||
|  | ||||
| 	// Draw un-antialiased glyph to update FBO. | ||||
| 	glyph_draw_scale     := over_sample * entry.size_scale | ||||
| 	glyph_draw_translate := - Vec2{ f32(bounds_0.x), f32(bounds_0.y)} * glyph_draw_scale + Vec2{ f32(ctx.atlas.glyph_padding), f32(ctx.atlas.glyph_padding) } | ||||
| 	screenspace_x_form( & glyph_draw_translate, & glyph_draw_scale, f32(ctx.atlas.buffer_width), f32(ctx.atlas.buffer_height) ) | ||||
|  | ||||
| 	cache_glyph( ctx, entry.id, glyph, glyph_draw_scale, glyph_draw_translate ) | ||||
|  | ||||
| 	// Figure out the source rect. | ||||
| 	glyph_position   := Vec2 {} | ||||
| 	glyph_width      := f32(bounds_width)  * entry.size_scale * over_sample.x | ||||
| 	glyph_height     := f32(bounds_height) * entry.size_scale * over_sample.y | ||||
| 	glyph_dst_width  := f32(bounds_width)  * entry.size_scale | ||||
| 	glyph_dst_height := f32(bounds_height) * entry.size_scale | ||||
| 	glyph_width      += f32(2 * ctx.atlas.glyph_padding) | ||||
| 	glyph_height     += f32(2 * ctx.atlas.glyph_padding) | ||||
|  | ||||
| 	// Figure out the destination rect. | ||||
| 	bounds_scaled := Vec2 { | ||||
| 		cast(f32) i32(f32(bounds_0.x) * entry.size_scale - 0.5), | ||||
| 		cast(f32) i32(f32(bounds_0.y) * entry.size_scale - 0.5), | ||||
| 	} | ||||
| 	dst        := position + scale * bounds_scaled | ||||
| 	dst_width  := scale.x * glyph_dst_width | ||||
| 	dst_height := scale.y * glyph_dst_height | ||||
| 	dst.x      -= scale.x * f32(ctx.atlas.glyph_padding) | ||||
| 	dst.y      -= scale.y * f32(ctx.atlas.glyph_padding) | ||||
|  | ||||
| 	glyph_size := Vec2 { glyph_width, glyph_height } | ||||
| 	textspace_x_form( & glyph_position, & glyph_size, f32(ctx.atlas.buffer_width), f32(ctx.atlas.buffer_height) ) | ||||
|  | ||||
| 	// Add the glyph drawcall. | ||||
| 	call : DrawCall | ||||
| 	{ | ||||
| 		using call | ||||
| 		pass        = .Target_Unchanged | ||||
| 		colour      = ctx.colour | ||||
| 		start_index = u32(ctx.draw_list.indices.num) | ||||
| 		blit_quad( & ctx.draw_list, dst, dst + { dst_width, dst_height }, glyph_position, glyph_position + glyph_size ) | ||||
| 		end_index   = u32(ctx.draw_list.indices.num) | ||||
| 		append( & ctx.draw_list.calls, call ) | ||||
| 	} | ||||
|  | ||||
| 	// Clear glyph_update_FBO. | ||||
| 	call.pass              = .Glyph | ||||
| 	call.start_index       = 0 | ||||
| 	call.end_index         = 0 | ||||
| 	call.clear_before_draw = true | ||||
| 	append( & ctx.draw_list.calls, call ) | ||||
| } | ||||
|  | ||||
| draw_cached_glyph :: proc( ctx : ^Context, entry : ^Entry, glyph_index : Glyph, position, scale : Vec2 ) -> b32 | ||||
| { | ||||
| 	// Glyph not in current font | ||||
|   | ||||
| @@ -78,7 +78,7 @@ parser_load_font :: proc( ctx : ParserContext, label : string, data : []byte ) - | ||||
|  | ||||
| 	error : AllocatorError | ||||
| 	font, error = set( ctx.fonts, key, ParserFontInfo {} ) | ||||
| 	assert( error != .None, "VEFontCache.parser_load_font: Failed to set a new parser font info" ) | ||||
| 	assert( error == .None, "VEFontCache.parser_load_font: Failed to set a new parser font info" ) | ||||
| 	switch ctx.kind | ||||
| 	{ | ||||
| 		case .Freetype: | ||||
| @@ -434,7 +434,7 @@ parser_convert_conic_to_cubic_freetype :: proc( vertices : Array(ParserGlyphVert | ||||
| 	scratch_arena : Arena; arena_init(& scratch_arena, scratch[:]) | ||||
|  | ||||
| 	points, error := make( Array(freetype.Vector), 256, allocator = arena_allocator( &scratch_arena) ) | ||||
| 	assert(error != .None) | ||||
| 	assert(error == .None) | ||||
|  | ||||
| 	append( & points, p0) | ||||
| 	append( & points, p1) | ||||
|   | ||||
| @@ -44,7 +44,7 @@ shaper_load_font :: proc( ctx : ^ShaperContext, label : string, data : []byte, u | ||||
|  | ||||
| 	error : AllocatorError | ||||
| 	info, error = set( ctx.infos, key, ShaperInfo {} ) | ||||
| 	assert( error != .None, "VEFontCache.parser_load_font: Failed to set a new shaper info" ) | ||||
| 	assert( error == .None, "VEFontCache.parser_load_font: Failed to set a new shaper info" ) | ||||
|  | ||||
| 	using info | ||||
| 	blob = harfbuzz.blob_create( raw_data(data), cast(c.uint) len(data), harfbuzz.Memory_Mode.READONLY, user_data, nil ) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user