2013-09-24 12:13:13 +00:00
|
|
|
// Copyright (c) 2013- PPSSPP Project.
|
|
|
|
|
|
|
|
// 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 or later versions.
|
|
|
|
|
|
|
|
// 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 git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#pragma once
|
2018-07-10 16:09:20 +00:00
|
|
|
#include <unordered_map>
|
2013-09-24 12:13:13 +00:00
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
2017-08-31 15:13:18 +00:00
|
|
|
#include "Common/Swap.h"
|
2013-09-24 12:13:13 +00:00
|
|
|
#include "GPU/Math3D.h"
|
2015-07-29 10:04:52 +00:00
|
|
|
#include "GPU/ge_constants.h"
|
2018-07-10 16:09:20 +00:00
|
|
|
#include "Core/Config.h"
|
2013-09-24 12:13:13 +00:00
|
|
|
|
2018-02-04 16:47:58 +00:00
|
|
|
#define HALF_CEIL(x) (x + 1) / 2 // Integer ceil = (int)ceil((float)x / 2.0f)
|
|
|
|
|
2013-09-24 12:13:13 +00:00
|
|
|
// PSP compatible format so we can use the end of the pipeline in beziers etc
|
|
|
|
struct SimpleVertex {
|
|
|
|
float uv[2];
|
2014-12-13 18:36:09 +00:00
|
|
|
union {
|
|
|
|
u8 color[4];
|
|
|
|
u32_le color_32;
|
|
|
|
};
|
2014-03-17 13:58:50 +00:00
|
|
|
Vec3Packedf nrm;
|
|
|
|
Vec3Packedf pos;
|
2013-09-24 12:13:13 +00:00
|
|
|
};
|
2014-09-13 13:13:34 +00:00
|
|
|
|
2018-09-29 04:39:02 +00:00
|
|
|
class SimpleBufferManager;
|
|
|
|
|
|
|
|
namespace Spline {
|
|
|
|
|
2018-02-04 16:47:58 +00:00
|
|
|
void BuildIndex(u16 *indices, int &count, int num_u, int num_v, GEPatchPrimType prim_type, int total = 0);
|
|
|
|
|
|
|
|
enum SplineQuality {
|
|
|
|
LOW_QUALITY = 0,
|
|
|
|
MEDIUM_QUALITY = 1,
|
|
|
|
HIGH_QUALITY = 2,
|
|
|
|
};
|
|
|
|
|
2018-07-30 10:36:41 +00:00
|
|
|
class Bezier3DWeight;
|
|
|
|
class Spline3DWeight;
|
|
|
|
|
2014-09-13 13:13:34 +00:00
|
|
|
// We decode all vertices into a common format for easy interpolation and stuff.
|
|
|
|
// Not fast but can be optimized later.
|
2018-07-30 10:36:41 +00:00
|
|
|
|
2018-09-27 11:57:30 +00:00
|
|
|
struct SurfaceInfo {
|
|
|
|
int tess_u, tess_v;
|
|
|
|
int num_points_u, num_points_v;
|
|
|
|
int num_patches_u, num_patches_v;
|
|
|
|
int type_u, type_v;
|
2015-07-29 10:04:52 +00:00
|
|
|
GEPatchPrimType primType;
|
|
|
|
bool patchFacing;
|
2018-02-04 16:47:58 +00:00
|
|
|
|
2018-09-27 11:57:30 +00:00
|
|
|
void Init() {
|
|
|
|
// If specified as 0, uses 1.
|
|
|
|
if (tess_u < 1) tess_u = 1;
|
|
|
|
if (tess_v < 1) tess_v = 1;
|
|
|
|
|
2018-02-04 16:47:58 +00:00
|
|
|
switch (g_Config.iSplineBezierQuality) {
|
|
|
|
case LOW_QUALITY:
|
|
|
|
tess_u = 2;
|
|
|
|
tess_v = 2;
|
|
|
|
break;
|
|
|
|
case MEDIUM_QUALITY:
|
|
|
|
// Don't cut below 2, though.
|
|
|
|
if (tess_u > 2) tess_u = HALF_CEIL(tess_u);
|
|
|
|
if (tess_v > 2) tess_v = HALF_CEIL(tess_v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 11:57:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BezierSurface : public SurfaceInfo {
|
|
|
|
using WeightType = Bezier3DWeight;
|
|
|
|
|
|
|
|
int num_verts_per_patch;
|
|
|
|
|
|
|
|
void Init(int maxVertices) {
|
|
|
|
SurfaceInfo::Init();
|
|
|
|
// Downsample until it fits, in case crazy tessellation factors are sent.
|
|
|
|
while ((tess_u + 1) * (tess_v + 1) * num_patches_u * num_patches_v > maxVertices) {
|
|
|
|
tess_u--;
|
|
|
|
tess_v--;
|
|
|
|
}
|
|
|
|
num_verts_per_patch = (tess_u + 1) * (tess_v + 1);
|
|
|
|
}
|
2018-02-04 16:47:58 +00:00
|
|
|
|
2018-02-11 15:50:21 +00:00
|
|
|
int GetTessStart(int patch) const { return 0; }
|
2018-02-04 16:47:58 +00:00
|
|
|
|
2018-09-27 11:57:30 +00:00
|
|
|
int GetPointIndex(int patch_u, int patch_v) const { return patch_v * 3 * num_points_u + patch_u * 3; }
|
2018-02-04 16:47:58 +00:00
|
|
|
|
|
|
|
int GetIndexU(int patch_u, int tile_u) const { return tile_u; }
|
|
|
|
int GetIndexV(int patch_v, int tile_v) const { return tile_v; }
|
|
|
|
|
|
|
|
int GetIndex(int index_u, int index_v, int patch_u, int patch_v) const {
|
2018-09-27 11:57:30 +00:00
|
|
|
int patch_index = patch_v * num_patches_u + patch_u;
|
|
|
|
return index_v * (tess_u + 1) + index_u + num_verts_per_patch * patch_index;
|
2018-02-04 16:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BuildIndex(u16 *indices, int &count) const {
|
|
|
|
for (int patch_u = 0; patch_u < num_patches_u; ++patch_u) {
|
|
|
|
for (int patch_v = 0; patch_v < num_patches_v; ++patch_v) {
|
|
|
|
int patch_index = patch_v * num_patches_u + patch_u;
|
2018-09-27 11:57:30 +00:00
|
|
|
int total = patch_index * num_verts_per_patch;
|
2018-09-29 04:39:02 +00:00
|
|
|
Spline::BuildIndex(indices + count, count, tess_u, tess_v, primType, total);
|
2018-02-04 16:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-13 13:13:34 +00:00
|
|
|
};
|
|
|
|
|
2018-09-27 11:57:30 +00:00
|
|
|
struct SplineSurface : public SurfaceInfo {
|
2018-07-30 10:36:41 +00:00
|
|
|
using WeightType = Spline3DWeight;
|
|
|
|
|
2018-09-27 11:57:30 +00:00
|
|
|
int num_vertices_u;
|
2014-09-13 13:13:34 +00:00
|
|
|
|
2018-02-04 16:47:58 +00:00
|
|
|
void Init(int maxVertices) {
|
2018-09-27 11:57:30 +00:00
|
|
|
SurfaceInfo::Init();
|
|
|
|
// Downsample until it fits, in case crazy tessellation factors are sent.
|
|
|
|
while ((num_patches_u * tess_u + 1) * (num_patches_v * tess_v + 1) > maxVertices) {
|
|
|
|
tess_u--;
|
|
|
|
tess_v--;
|
2018-02-04 16:47:58 +00:00
|
|
|
}
|
2018-09-27 11:57:30 +00:00
|
|
|
num_vertices_u = num_patches_u * tess_u + 1;
|
2018-02-04 16:47:58 +00:00
|
|
|
}
|
|
|
|
|
2018-02-11 15:50:21 +00:00
|
|
|
int GetTessStart(int patch) const { return (patch == 0) ? 0 : 1; }
|
2018-02-04 16:47:58 +00:00
|
|
|
|
2018-09-27 11:57:30 +00:00
|
|
|
int GetPointIndex(int patch_u, int patch_v) const { return patch_v * num_points_u + patch_u; }
|
2018-02-04 16:47:58 +00:00
|
|
|
|
|
|
|
int GetIndexU(int patch_u, int tile_u) const { return patch_u * tess_u + tile_u; }
|
|
|
|
int GetIndexV(int patch_v, int tile_v) const { return patch_v * tess_v + tile_v; }
|
|
|
|
|
|
|
|
int GetIndex(int index_u, int index_v, int patch_u, int patch_v) const {
|
2018-09-27 11:57:30 +00:00
|
|
|
return index_v * num_vertices_u + index_u;
|
2018-02-04 16:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BuildIndex(u16 *indices, int &count) const {
|
2018-09-29 04:39:02 +00:00
|
|
|
Spline::BuildIndex(indices, count, num_patches_u * tess_u, num_patches_v * tess_v, primType);
|
2018-02-04 16:47:58 +00:00
|
|
|
}
|
2014-09-13 13:13:34 +00:00
|
|
|
};
|
|
|
|
|
2018-07-10 16:09:20 +00:00
|
|
|
struct Weight {
|
|
|
|
float basis[4], deriv[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class WeightCache : public T {
|
|
|
|
private:
|
|
|
|
std::unordered_map<u32, Weight*> weightsCache;
|
|
|
|
public:
|
|
|
|
Weight* operator [] (u32 key) {
|
|
|
|
Weight *&weights = weightsCache[key];
|
|
|
|
if (!weights)
|
2018-08-31 11:10:02 +00:00
|
|
|
weights = T::CalcWeightsAll(key);
|
2018-07-10 16:09:20 +00:00
|
|
|
return weights;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear() {
|
|
|
|
for (auto it : weightsCache)
|
|
|
|
delete[] it.second;
|
|
|
|
weightsCache.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Weight2D {
|
|
|
|
const Weight *u, *v;
|
|
|
|
int size_u, size_v;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
Weight2D(WeightCache<T> &cache, u32 key_u, u32 key_v) {
|
|
|
|
u = cache[key_u];
|
|
|
|
v = (key_u != key_v) ? cache[key_v] : u; // Use same weights if u == v
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-30 19:12:12 +00:00
|
|
|
struct ControlPoints {
|
|
|
|
Vec3f *pos;
|
|
|
|
Vec2f *tex;
|
|
|
|
Vec4f *col;
|
|
|
|
u32_le defcolor;
|
|
|
|
|
2018-08-09 10:29:19 +00:00
|
|
|
ControlPoints() {}
|
|
|
|
ControlPoints(const SimpleVertex *const *points, int size, SimpleBufferManager &managedBuf);
|
|
|
|
void Convert(const SimpleVertex *const *points, int size);
|
2018-07-30 19:12:12 +00:00
|
|
|
};
|
|
|
|
|
2018-07-22 02:22:17 +00:00
|
|
|
struct OutputBuffers {
|
|
|
|
SimpleVertex *vertices;
|
|
|
|
u16 *indices;
|
|
|
|
int count;
|
|
|
|
};
|
|
|
|
|
2018-08-09 10:29:19 +00:00
|
|
|
template<class Patch>
|
|
|
|
void SoftwareTessellation(OutputBuffers &output, const Patch &patch, u32 origVertType, const ControlPoints &points);
|
2018-02-01 11:33:17 +00:00
|
|
|
|
2018-09-29 04:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CanUseHardwareTessellation(GEPatchPrimType prim);
|
|
|
|
|
2018-07-21 16:11:23 +00:00
|
|
|
// Define function object for TemplateParameterDispatcher
|
2018-08-31 11:10:02 +00:00
|
|
|
#define TEMPLATE_PARAMETER_DISPATCHER_FUNCTION(NAME, FUNCNAME, FUNCTYPE) \
|
2018-07-21 16:11:23 +00:00
|
|
|
struct NAME { \
|
|
|
|
template<bool ...Params> \
|
2018-08-31 11:10:02 +00:00
|
|
|
static FUNCTYPE GetFunc() { \
|
2018-07-21 16:11:23 +00:00
|
|
|
return &FUNCNAME<Params...>; \
|
2018-02-01 11:33:17 +00:00
|
|
|
} \
|
|
|
|
};
|
2018-07-21 16:11:23 +00:00
|
|
|
|
|
|
|
template<typename Func, int NumParams, class Dispatcher>
|
|
|
|
class TemplateParameterDispatcher {
|
|
|
|
|
|
|
|
/* Store all combinations of template functions into an array */
|
|
|
|
template<int LoopCount, int Index = 0, bool ...Params>
|
|
|
|
struct Initializer {
|
|
|
|
static void Init(Func funcs[]) {
|
|
|
|
Initializer<LoopCount - 1, (Index << 1) + 1, true, Params...>::Init(funcs); // true
|
|
|
|
Initializer<LoopCount - 1, (Index << 1) + 0, false, Params...>::Init(funcs); // false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/* Specialized for terminates the recursive loop */
|
|
|
|
template<int Index, bool ...Params>
|
|
|
|
struct Initializer<0, Index, Params...> {
|
|
|
|
static void Init(Func funcs[]) {
|
2018-08-31 11:10:02 +00:00
|
|
|
funcs[Index] = Dispatcher::template GetFunc<Params...>(); // Resolve the nested dependent name as template function.
|
2018-07-21 16:11:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
Func funcs[1 << NumParams]; /* Function pointers array */
|
|
|
|
public:
|
|
|
|
TemplateParameterDispatcher() {
|
|
|
|
Initializer<NumParams>::Init(funcs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Func GetFunc(const bool params[]) const {
|
|
|
|
/* Convert bool parameters to index of the array */
|
|
|
|
int index = 0;
|
|
|
|
for (int i = 0; i < NumParams; ++i)
|
|
|
|
index |= params[i] << i;
|
|
|
|
|
|
|
|
return funcs[index];
|
|
|
|
}
|
|
|
|
};
|