No more globals in shaders

This commit is contained in:
Lior Halphon 2018-06-15 18:22:09 +03:00
parent c6dba26d02
commit da7c32cb10
14 changed files with 45 additions and 45 deletions

View File

@ -4,7 +4,7 @@ float quickDistance(vec4 a, vec4 b)
return abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z);
}
vec4 omniScale(sampler2D image, vec2 position)
vec4 omniScale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
vec2 pixel = position * input_resolution - vec2(0.5, 0.5);
@ -104,15 +104,15 @@ vec4 omniScale(sampler2D image, vec2 position)
return q22;
}
vec4 scale(sampler2D image, vec2 position)
vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
vec2 pixel = vec2(1.0, 1.0) / output_resolution;
// 4-pixel super sampling
vec4 q11 = omniScale(image, position + pixel * vec2(-0.25, -0.25));
vec4 q21 = omniScale(image, position + pixel * vec2(+0.25, -0.25));
vec4 q12 = omniScale(image, position + pixel * vec2(-0.25, +0.25));
vec4 q22 = omniScale(image, position + pixel * vec2(+0.25, +0.25));
vec4 q11 = omniScale(image, position + pixel * vec2(-0.25, -0.25), input_resolution, output_resolution);
vec4 q21 = omniScale(image, position + pixel * vec2(+0.25, -0.25), input_resolution, output_resolution);
vec4 q12 = omniScale(image, position + pixel * vec2(-0.25, +0.25), input_resolution, output_resolution);
vec4 q22 = omniScale(image, position + pixel * vec2(+0.25, +0.25), input_resolution, output_resolution);
return (q11 + q21 + q12 + q22) / 4.0;
}

View File

@ -1,4 +1,4 @@
vec4 scale2x(sampler2D image, vec2 position)
vec4 scale2x(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
// o = offset, the width of a pixel
vec2 o = 1.0 / input_resolution;
@ -38,7 +38,7 @@ vec4 scale2x(sampler2D image, vec2 position)
}
}
vec4 scale(sampler2D image, vec2 position)
vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
return mix(texture(image, position), scale2x(image, position), 0.5);
return mix(texture(image, position), scale2x(image, position, input_resolution, output_resolution), 0.5);
}

View File

@ -1,4 +1,4 @@
vec4 scale2x(sampler2D image, vec2 position)
vec4 scale2x(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
// o = offset, the width of a pixel
vec2 o = 1.0 / input_resolution;
@ -37,12 +37,12 @@ vec4 scale2x(sampler2D image, vec2 position)
}
}
vec4 aaScale2x(sampler2D image, vec2 position)
vec4 aaScale2x(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
return mix(texture(image, position), scale2x(image, position), 0.5);
return mix(texture(image, position), scale2x(image, position, input_resolution, output_resolution), 0.5);
}
vec4 scale(sampler2D image, vec2 position)
vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
// o = offset, the width of a pixel
vec2 o = 1.0 / (input_resolution * 2.);
@ -51,15 +51,15 @@ vec4 scale(sampler2D image, vec2 position)
// A B C
// D E F
// G H I
vec4 A = aaScale2x(image, position + vec2( -o.x, o.y));
vec4 B = aaScale2x(image, position + vec2( 0, o.y));
vec4 C = aaScale2x(image, position + vec2( o.x, o.y));
vec4 D = aaScale2x(image, position + vec2( -o.x, 0));
vec4 E = aaScale2x(image, position + vec2( 0, 0));
vec4 F = aaScale2x(image, position + vec2( o.x, 0));
vec4 G = aaScale2x(image, position + vec2( -o.x, -o.y));
vec4 H = aaScale2x(image, position + vec2( 0, -o.y));
vec4 I = aaScale2x(image, position + vec2( o.x, -o.y));
vec4 A = aaScale2x(image, position + vec2( -o.x, o.y), input_resolution, output_resolution);
vec4 B = aaScale2x(image, position + vec2( 0, o.y), input_resolution, output_resolution);
vec4 C = aaScale2x(image, position + vec2( o.x, o.y), input_resolution, output_resolution);
vec4 D = aaScale2x(image, position + vec2( -o.x, 0), input_resolution, output_resolution);
vec4 E = aaScale2x(image, position + vec2( 0, 0), input_resolution, output_resolution);
vec4 F = aaScale2x(image, position + vec2( o.x, 0), input_resolution, output_resolution);
vec4 G = aaScale2x(image, position + vec2( -o.x, -o.y), input_resolution, output_resolution);
vec4 H = aaScale2x(image, position + vec2( 0, -o.y), input_resolution, output_resolution);
vec4 I = aaScale2x(image, position + vec2( o.x, -o.y), input_resolution, output_resolution);
vec4 R;
vec2 p = position * input_resolution * 2.;
// p = the position within a pixel [0...1]

View File

@ -1,4 +1,4 @@
vec4 scale(sampler2D image, vec2 position)
vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
vec2 pixel = position * input_resolution - vec2(0.5, 0.5);

View File

@ -27,7 +27,7 @@ vec4 interp_3px(vec4 c1, float w1, vec4 c2, float w2, vec4 c3, float w3)
return (c1 * w1 + c2 * w2 + c3 * w3) / (w1 + w2 + w3);
}
vec4 scale(sampler2D image, vec2 position)
vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
// o = offset, the width of a pixel
vec2 o = 1.0 / input_resolution;

View File

@ -2,7 +2,7 @@
#define COLOR_HIGH 1.0
#define SCANLINE_DEPTH 0.1
vec4 scale(sampler2D image, vec2 position)
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);

View File

@ -9,7 +9,6 @@ const vec2 input_resolution = vec2(160, 144);
out vec4 frag_color;
vec4 modified_frag_cord;
#line 1
{filter}
@ -20,9 +19,10 @@ void main()
position.y = 1 - position.y;
if (mix_previous) {
frag_color = mix(scale(image, position), scale(previous_image, position), 0.5);
frag_color = mix(scale(image, position, input_resolution, output_resolution),
scale(previous_image, position, input_resolution, output_resolution), 0.5);
}
else {
frag_color = scale(image, position);
frag_color = scale(image, position, input_resolution, output_resolution);
}
}

View File

@ -3,7 +3,7 @@
#include <metal_math>
using namespace metal;
const float4 input_resolution = float4(160, 144);
constant float2 input_resolution = float2(160, 144);
/* For GLSL compatibility */
typedef float2 vec2;

View File

@ -1,4 +1,4 @@
vec4 scale(sampler2D image, vec2 position)
vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
return texture(image, position);
}

View File

@ -24,7 +24,7 @@ bool is_different(vec4 a, vec4 b)
#define P(m, r) ((pattern & (m)) == (r))
vec4 scale(sampler2D image, vec2 position)
vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
// o = offset, the width of a pixel
vec2 o = 1.0 / input_resolution;

View File

@ -4,7 +4,7 @@ float quickDistance(vec4 a, vec4 b)
return abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z);
}
vec4 scale(sampler2D image, vec2 position)
vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
vec2 pixel = position * input_resolution - vec2(0.5, 0.5);

View File

@ -1,6 +1,6 @@
/* Shader implementation of Scale2x is adapted from https://gist.github.com/singron/3161079 */
vec4 scale(sampler2D image, vec2 position)
vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
// o = offset, the width of a pixel
vec2 o = 1.0 / input_resolution;

View File

@ -1,4 +1,4 @@
vec4 scale2x(sampler2D image, vec2 position)
vec4 scale2x(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
// o = offset, the width of a pixel
vec2 o = 1.0 / input_resolution;
@ -38,7 +38,7 @@ vec4 scale2x(sampler2D image, vec2 position)
}
}
vec4 scale(sampler2D image, vec2 position)
vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
// o = offset, the width of a pixel
vec2 o = 1.0 / (input_resolution * 2.);
@ -47,15 +47,15 @@ vec4 scale(sampler2D image, vec2 position)
// A B C
// D E F
// G H I
vec4 A = scale2x(image, position + vec2( -o.x, o.y));
vec4 B = scale2x(image, position + vec2( 0, o.y));
vec4 C = scale2x(image, position + vec2( o.x, o.y));
vec4 D = scale2x(image, position + vec2( -o.x, 0));
vec4 E = scale2x(image, position + vec2( 0, 0));
vec4 F = scale2x(image, position + vec2( o.x, 0));
vec4 G = scale2x(image, position + vec2( -o.x, -o.y));
vec4 H = scale2x(image, position + vec2( 0, -o.y));
vec4 I = scale2x(image, position + vec2( o.x, -o.y));
vec4 A = scale2x(image, position + vec2( -o.x, o.y), input_resolution, output_resolution);
vec4 B = scale2x(image, position + vec2( 0, o.y), input_resolution, output_resolution);
vec4 C = scale2x(image, position + vec2( o.x, o.y), input_resolution, output_resolution);
vec4 D = scale2x(image, position + vec2( -o.x, 0), input_resolution, output_resolution);
vec4 E = scale2x(image, position + vec2( 0, 0), input_resolution, output_resolution);
vec4 F = scale2x(image, position + vec2( o.x, 0), input_resolution, output_resolution);
vec4 G = scale2x(image, position + vec2( -o.x, -o.y), input_resolution, output_resolution);
vec4 H = scale2x(image, position + vec2( 0, -o.y), input_resolution, output_resolution);
vec4 I = scale2x(image, position + vec2( o.x, -o.y), input_resolution, output_resolution);
vec2 p = position * input_resolution * 2.;
// p = the position within a pixel [0...1]
p = fract(p);

View File

@ -1,4 +1,4 @@
vec4 scale(sampler2D image, vec2 position)
vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
{
vec2 pixel = position * input_resolution - vec2(0.5, 0.5);