- implement more of the default shader

This commit is contained in:
Magnus Norddahl 2019-08-05 23:31:56 +02:00
parent 82a17572ef
commit 4e24fdacf0

View file

@ -238,6 +238,7 @@ static void RunShader(int x0, int x1, PolyTriangleThreadData* thread)
int texHeight = thread->drawargs.TextureHeight(); int texHeight = thread->drawargs.TextureHeight();
const uint32_t* texPixels = (const uint32_t*)thread->drawargs.TexturePixels(); const uint32_t* texPixels = (const uint32_t*)thread->drawargs.TexturePixels();
auto constants = thread->PushConstants; auto constants = thread->PushConstants;
auto& streamdata = thread->mainVertexShader.Data;
uint32_t* fragcolor = thread->scanline.FragColor; uint32_t* fragcolor = thread->scanline.FragColor;
float* u = thread->scanline.U; float* u = thread->scanline.U;
float* v = thread->scanline.V; float* v = thread->scanline.V;
@ -303,67 +304,102 @@ static void RunShader(int x0, int x1, PolyTriangleThreadData* thread)
} }
} }
#if 0
if (constants->uTextureMode != TM_FOGLAYER) if (constants->uTextureMode != TM_FOGLAYER)
{ {
texel.rgb += uAddColor.rgb; if (streamdata.uAddColor.r != 0.0f || streamdata.uAddColor.g != 0.0f || streamdata.uAddColor.b != 0.0f)
{
if (uObjectColor2.a == 0.0) texel *= uObjectColor; uint32_t r = (int)(streamdata.uAddColor.r * 255.0f);
else texel *= mix(uObjectColor, uObjectColor2, gradientdist.z); uint32_t g = (int)(streamdata.uAddColor.g * 255.0f);
texel = desaturate(texel); uint32_t b = (int)(streamdata.uAddColor.b * 255.0f);
}
#endif
#if 0
for (int x = x0; x < x1; x++) for (int x = x0; x < x1; x++)
{ {
uint32_t texel = fragcolor[x]; uint32_t texel = fragcolor[x];
fragcolor[x] = MAKEARGB(
//#ifndef NO_ALPHATEST APART(texel),
//if (texel.a <= uAlphaThreshold) discard; MIN(r + RPART(texel), (uint32_t)255),
//#endif MIN(g + GPART(texel), (uint32_t)255),
MIN(b + BPART(texel), (uint32_t)255));
if (constants->uFogEnabled != -3) // check for special 2D 'fog' mode. }
{
float fogdist = 0.0f;
float fogfactor = 0.0f;
// calculate fog factor
if (constants->uFogEnabled != 0)
{
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);
} }
if (constants->uTextureMode != 7) if (streamdata.uObjectColor2.a == 0.0f)
{ {
texel = getLightColor(texel, fogdist, fogfactor); if (streamdata.uObjectColor.r != 0.0f || streamdata.uObjectColor.g != 0.0f || streamdata.uObjectColor.b != 0.0f)
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
{ {
texel = vec4(uFogColor.rgb, (1.0 - fogfactor) * texel.a * 0.75 * vColor.a); 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);
else // simple 2D (uses the fog color to add a color overlay) 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++)
{ {
if (constants->uTextureMode == 7) uint32_t texel = fragcolor[x];
{ fragcolor[x] = MAKEARGB(
float gray = grayscale(texel); APART(texel),
vec4 cm = (uObjectColor + gray * (uAddColor - uObjectColor)) * 2; MIN((r * RPART(texel)) >> 8, (uint32_t)255),
texel = vec4(clamp(cm.rgb, 0.0, 1.0), texel.a); MIN((g * GPART(texel)) >> 8, (uint32_t)255),
MIN((b * BPART(texel)) >> 8, (uint32_t)255));
} }
texel = texel * ProcessLight(texel, vColor);
texel.rgb = texel.rgb + uFogColor.rgb;
} }
fragcolor[x] = texel; if (streamdata.uDesaturationFactor > 0.0f)
{
uint32_t t = (int)(streamdata.uDesaturationFactor * 256.0f);
uint32_t inv_t = 256 - t;
for (int x = x0; x < x1; x++)
{
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);
}
}
}
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) static void DrawSpan(int y, int x0, int x1, const TriDrawTriangleArgs* args, PolyTriangleThreadData* thread)