-
Notifications
You must be signed in to change notification settings - Fork 125
/
crtemu.h
1928 lines (1714 loc) · 92.7 KB
/
crtemu.h
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
crtemu.h - v0.1 - Cathode ray tube emulation shader for C/C++.
Do this:
#define CRTEMU_IMPLEMENTATION
before you include this file in *one* C/C++ file to create the implementation.
*/
#ifndef crtemu_h
#define crtemu_h
#ifndef CRTEMU_U32
#define CRTEMU_U32 unsigned int
#endif
#ifndef CRTEMU_U64
#define CRTEMU_U64 unsigned long long
#endif
typedef enum crtemu_type_t {
CRTEMU_TYPE_TV,
CRTEMU_TYPE_PC,
CRTEMU_TYPE_LITE,
} crtemu_type_t;
typedef struct crtemu_t crtemu_t;
crtemu_t* crtemu_create( crtemu_type_t type, void* memctx );
void crtemu_destroy( crtemu_t* crtemu );
void crtemu_frame( crtemu_t* crtemu, CRTEMU_U32* frame_abgr, int frame_width, int frame_height );
void crtemu_present( crtemu_t* crtemu, CRTEMU_U64 time_us, CRTEMU_U32 const* pixels_xbgr, int width, int height,
CRTEMU_U32 mod_xbgr, CRTEMU_U32 border_xbgr );
void crtemu_coordinates_window_to_bitmap( crtemu_t* crtemu, int width, int height, int* x, int* y );
#endif /* crtemu_h */
/*
----------------------
IMPLEMENTATION
----------------------
*/
#ifdef CRTEMU_IMPLEMENTATION
#undef CRTEMU_IMPLEMENTATION
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <stddef.h>
#include <string.h>
#ifndef CRTEMU_MALLOC
#include <stdlib.h>
#if defined(__cplusplus)
#define CRTEMU_MALLOC( ctx, size ) ( ::malloc( size ) )
#define CRTEMU_FREE( ctx, ptr ) ( ::free( ptr ) )
#else
#define CRTEMU_MALLOC( ctx, size ) ( malloc( size ) )
#define CRTEMU_FREE( ctx, ptr ) ( free( ptr ) )
#endif
#endif
#ifdef CRTEMU_REPORT_SHADER_ERRORS
#ifndef CRTEMU_REPORT_ERROR
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define CRTEMU_REPORT_ERROR( str ) printf( "%s", str )
#endif
#endif
#ifndef _WIN32
#define CRTEMU_SDL
#endif
#ifdef __wasm__
#define CRTEMU_WEBGL
#endif
#ifndef CRTEMU_SDL
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllimport) struct HINSTANCE__* __stdcall LoadLibraryA( char const* lpLibFileName );
__declspec(dllimport) int __stdcall FreeLibrary( struct HINSTANCE__* hModule );
#if defined(_WIN64)
typedef __int64 (__stdcall* CRTEMU_PROC)( void );
__declspec(dllimport) CRTEMU_PROC __stdcall GetProcAddress( struct HINSTANCE__* hModule, char const* lpLibFileName );
#else
typedef __int32 (__stdcall* CRTEMU_PROC)( void );
__declspec(dllimport) CRTEMU_PROC __stdcall GetProcAddress( struct HINSTANCE__* hModule, char const* lpLibFileName );
#endif
#ifdef __cplusplus
}
#endif
#define CRTEMU_GLCALLTYPE __stdcall
typedef unsigned int CRTEMU_GLuint;
typedef int CRTEMU_GLsizei;
typedef unsigned int CRTEMU_GLenum;
typedef int CRTEMU_GLint;
typedef float CRTEMU_GLfloat;
typedef char CRTEMU_GLchar;
typedef unsigned char CRTEMU_GLboolean;
typedef size_t CRTEMU_GLsizeiptr;
typedef unsigned int CRTEMU_GLbitfield;
#define CRTEMU_GL_FLOAT 0x1406
#define CRTEMU_GL_FALSE 0
#define CRTEMU_GL_FRAGMENT_SHADER 0x8b30
#define CRTEMU_GL_VERTEX_SHADER 0x8b31
#define CRTEMU_GL_COMPILE_STATUS 0x8b81
#define CRTEMU_GL_LINK_STATUS 0x8b82
#define CRTEMU_GL_INFO_LOG_LENGTH 0x8b84
#define CRTEMU_GL_ARRAY_BUFFER 0x8892
#define CRTEMU_GL_TEXTURE_2D 0x0de1
#define CRTEMU_GL_TEXTURE0 0x84c0
#define CRTEMU_GL_TEXTURE1 0x84c1
#define CRTEMU_GL_TEXTURE2 0x84c2
#define CRTEMU_GL_TEXTURE3 0x84c3
#define CRTEMU_GL_TEXTURE_MIN_FILTER 0x2801
#define CRTEMU_GL_TEXTURE_MAG_FILTER 0x2800
#define CRTEMU_GL_NEAREST 0x2600
#define CRTEMU_GL_LINEAR 0x2601
#define CRTEMU_GL_STATIC_DRAW 0x88e4
#define CRTEMU_GL_RGBA 0x1908
#define CRTEMU_GL_UNSIGNED_BYTE 0x1401
#define CRTEMU_GL_COLOR_BUFFER_BIT 0x00004000
#define CRTEMU_GL_TRIANGLE_FAN 0x0006
#define CRTEMU_GL_FRAMEBUFFER 0x8d40
#define CRTEMU_GL_VIEWPORT 0x0ba2
#define CRTEMU_GL_RGB 0x1907
#define CRTEMU_GL_COLOR_ATTACHMENT0 0x8ce0
#define CRTEMU_GL_TEXTURE_WRAP_S 0x2802
#define CRTEMU_GL_TEXTURE_WRAP_T 0x2803
#define CRTEMU_GL_CLAMP_TO_BORDER 0x812D
#define CRTEMU_GL_TEXTURE_BORDER_COLOR 0x1004
#else
#ifndef CRTEMU_WEBGL
#include <GL/glew.h>
#include "SDL_opengl.h"
#else
#include <wajic_gl.h>
#endif
#define CRTEMU_GLCALLTYPE GLAPIENTRY
typedef GLuint CRTEMU_GLuint;
typedef GLsizei CRTEMU_GLsizei;
typedef GLenum CRTEMU_GLenum;
typedef GLint CRTEMU_GLint;
typedef GLfloat CRTEMU_GLfloat;
typedef GLchar CRTEMU_GLchar;
typedef GLboolean CRTEMU_GLboolean;
typedef GLsizeiptr CRTEMU_GLsizeiptr;
typedef GLbitfield CRTEMU_GLbitfield;
#define CRTEMU_GL_FLOAT GL_FLOAT
#define CRTEMU_GL_FALSE GL_FALSE
#define CRTEMU_GL_FRAGMENT_SHADER GL_FRAGMENT_SHADER
#define CRTEMU_GL_VERTEX_SHADER GL_VERTEX_SHADER
#define CRTEMU_GL_COMPILE_STATUS GL_COMPILE_STATUS
#define CRTEMU_GL_LINK_STATUS GL_LINK_STATUS
#define CRTEMU_GL_INFO_LOG_LENGTH GL_INFO_LOG_LENGTH
#define CRTEMU_GL_ARRAY_BUFFER GL_ARRAY_BUFFER
#define CRTEMU_GL_TEXTURE_2D GL_TEXTURE_2D
#define CRTEMU_GL_TEXTURE0 GL_TEXTURE0
#define CRTEMU_GL_TEXTURE1 GL_TEXTURE1
#define CRTEMU_GL_TEXTURE2 GL_TEXTURE2
#define CRTEMU_GL_TEXTURE3 GL_TEXTURE3
#define CRTEMU_GL_TEXTURE_MIN_FILTER GL_TEXTURE_MIN_FILTER
#define CRTEMU_GL_TEXTURE_MAG_FILTER GL_TEXTURE_MAG_FILTER
#define CRTEMU_GL_NEAREST GL_NEAREST
#define CRTEMU_GL_LINEAR GL_LINEAR
#define CRTEMU_GL_STATIC_DRAW GL_STATIC_DRAW
#define CRTEMU_GL_RGBA GL_RGBA
#define CRTEMU_GL_UNSIGNED_BYTE GL_UNSIGNED_BYTE
#define CRTEMU_GL_COLOR_BUFFER_BIT GL_COLOR_BUFFER_BIT
#define CRTEMU_GL_TRIANGLE_FAN GL_TRIANGLE_FAN
#define CRTEMU_GL_FRAMEBUFFER GL_FRAMEBUFFER
#define CRTEMU_GL_VIEWPORT GL_VIEWPORT
#define CRTEMU_GL_RGB GL_RGB
#define CRTEMU_GL_COLOR_ATTACHMENT0 GL_COLOR_ATTACHMENT0
#define CRTEMU_GL_TEXTURE_WRAP_S GL_TEXTURE_WRAP_S
#define CRTEMU_GL_TEXTURE_WRAP_T GL_TEXTURE_WRAP_T
#ifndef CRTEMU_WEBGL
#define CRTEMU_GL_CLAMP_TO_BORDER GL_CLAMP_TO_BORDER
#define CRTEMU_GL_TEXTURE_BORDER_COLOR GL_TEXTURE_BORDER_COLOR
#else
// WebGL does not support GL_CLAMP_TO_BORDER, we have to emulate
// this behavior with code in the fragment shader
#define CRTEMU_GL_CLAMP_TO_BORDER GL_CLAMP_TO_EDGE
#endif
#endif
struct crtemu_t {
crtemu_type_t type;
void* memctx;
CRTEMU_GLuint vertexbuffer;
CRTEMU_GLuint backbuffer;
CRTEMU_GLuint fbo_backbuffer;
CRTEMU_GLuint accumulatetexture_a;
CRTEMU_GLuint accumulatetexture_b;
CRTEMU_GLuint accumulatebuffer_a;
CRTEMU_GLuint accumulatebuffer_b;
CRTEMU_GLuint blurtexture_a;
CRTEMU_GLuint blurtexture_b;
CRTEMU_GLuint blurbuffer_a;
CRTEMU_GLuint blurbuffer_b;
CRTEMU_GLuint frametexture;
float use_frame;
CRTEMU_GLuint crt_shader;
CRTEMU_GLuint blur_shader;
CRTEMU_GLuint accumulate_shader;
CRTEMU_GLuint blend_shader;
CRTEMU_GLuint copy_shader;
int last_present_width;
int last_present_height;
#ifndef CRTEMU_SDL
struct HINSTANCE__* gl_dll;
CRTEMU_PROC (CRTEMU_GLCALLTYPE *wglGetProcAddress) (char const* );
#endif
void (CRTEMU_GLCALLTYPE* TexParameterfv) (CRTEMU_GLenum target, CRTEMU_GLenum pname, CRTEMU_GLfloat const* params);
void (CRTEMU_GLCALLTYPE* DeleteFramebuffers) (CRTEMU_GLsizei n, CRTEMU_GLuint const* framebuffers);
void (CRTEMU_GLCALLTYPE* GetIntegerv) (CRTEMU_GLenum pname, CRTEMU_GLint *data);
void (CRTEMU_GLCALLTYPE* GenFramebuffers) (CRTEMU_GLsizei n, CRTEMU_GLuint *framebuffers);
void (CRTEMU_GLCALLTYPE* BindFramebuffer) (CRTEMU_GLenum target, CRTEMU_GLuint framebuffer);
void (CRTEMU_GLCALLTYPE* Uniform1f) (CRTEMU_GLint location, CRTEMU_GLfloat v0);
void (CRTEMU_GLCALLTYPE* Uniform2f) (CRTEMU_GLint location, CRTEMU_GLfloat v0, CRTEMU_GLfloat v1);
void (CRTEMU_GLCALLTYPE* FramebufferTexture2D) (CRTEMU_GLenum target, CRTEMU_GLenum attachment, CRTEMU_GLenum textarget, CRTEMU_GLuint texture, CRTEMU_GLint level);
CRTEMU_GLuint (CRTEMU_GLCALLTYPE* CreateShader) (CRTEMU_GLenum type);
void (CRTEMU_GLCALLTYPE* ShaderSource) (CRTEMU_GLuint shader, CRTEMU_GLsizei count, CRTEMU_GLchar const* const* string, CRTEMU_GLint const* length);
void (CRTEMU_GLCALLTYPE* CompileShader) (CRTEMU_GLuint shader);
void (CRTEMU_GLCALLTYPE* GetShaderiv) (CRTEMU_GLuint shader, CRTEMU_GLenum pname, CRTEMU_GLint *params);
CRTEMU_GLuint (CRTEMU_GLCALLTYPE* CreateProgram) (void);
void (CRTEMU_GLCALLTYPE* AttachShader) (CRTEMU_GLuint program, CRTEMU_GLuint shader);
void (CRTEMU_GLCALLTYPE* BindAttribLocation) (CRTEMU_GLuint program, CRTEMU_GLuint index, CRTEMU_GLchar const* name);
void (CRTEMU_GLCALLTYPE* LinkProgram) (CRTEMU_GLuint program);
void (CRTEMU_GLCALLTYPE* GetProgramiv) (CRTEMU_GLuint program, CRTEMU_GLenum pname, CRTEMU_GLint *params);
void (CRTEMU_GLCALLTYPE* GenBuffers) (CRTEMU_GLsizei n, CRTEMU_GLuint *buffers);
void (CRTEMU_GLCALLTYPE* BindBuffer) (CRTEMU_GLenum target, CRTEMU_GLuint buffer);
void (CRTEMU_GLCALLTYPE* EnableVertexAttribArray) (CRTEMU_GLuint index);
void (CRTEMU_GLCALLTYPE* VertexAttribPointer) (CRTEMU_GLuint index, CRTEMU_GLint size, CRTEMU_GLenum type, CRTEMU_GLboolean normalized, CRTEMU_GLsizei stride, void const* pointer);
void (CRTEMU_GLCALLTYPE* GenTextures) (CRTEMU_GLsizei n, CRTEMU_GLuint* textures);
void (CRTEMU_GLCALLTYPE* Enable) (CRTEMU_GLenum cap);
void (CRTEMU_GLCALLTYPE* ActiveTexture) (CRTEMU_GLenum texture);
void (CRTEMU_GLCALLTYPE* BindTexture) (CRTEMU_GLenum target, CRTEMU_GLuint texture);
void (CRTEMU_GLCALLTYPE* TexParameteri) (CRTEMU_GLenum target, CRTEMU_GLenum pname, CRTEMU_GLint param);
void (CRTEMU_GLCALLTYPE* DeleteBuffers) (CRTEMU_GLsizei n, CRTEMU_GLuint const* buffers);
void (CRTEMU_GLCALLTYPE* DeleteTextures) (CRTEMU_GLsizei n, CRTEMU_GLuint const* textures);
void (CRTEMU_GLCALLTYPE* BufferData) (CRTEMU_GLenum target, CRTEMU_GLsizeiptr size, void const *data, CRTEMU_GLenum usage);
void (CRTEMU_GLCALLTYPE* UseProgram) (CRTEMU_GLuint program);
void (CRTEMU_GLCALLTYPE* Uniform1i) (CRTEMU_GLint location, CRTEMU_GLint v0);
void (CRTEMU_GLCALLTYPE* Uniform3f) (CRTEMU_GLint location, CRTEMU_GLfloat v0, CRTEMU_GLfloat v1, CRTEMU_GLfloat v2);
CRTEMU_GLint (CRTEMU_GLCALLTYPE* GetUniformLocation) (CRTEMU_GLuint program, CRTEMU_GLchar const* name);
void (CRTEMU_GLCALLTYPE* TexImage2D) (CRTEMU_GLenum target, CRTEMU_GLint level, CRTEMU_GLint internalformat, CRTEMU_GLsizei width, CRTEMU_GLsizei height, CRTEMU_GLint border, CRTEMU_GLenum format, CRTEMU_GLenum type, void const* pixels);
void (CRTEMU_GLCALLTYPE* ClearColor) (CRTEMU_GLfloat red, CRTEMU_GLfloat green, CRTEMU_GLfloat blue, CRTEMU_GLfloat alpha);
void (CRTEMU_GLCALLTYPE* Clear) (CRTEMU_GLbitfield mask);
void (CRTEMU_GLCALLTYPE* DrawArrays) (CRTEMU_GLenum mode, CRTEMU_GLint first, CRTEMU_GLsizei count);
void (CRTEMU_GLCALLTYPE* Viewport) (CRTEMU_GLint x, CRTEMU_GLint y, CRTEMU_GLsizei width, CRTEMU_GLsizei height);
void (CRTEMU_GLCALLTYPE* DeleteShader) (CRTEMU_GLuint shader);
void (CRTEMU_GLCALLTYPE* DeleteProgram) (CRTEMU_GLuint program);
#ifdef CRTEMU_REPORT_SHADER_ERRORS
void (CRTEMU_GLCALLTYPE* GetShaderInfoLog) (CRTEMU_GLuint shader, CRTEMU_GLsizei bufSize, CRTEMU_GLsizei *length, CRTEMU_GLchar *infoLog);
#endif
};
static CRTEMU_GLuint crtemu_internal_build_shader( crtemu_t* crtemu, char const* vs_source, char const* fs_source ) {
#ifdef CRTEMU_REPORT_SHADER_ERRORS
char error_message[ 1024 ];
#endif
CRTEMU_GLuint vs = crtemu->CreateShader( CRTEMU_GL_VERTEX_SHADER );
crtemu->ShaderSource( vs, 1, (char const**) &vs_source, NULL );
crtemu->CompileShader( vs );
CRTEMU_GLint vs_compiled;
crtemu->GetShaderiv( vs, CRTEMU_GL_COMPILE_STATUS, &vs_compiled );
if( !vs_compiled ) {
#ifdef CRTEMU_REPORT_SHADER_ERRORS
char const* prefix = "Vertex Shader Error: ";
strcpy( error_message, prefix );
int len = 0, written = 0;
crtemu->GetShaderiv( vs, CRTEMU_GL_INFO_LOG_LENGTH, &len );
crtemu->GetShaderInfoLog( vs, (CRTEMU_GLsizei)( sizeof( error_message ) - strlen( prefix ) ), &written,
error_message + strlen( prefix ) );
CRTEMU_REPORT_ERROR( error_message );
#endif
return 0;
}
CRTEMU_GLuint fs = crtemu->CreateShader( CRTEMU_GL_FRAGMENT_SHADER );
crtemu->ShaderSource( fs, 1, (char const**) &fs_source, NULL );
crtemu->CompileShader( fs );
CRTEMU_GLint fs_compiled;
crtemu->GetShaderiv( fs, CRTEMU_GL_COMPILE_STATUS, &fs_compiled );
if( !fs_compiled ) {
#ifdef CRTEMU_REPORT_SHADER_ERRORS
char const* prefix = "Fragment Shader Error: ";
strcpy( error_message, prefix );
int len = 0, written = 0;
crtemu->GetShaderiv( vs, CRTEMU_GL_INFO_LOG_LENGTH, &len );
crtemu->GetShaderInfoLog( fs, (CRTEMU_GLsizei)( sizeof( error_message ) - strlen( prefix ) ), &written,
error_message + strlen( prefix ) );
CRTEMU_REPORT_ERROR( error_message );
#endif
return 0;
}
CRTEMU_GLuint prg = crtemu->CreateProgram();
crtemu->AttachShader( prg, fs );
crtemu->AttachShader( prg, vs );
crtemu->BindAttribLocation( prg, 0, "pos" );
crtemu->LinkProgram( prg );
CRTEMU_GLint linked;
crtemu->GetProgramiv( prg, CRTEMU_GL_LINK_STATUS, &linked );
if( !linked ) {
#ifdef CRTEMU_REPORT_SHADER_ERRORS
char const* prefix = "Shader Link Error: ";
strcpy( error_message, prefix );
int len = 0, written = 0;
crtemu->GetShaderiv( vs, CRTEMU_GL_INFO_LOG_LENGTH, &len );
crtemu->GetShaderInfoLog( prg, (CRTEMU_GLsizei)( sizeof( error_message ) - strlen( prefix ) ), &written,
error_message + strlen( prefix ) );
CRTEMU_REPORT_ERROR( error_message );
#endif
return 0;
}
return prg;
}
bool crtemu_shaders_tv( crtemu_t* crtemu ) {
char const* vs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
""
"attribute vec4 pos;"
"varying vec2 uv;"
""
"void main( void )"
" {"
" gl_Position = vec4( pos.xy, 0.0, 1.0 );"
" uv = pos.zw;"
" }";
char const* crt_fs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
"\n"
"varying vec2 uv;\n"
"\n"
"uniform vec3 modulate;\n"
"uniform vec2 resolution;\n"
"uniform vec2 size;\n"
"uniform float time;\n"
"uniform sampler2D backbuffer;\n"
"uniform sampler2D blurbuffer;\n"
"uniform sampler2D frametexture;\n"
"uniform float use_frame;\n"
"\n"
#ifdef CRTEMU_WEBGL
// WebGL does not support GL_CLAMP_TO_BORDER so we overwrite texture2D
// with this function which emulates the clamp-to-border behavior
"vec4 texture2Dborder(sampler2D samp, vec2 tc)\n"
" {\n"
" float borderdist = .502-max(abs(.5-tc.x), abs(.5-tc.y));\n"
" float borderfade = clamp(borderdist * 400.0, 0.0, 1.0);\n"
" return texture2D( samp, tc ) * borderfade;\n"
" }\n"
"#define texture2D texture2Dborder\n"
#endif
"vec3 tsample( sampler2D samp, vec2 tc, float offs, vec2 resolution )\n"
" {\n"
" tc = tc * vec2(1.025, 0.92) + vec2(-0.0125, 0.04);\n"
" vec3 s = pow( abs( texture2D( samp, vec2( tc.x, 1.0-tc.y ) ).rgb), vec3( 2.2 ) );\n"
" return s*vec3(1.25);\n"
" }"
"\n"
"vec3 filmic( vec3 LinearColor )\n"
" {\n"
" vec3 x = max( vec3(0.0), LinearColor-vec3(0.004));\n"
" return (x*(6.2*x+0.5))/(x*(6.2*x+1.7)+0.06);\n"
" }\n"
"\n"
"vec2 curve( vec2 uv )\n"
" {\n"
" uv = (uv - 0.5) * 2.0;\n"
" uv *= 1.1; \n"
" uv.x *= 1.0 + pow((abs(uv.y) / 5.0), 2.0);\n"
" uv.y *= 1.0 + pow((abs(uv.x) / 4.0), 2.0);\n"
" uv = (uv / 2.0) + 0.5;\n"
" uv = uv *0.92 + 0.04;\n"
" return uv;\n"
" }\n"
"\n"
"float rand(vec2 co)\n"
" {\n"
" return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);\n"
" }\n"
" \n"
"void main(void)\n"
" {\n"
" /* Curve */\n"
" vec2 curved_uv = mix( curve( uv ), uv, 0.4 );\n"
" float scale = 0.04;\n"
" vec2 scuv = curved_uv*(1.0-scale)+scale/2.0+vec2(0.003, -0.001);\n"
"\n"
" /* Main color, Bleed */\n"
" vec3 col;\n"
" float x = sin(0.1*time+curved_uv.y*13.0)*sin(0.23*time+curved_uv.y*19.0)*sin(0.3+0.11*time+curved_uv.y*23.0)*0.0012;\n"
" float o =sin(gl_FragCoord.y*1.5)/resolution.x;\n"
" x=x*0.25+o*0.25;\n"
" col.r = tsample(backbuffer,vec2(x+scuv.x+0.0009,scuv.y+0.0009),resolution.y/800.0, resolution ).x+0.02;\n"
" col.g = tsample(backbuffer,vec2(x+scuv.x+0.0000,scuv.y-0.0011),resolution.y/800.0, resolution ).y+0.02;\n"
" col.b = tsample(backbuffer,vec2(x+scuv.x-0.0015,scuv.y+0.0000),resolution.y/800.0, resolution ).z+0.02;\n"
" float i = clamp(col.r*0.299 + col.g*0.587 + col.b*0.114, 0.0, 1.0 ); \n"
" i = pow( 1.0 - pow(i,2.0), 1.0 );\n"
" i = (1.0-i) * 0.85 + 0.15; \n"
"\n"
" /* Ghosting */\n"
" float ghs = 0.15;\n"
" vec3 r = tsample(blurbuffer, vec2(x-0.014*1.0, -0.027)*0.85+0.007*vec2( 0.35*sin(1.0/7.0 + 15.0*curved_uv.y + 0.9*time), \n"
" 0.35*sin( 2.0/7.0 + 10.0*curved_uv.y + 1.37*time) )+vec2(scuv.x+0.001,scuv.y+0.001),\n"
" 5.5+1.3*sin( 3.0/9.0 + 31.0*curved_uv.x + 1.70*time),resolution).xyz*vec3(0.5,0.25,0.25);\n"
" vec3 g = tsample(blurbuffer, vec2(x-0.019*1.0, -0.020)*0.85+0.007*vec2( 0.35*cos(1.0/9.0 + 15.0*curved_uv.y + 0.5*time), \n"
" 0.35*sin( 2.0/9.0 + 10.0*curved_uv.y + 1.50*time) )+vec2(scuv.x+0.000,scuv.y-0.002),\n"
" 5.4+1.3*sin( 3.0/3.0 + 71.0*curved_uv.x + 1.90*time),resolution).xyz*vec3(0.25,0.5,0.25);\n"
" vec3 b = tsample(blurbuffer, vec2(x-0.017*1.0, -0.003)*0.85+0.007*vec2( 0.35*sin(2.0/3.0 + 15.0*curved_uv.y + 0.7*time), \n"
" 0.35*cos( 2.0/3.0 + 10.0*curved_uv.y + 1.63*time) )+vec2(scuv.x-0.002,scuv.y+0.000),\n"
" 5.3+1.3*sin( 3.0/7.0 + 91.0*curved_uv.x + 1.65*time),resolution).xyz*vec3(0.25,0.25,0.5);\n"
"\n"
" col += vec3(ghs*(1.0-0.299))*pow(clamp(vec3(3.0)*r,vec3(0.0),vec3(1.0)),vec3(2.0))*vec3(i);\n"
" col += vec3(ghs*(1.0-0.587))*pow(clamp(vec3(3.0)*g,vec3(0.0),vec3(1.0)),vec3(2.0))*vec3(i);\n"
" col += vec3(ghs*(1.0-0.114))*pow(clamp(vec3(3.0)*b,vec3(0.0),vec3(1.0)),vec3(2.0))*vec3(i);\n"
"\n"
" /* Level adjustment (curves) */\n"
" col *= vec3(0.95,1.05,0.95);\n"
" col = clamp(col*1.3 + 0.75*col*col + 1.25*col*col*col*col*col,vec3(0.0),vec3(10.0));\n"
"\n"
" /* Vignette */\n"
" float vig = (0.1 + 1.0*16.0*curved_uv.x*curved_uv.y*(1.0-curved_uv.x)*(1.0-curved_uv.y));\n"
" vig = 1.3*pow(vig,0.5);\n"
" col *= vig;\n"
"\n"
" /* Scanlines */\n"
" float scans = clamp( 0.35+0.18*sin(4.0*time+curved_uv.y*size.y*1.5), 0.0, 1.0);\n"
" float s = pow(scans,0.9);\n"
" col = col * vec3(s);\n"
"\n"
" /* Vertical lines (shadow mask) */\n"
" col*=1.0-0.23*(clamp((mod(gl_FragCoord.xy.x, 3.0))/2.0,0.0,1.0));\n"
"\n"
" /* Tone map */\n"
" col = filmic( col );\n"
"\n"
" /* Noise */\n"
" /*vec2 seed = floor(curved_uv*resolution.xy*vec2(0.5))/resolution.xy;*/\n"
" vec2 seed = curved_uv*resolution.xy;;\n"
" /* seed = curved_uv; */\n"
" col -= 0.015*pow(vec3(rand( seed +time ), rand( seed +time*2.0 ), rand( seed +time * 3.0 ) ), vec3(1.5) );\n"
"\n"
" /* Flicker */\n"
" col *= (1.0-0.004*(sin(50.0*time+curved_uv.y*2.0)*0.5+0.5));\n"
"\n"
" /* Clamp */\n"
" if (curved_uv.x < 0.0 || curved_uv.x > 1.0)\n"
" col *= 0.0;\n"
" if (curved_uv.y < 0.0 || curved_uv.y > 1.0)\n"
" col *= 0.0;\n"
" col *= modulate;\n"
" /* Frame */\n"
" vec2 fuv=vec2( uv.x, 1.0 - uv.y);\n"
" vec4 f=texture2D(frametexture,fuv);\n"
" vec3 fr = mix( max( col, 0.0), f.xyz, f.w);\n"
" col = mix( col, fr, vec3( use_frame ) );\n"
" \n"
" gl_FragColor = vec4( col, 1.0 );\n"
" }\n"
"\n";
char const* blur_fs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
""
"varying vec2 uv;"
""
"uniform vec2 blur;"
"uniform sampler2D texture;"
""
"void main( void )"
" {"
" vec4 sum = texture2D( texture, uv ) * 0.2270270270;"
" sum += texture2D(texture, vec2( uv.x - 4.0 * blur.x, uv.y - 4.0 * blur.y ) ) * 0.0162162162;"
" sum += texture2D(texture, vec2( uv.x - 3.0 * blur.x, uv.y - 3.0 * blur.y ) ) * 0.0540540541;"
" sum += texture2D(texture, vec2( uv.x - 2.0 * blur.x, uv.y - 2.0 * blur.y ) ) * 0.1216216216;"
" sum += texture2D(texture, vec2( uv.x - 1.0 * blur.x, uv.y - 1.0 * blur.y ) ) * 0.1945945946;"
" sum += texture2D(texture, vec2( uv.x + 1.0 * blur.x, uv.y + 1.0 * blur.y ) ) * 0.1945945946;"
" sum += texture2D(texture, vec2( uv.x + 2.0 * blur.x, uv.y + 2.0 * blur.y ) ) * 0.1216216216;"
" sum += texture2D(texture, vec2( uv.x + 3.0 * blur.x, uv.y + 3.0 * blur.y ) ) * 0.0540540541;"
" sum += texture2D(texture, vec2( uv.x + 4.0 * blur.x, uv.y + 4.0 * blur.y ) ) * 0.0162162162;"
" gl_FragColor = sum;"
" } "
"";
char const* accumulate_fs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
""
"varying vec2 uv;"
""
"uniform sampler2D tex0;"
"uniform sampler2D tex1;"
"uniform float modulate;"
""
"void main( void )"
" {"
" vec4 a = texture2D( tex0, uv ) * vec4( modulate );"
" vec4 b = texture2D( tex1, uv );"
""
" gl_FragColor = max( a, b * 0.96 );"
" } "
"";
char const* blend_fs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
""
"varying vec2 uv;"
""
"uniform sampler2D tex0;"
"uniform sampler2D tex1;"
"uniform float modulate;"
""
"void main( void )"
" {"
" vec4 a = texture2D( tex0, uv ) * vec4( modulate );"
" vec4 b = texture2D( tex1, uv );"
""
" gl_FragColor = max( a, b * 0.32 );"
" } "
"";
char const* copy_fs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
""
"varying vec2 uv;"
""
"uniform sampler2D tex0;"
""
"void main( void )"
" {"
" gl_FragColor = texture2D( tex0, uv );"
" } "
"";
crtemu->crt_shader = crtemu_internal_build_shader( crtemu, vs_source, crt_fs_source );
if( crtemu->crt_shader == 0 ) return false;
crtemu->blur_shader = crtemu_internal_build_shader( crtemu, vs_source, blur_fs_source );
if( crtemu->blur_shader == 0 ) return false;
crtemu->accumulate_shader = crtemu_internal_build_shader( crtemu, vs_source, accumulate_fs_source );
if( crtemu->accumulate_shader == 0 ) return false;
crtemu->blend_shader = crtemu_internal_build_shader( crtemu, vs_source, blend_fs_source );
if( crtemu->blend_shader == 0 ) return false;
crtemu->copy_shader = crtemu_internal_build_shader( crtemu, vs_source, copy_fs_source );
if( crtemu->copy_shader == 0 ) return false;
return true;
}
bool crtemu_shaders_pc( crtemu_t* crtemu ) {
char const* vs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
""
"attribute vec4 pos;"
"varying vec2 uv;"
""
"void main( void )"
" {"
" gl_Position = vec4( pos.xy, 0.0, 1.0 );"
" uv = pos.zw;"
" }";
char const* crt_fs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
"\n"
"varying vec2 uv;\n"
"\n"
"uniform vec3 modulate;\n"
"uniform vec2 resolution;\n"
"uniform vec2 size;\n"
"uniform float time;\n"
"uniform sampler2D backbuffer;\n"
"uniform sampler2D blurbuffer;\n"
"uniform sampler2D frametexture;\n"
"uniform float use_frame;\n"
"\n"
#ifdef CRTEMU_WEBGL
// WebGL does not support GL_CLAMP_TO_BORDER so we overwrite texture2D
// with this function which emulates the clamp-to-border behavior
"vec4 texture2Dborder(sampler2D samp, vec2 tc)\n"
" {\n"
" float borderdist = .502-max(abs(.5-tc.x), abs(.5-tc.y));\n"
" float borderfade = clamp(borderdist * 400.0, 0.0, 1.0);\n"
" return texture2D( samp, tc ) * borderfade;\n"
" }\n"
"#define texture2D texture2Dborder\n"
#endif
"vec3 tsample( sampler2D samp, vec2 tc, float offs, vec2 resolution )\n"
" {\n"
" tc = tc * vec2(1.156, 1.156) - vec2( 0.078 + 0.003, 0.078 );\n"
" vec3 s = pow( abs( texture2D( samp, vec2( tc.x, 1.0-tc.y ) ).rgb), vec3( 2.2 ) );\n"
" return s*vec3(1.25);\n"
" }\n"
"\n"
"vec3 filmic( vec3 LinearColor )\n"
" {\n"
" vec3 x = max( vec3(0.0), LinearColor-vec3(0.004));\n"
" return (x*(6.2*x+0.5))/(x*(6.2*x+1.7)+0.06);\n"
" }\n"
"\n"
"vec2 curve( vec2 uv )\n"
" {\n"
" uv = (uv - 0.5) * 2.0;\n"
" uv *= 1.1; \n"
" uv.x *= 1.0 + pow((abs(uv.y) / 5.0), 2.0);\n"
" uv.y *= 1.0 + pow((abs(uv.x) / 4.0), 2.0);\n"
" uv = (uv / 2.0) + 0.5;\n"
" uv = uv *0.92 + 0.04;\n"
" return uv;\n"
" }\n"
"\n"
"float rand(vec2 co)\n"
" {\n"
" return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);\n"
" }\n"
" \n"
"void main(void)\n"
" {\n"
" /* Curve */\n"
" vec2 curved_uv = mix( curve( uv ), uv, 0.8 );\n"
" float scale = 0.04;\n"
" vec2 scuv = curved_uv*(1.0-scale)+scale/2.0+vec2(0.003, -0.001);\n"
"\n"
" /* Main color, Bleed */\n"
" vec3 col;\n"
" float x = sin(0.1*time+curved_uv.y*13.0)*sin(0.23*time+curved_uv.y*19.0)*sin(0.3+0.11*time+curved_uv.y*23.0)*0.0012;\n"
" float o =sin(gl_FragCoord.y*1.5)/resolution.x;\n"
" x+=o*0.25;\n"
" x *= 0.2;\n"
" col.r = tsample(backbuffer,vec2(x+scuv.x+0.0009*0.25,scuv.y+0.0009*0.25),resolution.y/800.0, resolution ).x+0.02;\n"
" col.g = tsample(backbuffer,vec2(x+scuv.x+0.0000*0.25,scuv.y-0.0011*0.25),resolution.y/800.0, resolution ).y+0.02;\n"
" col.b = tsample(backbuffer,vec2(x+scuv.x-0.0015*0.25,scuv.y+0.0000*0.25),resolution.y/800.0, resolution ).z+0.02;\n"
" float i = clamp(col.r*0.299 + col.g*0.587 + col.b*0.114, 0.0, 1.0 ); \n"
" i = pow( 1.0 - pow(i,2.0), 1.0 );\n"
" i = (1.0-i) * 0.85 + 0.15; \n"
"\n"
" /* Ghosting */\n"
" float ghs = 0.05;\n"
" vec3 r = tsample(blurbuffer, vec2(x-0.014*1.0, -0.027)*0.45+0.007*vec2( 0.35*sin(1.0/7.0 + 15.0*curved_uv.y + 0.9*time), \n"
" 0.35*sin( 2.0/7.0 + 10.0*curved_uv.y + 1.37*time) )+vec2(scuv.x+0.001,scuv.y+0.001),\n"
" 5.5+1.3*sin( 3.0/9.0 + 31.0*curved_uv.x + 1.70*time),resolution).xyz*vec3(0.5,0.25,0.25);\n"
" vec3 g = tsample(blurbuffer, vec2(x-0.019*1.0, -0.020)*0.45+0.007*vec2( 0.35*cos(1.0/9.0 + 15.0*curved_uv.y + 0.5*time), \n"
" 0.35*sin( 2.0/9.0 + 10.0*curved_uv.y + 1.50*time) )+vec2(scuv.x+0.000,scuv.y-0.002),\n"
" 5.4+1.3*sin( 3.0/3.0 + 71.0*curved_uv.x + 1.90*time),resolution).xyz*vec3(0.25,0.5,0.25);\n"
" vec3 b = tsample(blurbuffer, vec2(x-0.017*1.0, -0.003)*0.35+0.007*vec2( 0.35*sin(2.0/3.0 + 15.0*curved_uv.y + 0.7*time), \n"
" 0.35*cos( 2.0/3.0 + 10.0*curved_uv.y + 1.63*time) )+vec2(scuv.x-0.002,scuv.y+0.000),\n"
" 5.3+1.3*sin( 3.0/7.0 + 91.0*curved_uv.x + 1.65*time),resolution).xyz*vec3(0.25,0.25,0.5);\n"
"\n"
" col += vec3(ghs*(1.0-0.299))*pow(clamp(vec3(3.0)*r,vec3(0.0),vec3(1.0)),vec3(2.0))*vec3(i);\n"
" col += vec3(ghs*(1.0-0.587))*pow(clamp(vec3(3.0)*g,vec3(0.0),vec3(1.0)),vec3(2.0))*vec3(i);\n"
" col += vec3(ghs*(1.0-0.114))*pow(clamp(vec3(3.0)*b,vec3(0.0),vec3(1.0)),vec3(2.0))*vec3(i);\n"
"\n"
" /* Level adjustment (curves) */\n"
" col *= vec3(0.95,0.95,0.95);\n"
" col = clamp(col*1.3 + 0.75*col*col + 1.25*col*col*col*col*col,vec3(0.0),vec3(10.0));\n"
"\n"
" /* Vignette */\n"
" float vig = (0.1 + 1.0*16.0*curved_uv.x*curved_uv.y*(1.0-curved_uv.x)*(1.0-curved_uv.y));\n"
" vig = 1.3*pow(vig,0.5);\n"
" col *= vig;\n"
"\n"
" /* Scanlines */\n"
" float scans = clamp( 0.5+0.2*sin(cos(20.0*time)*0.32+curved_uv.y*size.y*1.75), 0.0, 1.0);\n"
" float s = pow(scans,0.9);\n"
" col = col * vec3(s);\n"
"\n"
" /* Vertical lines (shadow mask) */\n"
" col*=1.0-0.23*(clamp((mod(gl_FragCoord.xy.x, 3.0))/2.0,0.0,1.0));\n"
"\n"
" /* Tone map */\n"
" col = filmic( col );\n"
"\n"
" /* Noise */\n"
" //vec2 seed = floor(curved_uv*resolution.xy*vec2(0.5))/resolution.xy;\n"
" vec2 seed = curved_uv*resolution.xy;;\n"
" /* seed = curved_uv; */\n"
" col -= 0.015*pow(vec3(rand( seed +time ), rand( seed +time*2.0 ), rand( seed +time * 3.0 ) ), vec3(1.5) );\n"
"\n"
" /* Flicker */\n"
" col *= (1.0-0.004*(sin(50.0*time+curved_uv.y*2.0)*0.5+0.5));\n"
"\n"
" /* Clamp */\n"
" if (curved_uv.x < 0.0 || curved_uv.x > 1.0)\n"
" col *= 0.0;\n"
" if (curved_uv.y < 0.0 || curved_uv.y > 1.0)\n"
" col *= 0.0;\n"
" col*=modulate; \n"
" /* Frame */\n"
" vec2 fuv=vec2( uv.x, 1.0 - uv.y);\n"
" vec4 f=texture2D(frametexture, fuv);\n"
" col = mix( col, mix( max( col, 0.0), f.xyz, f.w), vec3( use_frame) );\n"
" \n"
" gl_FragColor = vec4( col, 1.0 );\n"
" }\n"
" \n"
"";
char const* blur_fs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
""
"varying vec2 uv;"
""
"uniform vec2 blur;"
"uniform sampler2D texture;"
""
"void main( void )"
" {"
" vec4 sum = texture2D( texture, uv ) * 0.2270270270;"
" sum += texture2D(texture, vec2( uv.x - 4.0 * blur.x, uv.y - 4.0 * blur.y ) ) * 0.0162162162;"
" sum += texture2D(texture, vec2( uv.x - 3.0 * blur.x, uv.y - 3.0 * blur.y ) ) * 0.0540540541;"
" sum += texture2D(texture, vec2( uv.x - 2.0 * blur.x, uv.y - 2.0 * blur.y ) ) * 0.1216216216;"
" sum += texture2D(texture, vec2( uv.x - 1.0 * blur.x, uv.y - 1.0 * blur.y ) ) * 0.1945945946;"
" sum += texture2D(texture, vec2( uv.x + 1.0 * blur.x, uv.y + 1.0 * blur.y ) ) * 0.1945945946;"
" sum += texture2D(texture, vec2( uv.x + 2.0 * blur.x, uv.y + 2.0 * blur.y ) ) * 0.1216216216;"
" sum += texture2D(texture, vec2( uv.x + 3.0 * blur.x, uv.y + 3.0 * blur.y ) ) * 0.0540540541;"
" sum += texture2D(texture, vec2( uv.x + 4.0 * blur.x, uv.y + 4.0 * blur.y ) ) * 0.0162162162;"
" gl_FragColor = sum;"
" } "
"";
char const* accumulate_fs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
""
"varying vec2 uv;"
""
"uniform sampler2D tex0;"
"uniform sampler2D tex1;"
"uniform float modulate;"
""
"void main( void )"
" {"
" vec4 a = texture2D( tex0, uv ) * vec4( modulate );"
" vec4 b = texture2D( tex1, uv );"
""
" gl_FragColor = max( a, b * 0.96 );"
" } "
"";
char const* blend_fs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
""
"varying vec2 uv;"
""
"uniform sampler2D tex0;"
"uniform sampler2D tex1;"
"uniform float modulate;"
""
"void main( void )"
" {"
" vec4 a = texture2D( tex0, uv ) * vec4( modulate );"
" vec4 b = texture2D( tex1, uv );"
""
" gl_FragColor = max( a, b * 0.24 );"
" } "
"";
char const* copy_fs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
""
"varying vec2 uv;"
""
"uniform sampler2D tex0;"
""
"void main( void )"
" {"
" gl_FragColor = texture2D( tex0, uv );"
" } "
"";
crtemu->crt_shader = crtemu_internal_build_shader( crtemu, vs_source, crt_fs_source );
if( crtemu->crt_shader == 0 ) return false;
crtemu->blur_shader = crtemu_internal_build_shader( crtemu, vs_source, blur_fs_source );
if( crtemu->blur_shader == 0 ) return false;
crtemu->accumulate_shader = crtemu_internal_build_shader( crtemu, vs_source, accumulate_fs_source );
if( crtemu->accumulate_shader == 0 ) return false;
crtemu->blend_shader = crtemu_internal_build_shader( crtemu, vs_source, blend_fs_source );
if( crtemu->blend_shader == 0 ) return false;
crtemu->copy_shader = crtemu_internal_build_shader( crtemu, vs_source, copy_fs_source );
if( crtemu->copy_shader == 0 ) return false;
return true;
}
bool crtemu_shaders_lite( crtemu_t* crtemu ) {
char const* vs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
""
"attribute vec4 pos;"
"varying vec2 uv;"
""
"void main( void )"
" {"
" gl_Position = vec4( pos.xy, 0.0, 1.0 );"
" uv = pos.zw;"
" }";
char const* crt_fs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
"\n"
"varying vec2 uv;\n"
"\n"
"uniform vec3 modulate;\n"
"uniform vec2 resolution;\n"
"uniform vec2 size;\n"
"uniform float time;\n"
"uniform sampler2D backbuffer;\n"
"uniform sampler2D blurbuffer;\n"
"uniform sampler2D frametexture;\n"
"uniform float use_frame;\n"
"\n"
/* #ifdef CRTEMU_WEBGL
// WebGL does not support GL_CLAMP_TO_BORDER so we overwrite texture2D
// with this function which emulates the clamp-to-border behavior
"vec4 texture2Dborder(sampler2D samp, vec2 tc)\n"
" {\n"
" float borderdist = .502-max(abs(.5-tc.x), abs(.5-tc.y));\n"
" float borderfade = clamp(borderdist * 400.0, 0.0, 1.0);\n"
" return texture2D( samp, tc ) * borderfade;\n"
" }\n"
"#define texture2D texture2Dborder\n"
#endif*/
"vec3 tsample( sampler2D samp, vec2 tc )\n"
" {\n"
" vec3 s = pow( abs( texture2D( samp, vec2( tc.x, 1.0-tc.y ) ).rgb), vec3( 2.2 ) );\n"
" return s;\n"
" }\n"
"\n"
"vec3 filmic( vec3 LinearColor )\n"
" {\n"
" vec3 x = max( vec3(0.0), LinearColor-vec3(0.004));\n"
" return (x*(6.2*x+0.5))/(x*(6.2*x+1.7)+0.06);\n"
" }\n"
"\n"
"void main(void)\n"
" {\n"
" // Main color\n"
" vec3 col;\n"
" col = mix( tsample(backbuffer,uv ), tsample(frametexture,uv ), 0.45 );\n"
" col = 3.0*col + pow( col, vec3( 3.0 ) );\n"
" col += tsample(blurbuffer,uv )*0.3;\n"
"\n"
" // Scanlines\n"
" float scans = clamp( 0.5-0.5*cos( uv.y * 6.28319 * (size.y ) ), 0.0, 1.0);\n"
" float s = pow(scans,1.3);\n"
" col = mix( col, col * vec3(s), 0.7 );\n"
"\n"
" // Vertical lines (shadow mask)\n"
" col*=1.0-0.23*(clamp((mod(gl_FragCoord.xy.x, 3.0))/2.0,0.0,1.0));\n"
"\n"
" // Vignette\n"
" float vig = (0.1 + 1.0*16.0*uv.x*uv.y*(1.0-uv.x)*(1.0-uv.y));\n"
" vig = 1.3*pow(vig,0.5);\n"
" col = mix( col, col*vig, 0.2 );\n"
"\n"
" // Tone map\n"
" col = mix( pow( col, vec3(1.0 / 2.2) ), filmic( col ), 0.5 );\n"
"\n"
" col*=modulate; \n"
" gl_FragColor = vec4( col, 1.0 );\n"
" }\n"
" \n"
"";
char const* blur_fs_source =
#ifdef CRTEMU_WEBGL
"precision highp float;\n\n"
#else
"#version 120\n\n"
#endif
""
"varying vec2 uv;"
""
"uniform vec2 blur;"
"uniform sampler2D texture;"
""
"void main( void )"
" {"
" vec4 sum = texture2D( texture, uv ) * 0.2270270270;"
" sum += texture2D(texture, vec2( uv.x - 4.0 * blur.x, uv.y - 4.0 * blur.y ) ) * 0.0162162162;"
" sum += texture2D(texture, vec2( uv.x - 3.0 * blur.x, uv.y - 3.0 * blur.y ) ) * 0.0540540541;"
" sum += texture2D(texture, vec2( uv.x - 2.0 * blur.x, uv.y - 2.0 * blur.y ) ) * 0.1216216216;"
" sum += texture2D(texture, vec2( uv.x - 1.0 * blur.x, uv.y - 1.0 * blur.y ) ) * 0.1945945946;"
" sum += texture2D(texture, vec2( uv.x + 1.0 * blur.x, uv.y + 1.0 * blur.y ) ) * 0.1945945946;"
" sum += texture2D(texture, vec2( uv.x + 2.0 * blur.x, uv.y + 2.0 * blur.y ) ) * 0.1216216216;"
" sum += texture2D(texture, vec2( uv.x + 3.0 * blur.x, uv.y + 3.0 * blur.y ) ) * 0.0540540541;"
" sum += texture2D(texture, vec2( uv.x + 4.0 * blur.x, uv.y + 4.0 * blur.y ) ) * 0.0162162162;"
" gl_FragColor = sum;"
" } "
"";
char const* accumulate_fs_source =
#ifdef CRTEMU_WEBGL