1
0
mirror of https://github.com/blawar/GLideN64.git synced 2024-07-07 03:13:49 +00:00
GLideN64/Shaders.h
2015-05-13 10:11:25 +06:00

296 lines
11 KiB
C

static const char* vertex_shader =
"attribute vec4 aPosition; \n"
"attribute vec4 aColor; \n"
"attribute vec2 aTexCoord0; \n"
"attribute vec2 aTexCoord1; \n"
"attribute float aSTScaled; \n"
"attribute float aNumLights; \n"
" \n"
"uniform int uRenderState; \n"
"uniform int uTexturePersp; \n"
"uniform float uNoiseTime; \n"
"uniform float uFogMultiplier, uFogOffset; \n"
" \n"
"uniform vec2 uTexScale; \n"
"uniform vec2 uTexOffset[2]; \n"
"uniform vec2 uCacheShiftScale[2]; \n"
"uniform vec2 uCacheScale[2]; \n"
"uniform vec2 uCacheOffset[2]; \n"
"uniform ivec2 uCacheFrameBuffer; \n"
" \n"
"varying vec4 vShadeColor; \n"
"varying vec2 vTexCoord0; \n"
"varying vec2 vTexCoord1; \n"
"varying vec2 vLodTexCoord; \n"
"varying vec2 vNoiseCoord2D; \n"
"varying float vNumLights; \n"
"void main() \n"
"{ \n"
" gl_Position = aPosition; \n"
" vShadeColor = aColor; \n"
" if (uRenderState == 1.0) { \n"
" vec2 texCoord = aTexCoord0; \n"
" if (aSTScaled == 0.0) texCoord *= uTexScale; \n"
" if (uTexturePersp == 0) texCoord *= 0.5; \n"
" vec2 texCoord0 = texCoord*uCacheShiftScale[0] \n"
" - uTexOffset[0]; \n"
" if (uCacheFrameBuffer[0] != 0) \n"
" texCoord0.t = -texCoord0.t; \n"
" vTexCoord0 = (uCacheOffset[0] + texCoord0)* uCacheScale[0];\n"
" vec2 texCoord1 = texCoord*uCacheShiftScale[1] \n"
" - uTexOffset[1]; \n"
" if (uCacheFrameBuffer[1] != 0) \n"
" texCoord1.t = -texCoord1.t; \n"
" vTexCoord1 = (uCacheOffset[1] + texCoord1)* uCacheScale[1];\n"
" vLodTexCoord = texCoord * uCacheShiftScale[0]; \n"
" } else { \n"
" vTexCoord0 = aTexCoord0; \n"
" vTexCoord1 = aTexCoord1; \n"
" } \n"
" vNoiseCoord2D = gl_Vertex.xy + vec2(0.0, uNoiseTime); \n"
" gl_FogFragCoord = max(-1.0, aPosition.z / aPosition.w) \n"
" * uFogMultiplier + uFogOffset; \n"
" gl_FogFragCoord = clamp(gl_FogFragCoord, 0.0, 1.0); \n"
" vNumLights = aNumLights; \n"
"} \n"
;
static const char* fragment_shader_header_common_variables =
"uniform sampler2D uTex0; \n"
"uniform sampler2D uTex1; \n"
"uniform vec4 uPrimColor; \n"
"uniform vec4 uEnvColor; \n"
"uniform vec4 uCenterColor; \n"
"uniform vec4 uScaleColor; \n"
"uniform vec4 uFogColor; \n"
"uniform float uK4; \n"
"uniform float uK5; \n"
"uniform float uPrimLod; \n"
"uniform int uEnableDither; \n"
"uniform int uEnableFog; \n"
"uniform int uFb8Bit; \n"
"uniform int uFbFixedAlpha; \n"
"varying vec4 vShadeColor; \n"
"varying vec2 vTexCoord0; \n"
"varying vec2 vTexCoord1; \n"
"varying vec2 vLodTexCoord; \n"
"varying vec2 vNoiseCoord2D; \n"
"varying float vNumLights; \n"
"vec3 input_color; \n"
;
static const char* fragment_shader_header_common_functions =
" \n"
"float snoise(vec2 v); \n"
"float calc_light(in int nLights, in vec3 input_color, out vec3 output_color);\n"
"float calc_lod(in float primLod, in vec2 texCoord); \n"
"bool depth_compare(); \n"
"bool alpha_test(in float alphaValue); \n"
#ifdef USE_TOONIFY
"void toonify(in float intensity); \n"
#endif
;
static const char* fragment_shader_calc_light =
" \n"
"float calc_light(in int nLights, in vec3 input_color, out vec3 output_color) {\n"
" output_color = input_color; \n"
" if (nLights == 0) \n"
" return 1.0; \n"
" float full_intensity = 0.0; \n"
" output_color = vec3(gl_LightSource[nLights].ambient); \n"
" vec3 lightDir, lightColor; \n"
" float intensity; \n"
" vec3 n = normalize(input_color); \n"
" for (int i = 0; i < nLights; i++) { \n"
" lightDir = vec3(gl_LightSource[i].position); \n"
" intensity = max(dot(n,lightDir),0.0); \n"
" full_intensity += intensity; \n"
" lightColor = vec3(gl_LightSource[i].ambient)*intensity; \n"
" output_color += lightColor; \n"
" }; \n"
" return full_intensity; \n"
"} \n"
;
static const char* fragment_shader_calc_lod =
"uniform int uEnableLod; \n"
"uniform float uLodXScale; \n"
"uniform float uLodYScale; \n"
"uniform float uMinLod; \n"
"uniform int uMaxTile; \n"
"uniform int uTextureDetail; \n"
" \n"
"float calc_lod(in float primLod, in vec2 texCoord) { \n"
" if (uEnableLod == 0) \n"
" return primLod; \n"
" vec2 dx = dFdx(texCoord); \n"
" dx.x *= uLodXScale; \n"
" dx.y *= uLodYScale; \n"
" vec2 dy = dFdy(texCoord); \n"
" dy.x *= uLodXScale; \n"
" dy.y *= uLodYScale; \n"
" float lod = max(length(dx), length(dy)); \n"
" float lod_frac; \n"
" if (lod < 1.0) { \n"
" lod_frac = max(lod, uMinLod); \n"
" if (uTextureDetail == 1) \n"
" lod_frac = 1.0 - lod_frac; \n"
" } else { \n"
" float tile = min(float(uMaxTile), floor(log2(floor(lod)))); \n"
" lod_frac = max(uMinLod, fract(lod/pow(2.0, tile)));\n"
" } \n"
" return lod_frac; \n"
"} \n"
;
static const char* fragment_shader_header_main =
" \n"
"void main() \n"
"{ \n"
//" if (uEnableDither != 0) { \n"
//" if (snoise(vNoiseCoord2D) < 0.0) discard; \n"
//" } \n"
" vec4 vec_color, combined_color; \n"
" float alpha1, alpha2; \n"
" vec3 color1, color2; \n"
;
#ifdef USE_TOONIFY
static const char* fragment_shader_toonify =
" \n"
"void toonify(in float intensity) { \n"
" if (intensity > 0.5) \n"
" return; \n"
" else if (intensity > 0.125) \n"
" gl_FragColor = vec4(vec3(gl_FragColor)*0.5, gl_FragColor.a);\n"
" else \n"
" gl_FragColor = vec4(vec3(gl_FragColor)*0.2, gl_FragColor.a);\n"
"} \n"
;
#endif
static const char* fragment_shader_default =
//" gl_FragColor = texture2D(uTex0, gl_TexCoord[0].st); \n"
//" gl_FragColor = gl_Color; \n"
" vec4 color = texture2D(uTex0, vTexCoord0); \n"
" gl_FragColor = vShadeColor*color; \n"
;
static const char* fragment_shader_readtex0color =
" vec4 readtex0 = texture2D(uTex0, vTexCoord0); \n"
" if (uFb8Bit == 1 || uFb8Bit == 3) readtex0 = vec4(readtex0.r); \n"
" if (uFbFixedAlpha == 1 || uFbFixedAlpha == 3) readtex0.a = 0.825; \n"
;
static const char* fragment_shader_readtex1color =
" vec4 readtex1 = texture2D(uTex1, vTexCoord1); \n"
" if (uFb8Bit == 2 || uFb8Bit == 3) readtex1 = vec4(readtex1.r); \n"
" if (uFbFixedAlpha == 2 || uFbFixedAlpha == 3) readtex1.a = 0.825; \n"
;
static const char* fragment_shader_end =
"} \n"
;
static const char* depth_compare_shader_float =
"#version 420 core \n"
"uniform int uEnableDepth; \n"
"uniform int uDepthMode; \n"
"uniform int uEnableDepthCompare; \n"
"uniform int uEnableDepthUpdate; \n"
"uniform float uDepthTrans; \n"
"uniform float uDepthScale; \n"
"layout(binding = 0, r16ui) uniform readonly uimage2D uZlutImage;\n"
"layout(binding = 2, rgba32f) uniform restrict image2D uDepthImage;\n"
"void write_depth(in highp float dz, in ivec2 coord) \n"
"{ \n"
" highp float fZ = 2.0*gl_FragCoord.z - 1.0; \n"
" fZ = (uDepthScale*fZ + uDepthTrans)*8.0; \n"
" const highp int iZ = int(floor(fZ + 0.5)); \n"
" int y0 = clamp(iZ/512, 0, 511); \n"
" int x0 = iZ - 512*y0; \n"
" unsigned int iN64z = imageLoad(uZlutImage,ivec2(x0,y0)).r;\n"
" highp float n64z = clamp(float(iN64z)/65532.0, 0.0, 1.0);\n"
" highp vec4 depth = vec4(n64z, gl_FragCoord.z, dz, 1.0); \n"
" memoryBarrier(); \n"
" imageStore(uDepthImage,coord,depth); \n"
"} \n"
"bool depth_compare() \n"
"{ \n"
" if (uEnableDepth == 0) return true; \n"
" ivec2 coord = ivec2(gl_FragCoord.xy); \n"
" memoryBarrier(); \n"
" highp vec4 depth = imageLoad(uDepthImage,coord); \n"
" highp float bufZ = depth.g; \n"
" highp float curZ = gl_FragCoord.z; \n"
" highp float dz = fwidth(gl_FragCoord.z); \n"
" highp float dzMax = max(dz, depth.b); \n"
" const bool bInfront = curZ < bufZ; \n"
" const bool bFarther = (curZ + dzMax) >= bufZ; \n"
" const bool bNearer = (curZ - dzMax) <= bufZ; \n"
" const bool bMax = bufZ == 1.0; \n"
" bool bRes; \n"
" switch(uDepthMode) { \n"
" case 0: \n"
" case 1: \n"
" bRes = bMax || bNearer; \n"
" break; \n"
" case 2: \n"
" bRes = bMax || bInfront; \n"
" break; \n"
" case 3: \n"
" bRes = bFarther && bNearer && !bMax; \n"
" break; \n"
" default: \n"
" bRes = bInfront; \n"
" break; \n"
" } \n"
" if (uEnableDepthUpdate > 0 && bRes) { \n"
" write_depth(dz, coord); \n"
" } \n"
" if (uEnableDepthCompare > 0) \n"
" return bRes; \n"
" return true; \n"
"} \n"
;
static const char* alpha_test_fragment_shader =
"uniform int uEnableAlphaTest; \n"
"uniform float uAlphaTestValue; \n"
"bool alpha_test(in float alphaValue) \n"
"{ \n"
" if (uEnableAlphaTest == 0) return true; \n"
" if (uAlphaTestValue > 0.0) return alphaValue >= uAlphaTestValue;\n"
" return alphaValue > 0.0; \n"
"} \n"
;
static const char* shadow_map_vertex_shader =
"#version 420 core \n"
"attribute highp vec4 aPosition; \n"
"void main() \n"
"{ \n"
" gl_Position = aPosition; \n"
"} \n"
;
static const char* shadow_map_fragment_shader_float =
"#version 420 core \n"
"layout(binding = 1, r16ui) uniform readonly uimage1D uTlutImage;\n"
"layout(binding = 2, rgba32f) uniform readonly image2D uDepthImage;\n"
"float get_alpha() \n"
"{ \n"
" ivec2 coord = ivec2(gl_FragCoord.xy); \n"
" float bufZ = imageLoad(uDepthImage,coord).r; \n"
" int index = min(255, int(bufZ*255.0)); \n"
" unsigned int iAlpha = imageLoad(uTlutImage,index).r; \n"
" memoryBarrier(); \n"
" return float(iAlpha/256)/255.0; \n"
"} \n"
"void main() \n"
"{ \n"
" gl_FragColor = vec4(gl_Fog.color.rgb, get_alpha()); \n"
"} \n"
;