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/Common/PostShader.cpp
Views: 1401
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
19
// Postprocessing shader manager
20
21
#include <string>
22
#include <vector>
23
#include <algorithm>
24
25
#include "Common/Log.h"
26
#include "Common/Data/Format/IniFile.h"
27
#include "Common/File/FileUtil.h"
28
#include "Common/File/DirListing.h"
29
#include "Common/File/VFS/VFS.h"
30
#include "Common/GPU/OpenGL/GLFeatures.h"
31
#include "Common/GPU/thin3d.h"
32
#include "Common/StringUtils.h"
33
34
#include "Core/Config.h"
35
#include "Core/System.h"
36
#include "GPU/Common/PostShader.h"
37
38
static std::vector<ShaderInfo> shaderInfo;
39
// Okay, not really "post" shaders, but related.
40
static std::vector<TextureShaderInfo> textureShaderInfo;
41
42
// Scans the directories for shader ini files and collects info about all the shaders found.
43
44
void LoadPostShaderInfo(Draw::DrawContext *draw, const std::vector<Path> &directories) {
45
std::vector<ShaderInfo> notVisible;
46
47
Draw::GPUVendor gpuVendor = Draw::GPUVendor::VENDOR_UNKNOWN;
48
if (draw) {
49
gpuVendor = draw->GetDeviceCaps().vendor;
50
}
51
52
shaderInfo.clear();
53
textureShaderInfo.clear();
54
55
auto appendShader = [&](const ShaderInfo &info) {
56
auto beginErase = std::remove(shaderInfo.begin(), shaderInfo.end(), info.name);
57
if (beginErase != shaderInfo.end()) {
58
shaderInfo.erase(beginErase, shaderInfo.end());
59
}
60
shaderInfo.push_back(info);
61
};
62
63
auto appendTextureShader = [&](const TextureShaderInfo &info) {
64
auto beginErase = std::remove(textureShaderInfo.begin(), textureShaderInfo.end(), info.name);
65
if (beginErase != textureShaderInfo.end()) {
66
textureShaderInfo.erase(beginErase, textureShaderInfo.end());
67
}
68
textureShaderInfo.push_back(info);
69
};
70
71
for (size_t d = 0; d < directories.size(); d++) {
72
std::vector<File::FileInfo> fileInfo;
73
g_VFS.GetFileListing(directories[d].c_str(), &fileInfo, "ini:");
74
75
if (fileInfo.empty()) {
76
File::GetFilesInDir(directories[d], &fileInfo, "ini:");
77
}
78
79
for (size_t f = 0; f < fileInfo.size(); f++) {
80
IniFile ini;
81
bool success = false;
82
if (fileInfo[f].isDirectory)
83
continue;
84
85
Path name = fileInfo[f].fullName;
86
Path path = directories[d];
87
// Hack around Android VFS path bug. really need to redesign this.
88
if (name.ToString().substr(0, 7) == "assets/")
89
name = Path(name.ToString().substr(7));
90
if (path.ToString().substr(0, 7) == "assets/")
91
path = Path(path.ToString().substr(7));
92
93
if (ini.LoadFromVFS(g_VFS, name.ToString()) || ini.Load(fileInfo[f].fullName)) {
94
success = true;
95
// vsh load. meh.
96
}
97
98
if (!success)
99
continue;
100
101
// Alright, let's loop through the sections and see if any is a shader.
102
for (size_t i = 0; i < ini.Sections().size(); i++) {
103
Section &section = *(ini.Sections()[i].get());
104
std::string shaderType;
105
section.Get("Type", &shaderType, "render");
106
107
std::vector<std::string> vendorBlacklist;
108
section.Get("VendorBlacklist", vendorBlacklist);
109
bool skipped = false;
110
for (auto &item : vendorBlacklist) {
111
Draw::GPUVendor blacklistedVendor = Draw::GPUVendor::VENDOR_UNKNOWN;
112
// TODO: This should probably be a function somewhere.
113
if (item == "ARM") {
114
blacklistedVendor = Draw::GPUVendor::VENDOR_ARM;
115
} else if (item == "Qualcomm") {
116
blacklistedVendor = Draw::GPUVendor::VENDOR_QUALCOMM;
117
} else if (item == "IMGTEC") {
118
blacklistedVendor = Draw::GPUVendor::VENDOR_IMGTEC;
119
} else if (item == "NVIDIA") {
120
blacklistedVendor = Draw::GPUVendor::VENDOR_NVIDIA;
121
} else if (item == "AMD") {
122
blacklistedVendor = Draw::GPUVendor::VENDOR_AMD;
123
} else if (item == "Broadcom") {
124
blacklistedVendor = Draw::GPUVendor::VENDOR_BROADCOM;
125
} else if (item == "Apple") {
126
blacklistedVendor = Draw::GPUVendor::VENDOR_APPLE;
127
} else if (item == "Intel") {
128
blacklistedVendor = Draw::GPUVendor::VENDOR_INTEL;
129
} else if (item == "Mesa") {
130
blacklistedVendor = Draw::GPUVendor::VENDOR_MESA;
131
}
132
if (blacklistedVendor == gpuVendor && blacklistedVendor != Draw::GPUVendor::VENDOR_UNKNOWN) {
133
skipped = true;
134
break;
135
}
136
}
137
138
if (skipped) {
139
continue;
140
}
141
142
if (section.Exists("Fragment") && section.Exists("Vertex") &&
143
(strncasecmp(shaderType.c_str(), "render", shaderType.size()) == 0 ||
144
strncasecmp(shaderType.c_str(), "StereoToMono", shaderType.size()) == 0)) {
145
// Valid shader!
146
ShaderInfo info{};
147
std::string temp;
148
info.section = section.name();
149
150
section.Get("Name", &info.name, section.name().c_str());
151
section.Get("Parent", &info.parent, "");
152
section.Get("Visible", &info.visible, true);
153
section.Get("Fragment", &temp, "");
154
info.fragmentShaderFile = path / temp;
155
section.Get("Vertex", &temp, "");
156
info.vertexShaderFile = path / temp;
157
section.Get("OutputResolution", &info.outputResolution, false);
158
section.Get("Upscaling", &info.isUpscalingFilter, false);
159
section.Get("SSAA", &info.SSAAFilterLevel, 0);
160
section.Get("60fps", &info.requires60fps, false);
161
section.Get("UsePreviousFrame", &info.usePreviousFrame, false);
162
163
if (info.parent == "Off")
164
info.parent.clear();
165
166
if (strncasecmp(shaderType.c_str(), "stereotomono", shaderType.size()) == 0) {
167
info.isStereo = true;
168
info.isUpscalingFilter = false;
169
info.parent.clear();
170
}
171
172
for (size_t i = 0; i < ARRAY_SIZE(info.settings); ++i) {
173
auto &setting = info.settings[i];
174
section.Get(StringFromFormat("SettingName%d", i + 1).c_str(), &setting.name, "");
175
section.Get(StringFromFormat("SettingDefaultValue%d", i + 1).c_str(), &setting.value, 0.0f);
176
section.Get(StringFromFormat("SettingMinValue%d", i + 1).c_str(), &setting.minValue, -1.0f);
177
section.Get(StringFromFormat("SettingMaxValue%d", i + 1).c_str(), &setting.maxValue, 1.0f);
178
section.Get(StringFromFormat("SettingStep%d", i + 1).c_str(), &setting.step, 0.01f);
179
}
180
181
// Let's ignore shaders we can't support. TODO: Not a very good check
182
if (gl_extensions.IsGLES && !gl_extensions.GLES3) {
183
bool requiresIntegerSupport;
184
section.Get("RequiresIntSupport", &requiresIntegerSupport, false);
185
if (requiresIntegerSupport)
186
continue;
187
}
188
189
if (info.visible) {
190
appendShader(info);
191
} else {
192
notVisible.push_back(info);
193
}
194
} else if (section.Exists("Compute") && strncasecmp(shaderType.c_str(), "texture", shaderType.size()) == 0) {
195
// This is a texture shader.
196
TextureShaderInfo info{};
197
std::string temp;
198
info.section = section.name();
199
section.Get("Name", &info.name, section.name().c_str());
200
section.Get("Compute", &temp, "");
201
section.Get("Scale", &info.scaleFactor, 0);
202
info.computeShaderFile = path / temp;
203
if (info.scaleFactor >= 2 && info.scaleFactor < 8) {
204
appendTextureShader(info);
205
}
206
} else if (!section.name().empty()) {
207
WARN_LOG(Log::G3D, "Unrecognized shader type '%s' or invalid shader in section '%s'", shaderType.c_str(), section.name().c_str());
208
}
209
}
210
}
211
}
212
213
// Sort shaders alphabetically.
214
std::sort(shaderInfo.begin(), shaderInfo.end());
215
std::sort(textureShaderInfo.begin(), textureShaderInfo.end());
216
217
ShaderInfo off{};
218
off.visible = true;
219
off.name = "Off";
220
off.section = "Off";
221
for (size_t i = 0; i < ARRAY_SIZE(off.settings); ++i) {
222
off.settings[i].name.clear();
223
off.settings[i].value = 0.0f;
224
off.settings[i].minValue = -1.0f;
225
off.settings[i].maxValue = 1.0f;
226
off.settings[i].step = 0.01f;
227
}
228
229
TextureShaderInfo textureOff{};
230
textureOff.name = "Off";
231
textureOff.section = "Off";
232
textureShaderInfo.insert(textureShaderInfo.begin(), textureOff);
233
234
// We always want the not visible ones at the end. Makes menus easier.
235
shaderInfo.reserve(notVisible.size() + 1);
236
shaderInfo.insert(shaderInfo.begin(), off);
237
for (const auto &info : notVisible) {
238
appendShader(info);
239
}
240
}
241
242
// Scans the directories for shader ini files and collects info about all the shaders found.
243
void ReloadAllPostShaderInfo(Draw::DrawContext *draw) {
244
std::vector<Path> directories;
245
directories.push_back(Path("shaders")); // For VFS
246
directories.push_back(GetSysDirectory(DIRECTORY_CUSTOM_SHADERS));
247
LoadPostShaderInfo(draw, directories);
248
}
249
250
void RemoveUnknownPostShaders(std::vector<std::string> *names) {
251
for (auto iter = names->begin(); iter != names->end(); ) {
252
if (GetPostShaderInfo(*iter) == nullptr) {
253
iter = names->erase(iter);
254
} else {
255
++iter;
256
}
257
}
258
}
259
260
const ShaderInfo *GetPostShaderInfo(std::string_view name) {
261
for (size_t i = 0; i < shaderInfo.size(); i++) {
262
if (shaderInfo[i].section == name)
263
return &shaderInfo[i];
264
}
265
return nullptr;
266
}
267
268
std::vector<const ShaderInfo *> GetPostShaderChain(const std::string &name) {
269
std::vector<const ShaderInfo *> backwards;
270
const ShaderInfo *shaderInfo = GetPostShaderInfo(name);
271
while (shaderInfo) {
272
backwards.push_back(shaderInfo);
273
274
if (!shaderInfo->parent.empty()) {
275
shaderInfo = GetPostShaderInfo(shaderInfo->parent);
276
} else {
277
shaderInfo = nullptr;
278
}
279
auto dup = std::find(backwards.begin(), backwards.end(), shaderInfo);
280
if (dup != backwards.end()) {
281
// Don't loop forever.
282
break;
283
}
284
}
285
286
if (!backwards.empty())
287
std::reverse(backwards.begin(), backwards.end());
288
// Not backwards anymore.
289
return backwards;
290
}
291
292
std::vector<const ShaderInfo *> GetFullPostShadersChain(const std::vector<std::string> &names) {
293
std::vector<const ShaderInfo *> fullChain;
294
for (const auto &shaderName : names) {
295
const auto &shaderChain = GetPostShaderChain(shaderName);
296
fullChain.insert(fullChain.end(), shaderChain.begin(), shaderChain.end());
297
}
298
return fullChain;
299
}
300
301
bool PostShaderChainRequires60FPS(const std::vector<const ShaderInfo *> &chain) {
302
for (auto shaderInfo : chain) {
303
if (shaderInfo->requires60fps)
304
return true;
305
}
306
return false;
307
}
308
309
const std::vector<ShaderInfo> &GetAllPostShaderInfo() {
310
return shaderInfo;
311
}
312
313
const TextureShaderInfo *GetTextureShaderInfo(std::string_view name) {
314
for (auto &info : textureShaderInfo) {
315
if (info.section == name) {
316
return &info;
317
}
318
}
319
return nullptr;
320
}
321
const std::vector<TextureShaderInfo> &GetAllTextureShaderInfo() {
322
return textureShaderInfo;
323
}
324
325
void FixPostShaderOrder(std::vector<std::string> *names) {
326
// There's one rule only that we enforce - only one shader can use UsePreviousFrame,
327
// and it has to be the last one. So we simply remove any we find from the list,
328
// and then append it to the end if there is one.
329
std::string prevFrameShader;
330
for (auto iter = names->begin(); iter != names->end(); ) {
331
const ShaderInfo *info = GetPostShaderInfo(*iter);
332
if (info) {
333
if (info->usePreviousFrame) {
334
prevFrameShader = *iter;
335
iter = names->erase(iter++);
336
continue;
337
}
338
}
339
++iter;
340
}
341
342
if (!prevFrameShader.empty()) {
343
names->push_back(prevFrameShader);
344
}
345
}
346
347