forked from melonDS-emu/melonDS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2DXBRZ_FreescaleFS.glsl
More file actions
185 lines (153 loc) · 7.15 KB
/
Copy path2DXBRZ_FreescaleFS.glsl
File metadata and controls
185 lines (153 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
xBRZ freescale, based on the RetroArch shader by hunterk:
https://github.com/libretro/glsl-shaders/blob/master/xbrz/shaders/xbrz-freescale.glsl
Hyllian's xBR vertex code and texel mapping
Copyright (C) 2011/2016 Hyllian - sergiogdb@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This shader also uses code and/or concepts from xBRZ as it appears in the
DeSmuME source code. The license for which is as follows:
***************************************************************************
* This file is part of the HqMAME project. It is distributed under *
* GNU General Public License: http://www.gnu.org/licenses/gpl-3.0 *
* Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
* *
* Additionally and as a special exception, the author gives permission *
* to link the code of this program with the MAME library (or with modified*
* versions of MAME that use the same license as MAME), and distribute *
* linked combinations including the two. You must obey the GNU General *
* Public License in all respects for all of the code used other than MAME.*
* If you modify this file, you may extend this exception to your version *
* of the file, but you are not obligated to do so. If you do not wish to *
* do so, delete this exception statement from your version. *
***************************************************************************
Two-pass melonDS adaptation Copyright 2026 ZironZ and distributed under
GPL-3.0-or-later.
*/
#version 140
uniform sampler2D Source;
uniform sampler2D InfoTex;
uniform vec2 uOutputSize;
in vec2 fTexcoord;
out vec4 oColor;
const int BLEND_NONE = 0;
const int BLEND_NORMAL = 1;
const int BLEND_DOMINANT = 2;
const float LUMINANCE_WEIGHT = 1.0;
const float EQUAL_COLOR_TOLERANCE = 30.0 / 255.0;
const float STEEP_DIRECTION_THRESHOLD = 2.2;
vec4 FetchTexture(sampler2D tex, ivec2 center, ivec2 offset)
{
ivec2 size = textureSize(tex, 0);
ivec2 pos = clamp(center + offset, ivec2(0), size - ivec2(1));
return texelFetch(tex, pos, 0);
}
float DistYCbCr(vec4 pixA, vec4 pixB)
{
const vec3 w = vec3(0.2627, 0.6780, 0.0593);
const float scaleB = 0.5 / (1.0 - w.b);
const float scaleR = 0.5 / (1.0 - w.r);
vec4 diff = pixA - pixB;
float Y = dot(diff.rgb, w);
float Cb = scaleB * (diff.b - Y);
float Cr = scaleR * (diff.r - Y);
float colorDist = ((LUMINANCE_WEIGHT * Y) * (LUMINANCE_WEIGHT * Y)) + (Cb * Cb) + (Cr * Cr);
return sqrt((pixA.a * pixB.a * colorDist) + (diff.a * diff.a));
}
float GetLeftRatio(vec2 centerPos, vec2 origin, vec2 direction, vec2 scale)
{
vec2 p0 = centerPos - origin;
vec2 proj = direction * (dot(p0, direction) / dot(direction, direction));
vec2 distv = p0 - proj;
vec2 orth = vec2(-direction.y, direction.x);
float side = sign(dot(p0, orth));
float v = side * length(distv * scale);
return smoothstep(-sqrt(2.0) * 0.5, sqrt(2.0) * 0.5, v);
}
void main()
{
ivec2 srcSize = textureSize(Source, 0);
ivec2 outputSize = ivec2(uOutputSize);
ivec2 outputPixel = clamp(ivec2(gl_FragCoord.xy), ivec2(0), outputSize - ivec2(1));
vec2 srcPixel = ((vec2(outputPixel) + vec2(0.5)) * vec2(srcSize)) / uOutputSize;
ivec2 center = clamp(ivec2(floor(srcPixel)), ivec2(0), srcSize - ivec2(1));
vec2 pos = fract(srcPixel) - vec2(0.5);
vec2 scale = uOutputSize / vec2(srcSize);
vec4 B = FetchTexture(Source, center, ivec2( 0, -1));
vec4 D = FetchTexture(Source, center, ivec2(-1, 0));
vec4 E = FetchTexture(Source, center, ivec2( 0, 0));
vec4 F = FetchTexture(Source, center, ivec2( 1, 0));
vec4 H = FetchTexture(Source, center, ivec2( 0, 1));
ivec4 info = ivec4(floor((FetchTexture(InfoTex, center, ivec2(0)) * 255.0) + 0.5));
ivec4 blendResult = info % 4;
ivec4 doLineBlend = (info / 4) % 4;
ivec4 haveShallowLine = (info / 16) % 4;
ivec4 haveSteepLine = (info / 64) % 4;
vec4 res = E;
if (blendResult.z > BLEND_NONE)
{
vec2 origin = vec2(0.0, 1.0 / sqrt(2.0));
vec2 direction = vec2(1.0, -1.0);
if (doLineBlend.z > 0)
{
origin = (haveShallowLine.z > 0) ? vec2(0.0, 0.25) : vec2(0.0, 0.5);
direction.x += float(haveShallowLine.z);
direction.y -= float(haveSteepLine.z);
}
vec4 blendPix = mix(H, F, step(DistYCbCr(E, F), DistYCbCr(E, H)));
res = mix(res, blendPix, GetLeftRatio(pos, origin, direction, scale));
}
if (blendResult.w > BLEND_NONE)
{
vec2 origin = vec2(-1.0 / sqrt(2.0), 0.0);
vec2 direction = vec2(1.0, 1.0);
if (doLineBlend.w > 0)
{
origin = (haveShallowLine.w > 0) ? vec2(-0.25, 0.0) : vec2(-0.5, 0.0);
direction.y += float(haveShallowLine.w);
direction.x += float(haveSteepLine.w);
}
vec4 blendPix = mix(H, D, step(DistYCbCr(E, D), DistYCbCr(E, H)));
res = mix(res, blendPix, GetLeftRatio(pos, origin, direction, scale));
}
if (blendResult.y > BLEND_NONE)
{
vec2 origin = vec2(1.0 / sqrt(2.0), 0.0);
vec2 direction = vec2(-1.0, -1.0);
if (doLineBlend.y > 0)
{
origin = (haveShallowLine.y > 0) ? vec2(0.25, 0.0) : vec2(0.5, 0.0);
direction.y -= float(haveShallowLine.y);
direction.x -= float(haveSteepLine.y);
}
vec4 blendPix = mix(F, B, step(DistYCbCr(E, B), DistYCbCr(E, F)));
res = mix(res, blendPix, GetLeftRatio(pos, origin, direction, scale));
}
if (blendResult.x > BLEND_NONE)
{
vec2 origin = vec2(0.0, -1.0 / sqrt(2.0));
vec2 direction = vec2(-1.0, 1.0);
if (doLineBlend.x > 0)
{
origin = (haveShallowLine.x > 0) ? vec2(0.0, -0.25) : vec2(0.0, -0.5);
direction.x -= float(haveShallowLine.x);
direction.y += float(haveSteepLine.x);
}
vec4 blendPix = mix(D, B, step(DistYCbCr(E, B), DistYCbCr(E, D)));
res = mix(res, blendPix, GetLeftRatio(pos, origin, direction, scale));
}
oColor = res;
}