Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Common/SoftwareTransformCommon.cpp
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
#include <algorithm>
19
#include <cmath>
20
21
#include "Common/CPUDetect.h"
22
#include "Common/Math/math_util.h"
23
#include "Common/GPU/OpenGL/GLFeatures.h"
24
#include "Core/Config.h"
25
#include "Core/System.h"
26
#include "GPU/GPUState.h"
27
#include "GPU/Math3D.h"
28
#include "GPU/Common/FramebufferManagerCommon.h"
29
#include "GPU/Common/GPUStateUtils.h"
30
#include "GPU/Common/SoftwareTransformCommon.h"
31
#include "GPU/Common/TransformCommon.h"
32
#include "GPU/Common/VertexDecoderCommon.h"
33
#include "GPU/Common/DrawEngineCommon.h"
34
35
// This is the software transform pipeline, which is necessary for supporting RECT
36
// primitives correctly without geometry shaders, and may be easier to use for
37
// debugging than the hardware transform pipeline.
38
39
// There's code here that simply expands transformed RECTANGLES into plain triangles.
40
41
// We're gonna have to keep software transforming RECTANGLES, unless we use a geom shader which we can't on OpenGL ES 2.0.
42
// Usually, though, these primitives don't use lighting etc so it's no biggie performance wise, but it would be nice to get rid of
43
// this code.
44
45
// Actually, if we find the camera-relative right and down vectors, it might even be possible to add the extra points in pre-transformed
46
// space and thus make decent use of hardware transform.
47
48
// Actually again, single quads could be drawn more efficiently using GL_TRIANGLE_STRIP, no need to duplicate verts as for
49
// GL_TRIANGLES. Still need to sw transform to compute the extra two corners though.
50
//
51
52
// The verts are in the order: BR BL TL TR
53
static void SwapUVs(TransformedVertex &a, TransformedVertex &b) {
54
float tempu = a.u;
55
float tempv = a.v;
56
a.u = b.u;
57
a.v = b.v;
58
b.u = tempu;
59
b.v = tempv;
60
}
61
62
// 2 3 3 2 0 3 2 1
63
// to to or
64
// 1 0 0 1 1 2 3 0
65
66
// Note: 0 is BR and 2 is TL.
67
68
static void RotateUV(TransformedVertex v[4], bool flippedY) {
69
// We use the transformed tl/br coordinates to figure out whether they're flipped or not.
70
float ySign = flippedY ? -1.0 : 1.0;
71
72
const float x1 = v[2].x;
73
const float x2 = v[0].x;
74
const float y1 = v[2].y * ySign;
75
const float y2 = v[0].y * ySign;
76
77
if ((x1 < x2 && y1 < y2) || (x1 > x2 && y1 > y2))
78
SwapUVs(v[1], v[3]);
79
}
80
81
static void RotateUVThrough(TransformedVertex v[4]) {
82
float x1 = v[2].x;
83
float x2 = v[0].x;
84
float y1 = v[2].y;
85
float y2 = v[0].y;
86
87
if ((x1 < x2 && y1 > y2) || (x1 > x2 && y1 < y2))
88
SwapUVs(v[1], v[3]);
89
}
90
91
// Clears on the PSP are best done by drawing a series of vertical strips
92
// in clear mode. This tries to detect that.
93
static bool IsReallyAClear(const TransformedVertex *transformed, int numVerts, float x2, float y2) {
94
if (transformed[0].x < 0.0f || transformed[0].y < 0.0f || transformed[0].x > 0.5f || transformed[0].y > 0.5f)
95
return false;
96
97
const float originY = transformed[0].y;
98
99
// Color and Z are decided by the second vertex, so only need to check those for matching color.
100
const u32 matchcolor = transformed[1].color0_32;
101
const float matchz = transformed[1].z;
102
103
for (int i = 1; i < numVerts; i++) {
104
if ((i & 1) == 0) {
105
// Top left of a rectangle
106
if (transformed[i].y != originY)
107
return false;
108
float gap = fabsf(transformed[i].x - transformed[i - 1].x); // Should probably do some smarter check.
109
if (i > 0 && gap > 0.0625)
110
return false;
111
} else {
112
if (transformed[i].color0_32 != matchcolor || transformed[i].z != matchz)
113
return false;
114
// Bottom right
115
if (transformed[i].y < y2)
116
return false;
117
if (transformed[i].x <= transformed[i - 1].x)
118
return false;
119
}
120
}
121
122
// The last vertical strip often extends outside the drawing area.
123
if (transformed[numVerts - 1].x < x2)
124
return false;
125
126
return true;
127
}
128
129
void SoftwareTransform::SetProjMatrix(const float mtx[14], bool invertedX, bool invertedY, const Lin::Vec3 &trans, const Lin::Vec3 &scale) {
130
memcpy(&projMatrix_.m, mtx, 16 * sizeof(float));
131
132
if (invertedY) {
133
projMatrix_.xy = -projMatrix_.xy;
134
projMatrix_.yy = -projMatrix_.yy;
135
projMatrix_.zy = -projMatrix_.zy;
136
projMatrix_.wy = -projMatrix_.wy;
137
}
138
if (invertedX) {
139
projMatrix_.xx = -projMatrix_.xx;
140
projMatrix_.yx = -projMatrix_.yx;
141
projMatrix_.zx = -projMatrix_.zx;
142
projMatrix_.wx = -projMatrix_.wx;
143
}
144
145
projMatrix_.translateAndScale(trans, scale);
146
}
147
148
void SoftwareTransform::Transform(int prim, u32 vertType, const DecVtxFormat &decVtxFormat, int numDecodedVerts, SoftwareTransformResult *result) {
149
u8 *decoded = params_.decoded;
150
TransformedVertex *transformed = params_.transformed;
151
bool throughmode = (vertType & GE_VTYPE_THROUGH_MASK) != 0;
152
bool lmode = gstate.isUsingSecondaryColor() && gstate.isLightingEnabled();
153
154
float uscale = 1.0f;
155
float vscale = 1.0f;
156
if (throughmode && prim != GE_PRIM_RECTANGLES) {
157
// For through rectangles, we do this scaling in Expand.
158
uscale /= gstate_c.curTextureWidth;
159
vscale /= gstate_c.curTextureHeight;
160
}
161
162
const int w = gstate.getTextureWidth(0);
163
const int h = gstate.getTextureHeight(0);
164
float widthFactor = (float) w / (float) gstate_c.curTextureWidth;
165
float heightFactor = (float) h / (float) gstate_c.curTextureHeight;
166
167
Lighter lighter(vertType);
168
float fog_end = getFloat24(gstate.fog1);
169
float fog_slope = getFloat24(gstate.fog2);
170
// Same fixup as in ShaderManagerGLES.cpp
171
if (my_isnanorinf(fog_end)) {
172
// Not really sure what a sensible value might be, but let's try 64k.
173
fog_end = std::signbit(fog_end) ? -65535.0f : 65535.0f;
174
}
175
if (my_isnanorinf(fog_slope)) {
176
fog_slope = std::signbit(fog_slope) ? -65535.0f : 65535.0f;
177
}
178
179
VertexReader reader(decoded, decVtxFormat, vertType);
180
if (throughmode) {
181
const u32 materialAmbientRGBA = gstate.getMaterialAmbientRGBA();
182
const bool hasColor = reader.hasColor0();
183
const bool hasUV = reader.hasUV();
184
for (int index = 0; index < numDecodedVerts; index++) {
185
// Do not touch the coordinates or the colors. No lighting.
186
reader.Goto(index);
187
// TODO: Write to a flexible buffer, we don't always need all four components.
188
TransformedVertex &vert = transformed[index];
189
reader.ReadPosThrough(vert.pos);
190
vert.pos_w = 1.0f;
191
192
if (hasColor) {
193
vert.color0_32 = reader.ReadColor0_8888();
194
} else {
195
vert.color0_32 = materialAmbientRGBA;
196
}
197
198
if (hasUV) {
199
reader.ReadUV(vert.uv);
200
201
vert.u *= uscale;
202
vert.v *= vscale;
203
} else {
204
vert.u = 0.0f;
205
vert.v = 0.0f;
206
}
207
vert.uv_w = 1.0f;
208
209
// Ignore color1 and fog, never used in throughmode anyway.
210
// The w of uv is also never used (hardcoded to 1.0.)
211
}
212
} else {
213
const Vec4f materialAmbientRGBA = Vec4f::FromRGBA(gstate.getMaterialAmbientRGBA());
214
// Okay, need to actually perform the full transform.
215
for (int index = 0; index < numDecodedVerts; index++) {
216
reader.Goto(index);
217
218
float v[3] = {0, 0, 0};
219
Vec4f c0 = Vec4f(1, 1, 1, 1);
220
Vec4f c1 = Vec4f(0, 0, 0, 0);
221
float uv[3] = {0, 0, 1};
222
float fogCoef = 1.0f;
223
224
float out[3];
225
float pos[3];
226
Vec3f normal(0, 0, 1);
227
Vec3f worldnormal(0, 0, 1);
228
reader.ReadPosNonThrough(pos);
229
230
float ruv[2] = { 0.0f, 0.0f };
231
if (reader.hasUV())
232
reader.ReadUV(ruv);
233
234
Vec4f unlitColor;
235
if (reader.hasColor0())
236
reader.ReadColor0(unlitColor.AsArray());
237
else
238
unlitColor = materialAmbientRGBA;
239
if (reader.hasNormal())
240
reader.ReadNrm(normal.AsArray());
241
242
Vec3ByMatrix43(out, pos, gstate.worldMatrix);
243
if (reader.hasNormal()) {
244
if (gstate.areNormalsReversed()) {
245
normal = -normal;
246
}
247
Norm3ByMatrix43(worldnormal.AsArray(), normal.AsArray(), gstate.worldMatrix);
248
worldnormal = worldnormal.NormalizedOr001(cpu_info.bSSE4_1);
249
}
250
251
// Perform lighting here if enabled.
252
if (gstate.isLightingEnabled()) {
253
float litColor0[4];
254
float litColor1[4];
255
lighter.Light(litColor0, litColor1, unlitColor.AsArray(), out, worldnormal);
256
257
// Don't ignore gstate.lmode - we should send two colors in that case
258
for (int j = 0; j < 4; j++) {
259
c0[j] = litColor0[j];
260
}
261
if (lmode) {
262
// Separate colors
263
for (int j = 0; j < 4; j++) {
264
c1[j] = litColor1[j];
265
}
266
} else {
267
// Summed color into c0 (will clamp in ToRGBA().)
268
for (int j = 0; j < 4; j++) {
269
c0[j] += litColor1[j];
270
}
271
}
272
} else {
273
for (int j = 0; j < 4; j++) {
274
c0[j] = unlitColor[j];
275
}
276
if (lmode) {
277
// c1 is already 0.
278
}
279
}
280
281
// Perform texture coordinate generation after the transform and lighting - one style of UV depends on lights.
282
switch (gstate.getUVGenMode()) {
283
case GE_TEXMAP_TEXTURE_COORDS: // UV mapping
284
case GE_TEXMAP_UNKNOWN: // Seen in Riviera. Unsure of meaning, but this works.
285
// We always prescale in the vertex decoder now.
286
uv[0] = ruv[0];
287
uv[1] = ruv[1];
288
uv[2] = 1.0f;
289
break;
290
291
case GE_TEXMAP_TEXTURE_MATRIX:
292
{
293
// Projection mapping
294
Vec3f source(0.0f, 0.0f, 1.0f);
295
switch (gstate.getUVProjMode()) {
296
case GE_PROJMAP_POSITION: // Use model space XYZ as source
297
source = pos;
298
break;
299
300
case GE_PROJMAP_UV: // Use unscaled UV as source
301
source = Vec3f(ruv[0], ruv[1], 0.0f);
302
break;
303
304
case GE_PROJMAP_NORMALIZED_NORMAL: // Use normalized normal as source
305
source = normal.Normalized(cpu_info.bSSE4_1);
306
break;
307
308
case GE_PROJMAP_NORMAL: // Use non-normalized normal as source!
309
source = normal;
310
break;
311
}
312
313
float uvw[3];
314
Vec3ByMatrix43(uvw, &source.x, gstate.tgenMatrix);
315
uv[0] = uvw[0];
316
uv[1] = uvw[1];
317
uv[2] = uvw[2];
318
}
319
break;
320
321
case GE_TEXMAP_ENVIRONMENT_MAP:
322
// Shade mapping - use two light sources to generate U and V.
323
{
324
auto getLPosFloat = [&](int l, int i) {
325
return getFloat24(gstate.lpos[l * 3 + i]);
326
};
327
auto getLPos = [&](int l) {
328
return Vec3f(getLPosFloat(l, 0), getLPosFloat(l, 1), getLPosFloat(l, 2));
329
};
330
auto calcShadingLPos = [&](int l) {
331
Vec3f pos = getLPos(l);
332
return pos.NormalizedOr001(cpu_info.bSSE4_1);
333
};
334
335
// Might not have lighting enabled, so don't use lighter.
336
Vec3f lightpos0 = calcShadingLPos(gstate.getUVLS0());
337
Vec3f lightpos1 = calcShadingLPos(gstate.getUVLS1());
338
339
uv[0] = (1.0f + Dot(lightpos0, worldnormal))/2.0f;
340
uv[1] = (1.0f + Dot(lightpos1, worldnormal))/2.0f;
341
uv[2] = 1.0f;
342
}
343
break;
344
default:
345
break;
346
}
347
348
uv[0] = uv[0] * widthFactor;
349
uv[1] = uv[1] * heightFactor;
350
351
// Transform the coord by the view matrix.
352
Vec3ByMatrix43(v, out, gstate.viewMatrix);
353
fogCoef = (v[2] + fog_end) * fog_slope;
354
355
// TODO: Write to a flexible buffer, we don't always need all four components.
356
Vec3ByMatrix44(transformed[index].pos, v, projMatrix_.m);
357
transformed[index].fog = fogCoef;
358
memcpy(&transformed[index].uv, uv, 3 * sizeof(float));
359
transformed[index].color0_32 = c0.ToRGBA();
360
transformed[index].color1_32 = c1.ToRGBA();
361
362
// Vertex depth rounding is done in the shader, to simulate the 16-bit depth buffer.
363
}
364
}
365
366
// Here's the best opportunity to try to detect rectangles used to clear the screen, and
367
// replace them with real clears. This can provide a speedup on certain mobile chips.
368
//
369
// An alternative option is to simply ditch all the verts except the first and last to create a single
370
// rectangle out of many. Quite a small optimization though.
371
// TODO: This bleeds outside the play area in non-buffered mode. Big deal? Probably not.
372
// TODO: Allow creating a depth clear and a color draw.
373
bool reallyAClear = false;
374
if (numDecodedVerts > 1 && prim == GE_PRIM_RECTANGLES && gstate.isModeClear() && throughmode) {
375
int scissorX2 = gstate.getScissorX2() + 1;
376
int scissorY2 = gstate.getScissorY2() + 1;
377
reallyAClear = IsReallyAClear(transformed, numDecodedVerts, scissorX2, scissorY2);
378
if (reallyAClear && gstate.getColorMask() != 0xFFFFFFFF && (gstate.isClearModeColorMask() || gstate.isClearModeAlphaMask())) {
379
result->setSafeSize = true;
380
result->safeWidth = scissorX2;
381
result->safeHeight = scissorY2;
382
}
383
}
384
if (params_.allowClear && reallyAClear && gl_extensions.gpuVendor != GPU_VENDOR_IMGTEC) {
385
// If alpha is not allowed to be separate, it must match for both depth/stencil and color. Vulkan requires this.
386
bool alphaMatchesColor = gstate.isClearModeColorMask() == gstate.isClearModeAlphaMask();
387
bool depthMatchesStencil = gstate.isClearModeAlphaMask() == gstate.isClearModeDepthMask();
388
bool matchingComponents = params_.allowSeparateAlphaClear || (alphaMatchesColor && depthMatchesStencil);
389
bool stencilNotMasked = !gstate.isClearModeAlphaMask() || gstate.getStencilWriteMask() == 0x00;
390
if (matchingComponents && stencilNotMasked) {
391
DepthScaleFactors depthScale = GetDepthScaleFactors(gstate_c.UseFlags());
392
result->color = transformed[1].color0_32;
393
// Need to rescale from a [0, 1] float. This is the final transformed value.
394
result->depth = depthScale.EncodeFromU16((float)(int)(transformed[1].z * 65535.0f));
395
result->action = SW_CLEAR;
396
gpuStats.numClears++;
397
return;
398
}
399
}
400
401
// Detect full screen "clears" that might not be so obvious, to set the safe size if possible.
402
if (!result->setSafeSize && prim == GE_PRIM_RECTANGLES && numDecodedVerts == 2 && throughmode) {
403
bool clearingColor = gstate.isModeClear() && (gstate.isClearModeColorMask() || gstate.isClearModeAlphaMask());
404
bool writingColor = gstate.getColorMask() != 0xFFFFFFFF;
405
bool startsZeroX = transformed[0].x <= 0.0f && transformed[1].x > 0.0f && transformed[1].x > transformed[0].x;
406
bool startsZeroY = transformed[0].y <= 0.0f && transformed[1].y > 0.0f && transformed[1].y > transformed[0].y;
407
408
if (startsZeroX && startsZeroY && (clearingColor || writingColor)) {
409
int scissorX2 = gstate.getScissorX2() + 1;
410
int scissorY2 = gstate.getScissorY2() + 1;
411
result->setSafeSize = true;
412
result->safeWidth = std::min(scissorX2, (int)transformed[1].x);
413
result->safeHeight = std::min(scissorY2, (int)transformed[1].y);
414
}
415
}
416
}
417
418
void SoftwareTransform::BuildDrawingParams(int prim, int vertexCount, u32 vertType, u16 *&inds, int indsSize, int &numDecodedVerts, int vertsSize, SoftwareTransformResult *result) {
419
TransformedVertex *transformed = params_.transformed;
420
TransformedVertex *transformedExpanded = params_.transformedExpanded;
421
bool throughmode = (vertType & GE_VTYPE_THROUGH_MASK) != 0;
422
423
// Step 2: expand and process primitives.
424
result->drawBuffer = transformed;
425
int numTrans = 0;
426
427
FramebufferManagerCommon *fbman = params_.fbman;
428
bool useBufferedRendering = fbman->UseBufferedRendering();
429
430
if (prim == GE_PRIM_RECTANGLES) {
431
if (!ExpandRectangles(vertexCount, numDecodedVerts, vertsSize, inds, indsSize, transformed, transformedExpanded, numTrans, throughmode, &result->pixelMapped)) {
432
result->drawNumTrans = 0;
433
result->pixelMapped = false;
434
return;
435
}
436
result->drawBuffer = transformedExpanded;
437
438
// We don't know the color until here, so we have to do it now, instead of in StateMapping.
439
// Might want to reconsider the order of things later...
440
if (gstate.isModeClear() && gstate.isClearModeAlphaMask()) {
441
result->setStencil = true;
442
if (vertexCount > 1) {
443
// Take the bottom right alpha value of the first rect as the stencil value.
444
// Technically, each rect could individually fill its stencil, but most of the
445
// time they use the same one.
446
result->stencilValue = transformed[inds[1]].color0[3];
447
} else {
448
result->stencilValue = 0;
449
}
450
}
451
} else if (prim == GE_PRIM_POINTS) {
452
result->pixelMapped = false;
453
if (!ExpandPoints(vertexCount, numDecodedVerts, vertsSize, inds, indsSize, transformed, transformedExpanded, numTrans, throughmode)) {
454
result->drawNumTrans = 0;
455
return;
456
}
457
result->drawBuffer = transformedExpanded;
458
} else if (prim == GE_PRIM_LINES) {
459
result->pixelMapped = false;
460
if (!ExpandLines(vertexCount, numDecodedVerts, vertsSize, inds, indsSize, transformed, transformedExpanded, numTrans, throughmode)) {
461
result->drawNumTrans = 0;
462
return;
463
}
464
result->drawBuffer = transformedExpanded;
465
} else {
466
// We can simply draw the unexpanded buffer.
467
numTrans = vertexCount;
468
result->pixelMapped = false;
469
470
// If we don't support custom cull in the shader, process it here.
471
if (!gstate_c.Use(GPU_USE_CULL_DISTANCE) && vertexCount > 0 && !throughmode) {
472
const u16 *indsIn = (const u16 *)inds;
473
u16 *newInds = inds + vertexCount;
474
u16 *indsOut = newInds;
475
476
float minZValue, maxZValue;
477
CalcCullParams(minZValue, maxZValue);
478
479
std::vector<int> outsideZ;
480
outsideZ.resize(vertexCount);
481
482
// First, check inside/outside directions for each index.
483
for (int i = 0; i < vertexCount; ++i) {
484
float z = transformed[indsIn[i]].z / transformed[indsIn[i]].pos_w;
485
if (z > maxZValue)
486
outsideZ[i] = 1;
487
else if (z < minZValue)
488
outsideZ[i] = -1;
489
else
490
outsideZ[i] = 0;
491
}
492
493
// Now, for each primitive type, throw away the indices if:
494
// - Depth clamp on, and ALL verts are outside *in the same direction*.
495
// - Depth clamp off, and ANY vert is outside.
496
if (prim == GE_PRIM_TRIANGLES && gstate.isDepthClampEnabled()) {
497
numTrans = 0;
498
for (int i = 0; i < vertexCount - 2; i += 3) {
499
if (outsideZ[i + 0] != 0 && outsideZ[i + 0] == outsideZ[i + 1] && outsideZ[i + 0] == outsideZ[i + 2]) {
500
// All outside, and all the same direction. Nuke this triangle.
501
continue;
502
}
503
504
memcpy(indsOut, indsIn + i, 3 * sizeof(uint16_t));
505
indsOut += 3;
506
numTrans += 3;
507
}
508
509
inds = newInds;
510
} else if (prim == GE_PRIM_TRIANGLES) {
511
numTrans = 0;
512
for (int i = 0; i < vertexCount - 2; i += 3) {
513
if (outsideZ[i + 0] != 0 || outsideZ[i + 1] != 0 || outsideZ[i + 2] != 0) {
514
// Even one outside, and we cull.
515
continue;
516
}
517
518
memcpy(indsOut, indsIn + i, 3 * sizeof(uint16_t));
519
indsOut += 3;
520
numTrans += 3;
521
}
522
523
inds = newInds;
524
}
525
} else if (throughmode && g_Config.bSmart2DTexFiltering && !gstate_c.textureIsVideo) {
526
// We check some common cases for pixel mapping.
527
// TODO: It's not really optimal that some previous step has removed the triangle strip.
528
if (vertexCount <= 6 && prim == GE_PRIM_TRIANGLES) {
529
// It's enough to check UV deltas vs pos deltas between vertex pairs:
530
// 0-1 1-3 3-2 2-0. Maybe can even skip the last one. Probably some simple math can get us that sequence.
531
// Unfortunately we need to reverse the previous UV scaling operation. Fortunately these are powers of two
532
// so the operations are exact.
533
bool pixelMapped = true;
534
const u16 *indsIn = (const u16 *)inds;
535
const float uscale = gstate_c.curTextureWidth;
536
const float vscale = gstate_c.curTextureHeight;
537
for (int t = 0; t < vertexCount; t += 3) {
538
struct { int a; int b; } pairs[] = { {0, 1}, {1, 2}, {2, 0} };
539
for (int i = 0; i < ARRAY_SIZE(pairs); i++) {
540
int a = indsIn[t + pairs[i].a];
541
int b = indsIn[t + pairs[i].b];
542
float du = fabsf((transformed[a].u - transformed[b].u) * uscale);
543
float dv = fabsf((transformed[a].v - transformed[b].v) * vscale);
544
float dx = fabsf(transformed[a].x - transformed[b].x);
545
float dy = fabsf(transformed[a].y - transformed[b].y);
546
if (du != dx || dv != dy) {
547
pixelMapped = false;
548
}
549
}
550
if (!pixelMapped) {
551
break;
552
}
553
}
554
result->pixelMapped = pixelMapped;
555
}
556
}
557
}
558
559
if (gstate.isModeClear()) {
560
gpuStats.numClears++;
561
}
562
563
result->action = SW_DRAW_INDEXED;
564
result->drawNumTrans = numTrans;
565
}
566
567
void SoftwareTransform::CalcCullParams(float &minZValue, float &maxZValue) const {
568
// The projected Z can be up to 0x3F8000FF, which is where this constant is from.
569
// It seems like it may only maintain 15 mantissa bits (excluding implied.)
570
maxZValue = 1.000030517578125f * gstate_c.vpDepthScale;
571
minZValue = -maxZValue;
572
// Scale and offset the Z appropriately, since we baked that into a projection transform.
573
if (params_.usesHalfZ) {
574
maxZValue = maxZValue * 0.5f + 0.5f + gstate_c.vpZOffset * 0.5f;
575
minZValue = minZValue * 0.5f + 0.5f + gstate_c.vpZOffset * 0.5f;
576
} else {
577
maxZValue += gstate_c.vpZOffset;
578
minZValue += gstate_c.vpZOffset;
579
}
580
// In case scale was negative, flip.
581
if (minZValue > maxZValue)
582
std::swap(minZValue, maxZValue);
583
}
584
585
bool SoftwareTransform::ExpandRectangles(int vertexCount, int &numDecodedVerts, int vertsSize, u16 *&inds, int indsSize, const TransformedVertex *transformed, TransformedVertex *transformedExpanded, int &numTrans, bool throughmode, bool *pixelMappedExactly) const {
586
// Before we start, do a sanity check - does the output fit?
587
if ((vertexCount / 2) * 6 > indsSize) {
588
// Won't fit, kill the draw.
589
return false;
590
}
591
if ((vertexCount / 2) * 4 > vertsSize) {
592
// Won't fit, kill the draw.
593
return false;
594
}
595
596
// Rectangles always need 2 vertices, disregard the last one if there's an odd number.
597
vertexCount = vertexCount & ~1;
598
numTrans = 0;
599
TransformedVertex *trans = &transformedExpanded[0];
600
601
const u16 *indsIn = (const u16 *)inds;
602
u16 *newInds = inds + vertexCount;
603
u16 *indsOut = newInds;
604
605
numDecodedVerts = 4 * (vertexCount / 2);
606
607
float uscale = 1.0f;
608
float vscale = 1.0f;
609
if (throughmode) {
610
uscale /= gstate_c.curTextureWidth;
611
vscale /= gstate_c.curTextureHeight;
612
}
613
614
bool pixelMapped = g_Config.bSmart2DTexFiltering && !gstate_c.textureIsVideo;
615
616
for (int i = 0; i < vertexCount; i += 2) {
617
const TransformedVertex &transVtxTL = transformed[indsIn[i + 0]];
618
const TransformedVertex &transVtxBR = transformed[indsIn[i + 1]];
619
620
if (pixelMapped) {
621
float dx = transVtxBR.x - transVtxTL.x;
622
float dy = transVtxBR.y - transVtxTL.y;
623
float du = transVtxBR.u - transVtxTL.u;
624
float dv = transVtxBR.v - transVtxTL.v;
625
626
// NOTE: We will accept it as pixel mapped if only one dimension is stretched. This fixes dialog frames in FFI.
627
// Though, there could be false positives in other games due to this. Let's see if it is a problem...
628
if (dx <= 0 || dy <= 0 || (dx != du && dy != dv)) {
629
pixelMapped = false;
630
}
631
}
632
633
// We have to turn the rectangle into two triangles, so 6 points.
634
// This is 4 verts + 6 indices.
635
636
// bottom right
637
trans[0] = transVtxBR;
638
trans[0].u = transVtxBR.u * uscale;
639
trans[0].v = transVtxBR.v * vscale;
640
641
// top right
642
trans[1] = transVtxBR;
643
trans[1].y = transVtxTL.y;
644
trans[1].u = transVtxBR.u * uscale;
645
trans[1].v = transVtxTL.v * vscale;
646
647
// top left
648
trans[2] = transVtxBR;
649
trans[2].x = transVtxTL.x;
650
trans[2].y = transVtxTL.y;
651
trans[2].u = transVtxTL.u * uscale;
652
trans[2].v = transVtxTL.v * vscale;
653
654
// bottom left
655
trans[3] = transVtxBR;
656
trans[3].x = transVtxTL.x;
657
trans[3].u = transVtxTL.u * uscale;
658
trans[3].v = transVtxBR.v * vscale;
659
660
// That's the four corners. Now process UV rotation.
661
if (throughmode) {
662
RotateUVThrough(trans);
663
} else {
664
RotateUV(trans, params_.flippedY);
665
}
666
667
// Triangle: BR-TR-TL
668
indsOut[0] = i * 2 + 0;
669
indsOut[1] = i * 2 + 1;
670
indsOut[2] = i * 2 + 2;
671
// Triangle: BL-BR-TL
672
indsOut[3] = i * 2 + 3;
673
indsOut[4] = i * 2 + 0;
674
indsOut[5] = i * 2 + 2;
675
676
trans += 4;
677
indsOut += 6;
678
679
numTrans += 6;
680
}
681
inds = newInds;
682
*pixelMappedExactly = pixelMapped;
683
return true;
684
}
685
686
// In-place. So, better not be doing this on GPU memory!
687
void IndexBufferProvokingLastToFirst(int prim, u16 *inds, int indsSize) {
688
switch (prim) {
689
case GE_PRIM_LINES:
690
// Swap every two indices.
691
for (int i = 0; i < indsSize - 1; i += 2) {
692
u16 temp = inds[i];
693
inds[i] = inds[i + 1];
694
inds[i + 1] = temp;
695
}
696
break;
697
case GE_PRIM_TRIANGLES:
698
// Rotate the triangle so the last becomes the first, without changing the winding order.
699
// This could be done with a series of pshufb.
700
for (int i = 0; i < indsSize - 2; i += 3) {
701
u16 temp = inds[i + 2];
702
inds[i + 2] = inds[i + 1];
703
inds[i + 1] = inds[i];
704
inds[i] = temp;
705
}
706
break;
707
case GE_PRIM_POINTS:
708
// Nothing to do,
709
break;
710
case GE_PRIM_RECTANGLES:
711
// Nothing to do, already using the 2nd vertex.
712
break;
713
default:
714
_dbg_assert_msg_(false, "IndexBufferProvokingFirstToLast: Only works with plain indexed primitives, no strips or fans")
715
}
716
}
717
718
bool SoftwareTransform::ExpandLines(int vertexCount, int &numDecodedVerts, int vertsSize, u16 *&inds, int indsSize, const TransformedVertex *transformed, TransformedVertex *transformedExpanded, int &numTrans, bool throughmode) {
719
// Before we start, do a sanity check - does the output fit?
720
if ((vertexCount / 2) * 6 > indsSize) {
721
// Won't fit, kill the draw.
722
return false;
723
}
724
if ((vertexCount / 2) * 4 > vertsSize) {
725
return false;
726
}
727
728
// Lines always need 2 vertices, disregard the last one if there's an odd number.
729
vertexCount = vertexCount & ~1;
730
numTrans = 0;
731
TransformedVertex *trans = &transformedExpanded[0];
732
733
const u16 *indsIn = (const u16 *)inds;
734
u16 *newInds = inds + vertexCount;
735
u16 *indsOut = newInds;
736
737
float dx = 1.0f * gstate_c.vpWidthScale * (1.0f / fabsf(gstate.getViewportXScale()));
738
float dy = 1.0f * gstate_c.vpHeightScale * (1.0f / fabsf(gstate.getViewportYScale()));
739
float du = 1.0f / gstate_c.curTextureWidth;
740
float dv = 1.0f / gstate_c.curTextureHeight;
741
742
if (throughmode) {
743
dx = 1.0f;
744
dy = 1.0f;
745
}
746
747
numDecodedVerts = 4 * (vertexCount / 2);
748
749
if (PSP_CoreParameter().compat.flags().CenteredLines) {
750
// Lines meant to be pretty in 3D like in Echochrome.
751
752
// We expand them in both directions for symmetry, so we need to halve the expansion.
753
dx *= 0.5f;
754
dy *= 0.5f;
755
756
for (int i = 0; i < vertexCount; i += 2) {
757
const TransformedVertex &transVtx1 = transformed[indsIn[i + 0]];
758
const TransformedVertex &transVtx2 = transformed[indsIn[i + 1]];
759
760
// Okay, let's calculate the perpendicular.
761
float horizontal = transVtx2.x * transVtx2.pos_w - transVtx1.x * transVtx1.pos_w;
762
float vertical = transVtx2.y * transVtx2.pos_w - transVtx1.y * transVtx1.pos_w;
763
764
Vec2f addWidth = Vec2f(-vertical, horizontal).Normalized();
765
766
float xoff = addWidth.x * dx;
767
float yoff = addWidth.y * dy;
768
769
// bottom right
770
trans[0].CopyFromWithOffset(transVtx2, xoff * transVtx2.pos_w, yoff * transVtx2.pos_w);
771
// top right
772
trans[1].CopyFromWithOffset(transVtx1, xoff * transVtx1.pos_w, yoff * transVtx1.pos_w);
773
// top left
774
trans[2].CopyFromWithOffset(transVtx1, -xoff * transVtx1.pos_w, -yoff * transVtx1.pos_w);
775
// bottom left
776
trans[3].CopyFromWithOffset(transVtx2, -xoff * transVtx2.pos_w, -yoff * transVtx2.pos_w);
777
778
// Triangle: BR-TR-TL
779
indsOut[0] = i * 2 + 0;
780
indsOut[1] = i * 2 + 1;
781
indsOut[2] = i * 2 + 2;
782
// Triangle: BL-BR-TL
783
indsOut[3] = i * 2 + 3;
784
indsOut[4] = i * 2 + 0;
785
indsOut[5] = i * 2 + 2;
786
trans += 4;
787
indsOut += 6;
788
789
numTrans += 6;
790
}
791
} else {
792
// Lines meant to be as closely compatible with upscaled 2D drawing as possible.
793
// We use this as default.
794
795
for (int i = 0; i < vertexCount; i += 2) {
796
const TransformedVertex &transVtx1 = transformed[indsIn[i + 0]];
797
const TransformedVertex &transVtx2 = transformed[indsIn[i + 1]];
798
799
const TransformedVertex &transVtxT = transVtx1.y <= transVtx2.y ? transVtx1 : transVtx2;
800
const TransformedVertex &transVtxB = transVtx1.y <= transVtx2.y ? transVtx2 : transVtx1;
801
const TransformedVertex &transVtxL = transVtx1.x <= transVtx2.x ? transVtx1 : transVtx2;
802
const TransformedVertex &transVtxR = transVtx1.x <= transVtx2.x ? transVtx2 : transVtx1;
803
804
// Sort the points so our perpendicular will bias the right direction.
805
const TransformedVertex &transVtxTL = (transVtxT.y != transVtxB.y || transVtxT.x > transVtxB.x) ? transVtxT : transVtxB;
806
const TransformedVertex &transVtxBL = (transVtxT.y != transVtxB.y || transVtxT.x > transVtxB.x) ? transVtxB : transVtxT;
807
808
// Okay, let's calculate the perpendicular.
809
float horizontal = transVtxTL.x * transVtxTL.pos_w - transVtxBL.x * transVtxBL.pos_w;
810
float vertical = transVtxTL.y * transVtxTL.pos_w - transVtxBL.y * transVtxBL.pos_w;
811
Vec2f addWidth = Vec2f(-vertical, horizontal).Normalized();
812
813
// bottom right
814
trans[0] = transVtxBL;
815
trans[0].x += addWidth.x * dx * trans[0].pos_w;
816
trans[0].y += addWidth.y * dy * trans[0].pos_w;
817
trans[0].u += addWidth.x * du * trans[0].uv_w;
818
trans[0].v += addWidth.y * dv * trans[0].uv_w;
819
820
// top right
821
trans[1] = transVtxTL;
822
trans[1].x += addWidth.x * dx * trans[1].pos_w;
823
trans[1].y += addWidth.y * dy * trans[1].pos_w;
824
trans[1].u += addWidth.x * du * trans[1].uv_w;
825
trans[1].v += addWidth.y * dv * trans[1].uv_w;
826
827
// top left
828
trans[2] = transVtxTL;
829
830
// bottom left
831
trans[3] = transVtxBL;
832
833
// Triangle: BR-TR-TL
834
indsOut[0] = i * 2 + 0;
835
indsOut[1] = i * 2 + 1;
836
indsOut[2] = i * 2 + 2;
837
// Triangle: BL-BR-TL
838
indsOut[3] = i * 2 + 3;
839
indsOut[4] = i * 2 + 0;
840
indsOut[5] = i * 2 + 2;
841
trans += 4;
842
indsOut += 6;
843
844
numTrans += 6;
845
}
846
}
847
848
inds = newInds;
849
return true;
850
}
851
852
bool SoftwareTransform::ExpandPoints(int vertexCount, int &maxIndex, int vertsSize, u16 *&inds, int indsSize, const TransformedVertex *transformed, TransformedVertex *transformedExpanded, int &numTrans, bool throughmode) {
853
// Before we start, do a sanity check - does the output fit?
854
if (vertexCount * 6 > indsSize) {
855
// Won't fit, kill the draw.
856
return false;
857
}
858
if (vertexCount * 4 > vertsSize) {
859
// Won't fit, kill the draw.
860
return false;
861
}
862
863
numTrans = 0;
864
TransformedVertex *trans = &transformedExpanded[0];
865
866
const u16 *indsIn = (const u16 *)inds;
867
u16 *newInds = inds + vertexCount;
868
u16 *indsOut = newInds;
869
870
float dx = 1.0f * gstate_c.vpWidthScale * (1.0f / gstate.getViewportXScale());
871
float dy = 1.0f * gstate_c.vpHeightScale * (1.0f / gstate.getViewportYScale());
872
float du = 1.0f / gstate_c.curTextureWidth;
873
float dv = 1.0f / gstate_c.curTextureHeight;
874
875
if (throughmode) {
876
dx = 1.0f;
877
dy = 1.0f;
878
}
879
880
maxIndex = 4 * vertexCount;
881
for (int i = 0; i < vertexCount; ++i) {
882
const TransformedVertex &transVtxTL = transformed[indsIn[i]];
883
884
// Create the bottom right version.
885
TransformedVertex transVtxBR = transVtxTL;
886
transVtxBR.x += dx * transVtxTL.pos_w;
887
transVtxBR.y += dy * transVtxTL.pos_w;
888
transVtxBR.u += du * transVtxTL.uv_w;
889
transVtxBR.v += dv * transVtxTL.uv_w;
890
891
// We have to turn the rectangle into two triangles, so 6 points.
892
// This is 4 verts + 6 indices.
893
894
// bottom right
895
trans[0] = transVtxBR;
896
897
// top right
898
trans[1] = transVtxBR;
899
trans[1].y = transVtxTL.y;
900
trans[1].v = transVtxTL.v;
901
902
// top left
903
trans[2] = transVtxBR;
904
trans[2].x = transVtxTL.x;
905
trans[2].y = transVtxTL.y;
906
trans[2].u = transVtxTL.u;
907
trans[2].v = transVtxTL.v;
908
909
// bottom left
910
trans[3] = transVtxBR;
911
trans[3].x = transVtxTL.x;
912
trans[3].u = transVtxTL.u;
913
914
// Triangle: BR-TR-TL
915
indsOut[0] = i * 4 + 0;
916
indsOut[1] = i * 4 + 1;
917
indsOut[2] = i * 4 + 2;
918
// Triangle: BL-BR-TL
919
indsOut[3] = i * 4 + 3;
920
indsOut[4] = i * 4 + 0;
921
indsOut[5] = i * 4 + 2;
922
trans += 4;
923
indsOut += 6;
924
925
numTrans += 6;
926
}
927
inds = newInds;
928
return true;
929
}
930
931
// This normalizes a set of vertices in any format to SimpleVertex format, by processing away morphing AND skinning.
932
// The rest of the transform pipeline like lighting will go as normal, either hardware or software.
933
// The implementation is initially a bit inefficient but shouldn't be a big deal.
934
// An intermediate buffer of not-easy-to-predict size is stored at bufPtr.
935
u32 NormalizeVertices(SimpleVertex *sverts, u8 *bufPtr, const u8 *inPtr, int lowerBound, int upperBound, const VertexDecoder *dec, u32 vertType) {
936
// First, decode the vertices into a GPU compatible format. This step can be eliminated but will need a separate
937
// implementation of the vertex decoder.
938
dec->DecodeVerts(bufPtr, inPtr, &gstate_c.uv, lowerBound, upperBound);
939
940
// OK, morphing eliminated but bones still remain to be taken care of.
941
// Let's do a partial software transform where we only do skinning.
942
943
VertexReader reader(bufPtr, dec->GetDecVtxFmt(), vertType);
944
945
const u8 defaultColor[4] = {
946
(u8)gstate.getMaterialAmbientR(),
947
(u8)gstate.getMaterialAmbientG(),
948
(u8)gstate.getMaterialAmbientB(),
949
(u8)gstate.getMaterialAmbientA(),
950
};
951
952
// Let's have two separate loops, one for non skinning and one for skinning.
953
if (!dec->skinInDecode && (vertType & GE_VTYPE_WEIGHT_MASK) != GE_VTYPE_WEIGHT_NONE) {
954
int numBoneWeights = vertTypeGetNumBoneWeights(vertType);
955
for (int i = lowerBound; i <= upperBound; i++) {
956
reader.Goto(i - lowerBound);
957
SimpleVertex &sv = sverts[i];
958
if (vertType & GE_VTYPE_TC_MASK) {
959
reader.ReadUV(sv.uv);
960
}
961
962
if (vertType & GE_VTYPE_COL_MASK) {
963
sv.color_32 = reader.ReadColor0_8888();
964
} else {
965
memcpy(sv.color, defaultColor, 4);
966
}
967
968
float nrm[3], pos[3];
969
float bnrm[3], bpos[3];
970
971
if (vertType & GE_VTYPE_NRM_MASK) {
972
// Normals are generated during tessellation anyway, not sure if any need to supply
973
reader.ReadNrm(nrm);
974
} else {
975
nrm[0] = 0;
976
nrm[1] = 0;
977
nrm[2] = 1.0f;
978
}
979
reader.ReadPosAuto(pos);
980
981
// Apply skinning transform directly
982
float weights[8];
983
reader.ReadWeights(weights);
984
// Skinning
985
Vec3Packedf psum(0, 0, 0);
986
Vec3Packedf nsum(0, 0, 0);
987
for (int w = 0; w < numBoneWeights; w++) {
988
if (weights[w] != 0.0f) {
989
Vec3ByMatrix43(bpos, pos, gstate.boneMatrix + w * 12);
990
Vec3Packedf tpos(bpos);
991
psum += tpos * weights[w];
992
993
Norm3ByMatrix43(bnrm, nrm, gstate.boneMatrix + w * 12);
994
Vec3Packedf tnorm(bnrm);
995
nsum += tnorm * weights[w];
996
}
997
}
998
sv.pos = psum;
999
sv.nrm = nsum;
1000
}
1001
} else {
1002
for (int i = lowerBound; i <= upperBound; i++) {
1003
reader.Goto(i - lowerBound);
1004
SimpleVertex &sv = sverts[i];
1005
if (vertType & GE_VTYPE_TC_MASK) {
1006
reader.ReadUV(sv.uv);
1007
} else {
1008
sv.uv[0] = 0.0f; // This will get filled in during tessellation
1009
sv.uv[1] = 0.0f;
1010
}
1011
if (vertType & GE_VTYPE_COL_MASK) {
1012
sv.color_32 = reader.ReadColor0_8888();
1013
} else {
1014
memcpy(sv.color, defaultColor, 4);
1015
}
1016
if (vertType & GE_VTYPE_NRM_MASK) {
1017
// Normals are generated during tessellation anyway, not sure if any need to supply
1018
reader.ReadNrm((float *)&sv.nrm);
1019
} else {
1020
sv.nrm.x = 0.0f;
1021
sv.nrm.y = 0.0f;
1022
sv.nrm.z = 1.0f;
1023
}
1024
reader.ReadPosAuto((float *)&sv.pos);
1025
}
1026
}
1027
1028
// Okay, there we are! Return the new type (but keep the index bits)
1029
return GE_VTYPE_TC_FLOAT | GE_VTYPE_COL_8888 | GE_VTYPE_NRM_FLOAT | GE_VTYPE_POS_FLOAT | (vertType & (GE_VTYPE_IDX_MASK | GE_VTYPE_THROUGH));
1030
}
1031
1032
// clip space to screen space
1033
Vec3f ClipToScreen(const Vec4f& coords) {
1034
float xScale = gstate.getViewportXScale();
1035
float xCenter = gstate.getViewportXCenter();
1036
float yScale = gstate.getViewportYScale();
1037
float yCenter = gstate.getViewportYCenter();
1038
float zScale = gstate.getViewportZScale();
1039
float zCenter = gstate.getViewportZCenter();
1040
1041
float x = coords.x * xScale / coords.w + xCenter;
1042
float y = coords.y * yScale / coords.w + yCenter;
1043
float z = coords.z * zScale / coords.w + zCenter;
1044
1045
// 16 = 0xFFFF / 4095.9375
1046
return Vec3f(x * 16 - gstate.getOffsetX16(), y * 16 - gstate.getOffsetY16(), z);
1047
}
1048
1049
static Vec3f ScreenToDrawing(const Vec3f& coords) {
1050
Vec3f ret;
1051
ret.x = coords.x * (1.0f / 16.0f);
1052
ret.y = coords.y * (1.0f / 16.0f);
1053
ret.z = coords.z;
1054
return ret;
1055
}
1056
1057
// TODO: This probably is not the best interface.
1058
// drawEngine is just for the vertex decoder lookup.
1059
// This is really just for vertex preview in the debugger, not for actual rendering!
1060
bool GetCurrentDrawAsDebugVertices(DrawEngineCommon *drawEngine, int count, std::vector<GPUDebugVertex> &vertices, std::vector<u16> &indices) {
1061
// This is always for the current vertices.
1062
u16 indexLowerBound = 0;
1063
u16 indexUpperBound = count - 1;
1064
1065
if (!Memory::IsValidAddress(gstate_c.vertexAddr) || count == 0)
1066
return false;
1067
1068
bool savedVertexFullAlpha = gstate_c.vertexFullAlpha;
1069
1070
if ((gstate.vertType & GE_VTYPE_IDX_MASK) != GE_VTYPE_IDX_NONE) {
1071
const u8 *inds = Memory::GetPointer(gstate_c.indexAddr);
1072
const u16_le *inds16 = (const u16_le *)inds;
1073
const u32_le *inds32 = (const u32_le *)inds;
1074
1075
if (inds) {
1076
GetIndexBounds(inds, count, gstate.vertType, &indexLowerBound, &indexUpperBound);
1077
indices.resize(count);
1078
switch (gstate.vertType & GE_VTYPE_IDX_MASK) {
1079
case GE_VTYPE_IDX_8BIT:
1080
for (int i = 0; i < count; ++i) {
1081
indices[i] = inds[i];
1082
}
1083
break;
1084
case GE_VTYPE_IDX_16BIT:
1085
for (int i = 0; i < count; ++i) {
1086
indices[i] = inds16[i];
1087
}
1088
break;
1089
case GE_VTYPE_IDX_32BIT:
1090
for (int i = 0; i < count; ++i) {
1091
// These are rare. Only the bottom 16 bits are used.
1092
indices[i] = (u16)inds32[i];
1093
}
1094
break;
1095
}
1096
} else {
1097
indices.clear();
1098
}
1099
} else {
1100
indices.clear();
1101
}
1102
1103
static std::vector<u32> temp_buffer;
1104
static std::vector<SimpleVertex> simpleVertices;
1105
temp_buffer.resize(std::max((int)indexUpperBound, 8192) * 128 / sizeof(u32));
1106
simpleVertices.resize(indexUpperBound + 1);
1107
1108
// We always want "applyskinindecode" here, faster than letting NormalizeVertices handle it.
1109
const u32 vertTypeID = GetVertTypeID(gstate.vertType, gstate.getUVGenMode(), true);
1110
VertexDecoder *dec = drawEngine->GetVertexDecoder(vertTypeID);
1111
NormalizeVertices(&simpleVertices[0], (u8 *)(&temp_buffer[0]), Memory::GetPointerUnchecked(gstate_c.vertexAddr), indexLowerBound, indexUpperBound, dec, gstate.vertType);
1112
1113
float world[16];
1114
float view[16];
1115
float worldview[16];
1116
float worldviewproj[16];
1117
ConvertMatrix4x3To4x4(world, gstate.worldMatrix);
1118
ConvertMatrix4x3To4x4(view, gstate.viewMatrix);
1119
Matrix4ByMatrix4(worldview, world, view);
1120
Matrix4ByMatrix4(worldviewproj, worldview, gstate.projMatrix);
1121
1122
// This transforms the vertices.
1123
// NOTE: We really should just run the full software transform?
1124
1125
vertices.resize(indexUpperBound + 1);
1126
uint32_t vertType = gstate.vertType;
1127
for (int i = indexLowerBound; i <= indexUpperBound; ++i) {
1128
const SimpleVertex &vert = simpleVertices[i];
1129
1130
if ((vertType & GE_VTYPE_THROUGH) != 0) {
1131
if (vertType & GE_VTYPE_TC_MASK) {
1132
vertices[i].u = vert.uv[0];
1133
vertices[i].v = vert.uv[1];
1134
} else {
1135
vertices[i].u = 0.0f;
1136
vertices[i].v = 0.0f;
1137
}
1138
vertices[i].x = vert.pos.x;
1139
vertices[i].y = vert.pos.y;
1140
vertices[i].z = vert.pos.z;
1141
if (vertType & GE_VTYPE_COL_MASK) {
1142
memcpy(vertices[i].c, vert.color, sizeof(vertices[i].c));
1143
} else {
1144
memset(vertices[i].c, 0, sizeof(vertices[i].c));
1145
}
1146
vertices[i].nx = 0.0f; // No meaningful normals in through mode
1147
vertices[i].ny = 0.0f;
1148
vertices[i].nz = 1.0f;
1149
} else {
1150
float clipPos[4];
1151
Vec3ByMatrix44(clipPos, vert.pos.AsArray(), worldviewproj);
1152
Vec3f screenPos = ClipToScreen(clipPos);
1153
Vec3f drawPos = ScreenToDrawing(screenPos);
1154
1155
if (vertType & GE_VTYPE_TC_MASK) {
1156
vertices[i].u = vert.uv[0] * (float)gstate.getTextureWidth(0);
1157
vertices[i].v = vert.uv[1] * (float)gstate.getTextureHeight(0);
1158
} else {
1159
vertices[i].u = 0.0f;
1160
vertices[i].v = 0.0f;
1161
}
1162
// Should really have separate coordinates for before and after transform.
1163
vertices[i].x = drawPos.x;
1164
vertices[i].y = drawPos.y;
1165
vertices[i].z = drawPos.z;
1166
if (vertType & GE_VTYPE_COL_MASK) {
1167
memcpy(vertices[i].c, vert.color, sizeof(vertices[i].c));
1168
} else {
1169
memset(vertices[i].c, 0, sizeof(vertices[i].c));
1170
}
1171
vertices[i].nx = vert.nrm.x;
1172
vertices[i].ny = vert.nrm.y;
1173
vertices[i].nz = vert.nrm.z;
1174
}
1175
}
1176
1177
gstate_c.vertexFullAlpha = savedVertexFullAlpha;
1178
1179
return true;
1180
}
1181
1182