Add the VideoCommon PostProcessing class.

This class loads all the common PP shader configuration options and passes those options through to a inherited class that OpenGL or D3D will have.
Makes it so all the common code for PP shaders is in VideoCommon instead of duplicating the code across each backend.
This commit is contained in:
Ryan Houdek
2014-07-29 11:47:56 -05:00
parent 3a657fe33c
commit 6bdc32c54a
13 changed files with 555 additions and 27 deletions

View File

@ -18,6 +18,7 @@ set(SRCS BPFunctions.cpp
PixelEngine.cpp
PixelShaderGen.cpp
PixelShaderManager.cpp
PostProcessing.cpp
RenderBase.cpp
Statistics.cpp
TextureCacheBase.cpp

View File

@ -0,0 +1,304 @@
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#include <string>
#include "Common/CommonPaths.h"
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Common/StringUtil.h"
#include "VideoCommon/PostProcessing.h"
#include "VideoCommon/VideoConfig.h"
PostProcessingShaderImplementation::PostProcessingShaderImplementation()
{
m_timer.Start();
}
PostProcessingShaderImplementation::~PostProcessingShaderImplementation()
{
m_timer.Stop();
}
std::string PostProcessingShaderConfiguration::LoadShader(std::string shader)
{
// Load the shader from the configuration if there isn't one sent to us.
if (shader == "")
shader = g_ActiveConfig.sPostProcessingShader;
m_current_shader = shader;
// loading shader code
std::string code;
std::string path = File::GetUserPath(D_SHADERS_IDX) + shader + ".glsl";
if (!File::Exists(path))
{
// Fallback to shared user dir
path = File::GetSysDirectory() + SHADERS_DIR DIR_SEP + shader + ".glsl";
}
if (!File::ReadFileToString(path, code))
{
ERROR_LOG(VIDEO, "Post-processing shader not found: %s", path.c_str());
return "";
}
LoadOptions(code);
LoadOptionsConfiguration();
return code;
}
void PostProcessingShaderConfiguration::LoadOptions(const std::string& code)
{
const std::string config_start_delimiter = "[configuration]";
const std::string config_end_delimiter = "[/configuration]";
size_t configuration_start = code.find(config_start_delimiter);
size_t configuration_end = code.find(config_end_delimiter);
m_options.clear();
m_any_options_dirty = true;
if (configuration_start == std::string::npos ||
configuration_end == std::string::npos)
{
// Issue loading configuration or there isn't one.
return;
}
std::string configuration_string = code.substr(configuration_start + config_start_delimiter.size(),
configuration_end - configuration_start - config_start_delimiter.size());
std::istringstream in(configuration_string);
struct GLSLStringOption
{
std::string m_type;
std::vector<std::pair<std::string, std::string>> m_options;
};
std::vector<GLSLStringOption> option_strings;
GLSLStringOption* current_strings = nullptr;
while (!in.eof())
{
std::string line;
if (std::getline(in, line))
{
#ifndef _WIN32
// Check for CRLF eol and convert it to LF
if (!line.empty() && line.at(line.size()-1) == '\r')
{
line.erase(line.size()-1);
}
#endif
if (line.size() > 0)
{
if (line[0] == '[')
{
size_t endpos = line.find("]");
if (endpos != std::string::npos)
{
// New section!
std::string sub = line.substr(1, endpos - 1);
option_strings.push_back({ sub });
current_strings = &option_strings.back();
}
}
else
{
if (current_strings)
{
std::string key, value;
IniFile::ParseLine(line, &key, &value);
if (!(key == "" && value == ""))
current_strings->m_options.push_back(std::make_pair(key, value));
}
}
}
}
}
for (const auto& it : option_strings)
{
ConfigurationOption option;
option.m_dirty = true;
if (it.m_type == "OptionBool")
option.m_type = ConfigurationOption::OptionType::OPTION_BOOL;
else if (it.m_type == "OptionRangeFloat")
option.m_type = ConfigurationOption::OptionType::OPTION_FLOAT;
else if (it.m_type == "OptionRangeInteger")
option.m_type = ConfigurationOption::OptionType::OPTION_INTEGER;
for (const auto& string_option : it.m_options)
{
if (string_option.first == "GUIName")
{
option.m_gui_name = string_option.second;
}
else if (string_option.first == "OptionName")
{
option.m_option_name = string_option.second;
}
else if (string_option.first == "DependentOption")
{
option.m_dependent_option = string_option.second;
}
else if (string_option.first == "MinValue" ||
string_option.first == "MaxValue" ||
string_option.first == "DefaultValue" ||
string_option.first == "StepAmount")
{
std::vector<s32>* output_integer = nullptr;
std::vector<float>* output_float = nullptr;
if (string_option.first == "MinValue")
{
output_integer = &option.m_integer_min_values;
output_float = &option.m_float_min_values;
}
else if (string_option.first == "MaxValue")
{
output_integer = &option.m_integer_max_values;
output_float = &option.m_float_max_values;
}
else if (string_option.first == "DefaultValue")
{
output_integer = &option.m_integer_values;
output_float = &option.m_float_values;
}
else if (string_option.first == "StepAmount")
{
output_integer = &option.m_integer_step_values;
output_float = &option.m_float_step_values;
}
if (option.m_type == ConfigurationOption::OptionType::OPTION_BOOL)
{
TryParse(string_option.second, &option.m_bool_value);
}
else if (option.m_type == ConfigurationOption::OptionType::OPTION_INTEGER)
{
TryParseVector(string_option.second, output_integer);
if (output_integer->size() > 4)
output_integer->erase(output_integer->begin() + 4, output_integer->end());
}
else if (option.m_type == ConfigurationOption::OptionType::OPTION_FLOAT)
{
TryParseVector(string_option.second, output_float);
if (output_float->size() > 4)
output_float->erase(output_float->begin() + 4, output_float->end());
}
}
}
m_options[option.m_option_name] = option;
}
}
void PostProcessingShaderConfiguration::LoadOptionsConfiguration()
{
IniFile ini;
ini.Load(File::GetUserPath(F_DOLPHINCONFIG_IDX));
std::string section = m_current_shader + "-options";
for (auto& it : m_options)
{
switch (it.second.m_type)
{
case ConfigurationOption::OptionType::OPTION_BOOL:
ini.GetOrCreateSection(section)->Get(it.second.m_option_name, &it.second.m_bool_value, it.second.m_bool_value);
break;
case ConfigurationOption::OptionType::OPTION_INTEGER:
{
std::string value;
ini.GetOrCreateSection(section)->Get(it.second.m_option_name, &value);
if (value != "")
TryParseVector(value, &it.second.m_integer_values);
}
break;
case ConfigurationOption::OptionType::OPTION_FLOAT:
{
std::string value;
ini.GetOrCreateSection(section)->Get(it.second.m_option_name, &value);
if (value != "")
TryParseVector(value, &it.second.m_float_values);
}
break;
}
}
}
void PostProcessingShaderConfiguration::SaveOptionsConfiguration()
{
IniFile ini;
ini.Load(File::GetUserPath(F_DOLPHINCONFIG_IDX));
std::string section = m_current_shader + "-options";
for (auto& it : m_options)
{
switch (it.second.m_type)
{
case ConfigurationOption::OptionType::OPTION_BOOL:
{
ini.GetOrCreateSection(section)->Set(it.second.m_option_name, it.second.m_bool_value);
}
break;
case ConfigurationOption::OptionType::OPTION_INTEGER:
{
std::string value = "";
for (size_t i = 0; i < it.second.m_integer_values.size(); ++i)
value += StringFromFormat("%d%s", it.second.m_integer_values[i], i == (it.second.m_integer_values.size() - 1) ? "": ", ");
ini.GetOrCreateSection(section)->Set(it.second.m_option_name, value);
}
break;
case ConfigurationOption::OptionType::OPTION_FLOAT:
{
std::string value = "";
for (size_t i = 0; i < it.second.m_float_values.size(); ++i)
value += StringFromFormat("%f%s", it.second.m_float_values[i], i == (it.second.m_float_values.size() - 1) ? "": ", ");
ini.GetOrCreateSection(section)->Set(it.second.m_option_name, value);
}
break;
}
}
ini.Save(File::GetUserPath(F_DOLPHINCONFIG_IDX));
}
void PostProcessingShaderConfiguration::ReloadShader()
{
m_current_shader = "";
}
void PostProcessingShaderConfiguration::SetOptionf(std::string option, int index, float value)
{
auto it = m_options.find(option);
it->second.m_float_values[index] = value;
it->second.m_dirty = true;
m_any_options_dirty = true;
}
void PostProcessingShaderConfiguration::SetOptioni(std::string option, int index, s32 value)
{
auto it = m_options.find(option);
it->second.m_integer_values[index] = value;
it->second.m_dirty = true;
m_any_options_dirty = true;
}
void PostProcessingShaderConfiguration::SetOptionb(std::string option, bool value)
{
auto it = m_options.find(option);
it->second.m_bool_value = value;
it->second.m_dirty = true;
m_any_options_dirty = true;
}

View File

@ -0,0 +1,104 @@
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#pragma once
#include <map>
#include <string>
#include <vector>
#include "Common/StringUtil.h"
#include "Common/Timer.h"
class PostProcessingShaderConfiguration
{
public:
struct ConfigurationOption
{
enum OptionType
{
OPTION_BOOL = 0,
OPTION_FLOAT,
OPTION_INTEGER,
};
bool m_bool_value;
std::vector<float> m_float_values;
std::vector<s32> m_integer_values;
std::vector<float> m_float_min_values;
std::vector<s32> m_integer_min_values;
std::vector<float> m_float_max_values;
std::vector<s32> m_integer_max_values;
std::vector<float> m_float_step_values;
std::vector<s32> m_integer_step_values;
OptionType m_type;
std::string m_gui_name;
std::string m_option_name;
std::string m_dependent_option;
bool m_dirty;
};
typedef std::map<std::string, ConfigurationOption> ConfigMap;
PostProcessingShaderConfiguration() : m_current_shader("") {}
virtual ~PostProcessingShaderConfiguration() {}
// Loads the configuration with a shader
// If the argument is "" the class will load the shader from the g_activeConfig option.
// Returns the loaded shader source from file
std::string LoadShader(std::string shader = "");
void SaveOptionsConfiguration();
void ReloadShader();
std::string GetShader() { return m_current_shader; }
bool IsDirty() { return m_any_options_dirty; }
void SetDirty(bool dirty) { m_any_options_dirty = dirty; }
bool HasOptions() { return m_options.size() > 0; }
ConfigMap& GetOptions() { return m_options; }
const ConfigurationOption& GetOption(const std::string& option) { return m_options[option]; }
// For updating option's values
void SetOptionf(std::string option, int index, float value);
void SetOptioni(std::string option, int index, s32 value);
void SetOptionb(std::string option, bool value);
private:
bool m_any_options_dirty;
std::string m_current_shader;
ConfigMap m_options;
void LoadOptions(const std::string& code);
void LoadOptionsConfiguration();
};
class PostProcessingShaderImplementation
{
public:
PostProcessingShaderImplementation();
virtual ~PostProcessingShaderImplementation();
PostProcessingShaderConfiguration* GetConfig() { return &m_config; }
// Should be implemented by the backends for backend specific code
virtual void BindTargetFramebuffer() = 0;
virtual void BlitToScreen() = 0;
virtual void Update(u32 width, u32 height) = 0;
virtual void ApplyShader() = 0;
protected:
bool m_enable;
u32 m_width;
u32 m_height;
// Timer for determining our time value
Common::Timer m_timer;
PostProcessingShaderConfiguration m_config;
};

View File

@ -59,6 +59,8 @@ int Renderer::s_target_height;
int Renderer::s_backbuffer_width;
int Renderer::s_backbuffer_height;
PostProcessingShaderImplementation* Renderer::m_post_processor;
TargetRectangle Renderer::target_rc;
int Renderer::s_LastEFBScale;

View File

@ -115,6 +115,8 @@ public:
static PEControl::PixelFormat GetPrevPixelFormat() { return prev_efb_format; }
static void StorePixelFormat(PEControl::PixelFormat new_format) { prev_efb_format = new_format; }
PostProcessingShaderImplementation* GetPostProcessor() { return m_post_processor; }
protected:
static void CalculateTargetScale(int x, int y, int &scaledX, int &scaledY);
@ -153,6 +155,8 @@ protected:
FPSCounter m_fps_counter;
static PostProcessingShaderImplementation* m_post_processor;
private:
static PEControl::PixelFormat prev_efb_format;
static unsigned int efb_scale_numeratorX;

View File

@ -56,6 +56,7 @@
<ClCompile Include="PixelEngine.cpp" />
<ClCompile Include="PixelShaderGen.cpp" />
<ClCompile Include="PixelShaderManager.cpp" />
<ClCompile Include="PostProcessing.cpp" />
<ClCompile Include="RenderBase.cpp" />
<ClCompile Include="Statistics.cpp" />
<ClCompile Include="stdafx.cpp">
@ -105,6 +106,7 @@
<ClInclude Include="PixelEngine.h" />
<ClInclude Include="PixelShaderGen.h" />
<ClInclude Include="PixelShaderManager.h" />
<ClInclude Include="PostProcessing.h" />
<ClInclude Include="RenderBase.h" />
<ClInclude Include="ShaderGenCommon.h" />
<ClInclude Include="Statistics.h" />
@ -148,4 +150,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -107,6 +107,9 @@
<ClCompile Include="OnScreenDisplay.cpp">
<Filter>Util</Filter>
</ClCompile>
<ClCompile Include="PostProcessing.cpp">
<Filter>Util</Filter>
</ClCompile>
<ClCompile Include="Statistics.cpp">
<Filter>Util</Filter>
</ClCompile>
@ -234,6 +237,9 @@
<ClInclude Include="OnScreenDisplay.h">
<Filter>Util</Filter>
</ClInclude>
<ClInclude Include="PostProcessing.h">
<Filter>Util</Filter>
</ClInclude>
<ClInclude Include="Statistics.h">
<Filter>Util</Filter>
</ClInclude>
@ -266,4 +272,4 @@
<ItemGroup>
<Text Include="CMakeLists.txt" />
</ItemGroup>
</Project>
</Project>