Skip to content

Commit

Permalink
Fix rounding errors in the LCD and CRT shaders, closes #595
Browse files Browse the repository at this point in the history
  • Loading branch information
LIJI32 committed Mar 9, 2024
1 parent a125da6 commit 5e2b6e7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
36 changes: 18 additions & 18 deletions Shaders/CRT.fsh
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou

/* Setting up common vars */
vec2 pos = fract(position * input_resolution);
vec2 sub_pos = fract(position * input_resolution * 6);
vec2 sub_pos = pos * 6;

vec4 center = texture_relative(image, position, vec2(0, 0));
vec4 left = texture_relative(image, position, vec2(-1, 0));
vec4 right = texture_relative(image, position, vec2(1, 0));

/* Vertical blurring */
if (pos.y < 1.0 / 6.0) {
if (sub_pos.y < 1.0) {
center = mix(center, texture_relative(image, position, vec2( 0, -1)), 0.5 - sub_pos.y / 2.0);
left = mix(left, texture_relative(image, position, vec2(-1, -1)), 0.5 - sub_pos.y / 2.0);
right = mix(right, texture_relative(image, position, vec2( 1, -1)), 0.5 - sub_pos.y / 2.0);
}
else if (pos.y > 5.0 / 6.0) {
center = mix(center, texture_relative(image, position, vec2( 0, 1)), sub_pos.y / 2.0);
left = mix(left, texture_relative(image, position, vec2(-1, 1)), sub_pos.y / 2.0);
right = mix(right, texture_relative(image, position, vec2( 1, 1)), sub_pos.y / 2.0);
else if (sub_pos.y > 5.0) {
center = mix(center, texture_relative(image, position, vec2( 0, 1)), (sub_pos.y - 5) / 2.0);
left = mix(left, texture_relative(image, position, vec2(-1, 1)), (sub_pos.y - 5) / 2.0);
right = mix(right, texture_relative(image, position, vec2( 1, 1)), (sub_pos.y - 5) / 2.0);
}

/* Scanlines */
Expand Down Expand Up @@ -75,7 +75,7 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou
}

/* Blur the edges of the separators of adjacent columns */
if (pos.x < 1.0 / 6.0 || pos.x > 5.0 / 6.0) {
if (sub_pos.x < 1.0 || sub_pos.x > 5.0) {
pos.y += 0.5;
pos.y = fract(pos.y);

Expand Down Expand Up @@ -112,35 +112,35 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou
vec4 midright = mix(right, center, 0.5);

vec4 ret;
if (pos.x < 1.0 / 6.0) {
if (sub_pos.x < 1.0) {
ret = mix(vec4(COLOR_HIGH * center.r, COLOR_LOW * center.g, COLOR_HIGH * left.b, 1),
vec4(COLOR_HIGH * center.r, COLOR_LOW * center.g, COLOR_LOW * left.b, 1),
sub_pos.x);
}
else if (pos.x < 2.0 / 6.0) {
else if (sub_pos.x < 2.0) {
ret = mix(vec4(COLOR_HIGH * center.r, COLOR_LOW * center.g, COLOR_LOW * left.b, 1),
vec4(COLOR_HIGH * center.r, COLOR_HIGH * center.g, COLOR_LOW * midleft.b, 1),
sub_pos.x);
sub_pos.x - 1);
}
else if (pos.x < 3.0 / 6.0) {
else if (sub_pos.x < 3.0) {
ret = mix(vec4(COLOR_HIGH * center.r , COLOR_HIGH * center.g, COLOR_LOW * midleft.b, 1),
vec4(COLOR_LOW * midright.r, COLOR_HIGH * center.g, COLOR_LOW * center.b, 1),
sub_pos.x);
sub_pos.x - 2);
}
else if (pos.x < 4.0 / 6.0) {
else if (sub_pos.x < 4.0) {
ret = mix(vec4(COLOR_LOW * midright.r, COLOR_HIGH * center.g , COLOR_LOW * center.b, 1),
vec4(COLOR_LOW * right.r , COLOR_HIGH * center.g, COLOR_HIGH * center.b, 1),
sub_pos.x);
sub_pos.x - 3);
}
else if (pos.x < 5.0 / 6.0) {
else if (sub_pos.x < 5.0) {
ret = mix(vec4(COLOR_LOW * right.r, COLOR_HIGH * center.g , COLOR_HIGH * center.b, 1),
vec4(COLOR_LOW * right.r, COLOR_LOW * midright.g, COLOR_HIGH * center.b, 1),
sub_pos.x);
sub_pos.x - 4);
}
else {
ret = mix(vec4(COLOR_LOW * right.r, COLOR_LOW * midright.g, COLOR_HIGH * center.b, 1),
vec4(COLOR_HIGH * right.r, COLOR_LOW * right.g , COLOR_HIGH * center.b, 1),
sub_pos.x);
sub_pos.x - 5);
}

/* Anti alias the curve */
Expand Down
38 changes: 19 additions & 19 deletions Shaders/LCD.fsh
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,63 @@
STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
vec2 pos = fract(position * input_resolution);
vec2 sub_pos = fract(position * input_resolution * 6);
vec2 sub_pos = pos * 6;

vec4 center = texture_relative(image, position, vec2(0, 0));
vec4 left = texture_relative(image, position, vec2(-1, 0));
vec4 right = texture_relative(image, position, vec2(1, 0));

if (pos.y < 1.0 / 6.0) {
if (sub_pos.y < 1.0) {
center = mix(center, texture_relative(image, position, vec2( 0, -1)), 0.5 - sub_pos.y / 2.0);
left = mix(left, texture_relative(image, position, vec2(-1, -1)), 0.5 - sub_pos.y / 2.0);
right = mix(right, texture_relative(image, position, vec2( 1, -1)), 0.5 - sub_pos.y / 2.0);
center *= sub_pos.y * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
left *= sub_pos.y * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
right *= sub_pos.y * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
}
else if (pos.y > 5.0 / 6.0) {
center = mix(center, texture_relative(image, position, vec2( 0, 1)), sub_pos.y / 2.0);
left = mix(left, texture_relative(image, position, vec2(-1, 1)), sub_pos.y / 2.0);
right = mix(right, texture_relative(image, position, vec2( 1, 1)), sub_pos.y / 2.0);
center *= (1.0 - sub_pos.y) * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
left *= (1.0 - sub_pos.y) * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
right *= (1.0 - sub_pos.y) * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
else if (sub_pos.y > 5.0) {
center = mix(center, texture_relative(image, position, vec2( 0, 1)), (sub_pos.y - 5) / 2.0);
left = mix(left, texture_relative(image, position, vec2(-1, 1)), (sub_pos.y - 5) / 2.0);
right = mix(right, texture_relative(image, position, vec2( 1, 1)), (sub_pos.y - 5) / 2.0);
center *= (6.0 - sub_pos.y) * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
left *= (6.0 - sub_pos.y) * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
right *= (6.0 - sub_pos.y) * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
}


vec4 midleft = mix(left, center, 0.5);
vec4 midright = mix(right, center, 0.5);

vec4 ret;
if (pos.x < 1.0 / 6.0) {
if (sub_pos.x < 1.0) {
ret = mix(vec4(COLOR_HIGH * center.r, COLOR_LOW * center.g, COLOR_HIGH * left.b, 1),
vec4(COLOR_HIGH * center.r, COLOR_LOW * center.g, COLOR_LOW * left.b, 1),
sub_pos.x);
}
else if (pos.x < 2.0 / 6.0) {
else if (sub_pos.x < 2.0) {
ret = mix(vec4(COLOR_HIGH * center.r, COLOR_LOW * center.g, COLOR_LOW * left.b, 1),
vec4(COLOR_HIGH * center.r, COLOR_HIGH * center.g, COLOR_LOW * midleft.b, 1),
sub_pos.x);
sub_pos.x - 1);
}
else if (pos.x < 3.0 / 6.0) {
else if (sub_pos.x < 3.0) {
ret = mix(vec4(COLOR_HIGH * center.r , COLOR_HIGH * center.g, COLOR_LOW * midleft.b, 1),
vec4(COLOR_LOW * midright.r, COLOR_HIGH * center.g, COLOR_LOW * center.b, 1),
sub_pos.x);
sub_pos.x - 2);
}
else if (pos.x < 4.0 / 6.0) {
else if (sub_pos.x < 4.0) {
ret = mix(vec4(COLOR_LOW * midright.r, COLOR_HIGH * center.g , COLOR_LOW * center.b, 1),
vec4(COLOR_LOW * right.r , COLOR_HIGH * center.g, COLOR_HIGH * center.b, 1),
sub_pos.x);
sub_pos.x - 3);
}
else if (pos.x < 5.0 / 6.0) {
else if (sub_pos.x < 5.0) {
ret = mix(vec4(COLOR_LOW * right.r, COLOR_HIGH * center.g , COLOR_HIGH * center.b, 1),
vec4(COLOR_LOW * right.r, COLOR_LOW * midright.g, COLOR_HIGH * center.b, 1),
sub_pos.x);
sub_pos.x - 4);
}
else {
ret = mix(vec4(COLOR_LOW * right.r, COLOR_LOW * midright.g, COLOR_HIGH * center.b, 1),
vec4(COLOR_HIGH * right.r, COLOR_LOW * right.g , COLOR_HIGH * center.b, 1),
sub_pos.x);
sub_pos.x - 5);
}

return ret;
Expand Down
14 changes: 7 additions & 7 deletions Shaders/MonoLCD.fsh
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 ou
vec4 r2 = mix(q12, q22, s.x);

vec2 pos = fract(position * input_resolution);
vec2 sub_pos = fract(position * input_resolution * 6);
vec2 sub_pos = pos * 6;

float multiplier = 1.0;

if (pos.y < 1.0 / 6.0) {
if (sub_pos.y < 1.0) {
multiplier *= sub_pos.y * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
}
else if (pos.y > 5.0 / 6.0) {
multiplier *= (1.0 - sub_pos.y) * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
else if (sub_pos.y > 5.0) {
multiplier *= (6.0 - sub_pos.y) * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
}

if (pos.x < 1.0 / 6.0) {
if (sub_pos.x < 1.0) {
multiplier *= sub_pos.x * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
}
else if (pos.x > 5.0 / 6.0) {
multiplier *= (1.0 - sub_pos.x) * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
else if (sub_pos.x > 5.0) {
multiplier *= (6.0 - sub_pos.x) * SCANLINE_DEPTH + (1 - SCANLINE_DEPTH);
}

vec4 pre_shadow = mix(texture(image, position) * multiplier, mix(r1, r2, s.y), BLOOM);
Expand Down

0 comments on commit 5e2b6e7

Please sign in to comment.