Compare commits

...

4 Commits

Author SHA1 Message Date
Jordan Woyak
69ece6e6af
Merge 5048cf9a15 into 80ea68b13c 2024-11-11 22:13:31 +08:00
JMC47
80ea68b13c
Merge pull request #13183 from Tilka/sync_on_fifo_reset
ProcessorInterface: sync GPU just before PI_FIFO_RESET
2024-11-11 00:38:26 -05:00
Tillmann Karras
fbce737415 ProcessorInterface: sync GPU just before PI_FIFO_RESET
GXAbortFrame() is problematic for Dolphin because it first writes
PI_FIFO_RESET (for which we discard our internal fifo), then disables CP
reads (for which we execute pending commands in the GP fifo in emulated
memory). I don't know whether there is a race condition on hardware, but
there is one for us. Avoid this by also doing a GPU sync here.
2024-11-09 03:29:05 +00:00
Jordan Woyak
5048cf9a15 ControllerInterface/Pipes: Cleanup and minor fixes. 2024-11-05 00:28:22 -06:00
3 changed files with 99 additions and 97 deletions

View File

@ -98,6 +98,10 @@ void ProcessorInterfaceManager::RegisterMMIO(MMIO::Mapping* mmio, u32 base)
{
system.GetGPFifo().ResetGatherPipe();
// Assume that all bytes that made it into the GPU fifo did in fact execute
// before this MMIO write takes effect.
system.GetFifo().SyncGPUForRegisterAccess();
// Call Fifo::ResetVideoBuffer() from the video thread. Since that function
// resets various pointers used by the video thread, we can't call it directly
// from the CPU thread, so queue a task to do it instead. In single-core mode,

View File

@ -5,40 +5,88 @@
#include <algorithm>
#include <array>
#include <cstdlib>
#include <fcntl.h>
#include <iostream>
#include <locale>
#include <filesystem>
#include <map>
#include <sstream>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
#include <string_view>
#include <vector>
#include <fcntl.h>
#include <unistd.h>
#include "Common/FileUtil.h"
#include "Common/Logging/Log.h"
#include "Common/StringUtil.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
namespace ciface::Pipes
{
static const std::array<std::string, 12> s_button_tokens{
{"A", "B", "X", "Y", "Z", "START", "L", "R", "D_UP", "D_DOWN", "D_LEFT", "D_RIGHT"}};
static constexpr std::array s_button_tokens = {"A", "B", "X", "Y", "Z", "START",
"L", "R", "D_UP", "D_DOWN", "D_LEFT", "D_RIGHT"};
static const std::array<std::string, 2> s_shoulder_tokens{{"L", "R"}};
static constexpr std::array s_shoulder_tokens{"L", "R"};
static const std::array<std::string, 2> s_axis_tokens{{"MAIN", "C"}};
static constexpr std::array s_axis_tokens{"MAIN", "C"};
static double StringToDouble(const std::string& text)
static double StringToDouble(std::string text)
{
std::istringstream is(text);
// ignore current locale
is.imbue(std::locale::classic());
double result;
is >> result;
double result = 0;
TryParse(text, &result);
return result;
}
class PipeDevice : public Core::Device
{
public:
PipeDevice(int fd, std::string name);
~PipeDevice();
Core::DeviceRemoval UpdateInput() override;
std::string GetName() const override { return m_name; }
std::string GetSource() const override { return "Pipe"; }
private:
class ButtonInput : public Input
{
public:
ButtonInput(std::string_view name, const bool* value) : m_name(std::move(name)), m_state(*value)
{
}
std::string GetName() const override { return "Button " + std::string{m_name}; }
ControlState GetState() const override { return m_state; }
private:
const std::string_view m_name;
const bool& m_state;
};
template <int Range>
class AnalogInput : public Input
{
public:
AnalogInput(std::string name, const ControlState* value)
: m_name(std::move(name)), m_state(*value)
{
}
std::string GetName() const override { return "Axis " + m_name + (Range < 0 ? " -" : " +"); }
ControlState GetState() const override { return m_state / Range; }
private:
const std::string m_name;
const ControlState& m_state;
};
void AddAxis(std::string name);
void ParseCommand(const std::string& command);
void SetAxis(const std::string& entry, double value);
const int m_fd;
const std::string m_name;
std::string m_buf;
std::map<std::string, bool> m_buttons;
std::map<std::string, ControlState> m_axes;
};
class InputBackend final : public ciface::InputBackend
{
public:
@ -53,43 +101,42 @@ std::unique_ptr<ciface::InputBackend> CreateInputBackend(ControllerInterface* co
void InputBackend::PopulateDevices()
{
// Search the Pipes directory for files that we can open in read-only,
// non-blocking mode. The device name is the virtual name of the file.
File::FSTEntry fst;
std::string dir_path = File::GetUserPath(D_PIPES_IDX);
if (!File::Exists(dir_path))
return;
fst = File::ScanDirectoryTree(dir_path, false);
if (!fst.isDirectory)
return;
for (unsigned int i = 0; i < fst.size; ++i)
// Search the "Pipes" directory for fifo files that we can open in read-only,
// non-blocking mode. The filename becomes the Device name.
const std::string& dir_path = File::GetUserPath(D_PIPES_IDX);
std::error_code error;
for (const auto& dir_entry : std::filesystem::directory_iterator{dir_path, error})
{
const File::FSTEntry& child = fst.children[i];
if (child.isDirectory)
if (!dir_entry.is_fifo())
continue;
int fd = open(child.physicalName.c_str(), O_RDONLY | O_NONBLOCK);
const int fd = open(dir_entry.path().c_str(), O_RDONLY | O_NONBLOCK);
if (fd < 0)
continue;
g_controller_interface.AddDevice(std::make_shared<PipeDevice>(fd, child.virtualName));
GetControllerInterface().AddDevice(
std::make_shared<PipeDevice>(fd, dir_entry.path().filename()));
}
if (error)
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "Pipes/PopulateDevices: {}", error.message());
}
PipeDevice::PipeDevice(int fd, const std::string& name) : m_fd(fd), m_name(name)
PipeDevice::PipeDevice(int fd, std::string name) : m_fd(fd), m_name(std::move(name))
{
for (const auto& tok : s_button_tokens)
{
PipeInput* btn = new PipeInput("Button " + tok);
AddInput(btn);
m_buttons[tok] = btn;
auto [iter, inserted] = m_buttons.emplace(tok, false);
AddInput(new ButtonInput(tok, &iter->second));
}
for (const auto& tok : s_shoulder_tokens)
{
AddAxis(tok, 0.0);
AddAxis(tok);
}
for (const auto& tok : s_axis_tokens)
{
AddAxis(tok + " X", 0.5);
AddAxis(tok + " Y", 0.5);
AddAxis(tok + std::string{" X"});
AddAxis(tok + std::string{" Y"});
}
}
@ -103,7 +150,7 @@ Core::DeviceRemoval PipeDevice::UpdateInput()
// Read any pending characters off the pipe. If we hit a newline,
// then dequeue a command off the front of m_buf and parse it.
char buf[32];
ssize_t bytes_read = read(m_fd, buf, sizeof buf);
ssize_t bytes_read = read(m_fd, buf, sizeof(buf));
while (bytes_read > 0)
{
m_buf.append(buf, bytes_read);
@ -120,29 +167,18 @@ Core::DeviceRemoval PipeDevice::UpdateInput()
return Core::DeviceRemoval::Keep;
}
void PipeDevice::AddAxis(const std::string& name, double value)
void PipeDevice::AddAxis(std::string name)
{
// Dolphin uses separate axes for left/right, which complicates things.
PipeInput* ax_hi = new PipeInput("Axis " + name + " +");
ax_hi->SetState(value);
PipeInput* ax_lo = new PipeInput("Axis " + name + " -");
ax_lo->SetState(value);
m_axes[name + " +"] = ax_hi;
m_axes[name + " -"] = ax_lo;
AddFullAnalogSurfaceInputs(ax_lo, ax_hi);
auto [iter, inserted] = m_axes.emplace(name, 0.0);
AddInput(new AnalogInput<-1>(name, &iter->second));
AddInput(new AnalogInput<+1>(std::move(name), &iter->second));
}
void PipeDevice::SetAxis(const std::string& entry, double value)
{
value = std::clamp(value, 0.0, 1.0);
double hi = std::max(0.0, value - 0.5) * 2.0;
double lo = (0.5 - std::min(0.5, value)) * 2.0;
auto search_hi = m_axes.find(entry + " +");
if (search_hi != m_axes.end())
search_hi->second->SetState(hi);
auto search_lo = m_axes.find(entry + " -");
if (search_lo != m_axes.end())
search_lo->second->SetState(lo);
auto search = m_axes.find(entry);
if (search != m_axes.end())
search->second = std::clamp(value, 0.0, 1.0) * 2.0 - 1.0;
}
void PipeDevice::ParseCommand(const std::string& command)
@ -154,14 +190,14 @@ void PipeDevice::ParseCommand(const std::string& command)
{
auto search = m_buttons.find(tokens[1]);
if (search != m_buttons.end())
search->second->SetState(tokens[0] == "PRESS" ? 1.0 : 0.0);
search->second = tokens[0] == "PRESS";
}
else if (tokens[0] == "SET")
{
if (tokens.size() == 3)
{
double value = StringToDouble(tokens[2]);
SetAxis(tokens[1], (value / 2.0) + 0.5);
SetAxis(tokens[1], (value + 1.0) / 2.0);
}
else if (tokens.size() == 4)
{

View File

@ -3,10 +3,6 @@
#pragma once
#include <map>
#include <string>
#include <vector>
#include "InputCommon/ControllerInterface/ControllerInterface.h"
namespace ciface::Pipes
@ -23,38 +19,4 @@ namespace ciface::Pipes
std::unique_ptr<ciface::InputBackend> CreateInputBackend(ControllerInterface* controller_interface);
class PipeDevice : public Core::Device
{
public:
PipeDevice(int fd, const std::string& name);
~PipeDevice();
Core::DeviceRemoval UpdateInput() override;
std::string GetName() const override { return m_name; }
std::string GetSource() const override { return "Pipe"; }
private:
class PipeInput : public Input
{
public:
PipeInput(const std::string& name) : m_name(name), m_state(0.0) {}
std::string GetName() const override { return m_name; }
ControlState GetState() const override { return m_state; }
void SetState(ControlState state) { m_state = state; }
private:
const std::string m_name;
ControlState m_state;
};
void AddAxis(const std::string& name, double value);
void ParseCommand(const std::string& command);
void SetAxis(const std::string& entry, double value);
const int m_fd;
const std::string m_name;
std::string m_buf;
std::map<std::string, PipeInput*> m_buttons;
std::map<std::string, PipeInput*> m_axes;
};
} // namespace ciface::Pipes