CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/GPU/Common/PostShader.cpp
Views: 1401
// Copyright (c) 2013- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.161718// Postprocessing shader manager1920#include <string>21#include <vector>22#include <algorithm>2324#include "Common/Log.h"25#include "Common/Data/Format/IniFile.h"26#include "Common/File/FileUtil.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"3233#include "Core/Config.h"34#include "Core/System.h"35#include "GPU/Common/PostShader.h"3637static std::vector<ShaderInfo> shaderInfo;38// Okay, not really "post" shaders, but related.39static std::vector<TextureShaderInfo> textureShaderInfo;4041// Scans the directories for shader ini files and collects info about all the shaders found.4243void LoadPostShaderInfo(Draw::DrawContext *draw, const std::vector<Path> &directories) {44std::vector<ShaderInfo> notVisible;4546Draw::GPUVendor gpuVendor = Draw::GPUVendor::VENDOR_UNKNOWN;47if (draw) {48gpuVendor = draw->GetDeviceCaps().vendor;49}5051shaderInfo.clear();52textureShaderInfo.clear();5354auto appendShader = [&](const ShaderInfo &info) {55auto beginErase = std::remove(shaderInfo.begin(), shaderInfo.end(), info.name);56if (beginErase != shaderInfo.end()) {57shaderInfo.erase(beginErase, shaderInfo.end());58}59shaderInfo.push_back(info);60};6162auto appendTextureShader = [&](const TextureShaderInfo &info) {63auto beginErase = std::remove(textureShaderInfo.begin(), textureShaderInfo.end(), info.name);64if (beginErase != textureShaderInfo.end()) {65textureShaderInfo.erase(beginErase, textureShaderInfo.end());66}67textureShaderInfo.push_back(info);68};6970for (size_t d = 0; d < directories.size(); d++) {71std::vector<File::FileInfo> fileInfo;72g_VFS.GetFileListing(directories[d].c_str(), &fileInfo, "ini:");7374if (fileInfo.empty()) {75File::GetFilesInDir(directories[d], &fileInfo, "ini:");76}7778for (size_t f = 0; f < fileInfo.size(); f++) {79IniFile ini;80bool success = false;81if (fileInfo[f].isDirectory)82continue;8384Path name = fileInfo[f].fullName;85Path path = directories[d];86// Hack around Android VFS path bug. really need to redesign this.87if (name.ToString().substr(0, 7) == "assets/")88name = Path(name.ToString().substr(7));89if (path.ToString().substr(0, 7) == "assets/")90path = Path(path.ToString().substr(7));9192if (ini.LoadFromVFS(g_VFS, name.ToString()) || ini.Load(fileInfo[f].fullName)) {93success = true;94// vsh load. meh.95}9697if (!success)98continue;99100// Alright, let's loop through the sections and see if any is a shader.101for (size_t i = 0; i < ini.Sections().size(); i++) {102Section §ion = *(ini.Sections()[i].get());103std::string shaderType;104section.Get("Type", &shaderType, "render");105106std::vector<std::string> vendorBlacklist;107section.Get("VendorBlacklist", vendorBlacklist);108bool skipped = false;109for (auto &item : vendorBlacklist) {110Draw::GPUVendor blacklistedVendor = Draw::GPUVendor::VENDOR_UNKNOWN;111// TODO: This should probably be a function somewhere.112if (item == "ARM") {113blacklistedVendor = Draw::GPUVendor::VENDOR_ARM;114} else if (item == "Qualcomm") {115blacklistedVendor = Draw::GPUVendor::VENDOR_QUALCOMM;116} else if (item == "IMGTEC") {117blacklistedVendor = Draw::GPUVendor::VENDOR_IMGTEC;118} else if (item == "NVIDIA") {119blacklistedVendor = Draw::GPUVendor::VENDOR_NVIDIA;120} else if (item == "AMD") {121blacklistedVendor = Draw::GPUVendor::VENDOR_AMD;122} else if (item == "Broadcom") {123blacklistedVendor = Draw::GPUVendor::VENDOR_BROADCOM;124} else if (item == "Apple") {125blacklistedVendor = Draw::GPUVendor::VENDOR_APPLE;126} else if (item == "Intel") {127blacklistedVendor = Draw::GPUVendor::VENDOR_INTEL;128} else if (item == "Mesa") {129blacklistedVendor = Draw::GPUVendor::VENDOR_MESA;130}131if (blacklistedVendor == gpuVendor && blacklistedVendor != Draw::GPUVendor::VENDOR_UNKNOWN) {132skipped = true;133break;134}135}136137if (skipped) {138continue;139}140141if (section.Exists("Fragment") && section.Exists("Vertex") &&142(strncasecmp(shaderType.c_str(), "render", shaderType.size()) == 0 ||143strncasecmp(shaderType.c_str(), "StereoToMono", shaderType.size()) == 0)) {144// Valid shader!145ShaderInfo info{};146std::string temp;147info.section = section.name();148149section.Get("Name", &info.name, section.name().c_str());150section.Get("Parent", &info.parent, "");151section.Get("Visible", &info.visible, true);152section.Get("Fragment", &temp, "");153info.fragmentShaderFile = path / temp;154section.Get("Vertex", &temp, "");155info.vertexShaderFile = path / temp;156section.Get("OutputResolution", &info.outputResolution, false);157section.Get("Upscaling", &info.isUpscalingFilter, false);158section.Get("SSAA", &info.SSAAFilterLevel, 0);159section.Get("60fps", &info.requires60fps, false);160section.Get("UsePreviousFrame", &info.usePreviousFrame, false);161162if (info.parent == "Off")163info.parent.clear();164165if (strncasecmp(shaderType.c_str(), "stereotomono", shaderType.size()) == 0) {166info.isStereo = true;167info.isUpscalingFilter = false;168info.parent.clear();169}170171for (size_t i = 0; i < ARRAY_SIZE(info.settings); ++i) {172auto &setting = info.settings[i];173section.Get(StringFromFormat("SettingName%d", i + 1).c_str(), &setting.name, "");174section.Get(StringFromFormat("SettingDefaultValue%d", i + 1).c_str(), &setting.value, 0.0f);175section.Get(StringFromFormat("SettingMinValue%d", i + 1).c_str(), &setting.minValue, -1.0f);176section.Get(StringFromFormat("SettingMaxValue%d", i + 1).c_str(), &setting.maxValue, 1.0f);177section.Get(StringFromFormat("SettingStep%d", i + 1).c_str(), &setting.step, 0.01f);178}179180// Let's ignore shaders we can't support. TODO: Not a very good check181if (gl_extensions.IsGLES && !gl_extensions.GLES3) {182bool requiresIntegerSupport;183section.Get("RequiresIntSupport", &requiresIntegerSupport, false);184if (requiresIntegerSupport)185continue;186}187188if (info.visible) {189appendShader(info);190} else {191notVisible.push_back(info);192}193} else if (section.Exists("Compute") && strncasecmp(shaderType.c_str(), "texture", shaderType.size()) == 0) {194// This is a texture shader.195TextureShaderInfo info{};196std::string temp;197info.section = section.name();198section.Get("Name", &info.name, section.name().c_str());199section.Get("Compute", &temp, "");200section.Get("Scale", &info.scaleFactor, 0);201info.computeShaderFile = path / temp;202if (info.scaleFactor >= 2 && info.scaleFactor < 8) {203appendTextureShader(info);204}205} else if (!section.name().empty()) {206WARN_LOG(Log::G3D, "Unrecognized shader type '%s' or invalid shader in section '%s'", shaderType.c_str(), section.name().c_str());207}208}209}210}211212// Sort shaders alphabetically.213std::sort(shaderInfo.begin(), shaderInfo.end());214std::sort(textureShaderInfo.begin(), textureShaderInfo.end());215216ShaderInfo off{};217off.visible = true;218off.name = "Off";219off.section = "Off";220for (size_t i = 0; i < ARRAY_SIZE(off.settings); ++i) {221off.settings[i].name.clear();222off.settings[i].value = 0.0f;223off.settings[i].minValue = -1.0f;224off.settings[i].maxValue = 1.0f;225off.settings[i].step = 0.01f;226}227228TextureShaderInfo textureOff{};229textureOff.name = "Off";230textureOff.section = "Off";231textureShaderInfo.insert(textureShaderInfo.begin(), textureOff);232233// We always want the not visible ones at the end. Makes menus easier.234shaderInfo.reserve(notVisible.size() + 1);235shaderInfo.insert(shaderInfo.begin(), off);236for (const auto &info : notVisible) {237appendShader(info);238}239}240241// Scans the directories for shader ini files and collects info about all the shaders found.242void ReloadAllPostShaderInfo(Draw::DrawContext *draw) {243std::vector<Path> directories;244directories.push_back(Path("shaders")); // For VFS245directories.push_back(GetSysDirectory(DIRECTORY_CUSTOM_SHADERS));246LoadPostShaderInfo(draw, directories);247}248249void RemoveUnknownPostShaders(std::vector<std::string> *names) {250for (auto iter = names->begin(); iter != names->end(); ) {251if (GetPostShaderInfo(*iter) == nullptr) {252iter = names->erase(iter);253} else {254++iter;255}256}257}258259const ShaderInfo *GetPostShaderInfo(std::string_view name) {260for (size_t i = 0; i < shaderInfo.size(); i++) {261if (shaderInfo[i].section == name)262return &shaderInfo[i];263}264return nullptr;265}266267std::vector<const ShaderInfo *> GetPostShaderChain(const std::string &name) {268std::vector<const ShaderInfo *> backwards;269const ShaderInfo *shaderInfo = GetPostShaderInfo(name);270while (shaderInfo) {271backwards.push_back(shaderInfo);272273if (!shaderInfo->parent.empty()) {274shaderInfo = GetPostShaderInfo(shaderInfo->parent);275} else {276shaderInfo = nullptr;277}278auto dup = std::find(backwards.begin(), backwards.end(), shaderInfo);279if (dup != backwards.end()) {280// Don't loop forever.281break;282}283}284285if (!backwards.empty())286std::reverse(backwards.begin(), backwards.end());287// Not backwards anymore.288return backwards;289}290291std::vector<const ShaderInfo *> GetFullPostShadersChain(const std::vector<std::string> &names) {292std::vector<const ShaderInfo *> fullChain;293for (const auto &shaderName : names) {294const auto &shaderChain = GetPostShaderChain(shaderName);295fullChain.insert(fullChain.end(), shaderChain.begin(), shaderChain.end());296}297return fullChain;298}299300bool PostShaderChainRequires60FPS(const std::vector<const ShaderInfo *> &chain) {301for (auto shaderInfo : chain) {302if (shaderInfo->requires60fps)303return true;304}305return false;306}307308const std::vector<ShaderInfo> &GetAllPostShaderInfo() {309return shaderInfo;310}311312const TextureShaderInfo *GetTextureShaderInfo(std::string_view name) {313for (auto &info : textureShaderInfo) {314if (info.section == name) {315return &info;316}317}318return nullptr;319}320const std::vector<TextureShaderInfo> &GetAllTextureShaderInfo() {321return textureShaderInfo;322}323324void FixPostShaderOrder(std::vector<std::string> *names) {325// There's one rule only that we enforce - only one shader can use UsePreviousFrame,326// and it has to be the last one. So we simply remove any we find from the list,327// and then append it to the end if there is one.328std::string prevFrameShader;329for (auto iter = names->begin(); iter != names->end(); ) {330const ShaderInfo *info = GetPostShaderInfo(*iter);331if (info) {332if (info->usePreviousFrame) {333prevFrameShader = *iter;334iter = names->erase(iter++);335continue;336}337}338++iter;339}340341if (!prevFrameShader.empty()) {342names->push_back(prevFrameShader);343}344}345346347