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