Add some experimental draw triangle code

This commit is contained in:
Magnus Norddahl 2016-10-18 18:23:56 +02:00
parent 79b14bbc08
commit 694cae054c
2 changed files with 254 additions and 0 deletions

View file

@ -1256,6 +1256,255 @@ void ApplySpecialColormapRGBACommand::Execute(DrawerThread *thread)
/////////////////////////////////////////////////////////////////////////////
struct TriVertex
{
enum { NumVarying = 3 };
float x, y, z, w;
float varying[NumVarying];
};
float gradx(float x0, float y0, float x1, float y1, float x2, float y2, float c0, float c1, float c2)
{
float top = (c1 - c2) * (y0 - y2) - (c0 - c2) * (y1 - y2);
float bottom = (x1 - x2) * (y0 - y2) - (x0 - x2) * (y1 - y2);
return top / bottom;
}
float grady(float x0, float y0, float x1, float y1, float x2, float y2, float c0, float c1, float c2)
{
float top = (c1 - c2) * (x0 - x2) - (c0 - c2) * (x1 - x2);
float bottom = -((x1 - x2) * (y0 - y2) - (x0 - x2) * (y1 - y2));
return top / bottom;
}
void triangle(uint32_t *dest, int pitch, const TriVertex &v1, const TriVertex &v2, const TriVertex &v3)
{
// 28.4 fixed-point coordinates
const int Y1 = (int)round(16.0f * v1.y);
const int Y2 = (int)round(16.0f * v2.y);
const int Y3 = (int)round(16.0f * v3.y);
const int X1 = (int)round(16.0f * v1.x);
const int X2 = (int)round(16.0f * v2.x);
const int X3 = (int)round(16.0f * v3.x);
// Deltas
const int DX12 = X1 - X2;
const int DX23 = X2 - X3;
const int DX31 = X3 - X1;
const int DY12 = Y1 - Y2;
const int DY23 = Y2 - Y3;
const int DY31 = Y3 - Y1;
// Fixed-point deltas
const int FDX12 = DX12 << 4;
const int FDX23 = DX23 << 4;
const int FDX31 = DX31 << 4;
const int FDY12 = DY12 << 4;
const int FDY23 = DY23 << 4;
const int FDY31 = DY31 << 4;
// Bounding rectangle
int minx = (MIN(MIN(X1, X2), X3) + 0xF) >> 4;
int maxx = (MAX(MAX(X1, X2), X3) + 0xF) >> 4;
int miny = (MIN(MIN(Y1, Y2), Y3) + 0xF) >> 4;
int maxy = (MAX(MAX(Y1, Y2), Y3) + 0xF) >> 4;
// Block size, standard 8x8 (must be power of two)
const int q = 8;
// Start in corner of 8x8 block
minx &= ~(q - 1);
miny &= ~(q - 1);
dest += miny * pitch;
// Half-edge constants
int C1 = DY12 * X1 - DX12 * Y1;
int C2 = DY23 * X2 - DX23 * Y2;
int C3 = DY31 * X3 - DX31 * Y3;
// Correct for fill convention
if (DY12 < 0 || (DY12 == 0 && DX12 > 0)) C1++;
if (DY23 < 0 || (DY23 == 0 && DX23 > 0)) C2++;
if (DY31 < 0 || (DY31 == 0 && DX31 > 0)) C3++;
// Gradients
float gradWX = gradx(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.w, v2.w, v3.w);
float gradWY = grady(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.w, v2.w, v3.w);
float startW = v1.w + gradWX * (minx - v1.x) + gradWY * (miny - v1.y);
float gradVaryingX[TriVertex::NumVarying], gradVaryingY[TriVertex::NumVarying], startVarying[TriVertex::NumVarying];
for (int i = 0; i < TriVertex::NumVarying; i++)
{
gradVaryingX[i] = gradx(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.varying[i] * v1.w, v2.varying[i] * v2.w, v3.varying[i] * v3.w);
gradVaryingY[i] = grady(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.varying[i] * v1.w, v2.varying[i] * v2.w, v3.varying[i] * v3.w);
startVarying[i] = v1.varying[i] * v1.w + gradVaryingX[i] * (minx - v1.x) + gradVaryingY[i] * (miny - v1.y);
}
// Loop through blocks
for (int y = miny; y < maxy; y += q)
{
for (int x = minx; x < maxx; x += q)
{
// Corners of block
int x0 = x << 4;
int x1 = (x + q - 1) << 4;
int y0 = y << 4;
int y1 = (y + q - 1) << 4;
// Evaluate half-space functions
bool a00 = C1 + DX12 * y0 - DY12 * x0 > 0;
bool a10 = C1 + DX12 * y0 - DY12 * x1 > 0;
bool a01 = C1 + DX12 * y1 - DY12 * x0 > 0;
bool a11 = C1 + DX12 * y1 - DY12 * x1 > 0;
int a = (a00 << 0) | (a10 << 1) | (a01 << 2) | (a11 << 3);
bool b00 = C2 + DX23 * y0 - DY23 * x0 > 0;
bool b10 = C2 + DX23 * y0 - DY23 * x1 > 0;
bool b01 = C2 + DX23 * y1 - DY23 * x0 > 0;
bool b11 = C2 + DX23 * y1 - DY23 * x1 > 0;
int b = (b00 << 0) | (b10 << 1) | (b01 << 2) | (b11 << 3);
bool c00 = C3 + DX31 * y0 - DY31 * x0 > 0;
bool c10 = C3 + DX31 * y0 - DY31 * x1 > 0;
bool c01 = C3 + DX31 * y1 - DY31 * x0 > 0;
bool c11 = C3 + DX31 * y1 - DY31 * x1 > 0;
int c = (c00 << 0) | (c10 << 1) | (c01 << 2) | (c11 << 3);
// Skip block when outside an edge
if (a == 0x0 || b == 0x0 || c == 0x0) continue;
// Calculate varying variables for affine block
float offx0 = (x - minx) + 0.5f;
float offy0 = (y - miny) + 0.5f;
float offx1 = offx0 + q;
float offy1 = offy0 + q;
float rcpWTL = 1.0f / (startW + offx0 * gradWX + offy0 * gradWY);
float rcpWTR = 1.0f / (startW + offx1 * gradWX + offy0 * gradWY);
float rcpWBL = 1.0f / (startW + offx0 * gradWX + offy1 * gradWY);
float rcpWBR = 1.0f / (startW + offx1 * gradWX + offy1 * gradWY);
float varyingTL[TriVertex::NumVarying];
float varyingTR[TriVertex::NumVarying];
float varyingBL[TriVertex::NumVarying];
float varyingBR[TriVertex::NumVarying];
for (int i = 0; i < TriVertex::NumVarying; i++)
{
varyingTL[i] = (startVarying[i] + offx0 * gradVaryingX[i] + offy0 * gradVaryingY[i]) * rcpWTL;
varyingTR[i] = (startVarying[i] + offx1 * gradVaryingX[i] + offy0 * gradVaryingY[i]) * rcpWTR;
varyingBL[i] = ((startVarying[i] + offx0 * gradVaryingX[i] + offy1 * gradVaryingY[i]) * rcpWBL - varyingTL[i]) * (1.0f / q);
varyingBR[i] = ((startVarying[i] + offx1 * gradVaryingX[i] + offy1 * gradVaryingY[i]) * rcpWBR - varyingTR[i]) * (1.0f / q);
}
uint32_t *buffer = dest;
// Accept whole block when totally covered
if (a == 0xF && b == 0xF && c == 0xF)
{
for (int iy = 0; iy < q; iy++)
{
float varying[TriVertex::NumVarying], varyingStep[TriVertex::NumVarying];
for (int i = 0; i < TriVertex::NumVarying; i++)
{
varying[i] = varyingTL[i] + varyingBL[i] * iy;
varyingStep[i] = (varyingTR[i] + varyingBR[i] * iy - varying[i]) * (1.0f / q);
}
for (int ix = x; ix < x + q; ix++)
{
uint32_t red = (uint32_t)clamp(varying[0] * 255.0f + 0.5f, 0.0f, 255.0f);
uint32_t green = (uint32_t)clamp(varying[1] * 255.0f + 0.5f, 0.0f, 255.0f);
uint32_t blue = (uint32_t)clamp(varying[2] * 255.0f + 0.5f, 0.0f, 255.0f);
buffer[ix] = 0xff000000 | (red << 16) | (green << 8) | blue;
for (int i = 0; i < TriVertex::NumVarying; i++)
varying[i] += varyingStep[i];
}
buffer += pitch;
}
}
else // Partially covered block
{
int CY1 = C1 + DX12 * y0 - DY12 * x0;
int CY2 = C2 + DX23 * y0 - DY23 * x0;
int CY3 = C3 + DX31 * y0 - DY31 * x0;
for (int iy = 0; iy < q; iy++)
{
int CX1 = CY1;
int CX2 = CY2;
int CX3 = CY3;
float varying[TriVertex::NumVarying], varyingStep[TriVertex::NumVarying];
for (int i = 0; i < TriVertex::NumVarying; i++)
{
varying[i] = varyingTL[i] + varyingBL[i] * iy;
varyingStep[i] = (varyingTR[i] + varyingBR[i] * iy - varying[i]) * (1.0f / q);
}
for (int ix = x; ix < x + q; ix++)
{
if (CX1 > 0 && CX2 > 0 && CX3 > 0)
{
uint32_t red = (uint32_t)clamp(varying[0] * 255.0f + 0.5f, 0.0f, 255.0f);
uint32_t green = (uint32_t)clamp(varying[1] * 255.0f + 0.5f, 0.0f, 255.0f);
uint32_t blue = (uint32_t)clamp(varying[2] * 255.0f + 0.5f, 0.0f, 255.0f);
buffer[ix] = 0xff000000 | (red << 16) | (green << 8) | blue;
}
for (int i = 0; i < TriVertex::NumVarying; i++)
varying[i] += varyingStep[i];
CX1 -= FDY12;
CX2 -= FDY23;
CX3 -= FDY31;
}
CY1 += FDX12;
CY2 += FDX23;
CY3 += FDX31;
buffer += pitch;
}
}
}
dest += q * pitch;
}
}
void R_DrawTriangle()
{
TriVertex trivert[6];
trivert[0].x = 100;
trivert[0].y = 350;
trivert[0].w = 1.0f;
trivert[0].varying[0] = 0.0f;
trivert[0].varying[1] = 1.0f;
trivert[0].varying[2] = 0.0f;
trivert[1].x = 400;
trivert[1].y = 500;
trivert[1].w = 1.0f;
trivert[1].varying[0] = 1.0f;
trivert[1].varying[1] = 0.0f;
trivert[1].varying[2] = 0.0f;
trivert[2].x = 200;
trivert[2].y = 200;
trivert[2].w = 1.0f;
trivert[2].varying[0] = 0.0f;
trivert[2].varying[1] = 0.0f;
trivert[2].varying[2] = 1.0f;
triangle((uint32_t*)dc_destorg, dc_pitch, trivert[0], trivert[1], trivert[2]);
}
/////////////////////////////////////////////////////////////////////////////
void R_DrawSingleSkyCol1(uint32_t solid_top, uint32_t solid_bottom)
{
DrawerCommandQueue::QueueCommand<DrawSingleSky1LLVMCommand>(solid_top, solid_bottom);

View file

@ -47,10 +47,13 @@
EXTERN_CVAR(Bool, r_shadercolormaps)
CVAR(Bool, r_drawtriangle, false, 0)
void R_SWRSetWindow(int windowSize, int fullWidth, int fullHeight, int stHeight, float trueratio);
void R_SetupColormap(player_t *);
void R_SetupFreelook();
void R_InitRenderer();
void R_DrawTriangle();
FSoftwareRenderer::FSoftwareRenderer()
{
@ -192,6 +195,8 @@ void FSoftwareRenderer::RenderView(player_t *player)
}
R_EndDrawerCommands();
if (r_swtruecolor && r_drawtriangle)
R_DrawTriangle();
}
//==========================================================================