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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2016-11-20 00:51:08 +00:00
|
|
|
#pragma once
|
2016-11-08 13:28:58 +00:00
|
|
|
|
2016-11-20 01:07:55 +00:00
|
|
|
#include "r_draw.h"
|
|
|
|
#include "r_thread.h"
|
2016-11-28 16:31:56 +00:00
|
|
|
#include "r_drawers.h"
|
2016-11-19 12:32:57 +00:00
|
|
|
#include "r_data/r_translate.h"
|
2016-11-20 03:06:21 +00:00
|
|
|
#include "r_data/colormaps.h"
|
2016-11-08 13:28:58 +00:00
|
|
|
|
2016-11-20 01:07:55 +00:00
|
|
|
class FTexture;
|
|
|
|
|
|
|
|
enum class TriangleDrawMode
|
|
|
|
{
|
|
|
|
Normal,
|
|
|
|
Fan,
|
|
|
|
Strip
|
|
|
|
};
|
|
|
|
|
2016-11-11 19:12:09 +00:00
|
|
|
struct TriDrawTriangleArgs;
|
2016-11-26 09:49:29 +00:00
|
|
|
struct TriMatrix;
|
2016-11-08 13:28:58 +00:00
|
|
|
|
2016-11-11 17:24:59 +00:00
|
|
|
class PolyDrawArgs
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TriUniforms uniforms;
|
2016-11-20 01:07:55 +00:00
|
|
|
const TriMatrix *objectToClip = nullptr;
|
2016-11-11 17:24:59 +00:00
|
|
|
const TriVertex *vinput = nullptr;
|
|
|
|
int vcount = 0;
|
|
|
|
TriangleDrawMode mode = TriangleDrawMode::Normal;
|
|
|
|
bool ccw = false;
|
|
|
|
const uint8_t *texturePixels = nullptr;
|
|
|
|
int textureWidth = 0;
|
|
|
|
int textureHeight = 0;
|
2016-11-19 01:53:32 +00:00
|
|
|
const uint8_t *translation = nullptr;
|
2016-11-11 18:26:28 +00:00
|
|
|
uint8_t stenciltestvalue = 0;
|
|
|
|
uint8_t stencilwritevalue = 0;
|
2016-11-20 03:06:21 +00:00
|
|
|
const uint8_t *colormaps = nullptr;
|
2016-11-26 09:49:29 +00:00
|
|
|
float clipPlane[4];
|
|
|
|
|
|
|
|
void SetClipPlane(float a, float b, float c, float d)
|
|
|
|
{
|
|
|
|
clipPlane[0] = a;
|
|
|
|
clipPlane[1] = b;
|
|
|
|
clipPlane[2] = c;
|
|
|
|
clipPlane[3] = d;
|
|
|
|
}
|
2016-11-11 17:24:59 +00:00
|
|
|
|
|
|
|
void SetTexture(FTexture *texture)
|
|
|
|
{
|
|
|
|
textureWidth = texture->GetWidth();
|
|
|
|
textureHeight = texture->GetHeight();
|
2016-12-01 01:38:32 +00:00
|
|
|
if (swrenderer::r_swtruecolor)
|
2016-11-11 17:24:59 +00:00
|
|
|
texturePixels = (const uint8_t *)texture->GetPixelsBgra();
|
|
|
|
else
|
|
|
|
texturePixels = texture->GetPixels();
|
2016-11-19 12:32:57 +00:00
|
|
|
translation = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-21 04:52:02 +00:00
|
|
|
void SetTexture(FTexture *texture, uint32_t translationID, bool forcePal = false)
|
2016-11-19 12:32:57 +00:00
|
|
|
{
|
2016-11-21 04:09:53 +00:00
|
|
|
if (translationID != 0xffffffff && translationID != 0)
|
2016-11-19 12:32:57 +00:00
|
|
|
{
|
|
|
|
FRemapTable *table = TranslationToTable(translationID);
|
|
|
|
if (table != nullptr && !table->Inactive)
|
|
|
|
{
|
2016-12-01 01:38:32 +00:00
|
|
|
if (swrenderer::r_swtruecolor)
|
2016-11-19 12:32:57 +00:00
|
|
|
translation = (uint8_t*)table->Palette;
|
|
|
|
else
|
|
|
|
translation = table->Remap;
|
|
|
|
|
|
|
|
textureWidth = texture->GetWidth();
|
|
|
|
textureHeight = texture->GetHeight();
|
|
|
|
texturePixels = texture->GetPixels();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-11-21 04:52:02 +00:00
|
|
|
|
|
|
|
if (forcePal)
|
|
|
|
{
|
|
|
|
textureWidth = texture->GetWidth();
|
|
|
|
textureHeight = texture->GetHeight();
|
|
|
|
texturePixels = texture->GetPixels();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetTexture(texture);
|
|
|
|
}
|
2016-11-11 17:24:59 +00:00
|
|
|
}
|
2016-11-20 03:06:21 +00:00
|
|
|
|
|
|
|
void SetColormap(FSWColormap *base_colormap)
|
|
|
|
{
|
|
|
|
uniforms.light_red = base_colormap->Color.r * 256 / 255;
|
|
|
|
uniforms.light_green = base_colormap->Color.g * 256 / 255;
|
|
|
|
uniforms.light_blue = base_colormap->Color.b * 256 / 255;
|
|
|
|
uniforms.light_alpha = base_colormap->Color.a * 256 / 255;
|
|
|
|
uniforms.fade_red = base_colormap->Fade.r;
|
|
|
|
uniforms.fade_green = base_colormap->Fade.g;
|
|
|
|
uniforms.fade_blue = base_colormap->Fade.b;
|
|
|
|
uniforms.fade_alpha = base_colormap->Fade.a;
|
|
|
|
uniforms.desaturate = MIN(abs(base_colormap->Desaturate), 255) * 255 / 256;
|
|
|
|
bool simple_shade = (base_colormap->Color.d == 0x00ffffff && base_colormap->Fade.d == 0x00000000 && base_colormap->Desaturate == 0);
|
|
|
|
if (simple_shade)
|
|
|
|
uniforms.flags |= TriUniforms::simple_shade;
|
|
|
|
else
|
|
|
|
uniforms.flags &= ~TriUniforms::simple_shade;
|
|
|
|
colormaps = base_colormap->Maps;
|
|
|
|
}
|
2016-11-11 17:24:59 +00:00
|
|
|
};
|
|
|
|
|
2016-11-26 09:49:29 +00:00
|
|
|
struct ShadedTriVertex : public TriVertex
|
|
|
|
{
|
|
|
|
float clipDistance0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TriMatrix
|
|
|
|
{
|
|
|
|
static TriMatrix null();
|
|
|
|
static TriMatrix identity();
|
|
|
|
static TriMatrix translate(float x, float y, float z);
|
|
|
|
static TriMatrix scale(float x, float y, float z);
|
|
|
|
static TriMatrix rotate(float angle, float x, float y, float z);
|
|
|
|
static TriMatrix swapYZ();
|
|
|
|
static TriMatrix perspective(float fovy, float aspect, float near, float far);
|
|
|
|
static TriMatrix frustum(float left, float right, float bottom, float top, float near, float far);
|
|
|
|
|
|
|
|
static TriMatrix worldToView(); // Software renderer world to view space transform
|
|
|
|
static TriMatrix viewToClip(); // Software renderer shearing projection
|
|
|
|
|
|
|
|
ShadedTriVertex operator*(TriVertex v) const;
|
|
|
|
TriMatrix operator*(const TriMatrix &m) const;
|
|
|
|
|
|
|
|
float matrix[16];
|
|
|
|
};
|
|
|
|
|
2016-11-08 13:28:58 +00:00
|
|
|
class PolyTriangleDrawer
|
|
|
|
{
|
|
|
|
public:
|
2016-11-18 00:58:39 +00:00
|
|
|
static void set_viewport(int x, int y, int width, int height, DCanvas *canvas);
|
2016-11-19 01:53:32 +00:00
|
|
|
static void draw(const PolyDrawArgs &args, TriDrawVariant variant, TriBlendMode blendmode);
|
2016-12-07 21:26:18 +00:00
|
|
|
static void toggle_mirror();
|
2016-11-08 13:28:58 +00:00
|
|
|
|
|
|
|
private:
|
2016-11-26 09:49:29 +00:00
|
|
|
static ShadedTriVertex shade_vertex(const TriMatrix &objectToClip, const float *clipPlane, const TriVertex &v);
|
2016-11-19 01:53:32 +00:00
|
|
|
static void draw_arrays(const PolyDrawArgs &args, TriDrawVariant variant, TriBlendMode blendmode, WorkerThreadData *thread);
|
2016-11-26 09:49:29 +00:00
|
|
|
static void draw_shaded_triangle(const ShadedTriVertex *vertices, bool ccw, TriDrawTriangleArgs *args, WorkerThreadData *thread, void(*drawfunc)(const TriDrawTriangleArgs *, WorkerThreadData *));
|
2016-11-08 13:28:58 +00:00
|
|
|
static bool cullhalfspace(float clipdistance1, float clipdistance2, float &t1, float &t2);
|
2016-11-26 09:49:29 +00:00
|
|
|
static void clipedge(const ShadedTriVertex *verts, TriVertex *clippedvert, int &numclipvert);
|
2016-11-08 13:28:58 +00:00
|
|
|
|
2016-11-18 00:58:39 +00:00
|
|
|
static int viewport_x, viewport_y, viewport_width, viewport_height, dest_pitch, dest_width, dest_height;
|
|
|
|
static bool dest_bgra;
|
|
|
|
static uint8_t *dest;
|
2016-12-07 21:26:18 +00:00
|
|
|
static bool mirror;
|
2016-11-18 00:58:39 +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 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++)
|
|
|
|
{
|
2016-11-20 00:51:08 +00:00
|
|
|
m[i] = 0xffffff00 | stencil_value;
|
2016-11-10 07:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-11-20 00:51:08 +00:00
|
|
|
|
|
|
|
// 8x8 blocks of stencil values, plus a mask for each block indicating if values are the same for early out stencil testing
|
2016-11-10 07:08:37 +00:00
|
|
|
std::vector<uint8_t> values;
|
|
|
|
std::vector<uint32_t> masks;
|
|
|
|
};
|
|
|
|
|
2016-11-08 13:28:58 +00:00
|
|
|
class DrawPolyTrianglesCommand : public DrawerCommand
|
|
|
|
{
|
|
|
|
public:
|
2016-12-07 21:26:18 +00:00
|
|
|
DrawPolyTrianglesCommand(const PolyDrawArgs &args, TriDrawVariant variant, TriBlendMode blendmode, bool mirror);
|
2016-11-08 13:28:58 +00:00
|
|
|
|
|
|
|
void Execute(DrawerThread *thread) override;
|
|
|
|
FString DebugInfo() override;
|
|
|
|
|
|
|
|
private:
|
2016-11-11 17:24:59 +00:00
|
|
|
PolyDrawArgs args;
|
2016-11-11 23:50:21 +00:00
|
|
|
TriDrawVariant variant;
|
2016-11-19 01:53:32 +00:00
|
|
|
TriBlendMode blendmode;
|
2016-11-08 13:28:58 +00:00
|
|
|
};
|
2016-11-24 04:51:37 +00:00
|
|
|
|
|
|
|
class PolyVertexBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static TriVertex *GetVertices(int count);
|
|
|
|
static void Clear();
|
|
|
|
};
|