mirror of
https://github.com/ZDoom/qzdoom-gpl.git
synced 2024-11-26 13:51:35 +00:00
Add codegen for one of the triangle variants
This commit is contained in:
parent
667f35bfce
commit
c852b6c5e9
18 changed files with 739 additions and 38 deletions
|
@ -1487,6 +1487,7 @@ set (PCH_SOURCES
|
|||
r_compiler/fixedfunction/drawwallcodegen.cpp
|
||||
r_compiler/fixedfunction/drawcolumncodegen.cpp
|
||||
r_compiler/fixedfunction/drawskycodegen.cpp
|
||||
r_compiler/fixedfunction/drawtrianglecodegen.cpp
|
||||
r_data/sprites.cpp
|
||||
r_data/voxels.cpp
|
||||
r_data/renderstyle.cpp
|
||||
|
|
483
src/r_compiler/fixedfunction/drawtrianglecodegen.cpp
Normal file
483
src/r_compiler/fixedfunction/drawtrianglecodegen.cpp
Normal file
|
@ -0,0 +1,483 @@
|
|||
/*
|
||||
** DrawTriangle code generation
|
||||
** Copyright (c) 2016 Magnus Norddahl
|
||||
**
|
||||
** This software is provided 'as-is', without any express or implied
|
||||
** warranty. In no event will the authors be held liable for any damages
|
||||
** arising from the use of this software.
|
||||
**
|
||||
** Permission is granted to anyone to use this software for any purpose,
|
||||
** including commercial applications, and to alter it and redistribute it
|
||||
** freely, subject to the following restrictions:
|
||||
**
|
||||
** 1. The origin of this software must not be misrepresented; you must not
|
||||
** claim that you wrote the original software. If you use this software
|
||||
** in a product, an acknowledgment in the product documentation would be
|
||||
** appreciated but is not required.
|
||||
** 2. Altered source versions must be plainly marked as such, and must not be
|
||||
** misrepresented as being the original software.
|
||||
** 3. This notice may not be removed or altered from any source distribution.
|
||||
**
|
||||
*/
|
||||
|
||||
#include "i_system.h"
|
||||
#include "r_compiler/llvm_include.h"
|
||||
#include "r_compiler/fixedfunction/drawtrianglecodegen.h"
|
||||
#include "r_compiler/ssa/ssa_function.h"
|
||||
#include "r_compiler/ssa/ssa_scope.h"
|
||||
#include "r_compiler/ssa/ssa_for_block.h"
|
||||
#include "r_compiler/ssa/ssa_if_block.h"
|
||||
#include "r_compiler/ssa/ssa_stack.h"
|
||||
#include "r_compiler/ssa/ssa_function.h"
|
||||
#include "r_compiler/ssa/ssa_struct_type.h"
|
||||
#include "r_compiler/ssa/ssa_value.h"
|
||||
|
||||
void DrawTriangleCodegen::Generate(TriDrawVariant variant, bool truecolor, SSAValue args, SSAValue thread_data)
|
||||
{
|
||||
LoadArgs(variant, truecolor, args, thread_data);
|
||||
Setup(variant, truecolor);
|
||||
LoopBlockY(variant, truecolor);
|
||||
}
|
||||
|
||||
void DrawTriangleCodegen::Setup(TriDrawVariant variant, bool truecolor)
|
||||
{
|
||||
int pixelsize = truecolor ? 4 : 1;
|
||||
|
||||
// 28.4 fixed-point coordinates
|
||||
Y1 = SSAInt(SSAFloat::round(16.0f * v1.y), false);
|
||||
Y2 = SSAInt(SSAFloat::round(16.0f * v2.y), false);
|
||||
Y3 = SSAInt(SSAFloat::round(16.0f * v3.y), false);
|
||||
|
||||
X1 = SSAInt(SSAFloat::round(16.0f * v1.x), false);
|
||||
X2 = SSAInt(SSAFloat::round(16.0f * v2.x), false);
|
||||
X3 = SSAInt(SSAFloat::round(16.0f * v3.x), false);
|
||||
|
||||
// Deltas
|
||||
DX12 = X1 - X2;
|
||||
DX23 = X2 - X3;
|
||||
DX31 = X3 - X1;
|
||||
|
||||
DY12 = Y1 - Y2;
|
||||
DY23 = Y2 - Y3;
|
||||
DY31 = Y3 - Y1;
|
||||
|
||||
// Fixed-point deltas
|
||||
FDX12 = DX12 << 4;
|
||||
FDX23 = DX23 << 4;
|
||||
FDX31 = DX31 << 4;
|
||||
|
||||
FDY12 = DY12 << 4;
|
||||
FDY23 = DY23 << 4;
|
||||
FDY31 = DY31 << 4;
|
||||
|
||||
// Bounding rectangle
|
||||
minx = SSAInt::MAX((SSAInt::MIN(SSAInt::MIN(X1, X2), X3) + 0xF) >> 4, clipleft);
|
||||
maxx = SSAInt::MIN((SSAInt::MAX(SSAInt::MAX(X1, X2), X3) + 0xF) >> 4, clipright - 1);
|
||||
miny = SSAInt::MAX((SSAInt::MIN(SSAInt::MIN(Y1, Y2), Y3) + 0xF) >> 4, cliptop);
|
||||
maxy = SSAInt::MIN((SSAInt::MAX(SSAInt::MAX(Y1, Y2), Y3) + 0xF) >> 4, clipbottom - 1);
|
||||
|
||||
SSAIfBlock if0;
|
||||
if0.if_block(minx >= maxx || miny >= maxy);
|
||||
if0.end_retvoid();
|
||||
|
||||
// Start in corner of 8x8 block
|
||||
minx = minx & ~(q - 1);
|
||||
miny = miny & ~(q - 1);
|
||||
|
||||
dest = dest[miny * pitch * pixelsize];
|
||||
subsectorGBuffer = subsectorGBuffer[miny * pitch];
|
||||
|
||||
// Half-edge constants
|
||||
C1 = DY12 * X1 - DX12 * Y1;
|
||||
C2 = DY23 * X2 - DX23 * Y2;
|
||||
C3 = DY31 * X3 - DX31 * Y3;
|
||||
|
||||
// Correct for fill convention
|
||||
SSAIfBlock if1;
|
||||
if1.if_block(DY12 < SSAInt(0) || (DY12 == SSAInt(0) && DX12 > SSAInt(0)));
|
||||
stack_C1.store(C1 + 1);
|
||||
if1.else_block();
|
||||
stack_C1.store(C1);
|
||||
if1.end_block();
|
||||
C1 = stack_C1.load();
|
||||
SSAIfBlock if2;
|
||||
if2.if_block(DY23 < SSAInt(0) || (DY23 == SSAInt(0) && DX23 > SSAInt(0)));
|
||||
stack_C2.store(C2 + 1);
|
||||
if2.else_block();
|
||||
stack_C2.store(C2);
|
||||
if2.end_block();
|
||||
C2 = stack_C2.load();
|
||||
SSAIfBlock if3;
|
||||
if3.if_block(DY31 < SSAInt(0) || (DY31 == SSAInt(0) && DX31 > SSAInt(0)));
|
||||
stack_C3.store(C3 + 1);
|
||||
if3.else_block();
|
||||
stack_C3.store(C3);
|
||||
if3.end_block();
|
||||
C3 = stack_C3.load();
|
||||
|
||||
// Gradients
|
||||
gradWX = gradx(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.w, v2.w, v3.w);
|
||||
gradWY = grady(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v1.w, v2.w, v3.w);
|
||||
startW = v1.w + gradWX * (SSAFloat(minx) - v1.x) + gradWY * (SSAFloat(miny) - v1.y);
|
||||
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] * (SSAFloat(minx) - v1.x) + gradVaryingY[i] * (SSAFloat(miny) - v1.y);
|
||||
}
|
||||
}
|
||||
|
||||
SSAFloat DrawTriangleCodegen::gradx(SSAFloat x0, SSAFloat y0, SSAFloat x1, SSAFloat y1, SSAFloat x2, SSAFloat y2, SSAFloat c0, SSAFloat c1, SSAFloat c2)
|
||||
{
|
||||
SSAFloat top = (c1 - c2) * (y0 - y2) - (c0 - c2) * (y1 - y2);
|
||||
SSAFloat bottom = (x1 - x2) * (y0 - y2) - (x0 - x2) * (y1 - y2);
|
||||
return top / bottom;
|
||||
}
|
||||
|
||||
SSAFloat DrawTriangleCodegen::grady(SSAFloat x0, SSAFloat y0, SSAFloat x1, SSAFloat y1, SSAFloat x2, SSAFloat y2, SSAFloat c0, SSAFloat c1, SSAFloat c2)
|
||||
{
|
||||
SSAFloat top = (c1 - c2) * (x0 - x2) - (c0 - c2) * (x1 - x2);
|
||||
SSAFloat bottom = (x0 - x2) * (y1 - y2) - (x1 - x2) * (y0 - y2);
|
||||
return top / bottom;
|
||||
}
|
||||
|
||||
void DrawTriangleCodegen::LoopBlockY(TriDrawVariant variant, bool truecolor)
|
||||
{
|
||||
int pixelsize = truecolor ? 4 : 1;
|
||||
|
||||
stack_y.store(miny);
|
||||
stack_dest.store(dest);
|
||||
stack_subsectorGBuffer.store(subsectorGBuffer);
|
||||
|
||||
SSAForBlock loop;
|
||||
y = stack_y.load();
|
||||
dest = stack_dest.load();
|
||||
subsectorGBuffer = stack_subsectorGBuffer.load();
|
||||
loop.loop_block(y < maxy, 0);
|
||||
{
|
||||
SSAIfBlock branch;
|
||||
branch.if_block((y / q) % thread.num_cores == thread.core);
|
||||
{
|
||||
LoopBlockX(variant, truecolor);
|
||||
}
|
||||
branch.end_block();
|
||||
|
||||
stack_dest.store(dest[q * pitch * pixelsize]);
|
||||
stack_subsectorGBuffer.store(subsectorGBuffer[q * pitch]);
|
||||
stack_y.store(y + q);
|
||||
}
|
||||
loop.end_block();
|
||||
}
|
||||
|
||||
void DrawTriangleCodegen::LoopBlockX(TriDrawVariant variant, bool truecolor)
|
||||
{
|
||||
stack_x.store(minx);
|
||||
|
||||
SSAForBlock loop;
|
||||
x = stack_x.load();
|
||||
loop.loop_block(x < maxx, 0);
|
||||
{
|
||||
// Corners of block
|
||||
x0 = x << 4;
|
||||
x1 = (x + q - 1) << 4;
|
||||
y0 = y << 4;
|
||||
y1 = (y + q - 1) << 4;
|
||||
|
||||
// Evaluate half-space functions
|
||||
SSABool a00 = C1 + DX12 * y0 - DY12 * x0 > SSAInt(0);
|
||||
SSABool a10 = C1 + DX12 * y0 - DY12 * x1 > SSAInt(0);
|
||||
SSABool a01 = C1 + DX12 * y1 - DY12 * x0 > SSAInt(0);
|
||||
SSABool a11 = C1 + DX12 * y1 - DY12 * x1 > SSAInt(0);
|
||||
|
||||
SSAInt a = (a00.zext_int() << 0) | (a10.zext_int() << 1) | (a01.zext_int() << 2) | (a11.zext_int() << 3);
|
||||
|
||||
SSABool b00 = C2 + DX23 * y0 - DY23 * x0 > SSAInt(0);
|
||||
SSABool b10 = C2 + DX23 * y0 - DY23 * x1 > SSAInt(0);
|
||||
SSABool b01 = C2 + DX23 * y1 - DY23 * x0 > SSAInt(0);
|
||||
SSABool b11 = C2 + DX23 * y1 - DY23 * x1 > SSAInt(0);
|
||||
SSAInt b = (b00.zext_int() << 0) | (b10.zext_int() << 1) | (b01.zext_int() << 2) | (b11.zext_int() << 3);
|
||||
|
||||
SSABool c00 = C3 + DX31 * y0 - DY31 * x0 > SSAInt(0);
|
||||
SSABool c10 = C3 + DX31 * y0 - DY31 * x1 > SSAInt(0);
|
||||
SSABool c01 = C3 + DX31 * y1 - DY31 * x0 > SSAInt(0);
|
||||
SSABool c11 = C3 + DX31 * y1 - DY31 * x1 > SSAInt(0);
|
||||
SSAInt c = (c00.zext_int() << 0) | (c10.zext_int() << 1) | (c01.zext_int() << 2) | (c11.zext_int() << 3);
|
||||
|
||||
// Skip block when outside an edge
|
||||
SSAIfBlock branch;
|
||||
branch.if_block(!(a == SSAInt(0) || b == SSAInt(0) || c == SSAInt(0)));
|
||||
|
||||
// Check if block needs clipping
|
||||
SSABool clipneeded = clipleft > x || clipright < (x + q) || cliptop > y || clipbottom < (y + q);
|
||||
|
||||
// Calculate varying variables for affine block
|
||||
SSAFloat offx0 = SSAFloat(x - minx) + 0.5f;
|
||||
SSAFloat offy0 = SSAFloat(y - miny) + 0.5f;
|
||||
SSAFloat offx1 = offx0 + SSAFloat(q);
|
||||
SSAFloat offy1 = offy0 + SSAFloat(q);
|
||||
SSAFloat rcpWTL = 1.0f / (startW + offx0 * gradWX + offy0 * gradWY);
|
||||
SSAFloat rcpWTR = 1.0f / (startW + offx1 * gradWX + offy0 * gradWY);
|
||||
SSAFloat rcpWBL = 1.0f / (startW + offx0 * gradWX + offy1 * gradWY);
|
||||
SSAFloat rcpWBR = 1.0f / (startW + offx1 * gradWX + offy1 * gradWY);
|
||||
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);
|
||||
}
|
||||
|
||||
SSAFloat globVis = SSAFloat(1706.0f);
|
||||
SSAFloat vis = globVis / rcpWTL;
|
||||
SSAFloat shade = 64.0f - (SSAFloat(light * 255 / 256) + 12.0f) * 32.0f / 128.0f;
|
||||
SSAFloat lightscale = SSAFloat::clamp((shade - SSAFloat::MIN(SSAFloat(24.0f), vis)) / 32.0f, SSAFloat(0.0f), SSAFloat(31.0f / 32.0f));
|
||||
diminishedlight = SSAInt(SSAFloat::clamp((1.0f - lightscale) * 256.0f + 0.5f, SSAFloat(0.0f), SSAFloat(256.0f)), false);
|
||||
|
||||
// Accept whole block when totally covered
|
||||
SSAIfBlock branch_covered;
|
||||
branch_covered.if_block(a == SSAInt(0xF) && b == SSAInt(0xF) && c == SSAInt(0xF) && !clipneeded);
|
||||
{
|
||||
LoopFullBlock(variant, truecolor);
|
||||
}
|
||||
branch_covered.else_block();
|
||||
{
|
||||
LoopPartialBlock(variant, truecolor);
|
||||
}
|
||||
branch_covered.end_block();
|
||||
|
||||
branch.end_block();
|
||||
|
||||
stack_x.store(x + q);
|
||||
}
|
||||
loop.end_block();
|
||||
}
|
||||
|
||||
void DrawTriangleCodegen::LoopFullBlock(TriDrawVariant variant, bool truecolor)
|
||||
{
|
||||
int pixelsize = truecolor ? 4 : 1;
|
||||
|
||||
stack_iy.store(SSAInt(0));
|
||||
stack_buffer.store(dest);
|
||||
stack_subsectorbuffer.store(subsectorGBuffer);
|
||||
|
||||
SSAForBlock loopy;
|
||||
SSAInt iy = stack_iy.load();
|
||||
SSAUBytePtr buffer = stack_buffer.load();
|
||||
SSAIntPtr subsectorbuffer = stack_subsectorbuffer.load();
|
||||
loopy.loop_block(iy < SSAInt(q), q);
|
||||
{
|
||||
SSAInt varyingStep[TriVertex::NumVarying];
|
||||
for (int i = 0; i < TriVertex::NumVarying; i++)
|
||||
{
|
||||
SSAFloat pos = varyingTL[i] + varyingBL[i] * SSAFloat(iy);
|
||||
SSAFloat step = (varyingTR[i] + varyingBR[i] * SSAFloat(iy) - pos) * (1.0f / q);
|
||||
|
||||
stack_varying[i].store(SSAInt((pos - SSAFloat::floor(pos)) * SSAFloat((float)0x100000000LL), true));
|
||||
varyingStep[i] = SSAInt(step * SSAFloat((float)0x100000000LL), true);
|
||||
}
|
||||
|
||||
stack_ix.store(x);
|
||||
SSAForBlock loopx;
|
||||
SSAInt ix = stack_ix.load();
|
||||
SSAInt varying[TriVertex::NumVarying];
|
||||
for (int i = 0; i < TriVertex::NumVarying; i++)
|
||||
varying[i] = stack_varying[i].load();
|
||||
loopx.loop_block(ix < x + q, q);
|
||||
{
|
||||
SSAIfBlock branch;
|
||||
branch.if_block(subsectorbuffer[ix].load(true) >= subsectorDepth);
|
||||
{
|
||||
if (truecolor)
|
||||
ProcessPixel(buffer[ix * 4], subsectorbuffer[ix], varying, variant, truecolor);
|
||||
else
|
||||
ProcessPixel(buffer[ix], subsectorbuffer[ix], varying, variant, truecolor);
|
||||
}
|
||||
branch.end_block();
|
||||
|
||||
for (int i = 0; i < TriVertex::NumVarying; i++)
|
||||
stack_varying[i].store(varying[i] + varyingStep[i]);
|
||||
|
||||
stack_ix.store(ix + 1);
|
||||
}
|
||||
loopx.end_block();
|
||||
|
||||
stack_buffer.store(buffer[pitch * pixelsize]);
|
||||
stack_subsectorbuffer.store(subsectorbuffer[pitch]);
|
||||
stack_iy.store(iy + 1);
|
||||
}
|
||||
loopy.end_block();
|
||||
}
|
||||
|
||||
void DrawTriangleCodegen::LoopPartialBlock(TriDrawVariant variant, bool truecolor)
|
||||
{
|
||||
int pixelsize = truecolor ? 4 : 1;
|
||||
|
||||
stack_CY1.store(C1 + DX12 * y0 - DY12 * x0);
|
||||
stack_CY2.store(C2 + DX23 * y0 - DY23 * x0);
|
||||
stack_CY3.store(C3 + DX31 * y0 - DY31 * x0);
|
||||
stack_iy.store(SSAInt(0));
|
||||
stack_buffer.store(dest);
|
||||
stack_subsectorbuffer.store(subsectorGBuffer);
|
||||
|
||||
SSAForBlock loopy;
|
||||
SSAInt iy = stack_iy.load();
|
||||
SSAUBytePtr buffer = stack_buffer.load();
|
||||
SSAIntPtr subsectorbuffer = stack_subsectorbuffer.load();
|
||||
SSAInt CY1 = stack_CY1.load();
|
||||
SSAInt CY2 = stack_CY2.load();
|
||||
SSAInt CY3 = stack_CY3.load();
|
||||
loopy.loop_block(iy < SSAInt(q), q);
|
||||
{
|
||||
SSAInt varyingStep[TriVertex::NumVarying];
|
||||
for (int i = 0; i < TriVertex::NumVarying; i++)
|
||||
{
|
||||
SSAFloat pos = varyingTL[i] + varyingBL[i] * SSAFloat(iy);
|
||||
SSAFloat step = (varyingTR[i] + varyingBR[i] * SSAFloat(iy) - pos) * (1.0f / q);
|
||||
|
||||
stack_varying[i].store(SSAInt((pos - SSAFloat::floor(pos)) * SSAFloat((float)0x100000000LL), true));
|
||||
varyingStep[i] = SSAInt(step * SSAFloat((float)0x100000000LL), true);
|
||||
}
|
||||
|
||||
stack_CX1.store(CY1);
|
||||
stack_CX2.store(CY2);
|
||||
stack_CX3.store(CY3);
|
||||
stack_ix.store(SSAInt(0));
|
||||
|
||||
SSAForBlock loopx;
|
||||
SSAInt ix = stack_ix.load();
|
||||
SSAInt CX1 = stack_CX1.load();
|
||||
SSAInt CX2 = stack_CX2.load();
|
||||
SSAInt CX3 = stack_CX3.load();
|
||||
SSAInt varying[TriVertex::NumVarying];
|
||||
for (int i = 0; i < TriVertex::NumVarying; i++)
|
||||
varying[i] = stack_varying[i].load();
|
||||
loopx.loop_block(ix < SSAInt(q), q);
|
||||
{
|
||||
SSABool visible = (ix + x >= clipleft) && (ix + x < clipright) && (cliptop <= y + iy) && (clipbottom > y + iy);
|
||||
|
||||
SSAIfBlock branch;
|
||||
branch.if_block(CX1 > SSAInt(0) && CX2 > SSAInt(0) && CX3 > SSAInt(0) && visible && subsectorbuffer[ix + x].load(true) >= subsectorDepth);
|
||||
{
|
||||
if (truecolor)
|
||||
ProcessPixel(buffer[(ix + x) * 4], subsectorbuffer[ix + x], varying, variant, truecolor);
|
||||
else
|
||||
ProcessPixel(buffer[ix + x], subsectorbuffer[ix + x], varying, variant, truecolor);
|
||||
}
|
||||
branch.end_block();
|
||||
|
||||
for (int i = 0; i < TriVertex::NumVarying; i++)
|
||||
stack_varying[i].store(varying[i] + varyingStep[i]);
|
||||
|
||||
stack_CX1.store(CX1 - FDY12);
|
||||
stack_CX2.store(CX2 - FDY23);
|
||||
stack_CX3.store(CX3 - FDY31);
|
||||
stack_ix.store(ix + 1);
|
||||
}
|
||||
loopx.end_block();
|
||||
|
||||
stack_CY1.store(CY1 + FDX12);
|
||||
stack_CY2.store(CY2 + FDX23);
|
||||
stack_CY3.store(CY3 + FDX31);
|
||||
stack_buffer.store(buffer[pitch * pixelsize]);
|
||||
stack_subsectorbuffer.store(subsectorbuffer[pitch]);
|
||||
stack_iy.store(iy + 1);
|
||||
}
|
||||
loopy.end_block();
|
||||
}
|
||||
|
||||
void DrawTriangleCodegen::ProcessPixel(SSAUBytePtr buffer, SSAIntPtr subsectorbuffer, SSAInt *varying, TriDrawVariant variant, bool truecolor)
|
||||
{
|
||||
SSAInt ufrac = varying[0];
|
||||
SSAInt vfrac = varying[1];
|
||||
|
||||
SSAInt upos = ((ufrac >> 16) * textureWidth) >> 16;
|
||||
SSAInt vpos = ((vfrac >> 16) * textureHeight) >> 16;
|
||||
SSAInt uvoffset = upos * textureHeight + vpos;
|
||||
|
||||
if (truecolor)
|
||||
{
|
||||
SSAVec4i fg = texturePixels[uvoffset * 4].load_vec4ub(true);
|
||||
SSAInt fg_alpha = fg[3];
|
||||
fg = (fg * diminishedlight) >> 8;
|
||||
fg.insert(3, fg_alpha);
|
||||
|
||||
SSAIfBlock branch_transparency;
|
||||
branch_transparency.if_block(fg_alpha > SSAInt(127));
|
||||
{
|
||||
buffer.store_vec4ub(fg);
|
||||
}
|
||||
branch_transparency.end_block();
|
||||
}
|
||||
else
|
||||
{
|
||||
SSAUByte palindex = texturePixels[uvoffset].load(true);
|
||||
SSAIfBlock branch_transparency;
|
||||
branch_transparency.if_block(!(palindex.zext_int() == SSAInt(0)));
|
||||
{
|
||||
buffer.store(palindex);
|
||||
}
|
||||
branch_transparency.end_block();
|
||||
}
|
||||
}
|
||||
|
||||
void DrawTriangleCodegen::LoadArgs(TriDrawVariant variant, bool truecolor, SSAValue args, SSAValue thread_data)
|
||||
{
|
||||
dest = args[0][0].load(true);
|
||||
pitch = args[0][1].load(true);
|
||||
v1 = LoadTriVertex(args[0][2].load(true));
|
||||
v2 = LoadTriVertex(args[0][3].load(true));
|
||||
v3 = LoadTriVertex(args[0][4].load(true));
|
||||
clipleft = args[0][5].load(true);
|
||||
clipright = args[0][6].load(true);
|
||||
cliptop = args[0][7].load(true);
|
||||
clipbottom = args[0][8].load(true);
|
||||
texturePixels = args[0][9].load(true);
|
||||
textureWidth = args[0][10].load(true);
|
||||
textureHeight = args[0][11].load(true);
|
||||
solidcolor = args[0][12].load(true);
|
||||
LoadUniforms(args[0][13].load(true));
|
||||
stencilValues = args[0][14].load(true);
|
||||
stencilMasks = args[0][15].load(true);
|
||||
stencilPitch = args[0][16].load(true);
|
||||
stencilTestValue = args[0][17].load(true);
|
||||
stencilWriteValue = args[0][18].load(true);
|
||||
subsectorGBuffer = args[0][19].load(true);
|
||||
|
||||
thread.core = thread_data[0][0].load(true);
|
||||
thread.num_cores = thread_data[0][1].load(true);
|
||||
}
|
||||
|
||||
SSATriVertex DrawTriangleCodegen::LoadTriVertex(SSAValue ptr)
|
||||
{
|
||||
SSATriVertex v;
|
||||
v.x = ptr[0][0].load(true);
|
||||
v.y = ptr[0][1].load(true);
|
||||
v.z = ptr[0][2].load(true);
|
||||
v.w = ptr[0][3].load(true);
|
||||
for (int i = 0; i < TriVertex::NumVarying; i++)
|
||||
v.varying[i] = ptr[0][4 + i].load(true);
|
||||
return v;
|
||||
}
|
||||
|
||||
void DrawTriangleCodegen::LoadUniforms(SSAValue uniforms)
|
||||
{
|
||||
light = uniforms[0][0].load(true);
|
||||
subsectorDepth = uniforms[0][1].load(true);
|
||||
|
||||
SSAShort light_alpha = uniforms[0][2].load(true);
|
||||
SSAShort light_red = uniforms[0][3].load(true);
|
||||
SSAShort light_green = uniforms[0][4].load(true);
|
||||
SSAShort light_blue = uniforms[0][5].load(true);
|
||||
SSAShort fade_alpha = uniforms[0][6].load(true);
|
||||
SSAShort fade_red = uniforms[0][7].load(true);
|
||||
SSAShort fade_green = uniforms[0][8].load(true);
|
||||
SSAShort fade_blue = uniforms[0][9].load(true);
|
||||
SSAShort desaturate = uniforms[0][10].load(true);
|
||||
SSAInt flags = uniforms[0][11].load(true);
|
||||
shade_constants.light = SSAVec4i(light_blue.zext_int(), light_green.zext_int(), light_red.zext_int(), light_alpha.zext_int());
|
||||
shade_constants.fade = SSAVec4i(fade_blue.zext_int(), fade_green.zext_int(), fade_red.zext_int(), fade_alpha.zext_int());
|
||||
shade_constants.desaturate = desaturate.zext_int();
|
||||
|
||||
is_simple_shade = (flags & TriUniforms::simple_shade) == SSAInt(TriUniforms::simple_shade);
|
||||
is_nearest_filter = (flags & TriUniforms::nearest_filter) == SSAInt(TriUniforms::nearest_filter);
|
||||
}
|
115
src/r_compiler/fixedfunction/drawtrianglecodegen.h
Normal file
115
src/r_compiler/fixedfunction/drawtrianglecodegen.h
Normal file
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
** DrawTriangle code generation
|
||||
** Copyright (c) 2016 Magnus Norddahl
|
||||
**
|
||||
** This software is provided 'as-is', without any express or implied
|
||||
** warranty. In no event will the authors be held liable for any damages
|
||||
** arising from the use of this software.
|
||||
**
|
||||
** Permission is granted to anyone to use this software for any purpose,
|
||||
** including commercial applications, and to alter it and redistribute it
|
||||
** freely, subject to the following restrictions:
|
||||
**
|
||||
** 1. The origin of this software must not be misrepresented; you must not
|
||||
** claim that you wrote the original software. If you use this software
|
||||
** in a product, an acknowledgment in the product documentation would be
|
||||
** appreciated but is not required.
|
||||
** 2. Altered source versions must be plainly marked as such, and must not be
|
||||
** misrepresented as being the original software.
|
||||
** 3. This notice may not be removed or altered from any source distribution.
|
||||
**
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "drawercodegen.h"
|
||||
|
||||
struct SSATriVertex
|
||||
{
|
||||
SSAFloat x, y, z, w;
|
||||
SSAFloat varying[TriVertex::NumVarying];
|
||||
};
|
||||
|
||||
class DrawTriangleCodegen : public DrawerCodegen
|
||||
{
|
||||
public:
|
||||
void Generate(TriDrawVariant variant, bool truecolor, SSAValue args, SSAValue thread_data);
|
||||
|
||||
private:
|
||||
void LoadArgs(TriDrawVariant variant, bool truecolor, SSAValue args, SSAValue thread_data);
|
||||
SSATriVertex LoadTriVertex(SSAValue v);
|
||||
void LoadUniforms(SSAValue uniforms);
|
||||
void Setup(TriDrawVariant variant, bool truecolor);
|
||||
void LoopBlockY(TriDrawVariant variant, bool truecolor);
|
||||
void LoopBlockX(TriDrawVariant variant, bool truecolor);
|
||||
void LoopFullBlock(TriDrawVariant variant, bool truecolor);
|
||||
void LoopPartialBlock(TriDrawVariant variant, bool truecolor);
|
||||
|
||||
void ProcessPixel(SSAUBytePtr buffer, SSAIntPtr subsectorbuffer, SSAInt *varying, TriDrawVariant variant, bool truecolor);
|
||||
|
||||
SSAFloat gradx(SSAFloat x0, SSAFloat y0, SSAFloat x1, SSAFloat y1, SSAFloat x2, SSAFloat y2, SSAFloat c0, SSAFloat c1, SSAFloat c2);
|
||||
SSAFloat grady(SSAFloat x0, SSAFloat y0, SSAFloat x1, SSAFloat y1, SSAFloat x2, SSAFloat y2, SSAFloat c0, SSAFloat c1, SSAFloat c2);
|
||||
|
||||
SSAStack<SSAInt> stack_C1, stack_C2, stack_C3;
|
||||
SSAStack<SSAInt> stack_y;
|
||||
SSAStack<SSAUBytePtr> stack_dest;
|
||||
SSAStack<SSAIntPtr> stack_subsectorGBuffer;
|
||||
SSAStack<SSAInt> stack_x;
|
||||
SSAStack<SSAUBytePtr> stack_buffer;
|
||||
SSAStack<SSAIntPtr> stack_subsectorbuffer;
|
||||
SSAStack<SSAInt> stack_iy, stack_ix;
|
||||
SSAStack<SSAInt> stack_varying[TriVertex::NumVarying];
|
||||
SSAStack<SSAInt> stack_CY1, stack_CY2, stack_CY3;
|
||||
SSAStack<SSAInt> stack_CX1, stack_CX2, stack_CX3;
|
||||
|
||||
SSAUBytePtr dest;
|
||||
SSAInt pitch;
|
||||
SSATriVertex v1;
|
||||
SSATriVertex v2;
|
||||
SSATriVertex v3;
|
||||
SSAInt clipleft;
|
||||
SSAInt clipright;
|
||||
SSAInt cliptop;
|
||||
SSAInt clipbottom;
|
||||
SSAUBytePtr texturePixels;
|
||||
SSAInt textureWidth;
|
||||
SSAInt textureHeight;
|
||||
SSAInt solidcolor;
|
||||
|
||||
SSAInt light;
|
||||
SSAInt subsectorDepth;
|
||||
SSAShadeConstants shade_constants;
|
||||
SSABool is_simple_shade;
|
||||
SSABool is_nearest_filter;
|
||||
|
||||
SSAUBytePtr stencilValues;
|
||||
SSAIntPtr stencilMasks;
|
||||
SSAInt stencilPitch;
|
||||
SSAUByte stencilTestValue;
|
||||
SSAUByte stencilWriteValue;
|
||||
SSAIntPtr subsectorGBuffer;
|
||||
|
||||
SSAWorkerThread thread;
|
||||
|
||||
// Block size, standard 8x8 (must be power of two)
|
||||
const int q = 8;
|
||||
|
||||
SSAInt Y1, Y2, Y3;
|
||||
SSAInt X1, X2, X3;
|
||||
SSAInt DX12, DX23, DX31;
|
||||
SSAInt DY12, DY23, DY31;
|
||||
SSAInt FDX12, FDX23, FDX31;
|
||||
SSAInt FDY12, FDY23, FDY31;
|
||||
SSAInt minx, maxx, miny, maxy;
|
||||
SSAInt C1, C2, C3;
|
||||
SSAFloat gradWX, gradWY, startW;
|
||||
SSAFloat gradVaryingX[TriVertex::NumVarying], gradVaryingY[TriVertex::NumVarying], startVarying[TriVertex::NumVarying];
|
||||
|
||||
SSAInt x, y;
|
||||
SSAInt x0, x1, y0, y1;
|
||||
SSAInt diminishedlight;
|
||||
SSAFloat varyingTL[TriVertex::NumVarying];
|
||||
SSAFloat varyingTR[TriVertex::NumVarying];
|
||||
SSAFloat varyingBL[TriVertex::NumVarying];
|
||||
SSAFloat varyingBR[TriVertex::NumVarying];
|
||||
};
|
|
@ -26,6 +26,7 @@
|
|||
#include "r_compiler/fixedfunction/drawwallcodegen.h"
|
||||
#include "r_compiler/fixedfunction/drawcolumncodegen.h"
|
||||
#include "r_compiler/fixedfunction/drawskycodegen.h"
|
||||
#include "r_compiler/fixedfunction/drawtrianglecodegen.h"
|
||||
#include "r_compiler/ssa/ssa_function.h"
|
||||
#include "r_compiler/ssa/ssa_scope.h"
|
||||
#include "r_compiler/ssa/ssa_for_block.h"
|
||||
|
@ -81,6 +82,7 @@ private:
|
|||
void CodegenDrawSpan(const char *name, DrawSpanVariant variant);
|
||||
void CodegenDrawWall(const char *name, DrawWallVariant variant, int columns);
|
||||
void CodegenDrawSky(const char *name, DrawSkyVariant variant, int columns);
|
||||
void CodegenDrawTriangle(const char *name, TriDrawVariant variant, bool truecolor);
|
||||
|
||||
static llvm::Type *GetDrawColumnArgsStruct(llvm::LLVMContext &context);
|
||||
static llvm::Type *GetDrawSpanArgsStruct(llvm::LLVMContext &context);
|
||||
|
@ -184,6 +186,13 @@ LLVMDrawersImpl::LLVMDrawersImpl()
|
|||
CodegenDrawSky("DrawSky4", DrawSkyVariant::Single, 4);
|
||||
CodegenDrawSky("DrawDoubleSky1", DrawSkyVariant::Double, 1);
|
||||
CodegenDrawSky("DrawDoubleSky4", DrawSkyVariant::Double, 4);
|
||||
CodegenDrawTriangle("TriDraw8", TriDrawVariant::Draw, false);
|
||||
CodegenDrawTriangle("TriDraw32", TriDrawVariant::Draw, true);
|
||||
CodegenDrawTriangle("TriDrawSubsector8", TriDrawVariant::DrawSubsector, false);
|
||||
CodegenDrawTriangle("TriDrawSubsector32", TriDrawVariant::DrawSubsector, true);
|
||||
CodegenDrawTriangle("TriFill8", TriDrawVariant::Fill, false);
|
||||
CodegenDrawTriangle("TriFill32", TriDrawVariant::Fill, true);
|
||||
CodegenDrawTriangle("TriStencil", TriDrawVariant::Stencil, false);
|
||||
|
||||
mProgram.CreateEE();
|
||||
|
||||
|
@ -249,6 +258,13 @@ LLVMDrawersImpl::LLVMDrawersImpl()
|
|||
DrawSky4 = mProgram.GetProcAddress<void(const DrawSkyArgs *, const WorkerThreadData *)>("DrawSky4");
|
||||
DrawDoubleSky1 = mProgram.GetProcAddress<void(const DrawSkyArgs *, const WorkerThreadData *)>("DrawDoubleSky1");
|
||||
DrawDoubleSky4 = mProgram.GetProcAddress<void(const DrawSkyArgs *, const WorkerThreadData *)>("DrawDoubleSky4");
|
||||
TriDraw8 = mProgram.GetProcAddress<void(const TriDrawTriangleArgs *, WorkerThreadData *)>("TriDraw8");
|
||||
TriDraw32 = mProgram.GetProcAddress<void(const TriDrawTriangleArgs *, WorkerThreadData *)>("TriDraw32");
|
||||
TriDrawSubsector8 = mProgram.GetProcAddress<void(const TriDrawTriangleArgs *, WorkerThreadData *)>("TriDrawSubsector8");
|
||||
TriDrawSubsector32 = mProgram.GetProcAddress<void(const TriDrawTriangleArgs *, WorkerThreadData *)>("TriDrawSubsector32");
|
||||
TriFill8 = mProgram.GetProcAddress<void(const TriDrawTriangleArgs *, WorkerThreadData *)>("TriFill8");
|
||||
TriFill32 = mProgram.GetProcAddress<void(const TriDrawTriangleArgs *, WorkerThreadData *)>("TriFill32");
|
||||
TriStencil = mProgram.GetProcAddress<void(const TriDrawTriangleArgs *, WorkerThreadData *)>("TriStencil");
|
||||
|
||||
#if 0
|
||||
std::vector<uint32_t> foo(1024 * 4);
|
||||
|
@ -359,6 +375,25 @@ void LLVMDrawersImpl::CodegenDrawSky(const char *name, DrawSkyVariant variant, i
|
|||
I_FatalError("verifyFunction failed for CodegenDrawSky()");
|
||||
}
|
||||
|
||||
void LLVMDrawersImpl::CodegenDrawTriangle(const char *name, TriDrawVariant variant, bool truecolor)
|
||||
{
|
||||
llvm::IRBuilder<> builder(mProgram.context());
|
||||
SSAScope ssa_scope(&mProgram.context(), mProgram.module(), &builder);
|
||||
|
||||
SSAFunction function(name);
|
||||
function.add_parameter(GetTriDrawTriangleArgs(mProgram.context()));
|
||||
function.add_parameter(GetWorkerThreadDataStruct(mProgram.context()));
|
||||
function.create_public();
|
||||
|
||||
DrawTriangleCodegen codegen;
|
||||
codegen.Generate(variant, truecolor, function.parameter(0), function.parameter(1));
|
||||
|
||||
builder.CreateRetVoid();
|
||||
|
||||
if (llvm::verifyFunction(*function.func))
|
||||
I_FatalError("verifyFunction failed for CodegenDrawTriangle()");
|
||||
}
|
||||
|
||||
llvm::Type *LLVMDrawersImpl::GetDrawColumnArgsStruct(llvm::LLVMContext &context)
|
||||
{
|
||||
std::vector<llvm::Type *> elements;
|
||||
|
@ -468,7 +503,7 @@ llvm::Type *LLVMDrawersImpl::GetWorkerThreadDataStruct(llvm::LLVMContext &contex
|
|||
llvm::Type *LLVMDrawersImpl::GetTriVertexStruct(llvm::LLVMContext &context)
|
||||
{
|
||||
std::vector<llvm::Type *> elements;
|
||||
for (int i = 0; i < 6; i++)
|
||||
for (int i = 0; i < 4 + TriVertex::NumVarying; i++)
|
||||
elements.push_back(llvm::Type::getFloatTy(context));
|
||||
return llvm::StructType::create(context, elements, "TriVertex", false)->getPointerTo();
|
||||
}
|
||||
|
@ -505,9 +540,9 @@ llvm::Type *LLVMDrawersImpl::GetTriDrawTriangleArgs(llvm::LLVMContext &context)
|
|||
std::vector<llvm::Type *> elements;
|
||||
elements.push_back(llvm::Type::getInt8PtrTy(context)); // uint8_t *dest;
|
||||
elements.push_back(llvm::Type::getInt32Ty(context)); // int32_t pitch;
|
||||
elements.push_back(GetTriVertexStruct(context)->getPointerTo()); // TriVertex *v1;
|
||||
elements.push_back(GetTriVertexStruct(context)->getPointerTo()); // TriVertex *v2;
|
||||
elements.push_back(GetTriVertexStruct(context)->getPointerTo()); // TriVertex *v3;
|
||||
elements.push_back(GetTriVertexStruct(context)); // TriVertex *v1;
|
||||
elements.push_back(GetTriVertexStruct(context)); // TriVertex *v2;
|
||||
elements.push_back(GetTriVertexStruct(context)); // TriVertex *v3;
|
||||
elements.push_back(llvm::Type::getInt32Ty(context)); // int32_t clipleft;
|
||||
elements.push_back(llvm::Type::getInt32Ty(context)); // int32_t clipright;
|
||||
elements.push_back(llvm::Type::getInt32Ty(context)); // int32_t cliptop;
|
||||
|
@ -516,7 +551,7 @@ llvm::Type *LLVMDrawersImpl::GetTriDrawTriangleArgs(llvm::LLVMContext &context)
|
|||
elements.push_back(llvm::Type::getInt32Ty(context)); // uint32_t textureWidth;
|
||||
elements.push_back(llvm::Type::getInt32Ty(context)); // uint32_t textureHeight;
|
||||
elements.push_back(llvm::Type::getInt32Ty(context)); // uint32_t solidcolor;
|
||||
elements.push_back(GetTriUniformsStruct(context)->getPointerTo()); // const TriUniforms *uniforms;
|
||||
elements.push_back(GetTriUniformsStruct(context)); // const TriUniforms *uniforms;
|
||||
elements.push_back(llvm::Type::getInt8PtrTy(context)); // uint8_t *stencilValues;
|
||||
elements.push_back(llvm::Type::getInt32PtrTy(context)); // uint32_t *stencilMasks;
|
||||
elements.push_back(llvm::Type::getInt32Ty(context)); // int32_t stencilPitch;
|
||||
|
|
|
@ -260,6 +260,14 @@ struct TriDrawTriangleArgs
|
|||
uint32_t *subsectorGBuffer;
|
||||
};
|
||||
|
||||
enum class TriDrawVariant
|
||||
{
|
||||
Draw,
|
||||
Fill,
|
||||
DrawSubsector,
|
||||
Stencil,
|
||||
};
|
||||
|
||||
class LLVMDrawers
|
||||
{
|
||||
public:
|
||||
|
@ -335,6 +343,14 @@ public:
|
|||
void(*DrawDoubleSky1)(const DrawSkyArgs *, const WorkerThreadData *) = nullptr;
|
||||
void(*DrawDoubleSky4)(const DrawSkyArgs *, const WorkerThreadData *) = nullptr;
|
||||
|
||||
void(*TriDraw8)(const TriDrawTriangleArgs *, WorkerThreadData *) = nullptr;
|
||||
void(*TriDraw32)(const TriDrawTriangleArgs *, WorkerThreadData *) = nullptr;
|
||||
void(*TriDrawSubsector8)(const TriDrawTriangleArgs *, WorkerThreadData *) = nullptr;
|
||||
void(*TriDrawSubsector32)(const TriDrawTriangleArgs *, WorkerThreadData *) = nullptr;
|
||||
void(*TriFill8)(const TriDrawTriangleArgs *, WorkerThreadData *) = nullptr;
|
||||
void(*TriFill32)(const TriDrawTriangleArgs *, WorkerThreadData *) = nullptr;
|
||||
void(*TriStencil)(const TriDrawTriangleArgs *, WorkerThreadData *) = nullptr;
|
||||
|
||||
private:
|
||||
static LLVMDrawers *Singleton;
|
||||
};
|
||||
|
|
|
@ -44,6 +44,11 @@ llvm::Type *SSABool::llvm_type()
|
|||
return llvm::Type::getInt1Ty(SSAScope::context());
|
||||
}
|
||||
|
||||
SSAInt SSABool::zext_int()
|
||||
{
|
||||
return SSAInt::from_llvm(SSAScope::builder().CreateZExt(v, SSAInt::llvm_type(), SSAScope::hint()));
|
||||
}
|
||||
|
||||
SSABool operator&&(const SSABool &a, const SSABool &b)
|
||||
{
|
||||
return SSABool::from_llvm(SSAScope::builder().CreateAnd(a.v, b.v, SSAScope::hint()));
|
||||
|
|
|
@ -37,6 +37,8 @@ public:
|
|||
static SSABool from_llvm(llvm::Value *v) { return SSABool(v); }
|
||||
static llvm::Type *llvm_type();
|
||||
|
||||
SSAInt zext_int();
|
||||
|
||||
llvm::Value *v;
|
||||
};
|
||||
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "ssa_float.h"
|
||||
#include "ssa_int.h"
|
||||
#include "ssa_scope.h"
|
||||
#include "ssa_bool.h"
|
||||
|
||||
SSAFloat::SSAFloat()
|
||||
: v(0)
|
||||
|
@ -111,6 +112,35 @@ SSAFloat SSAFloat::fma(SSAFloat a, SSAFloat b, SSAFloat c)
|
|||
return SSAFloat::from_llvm(SSAScope::builder().CreateCall(SSAScope::intrinsic(llvm::Intrinsic::fma, params), args, SSAScope::hint()));
|
||||
}
|
||||
|
||||
SSAFloat SSAFloat::round(SSAFloat val)
|
||||
{
|
||||
std::vector<llvm::Type *> params;
|
||||
params.push_back(SSAFloat::llvm_type());
|
||||
return SSAFloat::from_llvm(SSAScope::builder().CreateCall(SSAScope::intrinsic(llvm::Intrinsic::round, params), val.v, SSAScope::hint()));
|
||||
}
|
||||
|
||||
SSAFloat SSAFloat::floor(SSAFloat val)
|
||||
{
|
||||
std::vector<llvm::Type *> params;
|
||||
params.push_back(SSAFloat::llvm_type());
|
||||
return SSAFloat::from_llvm(SSAScope::builder().CreateCall(SSAScope::intrinsic(llvm::Intrinsic::floor, params), val.v, SSAScope::hint()));
|
||||
}
|
||||
|
||||
SSAFloat SSAFloat::MIN(SSAFloat a, SSAFloat b)
|
||||
{
|
||||
return SSAFloat::from_llvm(SSAScope::builder().CreateSelect((a < b).v, a.v, b.v, SSAScope::hint()));
|
||||
}
|
||||
|
||||
SSAFloat SSAFloat::MAX(SSAFloat a, SSAFloat b)
|
||||
{
|
||||
return SSAFloat::from_llvm(SSAScope::builder().CreateSelect((a > b).v, a.v, b.v, SSAScope::hint()));
|
||||
}
|
||||
|
||||
SSAFloat SSAFloat::clamp(SSAFloat a, SSAFloat b, SSAFloat c)
|
||||
{
|
||||
return SSAFloat::MAX(SSAFloat::MIN(a, c), b);
|
||||
}
|
||||
|
||||
SSAFloat operator+(const SSAFloat &a, const SSAFloat &b)
|
||||
{
|
||||
return SSAFloat::from_llvm(SSAScope::builder().CreateFAdd(a.v, b.v, SSAScope::hint()));
|
||||
|
|
|
@ -43,6 +43,11 @@ public:
|
|||
static SSAFloat exp(SSAFloat val);
|
||||
static SSAFloat log(SSAFloat val);
|
||||
static SSAFloat fma(SSAFloat a, SSAFloat b, SSAFloat c);
|
||||
static SSAFloat round(SSAFloat val);
|
||||
static SSAFloat floor(SSAFloat val);
|
||||
static SSAFloat MIN(SSAFloat a, SSAFloat b);
|
||||
static SSAFloat MAX(SSAFloat a, SSAFloat b);
|
||||
static SSAFloat clamp(SSAFloat a, SSAFloat b, SSAFloat c);
|
||||
|
||||
llvm::Value *v;
|
||||
};
|
||||
|
|
|
@ -50,3 +50,9 @@ void SSAIfBlock::end_block()
|
|||
SSAScope::builder().CreateBr(end_basic_block);
|
||||
SSAScope::builder().SetInsertPoint(end_basic_block);
|
||||
}
|
||||
|
||||
void SSAIfBlock::end_retvoid()
|
||||
{
|
||||
SSAScope::builder().CreateRetVoid();
|
||||
SSAScope::builder().SetInsertPoint(end_basic_block);
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ public:
|
|||
void if_block(SSABool true_condition);
|
||||
void else_block();
|
||||
void end_block();
|
||||
void end_retvoid();
|
||||
|
||||
private:
|
||||
llvm::BasicBlock *if_basic_block;
|
||||
|
|
|
@ -37,10 +37,13 @@ SSAInt::SSAInt(int constant)
|
|||
v = llvm::ConstantInt::get(SSAScope::context(), llvm::APInt(32, constant, true));
|
||||
}
|
||||
|
||||
SSAInt::SSAInt(SSAFloat f)
|
||||
SSAInt::SSAInt(SSAFloat f, bool uint)
|
||||
: v(0)
|
||||
{
|
||||
v = SSAScope::builder().CreateFPToSI(f.v, llvm::Type::getInt32Ty(SSAScope::context()), SSAScope::hint());
|
||||
if (uint)
|
||||
v = SSAScope::builder().CreateFPToUI(f.v, llvm::Type::getInt32Ty(SSAScope::context()), SSAScope::hint());
|
||||
else
|
||||
v = SSAScope::builder().CreateFPToSI(f.v, llvm::Type::getInt32Ty(SSAScope::context()), SSAScope::hint());
|
||||
}
|
||||
|
||||
SSAInt::SSAInt(llvm::Value *v)
|
||||
|
@ -63,6 +66,11 @@ SSAInt SSAInt::MAX(SSAInt a, SSAInt b)
|
|||
return SSAInt::from_llvm(SSAScope::builder().CreateSelect((a > b).v, a.v, b.v, SSAScope::hint()));
|
||||
}
|
||||
|
||||
SSAInt SSAInt::clamp(SSAInt a, SSAInt b, SSAInt c)
|
||||
{
|
||||
return SSAInt::MAX(SSAInt::MIN(a, c), b);
|
||||
}
|
||||
|
||||
SSAInt SSAInt::add(SSAInt b, bool no_unsigned_wrap, bool no_signed_wrap)
|
||||
{
|
||||
return SSAInt::from_llvm(SSAScope::builder().CreateAdd(v, b.v, SSAScope::hint(), no_unsigned_wrap, no_signed_wrap));
|
||||
|
|
|
@ -32,13 +32,14 @@ class SSAInt
|
|||
public:
|
||||
SSAInt();
|
||||
explicit SSAInt(int constant);
|
||||
SSAInt(SSAFloat f);
|
||||
SSAInt(SSAFloat f, bool uint);
|
||||
explicit SSAInt(llvm::Value *v);
|
||||
static SSAInt from_llvm(llvm::Value *v) { return SSAInt(v); }
|
||||
static llvm::Type *llvm_type();
|
||||
|
||||
static SSAInt MIN(SSAInt a, SSAInt b);
|
||||
static SSAInt MAX(SSAInt a, SSAInt b);
|
||||
static SSAInt clamp(SSAInt a, SSAInt b, SSAInt c);
|
||||
|
||||
SSAInt add(SSAInt b, bool no_unsigned_wrap, bool no_signed_wrap);
|
||||
SSAInt ashr(int bits);
|
||||
|
|
|
@ -56,7 +56,7 @@ llvm::IRBuilder<> &SSAScope::builder()
|
|||
|
||||
llvm::Function *SSAScope::intrinsic(llvm::Intrinsic::ID id, llvm::ArrayRef<llvm::Type *> parameter_types)
|
||||
{
|
||||
llvm::Function *func = module()->getFunction(llvm::Intrinsic::getName(id));
|
||||
llvm::Function *func = module()->getFunction(llvm::Intrinsic::getName(id, parameter_types));
|
||||
if (func == 0)
|
||||
func = llvm::Function::Create(llvm::Intrinsic::getType(context(), id, parameter_types), llvm::Function::ExternalLinkage, llvm::Intrinsic::getName(id, parameter_types), module());
|
||||
return func;
|
||||
|
|
|
@ -222,13 +222,13 @@ void RenderPolyBsp::RenderPlane(subsector_t *sub, uint32_t subsectorDepth, bool
|
|||
if (!isSky)
|
||||
{
|
||||
args.SetTexture(tex);
|
||||
PolyTriangleDrawer::draw(args, PolyDrawVariant::Draw);
|
||||
PolyTriangleDrawer::draw(args, PolyDrawVariant::Stencil);
|
||||
PolyTriangleDrawer::draw(args, TriDrawVariant::Draw);
|
||||
PolyTriangleDrawer::draw(args, TriDrawVariant::Stencil);
|
||||
}
|
||||
else
|
||||
{
|
||||
args.stencilwritevalue = 255;
|
||||
PolyTriangleDrawer::draw(args, PolyDrawVariant::Stencil);
|
||||
PolyTriangleDrawer::draw(args, TriDrawVariant::Stencil);
|
||||
|
||||
for (uint32_t i = 0; i < sub->numlines; i++)
|
||||
{
|
||||
|
@ -313,7 +313,7 @@ void RenderPolyBsp::RenderPlane(subsector_t *sub, uint32_t subsectorDepth, bool
|
|||
|
||||
args.vinput = wallvert;
|
||||
args.vcount = 4;
|
||||
PolyTriangleDrawer::draw(args, PolyDrawVariant::Stencil);
|
||||
PolyTriangleDrawer::draw(args, TriDrawVariant::Stencil);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -568,7 +568,7 @@ void RenderPolyBsp::AddSprite(AActor *thing, subsector_t *sub, uint32_t subsecto
|
|||
args.stenciltestvalue = 0;
|
||||
args.stencilwritevalue = 1;
|
||||
args.SetTexture(tex);
|
||||
PolyTriangleDrawer::draw(args, PolyDrawVariant::DrawSubsector);
|
||||
PolyTriangleDrawer::draw(args, TriDrawVariant::DrawSubsector);
|
||||
}
|
||||
|
||||
void RenderPolyBsp::AddWallSprite(AActor *thing, subsector_t *sub, uint32_t subsectorDepth)
|
||||
|
@ -1234,8 +1234,8 @@ void RenderPolyWall::Render(const TriMatrix &worldToClip)
|
|||
args.stencilwritevalue = 1;
|
||||
args.SetTexture(tex);
|
||||
|
||||
PolyTriangleDrawer::draw(args, PolyDrawVariant::Draw);
|
||||
PolyTriangleDrawer::draw(args, PolyDrawVariant::Stencil);
|
||||
PolyTriangleDrawer::draw(args, TriDrawVariant::Draw);
|
||||
PolyTriangleDrawer::draw(args, TriDrawVariant::Stencil);
|
||||
}
|
||||
|
||||
FTexture *RenderPolyWall::GetTexture()
|
||||
|
@ -1567,7 +1567,7 @@ void PolySkyDome::RenderRow(PolyDrawArgs &args, int row)
|
|||
args.vcount = mPrimStart[row + 1] - mPrimStart[row];
|
||||
args.mode = TriangleDrawMode::Strip;
|
||||
args.ccw = false;
|
||||
PolyTriangleDrawer::draw(args, PolyDrawVariant::Draw);
|
||||
PolyTriangleDrawer::draw(args, TriDrawVariant::Draw);
|
||||
}
|
||||
|
||||
void PolySkyDome::RenderCapColorRow(PolyDrawArgs &args, FTexture *skytex, int row, bool bottomCap)
|
||||
|
@ -1581,7 +1581,7 @@ void PolySkyDome::RenderCapColorRow(PolyDrawArgs &args, FTexture *skytex, int ro
|
|||
args.mode = TriangleDrawMode::Fan;
|
||||
args.ccw = bottomCap;
|
||||
args.solidcolor = solid;
|
||||
PolyTriangleDrawer::draw(args, PolyDrawVariant::Fill);
|
||||
PolyTriangleDrawer::draw(args, TriDrawVariant::Fill);
|
||||
}
|
||||
|
||||
void PolySkyDome::Render(const TriMatrix &worldToClip)
|
||||
|
|
|
@ -106,7 +106,7 @@ public:
|
|||
void Render();
|
||||
void RenderScreenSprites();
|
||||
|
||||
static const uint32_t SkySubsectorDepth = 0xffffffff;
|
||||
static const uint32_t SkySubsectorDepth = 0x7fffffff;
|
||||
|
||||
private:
|
||||
void RenderNode(void *node);
|
||||
|
|
|
@ -40,7 +40,7 @@
|
|||
#include <immintrin.h>
|
||||
#endif
|
||||
|
||||
void PolyTriangleDrawer::draw(const PolyDrawArgs &args, PolyDrawVariant variant)
|
||||
void PolyTriangleDrawer::draw(const PolyDrawArgs &args, TriDrawVariant variant)
|
||||
{
|
||||
if (r_swtruecolor)
|
||||
DrawerCommandQueue::QueueCommand<DrawPolyTrianglesCommand>(args, variant);
|
||||
|
@ -48,19 +48,20 @@ void PolyTriangleDrawer::draw(const PolyDrawArgs &args, PolyDrawVariant variant)
|
|||
draw_arrays(args, variant, nullptr);
|
||||
}
|
||||
|
||||
void PolyTriangleDrawer::draw_arrays(const PolyDrawArgs &drawargs, PolyDrawVariant variant, WorkerThreadData *thread)
|
||||
void PolyTriangleDrawer::draw_arrays(const PolyDrawArgs &drawargs, TriDrawVariant variant, WorkerThreadData *thread)
|
||||
{
|
||||
if (drawargs.vcount < 3)
|
||||
return;
|
||||
|
||||
auto llvm = LLVMDrawers::Instance();
|
||||
void(*drawfunc)(const TriDrawTriangleArgs *, WorkerThreadData *);
|
||||
switch (variant)
|
||||
{
|
||||
default:
|
||||
case PolyDrawVariant::Draw: drawfunc = r_swtruecolor ? ScreenPolyTriangleDrawer::draw32 : ScreenPolyTriangleDrawer::draw; break;
|
||||
case PolyDrawVariant::Fill: drawfunc = r_swtruecolor ? ScreenPolyTriangleDrawer::fill32 : ScreenPolyTriangleDrawer::fill; break;
|
||||
case PolyDrawVariant::DrawSubsector: drawfunc = r_swtruecolor ? ScreenPolyTriangleDrawer::drawsubsector32 : ScreenPolyTriangleDrawer::draw; break;
|
||||
case PolyDrawVariant::Stencil: drawfunc = ScreenPolyTriangleDrawer::stencil; break;
|
||||
case TriDrawVariant::Draw: drawfunc = r_swtruecolor ? ScreenPolyTriangleDrawer::draw32 : ScreenPolyTriangleDrawer::draw; break;
|
||||
case TriDrawVariant::Fill: drawfunc = r_swtruecolor ? ScreenPolyTriangleDrawer::fill32 : ScreenPolyTriangleDrawer::fill; break;
|
||||
case TriDrawVariant::DrawSubsector: drawfunc = r_swtruecolor ? llvm->TriDrawSubsector32 : llvm->TriDrawSubsector8; break;
|
||||
case TriDrawVariant::Stencil: drawfunc = ScreenPolyTriangleDrawer::stencil; break;
|
||||
}
|
||||
|
||||
TriDrawTriangleArgs args;
|
||||
|
@ -1596,7 +1597,7 @@ float ScreenPolyTriangleDrawer::grady(float x0, float y0, float x1, float y1, fl
|
|||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
DrawPolyTrianglesCommand::DrawPolyTrianglesCommand(const PolyDrawArgs &args, PolyDrawVariant variant)
|
||||
DrawPolyTrianglesCommand::DrawPolyTrianglesCommand(const PolyDrawArgs &args, TriDrawVariant variant)
|
||||
: args(args), variant(variant)
|
||||
{
|
||||
}
|
||||
|
|
|
@ -28,14 +28,6 @@
|
|||
|
||||
struct TriDrawTriangleArgs;
|
||||
|
||||
enum class PolyDrawVariant
|
||||
{
|
||||
Draw,
|
||||
Fill,
|
||||
DrawSubsector,
|
||||
Stencil,
|
||||
};
|
||||
|
||||
class PolyDrawArgs
|
||||
{
|
||||
public:
|
||||
|
@ -69,11 +61,11 @@ public:
|
|||
class PolyTriangleDrawer
|
||||
{
|
||||
public:
|
||||
static void draw(const PolyDrawArgs &args, PolyDrawVariant variant);
|
||||
static void draw(const PolyDrawArgs &args, TriDrawVariant variant);
|
||||
|
||||
private:
|
||||
static TriVertex shade_vertex(const TriUniforms &uniforms, TriVertex v);
|
||||
static void draw_arrays(const PolyDrawArgs &args, PolyDrawVariant variant, WorkerThreadData *thread);
|
||||
static void draw_arrays(const PolyDrawArgs &args, TriDrawVariant variant, WorkerThreadData *thread);
|
||||
static void draw_shaded_triangle(const TriVertex *vertices, bool ccw, TriDrawTriangleArgs *args, WorkerThreadData *thread, void(*drawfunc)(const TriDrawTriangleArgs *, WorkerThreadData *));
|
||||
static bool cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2);
|
||||
static void clipedge(const TriVertex *verts, TriVertex *clippedvert, int &numclipvert);
|
||||
|
@ -245,14 +237,14 @@ private:
|
|||
class DrawPolyTrianglesCommand : public DrawerCommand
|
||||
{
|
||||
public:
|
||||
DrawPolyTrianglesCommand(const PolyDrawArgs &args, PolyDrawVariant variant);
|
||||
DrawPolyTrianglesCommand(const PolyDrawArgs &args, TriDrawVariant variant);
|
||||
|
||||
void Execute(DrawerThread *thread) override;
|
||||
FString DebugInfo() override;
|
||||
|
||||
private:
|
||||
PolyDrawArgs args;
|
||||
PolyDrawVariant variant;
|
||||
TriDrawVariant variant;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue