From 9de467eae95118aa68bcbea3d466ee645816f4cb Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Sat, 25 Feb 2023 00:02:48 +0100 Subject: [PATCH] optimize cache_get n shiet --- text_rendering/Makefile | 2 +- text_rendering/cache.c | 24 +++++++----------------- text_rendering/font.c | 5 +++-- text_rendering/genmake.sh | 2 +- text_rendering/main.c | 6 +++--- text_rendering/ren.c | 11 +++++------ 6 files changed, 20 insertions(+), 30 deletions(-) diff --git a/text_rendering/Makefile b/text_rendering/Makefile index 9de0726..2fdd6ae 100644 --- a/text_rendering/Makefile +++ b/text_rendering/Makefile @@ -14,4 +14,4 @@ util.o: util.c util.h test: cache.o font.o hash.o main.o ren.o util.o ${CC} ${LDFLAGS} -o test cache.o font.o hash.o main.o ren.o util.o clean: - rm -f cache.o font.o hash.o main.o ren.o util.o + rm -f test cache.o font.o hash.o main.o ren.o util.o diff --git a/text_rendering/cache.c b/text_rendering/cache.c index 0113b95..b5489b6 100644 --- a/text_rendering/cache.c +++ b/text_rendering/cache.c @@ -70,24 +70,14 @@ unsigned int cache_get(void) { uint32_t x = 0; // find an open spot in the cache - // TODO: use __builtin_clz to speed this up - for (; x < CACHE_SIZE; x++) { - //printf("testing spot %d\n", x); - //print_byte(bitmap[0]>>56); - //print_byte(bitmap[0]>>48); - //print_byte(bitmap[0]>>40); - //print_byte(bitmap[0]>>32); - //print_byte(bitmap[0]>>24); - //print_byte(bitmap[0]>>16); - //print_byte(bitmap[0]>>8); - //print_byte(bitmap[0]); - //printf("%lx", bitmap[i]); - //printf("\n"); - - if (!B_TEST(x)) - break; + for (int b = 0; b < _BSIZE; b++) { + if (bitmap[b] == 0) x = 64; + else x = __builtin_clzll(bitmap[b]); + x = 64-x; + if (!B_TEST(x+64*b)) + return x+64*b; } - return x; + return 0; } diff --git a/text_rendering/font.c b/text_rendering/font.c index 812c6c3..e1e2a01 100644 --- a/text_rendering/font.c +++ b/text_rendering/font.c @@ -46,7 +46,8 @@ struct font_atlas * font_init(void) // loads a font into memory, storing all the ASCII characters in the atlas, each font -// atlas structure holds glyphs of a specific size +// atlas structure holds glyphs of a specific size in pixels +// NOTE: size includes ascend and descend (so 12 does not mean that 'A' is 12px tall) int font_load(struct font_atlas *atlas, const char *path, int size) { if (!atlas || !path) @@ -62,9 +63,9 @@ int font_load(struct font_atlas *atlas, const char *path, int size) int ascent, descent, linegap, baseline; int x0,y0,x1,y1; float scale; - scale = stbtt_ScaleForPixelHeight(&(PRIV(atlas)->stb), size); stbtt_GetFontVMetrics(&(PRIV(atlas)->stb), &ascent, &descent, &linegap); stbtt_GetFontBoundingBox(&(PRIV(atlas)->stb), &x0, &y0, &x1, &y1); + scale = stbtt_ScaleForPixelHeight(&(PRIV(atlas)->stb), size); baseline = scale * -y0; atlas->glyph_max_w = (scale*x1) - (scale*x0); atlas->glyph_max_h = (baseline+scale*y1) - (baseline+scale*y0); diff --git a/text_rendering/genmake.sh b/text_rendering/genmake.sh index e835619..09c925e 100755 --- a/text_rendering/genmake.sh +++ b/text_rendering/genmake.sh @@ -31,7 +31,7 @@ done mainrule='test: ' linkcmd=' ${CC} ${LDFLAGS} -o test ' cleanrule='clean: - rm -f ' + rm -f test ' while IFS="" read -r line; do mainrule="$mainrule $line" linkcmd="$linkcmd $line" diff --git a/text_rendering/main.c b/text_rendering/main.c index 0fc87fe..7cccae0 100644 --- a/text_rendering/main.c +++ b/text_rendering/main.c @@ -26,7 +26,7 @@ int main(void) return 1; } - const char *str = "Ciao Mamma!\nprova: òçà°ù§|¬³¼$£ì"; + const char *str = "Ciao Mamma!\nprova: òçà°ù§|¬³¼$£ì\t"; SDL_Event e; while(1) { @@ -38,11 +38,11 @@ int main(void) case SDL_WINDOWEVENT_RESIZED: case SDL_WINDOWEVENT_SIZE_CHANGED: ren_update_viewport(e.window.data1, e.window.data2); - ren_render_text(str, 0, 0, 100, 100, 12); + ren_render_text(str, 0, 0, 200, 100, 12); SDL_GL_SwapWindow(win); break; case SDL_WINDOWEVENT_EXPOSED: - ren_render_text(str, 0, 0, 100, 100, 12); + ren_render_text(str, 0, 0, 200, 100, 12); SDL_GL_SwapWindow(win); break; default: break; diff --git a/text_rendering/ren.c b/text_rendering/ren.c index b68078a..d92bb60 100644 --- a/text_rendering/ren.c +++ b/text_rendering/ren.c @@ -391,7 +391,7 @@ static int ren_draw_font_stack(void) GL(glScissor(0, 0, ren.width, ren.height)) GL(glClear(GL_COLOR_BUFFER_BIT)); // this has caused me some trouble, convert from image coordiates to viewport - //GL(glScissor(ren.s_x, ren.height-ren.s_y-ren.s_h, ren.s_w, ren.s_h)) + GL(glScissor(ren.s_x, ren.height-ren.s_y-ren.s_h, ren.s_w, ren.s_h)) GL(glUniform2i(ren.viewsize_loc, ren.width, ren.height)) GL(glUniform2i(ren.texturesize_loc, ren.font->width, ren.font->height)) @@ -480,9 +480,9 @@ int ren_render_text(const char *str, int x, int y, int w, int h, int size) uint_least32_t cp; int updated, gx = x, gy = y; struct v_text v; - printf("rendering text: %s\n", str); for (off = 0; (ret = grapheme_decode_utf8(str+off, SIZE_MAX, &cp)) > 0 && cp != 0; off += ret) { // skip special characters that render a box (not present in font) + // FIXME: handle tab if (iscntrl(cp)) goto skip_render; g = font_get_glyph_texture(ren.font, cp, &updated); if (!g) REN_RET(-1, REN_FONT); @@ -531,9 +531,8 @@ int ren_render_text(const char *str, int x, int y, int w, int h, int size) vtstack_push(&ren.font_stack, &v); // TODO: possible kerning needs to be applied here - // FIXME: advance is too large - //gx += c.w + c.a; - //gx += c.w + c.x + 1; + // TODO: handle other unicode control characters such as the + // right-to-left isolate (\u2067) gx += c.x + c.a; skip_render: switch (cp) { @@ -542,7 +541,7 @@ int ren_render_text(const char *str, int x, int y, int w, int h, int size) break; case '\n': // TODO: encode and/or store line height - gy = ren.font->glyph_max_h; + gy += ren.font->glyph_max_h; gx = x; break; default: break;