Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Common/SplineCommon.h
3186 views
1
// Copyright (c) 2013- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
#include <unordered_map>
20
21
#include "Common/CommonTypes.h"
22
#include "Common/Swap.h"
23
#include "Core/ConfigValues.h"
24
#include "GPU/Math3D.h"
25
#include "GPU/ge_constants.h"
26
#include "GPU/Common/TransformCommon.h"
27
28
#include "Core/Config.h"
29
30
#define HALF_CEIL(x) (x + 1) / 2 // Integer ceil = (int)ceil((float)x / 2.0f)
31
32
class SimpleBufferManager;
33
34
namespace Spline {
35
36
void BuildIndex(u16 *indices, int &count, int num_u, int num_v, GEPatchPrimType prim_type, int total = 0);
37
38
class Bezier3DWeight;
39
class Spline3DWeight;
40
41
// We decode all vertices into a common format for easy interpolation and stuff.
42
// Not fast but can be optimized later.
43
44
struct SurfaceInfo {
45
int tess_u, tess_v;
46
int num_points_u, num_points_v;
47
int num_patches_u, num_patches_v;
48
int type_u, type_v;
49
GEPatchPrimType primType;
50
bool patchFacing;
51
52
void BaseInit() {
53
// If specified as 0, uses 1.
54
if (tess_u < 1) tess_u = 1;
55
if (tess_v < 1) tess_v = 1;
56
57
switch ((SplineQuality)g_Config.iSplineBezierQuality) {
58
case SplineQuality::LOW_QUALITY:
59
tess_u = 2;
60
tess_v = 2;
61
break;
62
case SplineQuality::MEDIUM_QUALITY:
63
// Don't cut below 2, though.
64
if (tess_u > 2) tess_u = HALF_CEIL(tess_u);
65
if (tess_v > 2) tess_v = HALF_CEIL(tess_v);
66
break;
67
default:
68
break;
69
}
70
}
71
};
72
73
struct BezierSurface : public SurfaceInfo {
74
using WeightType = Bezier3DWeight;
75
76
int num_verts_per_patch;
77
78
void Init(int maxVertices) {
79
SurfaceInfo::BaseInit();
80
// Downsample until it fits, in case crazy tessellation factors are sent.
81
while ((tess_u + 1) * (tess_v + 1) * num_patches_u * num_patches_v > maxVertices) {
82
tess_u--;
83
tess_v--;
84
}
85
num_verts_per_patch = (tess_u + 1) * (tess_v + 1);
86
}
87
88
int GetTessStart(int patch) const { return 0; }
89
90
int GetPointIndex(int patch_u, int patch_v) const { return patch_v * 3 * num_points_u + patch_u * 3; }
91
92
int GetIndexU(int patch_u, int tile_u) const { return tile_u; }
93
int GetIndexV(int patch_v, int tile_v) const { return tile_v; }
94
95
int GetIndex(int index_u, int index_v, int patch_u, int patch_v) const {
96
int patch_index = patch_v * num_patches_u + patch_u;
97
return index_v * (tess_u + 1) + index_u + num_verts_per_patch * patch_index;
98
}
99
100
void BuildIndex(u16 *indices, int &count) const {
101
for (int patch_u = 0; patch_u < num_patches_u; ++patch_u) {
102
for (int patch_v = 0; patch_v < num_patches_v; ++patch_v) {
103
int patch_index = patch_v * num_patches_u + patch_u;
104
int total = patch_index * num_verts_per_patch;
105
Spline::BuildIndex(indices + count, count, tess_u, tess_v, primType, total);
106
}
107
}
108
}
109
};
110
111
struct SplineSurface : public SurfaceInfo {
112
using WeightType = Spline3DWeight;
113
114
int num_vertices_u;
115
116
void Init(int maxVertices) {
117
SurfaceInfo::BaseInit();
118
// Downsample until it fits, in case crazy tessellation factors are sent.
119
while ((num_patches_u * tess_u + 1) * (num_patches_v * tess_v + 1) > maxVertices) {
120
tess_u--;
121
tess_v--;
122
}
123
num_vertices_u = num_patches_u * tess_u + 1;
124
}
125
126
int GetTessStart(int patch) const { return (patch == 0) ? 0 : 1; }
127
128
int GetPointIndex(int patch_u, int patch_v) const { return patch_v * num_points_u + patch_u; }
129
130
int GetIndexU(int patch_u, int tile_u) const { return patch_u * tess_u + tile_u; }
131
int GetIndexV(int patch_v, int tile_v) const { return patch_v * tess_v + tile_v; }
132
133
int GetIndex(int index_u, int index_v, int patch_u, int patch_v) const {
134
return index_v * num_vertices_u + index_u;
135
}
136
137
void BuildIndex(u16 *indices, int &count) const {
138
Spline::BuildIndex(indices, count, num_patches_u * tess_u, num_patches_v * tess_v, primType);
139
}
140
};
141
142
struct Weight {
143
float basis[4], deriv[4];
144
};
145
146
template<class T>
147
class WeightCache : public T {
148
private:
149
std::unordered_map<u32, Weight*> weightsCache;
150
public:
151
Weight* operator [] (u32 key) {
152
Weight *&weights = weightsCache[key];
153
if (!weights)
154
weights = T::CalcWeightsAll(key);
155
return weights;
156
}
157
158
void Clear() {
159
for (auto it : weightsCache)
160
delete[] it.second;
161
weightsCache.clear();
162
}
163
};
164
165
struct Weight2D {
166
const Weight *u, *v;
167
int size_u, size_v;
168
169
template<class T>
170
Weight2D(WeightCache<T> &cache, u32 key_u, u32 key_v) {
171
u = cache[key_u];
172
v = (key_u != key_v) ? cache[key_v] : u; // Use same weights if u == v
173
}
174
};
175
176
struct ControlPoints {
177
Vec3f *pos = nullptr;
178
Vec2f *tex = nullptr;
179
Vec4f *col = nullptr;
180
u32_le defcolor;
181
182
ControlPoints() {}
183
ControlPoints(const SimpleVertex *const *points, int size, SimpleBufferManager &managedBuf);
184
void Convert(const SimpleVertex *const *points, int size);
185
bool IsValid() const {
186
return pos && tex && col;
187
}
188
};
189
190
struct OutputBuffers {
191
SimpleVertex *vertices;
192
u16 *indices;
193
int count;
194
};
195
196
template<class Surface>
197
void SoftwareTessellation(OutputBuffers &output, const Surface &surface, u32 origVertType, const ControlPoints &points);
198
199
} // namespace Spline
200
201
// Define function object for TemplateParameterDispatcher
202
#define TEMPLATE_PARAMETER_DISPATCHER_FUNCTION(NAME, FUNCNAME, FUNCTYPE) \
203
struct NAME { \
204
template<bool ...Params> \
205
static FUNCTYPE GetFunc() { \
206
return &FUNCNAME<Params...>; \
207
} \
208
};
209
210
template<typename Func, int NumParams, class Dispatcher>
211
class TemplateParameterDispatcher {
212
213
/* Store all combinations of template functions into an array */
214
template<int LoopCount, int Index = 0, bool ...Params>
215
struct Initializer {
216
static void Init(Func funcs[]) {
217
Initializer<LoopCount - 1, (Index << 1) + 1, true, Params...>::Init(funcs); // true
218
Initializer<LoopCount - 1, (Index << 1) + 0, false, Params...>::Init(funcs); // false
219
}
220
};
221
/* Specialized for terminates the recursive loop */
222
template<int Index, bool ...Params>
223
struct Initializer<0, Index, Params...> {
224
static void Init(Func funcs[]) {
225
funcs[Index] = Dispatcher::template GetFunc<Params...>(); // Resolve the nested dependent name as template function.
226
}
227
};
228
229
private:
230
Func funcs[1 << NumParams]; /* Function pointers array */
231
public:
232
TemplateParameterDispatcher() {
233
Initializer<NumParams>::Init(funcs);
234
}
235
236
Func GetFunc(const bool params[]) const {
237
/* Convert bool parameters to index of the array */
238
int index = 0;
239
for (int i = 0; i < NumParams; ++i)
240
index |= params[i] << i;
241
242
return funcs[index];
243
}
244
};
245
246