upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/gameinput.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 21:44:23 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 21:44:23 -0500
commitb8563e949ed389fff20be91c73687a70b5213469 (patch)
treed69c6817854ce0a14a67ff1fd0f1c39dec68a91f /runtime/shims/gameinput.cpp
parent00fc23b9a9eeb1a75f1fec2b58024bea15c26663 (diff)
parent7ae1dd3de34f1542242bc8ccfc80d9b97d20cc01 (diff)
Merge main into D3D-448-GRAPHICS (#974)
main already lays graphics root arguments out in the console layout (from s8, one dword per table: #939's graphics-root-layout patch in #984's form, pack_stage(Stage::Pixel), RootArgumentLayout::graphics_console, graphics_arguments_end, LEONIDA_GRAPHICS_STAGE_LAYOUT=0 for pack()). That form stays. What this pull request adds beyond it is carried onto it: the mesh-shader root window (translator, index "root_window", mesh_ok, the two refusals) and its tests. Its own graphics spill handling (Spill::block) and LEONIDA_GRAPHICS_ROOT_LAYOUT=pc are not carried: main keeps pack()'s s0 layout for a graphics signature past s31, and its switch does what =pc did. Conflicts: - runtime/src/root_signature.h, root_signature.cpp: main's pack_stage (compute spill array, spill_dword); the Spill enum, first_spilled and spilled_dwords dropped. The header keeps the PR's evidence note, with Stage::Pixel. - runtime/shims/d3d12_com.h: main's comment and members, with the mesh window described; mesh_ok added (false by default, set only for a console-layout signature that Stage::Mesh also places); the PR's second graphics_console dropped. - runtime/shims/d3d12_vulkan_device.cpp (CreateRootSignature): main's pixel/packed form; the PR's Stage::Mesh pack and its log line added, mesh_ok = pixel.ok && mesh.ok. The mesh-shader refusal without "root_window" (no conflict) stays. - runtime/shims/d3d12_com.cpp: DispatchMesh keeps main's any_nonempty_viewport() and graphics_user_data() after the PR's mesh_ok check; address() keeps main's comment. The PR's kNoSgpr guard in constants()/address() stays, reworded: in main a spilled compute parameter has first_sgpr kNoSgpr, and kNoSgpr + offset wrapped round to s0. - runtime/src/shader_library.h: both (main's wave64_graphics, the PR's root_window). - runtime/test/d3d12_mesh_test.cpp: main's mesh shader-kind byte on both containers (also the PR's ms_old); the PR's two-parameter signature replaces main's 25-dword padding onto the s0 layout; both test blocks kept (the PR's window refusals, main's DISPATCH_MESH command signature), each in its own scope. The =pc case is dropped (main's switch is read once per process). - runtime/CMakeLists.txt (no conflict): the mesh test uses ps_draw.spv again; the ps_draw_s0.spv rule is gone. - runtime/test/glsl/ps_draw.frag, vs_depth.vert, vs_draw.vert, vs_draw_int.vert, ps_depth_write.frag and runtime/test/shaders/ps_m15, ps_m2, ps_m2_array, ps_m2_literal, ps_m2_uoff, ps_m3, ps_m3_post, ps_m3_query, vs_m15, vs_m3 (.asm): main's (the same s8 rewrite done twice); ps_draw.frag's mesh-test note updated. - runtime/test/root_signature_test.cpp (no conflict): the Spill::block cases dropped, the window cases kept. - docs/design.md: main's rows (loop guard, direct resource heap) and the PR's user-data row. - docs/d3d12_vulkan.md (no conflict): section 3.5 and the CreateRootSignature bullet rewritten for main's form. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp123
1 files changed, 116 insertions, 7 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 402179cde..db685e71f 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -47,6 +47,7 @@
47#include <mutex> 47#include <mutex>
48#include <string> 48#include <string>
49#include <thread> 49#include <thread>
50#include <utility>
50#include <vector> 51#include <vector>
51 52
52#include "gameinput.h" 53#include "gameinput.h"
@@ -132,10 +133,14 @@ const rt::gameinput::HapticApi& haptics() { return *g_haptic_api.load(); }
132// ---- configuration ---------------------------------------------------------------------------------------------- 133// ---- configuration ----------------------------------------------------------------------------------------------
133 134
134std::string env(const char* name) { 135std::string env(const char* name) {
135 char buf[64]; 136 // Any length: an input script (LEONIDA_INPUT_SCRIPT) is easily longer than a fixed buffer, and a value that did
136 const DWORD n = GetEnvironmentVariableA(name, buf, sizeof buf); 137 // not fit used to read as unset.
137 if (n == 0 || n >= sizeof buf) return {}; 138 const DWORD need = GetEnvironmentVariableA(name, nullptr, 0); // with the terminator
138 std::string s(buf, n); 139 if (need == 0) return {};
140 std::string s(need, '\0');
141 const DWORD n = GetEnvironmentVariableA(name, s.data(), need);
142 if (n == 0 || n >= need) return {};
143 s.resize(n);
139 for (auto& c : s) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); 144 for (auto& c : s) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
140 return s; 145 return s;
141} 146}
@@ -1861,8 +1866,8 @@ void update_keyboard_pad() {
1861 bool down[SDL_GAMEPAD_BUTTON_COUNT] = {}; 1866 bool down[SDL_GAMEPAD_BUTTON_COUNT] = {};
1862 int lx = 0, ly = 0; 1867 int lx = 0, ly = 0;
1863 bool aim = false, fire = false; 1868 bool aim = false, fire = false;
1864 // Out of focus every button, stick and trigger is released, so a key held while switching windows does not stay 1869 // Focus gates physical keyboard and mouse input. Read the cursor afresh on return to avoid a jump.
1865 // pressed, and the cursor is read afresh on return, so moving it in another window does not swing the stick. 1870 // Scripted presses still run while the window is unfocused.
1866 const bool active = mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()(); 1871 const bool active = mode == rt::gameinput::KeyboardPadMode::Always || g_has_focus.load()();
1867 if (active) { 1872 if (active) {
1868 const rt::gameinput::KeyDownFn key_down = g_key_down.load(); 1873 const rt::gameinput::KeyDownFn key_down = g_key_down.load();
@@ -1890,11 +1895,88 @@ void update_keyboard_pad() {
1890 } 1895 }
1891 clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours()); 1896 clip_cursor(active && g_raw_ready && g_cursor_pos.load() == system_cursor_pos && foreground_is_ours());
1892 for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]); 1897 for (const auto& b : kKeyBindings) SDL_SetJoystickVirtualButton(g_keyboard.js, b.sdl_button, down[b.sdl_button]);
1898 // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" (with LEONIDA_KEYBOARD_PAD=always): timed presses on the keyboard
1899 // pad, in seconds since the pad's first update, each held 300 ms. For unattended boots: the title's first prompt
1900 // waits for a button with no timeout (docs/re/notes/firstframe.md, step 5). Buttons: a b x y start back lb rb
1901 // up down left right. The rsup and rsdown entries replay the right-stick throws logged below.
1902 int right_y = g_keyboard.right_y;
1903 struct ScriptedPress {
1904 SDL_GamepadButton button;
1905 double at;
1906 int right_y = 0;
1907 };
1908 static const std::vector<ScriptedPress> script = [] {
1909 std::vector<ScriptedPress> out;
1910 static const std::pair<const char*, SDL_GamepadButton> names[] = {
1911 {"a", SDL_GAMEPAD_BUTTON_SOUTH}, {"b", SDL_GAMEPAD_BUTTON_EAST},
1912 {"x", SDL_GAMEPAD_BUTTON_WEST}, {"y", SDL_GAMEPAD_BUTTON_NORTH},
1913 {"start", SDL_GAMEPAD_BUTTON_START}, {"back", SDL_GAMEPAD_BUTTON_BACK},
1914 {"lb", SDL_GAMEPAD_BUTTON_LEFT_SHOULDER}, {"rb", SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER},
1915 {"up", SDL_GAMEPAD_BUTTON_DPAD_UP}, {"down", SDL_GAMEPAD_BUTTON_DPAD_DOWN},
1916 {"left", SDL_GAMEPAD_BUTTON_DPAD_LEFT}, {"right", SDL_GAMEPAD_BUTTON_DPAD_RIGHT}};
1917 const std::string text = env("LEONIDA_INPUT_SCRIPT");
1918 if (text.empty()) return out;
1919 constexpr char whitespace[] = " \t\r\n\f\v";
1920 size_t items = 0;
1921 for (size_t at = 0, end; at <= text.size(); at = end + 1) {
1922 ++items;
1923 end = std::min(text.find(',', at), text.size());
1924 const std::string item = text.substr(at, end - at);
1925 const size_t first = item.find_first_not_of(whitespace);
1926 const size_t sep = item.find('@');
1927 if (sep == std::string::npos || first >= sep) continue;
1928 const char* time = item.c_str() + sep + 1;
1929 char* tail;
1930 const double seconds = std::strtod(time, &tail);
1931 if (tail == time || !std::isfinite(seconds) || seconds < 0 ||
1932 item.find_first_not_of(whitespace, tail - item.c_str()) != std::string::npos) continue;
1933 if (item.compare(first, sep - first, "rsup") == 0 || item.compare(first, sep - first, "rsdown") == 0) {
1934 const bool up = item.compare(first, sep - first, "rsup") == 0;
1935 out.push_back({SDL_GAMEPAD_BUTTON_INVALID, seconds, up ? -kStickFull : kStickFull});
1936 continue;
1937 }
1938 for (const auto& [name, button] : names) {
1939 if (item.compare(first, sep - first, name) == 0) {
1940 out.push_back({button, seconds});
1941 break;
1942 }
1943 }
1944 }
1945 std::fprintf(stderr, "[rt] gameinput: LEONIDA_INPUT_SCRIPT: %zu scripted press(es), %zu rejected\n",
1946 out.size(), items - out.size());
1947 return out;
1948 }();
1949 if (!script.empty()) {
1950 static const ULONGLONG origin = GetTickCount64();
1951 const double now = (GetTickCount64() - origin) / 1000.0;
1952 // One decision per button: a later entry for the same button must not release what an earlier entry holds
1953 // (it did: with several entries only the last one ever reached the title).
1954 bool scripted[SDL_GAMEPAD_BUTTON_COUNT] = {};
1955 bool named[SDL_GAMEPAD_BUTTON_COUNT] = {};
1956 for (const auto& press : script) {
1957 const bool held = now >= press.at && now < press.at + 0.3;
1958 if (press.right_y) {
1959 if (held) right_y = press.right_y;
1960 } else {
1961 named[press.button] = true;
1962 scripted[press.button] |= held;
1963 }
1964 }
1965 for (int button = 0; button < SDL_GAMEPAD_BUTTON_COUNT; ++button) {
1966 if (!named[button]) continue;
1967 SDL_SetJoystickVirtualButton(g_keyboard.js, static_cast<SDL_GamepadButton>(button),
1968 scripted[button] || down[button]);
1969 static bool was[SDL_GAMEPAD_BUTTON_COUNT] = {};
1970 if (scripted[button] && !was[button])
1971 std::fprintf(stderr, "[rt] gameinput: scripted press of button %d at %.1f s\n", button, now);
1972 was[button] = scripted[button];
1973 }
1974 }
1893 const int reach = lx && ly ? kStickDiagonal : kStickFull; 1975 const int reach = lx && ly ? kStickDiagonal : kStickFull;
1894 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTX, static_cast<Sint16>(lx * reach)); 1976 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTX, static_cast<Sint16>(lx * reach));
1895 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTY, static_cast<Sint16>(ly * reach)); 1977 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFTY, static_cast<Sint16>(ly * reach));
1896 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTX, static_cast<Sint16>(g_keyboard.right_x)); 1978 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTX, static_cast<Sint16>(g_keyboard.right_x));
1897 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTY, static_cast<Sint16>(g_keyboard.right_y)); 1979 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHTY, static_cast<Sint16>(right_y));
1898 // SDL maps a full-range virtual axis to the trigger, so -32768 is released, 0 half pulled, 32767 full. 1980 // SDL maps a full-range virtual axis to the trigger, so -32768 is released, 0 half pulled, 32767 full.
1899 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, aim ? 32767 : -32768); 1981 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, aim ? 32767 : -32768);
1900 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, fire ? 32767 : -32768); 1982 SDL_SetJoystickVirtualAxis(g_keyboard.js, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, fire ? 32767 : -32768);
@@ -1965,6 +2047,33 @@ void poll_once() {
1965 read_raw(d, &snap); 2047 read_raw(d, &snap);
1966 const bool guide_changed = d->last_snap.guide != snap.guide; 2048 const bool guide_changed = d->last_snap.guide != snap.guide;
1967 const bool changed = !(d->last_snap == snap); 2049 const bool changed = !(d->last_snap == snap);
2050 // Every press is logged in LEONIDA_INPUT_SCRIPT's own form (name@seconds), so a session played by hand can
2051 // be replayed unattended: join the logged items with commas. The right stick's vertical throws (what scrolls
2052 // a page) are logged as rsup/rsdown. LEONIDA_INPUT_LOG=0 turns the lines off.
2053 {
2054 static const bool log_presses = env("LEONIDA_INPUT_LOG") != "0";
2055 static const ULONGLONG log_origin = GetTickCount64();
2056 if (log_presses) {
2057 static const std::pair<uint32_t, const char*> kNames[] = {
2058 {GameInputGamepadA, "a"}, {GameInputGamepadB, "b"},
2059 {GameInputGamepadX, "x"}, {GameInputGamepadY, "y"},
2060 {GameInputGamepadMenu, "start"}, {GameInputGamepadView, "back"},
2061 {GameInputGamepadLeftShoulder, "lb"}, {GameInputGamepadRightShoulder, "rb"},
2062 {GameInputGamepadDPadUp, "up"}, {GameInputGamepadDPadDown, "down"},
2063 {GameInputGamepadDPadLeft, "left"}, {GameInputGamepadDPadRight, "right"}};
2064 const double at = (GetTickCount64() - log_origin) / 1000.0;
2065 const uint32_t pressed = snap.gamepad.buttons & ~d->last_snap.gamepad.buttons;
2066 for (const auto& n : kNames)
2067 if (pressed & n.first)
2068 std::fprintf(stderr, "[rt] gameinput: press %s@%.1f (%s)\n", n.second, at, d->name.c_str());
2069 const auto throw_of = [](float y) { return y > 0.6f ? 1 : y < -0.6f ? -1 : 0; };
2070 const int was = throw_of(d->last_snap.gamepad.rightThumbstickY);
2071 const int is = throw_of(snap.gamepad.rightThumbstickY);
2072 if (is != was && is != 0)
2073 std::fprintf(stderr, "[rt] gameinput: press %s@%.1f (%s)\n", is > 0 ? "rsup" : "rsdown", at,
2074 d->name.c_str());
2075 }
2076 }
1968 d->last_snap = snap; 2077 d->last_snap = snap;
1969 // A newly opened pad's first sample here is its baseline (active_input.h), so connecting a pad with a button 2078 // A newly opened pad's first sample here is its baseline (active_input.h), so connecting a pad with a button
1970 // held changes nothing. 2079 // held changes nothing.