Skip to content
Permalink
Browse files
Merge pull request #9539 from iwubcode/dynamic_input_tex_more_optimiz…
…ations

InputCommon: dynamic input textures more optimizations
  • Loading branch information
leoetlino committed Mar 2, 2021
2 parents 0c9f11a + 32d584a commit 59f4164
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 124 deletions.
@@ -154,6 +154,8 @@ class IniFile
void SortSections();

Section* GetOrCreateSection(std::string_view section_name);
const Section* GetSection(std::string_view section_name) const;
Section* GetSection(std::string_view section_name);

// This function is related to parsing data from lines of INI files
// It's used outside of IniFile, which is why it is exposed publicly
@@ -165,8 +167,5 @@ class IniFile
private:
std::list<Section> sections;

const Section* GetSection(std::string_view section_name) const;
Section* GetSection(std::string_view section_name);

static const std::string& NULL_STRING;
};
@@ -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)
{
std::string defdev = GetDefaultDevice().ToString();
@@ -129,11 +123,6 @@ void EmulatedController::LoadConfig(IniFile::Section* sec, const std::string& ba

for (auto& cg : groups)
cg->LoadConfig(sec, defdev, base);

if (base.empty())
{
GenerateTextures(sec);
}
}

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)
ctrlGroup->SaveConfig(sec, defdev, base);

if (base.empty())
{
GenerateTextures(sec);
}
}

void EmulatedController::LoadDefaults(const ControllerInterface& ciface)
@@ -163,12 +147,4 @@ void EmulatedController::LoadDefaults(const ControllerInterface& ciface)
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
@@ -17,7 +17,6 @@
#include "Common/MathUtil.h"
#include "InputCommon/ControlReference/ExpressionParser.h"
#include "InputCommon/ControllerInterface/CoreDevice.h"
#include "InputCommon/DynamicInputTextureManager.h"

class ControllerInterface;

@@ -183,7 +182,6 @@ class EmulatedController
const ciface::Core::DeviceQualifier& GetDefaultDevice() const;
void SetDefaultDevice(const std::string& device);
void SetDefaultDevice(ciface::Core::DeviceQualifier devq);
void SetDynamicInputTextureManager(InputCommon::DynamicInputTextureManager*);

void UpdateReferences(const ControllerInterface& devi);
void UpdateSingleControlReference(const ControllerInterface& devi, ControlReference* ref);
@@ -226,8 +224,6 @@ class EmulatedController
void UpdateReferences(ciface::ExpressionParser::ControlEnvironment& env);

private:
void GenerateTextures(IniFile::Section* sec);
InputCommon::DynamicInputTextureManager* m_dynamic_input_tex_config_manager = nullptr;
ciface::Core::DeviceQualifier m_default_device;
bool m_default_device_is_connected{false};
};
@@ -40,13 +40,13 @@ void DynamicInputTextureManager::Load()
}
}

void DynamicInputTextureManager::GenerateTextures(const IniFile::Section* sec,
const std::string& controller_name)
void DynamicInputTextureManager::GenerateTextures(const IniFile& file,
const std::vector<std::string>& controller_names)
{
bool any_dirty = false;
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)
@@ -21,7 +21,7 @@ class DynamicInputTextureManager
DynamicInputTextureManager();
~DynamicInputTextureManager();
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:
std::vector<DynamicInputTextures::Configuration> m_configuration;
@@ -87,110 +87,111 @@ Configuration::Configuration(const std::string& json_file)

Configuration::~Configuration() = default;

bool Configuration::GenerateTextures(const IniFile::Section* sec,
const std::string& controller_name) const
bool Configuration::GenerateTextures(const IniFile& file,
const std::vector<std::string>& controller_names) const
{
bool any_dirty = false;
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;
}

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
{
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
// 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
const auto original_image = LoadImage(m_base_path + texture_data.m_image_name);
auto image_to_write = original_image;

bool dirty = false;
for (auto& [emulated_key, rects] : emulated_controls_iter->second)

for (const auto& controller_name : controller_names)
{
// 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;
}
};
auto* sec = file.GetSection(controller_name);
if (!sec)
{
continue;
}

if (!device_found)
std::string device_name;
if (!sec->Get("Device", &device_name))
{
// 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;
}

std::string host_key;
sec->Get(emulated_key, &host_key);
auto emulated_controls_iter = texture_data.m_emulated_controllers.find(controller_name);
if (emulated_controls_iter == texture_data.m_emulated_controllers.end())
{
continue;
}

const auto input_image_iter = host_devices_iter->second.find(host_key);
if (input_image_iter == host_devices_iter->second.end())
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())
{
apply_original();
// 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;
}
}
else

for (auto& [emulated_key, rects] : emulated_controls_iter->second)
{
const auto host_key_image = LoadImage(m_base_path + input_image_iter->second);
if (!device_found)
{
// If we get here, that means the controller is set to a
// device not exposed to the pack
dirty = true;
continue;
}

std::string host_key;
sec->Get(emulated_key, &host_key);

for (const auto& rect : rects)
const auto input_image_iter = host_devices_iter->second.find(host_key);
if (input_image_iter == host_devices_iter->second.end())
{
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
dirty = true;
}
else
{
const auto host_key_image = LoadImage(m_base_path + input_image_iter->second);

for (const auto& rect : rects)
{
pixel_data =
Resize(ResizeMode::Nearest, *host_key_image, rect.GetWidth(), rect.GetHeight());
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;
}

CopyImageRegion(pixel_data, *image_to_write, Rect{0, 0, rect.GetWidth(), rect.GetHeight()},
rect);
dirty = true;
}
}
}
@@ -19,10 +19,11 @@ class Configuration
public:
explicit Configuration(const std::string& json_file);
~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:
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;

std::vector<Data> m_dynamic_input_textures;

0 comments on commit 59f4164

Please sign in to comment.