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