18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-19 05:16:01 +01:00

Script screens: led_post and resize of the Lua screens on the gpu

This commit is contained in:
2026-11-10 16:22:35 +01:00
parent c4924f756d
commit e433b43fef
20 changed files with 971 additions and 34 deletions

76
shaders/screen_led.frag Normal file
View File

@@ -0,0 +1,76 @@
// image:led_post of the script screens on the gpu (rendering/opengl33screenpost.cpp), the pixels of led_post in
// scripting/scriptscreenkernels.cpp (led_util.render_post): each diode becomes 2x2 pixels with a darker grid between
// them, glowing into its neighbours, in a 1 pixel black frame
uniform sampler2D source; // the image, from the top row down
uniform ivec2 source_size;
uniform ivec2 target_size;
uniform int flip; // 1: the rows are written bottom row first, as the screen's texture takes them
uniform int mode; // 1: with the glow
layout(location = 0) out vec4 out_color;
const int glow_radius = 6;
const float glow_factor = 0.08;
ivec3 diode(ivec2 at)
{
return ivec3(round(texelFetch(source, at, 0).rgb * 255.0));
}
// the light gathered from the diodes around, weighted 1 / (0.08 + distance squared)
ivec3 glow(ivec2 at)
{
vec3 sum = vec3(0.0);
int left = max(at.x - glow_radius, 0);
int right = min(at.x + glow_radius + 1, source_size.x);
int top = max(at.y - glow_radius, 0);
int bottom = min(at.y + glow_radius + 1, source_size.y);
for (int i = left; i < right; ++i)
{
float m = glow_factor + float((i - at.x) * (i - at.x));
if (m > 20.0)
continue;
for (int j = top; j < bottom; ++j)
{
float s = m + float((j - at.y) * (j - at.y));
if (s > 20.0)
continue;
sum += vec3(diode(ivec2(i, j))) / s;
}
}
return ivec3(sum * glow_factor);
}
// the light through the grid, rounded down as led_util gets it in double precision. there 1 - 0.15 gives the exact
// result, but 1 - 2 * 0.15 is a little below 0.7, which takes these lights one lower (above 360 the result is clamped
// to 255 anyway)
int shaded(int light, int grid)
{
if (grid == 0)
return light;
if (grid == 1)
return light * 85 / 100;
int result = light * 70 / 100;
if (light == 90 || light == 170 || light == 180 || (light >= 330 && light <= 360 && light % 10 == 0))
result -= 1;
return result;
}
void main()
{
ivec2 at = ivec2(gl_FragCoord.xy);
if (flip != 0)
at.y = target_size.y - 1 - at.y;
if (at.x == 0 || at.y == 0 || at.x == target_size.x - 1 || at.y == target_size.y - 1)
{
out_color = vec4(0.0, 0.0, 0.0, 1.0);
return;
}
ivec2 pixel = at - 1;
ivec2 lit = pixel / 2;
ivec3 light = (mode != 0) ? glow(lit) : diode(lit);
int grid = pixel.x % 2 + pixel.y % 2;
ivec3 result = ivec3(shaded(light.r, grid), shaded(light.g, grid), shaded(light.b, grid));
out_color = vec4(vec3(clamp(result, 0, 255)) / 255.0, 1.0);
}

View File

@@ -0,0 +1,62 @@
// image:resize of the script screens on the gpu (rendering/opengl33screenpost.cpp): one pass of Pillow's resize with
// the Lanczos filter, along the rows (mode 0) or along the columns (mode 1), rounded to 8 bits, as resize_lanczos in
// scripting/scriptscreenkernels.cpp does it
uniform sampler2D source; // the image, from the top row down
uniform ivec2 source_size;
uniform ivec2 target_size;
uniform int flip; // 1: the rows are written bottom row first, as the screen's texture takes them
uniform int mode;
layout(location = 0) out vec4 out_color;
const float pi = 3.14159265358979;
float sinc(float x)
{
if (x == 0.0)
return 1.0;
x *= pi;
return sin(x) / x;
}
float lanczos(float x)
{
if (x >= -3.0 && x < 3.0)
return sinc(x) * sinc(x / 3.0);
return 0.0;
}
void main()
{
ivec2 at = ivec2(gl_FragCoord.xy);
if (flip != 0)
at.y = target_size.y - 1 - at.y;
bool vertical = mode != 0;
int index = vertical ? at.y : at.x;
int from_size = vertical ? source_size.y : source_size.x;
int to_size = vertical ? target_size.y : target_size.x;
float scale = float(from_size) / float(to_size);
float filterscale = max(scale, 1.0);
float support = 3.0 * filterscale;
float center = (float(index) + 0.5) * scale;
float ss = 1.0 / filterscale;
int first = max(int(center - support + 0.5), 0);
int count = min(int(center + support + 0.5), from_size) - first;
float total = 0.0;
for (int x = 0; x < count; ++x)
total += lanczos((float(x + first) - center + 0.5) * ss);
vec3 sum = vec3(0.0);
for (int x = 0; x < count; ++x)
{
float weight = lanczos((float(x + first) - center + 0.5) * ss);
if (total != 0.0)
weight /= total;
ivec2 tap = vertical ? ivec2(at.x, first + x) : ivec2(first + x, at.y);
sum += round(texelFetch(source, tap, 0).rgb * 255.0) * weight;
}
out_color = vec4(clamp(floor(sum + 0.5), 0.0, 255.0) / 255.0, 1.0);
}