Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/GLES/GPU_GLES.cpp
3186 views
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
#include "Common/Profiler/Profiler.h"
19
#include "Common/Data/Text/I18n.h"
20
21
#include "Common/Log.h"
22
#include "Common/Serialize/Serializer.h"
23
#include "Common/File/FileUtil.h"
24
#include "Common/GraphicsContext.h"
25
#include "Common/System/OSD.h"
26
#include "Common/VR/PPSSPPVR.h"
27
28
#include "Core/Config.h"
29
#include "Core/Debugger/Breakpoints.h"
30
#include "Core/MemMapHelpers.h"
31
#include "Core/Reporting.h"
32
#include "Core/Core.h"
33
#include "Core/ELF/ParamSFO.h"
34
35
#include "GPU/GPUState.h"
36
#include "GPU/ge_constants.h"
37
#include "GPU/GeDisasm.h"
38
#include "GPU/Common/FramebufferManagerCommon.h"
39
#include "GPU/GLES/ShaderManagerGLES.h"
40
#include "GPU/GLES/GPU_GLES.h"
41
#include "GPU/GLES/FramebufferManagerGLES.h"
42
#include "GPU/GLES/DrawEngineGLES.h"
43
#include "GPU/GLES/TextureCacheGLES.h"
44
45
#ifdef _WIN32
46
#include "Windows/GPU/WindowsGLContext.h"
47
#endif
48
49
GPU_GLES::GPU_GLES(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
50
: GPUCommonHW(gfxCtx, draw), drawEngine_(draw), fragmentTestCache_(draw) {
51
gstate_c.SetUseFlags(CheckGPUFeatures());
52
53
shaderManagerGL_ = new ShaderManagerGLES(draw);
54
framebufferManagerGL_ = new FramebufferManagerGLES(draw);
55
framebufferManager_ = framebufferManagerGL_;
56
textureCacheGL_ = new TextureCacheGLES(draw, framebufferManager_->GetDraw2D());
57
textureCache_ = textureCacheGL_;
58
drawEngineCommon_ = &drawEngine_;
59
shaderManager_ = shaderManagerGL_;
60
61
drawEngine_.SetGPUCommon(this);
62
drawEngine_.SetShaderManager(shaderManagerGL_);
63
drawEngine_.SetTextureCache(textureCacheGL_);
64
drawEngine_.SetFramebufferManager(framebufferManagerGL_);
65
drawEngine_.SetFragmentTestCache(&fragmentTestCache_);
66
drawEngine_.Init();
67
framebufferManagerGL_->SetTextureCache(textureCacheGL_);
68
framebufferManagerGL_->SetShaderManager(shaderManagerGL_);
69
framebufferManagerGL_->SetDrawEngine(&drawEngine_);
70
framebufferManagerGL_->Init(msaaLevel_);
71
textureCacheGL_->SetFramebufferManager(framebufferManagerGL_);
72
textureCacheGL_->SetShaderManager(shaderManagerGL_);
73
textureCacheGL_->SetDrawEngine(&drawEngine_);
74
fragmentTestCache_.SetTextureCache(textureCacheGL_);
75
76
// Sanity check gstate
77
if ((int *)&gstate.transferstart - (int *)&gstate != 0xEA) {
78
ERROR_LOG(Log::G3D, "gstate has drifted out of sync!");
79
}
80
81
// No need to flush before the tex scale/offset commands if we are baking
82
// the tex scale/offset into the vertices anyway.
83
84
UpdateCmdInfo();
85
86
BuildReportingInfo();
87
88
textureCache_->NotifyConfigChanged();
89
90
// Load shader cache.
91
std::string discID = g_paramSFO.GetDiscID();
92
if (discID.size()) {
93
if (g_Config.bShaderCache) {
94
File::CreateFullPath(GetSysDirectory(DIRECTORY_APP_CACHE));
95
shaderCachePath_ = GetSysDirectory(DIRECTORY_APP_CACHE) / (discID + ".glshadercache");
96
// Actually precompiled by IsReady() since we're single-threaded.
97
File::IOFile f(shaderCachePath_, "rb");
98
if (f.IsOpen()) {
99
if (shaderManagerGL_->LoadCacheFlags(f, &drawEngine_)) {
100
if (drawEngineCommon_->EverUsedExactEqualDepth()) {
101
sawExactEqualDepth_ = true;
102
}
103
gstate_c.SetUseFlags(CheckGPUFeatures());
104
// We're compiling now, clear if they changed.
105
gstate_c.useFlagsChanged = false;
106
107
if (shaderManagerGL_->LoadCache(f))
108
NOTICE_LOG(Log::G3D, "Precompiling the shader cache from '%s'", shaderCachePath_.c_str());
109
}
110
}
111
} else {
112
INFO_LOG(Log::G3D, "Shader cache disabled. Not loading.");
113
}
114
}
115
116
if (g_Config.bHardwareTessellation) {
117
// Disable hardware tessellation if device is unsupported.
118
if (!drawEngine_.SupportsHWTessellation()) {
119
ERROR_LOG(Log::G3D, "Hardware Tessellation is unsupported, falling back to software tessellation");
120
}
121
}
122
}
123
124
GPU_GLES::~GPU_GLES() {
125
// If we're here during app shutdown (exiting the Windows app in-game, for example)
126
// everything should already be cleared since DeviceLost has been run.
127
128
if (shaderCachePath_.Valid() && draw_) {
129
if (g_Config.bShaderCache) {
130
shaderManagerGL_->SaveCache(shaderCachePath_, &drawEngine_);
131
} else {
132
INFO_LOG(Log::G3D, "Shader cache disabled. Not saving.");
133
}
134
}
135
fragmentTestCache_.Clear();
136
}
137
138
// Take the raw GL extension and versioning data and turn into feature flags.
139
// TODO: This should use DrawContext::GetDeviceCaps() more and more, and eventually
140
// this can be shared between all the backends.
141
u32 GPU_GLES::CheckGPUFeatures() const {
142
u32 features = GPUCommonHW::CheckGPUFeatures();
143
144
features |= GPU_USE_16BIT_FORMATS;
145
146
if (gl_extensions.GLES3 || !gl_extensions.IsGLES)
147
features |= GPU_USE_TEXTURE_LOD_CONTROL;
148
149
bool canUseInstanceID = gl_extensions.EXT_draw_instanced || gl_extensions.ARB_draw_instanced;
150
bool canDefInstanceID = gl_extensions.IsGLES || gl_extensions.EXT_gpu_shader4 || gl_extensions.VersionGEThan(3, 1);
151
bool instanceRendering = gl_extensions.GLES3 || (canUseInstanceID && canDefInstanceID);
152
if (instanceRendering)
153
features |= GPU_USE_INSTANCE_RENDERING;
154
155
int maxVertexTextureImageUnits = gl_extensions.maxVertexTextureUnits;
156
if (maxVertexTextureImageUnits >= 3) // At least 3 for hardware tessellation
157
features |= GPU_USE_VERTEX_TEXTURE_FETCH;
158
159
if (gl_extensions.ARB_texture_float || gl_extensions.OES_texture_float)
160
features |= GPU_USE_TEXTURE_FLOAT;
161
162
if (!draw_->GetShaderLanguageDesc().bitwiseOps) {
163
features |= GPU_USE_FRAGMENT_TEST_CACHE;
164
}
165
166
// Can't use switch-case in older glsl.
167
if ((gl_extensions.IsGLES && !gl_extensions.GLES3) || (!gl_extensions.IsGLES && !gl_extensions.VersionGEThan(1, 3)))
168
features &= ~GPU_USE_LIGHT_UBERSHADER;
169
170
if (IsVREnabled() || g_Config.bForceVR) {
171
features |= GPU_USE_VIRTUAL_REALITY;
172
features &= ~GPU_USE_VS_RANGE_CULLING;
173
}
174
175
if (!gl_extensions.GLES3) {
176
// Heuristic.
177
features &= ~GPU_USE_FRAGMENT_UBERSHADER;
178
}
179
180
features = CheckGPUFeaturesLate(features);
181
182
if (draw_->GetBugs().Has(Draw::Bugs::ADRENO_RESOURCE_DEADLOCK) && g_Config.bVendorBugChecksEnabled) {
183
if (PSP_CoreParameter().compat.flags().OldAdrenoPixelDepthRoundingGL) {
184
features |= GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT;
185
}
186
}
187
188
// This is a bit ugly, but lets us reuse most of the depth logic in GPUCommon.
189
if (features & GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT) {
190
if (gl_extensions.IsGLES && !gl_extensions.GLES3) {
191
// Unsupported, switch to GPU_ROUND_DEPTH_TO_16BIT instead.
192
features &= ~GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT;
193
features |= GPU_ROUND_DEPTH_TO_16BIT;
194
}
195
}
196
return features;
197
}
198
199
void GPU_GLES::BuildReportingInfo() {
200
GLRenderManager *render = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
201
202
std::string glVendor = render->GetGLString(GL_VENDOR);
203
std::string glRenderer = render->GetGLString(GL_RENDERER);
204
std::string glVersion = render->GetGLString(GL_VERSION);
205
std::string glSlVersion = render->GetGLString(GL_SHADING_LANGUAGE_VERSION);
206
std::string glExtensions;
207
208
if (gl_extensions.VersionGEThan(3, 0)) {
209
glExtensions = g_all_gl_extensions;
210
} else {
211
glExtensions = render->GetGLString(GL_EXTENSIONS);
212
}
213
214
char temp[16384];
215
snprintf(temp, sizeof(temp), "%s (%s %s), %s (extensions: %s)", glVersion.c_str(), glVendor.c_str(), glRenderer.c_str(), glSlVersion.c_str(), glExtensions.c_str());
216
reportingPrimaryInfo_ = glVendor;
217
reportingFullInfo_ = temp;
218
219
Reporting::UpdateConfig();
220
}
221
222
void GPU_GLES::DeviceLost() {
223
INFO_LOG(Log::G3D, "GPU_GLES: DeviceLost");
224
225
// Simply drop all caches and textures.
226
// FBOs appear to survive? Or no?
227
// TransformDraw has registered as a GfxResourceHolder.
228
fragmentTestCache_.DeviceLost();
229
230
GPUCommonHW::DeviceLost();
231
}
232
233
void GPU_GLES::DeviceRestore(Draw::DrawContext *draw) {
234
GPUCommonHW::DeviceRestore(draw);
235
236
fragmentTestCache_.DeviceRestore(draw_);
237
}
238
239
void GPU_GLES::BeginHostFrame() {
240
GPUCommonHW::BeginHostFrame();
241
drawEngine_.BeginFrame();
242
243
textureCache_->StartFrame();
244
245
// Save the cache from time to time. TODO: How often? We save on exit, so shouldn't need to do this all that often.
246
247
const int saveShaderCacheFrameInterval = 32767; // power of 2 - 1. About every 10 minutes at 60fps.
248
if (shaderCachePath_.Valid() && !(gpuStats.numFlips & saveShaderCacheFrameInterval) && coreState == CORE_RUNNING_CPU) {
249
shaderManagerGL_->SaveCache(shaderCachePath_, &drawEngine_);
250
}
251
shaderManagerGL_->DirtyLastShader();
252
253
// Not sure if this is really needed.
254
gstate_c.Dirty(DIRTY_ALL_UNIFORMS);
255
256
framebufferManager_->BeginFrame();
257
258
fragmentTestCache_.Decimate();
259
if (gstate_c.useFlagsChanged) {
260
// TODO: It'd be better to recompile them in the background, probably?
261
// This most likely means that saw equal depth changed.
262
WARN_LOG(Log::G3D, "Shader use flags changed, clearing all shaders and depth buffers");
263
shaderManager_->ClearShaders();
264
framebufferManager_->ClearAllDepthBuffers();
265
gstate_c.useFlagsChanged = false;
266
}
267
}
268
269
void GPU_GLES::EndHostFrame() {
270
drawEngine_.EndFrame();
271
}
272
273
void GPU_GLES::FinishDeferred() {
274
// This finishes reading any vertex data that is pending.
275
drawEngine_.FinishDeferred();
276
}
277
278
void GPU_GLES::GetStats(char *buffer, size_t bufsize) {
279
size_t offset = FormatGPUStatsCommon(buffer, bufsize);
280
buffer += offset;
281
bufsize -= offset;
282
if ((int)bufsize < 0)
283
return;
284
snprintf(buffer, bufsize,
285
"Vertex, Fragment, Programs loaded: %d, %d, %d\n",
286
shaderManagerGL_->GetNumVertexShaders(),
287
shaderManagerGL_->GetNumFragmentShaders(),
288
shaderManagerGL_->GetNumPrograms()
289
);
290
}
291
292