1
// Copyright (c) 2012- PPSSPP Project.
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.
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.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
20
#include <unordered_map>
22
#include "GPU/GPUState.h"
23
#include "GPU/Common/GPUDebugInterface.h"
24
#include "GPU/Common/IndexGenerator.h"
25
#include "GPU/Common/VertexDecoderCommon.h"
26
#include "GPU/Common/DrawEngineCommon.h"
27
#include "GPU/Common/GPUStateUtils.h"
28
#include "GPU/GLES/FragmentShaderGenerator.h"
29
#include "gfx/gl_common.h"
30
#include "gfx/gl_lost_manager.h"
35
class FramebufferManager;
36
class FramebufferManagerCommon;
37
class TextureCacheCommon;
38
class FragmentTestCache;
39
struct TransformedVertex;
43
// States transitions:
44
// On creation: DRAWN_NEW
45
// DRAWN_NEW -> DRAWN_HASHING
46
// DRAWN_HASHING -> DRAWN_RELIABLE
47
// DRAWN_HASHING -> DRAWN_UNRELIABLE
48
// DRAWN_ONCE -> UNRELIABLE
49
// DRAWN_RELIABLE -> DRAWN_SAFE
50
// UNRELIABLE -> death
51
// DRAWN_ONCE -> death
52
// DRAWN_RELIABLE -> death
55
VAI_FLAG_VERTEXFULLALPHA = 1,
58
// Avoiding the full include of TextureDecoder.h.
59
#if (defined(_M_SSE) && defined(_M_X64)) || defined(ARM64)
60
typedef u64 ReliableHashType;
62
typedef u32 ReliableHashType;
65
// Try to keep this POD.
66
class VertexArrayInfo {
72
prim = GE_PRIM_INVALID;
75
lastFrame = gpuStats.numFlips;
77
drawsUntilNextFullHash = 0;
84
VAI_RELIABLE, // cache, don't hash
85
VAI_UNRELIABLE, // never cache
88
ReliableHashType hash;
96
// Precalculated parameter for drawRangeElements
104
int lastFrame; // So that we can forget.
105
u16 drawsUntilNextFullHash;
109
// Handles transform, lighting and drawing.
110
class DrawEngineGLES : public DrawEngineCommon, public GfxResourceHolder {
113
virtual ~DrawEngineGLES();
115
void SubmitPrim(void *verts, void *inds, GEPrimitiveType prim, int vertexCount, u32 vertType, int *bytesRead);
117
void SetShaderManager(ShaderManager *shaderManager) {
118
shaderManager_ = shaderManager;
120
void SetTextureCache(TextureCache *textureCache) {
121
textureCache_ = textureCache;
123
void SetFramebufferManager(FramebufferManager *fbManager) {
124
framebufferManager_ = fbManager;
126
void SetFragmentTestCache(FragmentTestCache *testCache) {
127
fragmentTestCache_ = testCache;
130
void InitDeviceObjects();
131
void DestroyDeviceObjects();
132
void GLLost() override;
133
void GLRestore() override;
136
void DecimateTrackedVertexArrays();
137
void ClearTrackedVertexArrays();
139
void SetupVertexDecoder(u32 vertType);
140
inline void SetupVertexDecoderInternal(u32 vertType);
142
// This requires a SetupVertexDecoder call first.
143
int EstimatePerVertexCost() {
144
// TODO: This is transform cost, also account for rasterization cost somehow... although it probably
145
// runs in parallel with transform.
147
// Also, this is all pure guesswork. If we can find a way to do measurements, that would be great.
149
// GTA wants a low value to run smooth, GoW wants a high value (otherwise it thinks things
150
// went too fast and starts doing all the work over again).
153
if (gstate.isLightingEnabled()) {
156
for (int i = 0; i < 4; i++) {
157
if (gstate.isLightChanEnabled(i))
162
if (gstate.getUVGenMode() != GE_TEXMAP_TEXTURE_COORDS) {
165
if (dec_ && dec_->morphcount > 1) {
166
cost += 5 * dec_->morphcount;
172
// So that this can be inlined
179
void FinishDeferred() {
185
bool IsCodePtrVertexDecoder(const u8 *ptr) const;
187
void DispatchFlush() override { Flush(); }
188
void DispatchSubmitPrim(void *verts, void *inds, GEPrimitiveType prim, int vertexCount, u32 vertType, int *bytesRead) override {
189
SubmitPrim(verts, inds, prim, vertexCount, vertType, bytesRead);
192
GLuint BindBuffer(const void *p, size_t sz);
193
GLuint BindBuffer(const void *p1, size_t sz1, const void *p2, size_t sz2);
194
GLuint BindElementBuffer(const void *p, size_t sz);
195
void DecimateBuffers();
199
void DecodeVertsStep();
201
void ApplyDrawState(int prim);
202
void ApplyDrawStateLate();
203
bool ApplyShaderBlending();
204
void ResetShaderBlending();
206
GLuint AllocateBuffer(size_t sz);
207
void FreeBuffer(GLuint buf);
208
void FreeVertexArray(VertexArrayInfo *vai);
210
u32 ComputeMiniHash();
211
ReliableHashType ComputeHash(); // Reads deferred vertex data.
212
void MarkUnreliable(VertexArrayInfo *vai);
214
// Defer all vertex decoding to a Flush, so that we can hash and cache the
215
// generated buffers without having to redecode them every time.
216
struct DeferredDrawCall {
227
// Vertex collector state
228
IndexGenerator indexGen;
230
GEPrimitiveType prevPrim_;
234
TransformedVertex *transformed;
235
TransformedVertex *transformedExpanded;
237
std::unordered_map<u32, VertexArrayInfo *> vai_;
239
// Vertex buffer objects
240
// Element buffer objects
241
struct BufferNameInfo {
242
BufferNameInfo() : sz(0), used(false), lastFrame(0) {}
248
std::vector<GLuint> bufferNameCache_;
249
std::multimap<size_t, GLuint> freeSizedBuffers_;
250
std::unordered_map<GLuint, BufferNameInfo> bufferNameInfo_;
251
std::vector<GLuint> buffersThisFrame_;
252
size_t bufferNameCacheSize_;
256
ShaderManager *shaderManager_;
257
TextureCache *textureCache_;
258
FramebufferManager *framebufferManager_;
259
FragmentTestCache *fragmentTestCache_;
261
enum { MAX_DEFERRED_DRAW_CALLS = 128 };
262
DeferredDrawCall drawCalls[MAX_DEFERRED_DRAW_CALLS];
264
int vertexCountInDrawCalls;
266
int decimationCounter_;
267
int bufferDecimationCounter_;
273
bool fboTexNeedBind_;