CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Common/SplineCommon.h
Views: 1401
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 "GPU/Math3D.h"
24
#include "GPU/ge_constants.h"
25
#include "Core/Config.h"
26
27
#define HALF_CEIL(x) (x + 1) / 2 // Integer ceil = (int)ceil((float)x / 2.0f)
28
29
// PSP compatible format so we can use the end of the pipeline in beziers etc
30
// 8 + 4 + 12 + 12 = 36 bytes
31
struct SimpleVertex {
32
float uv[2];
33
union {
34
u8 color[4];
35
u32_le color_32;
36
};
37
Vec3Packedf nrm;
38
Vec3Packedf pos;
39
};
40
41
class SimpleBufferManager;
42
43
namespace Spline {
44
45
void BuildIndex(u16 *indices, int &count, int num_u, int num_v, GEPatchPrimType prim_type, int total = 0);
46
47
enum SplineQuality {
48
LOW_QUALITY = 0,
49
MEDIUM_QUALITY = 1,
50
HIGH_QUALITY = 2,
51
};
52
53
class Bezier3DWeight;
54
class Spline3DWeight;
55
56
// We decode all vertices into a common format for easy interpolation and stuff.
57
// Not fast but can be optimized later.
58
59
struct SurfaceInfo {
60
int tess_u, tess_v;
61
int num_points_u, num_points_v;
62
int num_patches_u, num_patches_v;
63
int type_u, type_v;
64
GEPatchPrimType primType;
65
bool patchFacing;
66
67
void BaseInit() {
68
// If specified as 0, uses 1.
69
if (tess_u < 1) tess_u = 1;
70
if (tess_v < 1) tess_v = 1;
71
72
switch (g_Config.iSplineBezierQuality) {
73
case LOW_QUALITY:
74
tess_u = 2;
75
tess_v = 2;
76
break;
77
case MEDIUM_QUALITY:
78
// Don't cut below 2, though.
79
if (tess_u > 2) tess_u = HALF_CEIL(tess_u);
80
if (tess_v > 2) tess_v = HALF_CEIL(tess_v);
81
break;
82
}
83
}
84
};
85
86
struct BezierSurface : public SurfaceInfo {
87
using WeightType = Bezier3DWeight;
88
89
int num_verts_per_patch;
90
91
void Init(int maxVertices) {
92
SurfaceInfo::BaseInit();
93
// Downsample until it fits, in case crazy tessellation factors are sent.
94
while ((tess_u + 1) * (tess_v + 1) * num_patches_u * num_patches_v > maxVertices) {
95
tess_u--;
96
tess_v--;
97
}
98
num_verts_per_patch = (tess_u + 1) * (tess_v + 1);
99
}
100
101
int GetTessStart(int patch) const { return 0; }
102
103
int GetPointIndex(int patch_u, int patch_v) const { return patch_v * 3 * num_points_u + patch_u * 3; }
104
105
int GetIndexU(int patch_u, int tile_u) const { return tile_u; }
106
int GetIndexV(int patch_v, int tile_v) const { return tile_v; }
107
108
int GetIndex(int index_u, int index_v, int patch_u, int patch_v) const {
109
int patch_index = patch_v * num_patches_u + patch_u;
110
return index_v * (tess_u + 1) + index_u + num_verts_per_patch * patch_index;
111
}
112
113
void BuildIndex(u16 *indices, int &count) const {
114
for (int patch_u = 0; patch_u < num_patches_u; ++patch_u) {
115
for (int patch_v = 0; patch_v < num_patches_v; ++patch_v) {
116
int patch_index = patch_v * num_patches_u + patch_u;
117
int total = patch_index * num_verts_per_patch;
118
Spline::BuildIndex(indices + count, count, tess_u, tess_v, primType, total);
119
}
120
}
121
}
122
};
123
124
struct SplineSurface : public SurfaceInfo {
125
using WeightType = Spline3DWeight;
126
127
int num_vertices_u;
128
129
void Init(int maxVertices) {
130
SurfaceInfo::BaseInit();
131
// Downsample until it fits, in case crazy tessellation factors are sent.
132
while ((num_patches_u * tess_u + 1) * (num_patches_v * tess_v + 1) > maxVertices) {
133
tess_u--;
134
tess_v--;
135
}
136
num_vertices_u = num_patches_u * tess_u + 1;
137
}
138
139
int GetTessStart(int patch) const { return (patch == 0) ? 0 : 1; }
140
141
int GetPointIndex(int patch_u, int patch_v) const { return patch_v * num_points_u + patch_u; }
142
143
int GetIndexU(int patch_u, int tile_u) const { return patch_u * tess_u + tile_u; }
144
int GetIndexV(int patch_v, int tile_v) const { return patch_v * tess_v + tile_v; }
145
146
int GetIndex(int index_u, int index_v, int patch_u, int patch_v) const {
147
return index_v * num_vertices_u + index_u;
148
}
149
150
void BuildIndex(u16 *indices, int &count) const {
151
Spline::BuildIndex(indices, count, num_patches_u * tess_u, num_patches_v * tess_v, primType);
152
}
153
};
154
155
struct Weight {
156
float basis[4], deriv[4];
157
};
158
159
template<class T>
160
class WeightCache : public T {
161
private:
162
std::unordered_map<u32, Weight*> weightsCache;
163
public:
164
Weight* operator [] (u32 key) {
165
Weight *&weights = weightsCache[key];
166
if (!weights)
167
weights = T::CalcWeightsAll(key);
168
return weights;
169
}
170
171
void Clear() {
172
for (auto it : weightsCache)
173
delete[] it.second;
174
weightsCache.clear();
175
}
176
};
177
178
struct Weight2D {
179
const Weight *u, *v;
180
int size_u, size_v;
181
182
template<class T>
183
Weight2D(WeightCache<T> &cache, u32 key_u, u32 key_v) {
184
u = cache[key_u];
185
v = (key_u != key_v) ? cache[key_v] : u; // Use same weights if u == v
186
}
187
};
188
189
struct ControlPoints {
190
Vec3f *pos = nullptr;
191
Vec2f *tex = nullptr;
192
Vec4f *col = nullptr;
193
u32_le defcolor;
194
195
ControlPoints() {}
196
ControlPoints(const SimpleVertex *const *points, int size, SimpleBufferManager &managedBuf);
197
void Convert(const SimpleVertex *const *points, int size);
198
bool IsValid() const {
199
return pos && tex && col;
200
}
201
};
202
203
struct OutputBuffers {
204
SimpleVertex *vertices;
205
u16 *indices;
206
int count;
207
};
208
209
template<class Surface>
210
void SoftwareTessellation(OutputBuffers &output, const Surface &surface, u32 origVertType, const ControlPoints &points);
211
212
} // namespace Spline
213
214
// Define function object for TemplateParameterDispatcher
215
#define TEMPLATE_PARAMETER_DISPATCHER_FUNCTION(NAME, FUNCNAME, FUNCTYPE) \
216
struct NAME { \
217
template<bool ...Params> \
218
static FUNCTYPE GetFunc() { \
219
return &FUNCNAME<Params...>; \
220
} \
221
};
222
223
template<typename Func, int NumParams, class Dispatcher>
224
class TemplateParameterDispatcher {
225
226
/* Store all combinations of template functions into an array */
227
template<int LoopCount, int Index = 0, bool ...Params>
228
struct Initializer {
229
static void Init(Func funcs[]) {
230
Initializer<LoopCount - 1, (Index << 1) + 1, true, Params...>::Init(funcs); // true
231
Initializer<LoopCount - 1, (Index << 1) + 0, false, Params...>::Init(funcs); // false
232
}
233
};
234
/* Specialized for terminates the recursive loop */
235
template<int Index, bool ...Params>
236
struct Initializer<0, Index, Params...> {
237
static void Init(Func funcs[]) {
238
funcs[Index] = Dispatcher::template GetFunc<Params...>(); // Resolve the nested dependent name as template function.
239
}
240
};
241
242
private:
243
Func funcs[1 << NumParams]; /* Function pointers array */
244
public:
245
TemplateParameterDispatcher() {
246
Initializer<NumParams>::Init(funcs);
247
}
248
249
Func GetFunc(const bool params[]) const {
250
/* Convert bool parameters to index of the array */
251
int index = 0;
252
for (int i = 0; i < NumParams; ++i)
253
index |= params[i] << i;
254
255
return funcs[index];
256
}
257
};
258
259