Skip to content

Commit

Permalink
- implement more of the default shader
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Aug 5, 2019
1 parent 82a1757 commit 4e24fda
Showing 1 changed file with 80 additions and 44 deletions.
124 changes: 80 additions & 44 deletions src/rendering/polyrenderer/drawers/screen_triangle.cpp
Expand Up @@ -238,6 +238,7 @@ static void RunShader(int x0, int x1, PolyTriangleThreadData* thread)
int texHeight = thread->drawargs.TextureHeight();
const uint32_t* texPixels = (const uint32_t*)thread->drawargs.TexturePixels();
auto constants = thread->PushConstants;
auto& streamdata = thread->mainVertexShader.Data;
uint32_t* fragcolor = thread->scanline.FragColor;
float* u = thread->scanline.U;
float* v = thread->scanline.V;
Expand Down Expand Up @@ -303,67 +304,102 @@ static void RunShader(int x0, int x1, PolyTriangleThreadData* thread)
}
}

#if 0
if (constants->uTextureMode != TM_FOGLAYER)
{
texel.rgb += uAddColor.rgb;

if (uObjectColor2.a == 0.0) texel *= uObjectColor;
else texel *= mix(uObjectColor, uObjectColor2, gradientdist.z);
texel = desaturate(texel);
}
#endif

#if 0
for (int x = x0; x < x1; x++)
{
uint32_t texel = fragcolor[x];

//#ifndef NO_ALPHATEST
//if (texel.a <= uAlphaThreshold) discard;
//#endif

if (constants->uFogEnabled != -3) // check for special 2D 'fog' mode.
if (streamdata.uAddColor.r != 0.0f || streamdata.uAddColor.g != 0.0f || streamdata.uAddColor.b != 0.0f)
{
float fogdist = 0.0f;
float fogfactor = 0.0f;

// calculate fog factor
if (constants->uFogEnabled != 0)
uint32_t r = (int)(streamdata.uAddColor.r * 255.0f);
uint32_t g = (int)(streamdata.uAddColor.g * 255.0f);
uint32_t b = (int)(streamdata.uAddColor.b * 255.0f);
for (int x = x0; x < x1; x++)
{
if (constants->uFogEnabled == 1 || constants->uFogEnabled == -1)
fogdist = max(16.0, pixelpos.w);
else
fogdist = max(16.0, distance(pixelpos.xyz, uCameraPos.xyz));
fogfactor = exp2(constants->uFogDensity * fogdist);
uint32_t texel = fragcolor[x];
fragcolor[x] = MAKEARGB(
APART(texel),
MIN(r + RPART(texel), (uint32_t)255),
MIN(g + GPART(texel), (uint32_t)255),
MIN(b + BPART(texel), (uint32_t)255));
}
}

if (constants->uTextureMode != 7)
if (streamdata.uObjectColor2.a == 0.0f)
{
if (streamdata.uObjectColor.r != 0.0f || streamdata.uObjectColor.g != 0.0f || streamdata.uObjectColor.b != 0.0f)
{
texel = getLightColor(texel, fogdist, fogfactor);
if (constants->uFogEnabled < 0)
texel = applyFog(texel, fogfactor);
uint32_t r = (int)(streamdata.uObjectColor.r * 256.0f);
uint32_t g = (int)(streamdata.uObjectColor.g * 256.0f);
uint32_t b = (int)(streamdata.uObjectColor.b * 256.0f);
for (int x = x0; x < x1; x++)
{
uint32_t texel = fragcolor[x];
fragcolor[x] = MAKEARGB(
APART(texel),
MIN((r * RPART(texel)) >> 8, (uint32_t)255),
MIN((g * GPART(texel)) >> 8, (uint32_t)255),
MIN((b * BPART(texel)) >> 8, (uint32_t)255));
}
}
else
}
else
{
float t = thread->mainVertexShader.gradientdist.Z;
float inv_t = 1.0f - t;
uint32_t r = (int)((streamdata.uObjectColor.r * inv_t + streamdata.uObjectColor2.r * t) * 256.0f);
uint32_t g = (int)((streamdata.uObjectColor.g * inv_t + streamdata.uObjectColor2.r * t) * 256.0f);
uint32_t b = (int)((streamdata.uObjectColor.b * inv_t + streamdata.uObjectColor2.r * t) * 256.0f);
for (int x = x0; x < x1; x++)
{
texel = vec4(uFogColor.rgb, (1.0 - fogfactor) * texel.a * 0.75 * vColor.a);
uint32_t texel = fragcolor[x];
fragcolor[x] = MAKEARGB(
APART(texel),
MIN((r * RPART(texel)) >> 8, (uint32_t)255),
MIN((g * GPART(texel)) >> 8, (uint32_t)255),
MIN((b * BPART(texel)) >> 8, (uint32_t)255));
}
}
else // simple 2D (uses the fog color to add a color overlay)

if (streamdata.uDesaturationFactor > 0.0f)
{
if (constants->uTextureMode == 7)
uint32_t t = (int)(streamdata.uDesaturationFactor * 256.0f);
uint32_t inv_t = 256 - t;
for (int x = x0; x < x1; x++)
{
float gray = grayscale(texel);
vec4 cm = (uObjectColor + gray * (uAddColor - uObjectColor)) * 2;
texel = vec4(clamp(cm.rgb, 0.0, 1.0), texel.a);
uint32_t texel = fragcolor[x];
uint32_t gray = (RPART(texel) * 77 + GPART(texel) * 143 + BPART(texel) * 37) >> 8;
fragcolor[x] = MAKEARGB(
APART(texel),
(RPART(texel) * inv_t + gray * t + 127) >> 8,
(GPART(texel) * inv_t + gray * t + 127) >> 8,
(BPART(texel) * inv_t + gray * t + 127) >> 8);
}
texel = texel * ProcessLight(texel, vColor);
texel.rgb = texel.rgb + uFogColor.rgb;
}
}

fragcolor[x] = texel;
if (thread->mainVertexShader.vColor != 0xffffffff)
{
uint32_t a = APART(thread->mainVertexShader.vColor);
uint32_t r = RPART(thread->mainVertexShader.vColor);
uint32_t g = GPART(thread->mainVertexShader.vColor);
uint32_t b = BPART(thread->mainVertexShader.vColor);
a += a >> 7;
r += r >> 7;
g += g >> 7;
b += b >> 7;
for (int x = x0; x < x1; x++)
{
uint32_t texel = fragcolor[x];
fragcolor[x] = MAKEARGB(
(APART(texel) * a + 127) >> 8,
(RPART(texel) * r + 127) >> 8,
(GPART(texel) * g + 127) >> 8,
(BPART(texel) * b + 127) >> 8);
}
}

if (constants->uLightLevel >= 0.0f)
{
// To do: apply diminishing light and fog
}
#endif
}

static void DrawSpan(int y, int x0, int x1, const TriDrawTriangleArgs* args, PolyTriangleThreadData* thread)
Expand Down

0 comments on commit 4e24fda

Please sign in to comment.