diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 22:12:48 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 22:13:02 -0500 |
| commit | 3ee0037fd3accba459d0d5ecd8acad831174d4d7 (patch) | |
| tree | 55953ce44091103e4f7c4a2f45490be961282b27 /runtime/shims/gameinput.cpp | |
| parent | 9b8707c03dabb59ba2e29b9b0749d3c3eb1f3903 (diff) | |
| parent | 5115f0887940595aa0f46fed7fc9db28b64151ff (diff) | |
Merge main into RT-DIRECT-HEAP-GRAPHICS (#1232)
This merges #1064 as prepared (its head + a merge of main), so it brings #1232 up to date with main and
with #1064. #1232's branch carried #974 (merged into main since) and an older head of #1064 (7c47fd946).
main already supplies the directly indexed heaps to graphics draws (integration #984):
graphics_user_data() on DrawInstanced, DrawIndexedInstanced and DispatchMesh, at
RootArgumentLayout::graphics_arguments_end (pack_stage(Stage::Pixel)'s end_sgpr, set when the console
layout places the signature), LEONIDA_GRAPHICS_DIRECT_HEAPS=0, its notes for a signature on the s0
layout or one that fills s31, and d3d12_graphics_direct_heaps_test (recorded draws and the VS/PS GPU
reads). That form stays. What this pull request adds beyond it is carried onto it: the mesh case.
Conflicts (all taken from #1064's merged branch, then the PR's mesh parts carried over):
- runtime/shims/d3d12_com.cpp: main's graphics_user_data()/with_direct_heaps() and notes. The PR's own
graphics_user_data(), GetEnvironmentVariableA switch and no-room notes are main's in another form and
dropped. Carried: the comment says a mesh shader reads push dword d as s(d + 8) through #974's window,
so it gets the heaps after its arguments from s16 (main's comment still said the mesh stage gets none);
and DispatchMesh reports once when a heap falls past the mesh window (push dword 24 and up, s32 for the
mesh shader), the case raised in the review of #1232 (mesh_ok accepts arguments up to push dword 24).
- runtime/shims/d3d12_com.h, runtime/shims/d3d12_vulkan_device.cpp: main's (graphics_arguments_end from
pixel.end_sgpr; the PR set it from its Spill::block form, which main did not take from #974).
- runtime/test/glsl/cs_user_data.comp: main's (identical direct-indexing mode).
- docs/d3d12_vulkan.md: main's section 3.4 with #1064's compute paragraph; the graphics paragraph's
"a mesh shader ... is not modelled" replaced by the mesh window position, cited to issue #14 comment
7143020922 as a report not reproduced here, the window-overflow note and the mesh test.
- runtime/test/d3d12_mesh_test.cpp (no conflict): the PR's recorded-argument checks kept (vertex and mesh
draw with 4 constants, resource-only rebind, 23 constants, 30 constants); the 30-constant comment now
says it keeps pack()'s s0 layout on main.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/gameinput.cpp')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 123 |
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 | ||
| 134 | std::string env(const char* name) { | 135 | std::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. |