-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTVoutTweaks.cg
137 lines (109 loc) · 3.43 KB
/
TVoutTweaks.cg
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
///////////////
// TV-out tweaks
// Author: aliaspider - aliaspider@gmail.com
// License: GPLv3
////////////////////////////////////////////////////////
// this shader is meant to be used when running
// an emulator on a real CRT-TV @240p or @480i
////////////////////////////////////////////////////////
// Basic settings:
//
// use TV video color range (16-235)
// instead of PC full range (0-255)
// comment to disable
//
#define TV_COLOR_LEVELS
// signal resolution
// higher = sharper
//
#define RESOLUTION 256.0
// simulate a composite connection instead of RGB
// uncomment to enable
//
// #define COMPOSITE_CONNECTION
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// Advanced settings:
//
// these values will be used instead
// if COMPOSITE_CONNECTION is defined
// to simulate different signal resolutions(bandwidth)
// for luma (Y) and chroma ( I and Q )
// this is just an approximation
// and will only simulate the low bandwidth anspect of
// composite signal, not the crosstalk between luma and chroma
// Y = 4MHz I=1.3MHz Q=0.4MHz
#define RESOLUTION_Y 256.0
#define RESOLUTION_I 83.2
#define RESOLUTION_Q 25.6
// formula is MHz=resolution*15750Hz
// 15750Hz being the horizontal Frequency of NTSC
// (=262.5*60Hz)
////////////////////////////////////////////////////////
struct input
{
half2 video_size;
float2 texture_size;
half2 output_size;
};
void main_vertex
(
float4 position : POSITION ,
out float4 outPosition : POSITION ,
float2 texCoord : TEXCOORD0 ,
out float2 outTexCoord : TEXCOORD0 ,
uniform half4x4 modelViewProj
)
{
outPosition = mul(modelViewProj, position);
outTexCoord = texCoord;
}
float3x3 RGB_to_YIQ = float3x3(
0.299,0.587,0.114,
0.595716,-0.274453,-0.321263,
0.211456,-0.522591, 0.311135);
float3x3 YIQ_to_RGB = float3x3(
1.0,0.9563,0.6210,
1.0,-0.2721,-0.6474,
1.0,-1.1070, 1.7046);
#define pi 3.14159265358
#define a(x) abs(x)
#define d(x,b) (pi*b*min(a(x)+0.5,1.0/b))
#define e(x,b) (pi*b*min(max(a(x)-0.5,-1.0/b),1.0/b))
#define STU(x,b) ((d(x,b)+sin(d(x,b))-e(x,b)-sin(e(x,b)))/(2.0*pi))
#define X(i) (offset-(i))
#define L(C) clamp((C -16.5/ 256.0)*256.0/(236.0-16.0),0.0,1.0)
#define LCHR(C) clamp((C -16.5/ 256.0)*256.0/(240.0-16.0),0.0,1.0)
#ifdef TV_COLOR_LEVELS
#ifdef COMPOSITE_CONNECTION
float3 LEVELS(float3 c0){
return float3(L(c0.x),LCHR(c0.y),LCHR(c0.z));
}
#else
#define LEVELS(c0) L(c0)
#endif //COMPOSITE_CONNECTION
#else
#define LEVELS(c0) c0
#endif //TV_COLOR_LEVELS
#ifdef COMPOSITE_CONNECTION
#define GETC mul(RGB_to_YIQ,LEVELS(tex2D(tex, float2(texCoord.x - X*oneT,texCoord.y)).xyz))
#define VAL float3((c.x*STU(X,(RESOLUTION_Y*oneI))),(c.y*STU(X,(RESOLUTION_I*oneI))),(c.z*STU(X,(RESOLUTION_Q*oneI))))
#else
#define GETC (LEVELS(tex2D(tex, float2(texCoord.x - X*oneT,texCoord.y)).xyz))
#define VAL (c*STU(X,(RESOLUTION*oneI)))
#endif //COMPOSITE_CONNECTION
#define PROCESS(i) X=X(i);c=GETC;tempColor+=VAL;
float4 main_fragment(in float2 texCoord : TEXCOORD0 , uniform sampler2D tex : TEXUNIT0, uniform input IN) : COLOR
{
float3 tempColor=float3(0.0,0.0,0.0);
float offset = fract((texCoord.x * IN.texture_size.x) - 0.5);
float oneT=1.0/IN.texture_size.x;
float oneI=1.0/IN.video_size.x;
float X;
float3 c;
PROCESS(-1) PROCESS(0) PROCESS(1) PROCESS(2)
#ifdef COMPOSITE_CONNECTION
tempColor=mul(YIQ_to_RGB,tempColor);
#endif
return float4(tempColor, 1.0);
}