2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-08 05:25:12 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
2013-02-22 01:10:00 +00:00
|
|
|
#include <cstddef>
|
|
|
|
|
2013-02-22 09:22:20 +00:00
|
|
|
#include "Common.h"
|
2013-03-29 13:27:33 +00:00
|
|
|
#include "VideoConfig.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
#include "IndexGenerator.h"
|
|
|
|
|
2009-10-06 14:24:10 +00:00
|
|
|
//Init
|
2013-02-22 03:01:53 +00:00
|
|
|
u16 *IndexGenerator::Tptr;
|
|
|
|
u16 *IndexGenerator::BASETptr;
|
|
|
|
u16 *IndexGenerator::Lptr;
|
|
|
|
u16 *IndexGenerator::BASELptr;
|
|
|
|
u16 *IndexGenerator::Pptr;
|
|
|
|
u16 *IndexGenerator::BASEPptr;
|
|
|
|
u32 IndexGenerator::numT;
|
|
|
|
u32 IndexGenerator::numL;
|
|
|
|
u32 IndexGenerator::numP;
|
|
|
|
u32 IndexGenerator::index;
|
2009-10-06 14:24:10 +00:00
|
|
|
|
2013-04-08 15:58:23 +00:00
|
|
|
static const u16 s_primitive_restart = -1;
|
|
|
|
|
2013-02-22 01:10:00 +00:00
|
|
|
void IndexGenerator::Start(u16* Triangleptr, u16* Lineptr, u16* Pointptr)
|
2010-06-16 10:12:57 +00:00
|
|
|
{
|
2009-09-29 18:27:41 +00:00
|
|
|
Tptr = Triangleptr;
|
|
|
|
Lptr = Lineptr;
|
|
|
|
Pptr = Pointptr;
|
2010-05-22 21:58:43 +00:00
|
|
|
BASETptr = Triangleptr;
|
|
|
|
BASELptr = Lineptr;
|
|
|
|
BASEPptr = Pointptr;
|
2009-09-29 18:27:41 +00:00
|
|
|
index = 0;
|
|
|
|
numT = 0;
|
|
|
|
numL = 0;
|
|
|
|
numP = 0;
|
|
|
|
}
|
2013-02-22 01:10:00 +00:00
|
|
|
|
|
|
|
void IndexGenerator::AddIndices(int primitive, u32 numVerts)
|
|
|
|
{
|
|
|
|
//switch (primitive)
|
|
|
|
//{
|
2013-02-22 03:01:53 +00:00
|
|
|
//case GX_DRAW_QUADS: IndexGenerator::AddQuads(numVerts); break;
|
|
|
|
//case GX_DRAW_TRIANGLES: IndexGenerator::AddList(numVerts); break;
|
|
|
|
//case GX_DRAW_TRIANGLE_STRIP: IndexGenerator::AddStrip(numVerts); break;
|
|
|
|
//case GX_DRAW_TRIANGLE_FAN: IndexGenerator::AddFan(numVerts); break;
|
2013-03-20 01:51:12 +00:00
|
|
|
//case GX_DRAW_LINES: IndexGenerator::AddLineList(numVerts); break;
|
2013-02-22 03:01:53 +00:00
|
|
|
//case GX_DRAW_LINE_STRIP: IndexGenerator::AddLineStrip(numVerts); break;
|
|
|
|
//case GX_DRAW_POINTS: IndexGenerator::AddPoints(numVerts); break;
|
2013-02-22 01:10:00 +00:00
|
|
|
//}
|
|
|
|
|
|
|
|
static void (*const primitive_table[])(u32) =
|
|
|
|
{
|
|
|
|
IndexGenerator::AddQuads,
|
|
|
|
NULL,
|
|
|
|
IndexGenerator::AddList,
|
|
|
|
IndexGenerator::AddStrip,
|
|
|
|
IndexGenerator::AddFan,
|
|
|
|
IndexGenerator::AddLineList,
|
|
|
|
IndexGenerator::AddLineStrip,
|
|
|
|
IndexGenerator::AddPoints,
|
|
|
|
};
|
|
|
|
|
|
|
|
primitive_table[primitive](numVerts);
|
|
|
|
index += numVerts;
|
|
|
|
}
|
|
|
|
|
2009-09-29 18:27:41 +00:00
|
|
|
// Triangles
|
2013-02-22 09:22:20 +00:00
|
|
|
__forceinline void IndexGenerator::WriteTriangle(u32 index1, u32 index2, u32 index3)
|
2009-09-29 18:27:41 +00:00
|
|
|
{
|
2013-02-22 01:10:00 +00:00
|
|
|
*Tptr++ = index1;
|
|
|
|
*Tptr++ = index2;
|
|
|
|
*Tptr++ = index3;
|
2013-03-29 13:27:33 +00:00
|
|
|
if(g_Config.backend_info.bSupportsPrimitiveRestart)
|
2013-04-08 15:58:23 +00:00
|
|
|
*Tptr++ = s_primitive_restart;
|
2013-03-29 13:27:33 +00:00
|
|
|
|
2013-02-22 01:10:00 +00:00
|
|
|
++numT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IndexGenerator::AddList(u32 const numVerts)
|
2013-02-22 03:01:53 +00:00
|
|
|
{
|
2013-02-22 01:10:00 +00:00
|
|
|
auto const numTris = numVerts / 3;
|
2013-02-22 03:01:53 +00:00
|
|
|
for (u32 i = 0; i != numTris; ++i)
|
2010-05-22 21:58:43 +00:00
|
|
|
{
|
2013-02-22 03:01:53 +00:00
|
|
|
WriteTriangle(index + i * 3, index + i * 3 + 1, index + i * 3 + 2);
|
2009-09-29 18:27:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-22 01:10:00 +00:00
|
|
|
void IndexGenerator::AddStrip(u32 const numVerts)
|
2009-09-29 18:27:41 +00:00
|
|
|
{
|
2013-03-29 13:27:33 +00:00
|
|
|
if(g_Config.backend_info.bSupportsPrimitiveRestart) {
|
|
|
|
for (u32 i = 0; i < numVerts; ++i)
|
|
|
|
{
|
|
|
|
*Tptr++ = index + i;
|
|
|
|
}
|
2013-04-08 15:58:23 +00:00
|
|
|
*Tptr++ = s_primitive_restart;
|
2013-03-29 13:27:33 +00:00
|
|
|
numT += numVerts - 2;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
bool wind = false;
|
|
|
|
for (u32 i = 2; i < numVerts; ++i)
|
|
|
|
{
|
|
|
|
WriteTriangle(
|
|
|
|
index + i - 2,
|
|
|
|
index + i - !wind,
|
|
|
|
index + i - wind);
|
|
|
|
|
|
|
|
wind ^= true;
|
|
|
|
}
|
2009-09-29 18:27:41 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-22 01:10:00 +00:00
|
|
|
|
2013-04-08 15:22:16 +00:00
|
|
|
/**
|
|
|
|
* FAN simulator:
|
|
|
|
*
|
|
|
|
* 2---3
|
|
|
|
* / \ / \
|
|
|
|
* 1---0---4
|
|
|
|
*
|
|
|
|
* would generate this triangles:
|
|
|
|
* 012, 023, 034
|
|
|
|
*
|
|
|
|
* rotated (for better striping):
|
|
|
|
* 120, 302, 034
|
|
|
|
*
|
|
|
|
* as odd ones have to winded, following strip is fine:
|
|
|
|
* 12034
|
|
|
|
*
|
|
|
|
* so we use 6 indices for 3 triangles
|
|
|
|
*/
|
|
|
|
|
2013-02-22 01:10:00 +00:00
|
|
|
void IndexGenerator::AddFan(u32 numVerts)
|
2009-09-29 18:27:41 +00:00
|
|
|
{
|
2013-04-08 15:22:16 +00:00
|
|
|
u32 i = 2;
|
|
|
|
|
|
|
|
if(g_Config.backend_info.bSupportsPrimitiveRestart) {
|
|
|
|
for(; i<=numVerts-3; i+=3) {
|
|
|
|
*Tptr++ = index + i - 1;
|
|
|
|
*Tptr++ = index + i + 0;
|
|
|
|
*Tptr++ = index;
|
|
|
|
*Tptr++ = index + i + 1;
|
|
|
|
*Tptr++ = index + i + 2;
|
2013-04-08 15:58:23 +00:00
|
|
|
*Tptr++ = s_primitive_restart;
|
2013-04-08 15:22:16 +00:00
|
|
|
numT += 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(; i<=numVerts-2; i+=2) {
|
|
|
|
*Tptr++ = index + i - 1;
|
|
|
|
*Tptr++ = index + i + 0;
|
|
|
|
*Tptr++ = index;
|
|
|
|
*Tptr++ = index + i + 1;
|
2013-04-08 15:58:23 +00:00
|
|
|
*Tptr++ = s_primitive_restart;
|
2013-04-08 15:22:16 +00:00
|
|
|
numT += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i < numVerts; ++i)
|
2010-05-22 21:58:43 +00:00
|
|
|
{
|
2013-02-22 03:01:53 +00:00
|
|
|
WriteTriangle(index, index + i - 1, index + i);
|
2013-02-22 01:10:00 +00:00
|
|
|
}
|
2009-09-29 18:27:41 +00:00
|
|
|
}
|
|
|
|
|
2013-04-08 15:22:16 +00:00
|
|
|
/*
|
|
|
|
* QUAD simulator
|
|
|
|
*
|
|
|
|
* 0---1 4---5
|
|
|
|
* |\ | |\ |
|
|
|
|
* | \ | | \ |
|
|
|
|
* | \| | \|
|
|
|
|
* 3---2 7---6
|
|
|
|
*
|
|
|
|
* 012,023, 456,467 ...
|
|
|
|
* or 120,302, 564,746
|
|
|
|
* or as strip: 1203, 5647
|
|
|
|
*/
|
2013-02-22 01:10:00 +00:00
|
|
|
void IndexGenerator::AddQuads(u32 numVerts)
|
2009-09-29 18:27:41 +00:00
|
|
|
{
|
2013-02-22 01:10:00 +00:00
|
|
|
auto const numQuads = numVerts / 4;
|
2013-02-22 03:01:53 +00:00
|
|
|
for (u32 i = 0; i != numQuads; ++i)
|
2009-09-29 18:27:41 +00:00
|
|
|
{
|
2013-03-29 13:27:33 +00:00
|
|
|
if(g_Config.backend_info.bSupportsPrimitiveRestart) {
|
|
|
|
*Tptr++ = index + i * 4 + 1;
|
|
|
|
*Tptr++ = index + i * 4 + 2;
|
2013-04-08 15:22:16 +00:00
|
|
|
*Tptr++ = index + i * 4 + 0;
|
|
|
|
*Tptr++ = index + i * 4 + 3;
|
2013-04-08 15:58:23 +00:00
|
|
|
*Tptr++ = s_primitive_restart;
|
2013-03-29 13:27:33 +00:00
|
|
|
numT += 2;
|
|
|
|
} else {
|
|
|
|
WriteTriangle(index + i * 4, index + i * 4 + 1, index + i * 4 + 2);
|
|
|
|
WriteTriangle(index + i * 4, index + i * 4 + 2, index + i * 4 + 3);
|
|
|
|
}
|
2009-09-29 18:27:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-22 01:10:00 +00:00
|
|
|
// Lines
|
|
|
|
void IndexGenerator::AddLineList(u32 numVerts)
|
2009-09-29 18:27:41 +00:00
|
|
|
{
|
2013-02-22 01:10:00 +00:00
|
|
|
auto const numLines = numVerts / 2;
|
|
|
|
for (u32 i = 0; i != numLines; ++i)
|
2009-09-29 18:27:41 +00:00
|
|
|
{
|
2013-02-22 01:10:00 +00:00
|
|
|
*Lptr++ = index + i * 2;
|
|
|
|
*Lptr++ = index + i * 2 + 1;
|
|
|
|
++numL;
|
2009-09-29 18:27:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-08 15:58:23 +00:00
|
|
|
// shouldn't be used as strips as LineLists are much more common
|
|
|
|
// so converting them to lists
|
2013-02-22 01:10:00 +00:00
|
|
|
void IndexGenerator::AddLineStrip(u32 numVerts)
|
2009-09-29 18:27:41 +00:00
|
|
|
{
|
2013-02-22 01:10:00 +00:00
|
|
|
for (u32 i = 1; i < numVerts; ++i)
|
2009-09-29 18:27:41 +00:00
|
|
|
{
|
2013-02-22 01:10:00 +00:00
|
|
|
*Lptr++ = index + i - 1;
|
|
|
|
*Lptr++ = index + i;
|
|
|
|
++numL;
|
2009-09-29 18:27:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-22 01:10:00 +00:00
|
|
|
// Points
|
|
|
|
void IndexGenerator::AddPoints(u32 numVerts)
|
2009-09-29 18:27:41 +00:00
|
|
|
{
|
2013-02-22 01:10:00 +00:00
|
|
|
for (u32 i = 0; i != numVerts; ++i)
|
2009-09-29 18:27:41 +00:00
|
|
|
{
|
2013-02-22 01:10:00 +00:00
|
|
|
*Pptr++ = index + i;
|
|
|
|
++numP;
|
2009-09-29 18:27:41 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-22 23:18:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
u32 IndexGenerator::GetRemainingIndices()
|
|
|
|
{
|
2013-04-08 15:58:23 +00:00
|
|
|
u32 max_index = 65534; // -1 is reserved for primitive restart (ogl + dx11)
|
2013-03-22 23:18:35 +00:00
|
|
|
return max_index - index;
|
|
|
|
}
|