Skip to content

Commit

Permalink
Alpha test must be performed before vcolor is applied
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Dec 8, 2019
1 parent fdb9330 commit c97d02a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/rendering/polyrenderer/drawers/poly_triangle.cpp
Expand Up @@ -257,6 +257,8 @@ void PolyTriangleThreadData::PushStreamData(const StreamData &data, const PolyPu

PushConstants = &constants;

AlphaThreshold = clamp((int)(PushConstants->uAlphaThreshold * 255.0f + 0.5f), 0, 255) << 24;

numPolyLights = 0;
if (constants.uLightIndex >= 0)
{
Expand Down
2 changes: 2 additions & 0 deletions src/rendering/polyrenderer/drawers/poly_triangle.h
Expand Up @@ -214,6 +214,7 @@ class PolyTriangleThreadData
uint32_t FragColor[MAXWIDTH];
uint16_t lightarray[MAXWIDTH];
uint32_t dynlights[MAXWIDTH];
uint8_t discard[MAXWIDTH];
} scanline;

static PolyTriangleThreadData *Get(DrawerThread *thread);
Expand Down Expand Up @@ -242,6 +243,7 @@ class PolyTriangleThreadData
int SpecialEffect = EFF_NONE;
int EffectState = 0;
bool AlphaTest = false;
uint32_t AlphaThreshold = 0x7f000000;
const PolyPushConstants* PushConstants = nullptr;

const void *vertices = nullptr;
Expand Down
22 changes: 14 additions & 8 deletions src/rendering/polyrenderer/drawers/screen_triangle.cpp
Expand Up @@ -451,14 +451,15 @@ static void BlendColor(int y, int x0, int x1, PolyTriangleThreadData* thread)
uint32_t* dest = (uint32_t*)thread->dest;
uint32_t* line = dest + y * (ptrdiff_t)thread->dest_pitch;
uint32_t* fragcolor = thread->scanline.FragColor;
uint8_t* discard = thread->scanline.discard;

for (int x = x0; x < x1; x++)
{
uint32_t fg = fragcolor[x];

if (OptT::Flags & SWBLEND_AlphaTest)
{
if (fragcolor[x] <= 0x7f000000)
if (discard[x])
continue;
}

Expand Down Expand Up @@ -540,9 +541,10 @@ static void WriteColor(int y, int x0, int x1, PolyTriangleThreadData* thread)
}
else
{
uint8_t* discard = thread->scanline.discard;
for (int x = x0; x < x1; x++)
{
if (fragcolor[x] > 0x7f000000)
if (!discard[x])
line[x] = fragcolor[x];
}
}
Expand Down Expand Up @@ -587,10 +589,11 @@ static void WriteDepth(int y, int x0, int x1, PolyTriangleThreadData* thread)
}
else
{
uint8_t* discard = thread->scanline.discard;
uint32_t* fragcolor = thread->scanline.FragColor;
for (int x = x0; x < x1; x++)
{
if (fragcolor[x] > 0x7f000000)
if (!discard[x])
line[x] = w[x];
}
}
Expand All @@ -610,10 +613,11 @@ static void WriteStencil(int y, int x0, int x1, PolyTriangleThreadData* thread)
}
else
{
uint8_t* discard = thread->scanline.discard;
uint32_t* fragcolor = thread->scanline.FragColor;
for (int x = x0; x < x1; x++)
{
if (fragcolor[x] > 0x7f000000)
if (!discard[x])
line[x] = value;
}
}
Expand Down Expand Up @@ -790,10 +794,9 @@ static void RunShader(int x0, int x1, PolyTriangleThreadData* thread)
case TM_CLAMPY:
for (int x = x0; x < x1; x++)
{
if (v[x] >= 0.0 && v[x] <= 1.0)
fragcolor[x] = SampleTexture(u[x], v[x], texPixels, texWidth, texHeight, texBgra);
else
fragcolor[x] = 0;
fragcolor[x] = SampleTexture(u[x], v[x], texPixels, texWidth, texHeight, texBgra);
if (v[x] < 0.0 || v[x] > 1.0)
fragcolor[x] &= 0x00ffffff;
}
break;
case TM_INVERTOPAQUE:
Expand Down Expand Up @@ -878,6 +881,8 @@ static void RunShader(int x0, int x1, PolyTriangleThreadData* thread)
}
}

uint32_t alphaThreshold = thread->AlphaThreshold;
uint8_t* discard = thread->scanline.discard;
for (int x = x0; x < x1; x++)
{
uint32_t r = thread->scanline.vColorR[x];
Expand All @@ -891,6 +896,7 @@ static void RunShader(int x0, int x1, PolyTriangleThreadData* thread)
b += b >> 7;

uint32_t texel = fragcolor[x];
discard[x] = texel <= alphaThreshold;
fragcolor[x] = MAKEARGB(
(APART(texel) * a + 127) >> 8,
(RPART(texel) * r + 127) >> 8,
Expand Down

0 comments on commit c97d02a

Please sign in to comment.