~random-stuff/mupen64plus/mupen64plus-video-gliden64-tmp

« back to all changes in this revision

Viewing changes to src/Shaders.h

  • Committer: Sérgio Benjamim
  • Date: 2015-05-08 23:50:40 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20150508235040-ik7cmsfrx96a5vpz
Public Release 1.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifdef GLES3
 
2
#define SHADER_VERSION "#version 300 es \n"
 
3
#else
 
4
#define SHADER_VERSION "#version 330 core \n"
 
5
#endif
 
6
 
1
7
static const char* vertex_shader =
2
 
 
3
 
"#version 330 core                                                                              \n"
 
8
SHADER_VERSION
4
9
"in highp vec4 aPosition;                                               \n"
5
10
"in lowp vec4 aColor;                                                   \n"
6
11
"in highp vec2 aTexCoord0;                                              \n"
89
94
"      break;                                                                                                   \n"
90
95
"    }                                                                                                                  \n"
91
96
"  }                                                                                                                    \n"
 
97
#ifndef GLESX
92
98
"  gl_ClipDistance[0] = gl_Position.w - gl_Position.z;                  \n"
 
99
#endif
93
100
"}                                                                                                                              \n"
94
101
;
95
102
 
96
103
static const char* vertex_shader_notex =
97
 
"#version 330 core                                      \n"
 
104
SHADER_VERSION
98
105
"in highp vec4 aPosition;                       \n"
99
106
"in lowp vec4 aColor;                           \n"
100
107
"in lowp float aNumLights;                      \n"
146
153
"      break;                                                                                                   \n"
147
154
"    }                                                                                                                  \n"
148
155
"  }                                                                                                                    \n"
 
156
#ifndef GLESX
149
157
"  gl_ClipDistance[0] = gl_Position.w - gl_Position.z;                  \n"
 
158
#endif
150
159
"}                                                                                                                              \n"
151
160
;
152
161
 
153
162
static const char* fragment_shader_header_common_variables =
154
 
"#version 330 core                              \n"
 
163
SHADER_VERSION
155
164
"uniform sampler2D uTex0;               \n"
156
165
"uniform sampler2D uTex1;               \n"
 
166
#ifdef GL_MULTISAMPLING_SUPPORT
 
167
"uniform sampler2DMS uMSTex0;   \n"
 
168
"uniform sampler2DMS uMSTex1;   \n"
 
169
"uniform lowp ivec2 uMSTexEnabled;      \n"
 
170
#endif
157
171
"layout (std140) uniform ColorsBlock {\n"
158
172
"  lowp vec4 uFogColor;                 \n"
159
173
"  lowp vec4 uCenterColor;              \n"
165
179
"  lowp float uK4;                              \n"
166
180
"  lowp float uK5;                              \n"
167
181
"};                                                             \n"
 
182
#ifdef GLESX
 
183
"uniform mediump vec2 uScreenScale;     \n"
 
184
#endif
168
185
"uniform lowp int uAlphaCompareMode;    \n"
169
186
"uniform lowp int uAlphaDitherMode;     \n"
170
187
"uniform lowp int uColorDitherMode;     \n"
171
188
"uniform lowp int uGammaCorrectionEnabled;      \n"
172
189
"uniform lowp int uFogUsage;    \n"
173
 
"uniform lowp int uFb8Bit;              \n"
174
 
"uniform lowp int uFbFixedAlpha;\n"
 
190
"uniform lowp ivec2 uFb8Bit;            \n"
 
191
"uniform lowp ivec2 uFbFixedAlpha;\n"
175
192
"uniform lowp int uSpecialBlendMode;\n"
176
193
"uniform lowp int uEnableAlphaTest;     \n"
177
194
"uniform lowp float uAlphaTestValue;\n"
187
204
;
188
205
 
189
206
static const char* fragment_shader_header_common_variables_notex =
190
 
"#version 330 core                              \n"
 
207
SHADER_VERSION
191
208
"layout (std140) uniform ColorsBlock {\n"
192
209
"  lowp vec4 uFogColor;                 \n"
193
210
"  lowp vec4 uCenterColor;              \n"
199
216
"  lowp float uK4;                              \n"
200
217
"  lowp float uK5;                              \n"
201
218
"};                                                             \n"
 
219
#ifdef GLESX
 
220
"uniform mediump vec2 uScreenScale;     \n"
 
221
#endif
202
222
"uniform lowp int uAlphaCompareMode;    \n"
203
223
"uniform lowp int uAlphaDitherMode;     \n"
204
224
"uniform lowp int uColorDitherMode;     \n"
221
241
"void calc_light(in lowp float fLights, in lowp vec3 input_color, out lowp vec3 output_color);\n"
222
242
"mediump float mipmap(out lowp vec4 readtex0, out lowp vec4 readtex1);          \n"
223
243
"lowp vec4 readTex(in sampler2D tex, in mediump vec2 texCoord, in bool fb8bit, in bool fbFixedAlpha);   \n"
 
244
#ifdef GL_MULTISAMPLING_SUPPORT
 
245
"lowp vec4 readTexMS(in sampler2DMS mstex, in mediump vec2 texCoord, in bool fb8bit, in bool fbFixedAlpha);     \n"
 
246
#endif // GL_MULTISAMPLING_SUPPORT
224
247
"bool depth_compare();                                                                          \n"
225
248
"void colorNoiseDither(in float _noise, inout vec3 _color);     \n"
226
249
"void alphaNoiseDither(in float _noise, inout float _alpha);\n"
239
262
;
240
263
 
241
264
static const char* fragment_shader_calc_light =
242
 
"#version 330 core                                              \n"
 
265
#ifndef GLESX
 
266
SHADER_VERSION
 
267
#endif
243
268
"layout (std140) uniform LightBlock {                           \n"
244
269
"  mediump vec3 uLightDirection[8];     \n"
245
270
"  lowp vec3 uLightColor[8];    \n"
264
289
"                                                                       \n"
265
290
"void main()                                            \n"
266
291
"{                                                                      \n"
 
292
#ifndef GLESX
267
293
"  gl_FragDepth = clamp((gl_FragCoord.z * 2.0 - 1.0) * uDepthScale.s + uDepthScale.t, 0.0, 1.0);   \n"
 
294
#endif
268
295
"  lowp vec4 vec_color, combined_color; \n"
269
296
"  lowp float alpha1, alpha2;                   \n"
270
297
"  lowp vec3 color1, color2;                            \n"
271
298
;
272
299
 
273
300
static const char* fragment_shader_dither =
274
 
"#version 330 core                                      \n"
 
301
#ifndef GLESX
 
302
SHADER_VERSION
 
303
#endif
275
304
"void colorNoiseDither(in float _noise, inout vec3 _color)      \n"
276
305
"{                                                                                                                      \n"
277
306
"    mediump vec3 tmpColor = _color*255.0;                                      \n"
340
369
;
341
370
 
342
371
static const char* fragment_shader_mipmap =
343
 
"#version 330 core                                      \n"
 
372
#ifndef GLESX
 
373
SHADER_VERSION
344
374
"in mediump vec2 vTexCoord0;    \n"
345
375
"in mediump vec2 vTexCoord1;    \n"
346
376
"in mediump vec2 vLodTexCoord;  \n"
347
377
"uniform sampler2D uTex0;                       \n"
348
378
"uniform sampler2D uTex1;                       \n"
 
379
"uniform mediump vec2 uScreenScale;     \n"
 
380
#else
 
381
"#ifdef GL_OES_standard_derivatives                     \n"
 
382
"    #extension GL_OES_standard_derivatives : enable \n"
 
383
"#endif                                                                         \n"
 
384
#endif
349
385
"uniform lowp float uPrimitiveLod;              \n"
350
386
"uniform lowp int uEnableLod;           \n"
351
 
"uniform mediump vec2 uScreenScale;     \n"
352
387
"uniform mediump float uMinLod;         \n"
353
388
"uniform lowp int uMaxTile;                     \n"
354
389
"uniform lowp int uTextureDetail;       \n"
355
390
"                                                                                                               \n"
356
391
"mediump float mipmap(out lowp vec4 readtex0, out lowp vec4 readtex1) { \n"
357
 
"  if (uEnableLod == 0) {                                                               \n"
358
 
"    readtex0 = texture(uTex0, vTexCoord0);                     \n"
359
 
"    readtex1 = texture(uTex1, vTexCoord1);                     \n"
360
 
"    return uPrimitiveLod;                                                                      \n"
361
 
"  }                                                                                                    \n"
 
392
"  readtex0 = texture(uTex0, vTexCoord0);                               \n"
 
393
"  readtex1 = texture(uTex1, vTexCoord1);                               \n"
 
394
"  if (uEnableLod == 0)                                                                 \n"
 
395
"    return uPrimitiveLod;                                                              \n"
362
396
"  if (uMaxTile == 0) {                                                                 \n"
363
 
"    readtex0 = texture(uTex0, vTexCoord0);                             \n"
364
397
"    readtex1 = readtex0;                                                               \n"
365
398
"    return uMinLod;                                                                    \n"
366
399
"  }                                                                                                    \n"
383
416
"    lod_tile = min(float(uMaxTile), floor(log2(floor(lod)))); \n"
384
417
"    lod_frac = fract(lod/pow(2.0, lod_tile));                  \n"
385
418
"  }                                                                                                    \n"
 
419
"  lowp vec4 lod0 = textureLod(uTex1, vTexCoord1, 0.0); \n"
 
420
"  lowp vec4 lod1 = textureLod(uTex1, vTexCoord1, 1.0); \n"
 
421
"  lowp vec4 lodT = textureLod(uTex1, vTexCoord1, lod_tile);    \n"
 
422
"  lowp vec4 lodT_m1 = textureLod(uTex1, vTexCoord1, max(0.0, lod_tile - 1.0)); \n"
 
423
"  lowp vec4 lodT_p1 = textureLod(uTex1, vTexCoord1, lod_tile + 1.0);   \n"
386
424
"  if (lod_tile < 1.0) {                                                                \n"
387
425
"    if (magnifying) {                                                                  \n"
388
 
"      readtex0 = texture(uTex0, vTexCoord0);                   \n"
389
426
//     !sharpen && !detail
390
427
"      if (uTextureDetail == 0) readtex1 = readtex0;    \n"
391
 
"      else readtex1 = texture(uTex1, vTexCoord1);      \n"
392
428
"    } else {                                                                                   \n"
393
429
//     detail
394
430
"      if (uTextureDetail == 2) {                                               \n"
395
 
"        readtex0 = textureLod(uTex1, vTexCoord1, 0.0);\n"
396
 
"        readtex1 = textureLod(uTex1, vTexCoord1, 1.0);\n"
397
 
"      } else {                                                                                 \n"
398
 
"        readtex0 = texture(uTex0, vTexCoord0);         \n"
399
 
"        readtex1 = texture(uTex1, vTexCoord1);         \n"
 
431
"        readtex0 = lod0;                                                               \n"
 
432
"        readtex1 = lod1;                                                               \n"
400
433
"      }                                                                                                \n"
401
434
"    }                                                                                                  \n"
402
435
"  } else {                                                                                             \n"
403
436
"    if (uTextureDetail == 2) {                                                 \n"
404
 
"      readtex0 = textureLod(uTex1, vTexCoord1, lod_tile);              \n"
405
 
"      readtex1 = textureLod(uTex1, vTexCoord1, lod_tile + 1.0);        \n"
406
 
"    } else {                                                                                                           \n"
407
 
"      readtex0 = textureLod(uTex1, vTexCoord1, lod_tile - 1.0);        \n"
408
 
"      readtex1 = textureLod(uTex1, vTexCoord1, lod_tile);              \n"
409
 
"    }                                                                                                                          \n"
410
 
"  }                                                                                                                            \n"
411
 
"  return lod_frac;                                                                                                     \n"
412
 
"}                                                                                                                                      \n"
 
437
"      readtex0 = lodT;                                                                 \n"
 
438
"      readtex1 = lodT_p1;                                                              \n"
 
439
"    } else {                                                                                   \n"
 
440
"      readtex0 = lodT_m1;                                                              \n"
 
441
"      readtex1 = lodT;                                                                 \n"
 
442
"    }                                                                                                  \n"
 
443
"  }                                                                                                    \n"
 
444
"  return lod_frac;                                                                             \n"
 
445
"}                                                                                                              \n"
413
446
;
414
447
 
415
448
static const char* fragment_shader_readtex =
416
 
"#version 330 core                                                                                                      \n"
 
449
#ifndef GLESX
 
450
SHADER_VERSION
 
451
#endif
417
452
"uniform lowp int uTextureFilterMode;                                                           \n"
418
 
"lowp vec4 filterNearest(in sampler2D tex, in mediump vec2 texCoord)\n"
419
 
"{                                                                                                                                      \n"
420
 
"  return textureLod(tex, texCoord, 0.0);                                                       \n"
421
 
"}                                                                                                                                      \n"
422
453
// 3 point texture filtering.
423
454
// Original author: ArthurCarvalho
424
455
// GLSL implementation: twinaphex, mupen64plus-libretro project.
425
 
"#define TEX_OFFSET(off) textureLod(tex, texCoord - (off)/texSize, 0.0) \n"
 
456
"#define TEX_OFFSET(off) texture(tex, texCoord - (off)/texSize) \n"
426
457
"lowp vec4 filter3point(in sampler2D tex, in mediump vec2 texCoord)                     \n"
427
458
"{                                                                                                                                                      \n"
428
459
"  mediump vec2 texSize = vec2(textureSize(tex,0));                                                     \n"
435
466
"}                                                                                                                                                      \n"
436
467
"lowp vec4 readTex(in sampler2D tex, in mediump vec2 texCoord, in bool fb8bit, in bool fbFixedAlpha)    \n"
437
468
"{                                                                                                                                                      \n"
438
 
"  lowp vec4 texColor = uTextureFilterMode == 0 ? filterNearest(tex, texCoord) : filter3point(tex, texCoord); \n"
439
 
"  if (fb8bit) texColor = vec4(texColor.r);                                                                     \n"
440
 
"  if (fbFixedAlpha) texColor.a = 0.825;                                                                        \n"
441
 
"  return texColor;                                                                                                                     \n"
442
 
"}                                                                                                                                                      \n"
 
469
"  lowp vec4 texColor = uTextureFilterMode == 0 ? texture(tex, texCoord) : filter3point(tex, texCoord); \n"
 
470
"  if (fb8bit) texColor = vec4(texColor.r);                                                                     \n"
 
471
"  if (fbFixedAlpha) texColor.a = 0.825;                                                                        \n"
 
472
"  return texColor;                                                                                                                     \n"
 
473
"}                                                                                                                                                      \n"
 
474
#ifdef GL_MULTISAMPLING_SUPPORT
 
475
"uniform lowp int uMSAASamples; \n"
 
476
"uniform lowp float uMSAAScale; \n"
 
477
"lowp vec4 sampleMS(in sampler2DMS mstex, in mediump ivec2 ipos)                        \n"
 
478
"{                                                                                                                                                      \n"
 
479
"  lowp vec4 texel = vec4(0.0);                                                                                         \n"
 
480
"  for (int i = 0; i < uMSAASamples; ++i)                                                                       \n"
 
481
"    texel += texelFetch(mstex, ipos, i);                                                                       \n"
 
482
"  return texel * uMSAAScale;                                                                                           \n"
 
483
"}                                                                                                                                                      \n"
 
484
"                                                                                                                                                       \n"
 
485
"lowp vec4 readTexMS(in sampler2DMS mstex, in mediump vec2 texCoord, in bool fb8bit, in bool fbFixedAlpha)      \n"
 
486
"{                                                                                                                                                      \n"
 
487
"  mediump vec2 msTexSize = vec2(textureSize(mstex));                                           \n"
 
488
"  mediump ivec2 itexCoord = ivec2(msTexSize * texCoord);                                       \n"
 
489
"  lowp vec4 texColor = sampleMS(mstex, itexCoord);                                                     \n"
 
490
"  if (fb8bit) texColor = vec4(texColor.r);                                                                     \n"
 
491
"  if (fbFixedAlpha) texColor.a = 0.825;                                                                        \n"
 
492
"  return texColor;                                                                                                                     \n"
 
493
"}                                                                                                                                                      \n"
 
494
#endif // GL_MULTISAMPLING_SUPPORT
443
495
;
444
496
 
445
497
static const char* fragment_shader_noise =
446
 
"#version 330 core                                      \n"
 
498
#ifndef GLESX
 
499
SHADER_VERSION
 
500
"uniform mediump vec2 uScreenScale;     \n"
 
501
#endif
447
502
"uniform sampler2D uTexNoise;           \n"
448
 
"uniform mediump vec2 uScreenScale;     \n"
449
503
"lowp float snoise()                                                                    \n"
450
504
"{                                                                                                              \n"
451
505
"  ivec2 coord = ivec2(gl_FragCoord.xy/uScreenScale);   \n"
463
517
 
464
518
#ifdef GL_IMAGE_TEXTURES_SUPPORT
465
519
static const char* depth_compare_shader_float =
 
520
#ifndef GLESX
466
521
"#version 430                                                           \n"
 
522
#else
 
523
"#ifdef GL_OES_standard_derivatives                     \n"
 
524
"    #extension GL_OES_standard_derivatives : enable \n"
 
525
"#endif                                                                         \n"
 
526
#endif
467
527
//"uniform int uEnableDepth;                            \n"
468
528
"uniform lowp int uDepthMode;                           \n"
469
529
"uniform lowp int uDepthSource;                         \n"
529
589
"#version 420 core                                                                                      \n"
530
590
"layout(binding = 0) uniform sampler2D uDepthImage;             \n"
531
591
"layout(binding = 0, r16ui) uniform readonly uimage2D uZlutImage;\n"
532
 
"layout(binding = 1, r16ui) uniform readonly uimage1D uTlutImage;\n"
 
592
"layout(binding = 1, r16ui) uniform readonly uimage2D uTlutImage;\n"
533
593
"uniform lowp vec4 uFogColor;                                                           \n"
534
594
"out lowp vec4 fragColor;                                                                       \n"
535
595
"lowp float get_alpha()                                                                         \n"
542
602
"  uint iN64z = imageLoad(uZlutImage,ivec2(x0,y0)).r;           \n"
543
603
"  highp float n64z = clamp(float(iN64z)/65532.0, 0.0, 1.0);\n"
544
604
"  int index = min(255, int(n64z*255.0));                                       \n"
545
 
"  uint iAlpha = imageLoad(uTlutImage,index).r;                 \n"
 
605
"  uint iAlpha = imageLoad(uTlutImage,ivec2(index,0)).r;                        \n"
546
606
"  return float(iAlpha/256)/255.0;                                              \n"
547
607
"}                                                                                                              \n"
548
608
"void main()                                                                                    \n"
552
612
;
553
613
#endif // GL_IMAGE_TEXTURES_SUPPORT
554
614
 
 
615
#ifdef GL_IMAGE_TEXTURES_SUPPORT
555
616
#if 0 // Do palette based monochrome image. Exactly as N64 does
556
 
#ifdef GL_IMAGE_TEXTURES_SUPPORT
557
617
static const char* zelda_monochrome_fragment_shader =
558
618
"#version 420 core                                                                                      \n"
559
619
"layout(binding = 0) uniform sampler2D uColorImage;                     \n"
560
 
"layout(binding = 1, r16ui) uniform readonly uimage1D uTlutImage;\n"
 
620
"layout(binding = 1, r16ui) uniform readonly uimage2D uTlutImage;\n"
561
621
"out lowp vec4 fragColor;                                                                       \n"
562
622
"lowp float get_color()                                                                         \n"
563
623
"{                                                                                                                      \n"
568
628
//"  int color16 = 32768 + r*1024 + g*32 + b;           \n"
569
629
"  int color16 = r*1024 + g*32 + b;                                             \n"
570
630
"  int index = min(255, color16/256);                                   \n"
571
 
"  uint iAlpha = imageLoad(uTlutImage,index).r;                 \n"
 
631
"  uint iAlpha = imageLoad(uTlutImage,ivec2(index,0)).r;                        \n"
572
632
"  memoryBarrier();                                                                             \n"
573
633
"  return clamp(float((iAlpha&255) + index)/255.0, 0.0, 1.0); \n"
574
634
"}                                                                                                              \n"
577
637
"  fragColor = vec4(vec3(get_color()), 1.0);                    \n"
578
638
"}                                                                                                              \n"
579
639
;
580
 
#endif // GL_IMAGE_TEXTURES_SUPPORT
581
640
#else // Cheat it
582
641
static const char* zelda_monochrome_fragment_shader =
583
642
"#version 420 core                                                                              \n"
593
652
"}                                                                                                              \n"
594
653
;
595
654
#endif
 
655
#endif // GL_IMAGE_TEXTURES_SUPPORT