-
Notifications
You must be signed in to change notification settings - Fork 249
/
3dfx_pass_0.cg
168 lines (124 loc) · 3.79 KB
/
3dfx_pass_0.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
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
/* COMPATIBILITY
- HLSL compilers
- Cg compilers
*/
void main_vertex
(
float4 position : POSITION,
float4 color : COLOR,
float2 texCoord : TEXCOORD0,
uniform float4x4 modelViewProj,
out float4 oPosition : POSITION,
out float4 oColor : COLOR,
out float2 otexCoord : TEXCOORD
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
otexCoord = texCoord;
}
struct output
{
float4 color : COLOR;
};
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
float frame_count;
float frame_direction;
float frame_rotation;
};
// "LeiFX" shader - "dither" and reduction process
//
// Copyright (C) 2013-2014 leilei
//
// 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 2 of the License, or (at your option)
// any later version.
output main_fragment(float2 texCoord : TEXCOORD0, uniform sampler2D decal : TEXUNIT0, uniform input IN)
{
output OUT;
OUT.color = tex2D(decal, texCoord);
float2 res;
res.x = IN.texture_size.x;
res.y = IN.texture_size.y;
float2 what;
what.x = 1 / res.x;
what.y = 1 / res.y;
float2 dithet = texCoord.xy * res.xy;
dithet.x = texCoord.x * res.x;
dithet.y = texCoord.y * res.y;
float2 ditheu = texCoord.xy * res.xy;
ditheu.x = texCoord.x * res.x;
ditheu.y = texCoord.y * res.y;
// 2x2 matrix?
float vertline1 = (mod(dithet.x, 2.0));
float vertline2 = (mod(dithet.x+1, 2.0));
float vertline3 = (mod(dithet.x+1, 4.0));
float vertline4 = (mod(dithet.x+1, 4.0));
float vertline5 = (mod(dithet.x-1, 4.0));
float horzline1 = (mod(dithet.y, 2.0));
float horzline2 = (mod(dithet.y+1, 2.0));
float horzline3 = (mod(dithet.y, 4.0));
float horzline4 = (mod(dithet.y+1, 4.0));
float horzline5 = (mod(dithet.y-1, 4.0));
float dithone = vertline1 + horzline2;
float dithtwo = vertline2 + horzline1;
float diththree = vertline3 + horzline3;
dithone = dithone * 0.3;
dithtwo = dithtwo * 0.3 + 1;
dithone *= dithtwo;
// Lamest crudest 'dither matrix' ever.
// The estimated dither pattern
// . X . o . X . o . X . o . X
// v . x . v . x . v . x . v .
// . o . o . o . o . o . o . o
// x . v . x . v . x . v . x .
// . X . o . X . o . X . o . X
// v . v . v . v . v . v . v .
// This is the stupidest set of hacks ever to get just this stupid dither
// pattern. It obviously could be done better, but eh.......
// i'm bad at math :(
if (dithone > 1.1) dithone = 1.17;
else if (dithone < 0.9) dithone = 1.3;
else dithone = 1.0;
if (diththree > 1.1) diththree = 1.2;
if (diththree < 1.1) diththree = 5;
if (diththree == 5) diththree = 1.04;
else diththree = 1;
dithone *= diththree;
// ok somewhat better way to make a dither pattern
float3 dithonme;
float eeee = 0.03f;
dithone -= 1.0;
dithone = pow(dithone, 1.6f);
dithonme = OUT.color.rgb;
if (dithone < dithonme.r) dithonme.r += eeee;
if (dithone < dithonme.g) dithonme.g += eeee;
if (dithone < dithonme.b) dithonme.b += eeee;
OUT.color.rgb = dithonme.rgb;
// Reduce the 32-bit image to 16-bit
float3 why = 1;
float3 reduceme = 1;
float radooct = 32; // 32 is usually the proper value
reduceme.r = pow(OUT.color.r, why);
reduceme.r *= radooct;
reduceme.r = int(floor(reduceme.r));
reduceme.r /= radooct;
reduceme.r = pow(reduceme.r, why);
reduceme.g = pow(OUT.color.g, why);
reduceme.g *= radooct * 2;
reduceme.g = int(floor(reduceme.g));
reduceme.g /= radooct * 2;
reduceme.g = pow(reduceme.g, why);
reduceme.b = pow(OUT.color.b, why);
reduceme.b *= radooct;
reduceme.b = int(floor(reduceme.b));
reduceme.b /= radooct;
reduceme.b = pow(reduceme.b, why);
OUT.color.rgb = reduceme.rgb;
return OUT;
}