InputCommon: reduce number of image loads and texture cache invalidations by only running dynamic input textures once for all controllers

This commit is contained in:
iwubcode 2021-02-27 16:41:50 -06:00
parent 158674c274
commit 32d584a0f5
10 changed files with 117 additions and 123 deletions

View File

@ -112,12 +112,6 @@ void EmulatedController::SetDefaultDevice(ciface::Core::DeviceQualifier devq)
} }
} }
void EmulatedController::SetDynamicInputTextureManager(
InputCommon::DynamicInputTextureManager* dynamic_input_tex_config_manager)
{
m_dynamic_input_tex_config_manager = dynamic_input_tex_config_manager;
}
void EmulatedController::LoadConfig(IniFile::Section* sec, const std::string& base) void EmulatedController::LoadConfig(IniFile::Section* sec, const std::string& base)
{ {
std::string defdev = GetDefaultDevice().ToString(); std::string defdev = GetDefaultDevice().ToString();
@ -129,11 +123,6 @@ void EmulatedController::LoadConfig(IniFile::Section* sec, const std::string& ba
for (auto& cg : groups) for (auto& cg : groups)
cg->LoadConfig(sec, defdev, base); cg->LoadConfig(sec, defdev, base);
if (base.empty())
{
GenerateTextures(sec);
}
} }
void EmulatedController::SaveConfig(IniFile::Section* sec, const std::string& base) void EmulatedController::SaveConfig(IniFile::Section* sec, const std::string& base)
@ -144,11 +133,6 @@ void EmulatedController::SaveConfig(IniFile::Section* sec, const std::string& ba
for (auto& ctrlGroup : groups) for (auto& ctrlGroup : groups)
ctrlGroup->SaveConfig(sec, defdev, base); ctrlGroup->SaveConfig(sec, defdev, base);
if (base.empty())
{
GenerateTextures(sec);
}
} }
void EmulatedController::LoadDefaults(const ControllerInterface& ciface) void EmulatedController::LoadDefaults(const ControllerInterface& ciface)
@ -163,12 +147,4 @@ void EmulatedController::LoadDefaults(const ControllerInterface& ciface)
SetDefaultDevice(default_device_string); SetDefaultDevice(default_device_string);
} }
} }
void EmulatedController::GenerateTextures(IniFile::Section* sec)
{
if (m_dynamic_input_tex_config_manager)
{
m_dynamic_input_tex_config_manager->GenerateTextures(sec, GetName());
}
}
} // namespace ControllerEmu } // namespace ControllerEmu

View File

@ -17,7 +17,6 @@
#include "Common/MathUtil.h" #include "Common/MathUtil.h"
#include "InputCommon/ControlReference/ExpressionParser.h" #include "InputCommon/ControlReference/ExpressionParser.h"
#include "InputCommon/ControllerInterface/CoreDevice.h" #include "InputCommon/ControllerInterface/CoreDevice.h"
#include "InputCommon/DynamicInputTextureManager.h"
class ControllerInterface; class ControllerInterface;
@ -183,7 +182,6 @@ public:
const ciface::Core::DeviceQualifier& GetDefaultDevice() const; const ciface::Core::DeviceQualifier& GetDefaultDevice() const;
void SetDefaultDevice(const std::string& device); void SetDefaultDevice(const std::string& device);
void SetDefaultDevice(ciface::Core::DeviceQualifier devq); void SetDefaultDevice(ciface::Core::DeviceQualifier devq);
void SetDynamicInputTextureManager(InputCommon::DynamicInputTextureManager*);
void UpdateReferences(const ControllerInterface& devi); void UpdateReferences(const ControllerInterface& devi);
void UpdateSingleControlReference(const ControllerInterface& devi, ControlReference* ref); void UpdateSingleControlReference(const ControllerInterface& devi, ControlReference* ref);
@ -226,8 +224,6 @@ protected:
void UpdateReferences(ciface::ExpressionParser::ControlEnvironment& env); void UpdateReferences(ciface::ExpressionParser::ControlEnvironment& env);
private: private:
void GenerateTextures(IniFile::Section* sec);
InputCommon::DynamicInputTextureManager* m_dynamic_input_tex_config_manager = nullptr;
ciface::Core::DeviceQualifier m_default_device; ciface::Core::DeviceQualifier m_default_device;
bool m_default_device_is_connected{false}; bool m_default_device_is_connected{false};
}; };

View File

@ -40,13 +40,13 @@ void DynamicInputTextureManager::Load()
} }
} }
void DynamicInputTextureManager::GenerateTextures(const IniFile::Section* sec, void DynamicInputTextureManager::GenerateTextures(const IniFile& file,
const std::string& controller_name) const std::vector<std::string>& controller_names)
{ {
bool any_dirty = false; bool any_dirty = false;
for (const auto& configuration : m_configuration) for (const auto& configuration : m_configuration)
{ {
any_dirty |= configuration.GenerateTextures(sec, controller_name); any_dirty |= configuration.GenerateTextures(file, controller_names);
} }
if (any_dirty && g_renderer && Core::GetState() != Core::State::Starting) if (any_dirty && g_renderer && Core::GetState() != Core::State::Starting)

View File

@ -21,7 +21,7 @@ public:
DynamicInputTextureManager(); DynamicInputTextureManager();
~DynamicInputTextureManager(); ~DynamicInputTextureManager();
void Load(); void Load();
void GenerateTextures(const IniFile::Section* sec, const std::string& controller_name); void GenerateTextures(const IniFile& file, const std::vector<std::string>& controller_names);
private: private:
std::vector<DynamicInputTextures::Configuration> m_configuration; std::vector<DynamicInputTextures::Configuration> m_configuration;

View File

@ -87,48 +87,22 @@ Configuration::Configuration(const std::string& json_file)
Configuration::~Configuration() = default; Configuration::~Configuration() = default;
bool Configuration::GenerateTextures(const IniFile::Section* sec, bool Configuration::GenerateTextures(const IniFile& file,
const std::string& controller_name) const const std::vector<std::string>& controller_names) const
{ {
bool any_dirty = false; bool any_dirty = false;
for (const auto& texture_data : m_dynamic_input_textures) for (const auto& texture_data : m_dynamic_input_textures)
{ {
any_dirty |= GenerateTexture(sec, controller_name, texture_data); any_dirty |= GenerateTexture(file, controller_names, texture_data);
} }
return any_dirty; return any_dirty;
} }
bool Configuration::GenerateTexture(const IniFile::Section* sec, const std::string& controller_name, bool Configuration::GenerateTexture(const IniFile& file,
const std::vector<std::string>& controller_names,
const Data& texture_data) const const Data& texture_data) const
{ {
std::string device_name;
if (!sec->Get("Device", &device_name))
{
return false;
}
auto emulated_controls_iter = texture_data.m_emulated_controllers.find(controller_name);
if (emulated_controls_iter == texture_data.m_emulated_controllers.end())
{
return false;
}
bool device_found = true;
auto host_devices_iter = texture_data.m_host_devices.find(device_name);
if (host_devices_iter == texture_data.m_host_devices.end())
{
// If we fail to find our exact device,
// it's possible the creator doesn't care (single player game)
// and has used a wildcard for any device
host_devices_iter = texture_data.m_host_devices.find("");
if (host_devices_iter == texture_data.m_host_devices.end())
{
device_found = false;
}
}
// Two copies of the loaded texture // Two copies of the loaded texture
// The first one is used as a fallback if a key or device isn't mapped // The first one is used as a fallback if a key or device isn't mapped
// the second one is used as the final image to write to the textures directory // the second one is used as the final image to write to the textures directory
@ -136,61 +110,88 @@ bool Configuration::GenerateTexture(const IniFile::Section* sec, const std::stri
auto image_to_write = original_image; auto image_to_write = original_image;
bool dirty = false; bool dirty = false;
for (auto& [emulated_key, rects] : emulated_controls_iter->second)
{
// TODO: Remove this line when we move to C++20
auto& rects_ref = rects;
auto apply_original = [&] {
for (const auto& rect : rects_ref)
{
CopyImageRegion(*original_image, *image_to_write, rect, rect);
dirty = true;
}
};
if (!device_found) for (const auto& controller_name : controller_names)
{
auto* sec = file.GetSection(controller_name);
if (!sec)
{ {
// If we get here, that means the controller is set to a
// device not exposed to the pack
// We still apply the original image, in case the user
// switched devices and wants to see the changes
apply_original();
continue; continue;
} }
std::string host_key; std::string device_name;
sec->Get(emulated_key, &host_key); if (!sec->Get("Device", &device_name))
const auto input_image_iter = host_devices_iter->second.find(host_key);
if (input_image_iter == host_devices_iter->second.end())
{ {
apply_original(); continue;
} }
else
auto emulated_controls_iter = texture_data.m_emulated_controllers.find(controller_name);
if (emulated_controls_iter == texture_data.m_emulated_controllers.end())
{ {
const auto host_key_image = LoadImage(m_base_path + input_image_iter->second); continue;
}
for (const auto& rect : rects) bool device_found = true;
auto host_devices_iter = texture_data.m_host_devices.find(device_name);
if (host_devices_iter == texture_data.m_host_devices.end())
{
// If we fail to find our exact device,
// it's possible the creator doesn't care (single player game)
// and has used a wildcard for any device
host_devices_iter = texture_data.m_host_devices.find("");
if (host_devices_iter == texture_data.m_host_devices.end())
{ {
InputCommon::ImagePixelData pixel_data; device_found = false;
if (host_key_image->width == rect.GetWidth() && host_key_image->height == rect.GetHeight()) }
{ }
pixel_data = *host_key_image;
}
else if (texture_data.m_preserve_aspect_ratio)
{
pixel_data = ResizeKeepAspectRatio(ResizeMode::Nearest, *host_key_image, rect.GetWidth(),
rect.GetHeight(), Pixel{0, 0, 0, 0});
}
else
{
pixel_data =
Resize(ResizeMode::Nearest, *host_key_image, rect.GetWidth(), rect.GetHeight());
}
CopyImageRegion(pixel_data, *image_to_write, Rect{0, 0, rect.GetWidth(), rect.GetHeight()}, for (auto& [emulated_key, rects] : emulated_controls_iter->second)
rect); {
if (!device_found)
{
// If we get here, that means the controller is set to a
// device not exposed to the pack
dirty = true; dirty = true;
continue;
}
std::string host_key;
sec->Get(emulated_key, &host_key);
const auto input_image_iter = host_devices_iter->second.find(host_key);
if (input_image_iter == host_devices_iter->second.end())
{
dirty = true;
}
else
{
const auto host_key_image = LoadImage(m_base_path + input_image_iter->second);
for (const auto& rect : rects)
{
InputCommon::ImagePixelData pixel_data;
if (host_key_image->width == rect.GetWidth() &&
host_key_image->height == rect.GetHeight())
{
pixel_data = *host_key_image;
}
else if (texture_data.m_preserve_aspect_ratio)
{
pixel_data =
ResizeKeepAspectRatio(ResizeMode::Nearest, *host_key_image, rect.GetWidth(),
rect.GetHeight(), Pixel{0, 0, 0, 0});
}
else
{
pixel_data =
Resize(ResizeMode::Nearest, *host_key_image, rect.GetWidth(), rect.GetHeight());
}
CopyImageRegion(pixel_data, *image_to_write,
Rect{0, 0, rect.GetWidth(), rect.GetHeight()}, rect);
dirty = true;
}
} }
} }
} }

View File

@ -19,10 +19,11 @@ class Configuration
public: public:
explicit Configuration(const std::string& json_file); explicit Configuration(const std::string& json_file);
~Configuration(); ~Configuration();
bool GenerateTextures(const IniFile::Section* sec, const std::string& controller_name) const; bool GenerateTextures(const IniFile& file,
const std::vector<std::string>& controller_names) const;
private: private:
bool GenerateTexture(const IniFile::Section* sec, const std::string& controller_name, bool GenerateTexture(const IniFile& file, const std::vector<std::string>& controller_names,
const Data& texture_data) const; const Data& texture_data) const;
std::vector<Data> m_dynamic_input_textures; std::vector<Data> m_dynamic_input_textures;

View File

@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include "Common/Config/Config.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/IniFile.h" #include "Common/IniFile.h"
#include "Common/MsgHandler.h" #include "Common/MsgHandler.h"
@ -101,6 +102,8 @@ bool InputConfig::LoadConfig(bool isGC)
!inifile.GetSections().empty()) !inifile.GetSections().empty())
{ {
int n = 0; int n = 0;
std::vector<std::string> controller_names;
for (auto& controller : m_controllers) for (auto& controller : m_controllers)
{ {
IniFile::Section config; IniFile::Section config;
@ -113,9 +116,8 @@ bool InputConfig::LoadConfig(bool isGC)
controller->GetName() + "'", controller->GetName() + "'",
6000); 6000);
IniFile profile_ini; inifile.Load(profile[n]);
profile_ini.Load(profile[n]); config = *inifile.GetOrCreateSection("Profile");
config = *profile_ini.GetOrCreateSection("Profile");
} }
else else
{ {
@ -133,10 +135,13 @@ bool InputConfig::LoadConfig(bool isGC)
controller->LoadConfig(&config); controller->LoadConfig(&config);
// Update refs // Update refs
controller->UpdateReferences(g_controller_interface); controller->UpdateReferences(g_controller_interface);
controller_names.push_back(controller->GetName());
// Next profile // Next profile
n++; n++;
} }
m_dynamic_input_tex_config_manager.GenerateTextures(inifile, controller_names);
return true; return true;
} }
else else
@ -154,8 +159,14 @@ void InputConfig::SaveConfig()
IniFile inifile; IniFile inifile;
inifile.Load(ini_filename); inifile.Load(ini_filename);
std::vector<std::string> controller_names;
for (auto& controller : m_controllers) for (auto& controller : m_controllers)
{
controller->SaveConfig(inifile.GetOrCreateSection(controller->GetName())); controller->SaveConfig(inifile.GetOrCreateSection(controller->GetName()));
controller_names.push_back(controller->GetName());
}
m_dynamic_input_tex_config_manager.GenerateTextures(inifile, controller_names);
inifile.Save(ini_filename); inifile.Save(ini_filename);
} }
@ -195,11 +206,6 @@ void InputConfig::UnregisterHotplugCallback()
g_controller_interface.UnregisterDevicesChangedCallback(m_hotplug_callback_handle); g_controller_interface.UnregisterDevicesChangedCallback(m_hotplug_callback_handle);
} }
void InputConfig::OnControllerCreated(ControllerEmu::EmulatedController& controller)
{
controller.SetDynamicInputTextureManager(&m_dynamic_input_tex_config_manager);
}
bool InputConfig::IsControllerControlledByGamepadDevice(int index) const bool InputConfig::IsControllerControlledByGamepadDevice(int index) const
{ {
if (static_cast<size_t>(index) >= m_controllers.size()) if (static_cast<size_t>(index) >= m_controllers.size())
@ -215,3 +221,14 @@ bool InputConfig::IsControllerControlledByGamepadDevice(int index) const
|| (controller.source == "DInput" && || (controller.source == "DInput" &&
controller.name == "Keyboard Mouse")); // Windows Keyboard/Mouse controller.name == "Keyboard Mouse")); // Windows Keyboard/Mouse
} }
void InputConfig::GenerateControllerTextures(const IniFile& file)
{
std::vector<std::string> controller_names;
for (auto& controller : m_controllers)
{
controller_names.push_back(controller->GetName());
}
m_dynamic_input_tex_config_manager.GenerateTextures(file, controller_names);
}

View File

@ -31,8 +31,7 @@ public:
template <typename T, typename... Args> template <typename T, typename... Args>
void CreateController(Args&&... args) void CreateController(Args&&... args)
{ {
OnControllerCreated( m_controllers.emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
*m_controllers.emplace_back(std::make_unique<T>(std::forward<Args>(args)...)));
} }
ControllerEmu::EmulatedController* GetController(int index); ControllerEmu::EmulatedController* GetController(int index);
@ -48,8 +47,9 @@ public:
void RegisterHotplugCallback(); void RegisterHotplugCallback();
void UnregisterHotplugCallback(); void UnregisterHotplugCallback();
void GenerateControllerTextures(const IniFile& file);
private: private:
void OnControllerCreated(ControllerEmu::EmulatedController& controller);
ControllerInterface::HotplugCallbackHandle m_hotplug_callback_handle; ControllerInterface::HotplugCallbackHandle m_hotplug_callback_handle;
std::vector<std::unique_ptr<ControllerEmu::EmulatedController>> m_controllers; std::vector<std::unique_ptr<ControllerEmu::EmulatedController>> m_controllers;
const std::string m_ini_name; const std::string m_ini_name;

View File

@ -73,7 +73,8 @@ std::string ProfileCycler::GetProfile(CycleDirection cycle_direction, int& profi
} }
void ProfileCycler::UpdateToProfile(const std::string& profile_filename, void ProfileCycler::UpdateToProfile(const std::string& profile_filename,
ControllerEmu::EmulatedController* controller) ControllerEmu::EmulatedController* controller,
InputConfig* device_configuration)
{ {
std::string base; std::string base;
SplitPath(profile_filename, nullptr, &base, nullptr); SplitPath(profile_filename, nullptr, &base, nullptr);
@ -86,6 +87,7 @@ void ProfileCycler::UpdateToProfile(const std::string& profile_filename,
display_message_ms); display_message_ms);
controller->LoadConfig(ini_file.GetOrCreateSection("Profile")); controller->LoadConfig(ini_file.GetOrCreateSection("Profile"));
controller->UpdateReferences(g_controller_interface); controller->UpdateReferences(g_controller_interface);
device_configuration->GenerateControllerTextures(ini_file);
} }
else else
{ {
@ -129,7 +131,7 @@ void ProfileCycler::CycleProfile(CycleDirection cycle_direction, InputConfig* de
auto* controller = device_configuration->GetController(controller_index); auto* controller = device_configuration->GetController(controller_index);
if (controller) if (controller)
{ {
UpdateToProfile(profile, controller); UpdateToProfile(profile, controller, device_configuration);
} }
else else
{ {
@ -168,7 +170,7 @@ void ProfileCycler::CycleProfileForGame(CycleDirection cycle_direction,
auto* controller = device_configuration->GetController(controller_index); auto* controller = device_configuration->GetController(controller_index);
if (controller) if (controller)
{ {
UpdateToProfile(profile, controller); UpdateToProfile(profile, controller, device_configuration);
} }
else else
{ {

View File

@ -45,7 +45,8 @@ private:
const std::vector<std::string>& profiles, const std::vector<std::string>& profiles,
InputConfig* device_configuration); InputConfig* device_configuration);
void UpdateToProfile(const std::string& profile_filename, void UpdateToProfile(const std::string& profile_filename,
ControllerEmu::EmulatedController* controller); ControllerEmu::EmulatedController* controller,
InputConfig* device_configuration);
std::string GetWiimoteInputProfilesForGame(int controller_index); std::string GetWiimoteInputProfilesForGame(int controller_index);
int m_wiimote_profile_index = 0; int m_wiimote_profile_index = 0;