CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/GLES/GPU_GLES.cpp
Views: 1401
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/Config.h"
32
#include "Core/Reporting.h"
33
#include "Core/System.h"
34
#include "Core/ELF/ParamSFO.h"
35
36
#include "GPU/GPUState.h"
37
#include "GPU/ge_constants.h"
38
#include "GPU/GeDisasm.h"
39
#include "GPU/Common/FramebufferManagerCommon.h"
40
#include "GPU/GLES/ShaderManagerGLES.h"
41
#include "GPU/GLES/GPU_GLES.h"
42
#include "GPU/GLES/FramebufferManagerGLES.h"
43
#include "GPU/GLES/DrawEngineGLES.h"
44
#include "GPU/GLES/TextureCacheGLES.h"
45
46
#ifdef _WIN32
47
#include "Windows/GPU/WindowsGLContext.h"
48
#endif
49
50
GPU_GLES::GPU_GLES(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
51
: GPUCommonHW(gfxCtx, draw), drawEngine_(draw), fragmentTestCache_(draw) {
52
gstate_c.SetUseFlags(CheckGPUFeatures());
53
54
shaderManagerGL_ = new ShaderManagerGLES(draw);
55
framebufferManagerGL_ = new FramebufferManagerGLES(draw);
56
framebufferManager_ = framebufferManagerGL_;
57
textureCacheGL_ = new TextureCacheGLES(draw, framebufferManager_->GetDraw2D());
58
textureCache_ = textureCacheGL_;
59
drawEngineCommon_ = &drawEngine_;
60
shaderManager_ = shaderManagerGL_;
61
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
auto gr = GetI18NCategory(I18NCat::GRAPHICS);
121
g_OSD.Show(OSDType::MESSAGE_WARNING, gr->T("Turn off Hardware Tessellation - unsupported"));
122
}
123
}
124
}
125
126
GPU_GLES::~GPU_GLES() {
127
// If we're here during app shutdown (exiting the Windows app in-game, for example)
128
// everything should already be cleared since DeviceLost has been run.
129
130
if (shaderCachePath_.Valid() && draw_) {
131
if (g_Config.bShaderCache) {
132
shaderManagerGL_->SaveCache(shaderCachePath_, &drawEngine_);
133
} else {
134
INFO_LOG(Log::G3D, "Shader cache disabled. Not saving.");
135
}
136
}
137
fragmentTestCache_.Clear();
138
}
139
140
// Take the raw GL extension and versioning data and turn into feature flags.
141
// TODO: This should use DrawContext::GetDeviceCaps() more and more, and eventually
142
// this can be shared between all the backends.
143
u32 GPU_GLES::CheckGPUFeatures() const {
144
u32 features = GPUCommonHW::CheckGPUFeatures();
145
146
features |= GPU_USE_16BIT_FORMATS;
147
148
if (gl_extensions.GLES3 || !gl_extensions.IsGLES)
149
features |= GPU_USE_TEXTURE_LOD_CONTROL;
150
151
bool canUseInstanceID = gl_extensions.EXT_draw_instanced || gl_extensions.ARB_draw_instanced;
152
bool canDefInstanceID = gl_extensions.IsGLES || gl_extensions.EXT_gpu_shader4 || gl_extensions.VersionGEThan(3, 1);
153
bool instanceRendering = gl_extensions.GLES3 || (canUseInstanceID && canDefInstanceID);
154
if (instanceRendering)
155
features |= GPU_USE_INSTANCE_RENDERING;
156
157
int maxVertexTextureImageUnits = gl_extensions.maxVertexTextureUnits;
158
if (maxVertexTextureImageUnits >= 3) // At least 3 for hardware tessellation
159
features |= GPU_USE_VERTEX_TEXTURE_FETCH;
160
161
if (gl_extensions.ARB_texture_float || gl_extensions.OES_texture_float)
162
features |= GPU_USE_TEXTURE_FLOAT;
163
164
if (!draw_->GetShaderLanguageDesc().bitwiseOps) {
165
features |= GPU_USE_FRAGMENT_TEST_CACHE;
166
}
167
168
// Can't use switch-case in older glsl.
169
if ((gl_extensions.IsGLES && !gl_extensions.GLES3) || (!gl_extensions.IsGLES && !gl_extensions.VersionGEThan(1, 3)))
170
features &= ~GPU_USE_LIGHT_UBERSHADER;
171
172
if (IsVREnabled() || g_Config.bForceVR) {
173
features |= GPU_USE_VIRTUAL_REALITY;
174
features &= ~GPU_USE_VS_RANGE_CULLING;
175
}
176
177
if (!gl_extensions.GLES3) {
178
// Heuristic.
179
features &= ~GPU_USE_FRAGMENT_UBERSHADER;
180
}
181
182
features = CheckGPUFeaturesLate(features);
183
184
if (draw_->GetBugs().Has(Draw::Bugs::ADRENO_RESOURCE_DEADLOCK) && g_Config.bVendorBugChecksEnabled) {
185
if (PSP_CoreParameter().compat.flags().OldAdrenoPixelDepthRoundingGL) {
186
features |= GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT;
187
}
188
}
189
190
// This is a bit ugly, but lets us reuse most of the depth logic in GPUCommon.
191
if (features & GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT) {
192
if (gl_extensions.IsGLES && !gl_extensions.GLES3) {
193
// Unsupported, switch to GPU_ROUND_DEPTH_TO_16BIT instead.
194
features &= ~GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT;
195
features |= GPU_ROUND_DEPTH_TO_16BIT;
196
}
197
}
198
return features;
199
}
200
201
void GPU_GLES::BuildReportingInfo() {
202
GLRenderManager *render = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
203
204
std::string glVendor = render->GetGLString(GL_VENDOR);
205
std::string glRenderer = render->GetGLString(GL_RENDERER);
206
std::string glVersion = render->GetGLString(GL_VERSION);
207
std::string glSlVersion = render->GetGLString(GL_SHADING_LANGUAGE_VERSION);
208
std::string glExtensions;
209
210
if (gl_extensions.VersionGEThan(3, 0)) {
211
glExtensions = g_all_gl_extensions;
212
} else {
213
glExtensions = render->GetGLString(GL_EXTENSIONS);
214
}
215
216
char temp[16384];
217
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());
218
reportingPrimaryInfo_ = glVendor;
219
reportingFullInfo_ = temp;
220
221
Reporting::UpdateConfig();
222
}
223
224
void GPU_GLES::DeviceLost() {
225
INFO_LOG(Log::G3D, "GPU_GLES: DeviceLost");
226
227
// Simply drop all caches and textures.
228
// FBOs appear to survive? Or no?
229
// TransformDraw has registered as a GfxResourceHolder.
230
fragmentTestCache_.DeviceLost();
231
232
GPUCommonHW::DeviceLost();
233
}
234
235
void GPU_GLES::DeviceRestore(Draw::DrawContext *draw) {
236
GPUCommonHW::DeviceRestore(draw);
237
238
fragmentTestCache_.DeviceRestore(draw_);
239
}
240
241
void GPU_GLES::BeginHostFrame() {
242
GPUCommonHW::BeginHostFrame();
243
drawEngine_.BeginFrame();
244
245
textureCache_->StartFrame();
246
247
// Save the cache from time to time. TODO: How often? We save on exit, so shouldn't need to do this all that often.
248
249
const int saveShaderCacheFrameInterval = 32767; // power of 2 - 1. About every 10 minutes at 60fps.
250
if (shaderCachePath_.Valid() && !(gpuStats.numFlips & saveShaderCacheFrameInterval) && coreState == CORE_RUNNING) {
251
shaderManagerGL_->SaveCache(shaderCachePath_, &drawEngine_);
252
}
253
shaderManagerGL_->DirtyLastShader();
254
255
// Not sure if this is really needed.
256
gstate_c.Dirty(DIRTY_ALL_UNIFORMS);
257
258
framebufferManager_->BeginFrame();
259
260
fragmentTestCache_.Decimate();
261
if (gstate_c.useFlagsChanged) {
262
// TODO: It'd be better to recompile them in the background, probably?
263
// This most likely means that saw equal depth changed.
264
WARN_LOG(Log::G3D, "Shader use flags changed, clearing all shaders and depth buffers");
265
shaderManager_->ClearShaders();
266
framebufferManager_->ClearAllDepthBuffers();
267
gstate_c.useFlagsChanged = false;
268
}
269
}
270
271
void GPU_GLES::EndHostFrame() {
272
drawEngine_.EndFrame();
273
}
274
275
void GPU_GLES::FinishDeferred() {
276
// This finishes reading any vertex data that is pending.
277
drawEngine_.FinishDeferred();
278
}
279
280
void GPU_GLES::GetStats(char *buffer, size_t bufsize) {
281
size_t offset = FormatGPUStatsCommon(buffer, bufsize);
282
buffer += offset;
283
bufsize -= offset;
284
if ((int)bufsize < 0)
285
return;
286
snprintf(buffer, bufsize,
287
"Vertex, Fragment, Programs loaded: %d, %d, %d\n",
288
shaderManagerGL_->GetNumVertexShaders(),
289
shaderManagerGL_->GetNumFragmentShaders(),
290
shaderManagerGL_->GetNumPrograms()
291
);
292
}
293
294