2015-04-17 18:18:07 +00:00
|
|
|
//#version 420 // Keep it for text editor detection
|
|
|
|
|
2015-07-18 09:22:08 +00:00
|
|
|
// Require for bit operation
|
|
|
|
//#extension GL_ARB_gpu_shader5 : enable
|
2015-04-17 18:18:07 +00:00
|
|
|
|
|
|
|
#define FMT_32 0
|
|
|
|
#define FMT_24 1
|
|
|
|
#define FMT_16 2
|
|
|
|
#define FMT_PAL 4 /* flag bit */
|
|
|
|
|
|
|
|
// APITRACE_DEBUG allows to force pixel output to easily detect
|
|
|
|
// the fragment computed by primitive
|
|
|
|
#define APITRACE_DEBUG 0
|
|
|
|
// TEX_COORD_DEBUG output the uv coordinate as color. It is useful
|
|
|
|
// to detect bad sampling due to upscaling
|
|
|
|
//#define TEX_COORD_DEBUG
|
2015-07-17 18:57:32 +00:00
|
|
|
// Just copy directly the texture coordinate
|
|
|
|
#ifdef TEX_COORD_DEBUG
|
|
|
|
#define PS_TFX 1
|
|
|
|
#define PS_TCC 1
|
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
|
|
|
|
// Not sure we have same issue on opengl. Doesn't work anyway on ATI card
|
|
|
|
// And I say this as an ATI user.
|
|
|
|
#define ATI_SUCKS 0
|
|
|
|
|
2015-07-13 13:19:33 +00:00
|
|
|
#define SW_BLEND (PS_BLEND_A || PS_BLEND_B || PS_BLEND_D)
|
|
|
|
|
2015-04-17 18:18:07 +00:00
|
|
|
#ifdef FRAGMENT_SHADER
|
|
|
|
|
|
|
|
in SHADER
|
|
|
|
{
|
2015-04-25 10:46:59 +00:00
|
|
|
vec4 t;
|
|
|
|
vec4 c;
|
|
|
|
flat vec4 fc;
|
2015-04-17 18:18:07 +00:00
|
|
|
} PSin;
|
|
|
|
|
|
|
|
#define PSin_t (PSin.t)
|
|
|
|
#define PSin_c (PSin.c)
|
|
|
|
#define PSin_fc (PSin.fc)
|
|
|
|
|
|
|
|
// Same buffer but 2 colors for dual source blending
|
|
|
|
layout(location = 0, index = 0) out vec4 SV_Target0;
|
|
|
|
layout(location = 0, index = 1) out vec4 SV_Target1;
|
|
|
|
|
|
|
|
layout(binding = 0) uniform sampler2D TextureSampler;
|
|
|
|
layout(binding = 1) uniform sampler2D PaletteSampler;
|
2015-04-24 18:13:38 +00:00
|
|
|
layout(binding = 3) uniform sampler2D RtSampler; // note 2 already use by the image below
|
2015-04-17 18:18:07 +00:00
|
|
|
|
|
|
|
#ifndef DISABLE_GL42_image
|
|
|
|
#if PS_DATE > 0
|
|
|
|
// FIXME how to declare memory access
|
|
|
|
layout(r32i, binding = 2) coherent uniform iimage2D img_prim_min;
|
2015-06-26 18:03:15 +00:00
|
|
|
// Don't enable it. Discard fragment can still write in the depth buffer
|
|
|
|
// it breaks shadow in Shin Megami Tensei Nocturne
|
|
|
|
//layout(early_fragment_tests) in;
|
|
|
|
|
2015-05-01 18:04:23 +00:00
|
|
|
// I don't remember why I set this parameter but it is surely useless
|
|
|
|
//layout(pixel_center_integer) in vec4 gl_FragCoord;
|
2015-04-17 18:18:07 +00:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
// use basic stencil
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
layout(std140, binding = 21) uniform cb21
|
|
|
|
{
|
2015-04-25 10:46:59 +00:00
|
|
|
vec3 FogColor;
|
|
|
|
float AREF;
|
|
|
|
vec4 WH;
|
|
|
|
vec2 MinF;
|
|
|
|
vec2 TA;
|
|
|
|
uvec4 MskFix;
|
2015-06-21 06:47:45 +00:00
|
|
|
uvec4 FbMask;
|
|
|
|
vec3 _not_yet_used;
|
2015-06-17 20:55:17 +00:00
|
|
|
float Af;
|
2015-04-25 10:46:59 +00:00
|
|
|
vec4 HalfTexel;
|
|
|
|
vec4 MinMax;
|
2015-06-21 06:47:45 +00:00
|
|
|
vec2 TC_OffsetHack;
|
2015-04-17 18:18:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
vec4 sample_c(vec2 uv)
|
|
|
|
{
|
2015-04-25 10:46:59 +00:00
|
|
|
// FIXME: check the issue on openGL
|
2015-04-24 15:51:29 +00:00
|
|
|
#if (ATI_SUCKS == 1) && (PS_POINT_SAMPLER == 1)
|
2015-04-25 10:46:59 +00:00
|
|
|
// Weird issue with ATI cards (happens on at least HD 4xxx and 5xxx),
|
|
|
|
// it looks like they add 127/128 of a texel to sampling coordinates
|
|
|
|
// occasionally causing point sampling to erroneously round up.
|
|
|
|
// I'm manually adjusting coordinates to the centre of texels here,
|
|
|
|
// though the centre is just paranoia, the top left corner works fine.
|
|
|
|
uv = (trunc(uv * WH.zw) + vec2(0.5, 0.5)) / WH.zw;
|
2015-04-24 15:51:29 +00:00
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-25 10:46:59 +00:00
|
|
|
return texture(TextureSampler, uv);
|
2015-04-17 18:18:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-24 14:50:01 +00:00
|
|
|
vec4 sample_p(uint idx)
|
2015-04-17 18:18:07 +00:00
|
|
|
{
|
2015-05-24 14:50:01 +00:00
|
|
|
return texelFetch(PaletteSampler, ivec2(idx, 0u), 0);
|
2015-04-17 18:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vec4 wrapuv(vec4 uv)
|
|
|
|
{
|
2015-04-25 10:46:59 +00:00
|
|
|
vec4 uv_out = uv;
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-24 15:51:29 +00:00
|
|
|
#if PS_WMS == PS_WMT
|
|
|
|
|
|
|
|
#if PS_WMS == 2
|
2015-04-25 10:46:59 +00:00
|
|
|
uv_out = clamp(uv, MinMax.xyxy, MinMax.zwzw);
|
2015-04-24 15:51:29 +00:00
|
|
|
#elif PS_WMS == 3
|
2015-04-25 10:46:59 +00:00
|
|
|
uv_out = vec4((ivec4(uv * WH.xyxy) & ivec4(MskFix.xyxy)) | ivec4(MskFix.zwzw)) / WH.xyxy;
|
2015-04-24 15:51:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#else // PS_WMS != PS_WMT
|
|
|
|
|
|
|
|
#if PS_WMS == 2
|
2015-04-25 10:46:59 +00:00
|
|
|
uv_out.xz = clamp(uv.xz, MinMax.xx, MinMax.zz);
|
2015-04-24 15:51:29 +00:00
|
|
|
|
|
|
|
#elif PS_WMS == 3
|
2015-04-25 10:46:59 +00:00
|
|
|
uv_out.xz = vec2((ivec2(uv.xz * WH.xx) & ivec2(MskFix.xx)) | ivec2(MskFix.zz)) / WH.xx;
|
2015-04-24 15:51:29 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if PS_WMT == 2
|
2015-04-25 10:46:59 +00:00
|
|
|
uv_out.yw = clamp(uv.yw, MinMax.yy, MinMax.ww);
|
2015-04-24 15:51:29 +00:00
|
|
|
|
|
|
|
#elif PS_WMT == 3
|
|
|
|
|
2015-04-25 10:46:59 +00:00
|
|
|
uv_out.yw = vec2((ivec2(uv.yw * WH.yy) & ivec2(MskFix.yy)) | ivec2(MskFix.ww)) / WH.yy;
|
2015-04-24 15:51:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-25 10:46:59 +00:00
|
|
|
return uv_out;
|
2015-04-17 18:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vec2 clampuv(vec2 uv)
|
|
|
|
{
|
2015-04-25 10:46:59 +00:00
|
|
|
vec2 uv_out = uv;
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-25 10:46:59 +00:00
|
|
|
#if (PS_WMS == 2) && (PS_WMT == 2)
|
|
|
|
uv_out = clamp(uv, MinF, MinMax.zw);
|
2015-04-24 15:51:29 +00:00
|
|
|
#elif PS_WMS == 2
|
2015-04-25 10:46:59 +00:00
|
|
|
uv_out.x = clamp(uv.x, MinF.x, MinMax.z);
|
2015-04-24 15:51:29 +00:00
|
|
|
#elif PS_WMT == 2
|
2015-04-25 10:46:59 +00:00
|
|
|
uv_out.y = clamp(uv.y, MinF.y, MinMax.w);
|
2015-04-24 15:51:29 +00:00
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-25 10:46:59 +00:00
|
|
|
return uv_out;
|
2015-04-17 18:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mat4 sample_4c(vec4 uv)
|
|
|
|
{
|
2015-04-25 10:46:59 +00:00
|
|
|
mat4 c;
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-07-18 09:22:08 +00:00
|
|
|
// Note: texture gather can't be used because of special clamping/wrapping
|
|
|
|
// Also it doesn't support lod
|
2015-04-25 10:46:59 +00:00
|
|
|
c[0] = sample_c(uv.xy);
|
|
|
|
c[1] = sample_c(uv.zy);
|
|
|
|
c[2] = sample_c(uv.xw);
|
|
|
|
c[3] = sample_c(uv.zw);
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-25 10:46:59 +00:00
|
|
|
return c;
|
2015-04-17 18:18:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-24 14:50:01 +00:00
|
|
|
uvec4 sample_4_index(vec4 uv)
|
2015-04-17 18:18:07 +00:00
|
|
|
{
|
2015-04-25 10:46:59 +00:00
|
|
|
vec4 c;
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-05-24 14:50:01 +00:00
|
|
|
// Either GSdx will send a texture that contains a single channel
|
2015-06-14 08:02:30 +00:00
|
|
|
// in this case the red channel is remapped as alpha channel
|
2015-05-24 14:50:01 +00:00
|
|
|
//
|
|
|
|
// Or we have an old RT (ie RGBA8) that contains index (4/8) in the alpha channel
|
|
|
|
|
2015-07-18 09:22:08 +00:00
|
|
|
// Note: texture gather can't be used because of special clamping/wrapping
|
|
|
|
// Also it doesn't support lod
|
2015-05-24 14:50:01 +00:00
|
|
|
c.x = sample_c(uv.xy).a;
|
|
|
|
c.y = sample_c(uv.zy).a;
|
|
|
|
c.z = sample_c(uv.xw).a;
|
|
|
|
c.w = sample_c(uv.zw).a;
|
|
|
|
|
|
|
|
uvec4 i = uvec4(c * 255.0f + 0.5f); // Denormalize value
|
|
|
|
|
|
|
|
#if PS_IFMT == 1
|
2015-06-14 08:02:30 +00:00
|
|
|
// 4HH
|
2015-05-24 14:50:01 +00:00
|
|
|
return i >> 4u;
|
|
|
|
#elif PS_IFMT == 2
|
2015-06-14 08:02:30 +00:00
|
|
|
// 4HL
|
2015-07-17 17:59:32 +00:00
|
|
|
return i & 0xFu;
|
2015-05-24 14:50:01 +00:00
|
|
|
#else
|
2015-06-14 08:02:30 +00:00
|
|
|
// 8 bits
|
2015-05-24 14:50:01 +00:00
|
|
|
return i;
|
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-24 14:50:01 +00:00
|
|
|
mat4 sample_4p(uvec4 u)
|
2015-04-17 18:18:07 +00:00
|
|
|
{
|
2015-04-25 10:46:59 +00:00
|
|
|
mat4 c;
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-25 10:46:59 +00:00
|
|
|
c[0] = sample_p(u.x);
|
|
|
|
c[1] = sample_p(u.y);
|
|
|
|
c[2] = sample_p(u.z);
|
|
|
|
c[3] = sample_p(u.w);
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-25 10:46:59 +00:00
|
|
|
return c;
|
2015-04-17 18:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vec4 sample_color(vec2 st, float q)
|
|
|
|
{
|
2015-07-17 15:53:00 +00:00
|
|
|
//FIXME: maybe we can set gl_Position.w = q in VS
|
2015-04-24 15:51:29 +00:00
|
|
|
#if (PS_FST == 0)
|
2015-04-25 10:46:59 +00:00
|
|
|
st /= q;
|
2015-04-24 15:51:29 +00:00
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-24 15:51:29 +00:00
|
|
|
#if (PS_TCOFFSETHACK == 1)
|
2015-04-25 10:46:59 +00:00
|
|
|
st += TC_OffsetHack.xy;
|
2015-04-24 15:51:29 +00:00
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-25 10:46:59 +00:00
|
|
|
vec4 t;
|
|
|
|
mat4 c;
|
|
|
|
vec2 dd;
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-24 15:51:29 +00:00
|
|
|
#if (PS_LTF == 0 && PS_FMT <= FMT_16 && PS_WMS < 3 && PS_WMT < 3)
|
2015-04-25 10:46:59 +00:00
|
|
|
c[0] = sample_c(clampuv(st));
|
2015-04-17 18:18:07 +00:00
|
|
|
#ifdef TEX_COORD_DEBUG
|
2015-04-25 10:46:59 +00:00
|
|
|
c[0].rg = clampuv(st).xy;
|
2015-04-17 18:18:07 +00:00
|
|
|
#endif
|
2015-04-24 15:51:29 +00:00
|
|
|
|
|
|
|
#else
|
2015-04-25 10:46:59 +00:00
|
|
|
vec4 uv;
|
|
|
|
|
|
|
|
if(PS_LTF != 0)
|
|
|
|
{
|
|
|
|
uv = st.xyxy + HalfTexel;
|
|
|
|
dd = fract(uv.xy * WH.zw);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uv = st.xyxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
uv = wrapuv(uv);
|
|
|
|
|
|
|
|
if((PS_FMT & FMT_PAL) != 0)
|
|
|
|
{
|
2015-05-24 14:50:01 +00:00
|
|
|
c = sample_4p(sample_4_index(uv));
|
2015-04-25 10:46:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c = sample_4c(uv);
|
|
|
|
}
|
2015-04-17 18:18:07 +00:00
|
|
|
#ifdef TEX_COORD_DEBUG
|
2015-04-25 10:46:59 +00:00
|
|
|
c[0].rg = uv.xy;
|
|
|
|
c[1].rg = uv.xy;
|
|
|
|
c[2].rg = uv.xy;
|
|
|
|
c[3].rg = uv.xy;
|
2015-04-17 18:18:07 +00:00
|
|
|
#endif
|
2015-04-24 15:51:29 +00:00
|
|
|
|
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-25 10:46:59 +00:00
|
|
|
// PERF: see the impact of the exansion before/after the interpolation
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2015-08-07 12:04:54 +00:00
|
|
|
// PERF note: using dot product reduces by 1 the number of instruction
|
|
|
|
// but I'm not sure it is equivalent neither faster.
|
2015-07-18 09:22:08 +00:00
|
|
|
//float sum = dot(c[i].rgb, vec3(1.0f));
|
2015-04-24 15:51:29 +00:00
|
|
|
#if ((PS_FMT & ~FMT_PAL) == FMT_24)
|
2015-04-25 10:46:59 +00:00
|
|
|
c[i].a = ( (PS_AEM == 0) || any(bvec3(c[i].rgb)) ) ? TA.x : 0.0f;
|
2015-07-18 09:22:08 +00:00
|
|
|
//c[i].a = ( (PS_AEM == 0) || (sum > 0.0f) ) ? TA.x : 0.0f;
|
2015-04-24 15:51:29 +00:00
|
|
|
#elif ((PS_FMT & ~FMT_PAL) == FMT_16)
|
2015-04-25 10:46:59 +00:00
|
|
|
c[i].a = c[i].a >= 0.5 ? TA.y : ( (PS_AEM == 0) || any(bvec3(c[i].rgb)) ) ? TA.x : 0.0f;
|
2015-07-18 09:22:08 +00:00
|
|
|
//c[i].a = c[i].a >= 0.5 ? TA.y : ( (PS_AEM == 0) || (sum > 0.0f) ) ? TA.x : 0.0f;
|
2015-04-24 15:51:29 +00:00
|
|
|
#endif
|
2015-04-25 10:46:59 +00:00
|
|
|
}
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-24 15:51:29 +00:00
|
|
|
#if(PS_LTF != 0)
|
2015-04-25 10:46:59 +00:00
|
|
|
t = mix(mix(c[0], c[1], dd.x), mix(c[2], c[3], dd.x), dd.y);
|
2015-04-24 15:51:29 +00:00
|
|
|
#else
|
2015-04-25 10:46:59 +00:00
|
|
|
t = c[0];
|
2015-04-24 15:51:29 +00:00
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-07-19 19:06:08 +00:00
|
|
|
// The 0.05f helps to fix the overbloom of sotc
|
|
|
|
// I think the issue is related to the rounding of texture coodinate. The linear (from fixed unit)
|
|
|
|
// interpolation could be slightly below the correct one.
|
|
|
|
return trunc(t * 255.0f + 0.05f);
|
2015-04-17 18:18:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 11:40:10 +00:00
|
|
|
vec4 tfx(vec4 T, vec4 C)
|
2015-04-17 18:18:07 +00:00
|
|
|
{
|
2015-07-18 11:40:10 +00:00
|
|
|
vec4 C_out;
|
|
|
|
vec4 FxT = trunc(trunc(C) * T / 128.0f);
|
2015-07-17 18:05:31 +00:00
|
|
|
|
2015-04-24 15:51:29 +00:00
|
|
|
#if (PS_TFX == 0)
|
2015-07-18 11:40:10 +00:00
|
|
|
C_out = FxT;
|
2015-04-24 15:51:29 +00:00
|
|
|
#elif (PS_TFX == 1)
|
2015-07-18 11:40:10 +00:00
|
|
|
C_out = T;
|
2015-04-24 15:51:29 +00:00
|
|
|
#elif (PS_TFX == 2)
|
2015-07-18 11:40:10 +00:00
|
|
|
C_out.rgb = FxT.rgb + C.a;
|
|
|
|
C_out.a = T.a + C.a;
|
2015-04-24 15:51:29 +00:00
|
|
|
#elif (PS_TFX == 3)
|
2015-07-18 11:40:10 +00:00
|
|
|
C_out.rgb = FxT.rgb + C.a;
|
|
|
|
C_out.a = T.a;
|
2015-07-17 18:05:31 +00:00
|
|
|
#else
|
2015-07-18 11:40:10 +00:00
|
|
|
C_out = C;
|
2015-07-17 18:05:31 +00:00
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-07-17 18:05:31 +00:00
|
|
|
#if (PS_TCC == 0)
|
2015-07-18 11:40:10 +00:00
|
|
|
C_out.a = C.a;
|
2015-04-24 15:51:29 +00:00
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-07-18 11:40:10 +00:00
|
|
|
#if (PS_TFX == 0) || (PS_TFX == 2) || (PS_TFX == 3)
|
|
|
|
// Clamp only when it is useful
|
|
|
|
C_out = min(C_out, 255.0f);
|
|
|
|
#endif
|
2015-07-17 18:57:32 +00:00
|
|
|
|
2015-07-18 11:40:10 +00:00
|
|
|
return C_out;
|
2015-04-17 18:18:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 11:40:10 +00:00
|
|
|
void atst(vec4 C)
|
2015-04-17 18:18:07 +00:00
|
|
|
{
|
2015-07-18 11:40:10 +00:00
|
|
|
// FIXME use integer cmp
|
|
|
|
float a = C.a;
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-24 15:51:29 +00:00
|
|
|
#if (PS_ATST == 0) // never
|
2015-04-25 10:46:59 +00:00
|
|
|
discard;
|
2015-04-24 15:51:29 +00:00
|
|
|
#elif (PS_ATST == 1) // always
|
2015-04-25 10:46:59 +00:00
|
|
|
// nothing to do
|
2015-05-08 08:57:21 +00:00
|
|
|
#elif (PS_ATST == 2) // l
|
2015-04-25 10:46:59 +00:00
|
|
|
if ((AREF - a - 0.5f) < 0.0f)
|
|
|
|
discard;
|
2015-04-24 15:51:29 +00:00
|
|
|
#elif (PS_ATST == 3 ) // le
|
2015-04-25 10:46:59 +00:00
|
|
|
if ((AREF - a + 0.5f) < 0.0f)
|
|
|
|
discard;
|
2015-04-24 15:51:29 +00:00
|
|
|
#elif (PS_ATST == 4) // e
|
2015-04-25 10:46:59 +00:00
|
|
|
if ((0.5f - abs(a - AREF)) < 0.0f)
|
|
|
|
discard;
|
2015-04-24 15:51:29 +00:00
|
|
|
#elif (PS_ATST == 5) // ge
|
2015-04-25 10:46:59 +00:00
|
|
|
if ((a-AREF + 0.5f) < 0.0f)
|
|
|
|
discard;
|
2015-04-24 15:51:29 +00:00
|
|
|
#elif (PS_ATST == 6) // g
|
2015-04-25 10:46:59 +00:00
|
|
|
if ((a-AREF - 0.5f) < 0.0f)
|
|
|
|
discard;
|
2015-04-24 15:51:29 +00:00
|
|
|
#elif (PS_ATST == 7) // ne
|
2015-04-25 10:46:59 +00:00
|
|
|
if ((abs(a - AREF) - 0.5f) < 0.0f)
|
|
|
|
discard;
|
2015-04-24 15:51:29 +00:00
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 11:40:10 +00:00
|
|
|
void fog(inout vec4 C, float f)
|
2015-04-17 18:18:07 +00:00
|
|
|
{
|
2015-04-24 15:51:29 +00:00
|
|
|
#if PS_FOG != 0
|
2015-07-18 11:40:10 +00:00
|
|
|
C.rgb = trunc(mix(FogColor, C.rgb, f));
|
2015-04-24 15:51:29 +00:00
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vec4 ps_color()
|
|
|
|
{
|
2015-07-18 11:40:10 +00:00
|
|
|
vec4 T = sample_color(PSin_t.xy, PSin_t.w);
|
2015-04-17 18:18:07 +00:00
|
|
|
|
|
|
|
#if PS_IIP == 1
|
2015-07-18 11:40:10 +00:00
|
|
|
vec4 C = tfx(T, PSin_c);
|
2015-04-17 18:18:07 +00:00
|
|
|
#else
|
2015-07-18 11:40:10 +00:00
|
|
|
vec4 C = tfx(T, PSin_fc);
|
2015-04-17 18:18:07 +00:00
|
|
|
#endif
|
|
|
|
|
2015-07-18 11:40:10 +00:00
|
|
|
atst(C);
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-07-18 11:40:10 +00:00
|
|
|
fog(C, PSin_t.z);
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-24 15:51:29 +00:00
|
|
|
#if (PS_CLR1 != 0) // needed for Cd * (As/Ad/F + 1) blending modes
|
2015-07-18 11:40:10 +00:00
|
|
|
C.rgb = vec3(255.0f);
|
2015-04-24 15:51:29 +00:00
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-07-18 11:40:10 +00:00
|
|
|
return C;
|
2015-04-17 18:18:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 11:40:10 +00:00
|
|
|
void ps_fbmask(inout vec4 C)
|
2015-06-21 06:47:45 +00:00
|
|
|
{
|
|
|
|
// FIXME do I need special case for 16 bits
|
|
|
|
#if PS_FBMASK
|
2015-07-18 11:40:10 +00:00
|
|
|
vec4 RT = trunc(texelFetch(RtSampler, ivec2(gl_FragCoord.xy), 0) * 255.0f + 0.1f);
|
2015-07-19 21:55:27 +00:00
|
|
|
C = vec4((uvec4(C) & ~FbMask) | (uvec4(RT) & FbMask));
|
2015-06-21 06:47:45 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-07-18 11:40:10 +00:00
|
|
|
void ps_blend(inout vec4 Color, float As)
|
2015-05-08 18:27:13 +00:00
|
|
|
{
|
2015-07-13 13:19:33 +00:00
|
|
|
#if SW_BLEND
|
2015-07-18 11:40:10 +00:00
|
|
|
vec4 RT = trunc(texelFetch(RtSampler, ivec2(gl_FragCoord.xy), 0) * 255.0f + 0.1f);
|
|
|
|
|
2015-05-26 14:16:36 +00:00
|
|
|
#if PS_DFMT == FMT_24
|
|
|
|
float Ad = 1.0f;
|
|
|
|
#else
|
|
|
|
// FIXME FMT_16 case
|
2015-05-08 18:27:13 +00:00
|
|
|
// FIXME Ad or Ad * 2?
|
2015-07-18 11:40:10 +00:00
|
|
|
float Ad = RT.a / 128.0f;
|
2015-05-26 14:16:36 +00:00
|
|
|
#endif
|
2015-07-18 11:40:10 +00:00
|
|
|
|
2015-05-20 07:07:01 +00:00
|
|
|
// Let the compiler do its jobs !
|
2015-07-18 11:40:10 +00:00
|
|
|
vec3 Cd = RT.rgb;
|
|
|
|
vec3 Cs = Color.rgb;
|
2015-05-20 07:07:01 +00:00
|
|
|
|
2015-07-13 13:19:33 +00:00
|
|
|
#if PS_BLEND_A == 0
|
|
|
|
vec3 A = Cs;
|
|
|
|
#elif PS_BLEND_A == 1
|
|
|
|
vec3 A = Cd;
|
|
|
|
#else
|
|
|
|
vec3 A = vec3(0.0f);
|
|
|
|
#endif
|
2015-05-20 07:07:01 +00:00
|
|
|
|
2015-07-13 13:19:33 +00:00
|
|
|
#if PS_BLEND_B == 0
|
|
|
|
vec3 B = Cs;
|
|
|
|
#elif PS_BLEND_B == 1
|
|
|
|
vec3 B = Cd;
|
|
|
|
#else
|
|
|
|
vec3 B = vec3(0.0f);
|
|
|
|
#endif
|
2015-05-20 07:07:01 +00:00
|
|
|
|
2015-07-13 13:19:33 +00:00
|
|
|
#if PS_BLEND_C == 0
|
|
|
|
float C = As;
|
|
|
|
#elif PS_BLEND_C == 1
|
|
|
|
float C = Ad;
|
|
|
|
#else
|
|
|
|
float C = Af;
|
|
|
|
#endif
|
2015-05-20 07:07:01 +00:00
|
|
|
|
2015-07-13 13:19:33 +00:00
|
|
|
#if PS_BLEND_D == 0
|
|
|
|
vec3 D = Cs;
|
|
|
|
#elif PS_BLEND_D == 1
|
|
|
|
vec3 D = Cd;
|
|
|
|
#else
|
|
|
|
vec3 D = vec3(0.0f);
|
|
|
|
#endif
|
2015-05-20 07:07:01 +00:00
|
|
|
|
2015-07-13 13:19:33 +00:00
|
|
|
#if PS_BLEND_A == PS_BLEND_B
|
2015-07-18 11:40:10 +00:00
|
|
|
Color.rgb = D;
|
2015-07-13 13:19:33 +00:00
|
|
|
#else
|
2015-07-18 15:16:46 +00:00
|
|
|
Color.rgb = trunc((A - B) * C + D);
|
2015-05-19 22:51:37 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-26 14:16:36 +00:00
|
|
|
// FIXME dithering
|
|
|
|
|
|
|
|
// Correct the Color value based on the output format
|
2015-08-07 07:48:35 +00:00
|
|
|
#if PS_COLCLIP == 0 && PS_HDR == 0
|
2015-05-26 14:16:36 +00:00
|
|
|
// Standard Clamp
|
2015-07-18 11:40:10 +00:00
|
|
|
Color.rgb = clamp(Color.rgb, vec3(0.0f), vec3(255.0f));
|
2015-05-26 14:16:36 +00:00
|
|
|
#endif
|
|
|
|
|
2015-07-18 12:37:08 +00:00
|
|
|
// FIXME rouding of negative float?
|
|
|
|
// compiler uses trunc but it might need floor
|
|
|
|
|
2015-06-30 20:12:00 +00:00
|
|
|
// Warning: normally blending equation is mult(A, B) = A * B >> 7. GPU have the full accuracy
|
|
|
|
// GS: Color = 1, Alpha = 255 => output 1
|
|
|
|
// GPU: Color = 1/255, Alpha = 255/255 * 255/128 => output 1.9921875
|
2015-05-26 14:16:36 +00:00
|
|
|
#if PS_DFMT == FMT_16
|
|
|
|
// In 16 bits format, only 5 bits of colors are used. It impacts shadows computation of Castlevania
|
|
|
|
|
2015-07-18 12:37:08 +00:00
|
|
|
Color.rgb = vec3(ivec3(Color.rgb) & ivec3(0xF8));
|
2015-08-07 07:48:35 +00:00
|
|
|
#elif PS_COLCLIP == 1 && PS_HDR == 0
|
2015-07-18 12:37:08 +00:00
|
|
|
Color.rgb = vec3(ivec3(Color.rgb) & ivec3(0xFF));
|
2015-05-26 14:16:36 +00:00
|
|
|
#endif
|
2015-05-20 07:07:01 +00:00
|
|
|
|
2015-06-21 06:47:45 +00:00
|
|
|
#endif
|
2015-07-13 13:19:33 +00:00
|
|
|
}
|
2015-05-08 18:27:13 +00:00
|
|
|
|
2015-04-17 18:18:07 +00:00
|
|
|
void ps_main()
|
|
|
|
{
|
2015-08-04 18:08:33 +00:00
|
|
|
#if ((PS_DATE & 3) == 1 || (PS_DATE & 3) == 2) && !defined(DISABLE_GL42_image)
|
|
|
|
|
|
|
|
#if PS_WRITE_RG == 1
|
|
|
|
// Pseudo 16 bits access.
|
|
|
|
float rt_a = texelFetch(RtSampler, ivec2(gl_FragCoord.xy), 0).g;
|
2015-05-09 17:54:01 +00:00
|
|
|
#else
|
2015-04-24 18:13:38 +00:00
|
|
|
float rt_a = texelFetch(RtSampler, ivec2(gl_FragCoord.xy), 0).a;
|
2015-08-04 18:08:33 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (PS_DATE & 3) == 1
|
|
|
|
// DATM == 0: Pixel with alpha equal to 1 will failed
|
|
|
|
bool bad = (127.5f / 255.0f) < rt_a;
|
|
|
|
#elif (PS_DATE & 3) == 2
|
|
|
|
// DATM == 1: Pixel with alpha equal to 0 will failed
|
|
|
|
bool bad = rt_a < (127.5f / 255.0f);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (bad) {
|
2015-05-09 17:54:01 +00:00
|
|
|
#if PS_DATE >= 5
|
|
|
|
discard;
|
|
|
|
#else
|
2015-05-02 14:56:18 +00:00
|
|
|
imageStore(img_prim_min, ivec2(gl_FragCoord.xy), ivec4(-1));
|
|
|
|
return;
|
2015-05-09 17:54:01 +00:00
|
|
|
#endif
|
2015-04-25 10:46:59 +00:00
|
|
|
}
|
2015-08-04 18:08:33 +00:00
|
|
|
|
2015-04-24 18:13:38 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-17 18:18:07 +00:00
|
|
|
#if PS_DATE == 3 && !defined(DISABLE_GL42_image)
|
2015-06-27 14:58:37 +00:00
|
|
|
int stencil_ceil = imageLoad(img_prim_min, ivec2(gl_FragCoord.xy)).r;
|
2015-04-25 10:46:59 +00:00
|
|
|
// Note gl_PrimitiveID == stencil_ceil will be the primitive that will update
|
|
|
|
// the bad alpha value so we must keep it.
|
2015-04-17 18:18:07 +00:00
|
|
|
|
|
|
|
if (gl_PrimitiveID > stencil_ceil) {
|
|
|
|
discard;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-18 11:40:10 +00:00
|
|
|
vec4 C = ps_color();
|
2015-04-17 18:18:07 +00:00
|
|
|
#if (APITRACE_DEBUG & 1) == 1
|
2015-07-18 11:40:10 +00:00
|
|
|
C.r = 255f;
|
2015-04-17 18:18:07 +00:00
|
|
|
#endif
|
|
|
|
#if (APITRACE_DEBUG & 2) == 2
|
2015-07-18 11:40:10 +00:00
|
|
|
C.g = 255f;
|
2015-04-17 18:18:07 +00:00
|
|
|
#endif
|
|
|
|
#if (APITRACE_DEBUG & 4) == 4
|
2015-07-18 11:40:10 +00:00
|
|
|
C.b = 255f;
|
2015-04-17 18:18:07 +00:00
|
|
|
#endif
|
|
|
|
#if (APITRACE_DEBUG & 8) == 8
|
2015-07-18 11:40:10 +00:00
|
|
|
C.a = 128f;
|
2015-06-06 11:56:08 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if PS_SHUFFLE
|
2015-07-18 11:40:10 +00:00
|
|
|
uvec4 denorm_c = uvec4(C);
|
2015-06-06 11:56:08 +00:00
|
|
|
uvec2 denorm_TA = uvec2(vec2(TA.xy) * 255.0f + 0.5f);
|
|
|
|
|
|
|
|
// Write RB part. Mask will take care of the correct destination
|
|
|
|
#if PS_READ_BA
|
2015-07-18 11:40:10 +00:00
|
|
|
C.rb = C.bb;
|
2015-06-06 11:56:08 +00:00
|
|
|
#else
|
2015-07-18 11:40:10 +00:00
|
|
|
C.rb = C.rr;
|
2015-06-06 11:56:08 +00:00
|
|
|
#endif
|
|
|
|
|
2015-07-17 15:53:00 +00:00
|
|
|
// FIXME precompute my_TA & 0x80
|
|
|
|
|
2015-06-06 11:56:08 +00:00
|
|
|
// Write GA part. Mask will take care of the correct destination
|
2015-07-17 15:53:00 +00:00
|
|
|
// Note: GLSL 4.50/GL_EXT_shader_integer_mix support a mix instruction to select a component\n"
|
|
|
|
// However Nvidia emulate it with an if (at least on kepler arch) ...\n"
|
2015-06-06 11:56:08 +00:00
|
|
|
#if PS_READ_BA
|
2015-07-18 09:22:08 +00:00
|
|
|
// bit field operation requires GL4 HW. Could be nice to merge it with step/mix below
|
|
|
|
// uint my_ta = (bool(bitfieldExtract(denorm_c.a, 7, 1))) ? denorm_TA.y : denorm_TA.x;
|
|
|
|
// denorm_c.a = bitfieldInsert(denorm_c.a, bitfieldExtract(my_ta, 7, 1), 7, 1);
|
2015-07-18 11:40:10 +00:00
|
|
|
// c.ga = vec2(float(denorm_c.a));
|
2015-07-18 09:22:08 +00:00
|
|
|
|
2015-06-06 11:56:08 +00:00
|
|
|
if (bool(denorm_c.a & 0x80u))
|
2015-07-18 11:40:10 +00:00
|
|
|
C.ga = vec2(float((denorm_c.a & 0x7Fu) | (denorm_TA.y & 0x80u)));
|
2015-06-06 11:56:08 +00:00
|
|
|
else
|
2015-07-18 11:40:10 +00:00
|
|
|
C.ga = vec2(float((denorm_c.a & 0x7Fu) | (denorm_TA.x & 0x80u)));
|
2015-07-17 15:53:00 +00:00
|
|
|
|
2015-06-06 11:56:08 +00:00
|
|
|
#else
|
|
|
|
if (bool(denorm_c.g & 0x80u))
|
2015-07-18 11:40:10 +00:00
|
|
|
C.ga = vec2(float((denorm_c.g & 0x7Fu) | (denorm_TA.y & 0x80u)));
|
2015-06-06 11:56:08 +00:00
|
|
|
else
|
2015-07-18 11:40:10 +00:00
|
|
|
C.ga = vec2(float((denorm_c.g & 0x7Fu) | (denorm_TA.x & 0x80u)));
|
2015-07-17 15:53:00 +00:00
|
|
|
|
|
|
|
// Nice idea but step/mix requires 4 instructions
|
|
|
|
// set / trunc / I2F / Mad
|
|
|
|
//
|
2015-07-18 11:40:10 +00:00
|
|
|
// float sel = step(128.0f, c.g);
|
|
|
|
// vec2 c_shuffle = vec2((denorm_c.gg & 0x7Fu) | (denorm_TA & 0x80u));
|
2015-07-17 15:53:00 +00:00
|
|
|
// c.ga = mix(c_shuffle.xx, c_shuffle.yy, sel);
|
2015-06-06 11:56:08 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-17 18:18:07 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-26 14:16:36 +00:00
|
|
|
// Must be done before alpha correction
|
2015-07-18 11:40:10 +00:00
|
|
|
float alpha_blend = C.a / 128.0f;
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-05-26 14:16:36 +00:00
|
|
|
// Correct the ALPHA value based on the output format
|
2015-07-19 20:43:48 +00:00
|
|
|
#if (PS_DFMT == FMT_16)
|
2015-07-18 11:40:10 +00:00
|
|
|
float A_one = 128.0f; // alpha output will be 0x80
|
|
|
|
C.a = (PS_FBA != 0) ? A_one : step(128.0f, C.a) * A_one;
|
2015-05-26 14:16:36 +00:00
|
|
|
#elif (PS_DFMT == FMT_32) && (PS_FBA != 0)
|
2015-07-18 11:40:10 +00:00
|
|
|
if(C.a < 128.0f) C.a += 128.0f;
|
2015-04-24 15:51:29 +00:00
|
|
|
#endif
|
2015-04-17 18:18:07 +00:00
|
|
|
|
2015-04-25 10:46:59 +00:00
|
|
|
// Get first primitive that will write a failling alpha value
|
2015-04-17 18:18:07 +00:00
|
|
|
#if PS_DATE == 1 && !defined(DISABLE_GL42_image)
|
2015-04-25 10:46:59 +00:00
|
|
|
// DATM == 0
|
|
|
|
// Pixel with alpha equal to 1 will failed (128-255)
|
2015-07-18 11:40:10 +00:00
|
|
|
if (C.a > 127.5f) {
|
2015-04-25 10:46:59 +00:00
|
|
|
imageAtomicMin(img_prim_min, ivec2(gl_FragCoord.xy), gl_PrimitiveID);
|
2015-05-02 14:56:18 +00:00
|
|
|
return;
|
2015-04-25 10:46:59 +00:00
|
|
|
}
|
2015-04-17 18:18:07 +00:00
|
|
|
#elif PS_DATE == 2 && !defined(DISABLE_GL42_image)
|
2015-04-25 10:46:59 +00:00
|
|
|
// DATM == 1
|
|
|
|
// Pixel with alpha equal to 0 will failed (0-127)
|
2015-07-18 11:40:10 +00:00
|
|
|
if (C.a < 127.5f) {
|
2015-04-25 10:46:59 +00:00
|
|
|
imageAtomicMin(img_prim_min, ivec2(gl_FragCoord.xy), gl_PrimitiveID);
|
2015-05-02 14:56:18 +00:00
|
|
|
return;
|
2015-04-25 10:46:59 +00:00
|
|
|
}
|
2015-04-17 18:18:07 +00:00
|
|
|
#endif
|
|
|
|
|
2015-07-18 11:40:10 +00:00
|
|
|
ps_blend(C, alpha_blend);
|
2015-05-08 18:27:13 +00:00
|
|
|
|
2015-07-18 11:40:10 +00:00
|
|
|
ps_fbmask(C);
|
2015-06-21 06:47:45 +00:00
|
|
|
|
2015-07-30 20:05:08 +00:00
|
|
|
#if PS_HDR == 1
|
|
|
|
// Use negative value to avoid overflow of the texture (in accumulation mode)
|
2015-08-03 16:58:38 +00:00
|
|
|
// Note: code were initially done for an Half-Float texture. Due to overflow
|
|
|
|
// the texture was upgraded to a full float. Maybe this code is useless now!
|
|
|
|
// Good testcase is castlevania
|
2015-07-24 20:06:35 +00:00
|
|
|
if (any(greaterThan(C.rgb, vec3(128.0f)))) {
|
|
|
|
C.rgb = (C.rgb - 256.0f);
|
|
|
|
}
|
|
|
|
#endif
|
2015-07-18 11:40:10 +00:00
|
|
|
SV_Target0 = C / 255.0f;
|
2015-07-13 13:19:33 +00:00
|
|
|
SV_Target1 = vec4(alpha_blend);
|
2015-04-17 18:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|