Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Common/SplineCommon.cpp
3187 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
19
#include "Common/Common.h"
20
#include "Common/CPUDetect.h"
21
#include "Common/Profiler/Profiler.h"
22
#include "GPU/Common/SplineCommon.h"
23
#include "GPU/Common/DrawEngineCommon.h"
24
#include "GPU/Common/SoftwareTransformCommon.h"
25
#include "GPU/ge_constants.h"
26
#include "GPU/GPUState.h" // only needed for UVScale stuff
27
28
class SimpleBufferManager {
29
private:
30
u8 *buf_;
31
size_t totalSize, maxSize_;
32
public:
33
SimpleBufferManager(u8 *buf, size_t maxSize)
34
: buf_(buf), totalSize(0), maxSize_(maxSize) {}
35
36
u8 *Allocate(size_t size) {
37
size = (size + 15) & ~15; // Align for 16 bytes
38
39
if ((totalSize + size) > maxSize_)
40
return nullptr; // No more memory
41
42
size_t tmp = totalSize;
43
totalSize += size;
44
return buf_ + tmp;
45
}
46
};
47
48
namespace Spline {
49
50
static void CopyQuadIndex(u16 *&indices, GEPatchPrimType type, const int idx0, const int idx1, const int idx2, const int idx3) {
51
if (type == GE_PATCHPRIM_LINES) {
52
*(indices++) = idx0;
53
*(indices++) = idx2;
54
*(indices++) = idx1;
55
*(indices++) = idx3;
56
*(indices++) = idx1;
57
*(indices++) = idx2;
58
} else {
59
*(indices++) = idx0;
60
*(indices++) = idx2;
61
*(indices++) = idx1;
62
*(indices++) = idx1;
63
*(indices++) = idx2;
64
*(indices++) = idx3;
65
}
66
}
67
68
void BuildIndex(u16 *indices, int &count, int num_u, int num_v, GEPatchPrimType prim_type, int total) {
69
for (int v = 0; v < num_v; ++v) {
70
for (int u = 0; u < num_u; ++u) {
71
int idx0 = v * (num_u + 1) + u + total; // Top left
72
int idx2 = (v + 1) * (num_u + 1) + u + total; // Bottom left
73
74
CopyQuadIndex(indices, prim_type, idx0, idx0 + 1, idx2, idx2 + 1);
75
count += 6;
76
}
77
}
78
}
79
80
class Bezier3DWeight {
81
private:
82
static void CalcWeights(float t, Weight &w) {
83
// Bernstein 3D basis polynomial
84
w.basis[0] = (1 - t) * (1 - t) * (1 - t);
85
w.basis[1] = 3 * t * (1 - t) * (1 - t);
86
w.basis[2] = 3 * t * t * (1 - t);
87
w.basis[3] = t * t * t;
88
89
// Derivative
90
w.deriv[0] = -3 * (1 - t) * (1 - t);
91
w.deriv[1] = 9 * t * t - 12 * t + 3;
92
w.deriv[2] = 3 * (2 - 3 * t) * t;
93
w.deriv[3] = 3 * t * t;
94
}
95
public:
96
static Weight *CalcWeightsAll(u32 key) {
97
int tess = (int)key;
98
Weight *weights = new Weight[tess + 1];
99
const float inv_tess = 1.0f / (float)tess;
100
for (int i = 0; i < tess + 1; ++i) {
101
const float t = (float)i * inv_tess;
102
CalcWeights(t, weights[i]);
103
}
104
return weights;
105
}
106
107
static u32 ToKey(int tess, int count, int type) {
108
return tess;
109
}
110
111
static int CalcSize(int tess, int count) {
112
return tess + 1;
113
}
114
115
static WeightCache<Bezier3DWeight> weightsCache;
116
};
117
118
class Spline3DWeight {
119
private:
120
struct KnotDiv {
121
float _3_0 = 1.0f / 3.0f;
122
float _4_1 = 1.0f / 3.0f;
123
float _5_2 = 1.0f / 3.0f;
124
float _3_1 = 1.0f / 2.0f;
125
float _4_2 = 1.0f / 2.0f;
126
float _3_2 = 1.0f; // Always 1
127
};
128
129
// knot should be an array sized n + 5 (n + 1 + 1 + degree (cubic))
130
static void CalcKnots(int n, int type, float *knots, KnotDiv *divs) {
131
// Basic theory (-2 to +3), optimized with KnotDiv (-2 to +0)
132
// for (int i = 0; i < n + 5; ++i) {
133
for (int i = 0; i < n + 2; ++i) {
134
knots[i] = (float)i - 2;
135
}
136
137
// The first edge is open
138
if ((type & 1) != 0) {
139
knots[0] = 0;
140
knots[1] = 0;
141
142
divs[0]._3_0 = 1.0f;
143
divs[0]._4_1 = 1.0f / 2.0f;
144
divs[0]._3_1 = 1.0f;
145
if (n > 1)
146
divs[1]._3_0 = 1.0f / 2.0f;
147
}
148
// The last edge is open
149
if ((type & 2) != 0) {
150
// knots[n + 2] = (float)n; // Got rid of this line optimized with KnotDiv
151
// knots[n + 3] = (float)n; // Got rid of this line optimized with KnotDiv
152
// knots[n + 4] = (float)n; // Got rid of this line optimized with KnotDiv
153
divs[n - 1]._4_1 = 1.0f / 2.0f;
154
divs[n - 1]._5_2 = 1.0f;
155
divs[n - 1]._4_2 = 1.0f;
156
if (n > 1)
157
divs[n - 2]._5_2 = 1.0f / 2.0f;
158
}
159
}
160
161
static void CalcWeights(float t, const float *knots, const KnotDiv &div, Weight &w) {
162
#ifdef _M_SSE
163
const __m128 knot012 = _mm_loadu_ps(knots);
164
const __m128 t012 = _mm_sub_ps(_mm_set_ps1(t), knot012);
165
const __m128 f30_41_52 = _mm_mul_ps(t012, _mm_loadu_ps(&div._3_0));
166
const __m128 f52_31_42 = _mm_mul_ps(t012, _mm_loadu_ps(&div._5_2));
167
168
// Following comments are for explains order of the multiply.
169
// float a = (1-f30)*(1-f31);
170
// float c = (1-f41)*(1-f42);
171
// float b = ( f31 * f41);
172
// float d = ( f42 * f52);
173
const __m128 f30_41_31_42 = _mm_shuffle_ps(f30_41_52, f52_31_42, _MM_SHUFFLE(2, 1, 1, 0));
174
const __m128 f31_42_41_52 = _mm_shuffle_ps(f52_31_42, f30_41_52, _MM_SHUFFLE(2, 1, 2, 1));
175
const __m128 c1_1_0_0 = { 1, 1, 0, 0 };
176
const __m128 acbd = _mm_mul_ps(_mm_sub_ps(c1_1_0_0, f30_41_31_42), _mm_sub_ps(c1_1_0_0, f31_42_41_52));
177
178
alignas(16) float f_t012[4];
179
alignas(16) float f_acbd[4];
180
alignas(16) float f_f30_41_31_42[4];
181
_mm_store_ps(f_t012, t012);
182
_mm_store_ps(f_acbd, acbd);
183
_mm_store_ps(f_f30_41_31_42, f30_41_31_42);
184
185
const float &f32 = f_t012[2];
186
187
const float &a = f_acbd[0];
188
const float &b = f_acbd[2];
189
const float &c = f_acbd[1];
190
const float &d = f_acbd[3];
191
192
// For derivative
193
const float &f31 = f_f30_41_31_42[2];
194
const float &f42 = f_f30_41_31_42[3];
195
#else
196
// TODO: Maybe compilers could be coaxed into vectorizing this code without the above explicitly...
197
float t0 = (t - knots[0]);
198
float t1 = (t - knots[1]);
199
float t2 = (t - knots[2]);
200
201
float f30 = t0 * div._3_0;
202
float f41 = t1 * div._4_1;
203
float f52 = t2 * div._5_2;
204
float f31 = t1 * div._3_1;
205
float f42 = t2 * div._4_2;
206
float f32 = t2 * div._3_2;
207
208
float a = (1 - f30) * (1 - f31);
209
float b = (f31 * f41);
210
float c = (1 - f41) * (1 - f42);
211
float d = (f42 * f52);
212
#endif
213
w.basis[0] = a * (1 - f32); // (1-f30)*(1-f31)*(1-f32)
214
w.basis[1] = 1 - a - b + ((a + b + c - 1) * f32);
215
w.basis[2] = b + ((1 - b - c - d) * f32);
216
w.basis[3] = d * f32; // f32*f42*f52
217
218
// Derivative
219
float i1 = (1 - f31) * (1 - f32);
220
float i2 = f31 * (1 - f32) + (1 - f42) * f32;
221
float i3 = f42 * f32;
222
223
float f130 = i1 * div._3_0;
224
float f241 = i2 * div._4_1;
225
float f352 = i3 * div._5_2;
226
227
w.deriv[0] = 3 * (0 - f130);
228
w.deriv[1] = 3 * (f130 - f241);
229
w.deriv[2] = 3 * (f241 - f352);
230
w.deriv[3] = 3 * (f352 - 0);
231
}
232
public:
233
Weight *CalcWeightsAll(u32 key) {
234
int tess, count, type;
235
FromKey(key, tess, count, type);
236
const int num_patches = count - 3;
237
Weight *weights = new Weight[tess * num_patches + 1];
238
239
// float *knots = new float[num_patches + 5];
240
float *knots = new float[num_patches + 2]; // Optimized with KnotDiv, must use +5 in theory
241
KnotDiv *divs = new KnotDiv[num_patches];
242
CalcKnots(num_patches, type, knots, divs);
243
244
const float inv_tess = 1.0f / (float)tess;
245
for (int i = 0; i < num_patches; ++i) {
246
const int start = (i == 0) ? 0 : 1;
247
for (int j = start; j <= tess; ++j) {
248
const int index = i * tess + j;
249
const float t = (float)index * inv_tess;
250
CalcWeights(t, knots + i, divs[i], weights[index]);
251
}
252
}
253
254
delete[] knots;
255
delete[] divs;
256
257
return weights;
258
}
259
260
static u32 ToKey(int tess, int count, int type) {
261
return tess | (count << 8) | (type << 16);
262
}
263
264
static void FromKey(u32 key, int &tess, int &count, int &type) {
265
tess = key & 0xFF; count = (key >> 8) & 0xFF; type = (key >> 16) & 0xFF;
266
}
267
268
static int CalcSize(int tess, int count) {
269
return (count - 3) * tess + 1;
270
}
271
272
static WeightCache<Spline3DWeight> weightsCache;
273
};
274
275
WeightCache<Bezier3DWeight> Bezier3DWeight::weightsCache;
276
WeightCache<Spline3DWeight> Spline3DWeight::weightsCache;
277
278
// Tessellate single patch (4x4 control points)
279
template<typename T>
280
class Tessellator {
281
private:
282
const T *const p[4]; // T p[v][u]; 4x4 control points
283
T u[4]; // Pre-tessellated U lines
284
public:
285
Tessellator(const T *p, const int idx[4]) : p{ p + idx[0], p + idx[1], p + idx[2], p + idx[3] } {}
286
287
// Linear combination
288
T Sample(const T p[4], const float w[4]) {
289
return p[0] * w[0] + p[1] * w[1] + p[2] * w[2] + p[3] * w[3];
290
}
291
292
void SampleEdgeU(int idx) {
293
u[0] = p[0][idx];
294
u[1] = p[1][idx];
295
u[2] = p[2][idx];
296
u[3] = p[3][idx];
297
}
298
299
void SampleU(const float weights[4]) {
300
if (weights[0] == 1.0f) { SampleEdgeU(0); return; } // weights = {1,0,0,0}, first edge is open.
301
if (weights[3] == 1.0f) { SampleEdgeU(3); return; } // weights = {0,0,0,1}, last edge is open.
302
303
u[0] = Sample(p[0], weights);
304
u[1] = Sample(p[1], weights);
305
u[2] = Sample(p[2], weights);
306
u[3] = Sample(p[3], weights);
307
}
308
309
T SampleV(const float weights[4]) {
310
if (weights[0] == 1.0f) return u[0]; // weights = {1,0,0,0}, first edge is open.
311
if (weights[3] == 1.0f) return u[3]; // weights = {0,0,0,1}, last edge is open.
312
313
return Sample(u, weights);
314
}
315
};
316
317
ControlPoints::ControlPoints(const SimpleVertex *const *points, int size, SimpleBufferManager &managedBuf) {
318
pos = (Vec3f *)managedBuf.Allocate(sizeof(Vec3f) * size);
319
tex = (Vec2f *)managedBuf.Allocate(sizeof(Vec2f) * size);
320
col = (Vec4f *)managedBuf.Allocate(sizeof(Vec4f) * size);
321
if (pos && tex && col)
322
Convert(points, size);
323
}
324
325
void ControlPoints::Convert(const SimpleVertex *const *points, int size) {
326
for (int i = 0; i < size; ++i) {
327
pos[i] = Vec3f(points[i]->pos);
328
tex[i] = Vec2f(points[i]->uv);
329
col[i] = Vec4f::FromRGBA(points[i]->color_32);
330
}
331
defcolor = points[0]->color_32;
332
}
333
334
template<class Surface>
335
class SubdivisionSurface {
336
public:
337
template <bool sampleNrm, bool sampleCol, bool sampleTex, bool useSSE4, bool patchFacing>
338
static void Tessellate(OutputBuffers &output, const Surface &surface, const ControlPoints &points, const Weight2D &weights) {
339
const float inv_u = 1.0f / (float)surface.tess_u;
340
const float inv_v = 1.0f / (float)surface.tess_v;
341
342
for (int patch_u = 0; patch_u < surface.num_patches_u; ++patch_u) {
343
const int start_u = surface.GetTessStart(patch_u);
344
for (int patch_v = 0; patch_v < surface.num_patches_v; ++patch_v) {
345
const int start_v = surface.GetTessStart(patch_v);
346
347
// Prepare 4x4 control points to tessellate
348
const int idx = surface.GetPointIndex(patch_u, patch_v);
349
const int idx_v[4] = { idx, idx + surface.num_points_u, idx + surface.num_points_u * 2, idx + surface.num_points_u * 3 };
350
Tessellator<Vec3f> tess_pos(points.pos, idx_v);
351
Tessellator<Vec4f> tess_col(points.col, idx_v);
352
Tessellator<Vec2f> tess_tex(points.tex, idx_v);
353
Tessellator<Vec3f> tess_nrm(points.pos, idx_v);
354
355
for (int tile_u = start_u; tile_u <= surface.tess_u; ++tile_u) {
356
const int index_u = surface.GetIndexU(patch_u, tile_u);
357
const Weight &wu = weights.u[index_u];
358
359
// Pre-tessellate U lines
360
tess_pos.SampleU(wu.basis);
361
if constexpr (sampleCol)
362
tess_col.SampleU(wu.basis);
363
if constexpr (sampleTex)
364
tess_tex.SampleU(wu.basis);
365
if constexpr (sampleNrm)
366
tess_nrm.SampleU(wu.deriv);
367
368
for (int tile_v = start_v; tile_v <= surface.tess_v; ++tile_v) {
369
const int index_v = surface.GetIndexV(patch_v, tile_v);
370
const Weight &wv = weights.v[index_v];
371
372
SimpleVertex &vert = output.vertices[surface.GetIndex(index_u, index_v, patch_u, patch_v)];
373
374
// Tessellate
375
vert.pos = tess_pos.SampleV(wv.basis);
376
if constexpr (sampleCol) {
377
vert.color_32 = tess_col.SampleV(wv.basis).ToRGBA();
378
} else {
379
vert.color_32 = points.defcolor;
380
}
381
if constexpr (sampleTex) {
382
tess_tex.SampleV(wv.basis).Write(vert.uv);
383
} else {
384
// Generate texcoord
385
vert.uv[0] = patch_u + tile_u * inv_u;
386
vert.uv[1] = patch_v + tile_v * inv_v;
387
}
388
if constexpr (sampleNrm) {
389
const Vec3f derivU = tess_nrm.SampleV(wv.basis);
390
const Vec3f derivV = tess_pos.SampleV(wv.deriv);
391
392
vert.nrm = Cross(derivU, derivV).Normalized(useSSE4);
393
if constexpr (patchFacing)
394
vert.nrm *= -1.0f;
395
} else {
396
vert.nrm.SetZero();
397
vert.nrm.z = 1.0f;
398
}
399
}
400
}
401
}
402
}
403
404
surface.BuildIndex(output.indices, output.count);
405
}
406
407
using TessFunc = void(*)(OutputBuffers &, const Surface &, const ControlPoints &, const Weight2D &);
408
TEMPLATE_PARAMETER_DISPATCHER_FUNCTION(Tess, SubdivisionSurface::Tessellate, TessFunc);
409
410
static void Tessellate(OutputBuffers &output, const Surface &surface, const ControlPoints &points, const Weight2D &weights, u32 origVertType) {
411
const bool params[] = {
412
(origVertType & GE_VTYPE_NRM_MASK) != 0 || gstate.isLightingEnabled(),
413
(origVertType & GE_VTYPE_COL_MASK) != 0,
414
(origVertType & GE_VTYPE_TC_MASK) != 0,
415
cpu_info.bSSE4_1,
416
surface.patchFacing,
417
};
418
static TemplateParameterDispatcher<TessFunc, ARRAY_SIZE(params), Tess> dispatcher; // Initialize only once
419
420
TessFunc func = dispatcher.GetFunc(params);
421
func(output, surface, points, weights);
422
}
423
};
424
425
template<class Surface>
426
void SoftwareTessellation(OutputBuffers &output, const Surface &surface, u32 origVertType, const ControlPoints &points) {
427
using WeightType = typename Surface::WeightType;
428
u32 key_u = WeightType::ToKey(surface.tess_u, surface.num_points_u, surface.type_u);
429
u32 key_v = WeightType::ToKey(surface.tess_v, surface.num_points_v, surface.type_v);
430
Weight2D weights(WeightType::weightsCache, key_u, key_v);
431
432
SubdivisionSurface<Surface>::Tessellate(output, surface, points, weights, origVertType);
433
}
434
435
template void SoftwareTessellation<BezierSurface>(OutputBuffers &output, const BezierSurface &surface, u32 origVertType, const ControlPoints &points);
436
template void SoftwareTessellation<SplineSurface>(OutputBuffers &output, const SplineSurface &surface, u32 origVertType, const ControlPoints &points);
437
438
template<class Surface>
439
static void HardwareTessellation(OutputBuffers &output, const Surface &surface, u32 origVertType,
440
const SimpleVertex *const *points, TessellationDataTransfer *tessDataTransfer) {
441
using WeightType = typename Surface::WeightType;
442
u32 key_u = WeightType::ToKey(surface.tess_u, surface.num_points_u, surface.type_u);
443
u32 key_v = WeightType::ToKey(surface.tess_v, surface.num_points_v, surface.type_v);
444
Weight2D weights(WeightType::weightsCache, key_u, key_v);
445
weights.size_u = WeightType::CalcSize(surface.tess_u, surface.num_points_u);
446
weights.size_v = WeightType::CalcSize(surface.tess_v, surface.num_points_v);
447
tessDataTransfer->SendDataToShader(points, surface.num_points_u, surface.num_points_v, origVertType, weights);
448
449
// Generating simple input vertices for the spline-computing vertex shader.
450
float inv_u = 1.0f / (float)surface.tess_u;
451
float inv_v = 1.0f / (float)surface.tess_v;
452
for (int patch_u = 0; patch_u < surface.num_patches_u; ++patch_u) {
453
const int start_u = surface.GetTessStart(patch_u);
454
for (int patch_v = 0; patch_v < surface.num_patches_v; ++patch_v) {
455
const int start_v = surface.GetTessStart(patch_v);
456
for (int tile_u = start_u; tile_u <= surface.tess_u; ++tile_u) {
457
const int index_u = surface.GetIndexU(patch_u, tile_u);
458
for (int tile_v = start_v; tile_v <= surface.tess_v; ++tile_v) {
459
const int index_v = surface.GetIndexV(patch_v, tile_v);
460
SimpleVertex &vert = output.vertices[surface.GetIndex(index_u, index_v, patch_u, patch_v)];
461
// Index for the weights
462
vert.pos.x = index_u;
463
vert.pos.y = index_v;
464
// For texcoord generation
465
vert.nrm.x = patch_u + (float)tile_u * inv_u;
466
vert.nrm.y = patch_v + (float)tile_v * inv_v;
467
// Patch position
468
vert.pos.z = patch_u;
469
vert.nrm.z = patch_v;
470
}
471
}
472
}
473
}
474
surface.BuildIndex(output.indices, output.count);
475
}
476
477
} // namespace Spline
478
479
using namespace Spline;
480
481
void DrawEngineCommon::ClearSplineBezierWeights() {
482
Bezier3DWeight::weightsCache.Clear();
483
Spline3DWeight::weightsCache.Clear();
484
}
485
486
// Specialize to make instance (to avoid link error).
487
template void DrawEngineCommon::SubmitCurve<BezierSurface>(const void *control_points, const void *indices, BezierSurface &surface, u32 vertType, int *bytesRead, const char *scope);
488
template void DrawEngineCommon::SubmitCurve<SplineSurface>(const void *control_points, const void *indices, SplineSurface &surface, u32 vertType, int *bytesRead, const char *scope);
489
490
template<class Surface>
491
void DrawEngineCommon::SubmitCurve(const void *control_points, const void *indices, Surface &surface, u32 vertType, int *bytesRead, const char *scope) {
492
PROFILE_THIS_SCOPE(scope);
493
494
// Real hardware seems to draw nothing when given < 4 either U or V.
495
// This would result in num_patches_u / num_patches_v being 0.
496
if (surface.num_points_u < 4 || surface.num_points_v < 4)
497
return;
498
499
SimpleBufferManager managedBuf(decoded_, DECODED_VERTEX_BUFFER_SIZE / 2);
500
501
int num_points = surface.num_points_u * surface.num_points_v;
502
u16 index_lower_bound = 0;
503
u16 index_upper_bound = num_points - 1;
504
IndexConverter ConvertIndex(vertType, indices);
505
if (indices)
506
GetIndexBounds(indices, num_points, vertType, &index_lower_bound, &index_upper_bound);
507
508
u32 vertTypeID = GetVertTypeID(vertType, gstate.getUVGenMode(), applySkinInDecode_);
509
VertexDecoder *origVDecoder = GetVertexDecoder(vertTypeID);
510
*bytesRead = num_points * origVDecoder->VertexSize();
511
512
// Simplify away bones and morph before proceeding
513
// There are normally not a lot of control points so just splitting decoded should be reasonably safe, although not great.
514
SimpleVertex *simplified_control_points = (SimpleVertex *)managedBuf.Allocate(sizeof(SimpleVertex) * (index_upper_bound + 1));
515
if (!simplified_control_points) {
516
ERROR_LOG(Log::G3D, "Failed to allocate space for simplified control points, skipping curve draw");
517
return;
518
}
519
520
u8 *temp_buffer = managedBuf.Allocate(sizeof(SimpleVertex) * num_points);
521
if (!temp_buffer) {
522
ERROR_LOG(Log::G3D, "Failed to allocate space for temp buffer, skipping curve draw");
523
return;
524
}
525
526
u32 origVertType = vertType;
527
vertType = ::NormalizeVertices(simplified_control_points, temp_buffer, (u8 *)control_points, index_lower_bound, index_upper_bound, origVDecoder, vertType);
528
529
VertexDecoder *vdecoder = GetVertexDecoder(vertType);
530
531
int vertexSize = vdecoder->VertexSize();
532
if (vertexSize != sizeof(SimpleVertex)) {
533
ERROR_LOG(Log::G3D, "Something went really wrong, vertex size: %d vs %d", vertexSize, (int)sizeof(SimpleVertex));
534
}
535
536
// Make an array of pointers to the control points, to get rid of indices.
537
const SimpleVertex **points = (const SimpleVertex **)managedBuf.Allocate(sizeof(SimpleVertex *) * num_points);
538
if (!points) {
539
ERROR_LOG(Log::G3D, "Failed to allocate space for control point pointers, skipping curve draw");
540
return;
541
}
542
for (int idx = 0; idx < num_points; idx++)
543
points[idx] = simplified_control_points + (indices ? ConvertIndex(idx) : idx);
544
545
OutputBuffers output;
546
output.vertices = (SimpleVertex *)(decoded_ + DECODED_VERTEX_BUFFER_SIZE / 2);
547
output.indices = decIndex_;
548
output.count = 0;
549
550
int maxVerts = DECODED_VERTEX_BUFFER_SIZE / 2 / vertexSize;
551
552
surface.Init(maxVerts);
553
554
if (CanUseHardwareTessellation(surface.primType)) {
555
HardwareTessellation(output, surface, origVertType, points, tessDataTransfer);
556
} else {
557
ControlPoints cpoints(points, num_points, managedBuf);
558
if (cpoints.IsValid())
559
SoftwareTessellation(output, surface, origVertType, cpoints);
560
else
561
ERROR_LOG(Log::G3D, "Failed to allocate space for control point values, skipping curve draw");
562
}
563
564
u32 vertTypeWithIndex16 = (vertType & ~GE_VTYPE_IDX_MASK) | GE_VTYPE_IDX_16BIT;
565
566
UVScale prevUVScale;
567
if (origVertType & GE_VTYPE_TC_MASK) {
568
// We scaled during Normalize already so let's turn it off when drawing.
569
prevUVScale = gstate_c.uv;
570
gstate_c.uv.uScale = 1.0f;
571
gstate_c.uv.vScale = 1.0f;
572
gstate_c.uv.uOff = 0;
573
gstate_c.uv.vOff = 0;
574
}
575
576
vertTypeID = GetVertTypeID(vertTypeWithIndex16, gstate.getUVGenMode(), applySkinInDecode_);
577
int generatedBytesRead;
578
if (output.count)
579
DispatchSubmitPrim(output.vertices, output.indices, PatchPrimToPrim(surface.primType), output.count, vertTypeID, true, &generatedBytesRead);
580
581
if (flushOnParams_)
582
Flush();
583
584
if (origVertType & GE_VTYPE_TC_MASK) {
585
gstate_c.uv = prevUVScale;
586
}
587
}
588
589