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