Skip to content

Commit

Permalink
Merge pull request #12901 from iota97/postshader-setting
Browse files Browse the repository at this point in the history
Post shader setting uniform
  • Loading branch information
hrydgard committed May 16, 2020
2 parents 02fdf00 + b07874c commit 67ab51b
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 19 deletions.
32 changes: 32 additions & 0 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,12 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) {
}
}

auto postShaderSetting = iniFile.GetOrCreateSection("PostShaderSetting")->ToMap();
mPostShaderSetting.clear();
for (auto it : postShaderSetting) {
mPostShaderSetting[it.first] = std::stof(it.second);
}

// This caps the exponent 4 (so 16x.)
if (iAnisotropyLevel > 4) {
iAnisotropyLevel = 4;
Expand Down Expand Up @@ -1301,6 +1307,14 @@ void Config::Save(const char *saveReason) {
pinnedPaths->Set(keyName, vPinnedPaths[i]);
}

if (!bGameSpecific) {
IniFile::Section *postShaderSetting = iniFile.GetOrCreateSection("PostShaderSetting");
postShaderSetting->Clear();
for (auto it = mPostShaderSetting.begin(), end = mPostShaderSetting.end(); it != end; ++it) {
postShaderSetting->Set(it->first.c_str(), it->second);
}
}

IniFile::Section *control = iniFile.GetOrCreateSection("Control");
control->Delete("DPadRadius");

Expand Down Expand Up @@ -1551,6 +1565,12 @@ bool Config::saveGameConfig(const std::string &pGameId, const std::string &title
}
});

IniFile::Section *postShaderSetting = iniFile.GetOrCreateSection("PostShaderSetting");
postShaderSetting->Clear();
for (auto it = mPostShaderSetting.begin(), end = mPostShaderSetting.end(); it != end; ++it) {
postShaderSetting->Set(it->first.c_str(), it->second);
}

KeyMap::SaveToIni(iniFile);
iniFile.Save(fullIniFilePath);

Expand All @@ -1569,6 +1589,12 @@ bool Config::loadGameConfig(const std::string &pGameId, const std::string &title
IniFile iniFile;
iniFile.Load(iniFileNameFull);

auto postShaderSetting = iniFile.GetOrCreateSection("PostShaderSetting")->ToMap();
mPostShaderSetting.clear();
for (auto it : postShaderSetting) {
mPostShaderSetting[it.first] = std::stof(it.second);
}

IterateSettings(iniFile, [](IniFile::Section *section, ConfigSetting *setting) {
if (setting->perGame_) {
setting->Get(section);
Expand All @@ -1593,6 +1619,12 @@ void Config::unloadGameConfig() {
}
});

auto postShaderSetting = iniFile.GetOrCreateSection("PostShaderSetting")->ToMap();
mPostShaderSetting.clear();
for (auto it : postShaderSetting) {
mPostShaderSetting[it.first] = std::stof(it.second);
}

LoadStandardControllerIni();
}
}
Expand Down
1 change: 1 addition & 0 deletions Core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ struct Config {
int iSplineBezierQuality; // 0 = low , 1 = Intermediate , 2 = High
bool bHardwareTessellation;
std::string sPostShaderName; // Off for off.
std::map<std::string, float> mPostShaderSetting;
bool bGfxDebugOutput;
bool bGfxDebugSplitSubmit;
int iInflightFrames;
Expand Down
49 changes: 49 additions & 0 deletions GPU/Common/PostShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ void LoadPostShaderInfo(std::vector<std::string> directories) {
off.isUpscalingFilter = false;
off.SSAAFilterLevel = 0;
off.requires60fps = false;
off.settingName1 = "";
off.settingValue1 = 0.0f;
off.minSettingValue1 = -1.0f;
off.maxSettingValue1 = 1.0f;
off.settingStep1 = 0.01f;
off.settingName2 = "";
off.settingValue2 = 0.0f;
off.minSettingValue2 = -1.0f;
off.maxSettingValue2 = 1.0f;
off.settingStep2 = 0.01f;
off.settingName3 = "";
off.settingValue3 = 0.0f;
off.minSettingValue3 = -1.0f;
off.maxSettingValue3 = 1.0f;
off.settingStep3 = 0.01f;
off.settingName4 = "";
off.settingValue4 = 0.0f;
off.minSettingValue4 = -1.0f;
off.maxSettingValue4 = 1.0f;
off.settingStep4 = 0.01f;
shaderInfo.push_back(off);

for (size_t d = 0; d < directories.size(); d++) {
Expand Down Expand Up @@ -90,6 +110,35 @@ void LoadPostShaderInfo(std::vector<std::string> directories) {
section.Get("Upscaling", &info.isUpscalingFilter, false);
section.Get("SSAA", &info.SSAAFilterLevel, 0);
section.Get("60fps", &info.requires60fps, false);
section.Get("SettingName1", &info.settingName1, "");
section.Get("SettingDefaultValue1", &info.settingValue1, 0.0f);
section.Get("SettingMinValue1", &info.minSettingValue1, -1.0f);
section.Get("SettingMaxValue1", &info.maxSettingValue1, 1.0f);
section.Get("SettingStep1", &info.settingStep1, 0.01f);
section.Get("SettingName2", &info.settingName2, "");
section.Get("SettingDefaultValue2", &info.settingValue2, 0.0f);
section.Get("SettingMinValue2", &info.minSettingValue2, -1.0f);
section.Get("SettingMaxValue2", &info.maxSettingValue2, 1.0f);
section.Get("SettingStep2", &info.settingStep2, 0.01f);
section.Get("SettingName3", &info.settingName3, "");
section.Get("SettingDefaultValue3", &info.settingValue3, 0.0f);
section.Get("SettingMinValue3", &info.minSettingValue3, -1.0f);
section.Get("SettingMaxValue3", &info.maxSettingValue3, 1.0f);
section.Get("SettingStep3", &info.settingStep3, 0.01f);
section.Get("SettingName4", &info.settingName4, "");
section.Get("SettingDefaultValue4", &info.settingValue4, 0.0f);
section.Get("SettingMinValue4", &info.minSettingValue4, -1.0f);
section.Get("SettingMaxValue4", &info.maxSettingValue4, 1.0f);
section.Get("SettingStep4", &info.settingStep4, 0.01f);

if (g_Config.mPostShaderSetting.find(info.section + "SettingValue1") == g_Config.mPostShaderSetting.end())
g_Config.mPostShaderSetting.insert(std::pair<std::string, float>(info.section + "SettingValue1", info.settingValue1));
if (g_Config.mPostShaderSetting.find(info.section + "SettingValue2") == g_Config.mPostShaderSetting.end())
g_Config.mPostShaderSetting.insert(std::pair<std::string, float>(info.section + "SettingValue2", info.settingValue2));
if (g_Config.mPostShaderSetting.find(info.section + "SettingValue3") == g_Config.mPostShaderSetting.end())
g_Config.mPostShaderSetting.insert(std::pair<std::string, float>(info.section + "SettingValue3", info.settingValue3));
if (g_Config.mPostShaderSetting.find(info.section + "SettingValue4") == g_Config.mPostShaderSetting.end())
g_Config.mPostShaderSetting.insert(std::pair<std::string, float>(info.section + "SettingValue4", info.settingValue4));

// Let's ignore shaders we can't support. TODO: Not a very good check
if (gl_extensions.IsGLES && !gl_extensions.GLES3) {
Expand Down
21 changes: 21 additions & 0 deletions GPU/Common/PostShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ struct ShaderInfo {
// Force constant/max refresh for animated filters
bool requires60fps;

std::string settingName1;
float settingValue1;
float maxSettingValue1;
float minSettingValue1;
float settingStep1;
std::string settingName2;
float settingValue2;
float maxSettingValue2;
float minSettingValue2;
float settingStep2;
std::string settingName3;
float settingValue3;
float maxSettingValue3;
float minSettingValue3;
float settingStep3;
std::string settingName4;
float settingValue4;
float maxSettingValue4;
float minSettingValue4;
float settingStep4;

// TODO: Add support for all kinds of fun options like mapping the depth buffer,
// SRGB texture reads, multiple shaders chained, etc.

Expand Down
8 changes: 7 additions & 1 deletion GPU/Common/PresentationCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ void PresentationCommon::CalculatePostShaderUniforms(int bufferWidth, int buffer
// The shader translator tacks this onto our shaders, if we don't set it they render garbage.
uniforms->gl_HalfPixel[0] = u_pixel_delta * 0.5f;
uniforms->gl_HalfPixel[1] = v_pixel_delta * 0.5f;

uniforms->setting[0] = g_Config.mPostShaderSetting[g_Config.sPostShaderName + "SettingValue1"];;
uniforms->setting[1] = g_Config.mPostShaderSetting[g_Config.sPostShaderName + "SettingValue2"];
uniforms->setting[2] = g_Config.mPostShaderSetting[g_Config.sPostShaderName + "SettingValue3"];
uniforms->setting[3] = g_Config.mPostShaderSetting[g_Config.sPostShaderName + "SettingValue4"];
}

static std::string ReadShaderSrc(const std::string &filename) {
Expand Down Expand Up @@ -214,7 +219,8 @@ bool PresentationCommon::UpdatePostShader() {
{ "u_texelDelta", 1, 1, Draw::UniformType::FLOAT2, offsetof(PostShaderUniforms, texelDelta) },
{ "u_pixelDelta", 2, 2, Draw::UniformType::FLOAT2, offsetof(PostShaderUniforms, pixelDelta) },
{ "u_time", 3, 3, Draw::UniformType::FLOAT4, offsetof(PostShaderUniforms, time) },
{ "u_video", 4, 4, Draw::UniformType::FLOAT1, offsetof(PostShaderUniforms, video) },
{ "u_setting", 4, 4, Draw::UniformType::FLOAT4, offsetof(PostShaderUniforms, setting) },
{ "u_video", 5, 5, Draw::UniformType::FLOAT1, offsetof(PostShaderUniforms, video) },
} };
Draw::Pipeline *pipeline = CreatePipeline({ vs, fs }, true, &postShaderDesc);
if (!pipeline)
Expand Down
1 change: 1 addition & 0 deletions GPU/Common/PresentationCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct CardboardSettings {
struct PostShaderUniforms {
float texelDelta[2]; float pixelDelta[2];
float time[4];
float setting[4];
float video; float pad[3];
// Used on Direct3D9.
float gl_HalfPixel[4];
Expand Down
5 changes: 4 additions & 1 deletion GPU/Common/ShaderTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ cbuffer data : register(b0) {
float2 u_texelDelta;
float2 u_pixelDelta;
float4 u_time;
float4 u_setting;
float u_video;
};
)";
Expand All @@ -101,6 +102,7 @@ layout (std140, set = 0, binding = 0) uniform Data {
vec2 u_texelDelta;
vec2 u_pixelDelta;
vec4 u_time;
vec4 u_setting;
float u_video;
};
)";
Expand All @@ -110,7 +112,8 @@ float4 gl_HalfPixel : register(c0);
float2 u_texelDelta : register(c1);
float2 u_pixelDelta : register(c2);
float4 u_time : register(c3);
float u_video : register(c4);
float4 u_setting : register(c4);
float u_video : register(c5);
)";

// SPIRV-Cross' HLSL output has some deficiencies we need to work around.
Expand Down
23 changes: 21 additions & 2 deletions UI/GameSettingsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,24 @@ void GameSettingsScreen::CreateViews() {
return g_Config.iRenderingMode != FB_NON_BUFFERED_MODE;
});

const ShaderInfo *shaderInfo = GetPostShaderInfo(g_Config.sPostShaderName);
if (shaderInfo && !shaderInfo->settingName1.empty()) {
auto &value = g_Config.mPostShaderSetting[g_Config.sPostShaderName + "SettingValue1"];
graphicsSettings->Add(new PopupSliderChoiceFloat(&value, shaderInfo->minSettingValue1, shaderInfo->maxSettingValue1, shaderInfo->settingName1, shaderInfo->settingStep1, screenManager()));
}
if (shaderInfo && !shaderInfo->settingName2.empty()) {
auto &value = g_Config.mPostShaderSetting[g_Config.sPostShaderName + "SettingValue2"];
graphicsSettings->Add(new PopupSliderChoiceFloat(&value, shaderInfo->minSettingValue2, shaderInfo->maxSettingValue2, shaderInfo->settingName2, shaderInfo->settingStep2, screenManager()));
}
if (shaderInfo && !shaderInfo->settingName3.empty()) {
auto &value = g_Config.mPostShaderSetting[g_Config.sPostShaderName + "SettingValue3"];
graphicsSettings->Add(new PopupSliderChoiceFloat(&value, shaderInfo->minSettingValue3, shaderInfo->maxSettingValue3, shaderInfo->settingName3, shaderInfo->settingStep3, screenManager()));
}
if (shaderInfo && !shaderInfo->settingName4.empty()) {
auto &value = g_Config.mPostShaderSetting[g_Config.sPostShaderName + "SettingValue4"];
graphicsSettings->Add(new PopupSliderChoiceFloat(&value, shaderInfo->minSettingValue4, shaderInfo->maxSettingValue4, shaderInfo->settingName4, shaderInfo->settingStep4, screenManager()));
}

#if !defined(MOBILE_DEVICE)
graphicsSettings->Add(new CheckBox(&g_Config.bFullScreen, gr->T("FullScreen", "Full Screen")))->OnClick.Handle(this, &GameSettingsScreen::OnFullscreenChange);
if (System_GetPropertyInt(SYSPROP_DISPLAY_COUNT) > 1) {
Expand Down Expand Up @@ -1413,12 +1431,13 @@ UI::EventReturn GameSettingsScreen::OnPostProcShader(UI::EventParams &e) {

UI::EventReturn GameSettingsScreen::OnPostProcShaderChange(UI::EventParams &e) {
NativeMessageReceived("gpu_resized", "");
RecreateViews(); // Update setting name
return UI::EVENT_DONE;
}

UI::EventReturn GameSettingsScreen::OnDeveloperTools(UI::EventParams &e) {
screenManager()->push(new DeveloperToolsScreen());
return UI::EVENT_DONE;
screenManager()->push(new DeveloperToolsScreen());
return UI::EVENT_DONE;
}

UI::EventReturn GameSettingsScreen::OnRemoteISO(UI::EventParams &e) {
Expand Down
11 changes: 5 additions & 6 deletions assets/shaders/bloom.fsh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ precision mediump int;
uniform sampler2D sampler0;
varying vec2 v_texcoord0;

float amount = 0.60; // suitable range = 0.00 - 1.00
float power = 0.5; // suitable range = 0.0 - 1.0
uniform vec4 u_setting;

void main()
{
Expand All @@ -21,9 +20,9 @@ void main()

for(int i= -3 ;i < 3; i++)
{
sum += texture2D(sampler0, v_texcoord0 + vec2(-1, i)*0.004) * amount;
sum += texture2D(sampler0, v_texcoord0 + vec2(0, i)*0.004) * amount;
sum += texture2D(sampler0, v_texcoord0 + vec2(1, i)*0.004) * amount;
sum += texture2D(sampler0, v_texcoord0 + vec2(-1, i)*0.004) * u_setting.x;
sum += texture2D(sampler0, v_texcoord0 + vec2(0, i)*0.004) * u_setting.x;
sum += texture2D(sampler0, v_texcoord0 + vec2(1, i)*0.004) * u_setting.x;
}

if (color.r < 0.3 && color.g < 0.3 && color.b < 0.3)
Expand All @@ -42,7 +41,7 @@ void main()
}
}

bloom = mix(color, bloom, power);
bloom = mix(color, bloom, u_setting.y);
gl_FragColor.rgb = bloom;
gl_FragColor.a = 1.0;
}
4 changes: 2 additions & 2 deletions assets/shaders/cartoon.fsh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ precision mediump float;
precision mediump int;
#endif

const float bb = 0.5; // effects black border sensitivity; from 0.0 to 1.0
uniform vec4 u_setting;

uniform sampler2D sampler0;

Expand Down Expand Up @@ -39,7 +39,7 @@ void main()
float d2=dot(abs(c20-c02),dt);
float hl=dot(abs(c01-c21),dt);
float vl=dot(abs(c10-c12),dt);
float d = bb*(d1+d2+hl+vl)/(dot(c11,dt)+0.15);
float d = u_setting.x*(d1+d2+hl+vl)/(dot(c11,dt)+0.15);

float lc = 4.0*length(c11);
float f = fract(lc); f*=f;
Expand Down
20 changes: 20 additions & 0 deletions assets/shaders/colorcorrection.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Color correction

#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

uniform sampler2D sampler0;
varying vec2 v_texcoord0;

uniform vec4 u_setting;

void main()
{
vec3 rgb = texture2D( sampler0, v_texcoord0 ).xyz;
rgb = vec3(mix(vec3(dot(rgb, vec3(0.299, 0.587, 0.114))), rgb, u_setting.y));
rgb = (rgb-0.5)*u_setting.z+0.5+u_setting.x-1.0;
gl_FragColor.rgb = pow(rgb, vec3(1.0/u_setting.w));
gl_FragColor.a = 1.0;
}
Loading

0 comments on commit 67ab51b

Please sign in to comment.