2016-11-08 13:28:58 +00:00
|
|
|
/*
|
|
|
|
** Triangle drawers
|
|
|
|
** 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 <stddef.h>
|
|
|
|
#include "templates.h"
|
|
|
|
#include "doomdef.h"
|
|
|
|
#include "i_system.h"
|
|
|
|
#include "w_wad.h"
|
|
|
|
#include "v_video.h"
|
|
|
|
#include "doomstat.h"
|
|
|
|
#include "st_stuff.h"
|
|
|
|
#include "g_game.h"
|
|
|
|
#include "g_level.h"
|
|
|
|
#include "r_data/r_translate.h"
|
|
|
|
#include "v_palette.h"
|
|
|
|
#include "r_data/colormaps.h"
|
2016-12-27 05:31:55 +00:00
|
|
|
#include "poly_triangle.h"
|
2017-02-04 11:38:05 +00:00
|
|
|
#include "polyrenderer/poly_renderer.h"
|
2016-12-27 05:31:55 +00:00
|
|
|
#include "swrenderer/drawers/r_draw_rgba.h"
|
2016-12-30 01:20:24 +00:00
|
|
|
#include "screen_triangle.h"
|
2017-04-22 19:16:59 +00:00
|
|
|
#include "x86.h"
|
2016-11-08 13:28:58 +00:00
|
|
|
|
2016-11-18 00:58:39 +00:00
|
|
|
int PolyTriangleDrawer::viewport_x;
|
|
|
|
int PolyTriangleDrawer::viewport_y;
|
|
|
|
int PolyTriangleDrawer::viewport_width;
|
|
|
|
int PolyTriangleDrawer::viewport_height;
|
|
|
|
int PolyTriangleDrawer::dest_pitch;
|
|
|
|
int PolyTriangleDrawer::dest_width;
|
|
|
|
int PolyTriangleDrawer::dest_height;
|
|
|
|
uint8_t *PolyTriangleDrawer::dest;
|
|
|
|
bool PolyTriangleDrawer::dest_bgra;
|
2016-12-07 21:26:18 +00:00
|
|
|
bool PolyTriangleDrawer::mirror;
|
2016-11-18 00:58:39 +00:00
|
|
|
|
|
|
|
void PolyTriangleDrawer::set_viewport(int x, int y, int width, int height, DCanvas *canvas)
|
|
|
|
{
|
|
|
|
dest = (uint8_t*)canvas->GetBuffer();
|
|
|
|
dest_width = canvas->GetWidth();
|
|
|
|
dest_height = canvas->GetHeight();
|
|
|
|
dest_pitch = canvas->GetPitch();
|
|
|
|
dest_bgra = canvas->IsBgra();
|
|
|
|
|
|
|
|
int offsetx = clamp(x, 0, dest_width);
|
|
|
|
int offsety = clamp(y, 0, dest_height);
|
|
|
|
int pixelsize = dest_bgra ? 4 : 1;
|
|
|
|
|
|
|
|
viewport_x = x - offsetx;
|
|
|
|
viewport_y = y - offsety;
|
|
|
|
viewport_width = width;
|
|
|
|
viewport_height = height;
|
|
|
|
|
|
|
|
dest += (offsetx + offsety * dest_pitch) * pixelsize;
|
|
|
|
dest_width = clamp(viewport_x + viewport_width, 0, dest_width - offsetx);
|
|
|
|
dest_height = clamp(viewport_y + viewport_height, 0, dest_height - offsety);
|
2016-12-07 21:26:18 +00:00
|
|
|
|
|
|
|
mirror = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PolyTriangleDrawer::toggle_mirror()
|
|
|
|
{
|
|
|
|
mirror = !mirror;
|
2016-11-18 00:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 08:10:55 +00:00
|
|
|
bool PolyTriangleDrawer::is_mirror()
|
2016-11-08 13:28:58 +00:00
|
|
|
{
|
2017-03-26 08:10:55 +00:00
|
|
|
return mirror;
|
2016-11-08 13:28:58 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 23:35:45 +00:00
|
|
|
void PolyTriangleDrawer::draw_arrays(const PolyDrawArgs &drawargs, WorkerThreadData *thread)
|
2016-11-08 13:28:58 +00:00
|
|
|
{
|
2017-03-26 08:10:55 +00:00
|
|
|
if (drawargs.VertexCount() < 3)
|
2016-11-08 13:28:58 +00:00
|
|
|
return;
|
|
|
|
|
2016-11-11 19:12:09 +00:00
|
|
|
TriDrawTriangleArgs args;
|
2016-11-18 00:58:39 +00:00
|
|
|
args.dest = dest;
|
|
|
|
args.pitch = dest_pitch;
|
|
|
|
args.clipright = dest_width;
|
|
|
|
args.clipbottom = dest_height;
|
2017-03-26 08:10:55 +00:00
|
|
|
args.uniforms = &drawargs;
|
2017-04-01 01:44:45 +00:00
|
|
|
args.destBgra = dest_bgra;
|
2016-11-10 09:44:35 +00:00
|
|
|
args.stencilPitch = PolyStencilBuffer::Instance()->BlockWidth();
|
|
|
|
args.stencilValues = PolyStencilBuffer::Instance()->Values();
|
|
|
|
args.stencilMasks = PolyStencilBuffer::Instance()->Masks();
|
2017-08-12 13:16:31 +00:00
|
|
|
args.zbuffer = PolyZBuffer::Instance()->Values();
|
2016-11-08 13:28:58 +00:00
|
|
|
|
2017-03-26 08:10:55 +00:00
|
|
|
bool ccw = drawargs.FaceCullCCW();
|
|
|
|
const TriVertex *vinput = drawargs.Vertices();
|
|
|
|
int vcount = drawargs.VertexCount();
|
2016-11-11 17:24:59 +00:00
|
|
|
|
2016-11-26 09:49:29 +00:00
|
|
|
ShadedTriVertex vert[3];
|
2017-03-26 08:10:55 +00:00
|
|
|
if (drawargs.DrawMode() == PolyDrawMode::Triangles)
|
2016-11-08 13:28:58 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < vcount / 3; i++)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < 3; j++)
|
2017-08-27 21:26:54 +00:00
|
|
|
vert[j] = shade_vertex(drawargs, *(vinput++));
|
2017-04-01 01:44:45 +00:00
|
|
|
draw_shaded_triangle(vert, ccw, &args, thread);
|
2016-11-08 13:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-26 08:10:55 +00:00
|
|
|
else if (drawargs.DrawMode() == PolyDrawMode::TriangleFan)
|
2016-11-08 13:28:58 +00:00
|
|
|
{
|
2017-08-27 21:26:54 +00:00
|
|
|
vert[0] = shade_vertex(drawargs, *(vinput++));
|
|
|
|
vert[1] = shade_vertex(drawargs, *(vinput++));
|
2016-11-08 13:28:58 +00:00
|
|
|
for (int i = 2; i < vcount; i++)
|
|
|
|
{
|
2017-08-27 21:26:54 +00:00
|
|
|
vert[2] = shade_vertex(drawargs, *(vinput++));
|
2017-04-01 01:44:45 +00:00
|
|
|
draw_shaded_triangle(vert, ccw, &args, thread);
|
2016-11-08 13:28:58 +00:00
|
|
|
vert[1] = vert[2];
|
|
|
|
}
|
|
|
|
}
|
2017-03-26 08:10:55 +00:00
|
|
|
else // TriangleDrawMode::TriangleStrip
|
2016-11-08 13:28:58 +00:00
|
|
|
{
|
2017-08-27 21:26:54 +00:00
|
|
|
vert[0] = shade_vertex(drawargs, *(vinput++));
|
|
|
|
vert[1] = shade_vertex(drawargs, *(vinput++));
|
2016-11-08 13:28:58 +00:00
|
|
|
for (int i = 2; i < vcount; i++)
|
|
|
|
{
|
2017-08-27 21:26:54 +00:00
|
|
|
vert[2] = shade_vertex(drawargs, *(vinput++));
|
2017-04-01 01:44:45 +00:00
|
|
|
draw_shaded_triangle(vert, ccw, &args, thread);
|
2016-11-08 13:28:58 +00:00
|
|
|
vert[0] = vert[1];
|
|
|
|
vert[1] = vert[2];
|
|
|
|
ccw = !ccw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-27 21:26:54 +00:00
|
|
|
ShadedTriVertex PolyTriangleDrawer::shade_vertex(const PolyDrawArgs &drawargs, const TriVertex &v)
|
2016-11-08 13:28:58 +00:00
|
|
|
{
|
2017-08-27 21:26:54 +00:00
|
|
|
const TriMatrix &objectToClip = *drawargs.ObjectToClip();
|
|
|
|
|
2016-11-08 13:28:58 +00:00
|
|
|
// Apply transform to get clip coordinates:
|
2017-08-27 21:26:54 +00:00
|
|
|
ShadedTriVertex sv;
|
|
|
|
sv.position = objectToClip * v;
|
2016-11-26 09:49:29 +00:00
|
|
|
|
2017-08-27 21:26:54 +00:00
|
|
|
// Calculate gl_ClipDistance[i]
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
const auto &clipPlane = drawargs.ClipPlane(i);
|
|
|
|
sv.clipDistance[i] = v.x * clipPlane.A + v.y * clipPlane.B + v.z * clipPlane.C + v.w * clipPlane.D;
|
|
|
|
}
|
2016-11-26 09:49:29 +00:00
|
|
|
|
|
|
|
return sv;
|
2016-11-08 13:28:58 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 00:43:00 +00:00
|
|
|
bool PolyTriangleDrawer::is_degenerate(const ShadedTriVertex *vert)
|
|
|
|
{
|
|
|
|
// A degenerate triangle has a zero cross product for two of its sides.
|
2017-08-27 21:26:54 +00:00
|
|
|
float ax = vert[1].position.x - vert[0].position.x;
|
|
|
|
float ay = vert[1].position.y - vert[0].position.y;
|
|
|
|
float az = vert[1].position.w - vert[0].position.w;
|
|
|
|
float bx = vert[2].position.x - vert[0].position.x;
|
|
|
|
float by = vert[2].position.y - vert[0].position.y;
|
|
|
|
float bz = vert[2].position.w - vert[0].position.w;
|
2017-05-10 00:43:00 +00:00
|
|
|
float crossx = ay * bz - az * by;
|
|
|
|
float crossy = az * bx - ax * bz;
|
|
|
|
float crossz = ax * by - ay * bx;
|
|
|
|
float crosslengthsqr = crossx * crossx + crossy * crossy + crossz * crossz;
|
|
|
|
return crosslengthsqr <= 1.e-6f;
|
|
|
|
}
|
|
|
|
|
2017-04-23 08:39:07 +00:00
|
|
|
void PolyTriangleDrawer::draw_shaded_triangle(const ShadedTriVertex *vert, bool ccw, TriDrawTriangleArgs *args, WorkerThreadData *thread)
|
2016-11-08 13:28:58 +00:00
|
|
|
{
|
2017-05-10 00:43:00 +00:00
|
|
|
// Reject triangle if degenerate
|
|
|
|
if (is_degenerate(vert))
|
|
|
|
return;
|
|
|
|
|
2017-04-23 08:39:07 +00:00
|
|
|
// Cull, clip and generate additional vertices as needed
|
|
|
|
TriVertex clippedvert[max_additional_vertices];
|
|
|
|
int numclipvert = clipedge(vert, clippedvert);
|
|
|
|
|
|
|
|
#ifdef NO_SSE
|
|
|
|
// Map to 2D viewport:
|
2016-11-08 13:28:58 +00:00
|
|
|
for (int j = 0; j < numclipvert; j++)
|
|
|
|
{
|
|
|
|
auto &v = clippedvert[j];
|
|
|
|
|
|
|
|
// Calculate normalized device coordinates:
|
|
|
|
v.w = 1.0f / v.w;
|
|
|
|
v.x *= v.w;
|
|
|
|
v.y *= v.w;
|
|
|
|
v.z *= v.w;
|
|
|
|
|
|
|
|
// Apply viewport scale to get screen coordinates:
|
2016-11-18 00:58:39 +00:00
|
|
|
v.x = viewport_x + viewport_width * (1.0f + v.x) * 0.5f;
|
|
|
|
v.y = viewport_y + viewport_height * (1.0f - v.y) * 0.5f;
|
2016-11-08 13:28:58 +00:00
|
|
|
}
|
2017-04-23 08:39:07 +00:00
|
|
|
#else
|
|
|
|
// Map to 2D viewport:
|
2017-03-24 19:00:53 +00:00
|
|
|
__m128 mviewport_x = _mm_set1_ps((float)viewport_x);
|
|
|
|
__m128 mviewport_y = _mm_set1_ps((float)viewport_y);
|
|
|
|
__m128 mviewport_halfwidth = _mm_set1_ps(viewport_width * 0.5f);
|
|
|
|
__m128 mviewport_halfheight = _mm_set1_ps(viewport_height * 0.5f);
|
|
|
|
__m128 mone = _mm_set1_ps(1.0f);
|
|
|
|
int sse_length = (numclipvert + 3) / 4 * 4;
|
|
|
|
for (int j = 0; j < sse_length; j += 4)
|
|
|
|
{
|
|
|
|
__m128 vx = _mm_loadu_ps(&clippedvert[j].x);
|
|
|
|
__m128 vy = _mm_loadu_ps(&clippedvert[j + 1].x);
|
|
|
|
__m128 vz = _mm_loadu_ps(&clippedvert[j + 2].x);
|
|
|
|
__m128 vw = _mm_loadu_ps(&clippedvert[j + 3].x);
|
|
|
|
_MM_TRANSPOSE4_PS(vx, vy, vz, vw);
|
|
|
|
|
|
|
|
// Calculate normalized device coordinates:
|
|
|
|
vw = _mm_div_ps(mone, vw);
|
|
|
|
vx = _mm_mul_ps(vx, vw);
|
|
|
|
vy = _mm_mul_ps(vy, vw);
|
|
|
|
vz = _mm_mul_ps(vz, vw);
|
|
|
|
|
|
|
|
// Apply viewport scale to get screen coordinates:
|
|
|
|
vx = _mm_add_ps(mviewport_x, _mm_mul_ps(mviewport_halfwidth, _mm_add_ps(mone, vx)));
|
|
|
|
vy = _mm_add_ps(mviewport_y, _mm_mul_ps(mviewport_halfheight, _mm_sub_ps(mone, vy)));
|
|
|
|
|
|
|
|
_MM_TRANSPOSE4_PS(vx, vy, vz, vw);
|
|
|
|
_mm_storeu_ps(&clippedvert[j].x, vx);
|
|
|
|
_mm_storeu_ps(&clippedvert[j + 1].x, vy);
|
|
|
|
_mm_storeu_ps(&clippedvert[j + 2].x, vz);
|
|
|
|
_mm_storeu_ps(&clippedvert[j + 3].x, vw);
|
|
|
|
}
|
|
|
|
#endif
|
2016-11-08 13:28:58 +00:00
|
|
|
|
2016-12-12 20:34:22 +00:00
|
|
|
// Keep varyings in -128 to 128 range if possible
|
2017-05-05 01:27:22 +00:00
|
|
|
// But don't do this for the skycap mode since the V texture coordinate is used for blending
|
|
|
|
if (numclipvert > 0 && args->uniforms->BlendMode() != TriBlendMode::Skycap)
|
2016-12-12 20:34:22 +00:00
|
|
|
{
|
2017-03-28 08:52:19 +00:00
|
|
|
float newOriginU = floorf(clippedvert[0].u * 0.1f) * 10.0f;
|
|
|
|
float newOriginV = floorf(clippedvert[0].v * 0.1f) * 10.0f;
|
|
|
|
for (int i = 0; i < numclipvert; i++)
|
2016-12-12 20:34:22 +00:00
|
|
|
{
|
2017-03-28 08:52:19 +00:00
|
|
|
clippedvert[i].u -= newOriginU;
|
|
|
|
clippedvert[i].v -= newOriginV;
|
2016-12-12 20:34:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 13:28:58 +00:00
|
|
|
// Draw screen triangles
|
|
|
|
if (ccw)
|
|
|
|
{
|
2017-05-10 00:43:00 +00:00
|
|
|
for (int i = numclipvert - 1; i > 1; i--)
|
2016-11-08 13:28:58 +00:00
|
|
|
{
|
|
|
|
args->v1 = &clippedvert[numclipvert - 1];
|
|
|
|
args->v2 = &clippedvert[i - 1];
|
|
|
|
args->v3 = &clippedvert[i - 2];
|
2017-05-10 00:43:00 +00:00
|
|
|
if (args->CalculateGradients())
|
|
|
|
ScreenTriangle::Draw(args, thread);
|
2016-11-08 13:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 2; i < numclipvert; i++)
|
|
|
|
{
|
|
|
|
args->v1 = &clippedvert[0];
|
|
|
|
args->v2 = &clippedvert[i - 1];
|
|
|
|
args->v3 = &clippedvert[i];
|
2017-05-10 00:43:00 +00:00
|
|
|
if (args->CalculateGradients())
|
|
|
|
ScreenTriangle::Draw(args, thread);
|
2016-11-08 13:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:04:02 +00:00
|
|
|
int PolyTriangleDrawer::clipedge(const ShadedTriVertex *verts, TriVertex *clippedvert)
|
2016-11-08 13:28:58 +00:00
|
|
|
{
|
|
|
|
// Clip and cull so that the following is true for all vertices:
|
|
|
|
// -v.w <= v.x <= v.w
|
|
|
|
// -v.w <= v.y <= v.w
|
|
|
|
// -v.w <= v.z <= v.w
|
2016-11-08 22:08:25 +00:00
|
|
|
|
2017-03-24 12:04:02 +00:00
|
|
|
// halfspace clip distances
|
2017-08-27 21:26:54 +00:00
|
|
|
static const int numclipdistances = 9;
|
2017-04-23 08:39:07 +00:00
|
|
|
#ifdef NO_SSE
|
2017-03-24 12:04:02 +00:00
|
|
|
float clipdistance[numclipdistances * 3];
|
|
|
|
bool needsclipping = false;
|
|
|
|
float *clipd = clipdistance;
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2017-08-27 21:26:54 +00:00
|
|
|
const auto &v = verts[i].position;
|
2017-03-24 12:04:02 +00:00
|
|
|
clipd[0] = v.x + v.w;
|
|
|
|
clipd[1] = v.w - v.x;
|
|
|
|
clipd[2] = v.y + v.w;
|
|
|
|
clipd[3] = v.w - v.y;
|
|
|
|
clipd[4] = v.z + v.w;
|
|
|
|
clipd[5] = v.w - v.z;
|
2017-08-27 21:26:54 +00:00
|
|
|
clipd[6] = verts[i].clipDistance[0];
|
|
|
|
clipd[7] = verts[i].clipDistance[1];
|
|
|
|
clipd[8] = verts[i].clipDistance[2];
|
|
|
|
for (int j = 0; j < 9; j++)
|
|
|
|
needsclipping = needsclipping || clipd[i];
|
2017-03-25 05:40:06 +00:00
|
|
|
clipd += numclipdistances;
|
2017-03-24 12:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If all halfspace clip distances are positive then the entire triangle is visible. Skip the expensive clipping step.
|
|
|
|
if (!needsclipping)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2017-08-27 21:26:54 +00:00
|
|
|
memcpy(clippedvert + i, &verts[i].position, sizeof(TriVertex));
|
2017-03-24 12:04:02 +00:00
|
|
|
}
|
|
|
|
return 3;
|
|
|
|
}
|
2017-04-23 08:39:07 +00:00
|
|
|
#else
|
2017-08-27 21:26:54 +00:00
|
|
|
__m128 mx = _mm_loadu_ps(&verts[0].position.x);
|
|
|
|
__m128 my = _mm_loadu_ps(&verts[1].position.x);
|
|
|
|
__m128 mz = _mm_loadu_ps(&verts[2].position.x);
|
2017-03-24 12:04:02 +00:00
|
|
|
__m128 mw = _mm_setzero_ps();
|
|
|
|
_MM_TRANSPOSE4_PS(mx, my, mz, mw);
|
|
|
|
__m128 clipd0 = _mm_add_ps(mx, mw);
|
|
|
|
__m128 clipd1 = _mm_sub_ps(mw, mx);
|
|
|
|
__m128 clipd2 = _mm_add_ps(my, mw);
|
|
|
|
__m128 clipd3 = _mm_sub_ps(mw, my);
|
|
|
|
__m128 clipd4 = _mm_add_ps(mz, mw);
|
|
|
|
__m128 clipd5 = _mm_sub_ps(mw, mz);
|
2017-08-27 21:26:54 +00:00
|
|
|
__m128 clipd6 = _mm_setr_ps(verts[0].clipDistance[0], verts[1].clipDistance[0], verts[2].clipDistance[0], 0.0f);
|
|
|
|
__m128 clipd7 = _mm_setr_ps(verts[0].clipDistance[1], verts[1].clipDistance[1], verts[2].clipDistance[1], 0.0f);
|
|
|
|
__m128 clipd8 = _mm_setr_ps(verts[0].clipDistance[2], verts[1].clipDistance[2], verts[2].clipDistance[2], 0.0f);
|
2017-03-24 12:04:02 +00:00
|
|
|
__m128 mneedsclipping = _mm_cmplt_ps(clipd0, _mm_setzero_ps());
|
|
|
|
mneedsclipping = _mm_or_ps(mneedsclipping, _mm_cmplt_ps(clipd1, _mm_setzero_ps()));
|
|
|
|
mneedsclipping = _mm_or_ps(mneedsclipping, _mm_cmplt_ps(clipd2, _mm_setzero_ps()));
|
|
|
|
mneedsclipping = _mm_or_ps(mneedsclipping, _mm_cmplt_ps(clipd3, _mm_setzero_ps()));
|
|
|
|
mneedsclipping = _mm_or_ps(mneedsclipping, _mm_cmplt_ps(clipd4, _mm_setzero_ps()));
|
|
|
|
mneedsclipping = _mm_or_ps(mneedsclipping, _mm_cmplt_ps(clipd5, _mm_setzero_ps()));
|
|
|
|
mneedsclipping = _mm_or_ps(mneedsclipping, _mm_cmplt_ps(clipd6, _mm_setzero_ps()));
|
2017-08-27 21:26:54 +00:00
|
|
|
mneedsclipping = _mm_or_ps(mneedsclipping, _mm_cmplt_ps(clipd7, _mm_setzero_ps()));
|
|
|
|
mneedsclipping = _mm_or_ps(mneedsclipping, _mm_cmplt_ps(clipd8, _mm_setzero_ps()));
|
2017-03-24 12:04:02 +00:00
|
|
|
if (_mm_movemask_ps(mneedsclipping) == 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2017-08-27 21:26:54 +00:00
|
|
|
memcpy(clippedvert + i, &verts[i].position, sizeof(TriVertex));
|
2017-03-24 12:04:02 +00:00
|
|
|
}
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
float clipdistance[numclipdistances * 4];
|
|
|
|
_mm_storeu_ps(clipdistance, clipd0);
|
|
|
|
_mm_storeu_ps(clipdistance + 4, clipd1);
|
|
|
|
_mm_storeu_ps(clipdistance + 8, clipd2);
|
|
|
|
_mm_storeu_ps(clipdistance + 12, clipd3);
|
|
|
|
_mm_storeu_ps(clipdistance + 16, clipd4);
|
|
|
|
_mm_storeu_ps(clipdistance + 20, clipd5);
|
|
|
|
_mm_storeu_ps(clipdistance + 24, clipd6);
|
2017-08-27 21:26:54 +00:00
|
|
|
_mm_storeu_ps(clipdistance + 28, clipd7);
|
|
|
|
_mm_storeu_ps(clipdistance + 32, clipd8);
|
2017-04-23 08:39:07 +00:00
|
|
|
#endif
|
2017-03-24 12:04:02 +00:00
|
|
|
|
2016-11-08 22:08:25 +00:00
|
|
|
// use barycentric weights while clipping vertices
|
2016-11-10 04:30:33 +00:00
|
|
|
float weights[max_additional_vertices * 3 * 2];
|
2016-11-08 22:08:25 +00:00
|
|
|
for (int i = 0; i < 3; i++)
|
2016-11-08 13:28:58 +00:00
|
|
|
{
|
2016-11-08 22:08:25 +00:00
|
|
|
weights[i * 3 + 0] = 0.0f;
|
|
|
|
weights[i * 3 + 1] = 0.0f;
|
|
|
|
weights[i * 3 + 2] = 0.0f;
|
|
|
|
weights[i * 3 + i] = 1.0f;
|
2016-11-08 13:28:58 +00:00
|
|
|
}
|
2017-03-24 12:04:02 +00:00
|
|
|
|
2016-11-08 22:08:25 +00:00
|
|
|
// Clip against each halfspace
|
|
|
|
float *input = weights;
|
2016-11-10 04:30:33 +00:00
|
|
|
float *output = weights + max_additional_vertices * 3;
|
2016-11-08 22:08:25 +00:00
|
|
|
int inputverts = 3;
|
2016-11-26 09:49:29 +00:00
|
|
|
for (int p = 0; p < numclipdistances; p++)
|
2016-11-08 22:08:25 +00:00
|
|
|
{
|
|
|
|
// Clip each edge
|
2017-03-24 12:04:02 +00:00
|
|
|
int outputverts = 0;
|
2016-11-08 22:08:25 +00:00
|
|
|
for (int i = 0; i < inputverts; i++)
|
|
|
|
{
|
|
|
|
int j = (i + 1) % inputverts;
|
2017-04-23 08:39:07 +00:00
|
|
|
#ifdef NO_SSE
|
|
|
|
float clipdistance1 =
|
|
|
|
clipdistance[0 * numclipdistances + p] * input[i * 3 + 0] +
|
|
|
|
clipdistance[1 * numclipdistances + p] * input[i * 3 + 1] +
|
|
|
|
clipdistance[2 * numclipdistances + p] * input[i * 3 + 2];
|
|
|
|
|
|
|
|
float clipdistance2 =
|
|
|
|
clipdistance[0 * numclipdistances + p] * input[j * 3 + 0] +
|
|
|
|
clipdistance[1 * numclipdistances + p] * input[j * 3 + 1] +
|
|
|
|
clipdistance[2 * numclipdistances + p] * input[j * 3 + 2];
|
|
|
|
#else
|
2017-03-24 12:04:02 +00:00
|
|
|
float clipdistance1 =
|
|
|
|
clipdistance[0 + p * 4] * input[i * 3 + 0] +
|
|
|
|
clipdistance[1 + p * 4] * input[i * 3 + 1] +
|
|
|
|
clipdistance[2 + p * 4] * input[i * 3 + 2];
|
|
|
|
|
|
|
|
float clipdistance2 =
|
|
|
|
clipdistance[0 + p * 4] * input[j * 3 + 0] +
|
|
|
|
clipdistance[1 + p * 4] * input[j * 3 + 1] +
|
|
|
|
clipdistance[2 + p * 4] * input[j * 3 + 2];
|
2017-04-23 08:39:07 +00:00
|
|
|
#endif
|
2017-03-24 12:04:02 +00:00
|
|
|
|
|
|
|
// Clip halfspace
|
|
|
|
if ((clipdistance1 >= 0.0f || clipdistance2 >= 0.0f) && outputverts + 1 < max_additional_vertices)
|
2016-11-08 22:08:25 +00:00
|
|
|
{
|
2017-03-24 12:04:02 +00:00
|
|
|
float t1 = (clipdistance1 < 0.0f) ? MAX(-clipdistance1 / (clipdistance2 - clipdistance1), 0.0f) : 0.0f;
|
|
|
|
float t2 = (clipdistance2 < 0.0f) ? MIN(1.0f + clipdistance2 / (clipdistance1 - clipdistance2), 1.0f) : 1.0f;
|
|
|
|
|
2016-11-08 22:08:25 +00:00
|
|
|
// add t1 vertex
|
|
|
|
for (int k = 0; k < 3; k++)
|
|
|
|
output[outputverts * 3 + k] = input[i * 3 + k] * (1.0f - t1) + input[j * 3 + k] * t1;
|
|
|
|
outputverts++;
|
2017-04-23 08:39:07 +00:00
|
|
|
|
2016-11-08 22:08:25 +00:00
|
|
|
if (t2 != 1.0f && t2 > t1)
|
|
|
|
{
|
|
|
|
// add t2 vertex
|
|
|
|
for (int k = 0; k < 3; k++)
|
|
|
|
output[outputverts * 3 + k] = input[i * 3 + k] * (1.0f - t2) + input[j * 3 + k] * t2;
|
|
|
|
outputverts++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::swap(input, output);
|
2017-03-24 12:04:02 +00:00
|
|
|
inputverts = outputverts;
|
2016-11-08 22:08:25 +00:00
|
|
|
if (inputverts == 0)
|
|
|
|
break;
|
|
|
|
}
|
2017-04-23 08:39:07 +00:00
|
|
|
|
2016-11-08 22:08:25 +00:00
|
|
|
// Convert barycentric weights to actual vertices
|
2017-03-24 12:04:02 +00:00
|
|
|
for (int i = 0; i < inputverts; i++)
|
2016-11-08 13:28:58 +00:00
|
|
|
{
|
2016-11-08 22:08:25 +00:00
|
|
|
auto &v = clippedvert[i];
|
|
|
|
memset(&v, 0, sizeof(TriVertex));
|
|
|
|
for (int w = 0; w < 3; w++)
|
|
|
|
{
|
|
|
|
float weight = input[i * 3 + w];
|
2017-08-27 21:26:54 +00:00
|
|
|
v.x += verts[w].position.x * weight;
|
|
|
|
v.y += verts[w].position.y * weight;
|
|
|
|
v.z += verts[w].position.z * weight;
|
|
|
|
v.w += verts[w].position.w * weight;
|
|
|
|
v.u += verts[w].position.u * weight;
|
|
|
|
v.v += verts[w].position.v * weight;
|
2016-11-08 22:08:25 +00:00
|
|
|
}
|
2016-11-08 13:28:58 +00:00
|
|
|
}
|
2017-03-24 12:04:02 +00:00
|
|
|
return inputverts;
|
2016-11-08 13:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-12-15 23:35:45 +00:00
|
|
|
DrawPolyTrianglesCommand::DrawPolyTrianglesCommand(const PolyDrawArgs &args, bool mirror)
|
|
|
|
: args(args)
|
2016-11-08 13:28:58 +00:00
|
|
|
{
|
2016-12-07 21:26:18 +00:00
|
|
|
if (mirror)
|
2017-03-26 08:10:55 +00:00
|
|
|
this->args.SetFaceCullCCW(!this->args.FaceCullCCW());
|
2016-11-08 13:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawPolyTrianglesCommand::Execute(DrawerThread *thread)
|
|
|
|
{
|
2016-11-11 18:54:27 +00:00
|
|
|
WorkerThreadData thread_data;
|
|
|
|
thread_data.core = thread->core;
|
|
|
|
thread_data.num_cores = thread->num_cores;
|
|
|
|
|
2016-12-15 23:35:45 +00:00
|
|
|
PolyTriangleDrawer::draw_arrays(args, &thread_data);
|
2016-11-08 13:28:58 +00:00
|
|
|
}
|
2017-03-28 14:46:22 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void DrawRectCommand::Execute(DrawerThread *thread)
|
|
|
|
{
|
|
|
|
WorkerThreadData thread_data;
|
|
|
|
thread_data.core = thread->core;
|
|
|
|
thread_data.num_cores = thread->num_cores;
|
|
|
|
|
|
|
|
auto renderTarget = PolyRenderer::Instance()->RenderTarget;
|
|
|
|
const void *destOrg = renderTarget->GetBuffer();
|
|
|
|
int destWidth = renderTarget->GetWidth();
|
|
|
|
int destHeight = renderTarget->GetHeight();
|
|
|
|
int destPitch = renderTarget->GetPitch();
|
|
|
|
int blendmode = (int)args.BlendMode();
|
|
|
|
if (renderTarget->IsBgra())
|
|
|
|
ScreenTriangle::RectDrawers32[blendmode](destOrg, destWidth, destHeight, destPitch, &args, &thread_data);
|
|
|
|
else
|
|
|
|
ScreenTriangle::RectDrawers8[blendmode](destOrg, destWidth, destHeight, destPitch, &args, &thread_data);
|
|
|
|
}
|