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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef __R_POLY_TRIANGLE__
|
|
|
|
#define __R_POLY_TRIANGLE__
|
|
|
|
|
|
|
|
#include "r_triangle.h"
|
|
|
|
|
|
|
|
struct ScreenPolyTriangleDrawerArgs;
|
|
|
|
|
|
|
|
class PolyTriangleDrawer
|
|
|
|
{
|
|
|
|
public:
|
2016-11-10 09:44:35 +00:00
|
|
|
static void draw(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, int cliptop, int clipbottom, FTexture *texture, int stenciltestvalue);
|
|
|
|
static void fill(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, int cliptop, int clipbottom, int solidcolor, int stenciltestvalue);
|
|
|
|
static void stencil(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, int cliptop, int clipbottom, int stenciltestvalue, int stencilwritevalue);
|
2016-11-08 13:28:58 +00:00
|
|
|
|
|
|
|
private:
|
2016-11-08 15:16:24 +00:00
|
|
|
static TriVertex shade_vertex(const TriUniforms &uniforms, TriVertex v);
|
2016-11-10 09:44:35 +00:00
|
|
|
static void draw_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, int cliptop, int clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor, int stenciltestvalue, int stencilwritevalue, DrawerThread *thread, void(*drawfunc)(const ScreenPolyTriangleDrawerArgs *, DrawerThread *));
|
2016-11-08 13:28:58 +00:00
|
|
|
static void draw_shaded_triangle(const TriVertex *vertices, bool ccw, ScreenPolyTriangleDrawerArgs *args, DrawerThread *thread, void(*drawfunc)(const ScreenPolyTriangleDrawerArgs *, DrawerThread *));
|
|
|
|
static bool cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2);
|
2016-11-08 22:08:25 +00:00
|
|
|
static void clipedge(const TriVertex *verts, TriVertex *clippedvert, int &numclipvert);
|
2016-11-08 13:28:58 +00:00
|
|
|
|
2016-11-10 09:44:35 +00:00
|
|
|
static void queue_arrays(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, int cliptop, int clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor, int stenciltestvalue, int stencilwritevalue);
|
2016-11-08 13:28:58 +00:00
|
|
|
|
2016-11-10 04:30:33 +00:00
|
|
|
enum { max_additional_vertices = 16 };
|
|
|
|
|
2016-11-08 13:28:58 +00:00
|
|
|
friend class DrawPolyTrianglesCommand;
|
|
|
|
};
|
|
|
|
|
2016-11-10 07:08:37 +00:00
|
|
|
// 8x8 block of stencil values, plus a mask indicating if values are the same for early out stencil testing
|
|
|
|
class PolyStencilBlock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PolyStencilBlock(int block, uint8_t *values, uint32_t *masks) : Values(values + block * 64), ValueMask(masks[block])
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Set(int x, int y, uint8_t value)
|
|
|
|
{
|
|
|
|
if (ValueMask == 0xffffffff)
|
|
|
|
{
|
|
|
|
if (Values[0] == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (int i = 1; i < 8 * 8 + 4 * 4 + 2 * 2 + 1; i++)
|
|
|
|
Values[i] = Values[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Values[x + y * 8] == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Values[x + y * 8] = value;
|
|
|
|
|
|
|
|
int leveloffset = 0;
|
|
|
|
for (int i = 1; i < 4; i++)
|
|
|
|
{
|
|
|
|
x >>= 1;
|
|
|
|
y >>= 1;
|
|
|
|
|
|
|
|
bool same =
|
2016-11-10 09:44:35 +00:00
|
|
|
Values[(x << i) + (y << i) * 8] != value ||
|
|
|
|
Values[((x + 1) << i) + (y << i) * 8] != value ||
|
|
|
|
Values[(x << i) + ((y + 1) << i) * 8] != value ||
|
|
|
|
Values[((x + 1) << i) + ((y + 1) << i) * 8] != value;
|
2016-11-10 07:08:37 +00:00
|
|
|
|
|
|
|
int levelbit = 1 << (leveloffset + x + y * (8 >> i));
|
|
|
|
|
|
|
|
if (same)
|
|
|
|
ValueMask = ValueMask & ~levelbit;
|
|
|
|
else
|
|
|
|
ValueMask = ValueMask | levelbit;
|
2016-11-10 09:44:35 +00:00
|
|
|
|
|
|
|
leveloffset += (8 >> leveloffset) * (8 >> leveloffset);
|
2016-11-10 07:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Values[0] != value || Values[4] != value || Values[4 * 8] != value || Values[4 * 8 + 4] != value)
|
|
|
|
ValueMask = ValueMask & ~(1 << 22);
|
|
|
|
else
|
|
|
|
ValueMask = ValueMask | (1 << 22);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t Get(int x, int y) const
|
|
|
|
{
|
|
|
|
if (ValueMask == 0xffffffff)
|
|
|
|
return Values[0];
|
|
|
|
else
|
|
|
|
return Values[x + y * 8];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear(uint8_t value)
|
|
|
|
{
|
|
|
|
Values[0] = value;
|
|
|
|
ValueMask = 0xffffffff;
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:35 +00:00
|
|
|
bool IsSingleValue() const
|
|
|
|
{
|
|
|
|
return ValueMask == 0xffffffff;
|
|
|
|
}
|
|
|
|
|
2016-11-10 07:08:37 +00:00
|
|
|
private:
|
|
|
|
uint8_t *Values; // [8 * 8];
|
|
|
|
uint32_t &ValueMask; // 4 * 4 + 2 * 2 + 1 bits indicating is Values are the same
|
|
|
|
};
|
|
|
|
|
2016-11-10 12:58:03 +00:00
|
|
|
class PolySubsectorGBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PolySubsectorGBuffer *Instance()
|
|
|
|
{
|
|
|
|
static PolySubsectorGBuffer buffer;
|
|
|
|
return &buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resize(int newwidth, int newheight)
|
|
|
|
{
|
|
|
|
width = newwidth;
|
|
|
|
height = newheight;
|
|
|
|
values.resize(width * height);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Width() const { return width; }
|
|
|
|
int Height() const { return height; }
|
|
|
|
uint32_t *Values() { return values.data(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
std::vector<uint32_t> values;
|
|
|
|
};
|
|
|
|
|
2016-11-10 07:08:37 +00:00
|
|
|
class PolyStencilBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static PolyStencilBuffer *Instance()
|
|
|
|
{
|
|
|
|
static PolyStencilBuffer buffer;
|
|
|
|
return &buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear(int newwidth, int newheight, uint8_t stencil_value = 0)
|
|
|
|
{
|
|
|
|
width = newwidth;
|
|
|
|
height = newheight;
|
|
|
|
int count = BlockWidth() * BlockHeight();
|
|
|
|
values.resize(count * 64);
|
|
|
|
masks.resize(count);
|
|
|
|
|
|
|
|
uint8_t *v = Values();
|
|
|
|
uint32_t *m = Masks();
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
PolyStencilBlock block(i, v, m);
|
|
|
|
block.Clear(stencil_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Width() const { return width; }
|
|
|
|
int Height() const { return height; }
|
|
|
|
int BlockWidth() const { return (width + 7) / 8; }
|
|
|
|
int BlockHeight() const { return (height + 7) / 8; }
|
|
|
|
uint8_t *Values() { return values.data(); }
|
|
|
|
uint32_t *Masks() { return masks.data(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
std::vector<uint8_t> values;
|
|
|
|
std::vector<uint32_t> masks;
|
|
|
|
};
|
|
|
|
|
2016-11-08 13:28:58 +00:00
|
|
|
struct ScreenPolyTriangleDrawerArgs
|
|
|
|
{
|
|
|
|
uint8_t *dest;
|
|
|
|
int pitch;
|
|
|
|
TriVertex *v1;
|
|
|
|
TriVertex *v2;
|
|
|
|
TriVertex *v3;
|
|
|
|
int clipleft;
|
|
|
|
int clipright;
|
|
|
|
int cliptop;
|
|
|
|
int clipbottom;
|
|
|
|
const uint8_t *texturePixels;
|
|
|
|
int textureWidth;
|
|
|
|
int textureHeight;
|
|
|
|
int solidcolor;
|
2016-11-08 15:16:24 +00:00
|
|
|
const TriUniforms *uniforms;
|
2016-11-10 09:44:35 +00:00
|
|
|
uint8_t *stencilValues;
|
|
|
|
uint32_t *stencilMasks;
|
|
|
|
int stencilPitch;
|
|
|
|
uint8_t stencilTestValue;
|
|
|
|
uint8_t stencilWriteValue;
|
2016-11-10 12:58:03 +00:00
|
|
|
uint32_t *subsectorGBuffer;
|
2016-11-08 13:28:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ScreenPolyTriangleDrawer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void draw(const ScreenPolyTriangleDrawerArgs *args, DrawerThread *thread);
|
|
|
|
static void fill(const ScreenPolyTriangleDrawerArgs *args, DrawerThread *thread);
|
|
|
|
|
2016-11-10 09:44:35 +00:00
|
|
|
static void stencil(const ScreenPolyTriangleDrawerArgs *args, DrawerThread *thread);
|
|
|
|
|
2016-11-08 13:28:58 +00:00
|
|
|
static void draw32(const ScreenPolyTriangleDrawerArgs *args, DrawerThread *thread);
|
|
|
|
static void fill32(const ScreenPolyTriangleDrawerArgs *args, DrawerThread *thread);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static float gradx(float x0, float y0, float x1, float y1, float x2, float y2, float c0, float c1, float c2);
|
|
|
|
static float grady(float x0, float y0, float x1, float y1, float x2, float y2, float c0, float c1, float c2);
|
|
|
|
};
|
|
|
|
|
|
|
|
class DrawPolyTrianglesCommand : public DrawerCommand
|
|
|
|
{
|
|
|
|
public:
|
2016-11-10 09:44:35 +00:00
|
|
|
DrawPolyTrianglesCommand(const TriUniforms &uniforms, const TriVertex *vinput, int vcount, TriangleDrawMode mode, bool ccw, int clipleft, int clipright, int cliptop, int clipbottom, const uint8_t *texturePixels, int textureWidth, int textureHeight, int solidcolor, int stenciltestvalue, int stencilwritevalue);
|
2016-11-08 13:28:58 +00:00
|
|
|
|
|
|
|
void Execute(DrawerThread *thread) override;
|
|
|
|
FString DebugInfo() override;
|
|
|
|
|
|
|
|
private:
|
2016-11-08 15:16:24 +00:00
|
|
|
TriUniforms uniforms;
|
2016-11-08 13:28:58 +00:00
|
|
|
const TriVertex *vinput;
|
|
|
|
int vcount;
|
|
|
|
TriangleDrawMode mode;
|
|
|
|
bool ccw;
|
|
|
|
int clipleft;
|
|
|
|
int clipright;
|
|
|
|
int cliptop;
|
|
|
|
int clipbottom;
|
|
|
|
const uint8_t *texturePixels;
|
|
|
|
int textureWidth;
|
|
|
|
int textureHeight;
|
|
|
|
int solidcolor;
|
2016-11-10 09:44:35 +00:00
|
|
|
int stenciltestvalue;
|
|
|
|
int stencilwritevalue;
|
2016-11-08 13:28:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|