Skip to content

Commit 50b6eff

Browse files
committed
Hoist line_metrics and expensive get_line_metrics function
out of render_message and into render_msg - if we render text with drop shadows then we only call these functions once instead of twice
1 parent 23b62af commit 50b6eff

4 files changed

Lines changed: 50 additions & 37 deletions

File tree

gfx/drivers/d3d10.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,14 +1068,12 @@ static void d3d10_font_render_message(
10681068
float pos_y,
10691069
unsigned width,
10701070
unsigned height,
1071+
float line_height,
10711072
unsigned text_align)
10721073
{
1073-
float line_height;
1074-
struct font_line_metrics *line_metrics = NULL;
1075-
int lines = 0;
1076-
int x = roundf(pos_x * width);
1077-
font->font_driver->get_line_metrics(font->font_data, &line_metrics);
1078-
line_height = line_metrics->height * scale / height;
1074+
int lines = 0;
1075+
int x = roundf(pos_x * width);
1076+
10791077
for (;;)
10801078
{
10811079
const char *end = msg;
@@ -1103,6 +1101,8 @@ static void d3d10_font_render_msg(
11031101
const char* msg,
11041102
const struct font_params *params)
11051103
{
1104+
float line_height;
1105+
struct font_line_metrics *line_metrics = NULL;
11061106
int drop_x, drop_y;
11071107
enum text_alignment text_align;
11081108
const struct font_glyph* glyph_q;
@@ -1163,6 +1163,8 @@ static void d3d10_font_render_msg(
11631163

11641164
glyph_q = (font->font_driver)
11651165
? font->font_driver->get_glyph(font->font_data, '?') : NULL;
1166+
font->font_driver->get_line_metrics(font->font_data, &line_metrics);
1167+
line_height = line_metrics->height * scale / height;
11661168

11671169
if (drop_x || drop_y)
11681170
{
@@ -1176,11 +1178,11 @@ static void d3d10_font_render_msg(
11761178
font, glyph_q, msg, scale, color_dark,
11771179
x + scale * drop_x / width,
11781180
y + scale * drop_y / height,
1179-
width, height, text_align);
1181+
width, height, line_height, text_align);
11801182
}
11811183

11821184
d3d10_font_render_message(d3d10, font, glyph_q,
1183-
msg, scale, color, x, y, width, height, text_align);
1185+
msg, scale, color, x, y, width, height, line_height, text_align);
11841186
}
11851187

11861188
static const struct font_glyph* d3d10_font_get_glyph(void *data,

gfx/drivers/d3d11.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,25 +1219,20 @@ static void d3d11_font_render_line(
12191219
static void d3d11_font_render_message(
12201220
d3d11_video_t *d3d11,
12211221
d3d11_font_t* font,
1222+
const struct font_glyph* glyph_q,
12221223
const char* msg,
12231224
float scale,
12241225
const unsigned int color,
12251226
float pos_x,
12261227
float pos_y,
12271228
unsigned width,
12281229
unsigned height,
1230+
float line_height,
12291231
unsigned text_align)
12301232
{
1231-
float line_height;
1232-
struct font_line_metrics *line_metrics = NULL;
1233-
int lines = 0;
1234-
int x = roundf(pos_x * width);
1235-
const struct font_glyph* (*get_glyph)(void*, uint32_t)
1236-
= font->font_driver->get_glyph;
1237-
void *font_data = font->font_data;
1238-
const struct font_glyph* glyph_q = get_glyph(font_data, '?');
1239-
font->font_driver->get_line_metrics(font->font_data, &line_metrics);
1240-
line_height = line_metrics->height * scale / height;
1233+
int lines = 0;
1234+
int x = roundf(pos_x * width);
1235+
12411236
for (;;)
12421237
{
12431238
const char* delim = msg;
@@ -1265,8 +1260,11 @@ static void d3d11_font_render_msg(
12651260
const char* msg,
12661261
const struct font_params *params)
12671262
{
1263+
float line_height;
12681264
int drop_x, drop_y;
1265+
struct font_line_metrics *line_metrics = NULL;
12691266
enum text_alignment text_align;
1267+
const struct font_glyph* glyph_q;
12701268
unsigned color, r, g, b, alpha;
12711269
float x, y, scale, drop_mod, drop_alpha;
12721270
d3d11_font_t *font = (d3d11_font_t*)data;
@@ -1322,6 +1320,11 @@ static void d3d11_font_render_msg(
13221320
drop_alpha = 1.0f;
13231321
}
13241322

1323+
glyph_q = (font->font_driver)
1324+
? font->font_driver->get_glyph(font->font_data, '?') : NULL;
1325+
font->font_driver->get_line_metrics(font->font_data, &line_metrics);
1326+
line_height = line_metrics->height * scale / height;
1327+
13251328
if (drop_x || drop_y)
13261329
{
13271330
unsigned r_dark = r * drop_mod;
@@ -1332,14 +1335,14 @@ static void d3d11_font_render_msg(
13321335

13331336
if (d3d11->flags & D3D11_ST_FLAG_SPRITES_ENABLE)
13341337
d3d11_font_render_message(d3d11,
1335-
font, msg, scale, color_dark,
1338+
font, glyph_q, msg, scale, color_dark,
13361339
x + scale * drop_x / width,
13371340
y + scale * drop_y / height,
1338-
width, height, text_align);
1341+
width, height, line_height, text_align);
13391342
}
13401343

1341-
d3d11_font_render_message(d3d11, font, msg, scale,
1342-
color, x, y, width, height, text_align);
1344+
d3d11_font_render_message(d3d11, font, glyph_q, msg, scale,
1345+
color, x, y, width, height, line_height, text_align);
13431346
}
13441347

13451348
static const struct font_glyph* d3d11_font_get_glyph(void *data, uint32_t code)

gfx/drivers/d3d12.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,23 +1541,21 @@ static void d3d12_font_render_line(
15411541
static void d3d12_font_render_message(
15421542
d3d12_video_t *d3d12,
15431543
d3d12_font_t* font,
1544+
const struct font_glyph* glyph_q,
15441545
const char* msg,
15451546
float scale,
15461547
const unsigned int color,
15471548
float pos_x,
15481549
float pos_y,
15491550
unsigned width,
15501551
unsigned height,
1552+
float line_height,
15511553
unsigned text_align)
15521554
{
1553-
float line_height;
15541555
D3D12GraphicsCommandList cmd = d3d12->queue.cmd;
1555-
struct font_line_metrics *line_metrics = NULL;
15561556
int lines = 0;
15571557
int x = roundf(pos_x * width);
1558-
const struct font_glyph* glyph_q = font->font_driver->get_glyph(font->font_data, '?');
1559-
font->font_driver->get_line_metrics(font->font_data, &line_metrics);
1560-
line_height = line_metrics->height * scale / height;
1558+
15611559
for (;;)
15621560
{
15631561
const char *p;
@@ -1585,9 +1583,12 @@ static void d3d12_font_render_msg(
15851583
const char* msg,
15861584
const struct font_params *params)
15871585
{
1586+
float line_height;
1587+
struct font_line_metrics *line_metrics = NULL;
15881588
float x, y, scale, drop_mod, drop_alpha;
15891589
int drop_x, drop_y;
15901590
enum text_alignment text_align;
1591+
const struct font_glyph* glyph_q;
15911592
unsigned color, r, g, b, alpha;
15921593
d3d12_video_t *d3d12 = (d3d12_video_t*)userdata;
15931594
d3d12_font_t* font = (d3d12_font_t*)data;
@@ -1641,6 +1642,11 @@ static void d3d12_font_render_msg(
16411642
drop_alpha = 1.0f;
16421643
}
16431644

1645+
glyph_q = (font->font_driver)
1646+
? font->font_driver->get_glyph(font->font_data, '?') : NULL;
1647+
font->font_driver->get_line_metrics(font->font_data, &line_metrics);
1648+
line_height = line_metrics->height * scale / height;
1649+
16441650
if (drop_x || drop_y)
16451651
{
16461652
unsigned r_dark = r * drop_mod;
@@ -1650,15 +1656,15 @@ static void d3d12_font_render_msg(
16501656
unsigned color_dark = DXGI_COLOR_RGBA(r_dark, g_dark, b_dark, alpha_dark);
16511657

16521658
d3d12_font_render_message(d3d12,
1653-
font, msg, scale, color_dark,
1659+
font, glyph_q, msg, scale, color_dark,
16541660
x + scale * drop_x / width,
16551661
y + scale * drop_y / height,
1656-
width, height, text_align);
1662+
width, height, line_height, text_align);
16571663
}
16581664

1659-
d3d12_font_render_message(d3d12, font,
1665+
d3d12_font_render_message(d3d12, font, glyph_q,
16601666
msg, scale, color, x, y,
1661-
width, height, text_align);
1667+
width, height, line_height, text_align);
16621668
}
16631669

16641670
static const struct font_glyph* d3d12_font_get_glyph(

gfx/drivers/vulkan.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,18 +2199,16 @@ static void vulkan_font_render_message(
21992199
const float color[4],
22002200
float pos_x,
22012201
float pos_y,
2202+
float line_height,
22022203
unsigned text_align)
22032204
{
2204-
float line_height;
2205-
struct font_line_metrics *line_metrics = NULL;
22062205
int x = roundf(pos_x * vk->vp.width);
22072206
int lines = 0;
22082207
float inv_tex_size_x = 1.0f / font->texture.width;
22092208
float inv_tex_size_y = 1.0f / font->texture.height;
22102209
float inv_win_width = 1.0f / vk->vp.width;
22112210
float inv_win_height = 1.0f / vk->vp.height;
2212-
font->font_driver->get_line_metrics(font->font_data, &line_metrics);
2213-
line_height = line_metrics->height * scale / vk->vp.height;
2211+
22142212
for (;;)
22152213
{
22162214
const char *delim = msg;
@@ -2351,6 +2349,8 @@ static void vulkan_font_render_msg(
23512349
const char *msg,
23522350
const struct font_params *params)
23532351
{
2352+
float line_height;
2353+
struct font_line_metrics *line_metrics = NULL;
23542354
float color[4];
23552355
int drop_x, drop_y;
23562356
bool full_screen;
@@ -2427,6 +2427,8 @@ static void vulkan_font_render_msg(
24272427
font->pv = (struct vk_vertex*)font->range.data;
24282428
glyph_q = (font->font_driver)
24292429
? font->font_driver->get_glyph(font->font_data, '?') : NULL;
2430+
font->font_driver->get_line_metrics(font->font_data, &line_metrics);
2431+
line_height = line_metrics->height * scale / vk->vp.height;
24302432

24312433
if (drop_x || drop_y)
24322434
{
@@ -2438,11 +2440,11 @@ static void vulkan_font_render_msg(
24382440

24392441
vulkan_font_render_message(vk, font, glyph_q, msg, scale, color_dark,
24402442
x + scale * drop_x / vk->vp.width, y +
2441-
scale * drop_y / vk->vp.height, text_align);
2443+
scale * drop_y / vk->vp.height, line_height, text_align);
24422444
}
24432445

24442446
vulkan_font_render_message(vk, font, glyph_q, msg, scale,
2445-
color, x, y, text_align);
2447+
color, x, y, line_height, text_align);
24462448
vulkan_font_flush(vk, font);
24472449
}
24482450

0 commit comments

Comments
 (0)