1#version 450 2 3// xBRZ freescale 4// based on : 5 6/* 7 Hyllian's xBR-vertex code and texel mapping 8 9 Copyright (C) 2011/2016 Hyllian - sergiogdb@gmail.com 10 11 Permission is hereby granted, free of charge, to any person obtaining a copy 12 of this software and associated documentation files (the "Software"), to deal 13 in the Software without restriction, including without limitation the rights 14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 copies of the Software, and to permit persons to whom the Software is 16 furnished to do so, subject to the following conditions: 17 18 The above copyright notice and this permission notice shall be included in 19 all copies or substantial portions of the Software. 20 21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 27 THE SOFTWARE. 28 29*/ 30 31// This shader also uses code and/or concepts from xBRZ as it appears 32// in the Desmume source code. The license for which is as follows: 33 34// **************************************************************************** 35// * This file is part of the HqMAME project. It is distributed under * 36// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0 * 37// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved * 38// * * 39// * Additionally and as a special exception, the author gives permission * 40// * to link the code of this program with the MAME library (or with modified * 41// * versions of MAME that use the same license as MAME), and distribute * 42// * linked combinations including the two. You must obey the GNU General * 43// * Public License in all respects for all of the code used other than MAME. * 44// * If you modify this file, you may extend this exception to your version * 45// * of the file, but you are not obligated to do so. If you do not wish to * 46// * do so, delete this exception statement from your version. * 47// **************************************************************************** 48 49#define BLEND_NONE 0 50#define BLEND_NORMAL 1 51#define BLEND_DOMINANT 2 52#define LUMINANCE_WEIGHT 1.0 53#define EQUAL_COLOR_TOLERANCE 30.0/255.0 54#define STEEP_DIRECTION_THRESHOLD 2.2 55#define DOMINANT_DIRECTION_THRESHOLD 3.6 56 57float DistYCbCr(vec3 pixA, vec3 pixB) 58{ 59 const vec3 w = vec3(0.2627, 0.6780, 0.0593); 60 const float scaleB = 0.5 / (1.0 - w.b); 61 const float scaleR = 0.5 / (1.0 - w.r); 62 vec3 diff = pixA - pixB; 63 float Y = dot(diff.rgb, w); 64 float Cb = scaleB * (diff.b - Y); 65 float Cr = scaleR * (diff.r - Y); 66 67 return sqrt(((LUMINANCE_WEIGHT * Y) * (LUMINANCE_WEIGHT * Y)) + (Cb * Cb) + (Cr * Cr)); 68} 69 70bool IsPixEqual(const vec3 pixA, const vec3 pixB) 71{ 72 return (DistYCbCr(pixA, pixB) < EQUAL_COLOR_TOLERANCE); 73} 74 75float get_left_ratio(vec2 center, vec2 origin, vec2 direction, vec2 scale) 76{ 77 vec2 P0 = center - origin; 78 vec2 proj = direction * (dot(P0, direction) / dot(direction, direction)); 79 vec2 distv = P0 - proj; 80 vec2 orth = vec2(-direction.y, direction.x); 81 float side = sign(dot(P0, orth)); 82 float v = side * length(distv * scale); 83 84// return step(0, v); 85 return smoothstep(-sqrt(2.0)/2.0, sqrt(2.0)/2.0, v); 86} 87 88layout(push_constant) uniform Push 89{ 90 vec4 SourceSize; 91 vec4 OutputSize; 92} params; 93 94layout(std140, set = 0, binding = 0) uniform UBO 95{ 96 mat4 MVP; 97} global; 98 99#pragma stage vertex 100layout(location = 0) in vec4 Position; 101layout(location = 1) in vec2 TexCoord; 102layout(location = 0) out vec2 vTexCoord; 103 104void main() 105{ 106 gl_Position = global.MVP * Position; 107 vTexCoord = TexCoord; 108} 109 110#pragma stage fragment 111layout(location = 0) in vec2 vTexCoord; 112layout(location = 0) out vec4 FragColor; 113layout(set = 0, binding = 2) uniform sampler2D Source; 114 115 116#define eq(a,b) (a == b) 117#define neq(a,b) (a != b) 118 119#define P(x,y) texture(Source, coord + params.SourceSize.zw * vec2(x, y)).rgb 120 121void main() 122{ 123 124 //--------------------------------------- 125 // Input Pixel Mapping: -|x|x|x|- 126 // x|A|B|C|x 127 // x|D|E|F|x 128 // x|G|H|I|x 129 // -|x|x|x|- 130 131 vec2 pos = fract(vTexCoord * params.SourceSize.xy) - vec2(0.5, 0.5); 132 vec2 coord = vTexCoord - pos * params.SourceSize.zw; 133 134 vec3 A = P(-1,-1); 135 vec3 B = P( 0,-1); 136 vec3 C = P( 1,-1); 137 vec3 D = P(-1, 0); 138 vec3 E = P( 0, 0); 139 vec3 F = P( 1, 0); 140 vec3 G = P(-1, 1); 141 vec3 H = P( 0, 1); 142 vec3 I = P( 1, 1); 143 144 // blendResult Mapping: x|y| 145 // w|z| 146 ivec4 blendResult = ivec4(BLEND_NONE,BLEND_NONE,BLEND_NONE,BLEND_NONE); 147 148 // Preprocess corners 149 // Pixel Tap Mapping: -|-|-|-|- 150 // -|-|B|C|- 151 // -|D|E|F|x 152 // -|G|H|I|x 153 // -|-|x|x|- 154 if (!((eq(E,F) && eq(H,I)) || (eq(E,H) && eq(F,I)))) 155 { 156 float dist_H_F = DistYCbCr(G, E) + DistYCbCr(E, C) + DistYCbCr(P(0,2), I) + DistYCbCr(I, P(2,0)) + (4.0 * DistYCbCr(H, F)); 157 float dist_E_I = DistYCbCr(D, H) + DistYCbCr(H, P(1,2)) + DistYCbCr(B, F) + DistYCbCr(F, P(2,1)) + (4.0 * DistYCbCr(E, I)); 158 bool dominantGradient = (DOMINANT_DIRECTION_THRESHOLD * dist_H_F) < dist_E_I; 159 blendResult.z = ((dist_H_F < dist_E_I) && neq(E,F) && neq(E,H)) ? ((dominantGradient) ? BLEND_DOMINANT : BLEND_NORMAL) : BLEND_NONE; 160 } 161 162 163 // Pixel Tap Mapping: -|-|-|-|- 164 // -|A|B|-|- 165 // x|D|E|F|- 166 // x|G|H|I|- 167 // -|x|x|-|- 168 if (!((eq(D,E) && eq(G,H)) || (eq(D,G) && eq(E,H)))) 169 { 170 float dist_G_E = DistYCbCr(P(-2,1) , D) + DistYCbCr(D, B) + DistYCbCr(P(-1,2), H) + DistYCbCr(H, F) + (4.0 * DistYCbCr(G, E)); 171 float dist_D_H = DistYCbCr(P(-2,0) , G) + DistYCbCr(G, P(0,2)) + DistYCbCr(A, E) + DistYCbCr(E, I) + (4.0 * DistYCbCr(D, H)); 172 bool dominantGradient = (DOMINANT_DIRECTION_THRESHOLD * dist_D_H) < dist_G_E; 173 blendResult.w = ((dist_G_E > dist_D_H) && neq(E,D) && neq(E,H)) ? ((dominantGradient) ? BLEND_DOMINANT : BLEND_NORMAL) : BLEND_NONE; 174 } 175 176 // Pixel Tap Mapping: -|-|x|x|- 177 // -|A|B|C|x 178 // -|D|E|F|x 179 // -|-|H|I|- 180 // -|-|-|-|- 181 if (!((eq(B,C) && eq(E,F)) || (eq(B,E) && eq(C,F)))) 182 { 183 float dist_E_C = DistYCbCr(D, B) + DistYCbCr(B, P(1,-2)) + DistYCbCr(H, F) + DistYCbCr(F, P(2,-1)) + (4.0 * DistYCbCr(E, C)); 184 float dist_B_F = DistYCbCr(A, E) + DistYCbCr(E, I) + DistYCbCr(P(0,-2), C) + DistYCbCr(C, P(2,0)) + (4.0 * DistYCbCr(B, F)); 185 bool dominantGradient = (DOMINANT_DIRECTION_THRESHOLD * dist_B_F) < dist_E_C; 186 blendResult.y = ((dist_E_C > dist_B_F) && neq(E,B) && neq(E,F)) ? ((dominantGradient) ? BLEND_DOMINANT : BLEND_NORMAL) : BLEND_NONE; 187 } 188 189 // Pixel Tap Mapping: -|x|x|-|- 190 // x|A|B|C|- 191 // x|D|E|F|- 192 // -|G|H|-|- 193 // -|-|-|-|- 194 if (!((eq(A,B) && eq(D,E)) || (eq(A,D) && eq(B,E)))) 195 { 196 float dist_D_B = DistYCbCr(P(-2,0), A) + DistYCbCr(A, P(0,-2)) + DistYCbCr(G, E) + DistYCbCr(E, C) + (4.0 * DistYCbCr(D, B)); 197 float dist_A_E = DistYCbCr(P(-2,-1), D) + DistYCbCr(D, H) + DistYCbCr(P(-1,-2), B) + DistYCbCr(B, F) + (4.0 * DistYCbCr(A, E)); 198 bool dominantGradient = (DOMINANT_DIRECTION_THRESHOLD * dist_D_B) < dist_A_E; 199 blendResult.x = ((dist_D_B < dist_A_E) && neq(E,D) && neq(E,B)) ? ((dominantGradient) ? BLEND_DOMINANT : BLEND_NORMAL) : BLEND_NONE; 200 } 201 202 FragColor = vec4(blendResult); 203 204 // Pixel Tap Mapping: -|-|-|-|- 205 // -|-|B|C|- 206 // -|D|E|F|x 207 // -|G|H|I|x 208 // -|-|x|x|- 209 if(blendResult.z == BLEND_DOMINANT || (blendResult.z == BLEND_NORMAL && 210 !((blendResult.y != BLEND_NONE && !IsPixEqual(E, G)) || (blendResult.w != BLEND_NONE && !IsPixEqual(E, C)) || 211 (IsPixEqual(G, H) && IsPixEqual(H, I) && IsPixEqual(I, F) && IsPixEqual(F, C) && !IsPixEqual(E, I))))) 212 { 213 FragColor.z += 4.0; 214 215 float dist_F_G = DistYCbCr(F, G); 216 float dist_H_C = DistYCbCr(H, C); 217 218 if((STEEP_DIRECTION_THRESHOLD * dist_F_G <= dist_H_C) && neq(E,G) && neq(D,G)) 219 FragColor.z += 16.0; 220 221 if((STEEP_DIRECTION_THRESHOLD * dist_H_C <= dist_F_G) && neq(E,C) && neq(B,C)) 222 FragColor.z += 64.0; 223 } 224 225 // Pixel Tap Mapping: -|-|-|-|- 226 // -|A|B|-|- 227 // x|D|E|F|- 228 // x|G|H|I|- 229 // -|x|x|-|- 230 if(blendResult.w == BLEND_DOMINANT || (blendResult.w == BLEND_NORMAL && 231 !((blendResult.z != BLEND_NONE && !IsPixEqual(E, A)) || (blendResult.x != BLEND_NONE && !IsPixEqual(E, I)) || 232 (IsPixEqual(A, D) && IsPixEqual(D, G) && IsPixEqual(G, H) && IsPixEqual(H, I) && !IsPixEqual(E, G))))) 233 { 234 FragColor.w += 4.0; 235 236 float dist_H_A = DistYCbCr(H, A); 237 float dist_D_I = DistYCbCr(D, I); 238 239 if((STEEP_DIRECTION_THRESHOLD * dist_H_A <= dist_D_I) && neq(E,A) && neq(B,A)) 240 FragColor.w += 16.0; 241 242 if((STEEP_DIRECTION_THRESHOLD * dist_D_I <= dist_H_A) && neq(E,I) && neq(F,I)) 243 FragColor.w += 64.0; 244 } 245 246 // Pixel Tap Mapping: -|-|x|x|- 247 // -|A|B|C|x 248 // -|D|E|F|x 249 // -|-|H|I|- 250 // -|-|-|-|- 251 if(blendResult.y == BLEND_DOMINANT || (blendResult.y == BLEND_NORMAL && 252 !((blendResult.x != BLEND_NONE && !IsPixEqual(E, I)) || (blendResult.z != BLEND_NONE && !IsPixEqual(E, A)) || 253 (IsPixEqual(I, F) && IsPixEqual(F, C) && IsPixEqual(C, B) && IsPixEqual(B, A) && !IsPixEqual(E, C))))) 254 { 255 FragColor.y += 4.0; 256 257 float dist_B_I = DistYCbCr(B, I); 258 float dist_F_A = DistYCbCr(F, A); 259 260 if((STEEP_DIRECTION_THRESHOLD * dist_B_I <= dist_F_A) && neq(E,I) && neq(H,I)) 261 FragColor.y += 16.0; 262 263 if((STEEP_DIRECTION_THRESHOLD * dist_F_A <= dist_B_I) && neq(E,A) && neq(D,A)) 264 FragColor.y += 64.0; 265 } 266 267 // Pixel Tap Mapping: -|x|x|-|- 268 // x|A|B|C|- 269 // x|D|E|F|- 270 // -|G|H|-|- 271 // -|-|-|-|- 272 if(blendResult.x == BLEND_DOMINANT || (blendResult.x == BLEND_NORMAL && 273 !((blendResult.w != BLEND_NONE && !IsPixEqual(E, C)) || (blendResult.y != BLEND_NONE && !IsPixEqual(E, G)) || 274 (IsPixEqual(C, B) && IsPixEqual(B, A) && IsPixEqual(A, D) && IsPixEqual(D, G) && !IsPixEqual(E, A))))) 275 { 276 FragColor.x += 4.0; 277 278 float dist_D_C = DistYCbCr(D, C); 279 float dist_B_G = DistYCbCr(B, G); 280 281 if((STEEP_DIRECTION_THRESHOLD * dist_D_C <= dist_B_G) && neq(E,C) && neq(F,C)) 282 FragColor.x += 16.0; 283 284 if((STEEP_DIRECTION_THRESHOLD * dist_B_G <= dist_D_C) && neq(E,G) && neq(H,G)) 285 FragColor.x += 64.0; 286 } 287 FragColor /= 255.0; 288} 289