From 4d3735414da808aab2ecf1a543cbcf3f66b517b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Mon, 12 Feb 2024 10:56:46 +0100 Subject: [PATCH] font: move shaders to art/ folder --- engine/art/shaders/fs_font.glsl | 14 +++++++ engine/art/shaders/vs_font.glsl | 38 +++++++++++++++++++ engine/joint/v4k.h | 66 +++------------------------------ engine/split/v4k_font.c | 63 +------------------------------ engine/split/v4k_window.c | 3 ++ engine/v4k.c | 66 +++------------------------------ 6 files changed, 67 insertions(+), 183 deletions(-) create mode 100644 engine/art/shaders/fs_font.glsl create mode 100644 engine/art/shaders/vs_font.glsl diff --git a/engine/art/shaders/fs_font.glsl b/engine/art/shaders/fs_font.glsl new file mode 100644 index 0000000..0021e20 --- /dev/null +++ b/engine/art/shaders/fs_font.glsl @@ -0,0 +1,14 @@ +in vec2 uv; +in float color_index; + +uniform sampler2D sampler_font; +uniform sampler1D sampler_colors; +uniform float num_colors; + +out vec4 outColor; + +void main() { + vec4 col = texture(sampler_colors, (color_index+0.5)/num_colors); + float s = texture(sampler_font, uv).r; + outColor = vec4(col.rgb, s*col.a); +} diff --git a/engine/art/shaders/vs_font.glsl b/engine/art/shaders/vs_font.glsl new file mode 100644 index 0000000..90f364b --- /dev/null +++ b/engine/art/shaders/vs_font.glsl @@ -0,0 +1,38 @@ +in vec2 vertexPosition; +in vec4 instanceGlyph; + +uniform sampler2D sampler_font; +uniform sampler2D sampler_meta; + +uniform float offset_firstline; // ascent +uniform float scale_factor; // scaling factor proportional to font size +uniform vec2 string_offset; // offset of upper-left corner + +uniform vec2 res_meta; // 96x2 +uniform vec2 res_bitmap; // 512x256 +uniform vec2 resolution; // screen resolution + +out vec2 uv; +out float color_index; // for syntax highlighting + +void main() { + // (xoff, yoff, xoff2, yoff2), from second row of texture + vec4 q2 = texture(sampler_meta, vec2((instanceGlyph.z + 0.5)/res_meta.x, 0.75))*vec4(res_bitmap, res_bitmap); + + vec2 p = vertexPosition*(q2.zw - q2.xy) + q2.xy; // offset and scale it properly relative to baseline + p *= vec2(1.0, -1.0); // flip y, since texture is upside-down + p.y -= offset_firstline; // make sure the upper-left corner of the string is in the upper-left corner of the screen + p *= scale_factor; // scale relative to font size + p += instanceGlyph.xy + string_offset; // move glyph into the right position + p *= 2.0/resolution; // to NDC + p += vec2(-1.0, 1.0); // move to upper-left corner instead of center + + gl_Position = vec4(p, 0.0, 1.0); + + // (x0, y0, x1-x0, y1-y0), from first row of texture + vec4 q = texture(sampler_meta, vec2((instanceGlyph.z + 0.5)/res_meta.x, 0.25)); + + // send the correct uv's in the font atlas to the fragment shader + uv = q.xy + vertexPosition*q.zw; + color_index = instanceGlyph.w; +} diff --git a/engine/joint/v4k.h b/engine/joint/v4k.h index a0b6741..2414563 100644 --- a/engine/joint/v4k.h +++ b/engine/joint/v4k.h @@ -363049,64 +363049,6 @@ static const unsigned short packed_table_japanese[] = { // starts with 0x4E00 // ----------------------------------------------------------------------------- -static const char mv_vs_source[] = "//" FILELINE /*#version 330 core\n\*/"\ -\n\ -in vec2 vertexPosition;\n\ -in vec4 instanceGlyph;\n\ -\n\ -uniform sampler2D sampler_font;\n\ -uniform sampler2D sampler_meta;\n\ -\n\ -uniform float offset_firstline; // ascent\n\ -uniform float scale_factor; // scaling factor proportional to font size\n\ -uniform vec2 string_offset; // offset of upper-left corner\n\ -\n\ -uniform vec2 res_meta; // 96x2 \n\ -uniform vec2 res_bitmap; // 512x256\n\ -uniform vec2 resolution; // screen resolution\n\ -\n\ -out vec2 uv;\n\ -out float color_index; // for syntax highlighting\n\ -\n\ -void main() { \ - // (xoff, yoff, xoff2, yoff2), from second row of texture\n\ - vec4 q2 = texture(sampler_meta, vec2((instanceGlyph.z + 0.5)/res_meta.x, 0.75))*vec4(res_bitmap, res_bitmap);\n\ -\n\ - vec2 p = vertexPosition*(q2.zw - q2.xy) + q2.xy; // offset and scale it properly relative to baseline\n\ - p *= vec2(1.0, -1.0); // flip y, since texture is upside-down\n\ - p.y -= offset_firstline; // make sure the upper-left corner of the string is in the upper-left corner of the screen\n\ - p *= scale_factor; // scale relative to font size\n\ - p += instanceGlyph.xy + string_offset; // move glyph into the right position\n\ - p *= 2.0/resolution; // to NDC\n\ - p += vec2(-1.0, 1.0); // move to upper-left corner instead of center\n\ -\n\ - gl_Position = vec4(p, 0.0, 1.0);\n\ -\n\ - // (x0, y0, x1-x0, y1-y0), from first row of texture\n\ - vec4 q = texture(sampler_meta, vec2((instanceGlyph.z + 0.5)/res_meta.x, 0.25));\n\ -\n\ - // send the correct uv's in the font atlas to the fragment shader\n\ - uv = q.xy + vertexPosition*q.zw;\n\ - color_index = instanceGlyph.w;\n\ -}\n"; - -static const char mv_fs_source[] = "//" FILELINE /*#version 330 core\n\*/"\ -\n\ -in vec2 uv;\n\ -in float color_index;\n\ -\n\ -uniform sampler2D sampler_font;\n\ -uniform sampler1D sampler_colors;\n\ -uniform float num_colors;\n\ -\n\ -out vec4 outColor;\n\ -\n\ -void main() {\ - vec4 col = texture(sampler_colors, (color_index+0.5)/num_colors);\n\ - float s = texture(sampler_font, uv).r;\n\ - outColor = vec4(col.rgb, s*col.a);\n\ -}\n"; - enum { FONT_MAX_COLORS = 256}; enum { FONT_MAX_STRING_LEN = 40000 }; // more glyphs than any reasonable person would show on the screen at once. you can only fit 20736 10x10 rects in a 1920x1080 window @@ -363285,9 +363227,8 @@ void font_face_from_mem(const char *tag, const void *ttf_data, unsigned ttf_len, f->scale[5] = 0.3750f; // H5 f->scale[6] = 0.2500f; // H6 - const char *vs_filename = 0, *fs_filename = 0; - const char *vs = vs_filename ? vfs_read(vs_filename) : mv_vs_source; - const char *fs = fs_filename ? vfs_read(fs_filename) : mv_fs_source; + const char *vs = vfs_read("vs_font.glsl"); + const char *fs = vfs_read("fs_font.glsl"); f->program = shader(vs, fs, "vertexPosition,instanceGlyph", "outColor", NULL); // figure out what ranges we're about to bake @@ -379095,6 +379036,9 @@ int fps__timing_thread(void *arg) { timer_counter++; int64_t tt = (int64_t)(1e9/(float)framerate) - ns_excess; uint64_t took = -time_ns(); + #if is(win32) + timeBeginPeriod(1); + #endif sleep_ns( (float)tt ); took += time_ns(); ns_excess = took - tt; diff --git a/engine/split/v4k_font.c b/engine/split/v4k_font.c index 3e82e6e..74f3d4e 100644 --- a/engine/split/v4k_font.c +++ b/engine/split/v4k_font.c @@ -1493,64 +1493,6 @@ static const unsigned short packed_table_japanese[] = { // starts with 0x4E00 // ----------------------------------------------------------------------------- -static const char mv_vs_source[] = "//" FILELINE /*#version 330 core\n\*/"\ -\n\ -in vec2 vertexPosition;\n\ -in vec4 instanceGlyph;\n\ -\n\ -uniform sampler2D sampler_font;\n\ -uniform sampler2D sampler_meta;\n\ -\n\ -uniform float offset_firstline; // ascent\n\ -uniform float scale_factor; // scaling factor proportional to font size\n\ -uniform vec2 string_offset; // offset of upper-left corner\n\ -\n\ -uniform vec2 res_meta; // 96x2 \n\ -uniform vec2 res_bitmap; // 512x256\n\ -uniform vec2 resolution; // screen resolution\n\ -\n\ -out vec2 uv;\n\ -out float color_index; // for syntax highlighting\n\ -\n\ -void main() { \ - // (xoff, yoff, xoff2, yoff2), from second row of texture\n\ - vec4 q2 = texture(sampler_meta, vec2((instanceGlyph.z + 0.5)/res_meta.x, 0.75))*vec4(res_bitmap, res_bitmap);\n\ -\n\ - vec2 p = vertexPosition*(q2.zw - q2.xy) + q2.xy; // offset and scale it properly relative to baseline\n\ - p *= vec2(1.0, -1.0); // flip y, since texture is upside-down\n\ - p.y -= offset_firstline; // make sure the upper-left corner of the string is in the upper-left corner of the screen\n\ - p *= scale_factor; // scale relative to font size\n\ - p += instanceGlyph.xy + string_offset; // move glyph into the right position\n\ - p *= 2.0/resolution; // to NDC\n\ - p += vec2(-1.0, 1.0); // move to upper-left corner instead of center\n\ -\n\ - gl_Position = vec4(p, 0.0, 1.0);\n\ -\n\ - // (x0, y0, x1-x0, y1-y0), from first row of texture\n\ - vec4 q = texture(sampler_meta, vec2((instanceGlyph.z + 0.5)/res_meta.x, 0.25));\n\ -\n\ - // send the correct uv's in the font atlas to the fragment shader\n\ - uv = q.xy + vertexPosition*q.zw;\n\ - color_index = instanceGlyph.w;\n\ -}\n"; - -static const char mv_fs_source[] = "//" FILELINE /*#version 330 core\n\*/"\ -\n\ -in vec2 uv;\n\ -in float color_index;\n\ -\n\ -uniform sampler2D sampler_font;\n\ -uniform sampler1D sampler_colors;\n\ -uniform float num_colors;\n\ -\n\ -out vec4 outColor;\n\ -\n\ -void main() {\ - vec4 col = texture(sampler_colors, (color_index+0.5)/num_colors);\n\ - float s = texture(sampler_font, uv).r;\n\ - outColor = vec4(col.rgb, s*col.a);\n\ -}\n"; - enum { FONT_MAX_COLORS = 256}; enum { FONT_MAX_STRING_LEN = 40000 }; // more glyphs than any reasonable person would show on the screen at once. you can only fit 20736 10x10 rects in a 1920x1080 window @@ -1729,9 +1671,8 @@ void font_face_from_mem(const char *tag, const void *ttf_data, unsigned ttf_len, f->scale[5] = 0.3750f; // H5 f->scale[6] = 0.2500f; // H6 - const char *vs_filename = 0, *fs_filename = 0; - const char *vs = vs_filename ? vfs_read(vs_filename) : mv_vs_source; - const char *fs = fs_filename ? vfs_read(fs_filename) : mv_fs_source; + const char *vs = vfs_read("vs_font.glsl"); + const char *fs = vfs_read("fs_font.glsl"); f->program = shader(vs, fs, "vertexPosition,instanceGlyph", "outColor", NULL); // figure out what ranges we're about to bake diff --git a/engine/split/v4k_window.c b/engine/split/v4k_window.c index 6251731..d4bdea3 100644 --- a/engine/split/v4k_window.c +++ b/engine/split/v4k_window.c @@ -14,6 +14,9 @@ int fps__timing_thread(void *arg) { timer_counter++; int64_t tt = (int64_t)(1e9/(float)framerate) - ns_excess; uint64_t took = -time_ns(); + #if is(win32) + timeBeginPeriod(1); + #endif sleep_ns( (float)tt ); took += time_ns(); ns_excess = took - tt; diff --git a/engine/v4k.c b/engine/v4k.c index 0f51a96..101485f 100644 --- a/engine/v4k.c +++ b/engine/v4k.c @@ -10191,64 +10191,6 @@ static const unsigned short packed_table_japanese[] = { // starts with 0x4E00 // ----------------------------------------------------------------------------- -static const char mv_vs_source[] = "//" FILELINE /*#version 330 core\n\*/"\ -\n\ -in vec2 vertexPosition;\n\ -in vec4 instanceGlyph;\n\ -\n\ -uniform sampler2D sampler_font;\n\ -uniform sampler2D sampler_meta;\n\ -\n\ -uniform float offset_firstline; // ascent\n\ -uniform float scale_factor; // scaling factor proportional to font size\n\ -uniform vec2 string_offset; // offset of upper-left corner\n\ -\n\ -uniform vec2 res_meta; // 96x2 \n\ -uniform vec2 res_bitmap; // 512x256\n\ -uniform vec2 resolution; // screen resolution\n\ -\n\ -out vec2 uv;\n\ -out float color_index; // for syntax highlighting\n\ -\n\ -void main() { \ - // (xoff, yoff, xoff2, yoff2), from second row of texture\n\ - vec4 q2 = texture(sampler_meta, vec2((instanceGlyph.z + 0.5)/res_meta.x, 0.75))*vec4(res_bitmap, res_bitmap);\n\ -\n\ - vec2 p = vertexPosition*(q2.zw - q2.xy) + q2.xy; // offset and scale it properly relative to baseline\n\ - p *= vec2(1.0, -1.0); // flip y, since texture is upside-down\n\ - p.y -= offset_firstline; // make sure the upper-left corner of the string is in the upper-left corner of the screen\n\ - p *= scale_factor; // scale relative to font size\n\ - p += instanceGlyph.xy + string_offset; // move glyph into the right position\n\ - p *= 2.0/resolution; // to NDC\n\ - p += vec2(-1.0, 1.0); // move to upper-left corner instead of center\n\ -\n\ - gl_Position = vec4(p, 0.0, 1.0);\n\ -\n\ - // (x0, y0, x1-x0, y1-y0), from first row of texture\n\ - vec4 q = texture(sampler_meta, vec2((instanceGlyph.z + 0.5)/res_meta.x, 0.25));\n\ -\n\ - // send the correct uv's in the font atlas to the fragment shader\n\ - uv = q.xy + vertexPosition*q.zw;\n\ - color_index = instanceGlyph.w;\n\ -}\n"; - -static const char mv_fs_source[] = "//" FILELINE /*#version 330 core\n\*/"\ -\n\ -in vec2 uv;\n\ -in float color_index;\n\ -\n\ -uniform sampler2D sampler_font;\n\ -uniform sampler1D sampler_colors;\n\ -uniform float num_colors;\n\ -\n\ -out vec4 outColor;\n\ -\n\ -void main() {\ - vec4 col = texture(sampler_colors, (color_index+0.5)/num_colors);\n\ - float s = texture(sampler_font, uv).r;\n\ - outColor = vec4(col.rgb, s*col.a);\n\ -}\n"; - enum { FONT_MAX_COLORS = 256}; enum { FONT_MAX_STRING_LEN = 40000 }; // more glyphs than any reasonable person would show on the screen at once. you can only fit 20736 10x10 rects in a 1920x1080 window @@ -10427,9 +10369,8 @@ void font_face_from_mem(const char *tag, const void *ttf_data, unsigned ttf_len, f->scale[5] = 0.3750f; // H5 f->scale[6] = 0.2500f; // H6 - const char *vs_filename = 0, *fs_filename = 0; - const char *vs = vs_filename ? vfs_read(vs_filename) : mv_vs_source; - const char *fs = fs_filename ? vfs_read(fs_filename) : mv_fs_source; + const char *vs = vfs_read("vs_font.glsl"); + const char *fs = vfs_read("fs_font.glsl"); f->program = shader(vs, fs, "vertexPosition,instanceGlyph", "outColor", NULL); // figure out what ranges we're about to bake @@ -26237,6 +26178,9 @@ int fps__timing_thread(void *arg) { timer_counter++; int64_t tt = (int64_t)(1e9/(float)framerate) - ns_excess; uint64_t took = -time_ns(); + #if is(win32) + timeBeginPeriod(1); + #endif sleep_ns( (float)tt ); took += time_ns(); ns_excess = took - tt;