2013-04-17 21:09:55 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2013-02-21 18:10:00 -07:00
|
|
|
#include <cstddef>
|
|
|
|
|
2013-02-22 02:22:20 -07:00
|
|
|
#include "Common.h"
|
2013-03-29 07:27:33 -06:00
|
|
|
#include "VideoConfig.h"
|
2008-12-07 22:25:12 -07:00
|
|
|
#include "IndexGenerator.h"
|
|
|
|
|
2009-10-06 08:24:10 -06:00
|
|
|
//Init
|
2014-01-15 13:44:46 -07:00
|
|
|
u16 *IndexGenerator::Iptr;
|
|
|
|
u16 *IndexGenerator::BASEIptr;
|
2013-02-21 20:01:53 -07:00
|
|
|
u32 IndexGenerator::index;
|
2009-10-06 08:24:10 -06:00
|
|
|
|
2013-04-08 09:58:23 -06:00
|
|
|
static const u16 s_primitive_restart = -1;
|
|
|
|
|
2013-04-08 11:39:43 -06:00
|
|
|
static void (*primitive_table[8])(u32);
|
|
|
|
|
|
|
|
void IndexGenerator::Init()
|
|
|
|
{
|
|
|
|
if(g_Config.backend_info.bSupportsPrimitiveRestart)
|
|
|
|
{
|
|
|
|
primitive_table[0] = IndexGenerator::AddQuads<true>;
|
|
|
|
primitive_table[2] = IndexGenerator::AddList<true>;
|
|
|
|
primitive_table[3] = IndexGenerator::AddStrip<true>;
|
|
|
|
primitive_table[4] = IndexGenerator::AddFan<true>;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
primitive_table[0] = IndexGenerator::AddQuads<false>;
|
|
|
|
primitive_table[2] = IndexGenerator::AddList<false>;
|
|
|
|
primitive_table[3] = IndexGenerator::AddStrip<false>;
|
|
|
|
primitive_table[4] = IndexGenerator::AddFan<false>;
|
|
|
|
}
|
|
|
|
primitive_table[1] = NULL;
|
|
|
|
primitive_table[5] = &IndexGenerator::AddLineList;
|
|
|
|
primitive_table[6] = &IndexGenerator::AddLineStrip;
|
|
|
|
primitive_table[7] = &IndexGenerator::AddPoints;
|
|
|
|
}
|
|
|
|
|
2014-01-15 13:44:46 -07:00
|
|
|
void IndexGenerator::Start(u16* Indexptr)
|
2010-06-16 04:12:57 -06:00
|
|
|
{
|
2014-01-15 13:44:46 -07:00
|
|
|
Iptr = Indexptr;
|
|
|
|
BASEIptr = Indexptr;
|
2009-09-29 12:27:41 -06:00
|
|
|
index = 0;
|
|
|
|
}
|
2013-02-21 18:10:00 -07:00
|
|
|
|
|
|
|
void IndexGenerator::AddIndices(int primitive, u32 numVerts)
|
|
|
|
{
|
|
|
|
primitive_table[primitive](numVerts);
|
|
|
|
index += numVerts;
|
|
|
|
}
|
|
|
|
|
2009-09-29 12:27:41 -06:00
|
|
|
// Triangles
|
2013-04-08 11:39:43 -06:00
|
|
|
template <bool pr> __forceinline void IndexGenerator::WriteTriangle(u32 index1, u32 index2, u32 index3)
|
2009-09-29 12:27:41 -06:00
|
|
|
{
|
2014-01-15 13:44:46 -07:00
|
|
|
*Iptr++ = index1;
|
|
|
|
*Iptr++ = index2;
|
|
|
|
*Iptr++ = index3;
|
2013-04-08 11:39:43 -06:00
|
|
|
if(pr)
|
2014-01-15 13:44:46 -07:00
|
|
|
*Iptr++ = s_primitive_restart;
|
2013-02-21 18:10:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-08 11:39:43 -06:00
|
|
|
template <bool pr> void IndexGenerator::AddList(u32 const numVerts)
|
2013-02-21 20:01:53 -07:00
|
|
|
{
|
2013-06-23 06:38:25 -06:00
|
|
|
for (u32 i = 2; i < numVerts; i+=3)
|
2010-05-22 15:58:43 -06:00
|
|
|
{
|
2013-06-23 06:38:25 -06:00
|
|
|
WriteTriangle<pr>(index + i - 2, index + i - 1, index + i);
|
2009-09-29 12:27:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-08 11:39:43 -06:00
|
|
|
template <bool pr> void IndexGenerator::AddStrip(u32 const numVerts)
|
2009-09-29 12:27:41 -06:00
|
|
|
{
|
2013-04-24 07:21:54 -06:00
|
|
|
if(pr)
|
|
|
|
{
|
2013-03-29 07:27:33 -06:00
|
|
|
for (u32 i = 0; i < numVerts; ++i)
|
|
|
|
{
|
2014-01-15 13:44:46 -07:00
|
|
|
*Iptr++ = index + i;
|
2013-03-29 07:27:33 -06:00
|
|
|
}
|
2014-01-15 13:44:46 -07:00
|
|
|
*Iptr++ = s_primitive_restart;
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2013-04-24 07:21:54 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-29 07:27:33 -06:00
|
|
|
bool wind = false;
|
|
|
|
for (u32 i = 2; i < numVerts; ++i)
|
|
|
|
{
|
2013-04-08 11:39:43 -06:00
|
|
|
WriteTriangle<pr>(
|
2013-03-29 07:27:33 -06:00
|
|
|
index + i - 2,
|
|
|
|
index + i - !wind,
|
|
|
|
index + i - wind);
|
|
|
|
|
|
|
|
wind ^= true;
|
|
|
|
}
|
2009-09-29 12:27:41 -06:00
|
|
|
}
|
|
|
|
}
|
2013-02-21 18:10:00 -07:00
|
|
|
|
2013-04-08 09:22:16 -06:00
|
|
|
/**
|
|
|
|
* FAN simulator:
|
2013-10-28 23:23:17 -06:00
|
|
|
*
|
2013-04-08 09:22:16 -06:00
|
|
|
* 2---3
|
|
|
|
* / \ / \
|
|
|
|
* 1---0---4
|
2013-10-28 23:23:17 -06:00
|
|
|
*
|
2013-04-08 09:22:16 -06:00
|
|
|
* would generate this triangles:
|
|
|
|
* 012, 023, 034
|
2013-10-28 23:23:17 -06:00
|
|
|
*
|
2013-04-08 09:22:16 -06:00
|
|
|
* rotated (for better striping):
|
|
|
|
* 120, 302, 034
|
2013-10-28 23:23:17 -06:00
|
|
|
*
|
2013-04-08 09:22:16 -06:00
|
|
|
* as odd ones have to winded, following strip is fine:
|
|
|
|
* 12034
|
2013-10-28 23:23:17 -06:00
|
|
|
*
|
2013-04-08 09:22:16 -06:00
|
|
|
* so we use 6 indices for 3 triangles
|
|
|
|
*/
|
|
|
|
|
2013-04-08 11:39:43 -06:00
|
|
|
template <bool pr> void IndexGenerator::AddFan(u32 numVerts)
|
2009-09-29 12:27:41 -06:00
|
|
|
{
|
2013-04-08 09:22:16 -06:00
|
|
|
u32 i = 2;
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2013-04-24 07:21:54 -06:00
|
|
|
if(pr)
|
|
|
|
{
|
2013-05-09 02:17:12 -06:00
|
|
|
for(; i+3<=numVerts; i+=3)
|
2013-04-24 07:21:54 -06:00
|
|
|
{
|
2014-01-15 13:44:46 -07:00
|
|
|
*Iptr++ = index + i - 1;
|
|
|
|
*Iptr++ = index + i + 0;
|
|
|
|
*Iptr++ = index;
|
|
|
|
*Iptr++ = index + i + 1;
|
|
|
|
*Iptr++ = index + i + 2;
|
|
|
|
*Iptr++ = s_primitive_restart;
|
2013-04-08 09:22:16 -06:00
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2013-05-09 02:17:12 -06:00
|
|
|
for(; i+2<=numVerts; i+=2)
|
2013-04-24 07:21:54 -06:00
|
|
|
{
|
2014-01-15 13:44:46 -07:00
|
|
|
*Iptr++ = index + i - 1;
|
|
|
|
*Iptr++ = index + i + 0;
|
|
|
|
*Iptr++ = index;
|
|
|
|
*Iptr++ = index + i + 1;
|
|
|
|
*Iptr++ = s_primitive_restart;
|
2013-04-08 09:22:16 -06:00
|
|
|
}
|
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2013-04-08 09:22:16 -06:00
|
|
|
for (; i < numVerts; ++i)
|
2010-05-22 15:58:43 -06:00
|
|
|
{
|
2013-04-08 11:39:43 -06:00
|
|
|
WriteTriangle<pr>(index, index + i - 1, index + i);
|
2013-02-21 18:10:00 -07:00
|
|
|
}
|
2009-09-29 12:27:41 -06:00
|
|
|
}
|
|
|
|
|
2013-04-08 09:22:16 -06:00
|
|
|
/*
|
|
|
|
* QUAD simulator
|
2013-10-28 23:23:17 -06:00
|
|
|
*
|
2013-04-08 09:22:16 -06:00
|
|
|
* 0---1 4---5
|
|
|
|
* |\ | |\ |
|
|
|
|
* | \ | | \ |
|
|
|
|
* | \| | \|
|
|
|
|
* 3---2 7---6
|
2013-10-28 23:23:17 -06:00
|
|
|
*
|
2013-04-08 09:22:16 -06:00
|
|
|
* 012,023, 456,467 ...
|
|
|
|
* or 120,302, 564,746
|
|
|
|
* or as strip: 1203, 5647
|
2013-10-28 23:23:17 -06:00
|
|
|
*
|
|
|
|
* Warning:
|
2013-04-10 04:45:44 -06:00
|
|
|
* A simple triangle has to be rendered for three vertices.
|
|
|
|
* ZWW do this for sun rays
|
2013-04-08 09:22:16 -06:00
|
|
|
*/
|
2013-04-08 11:39:43 -06:00
|
|
|
template <bool pr> void IndexGenerator::AddQuads(u32 numVerts)
|
2009-09-29 12:27:41 -06:00
|
|
|
{
|
2013-06-23 06:38:25 -06:00
|
|
|
u32 i = 3;
|
|
|
|
for (; i < numVerts; i+=4)
|
2009-09-29 12:27:41 -06:00
|
|
|
{
|
2013-04-24 07:21:54 -06:00
|
|
|
if(pr)
|
|
|
|
{
|
2014-01-15 13:44:46 -07:00
|
|
|
*Iptr++ = index + i - 2;
|
|
|
|
*Iptr++ = index + i - 1;
|
|
|
|
*Iptr++ = index + i - 3;
|
|
|
|
*Iptr++ = index + i - 0;
|
|
|
|
*Iptr++ = s_primitive_restart;
|
2013-04-24 07:21:54 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-23 06:38:25 -06:00
|
|
|
WriteTriangle<pr>(index + i - 3, index + i - 2, index + i - 1);
|
|
|
|
WriteTriangle<pr>(index + i - 3, index + i - 1, index + i - 0);
|
2013-03-29 07:27:33 -06:00
|
|
|
}
|
2009-09-29 12:27:41 -06:00
|
|
|
}
|
2013-04-24 07:21:54 -06:00
|
|
|
|
2013-04-10 04:36:59 -06:00
|
|
|
// three vertices remaining, so render a triangle
|
2013-06-23 06:38:25 -06:00
|
|
|
if(i == numVerts)
|
2013-04-10 04:36:59 -06:00
|
|
|
{
|
|
|
|
WriteTriangle<pr>(index+numVerts-3, index+numVerts-2, index+numVerts-1);
|
|
|
|
}
|
2009-09-29 12:27:41 -06:00
|
|
|
}
|
|
|
|
|
2013-02-21 18:10:00 -07:00
|
|
|
// Lines
|
|
|
|
void IndexGenerator::AddLineList(u32 numVerts)
|
2009-09-29 12:27:41 -06:00
|
|
|
{
|
2013-06-23 06:38:25 -06:00
|
|
|
for (u32 i = 1; i < numVerts; i+=2)
|
2009-09-29 12:27:41 -06:00
|
|
|
{
|
2014-01-15 13:44:46 -07:00
|
|
|
*Iptr++ = index + i - 1;
|
|
|
|
*Iptr++ = index + i;
|
2009-09-29 12:27:41 -06:00
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2009-09-29 12:27:41 -06:00
|
|
|
}
|
|
|
|
|
2013-04-08 09:58:23 -06:00
|
|
|
// shouldn't be used as strips as LineLists are much more common
|
|
|
|
// so converting them to lists
|
2013-02-21 18:10:00 -07:00
|
|
|
void IndexGenerator::AddLineStrip(u32 numVerts)
|
2009-09-29 12:27:41 -06:00
|
|
|
{
|
2013-02-21 18:10:00 -07:00
|
|
|
for (u32 i = 1; i < numVerts; ++i)
|
2009-09-29 12:27:41 -06:00
|
|
|
{
|
2014-01-15 13:44:46 -07:00
|
|
|
*Iptr++ = index + i - 1;
|
|
|
|
*Iptr++ = index + i;
|
2009-09-29 12:27:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-21 18:10:00 -07:00
|
|
|
// Points
|
|
|
|
void IndexGenerator::AddPoints(u32 numVerts)
|
2009-09-29 12:27:41 -06:00
|
|
|
{
|
2013-02-21 18:10:00 -07:00
|
|
|
for (u32 i = 0; i != numVerts; ++i)
|
2009-09-29 12:27:41 -06:00
|
|
|
{
|
2014-01-15 13:44:46 -07:00
|
|
|
*Iptr++ = index + i;
|
2009-09-29 12:27:41 -06:00
|
|
|
}
|
|
|
|
}
|
2013-03-22 17:18:35 -06:00
|
|
|
|
|
|
|
|
|
|
|
u32 IndexGenerator::GetRemainingIndices()
|
|
|
|
{
|
2013-04-08 09:58:23 -06:00
|
|
|
u32 max_index = 65534; // -1 is reserved for primitive restart (ogl + dx11)
|
2013-03-22 17:18:35 -06:00
|
|
|
return max_index - index;
|
|
|
|
}
|