slang-shaders/handheld/gameboy/shader-files/gb-pass2.slang

101 lines
5.2 KiB
Plaintext
Raw Normal View History

2016-07-20 06:17:23 +10:00
#version 450
layout(std140, set = 0, binding = 0) uniform UBO
{
mat4 MVP;
vec4 OutputSize;
vec4 OriginalSize;
vec4 SourceSize;
} global;
///////////////////////////////////////////////////////////////////////////
// //
// Gameboy Classic Shader v0.2.2 //
// //
// Copyright (C) 2013 Harlequin : unknown92835@gmail.com //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
// //
///////////////////////////////////////////////////////////////////////////
#pragma stage vertex
layout(location = 0) in vec4 Position;
layout(location = 1) in vec2 TexCoord;
layout(location = 0) out vec2 vTexCoord;
layout(location = 1) out vec2 texel;
layout(location = 2) out vec2 lower_bound;
layout(location = 3) out vec2 upper_bound;
void main()
{
gl_Position = global.MVP * Position;
vTexCoord = TexCoord;
texel = 1.0 / global.SourceSize.xy;
lower_bound = vec2(0.0);
upper_bound = vec2(texel * (global.OutputSize.xy - 1.0));
}
#pragma stage fragment
layout(location = 0) in vec2 vTexCoord;
layout(location = 1) in vec2 texel;
layout(location = 2) in vec2 lower_bound;
layout(location = 3) in vec2 upper_bound;
layout(location = 0) out vec4 FragColor;
layout(set = 0, binding = 2) uniform sampler2D Source;
//sigma = 4.0, normalized for 5 pixel offset sigma = 4.0, normalized for 4 pixel offset
//Raw Gaussian weights: Raw Gaussian weights:
// 0.09973561222190091607086808117254 @position0 0.09973561222190091607086808117254 @position0
// 0.09666707205829167156101677393475 @position1 0.09666707205829167156101677393475 @position1
// 0.08801637626376240452162358324964 @position2 0.08801637626376240452162358324964 @position2
// 0.07528440407628116669052257071979 @position3 0.07528440407628116669052257071979 @position3
// 0.06049272702308815188099267447364 @position4 0.06049272702308815188099267447364 @position4
// 0.04566231462789672460813692086928 @position5
//
//sum [p0 + 2(p1 + p2 + p3 + p4 + p5)]: sum [p0 + 2(p1 + p2 + p3 + p4)]:
// 0.83198140032054115459545312766674 0.74065677106474770537917928592818
//
//Normalizing factor [1 / sum]: Normalizing factor [1 / sum]:
// 1.2019499469756482251051310195171 1.350153052084338115052273748029
//
//Normalized Gaussian weights: Normalized Gaussian weights:
// 0.11987721382169761913280166382392 @position0 0.13465834124289953661305802732548 @position0
// 0.11618898213475484076479592086597 @position1 0.13051534237555914090930704141833 @position1
// 0.10579127878321792515352079488329 @position2 0.11883557904592230273554609080014 @position2
// 0.09048808548757942339961181362524 @position3 0.10164546793794160274995705611009 @position3
// 0.07270923003781316665844409497651 @position4 0.08167444001912718529866079800870 @position4
// 0.05488381664578583445722654373702 @position5
void main()
{
//define offsets and weights - change this for both the X and Y passes if you change the sigma value or number of texels sampled
float offsets[5] = float[](0.0, 1.0, 2.0, 3.0, 4.0);
float weights[5] = float[]( 0.13465834124289953661305802732548, //precalculated using the Gaussian function:
0.13051534237555914090930704141833, // G(x) = (1 / sqrt(2 * pi * sigma^2)) * e^(-x^2 / (2 * sigma^2))
0.11883557904592230273554609080014, //where sigma = 4.0 and x = offset in range [0, 5]
0.10164546793794160274995705611009, //normalized to 1 to prevent image darkening by multiplying each weight by:
0.08167444001912718529866079800870 ); // 1 / sum(all weights)
//sample the current fragment and apply its weight
vec4 out_color = texture(Source, clamp(vTexCoord.xy, lower_bound.xy, upper_bound.xy)) * weights[0];
//iterate across the offsets in both directions sampling texels and adding their weighted alpha values to the total
for (int i = 1; i < 5; i++)
{
out_color.a += texture(Source, clamp(vTexCoord.xy + vec2(offsets[i] * texel.x, 0.0), lower_bound, upper_bound)).a * weights[i];
out_color.a += texture(Source, clamp(vTexCoord.xy - vec2(offsets[i] * texel.x, 0.0), lower_bound, upper_bound)).a * weights[i];
}
FragColor = vec4(out_color);
}