-
Notifications
You must be signed in to change notification settings - Fork 3
/
crt_lotte.go
357 lines (295 loc) · 8.42 KB
/
crt_lotte.go
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package shader
import "github.com/hajimehoshi/ebiten/v2"
// CrtLotteKage is a CRT shader based on crt-lottes.glsl.
//
// Credits: Elias Daler https://github.com/eliasdaler/crten and Timothy Lottes.
var CrtLotteKage = []byte(`
//go:build ignore
/*
This is a port of a public domain crt-lottes shader written by Timothy Lottes.
Rewritten to Kage by Elias Daler. The license is still public domain.
The original source code can be found here: https://github.com/libretro/glsl-shaders/blob/master/crt/shaders/crt-lottes.glsl
Changes:
1. DO_BLOOM is assumed. If you don't want bloom, set BloomAmount to 0
2. Accureate linear gamma is used because it looks better
3. Clamp fix is removed - there's no need for it, I think
*/
package main
var TextureSize vec2 // input screen size (e.g. 256x244 for SNES)
var ScreenSize vec2 // output screen size (e.g. when rendering at 4x resolution it is 1024x976)
var HardScan float
var HardPix float
var WarpX float
var WarpY float
var MaskDark float
var MaskLight float
var ScaleInLinearGamma float
var ShadowMask float
var BrightBoost float
var HardBloomPix float
var HardBloomScan float
var BloomAmount float
var Shape float
//use instead of uniforms if you don't need runtime modifiability
/*const HardScan = -8.0
const HardPix = -3.0
const WarpX = 0.031
const WarpY = 0.041
const MaskDark = 0.5
const MaskLight = 1.5
const ShadowMask = 3.0
const BrightBoost = 1.0
const HardBloomPix = -1.5
const HardBloomScan = -2.0
const BloomAmount = 0.05
const Shape = 2.0*/
func ToLinear1(c float) float {
if c <= 0.04045 {
return c / 12.92
}
return pow((c+0.055)/1.055, 2.4)
}
func ToLinear(c vec3) vec3 {
return vec3(ToLinear1(c.r), ToLinear1(c.g), ToLinear1(c.b))
}
// Linear to sRGB.
// Assuming using sRGB typed textures this should not be needed.
func ToSrgb1(c float) float {
if c < 0.0031308 {
return c * 12.92
}
return 1.055*pow(c, 0.41666) - 0.055
}
func ToSrgb(c vec3) vec3 {
return vec3(ToSrgb1(c.r), ToSrgb1(c.g), ToSrgb1(c.b))
}
// Nearest emulated sample given floating point position and texel offset.
// Also zero's off screen.
func Fetch(pos vec2, off vec2) vec3 {
pos = (floor(pos*TextureSize.xy+off) + vec2(0.5, 0.5)) / TextureSize.xy
origin, size := imageSrcRegionOnTexture()
pos = pos*size + origin // IMPORTANT: go back to atlas coordinates from texture coordinates which OpenGL uses
return ToLinear(BrightBoost * imageSrc0At(pos.xy).rgb)
}
// Distance in emulated pixels to nearest texel.
func Dist(pos vec2) vec2 {
pos = pos * TextureSize.xy
return -((pos - floor(pos)) - vec2(0.5))
}
// 1D Gaussian.
func Gaus(pos float, scale float) float {
return exp2(scale * pow(abs(pos), Shape))
}
// 3-tap Gaussian filter along horz line.
func Horz3(pos vec2, off float) vec3 {
b := Fetch(pos, vec2(-1.0, off))
c := Fetch(pos, vec2(0.0, off))
d := Fetch(pos, vec2(1.0, off))
dst := Dist(pos).x
// Convert distance to weight.
scale := HardPix
wb := Gaus(dst-1.0, scale)
wc := Gaus(dst+0.0, scale)
wd := Gaus(dst+1.0, scale)
// Return filtered sample.
return (b*wb + c*wc + d*wd) / (wb + wc + wd)
}
// 5-tap Gaussian filter along horz line.
func Horz5(pos vec2, off float) vec3 {
a := Fetch(pos, vec2(-2.0, off))
b := Fetch(pos, vec2(-1.0, off))
c := Fetch(pos, vec2(0.0, off))
d := Fetch(pos, vec2(1.0, off))
e := Fetch(pos, vec2(2.0, off))
dst := Dist(pos).x
// Convert distance to weight.
scale := HardPix
wa := Gaus(dst-2.0, scale)
wb := Gaus(dst-1.0, scale)
wc := Gaus(dst+0.0, scale)
wd := Gaus(dst+1.0, scale)
we := Gaus(dst+2.0, scale)
// Return filtered sample.
return (a*wa + b*wb + c*wc + d*wd + e*we) / (wa + wb + wc + wd + we)
}
// 7-tap Gaussian filter along horz line.
func Horz7(pos vec2, off float) vec3 {
a := Fetch(pos, vec2(-3.0, off))
b := Fetch(pos, vec2(-2.0, off))
c := Fetch(pos, vec2(-1.0, off))
d := Fetch(pos, vec2(0.0, off))
e := Fetch(pos, vec2(1.0, off))
f := Fetch(pos, vec2(2.0, off))
g := Fetch(pos, vec2(3.0, off))
dst := Dist(pos).x
// Convert distance to weight.
scale := HardBloomPix
wa := Gaus(dst-3.0, scale)
wb := Gaus(dst-2.0, scale)
wc := Gaus(dst-1.0, scale)
wd := Gaus(dst+0.0, scale)
we := Gaus(dst+1.0, scale)
wf := Gaus(dst+2.0, scale)
wg := Gaus(dst+3.0, scale)
// Return filtered sample.
return (a*wa + b*wb + c*wc + d*wd + e*we + f*wf + g*wg) / (wa + wb + wc + wd + we + wf + wg)
}
// Return scanline weight.
func Scan(pos vec2, off float) float {
dst := Dist(pos).y
return Gaus(dst+off, HardScan)
}
// Return scanline weight for Bloom.
func BloomScan(pos vec2, off float) float {
dst := Dist(pos).y
return Gaus(dst+off, HardBloomScan)
}
// Allow nearest three lines to effect pixel.
func Tri(pos vec2) vec3 {
a := Horz3(pos, -1.0)
b := Horz5(pos, 0.0)
c := Horz3(pos, 1.0)
wa := Scan(pos, -1.0)
wb := Scan(pos, 0.0)
wc := Scan(pos, 1.0)
return a*wa + b*wb + c*wc
}
// Small Bloom.
func Bloom(pos vec2) vec3 {
a := Horz5(pos, -2.0)
b := Horz7(pos, -1.0)
c := Horz7(pos, 0.0)
d := Horz7(pos, 1.0)
e := Horz5(pos, 2.0)
wa := BloomScan(pos, -2.0)
wb := BloomScan(pos, -1.0)
wc := BloomScan(pos, 0.0)
wd := BloomScan(pos, 1.0)
we := BloomScan(pos, 2.0)
return a*wa + b*wb + c*wc + d*wd + e*we
}
// Distortion of scanlines, and end of screen alpha.
func Warp(pos vec2) vec2 {
pos = pos*2.0 - 1.0
pos *= vec2(1.0+(pos.y*pos.y)*WarpX, 1.0+(pos.x*pos.x)*WarpY)
return pos*0.5 + 0.5
}
// Shadow mask.
func Mask(pos vec2) vec3 {
mask := vec3(MaskDark, MaskDark, MaskDark)
if ShadowMask == 1.0 {
// Very compressed TV style shadow mask.
line := MaskLight
odd := 0.0
if fract(pos.x*0.166666666) < 0.5 {
odd = 1.0
}
if fract((pos.y+odd)*0.5) < 0.5 {
line = MaskDark
}
pos.x = fract(pos.x * 0.333333333)
if pos.x < 0.333 {
mask.r = MaskLight
} else if pos.x < 0.666 {
mask.g = MaskLight
} else {
mask.b = MaskLight
}
mask *= line
} else if ShadowMask == 2.0 {
// Aperture-grille.
pos.x = fract(pos.x * 0.333333333)
if pos.x < 0.333 {
mask.r = MaskLight
} else if pos.x < 0.666 {
mask.g = MaskLight
} else {
mask.b = MaskLight
}
} else if ShadowMask == 3.0 {
// Stretched VGA style shadow mask (same as prior shaders).
pos.x += pos.y * 3.0
pos.x = fract(pos.x * 0.166666666)
if pos.x < 0.333 {
mask.r = MaskLight
} else if pos.x < 0.666 {
mask.g = MaskLight
} else {
mask.b = MaskLight
}
} else if ShadowMask == 4.0 {
// VGA style shadow mask.
pos.xy = floor(pos.xy * vec2(1.0, 0.5))
pos.x += pos.y * 3.0
pos.x = fract(pos.x * 0.166666666)
if pos.x < 0.333 {
mask.r = MaskLight
} else if pos.x < 0.666 {
mask.g = MaskLight
} else {
mask.b = MaskLight
}
}
return mask
}
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
// Adjust the texture position to [0, 1].
pos := texCoord
origin, size := imageSrcRegionOnTexture()
pos -= origin
pos /= size
pos = Warp(pos)
outColor := Tri(pos)
//Add Bloom
outColor.rgb += Bloom(pos) * BloomAmount
fragCoord := position.xy
fragCoord.y = ScreenSize.y - fragCoord.y // in OpenGL, Y is pointing up
if ShadowMask > 0.0 {
outColor.rgb *= Mask(fragCoord * 1.000001)
}
return vec4(ToSrgb(outColor.rgb), 1.0)
// return vec4(imageSrc0At(texCoord).rgb, 1.0)
}
`)
type CrtLotte struct {
BaseShader
}
func (s *CrtLotte) Apply(screen *ebiten.Image, buffer *ebiten.Image) error {
s.Lock()
defer s.Unlock()
s.Uniforms["ScreenSize"] = []float32{float32(screen.Bounds().Dx()), float32(screen.Bounds().Dy())}
s.Uniforms["TextureSize"] = []float32{float32(buffer.Bounds().Dx()), float32(buffer.Bounds().Dy())}
sop := &ebiten.DrawRectShaderOptions{}
sop.GeoM.Scale(float64(screen.Bounds().Dx())/float64(buffer.Bounds().Dx()), float64(screen.Bounds().Dy())/float64(buffer.Bounds().Dy()))
sop.Uniforms = s.Uniforms
sop.Images[0] = buffer
screen.Clear()
screen.DrawRectShader(screen.Bounds().Dx(), screen.Bounds().Dy(), s.Shader, sop)
return nil
}
func NewCrtLotte() (*CrtLotte, error) {
shader, err := ebiten.NewShader(CrtLotteKage)
if err != nil {
return nil, err
}
s := &CrtLotte{
BaseShader: BaseShader{
Shader: shader,
Uniforms: map[string]interface{}{
"HardScan": float32(-8),
"HardPix": float32(-3),
"WarpX": float32(0.031),
"WarpY": float32(0.041),
"MaskDark": float32(0.5),
"MaskLight": float32(1.5),
"ShadowMask": float32(3.0),
"BrightBoost": float32(1.0),
"HardBloomPix": float32(-1.5),
"HardBloomScan": float32(-2.0),
"BloomAmount": float32(0.05),
"Shape": float32(2.0),
},
},
}
return s, nil
}