Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Common/DrawEngineCommon.h
5650 views
1
// Copyright (c) 2013- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include <vector>
21
22
#include "Common/CommonTypes.h"
23
#include "Common/Data/Collections/Hashmaps.h"
24
25
#include "GPU/Math3D.h"
26
#include "GPU/GPUState.h"
27
#include "GPU/GPUDefinitions.h"
28
#include "GPU/Common/GPUStateUtils.h"
29
#include "GPU/Common/IndexGenerator.h"
30
#include "GPU/Common/VertexDecoderCommon.h"
31
32
class VertexDecoder;
33
struct DepthDraw;
34
35
enum {
36
VERTEX_BUFFER_MAX = 65536,
37
DECODED_VERTEX_BUFFER_SIZE = VERTEX_BUFFER_MAX * 2 * 36, // 36 == sizeof(SimpleVertex)
38
DECODED_INDEX_BUFFER_SIZE = VERTEX_BUFFER_MAX * 6 * 6 * 2, // * 6 for spline tessellation, then * 6 again for converting into points/lines, and * 2 for 2 bytes per index
39
};
40
41
enum {
42
TEX_SLOT_PSP_TEXTURE = 0,
43
TEX_SLOT_SHADERBLEND_SRC = 1,
44
TEX_SLOT_ALPHATEST = 2,
45
TEX_SLOT_CLUT = 3,
46
TEX_SLOT_SPLINE_POINTS = 4,
47
TEX_SLOT_SPLINE_WEIGHTS_U = 5,
48
TEX_SLOT_SPLINE_WEIGHTS_V = 6,
49
};
50
51
enum FBOTexState {
52
FBO_TEX_NONE,
53
FBO_TEX_COPY_BIND_TEX,
54
FBO_TEX_READ_FRAMEBUFFER,
55
};
56
57
struct SimpleVertex;
58
namespace Spline { struct Weight2D; }
59
60
class TessellationDataTransfer {
61
public:
62
virtual ~TessellationDataTransfer() {}
63
static void CopyControlPoints(float *pos, float *tex, float *col, int posStride, int texStride, int colStride, const SimpleVertex *const *points, int size, u32 vertType);
64
virtual void SendDataToShader(const SimpleVertex *const *points, int size_u, int size_v, u32 vertType, const Spline::Weight2D &weights) = 0;
65
};
66
67
// Culling plane, group of 8.
68
struct alignas(16) Plane8 {
69
float x[8], y[8], z[8], w[8];
70
void Set(int i, float _x, float _y, float _z, float _w) { x[i] = _x; y[i] = _y; z[i] = _z; w[i] = _w; }
71
float Test(int i, const float f[3]) const { return x[i] * f[0] + y[i] * f[1] + z[i] * f[2] + w[i]; }
72
};
73
74
class DrawEngineCommon {
75
public:
76
DrawEngineCommon();
77
virtual ~DrawEngineCommon();
78
79
void Init();
80
81
virtual void BeginFrame();
82
83
void SetGPUCommon(GPUCommon *gpuCommon) {
84
gpuCommon_ = gpuCommon;
85
}
86
87
virtual void DeviceLost() = 0;
88
virtual void DeviceRestore(Draw::DrawContext *draw) = 0;
89
90
// Dispatches the queued-up draws.
91
virtual void Flush() = 0;
92
93
// This would seem to be unnecessary now, but is still required for splines/beziers to work in the software backend since SubmitPrim
94
// is different. Should probably refactor that.
95
// Note that vertTypeID should be computed using GetVertTypeID().
96
virtual void DispatchSubmitPrim(const void *verts, const void *inds, GEPrimitiveType prim, int vertexCount, u32 vertTypeID, bool clockwise, int *bytesRead) {
97
VertexDecoder *dec = GetVertexDecoder(vertTypeID);
98
SubmitPrim(verts, inds, prim, vertexCount, dec, vertTypeID, clockwise, bytesRead);
99
}
100
101
virtual void DispatchSubmitImm(GEPrimitiveType prim, TransformedVertex *buffer, int vertexCount, int cullMode, bool continuation);
102
103
bool TestBoundingBox(const void *control_points, const void *inds, int vertexCount, const VertexDecoder *dec, u32 vertType);
104
105
// This is a less accurate version of TestBoundingBox, but faster. Can have more false positives.
106
// Doesn't support indexing.
107
bool TestBoundingBoxFast(const void *control_points, int vertexCount, const VertexDecoder *dec, u32 vertType);
108
bool TestBoundingBoxThrough(const void *vdata, int vertexCount, const VertexDecoder *dec, u32 vertType, int *bytesRead);
109
110
void FlushPartialDecode() {
111
DecodeVerts(dec_, decoded_);
112
}
113
114
void FlushSkin() {
115
if (dec_ && dec_->skinInDecode) {
116
FlushPartialDecode();
117
}
118
}
119
120
int ExtendNonIndexedPrim(const uint32_t *cmd, const uint32_t *stall, const VertexDecoder *dec, u32 vertTypeID, bool clockwise, int *bytesRead, bool isTriangle);
121
bool SubmitPrim(const void *verts, const void *inds, GEPrimitiveType prim, int vertexCount, const VertexDecoder *dec, u32 vertTypeID, bool clockwise, int *bytesRead);
122
void SkipPrim(GEPrimitiveType prim, int vertexCount, const VertexDecoder *dec, u32 vertTypeID, int *bytesRead);
123
124
template<class Surface>
125
void SubmitCurve(const void *control_points, const void *indices, Surface &surface, u32 vertType, int *bytesRead, const char *scope);
126
static void ClearSplineBezierWeights();
127
128
bool CanUseHardwareTransform(int prim) const;
129
bool CanUseHardwareTessellation(GEPatchPrimType prim) const;
130
131
std::vector<std::string> DebugGetVertexLoaderIDs();
132
std::string DebugGetVertexLoaderString(std::string_view id, DebugShaderStringType stringType);
133
134
virtual void NotifyConfigChanged();
135
136
bool EverUsedExactEqualDepth() const {
137
return everUsedExactEqualDepth_;
138
}
139
void SetEverUsedExactEqualDepth(bool v) {
140
everUsedExactEqualDepth_ = v;
141
}
142
143
bool DescribeCodePtr(const u8 *ptr, std::string &name) const;
144
int GetNumDrawCalls() const {
145
return numDrawVerts_;
146
}
147
148
VertexDecoder *GetVertexDecoder(u32 vertTypeID) {
149
VertexDecoder *dec;
150
if (decoderMap_.Get(vertTypeID, &dec))
151
return dec;
152
dec = new VertexDecoder();
153
_assert_(dec);
154
dec->SetVertexType(vertTypeID, decOptions_, decJitCache_);
155
decoderMap_.Insert(vertTypeID, dec);
156
return dec;
157
}
158
159
void AssertEmpty() {
160
_dbg_assert_(numDrawVerts_ == 0 && numDrawInds_ == 0);
161
}
162
163
// temporary hack
164
uint8_t *GetTempSpace() {
165
return decoded_ + 12 * 65536;
166
}
167
168
void FlushQueuedDepth();
169
170
protected:
171
virtual bool UpdateUseHWTessellation(bool enabled) const { return enabled; }
172
void UpdatePlanes();
173
174
void DecodeVerts(const VertexDecoder *dec, u8 *dest);
175
int DecodeInds();
176
177
int ComputeNumVertsToDecode() const;
178
179
void ApplyFramebufferRead(FBOTexState *fboTexState);
180
181
void InitDepthRaster();
182
void ShutdownDepthRaster();
183
void DepthRasterSubmitRaw(GEPrimitiveType prim, const VertexDecoder *dec, uint32_t vertTypeID, int vertexCount);
184
void DepthRasterPredecoded(GEPrimitiveType prim, const void *inVerts, int numDecoded, const VertexDecoder *dec, int vertexCount);
185
bool CalculateDepthDraw(DepthDraw *draw, GEPrimitiveType prim, int vertexCount);
186
187
static inline int IndexSize(u32 vtype) {
188
const u32 indexType = (vtype & GE_VTYPE_IDX_MASK);
189
if (indexType == GE_VTYPE_IDX_16BIT) {
190
return 2;
191
} else if (indexType == GE_VTYPE_IDX_32BIT) {
192
return 4;
193
}
194
return 1;
195
}
196
197
inline void UpdateEverUsedEqualDepth(GEComparison comp) {
198
switch (comp) {
199
case GE_COMP_EQUAL:
200
everUsedExactEqualDepth_ = true;
201
everUsedEqualDepth_ = true;
202
break;
203
204
case GE_COMP_NOTEQUAL:
205
case GE_COMP_LEQUAL:
206
case GE_COMP_GEQUAL:
207
everUsedEqualDepth_ = true;
208
break;
209
210
default:
211
break;
212
}
213
}
214
215
inline void ResetAfterDrawInline() {
216
gpuStats.numFlushes++;
217
gpuStats.numDrawCalls += numDrawInds_;
218
gpuStats.numVertexDecodes += numDrawVerts_;
219
gpuStats.numVertsSubmitted += vertexCountInDrawCalls_;
220
gpuStats.numVertsDecoded += numDecodedVerts_;
221
222
indexGen.Reset();
223
numDecodedVerts_ = 0;
224
numDrawVerts_ = 0;
225
numDrawInds_ = 0;
226
vertexCountInDrawCalls_ = 0;
227
decodeIndsCounter_ = 0;
228
decodeVertsCounter_ = 0;
229
seenPrims_ = 0;
230
anyCCWOrIndexed_ = false;
231
gstate_c.vertexFullAlpha = true;
232
233
// Now seems as good a time as any to reset the min/max coords, which we may examine later.
234
gstate_c.vertBounds.minU = 512;
235
gstate_c.vertBounds.minV = 512;
236
gstate_c.vertBounds.maxU = 0;
237
gstate_c.vertBounds.maxV = 0;
238
}
239
240
inline bool CollectedPureDraw() const {
241
// TODO: Do something faster.
242
if (useDepthRaster_) {
243
return false;
244
}
245
246
switch (seenPrims_) {
247
case 1 << GE_PRIM_TRIANGLE_STRIP:
248
return !anyCCWOrIndexed_ && numDrawInds_ == 1;
249
case 1 << GE_PRIM_LINES:
250
case 1 << GE_PRIM_POINTS:
251
case 1 << GE_PRIM_TRIANGLES:
252
return !anyCCWOrIndexed_;
253
default:
254
return false;
255
}
256
}
257
258
inline void DecodeIndsAndGetData(GEPrimitiveType *prim, int *numVerts, int *maxIndex, bool *useElements, bool forceIndexed) {
259
if (!forceIndexed && CollectedPureDraw()) {
260
*prim = drawInds_[0].prim;
261
*numVerts = numDecodedVerts_;
262
*maxIndex = numDecodedVerts_;
263
*useElements = false;
264
} else {
265
int vertexCount = DecodeInds();
266
*numVerts = vertexCount;
267
*maxIndex = numDecodedVerts_;
268
*prim = IndexGenerator::GeneralPrim((GEPrimitiveType)drawInds_[0].prim);
269
*useElements = true;
270
}
271
}
272
273
inline int RemainingIndices(const uint16_t *inds) const {
274
return DECODED_INDEX_BUFFER_SIZE / sizeof(uint16_t) - (inds - decIndex_);
275
}
276
277
bool useHWTransform_ = false;
278
bool useHWTessellation_ = false;
279
// Used to prevent unnecessary flushing in softgpu.
280
bool flushOnParams_ = true;
281
282
// Set once a equal depth test is encountered.
283
bool everUsedEqualDepth_ = false;
284
bool everUsedExactEqualDepth_ = false;
285
286
// Vertex collector buffers
287
u8 *decoded_ = nullptr;
288
u16 *decIndex_ = nullptr;
289
290
// Cached vertex decoders
291
DenseHashMap<u32, VertexDecoder *> decoderMap_;
292
VertexDecoderJitCache *decJitCache_ = nullptr;
293
VertexDecoderOptions decOptions_{};
294
295
TransformedVertex *transformed_ = nullptr;
296
TransformedVertex *transformedExpanded_ = nullptr;
297
298
// Defer all vertex decoding to a "Flush" (except when software skinning)
299
struct DeferredVerts {
300
const void *verts;
301
UVScale uvScale;
302
u32 vertexCount;
303
u16 indexLowerBound;
304
u16 indexUpperBound;
305
};
306
307
struct DeferredInds {
308
const void *inds;
309
u32 vertexCount;
310
u8 vertDecodeIndex; // index into the drawVerts_ array to look up the vertexOffset.
311
u8 indexType;
312
GEPrimitiveType prim;
313
bool clockwise;
314
u16 offset;
315
};
316
317
enum { MAX_DEFERRED_DRAW_VERTS = 128 }; // If you change this to more than 256, change type of DeferredInds::vertDecodeIndex.
318
enum { MAX_DEFERRED_DRAW_INDS = 512 }; // Monster Hunter spams indexed calls that we end up merging.
319
DeferredVerts drawVerts_[MAX_DEFERRED_DRAW_VERTS];
320
uint32_t drawVertexOffsets_[MAX_DEFERRED_DRAW_VERTS];
321
DeferredInds drawInds_[MAX_DEFERRED_DRAW_INDS];
322
323
const VertexDecoder *dec_ = nullptr;
324
u32 lastVType_ = -1; // corresponds to dec_. Could really just pick it out of dec_...
325
int numDrawVerts_ = 0;
326
int numDrawInds_ = 0;
327
int vertexCountInDrawCalls_ = 0;
328
329
int decodeVertsCounter_ = 0;
330
int decodeIndsCounter_ = 0;
331
332
int seenPrims_ = 0;
333
bool anyCCWOrIndexed_ = 0;
334
bool anyIndexed_ = 0;
335
336
bool applySkinInDecode_ = false;
337
338
// Vertex collector state
339
IndexGenerator indexGen;
340
int numDecodedVerts_ = 0;
341
GEPrimitiveType prevPrim_ = GE_PRIM_INVALID;
342
343
// Shader blending state
344
bool fboTexBound_ = false;
345
346
// Sometimes, unusual situations mean we need to reset dirty flags after state calc finishes.
347
uint64_t dirtyRequiresRecheck_ = 0;
348
349
ComputedPipelineState pipelineState_;
350
351
// Hardware tessellation
352
TessellationDataTransfer *tessDataTransfer;
353
354
// Culling
355
Plane8 planes_;
356
Vec2f minOffset_;
357
Vec2f maxOffset_;
358
bool offsetOutsideEdge_;
359
360
GPUCommon *gpuCommon_;
361
362
// Software depth raster
363
bool useDepthRaster_ = false;
364
365
float *depthTransformed_ = nullptr;
366
int *depthScreenVerts_ = nullptr;
367
uint16_t *depthIndices_ = nullptr;
368
369
// Queue
370
int depthVertexCount_ = 0;
371
int depthIndexCount_ = 0;
372
std::vector<DepthDraw> depthDraws_;
373
374
double rasterTimeStart_ = 0.0;
375
};
376
377