~ppsspp/ppsspp/ppsspp_1.3.0

« back to all changes in this revision

Viewing changes to GPU/GLES/DrawEngineGLES.h

  • Committer: Sérgio Benjamim
  • Date: 2017-01-02 00:12:05 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20170102001205-cxbta9za203nmjwm
1.3.0 source (from ppsspp_1.3.0-r160.p5.l1762.a165.t83~56~ubuntu16.04.1.tar.xz).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2012- 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 <unordered_map>
 
21
 
 
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"
 
31
 
 
32
class LinkedShader;
 
33
class ShaderManager;
 
34
class TextureCache;
 
35
class FramebufferManager;
 
36
class FramebufferManagerCommon;
 
37
class TextureCacheCommon;
 
38
class FragmentTestCache;
 
39
struct TransformedVertex;
 
40
 
 
41
struct DecVtxFormat;
 
42
 
 
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
 
53
 
 
54
enum {
 
55
        VAI_FLAG_VERTEXFULLALPHA = 1,
 
56
};
 
57
 
 
58
// Avoiding the full include of TextureDecoder.h.
 
59
#if (defined(_M_SSE) && defined(_M_X64)) || defined(ARM64)
 
60
typedef u64 ReliableHashType;
 
61
#else
 
62
typedef u32 ReliableHashType;
 
63
#endif
 
64
 
 
65
// Try to keep this POD.
 
66
class VertexArrayInfo {
 
67
public:
 
68
        VertexArrayInfo() {
 
69
                status = VAI_NEW;
 
70
                vbo = 0;
 
71
                ebo = 0;
 
72
                prim = GE_PRIM_INVALID;
 
73
                numDraws = 0;
 
74
                numFrames = 0;
 
75
                lastFrame = gpuStats.numFlips;
 
76
                numVerts = 0;
 
77
                drawsUntilNextFullHash = 0;
 
78
                flags = 0;
 
79
        }
 
80
 
 
81
        enum Status {
 
82
                VAI_NEW,
 
83
                VAI_HASHING,
 
84
                VAI_RELIABLE,  // cache, don't hash
 
85
                VAI_UNRELIABLE,  // never cache
 
86
        };
 
87
 
 
88
        ReliableHashType hash;
 
89
        u32 minihash;
 
90
 
 
91
        Status status;
 
92
 
 
93
        u32 vbo;
 
94
        u32 ebo;
 
95
 
 
96
        // Precalculated parameter for drawRangeElements
 
97
        u16 numVerts;
 
98
        u16 maxIndex;
 
99
        s8 prim;
 
100
 
 
101
        // ID information
 
102
        int numDraws;
 
103
        int numFrames;
 
104
        int lastFrame;  // So that we can forget.
 
105
        u16 drawsUntilNextFullHash;
 
106
        u8 flags;
 
107
};
 
108
 
 
109
// Handles transform, lighting and drawing.
 
110
class DrawEngineGLES : public DrawEngineCommon, public GfxResourceHolder {
 
111
public:
 
112
        DrawEngineGLES();
 
113
        virtual ~DrawEngineGLES();
 
114
 
 
115
        void SubmitPrim(void *verts, void *inds, GEPrimitiveType prim, int vertexCount, u32 vertType, int *bytesRead);
 
116
 
 
117
        void SetShaderManager(ShaderManager *shaderManager) {
 
118
                shaderManager_ = shaderManager;
 
119
        }
 
120
        void SetTextureCache(TextureCache *textureCache) {
 
121
                textureCache_ = textureCache;
 
122
        }
 
123
        void SetFramebufferManager(FramebufferManager *fbManager) {
 
124
                framebufferManager_ = fbManager;
 
125
        }
 
126
        void SetFragmentTestCache(FragmentTestCache *testCache) {
 
127
                fragmentTestCache_ = testCache;
 
128
        }
 
129
        void RestoreVAO();
 
130
        void InitDeviceObjects();
 
131
        void DestroyDeviceObjects();
 
132
        void GLLost() override;
 
133
        void GLRestore() override;
 
134
        void Resized();
 
135
 
 
136
        void DecimateTrackedVertexArrays();
 
137
        void ClearTrackedVertexArrays();
 
138
 
 
139
        void SetupVertexDecoder(u32 vertType);
 
140
        inline void SetupVertexDecoderInternal(u32 vertType);
 
141
 
 
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.
 
146
 
 
147
                // Also, this is all pure guesswork. If we can find a way to do measurements, that would be great.
 
148
 
 
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).
 
151
 
 
152
                int cost = 20;
 
153
                if (gstate.isLightingEnabled()) {
 
154
                        cost += 10;
 
155
 
 
156
                        for (int i = 0; i < 4; i++) {
 
157
                                if (gstate.isLightChanEnabled(i))
 
158
                                        cost += 10;
 
159
                        }
 
160
                }
 
161
 
 
162
                if (gstate.getUVGenMode() != GE_TEXMAP_TEXTURE_COORDS) {
 
163
                        cost += 20;
 
164
                }
 
165
                if (dec_ && dec_->morphcount > 1) {
 
166
                        cost += 5 * dec_->morphcount;
 
167
                }
 
168
 
 
169
                return cost;
 
170
        }
 
171
 
 
172
        // So that this can be inlined
 
173
        void Flush() {
 
174
                if (!numDrawCalls)
 
175
                        return;
 
176
                DoFlush();
 
177
        }
 
178
 
 
179
        void FinishDeferred() {
 
180
                if (!numDrawCalls)
 
181
                        return;
 
182
                DecodeVerts();
 
183
        }
 
184
 
 
185
        bool IsCodePtrVertexDecoder(const u8 *ptr) const;
 
186
 
 
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);
 
190
        }
 
191
 
 
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();
 
196
 
 
197
private:
 
198
        void DecodeVerts();
 
199
        void DecodeVertsStep();
 
200
        void DoFlush();
 
201
        void ApplyDrawState(int prim);
 
202
        void ApplyDrawStateLate();
 
203
        bool ApplyShaderBlending();
 
204
        void ResetShaderBlending();
 
205
 
 
206
        GLuint AllocateBuffer(size_t sz);
 
207
        void FreeBuffer(GLuint buf);
 
208
        void FreeVertexArray(VertexArrayInfo *vai);
 
209
 
 
210
        u32 ComputeMiniHash();
 
211
        ReliableHashType ComputeHash();  // Reads deferred vertex data.
 
212
        void MarkUnreliable(VertexArrayInfo *vai);
 
213
 
 
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 {
 
217
                void *verts;
 
218
                void *inds;
 
219
                u32 vertType;
 
220
                u8 indexType;
 
221
                s8 prim;
 
222
                u32 vertexCount;
 
223
                u16 indexLowerBound;
 
224
                u16 indexUpperBound;
 
225
        };
 
226
 
 
227
        // Vertex collector state
 
228
        IndexGenerator indexGen;
 
229
        int decodedVerts_;
 
230
        GEPrimitiveType prevPrim_;
 
231
 
 
232
        u32 lastVType_;
 
233
 
 
234
        TransformedVertex *transformed;
 
235
        TransformedVertex *transformedExpanded;
 
236
 
 
237
        std::unordered_map<u32, VertexArrayInfo *> vai_;
 
238
 
 
239
        // Vertex buffer objects
 
240
        // Element buffer objects
 
241
        struct BufferNameInfo {
 
242
                BufferNameInfo() : sz(0), used(false), lastFrame(0) {}
 
243
 
 
244
                size_t sz;
 
245
                bool used;
 
246
                int lastFrame;
 
247
        };
 
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_;
 
253
        GLuint sharedVao_;
 
254
 
 
255
        // Other
 
256
        ShaderManager *shaderManager_;
 
257
        TextureCache *textureCache_;
 
258
        FramebufferManager *framebufferManager_;
 
259
        FragmentTestCache *fragmentTestCache_;
 
260
 
 
261
        enum { MAX_DEFERRED_DRAW_CALLS = 128 };
 
262
        DeferredDrawCall drawCalls[MAX_DEFERRED_DRAW_CALLS];
 
263
        int numDrawCalls;
 
264
        int vertexCountInDrawCalls;
 
265
 
 
266
        int decimationCounter_;
 
267
        int bufferDecimationCounter_;
 
268
        int decodeCounter_;
 
269
        u32 dcid_;
 
270
 
 
271
        UVScale *uvScale;
 
272
 
 
273
        bool fboTexNeedBind_;
 
274
        bool fboTexBound_;
 
275
};