-
-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathVR.cpp
3183 lines (2449 loc) · 111 KB
/
VR.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define NOMINMAX
#include <fstream>
#include <windows.h>
#include <dbt.h>
#include <imgui.h>
#include <utility/Module.hpp>
#include <utility/Registry.hpp>
#include <utility/ScopeGuard.hpp>
#include <sdk/Globals.hpp>
#include <sdk/CVar.hpp>
#include <sdk/threading/GameThreadWorker.hpp>
#include <sdk/UGameplayStatics.hpp>
#include <sdk/APlayerController.hpp>
#include <tracy/Tracy.hpp>
#include "Framework.hpp"
#include "frameworkConfig.hpp"
#include "utility/Logging.hpp"
#include "VR.hpp"
std::shared_ptr<VR>& VR::get() {
static std::shared_ptr<VR> instance = std::make_shared<VR>();
return instance;
}
// Called when the mod is initialized
std::optional<std::string> VR::clean_initialize() try {
ZoneScopedN(__FUNCTION__);
auto openvr_error = initialize_openvr();
if (openvr_error || !m_openvr->loaded) {
if (m_openvr->error) {
spdlog::info("OpenVR failed to load: {}", *m_openvr->error);
}
m_openvr->is_hmd_active = false;
m_openvr->was_hmd_active = false;
m_openvr->needs_pose_update = false;
// Attempt to load OpenXR instead
auto openxr_error = initialize_openxr();
if (openxr_error || !m_openxr->loaded) {
m_openxr->needs_pose_update = false;
}
} else {
m_openxr->error = "OpenVR loaded first.";
}
if (!get_runtime()->loaded) {
// this is okay. we're not going to fail the whole thing entirely
// so we're just going to return OK, but
// when the VR mod draws its menu, it'll say "VR is not available"
return Mod::on_initialize();
}
// Check whether the user has Hardware accelerated GPU scheduling enabled
const auto hw_schedule_value = utility::get_registry_dword(
HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Control\\GraphicsDrivers",
"HwSchMode");
if (hw_schedule_value) {
m_has_hw_scheduling = *hw_schedule_value == 2;
}
m_init_finished = true;
// all OK
return Mod::on_initialize();
} catch(...) {
spdlog::error("Exception occurred in VR::on_initialize()");
m_runtime->error = "Exception occurred in VR::on_initialize()";
m_openxr->dll_missing = false;
m_openvr->dll_missing = false;
m_openxr->error = "Exception occurred in VR::on_initialize()";
m_openvr->error = "Exception occurred in VR::on_initialize()";
m_openvr->loaded = false;
m_openvr->is_hmd_active = false;
m_openxr->loaded = false;
m_init_finished = false;
return Mod::on_initialize();
}
std::optional<std::string> VR::initialize_openvr() {
ZoneScopedN(__FUNCTION__);
spdlog::info("Attempting to load OpenVR");
m_openvr = std::make_shared<runtimes::OpenVR>();
m_openvr->loaded = false;
const auto wants_openxr = m_requested_runtime_name->value() == "openxr_loader.dll";
SPDLOG_INFO("[VR] Requested runtime: {}", m_requested_runtime_name->value());
if (wants_openxr && GetModuleHandleW(L"openxr_loader.dll") != nullptr) {
// pre-injected
m_openvr->dll_missing = true;
m_openvr->error = "OpenXR already loaded";
return Mod::on_initialize();
}
if (GetModuleHandleW(L"openvr_api.dll") == nullptr) {
// pre-injected
if (GetModuleHandleW(L"openxr_loader.dll") != nullptr) {
m_openvr->dll_missing = true;
m_openvr->error = "OpenXR already loaded";
return Mod::on_initialize();
}
if (utility::load_module_from_current_directory(L"openvr_api.dll") == nullptr) {
spdlog::info("[VR] Could not load openvr_api.dll");
m_openvr->dll_missing = true;
m_openvr->error = "Could not load openvr_api.dll";
return Mod::on_initialize();
}
}
if (g_framework->is_dx12()) {
m_d3d12.on_reset(this);
} else {
m_d3d11.on_reset(this);
}
m_openvr->needs_pose_update = true;
m_openvr->got_first_poses = false;
m_openvr->is_hmd_active = true;
m_openvr->was_hmd_active = true;
spdlog::info("Attempting to call vr::VR_Init");
auto error = vr::VRInitError_None;
m_openvr->hmd = vr::VR_Init(&error, vr::VRApplication_Scene);
// check if error
if (error != vr::VRInitError_None) {
m_openvr->error = "VR_Init failed: " + std::string{vr::VR_GetVRInitErrorAsEnglishDescription(error)};
return Mod::on_initialize();
}
if (m_openvr->hmd == nullptr) {
m_openvr->error = "VR_Init failed: HMD is null";
return Mod::on_initialize();
}
// get render target size
m_openvr->update_render_target_size();
if (vr::VRCompositor() == nullptr) {
m_openvr->error = "VRCompositor failed to initialize.";
return Mod::on_initialize();
}
auto input_error = initialize_openvr_input();
if (input_error) {
m_openvr->error = *input_error;
return Mod::on_initialize();
}
auto overlay_error = m_overlay_component.on_initialize_openvr();
if (overlay_error) {
m_openvr->error = *overlay_error;
return Mod::on_initialize();
}
m_openvr->loaded = true;
m_openvr->error = std::nullopt;
m_runtime = m_openvr;
return Mod::on_initialize();
}
std::optional<std::string> VR::initialize_openvr_input() {
ZoneScopedN(__FUNCTION__);
const auto module_directory = Framework::get_persistent_dir();
// write default actions and bindings with the static strings we have
for (auto& it : m_binding_files) {
spdlog::info("Writing default binding file {}", it.first);
std::ofstream file{ module_directory / it.first };
file << it.second;
}
const auto actions_path = module_directory / "actions.json";
auto input_error = vr::VRInput()->SetActionManifestPath(actions_path.string().c_str());
if (input_error != vr::VRInputError_None) {
return "VRInput failed to set action manifest path: " + std::to_string((uint32_t)input_error);
}
// get action set
auto action_set_error = vr::VRInput()->GetActionSetHandle("/actions/default", &m_action_set);
if (action_set_error != vr::VRInputError_None) {
return "VRInput failed to get action set: " + std::to_string((uint32_t)action_set_error);
}
if (m_action_set == vr::k_ulInvalidActionSetHandle) {
return "VRInput failed to get action set handle.";
}
for (auto& it : m_action_handles) {
auto error = vr::VRInput()->GetActionHandle(it.first.c_str(), &it.second.get());
if (error != vr::VRInputError_None) {
return "VRInput failed to get action handle: (" + it.first + "): " + std::to_string((uint32_t)error);
}
if (it.second == vr::k_ulInvalidActionHandle) {
return "VRInput failed to get action handle: (" + it.first + ")";
}
}
m_active_action_set.ulActionSet = m_action_set;
m_active_action_set.ulRestrictedToDevice = vr::k_ulInvalidInputValueHandle;
m_active_action_set.nPriority = 0;
m_openvr->pose_action = m_action_pose;
m_openvr->grip_pose_action = m_action_grip_pose;
detect_controllers();
return std::nullopt;
}
std::optional<std::string> VR::initialize_openxr() {
ZoneScopedN(__FUNCTION__);
m_openxr.reset();
m_openxr = std::make_shared<runtimes::OpenXR>();
spdlog::info("[VR] Initializing OpenXR");
if (GetModuleHandleW(L"openxr_loader.dll") == nullptr) {
if (utility::load_module_from_current_directory(L"openxr_loader.dll") == nullptr) {
spdlog::info("[VR] Could not load openxr_loader.dll");
m_openxr->loaded = false;
m_openxr->error = "Could not load openxr_loader.dll";
return std::nullopt;
}
}
if (g_framework->is_dx12()) {
m_d3d12.on_reset(this);
} else {
m_d3d11.on_reset(this);
}
m_openxr->needs_pose_update = true;
m_openxr->got_first_poses = false;
// Step 1: Create an instance
spdlog::info("[VR] Creating OpenXR instance");
XrResult result{XR_SUCCESS};
// We may just be restarting OpenXR, so try to find an existing instance first
if (m_openxr->instance == XR_NULL_HANDLE) {
std::vector<const char*> extensions{};
if (g_framework->is_dx12()) {
extensions.push_back(XR_KHR_D3D12_ENABLE_EXTENSION_NAME);
} else {
extensions.push_back(XR_KHR_D3D11_ENABLE_EXTENSION_NAME);
}
// Enumerate available extensions and enable depth extension if available
uint32_t extension_count{};
result = xrEnumerateInstanceExtensionProperties(nullptr, 0, &extension_count, nullptr);
std::vector<XrExtensionProperties> extension_properties(extension_count, {XR_TYPE_EXTENSION_PROPERTIES});
if (!XR_FAILED(result)) try {
result = xrEnumerateInstanceExtensionProperties(nullptr, extension_count, &extension_count, extension_properties.data());
if (!XR_FAILED(result)) {
for (const auto& extension_property : extension_properties) {
spdlog::info("[VR] Found OpenXR extension: {}", extension_property.extensionName);
}
const std::unordered_set<std::string> wanted_extensions {
XR_KHR_COMPOSITION_LAYER_DEPTH_EXTENSION_NAME,
XR_KHR_COMPOSITION_LAYER_CYLINDER_EXTENSION_NAME
// To be seen if we need more!
};
for (const auto& extension_property : extension_properties) {
if (wanted_extensions.contains(extension_property.extensionName)) {
spdlog::info("[VR] Enabling {} extension", extension_property.extensionName);
m_openxr->enabled_extensions.insert(extension_property.extensionName);
extensions.push_back(extension_property.extensionName);
}
}
}
} catch(...) {
spdlog::error("[VR] Unknown error while enumerating OpenXR extensions");
}
XrInstanceCreateInfo instance_create_info{XR_TYPE_INSTANCE_CREATE_INFO};
instance_create_info.next = nullptr;
instance_create_info.enabledExtensionCount = (uint32_t)extensions.size();
instance_create_info.enabledExtensionNames = extensions.data();
std::string application_name{"UEVR"};
// Append the current executable name to the application base name
{
const auto exe = utility::get_executable();
const auto full_path = utility::get_module_path(exe);
if (full_path) {
const auto fs_path = std::filesystem::path(*full_path);
const auto filename = fs_path.stem().string();
application_name += "_" + filename;
// Trim the name to 127 characters
if (application_name.length() >= XR_MAX_APPLICATION_NAME_SIZE) {
application_name = application_name.substr(0, XR_MAX_APPLICATION_NAME_SIZE - 1);
}
}
}
spdlog::info("[VR] Application name: {}", application_name);
strcpy(instance_create_info.applicationInfo.applicationName, application_name.c_str());
instance_create_info.applicationInfo.applicationName[XR_MAX_APPLICATION_NAME_SIZE - 1] = '\0';
instance_create_info.applicationInfo.apiVersion = XR_CURRENT_API_VERSION;
result = xrCreateInstance(&instance_create_info, &m_openxr->instance);
// we can't convert the result to a string here
// because the function requires the instance to be valid
if (result != XR_SUCCESS) {
m_openxr->error = "Could not create openxr instance: " + std::to_string((int32_t)result);
if (result == XR_ERROR_LIMIT_REACHED) {
m_openxr->error = "Could not create openxr instance: XR_ERROR_LIMIT_REACHED\n"
"Ensure that the OpenXR plugin has been renamed or deleted from the game's binaries folder.";
}
spdlog::error("[VR] {}", m_openxr->error.value());
return std::nullopt;
}
} else {
spdlog::info("[VR] Found existing openxr instance");
}
// Step 2: Create a system
spdlog::info("[VR] Creating OpenXR system");
// We may just be restarting OpenXR, so try to find an existing system first
if (m_openxr->system == XR_NULL_SYSTEM_ID) {
XrSystemGetInfo system_info{XR_TYPE_SYSTEM_GET_INFO};
system_info.formFactor = m_openxr->form_factor;
result = xrGetSystem(m_openxr->instance, &system_info, &m_openxr->system);
if (result != XR_SUCCESS) {
m_openxr->error = "Could not create openxr system: " + m_openxr->get_result_string(result);
spdlog::error("[VR] {}", m_openxr->error.value());
return std::nullopt;
}
} else {
spdlog::info("[VR] Found existing openxr system");
}
// Step 3: Create a session
spdlog::info("[VR] Initializing graphics info");
XrSessionCreateInfo session_create_info{XR_TYPE_SESSION_CREATE_INFO};
if (g_framework->is_dx12()) {
m_d3d12.openxr().initialize(session_create_info);
} else {
m_d3d11.openxr().initialize(session_create_info);
}
spdlog::info("[VR] Creating OpenXR session");
session_create_info.systemId = m_openxr->system;
result = xrCreateSession(m_openxr->instance, &session_create_info, &m_openxr->session);
if (result != XR_SUCCESS) {
m_openxr->error = "Could not create openxr session: " + m_openxr->get_result_string(result);
spdlog::error("[VR] {}", m_openxr->error.value());
return std::nullopt;
}
// Step 4: Create a space
spdlog::info("[VR] Creating OpenXR space");
// We may just be restarting OpenXR, so try to find an existing space first
if (m_openxr->stage_space == XR_NULL_HANDLE) {
XrReferenceSpaceCreateInfo space_create_info{XR_TYPE_REFERENCE_SPACE_CREATE_INFO};
space_create_info.referenceSpaceType = XR_REFERENCE_SPACE_TYPE_LOCAL;
space_create_info.poseInReferenceSpace = {};
space_create_info.poseInReferenceSpace.orientation.w = 1.0f;
result = xrCreateReferenceSpace(m_openxr->session, &space_create_info, &m_openxr->stage_space);
if (result != XR_SUCCESS) {
m_openxr->error = "Could not create openxr stage space: " + m_openxr->get_result_string(result);
spdlog::error("[VR] {}", m_openxr->error.value());
return std::nullopt;
}
}
if (m_openxr->view_space == XR_NULL_HANDLE) {
XrReferenceSpaceCreateInfo space_create_info{XR_TYPE_REFERENCE_SPACE_CREATE_INFO};
space_create_info.referenceSpaceType = XR_REFERENCE_SPACE_TYPE_VIEW;
space_create_info.poseInReferenceSpace = {};
space_create_info.poseInReferenceSpace.orientation.w = 1.0f;
result = xrCreateReferenceSpace(m_openxr->session, &space_create_info, &m_openxr->view_space);
if (result != XR_SUCCESS) {
m_openxr->error = "Could not create openxr view space: " + m_openxr->get_result_string(result);
spdlog::error("[VR] {}", m_openxr->error.value());
return std::nullopt;
}
}
// Step 5: Get the system properties
spdlog::info("[VR] Getting OpenXR system properties");
XrSystemProperties system_properties{XR_TYPE_SYSTEM_PROPERTIES};
result = xrGetSystemProperties(m_openxr->instance, m_openxr->system, &system_properties);
if (result != XR_SUCCESS) {
m_openxr->error = "Could not get system properties: " + m_openxr->get_result_string(result);
spdlog::error("[VR] {}", m_openxr->error.value());
return std::nullopt;
}
m_openxr->on_system_properties_acquired(system_properties);
// Step 6: Get the view configuration properties
m_openxr->update_render_target_size();
// Step 7: Create a view
if (!m_openxr->view_configs.empty()){
m_openxr->views.resize(m_openxr->view_configs.size(), {XR_TYPE_VIEW});
m_openxr->stage_views.resize(m_openxr->view_configs.size(), {XR_TYPE_VIEW});
}
if (m_openxr->view_configs.empty()) {
m_openxr->error = "No view configurations found";
spdlog::error("[VR] {}", m_openxr->error.value());
return std::nullopt;
}
m_openxr->loaded = true;
m_runtime = m_openxr;
if (auto err = initialize_openxr_input()) {
m_openxr->error = err.value();
m_openxr->loaded = false;
spdlog::error("[VR] {}", m_openxr->error.value());
return std::nullopt;
}
detect_controllers();
if (m_init_finished) {
// This is usually done in on_config_load
// but the runtime can be reinitialized, so we do it here instead
initialize_openxr_swapchains();
}
return std::nullopt;
}
std::optional<std::string> VR::initialize_openxr_input() {
ZoneScopedN(__FUNCTION__);
if (auto err = m_openxr->initialize_actions(VR::actions_json)) {
m_openxr->error = err.value();
spdlog::error("[VR] {}", m_openxr->error.value());
return std::nullopt;
}
for (auto& it : m_action_handles) {
auto openxr_action_name = m_openxr->translate_openvr_action_name(it.first);
if (m_openxr->action_set.action_map.contains(openxr_action_name)) {
it.second.get() = (decltype(it.second)::type)m_openxr->action_set.action_map[openxr_action_name];
spdlog::info("[VR] Successfully mapped action {} to {}", it.first, openxr_action_name);
}
}
m_left_joystick = (decltype(m_left_joystick))VRRuntime::Hand::LEFT;
m_right_joystick = (decltype(m_right_joystick))VRRuntime::Hand::RIGHT;
return std::nullopt;
}
std::optional<std::string> VR::initialize_openxr_swapchains() {
ZoneScopedN(__FUNCTION__);
// This depends on the config being loaded.
if (!m_init_finished) {
return std::nullopt;
}
spdlog::info("[VR] Creating OpenXR swapchain");
const auto supported_swapchain_formats = m_openxr->get_supported_swapchain_formats();
// Log
for (auto f : supported_swapchain_formats) {
spdlog::info("[VR] Supported swapchain format: {}", f);
}
if (g_framework->is_dx12()) {
auto err = m_d3d12.openxr().create_swapchains();
if (err) {
m_openxr->error = err.value();
m_openxr->loaded = false;
spdlog::error("[VR] {}", m_openxr->error.value());
return m_openxr->error;
}
} else {
auto err = m_d3d11.openxr().create_swapchains();
if (err) {
m_openxr->error = err.value();
m_openxr->loaded = false;
spdlog::error("[VR] {}", m_openxr->error.value());
return m_openxr->error;
}
}
return std::nullopt;
}
bool VR::detect_controllers() {
ZoneScopedN(__FUNCTION__);
// already detected
if (!m_controllers.empty()) {
return true;
}
if (get_runtime()->is_openvr()) {
auto left_joystick_origin_error = vr::EVRInputError::VRInputError_None;
auto right_joystick_origin_error = vr::EVRInputError::VRInputError_None;
vr::InputOriginInfo_t left_joystick_origin_info{};
vr::InputOriginInfo_t right_joystick_origin_info{};
// Get input origin info for the joysticks
// get the source input device handles for the joysticks
auto left_joystick_error = vr::VRInput()->GetInputSourceHandle("/user/hand/left", &m_left_joystick);
if (left_joystick_error != vr::VRInputError_None) {
return false;
}
auto right_joystick_error = vr::VRInput()->GetInputSourceHandle("/user/hand/right", &m_right_joystick);
if (right_joystick_error != vr::VRInputError_None) {
return false;
}
m_openvr->left_controller_handle = m_left_joystick;
m_openvr->right_controller_handle = m_right_joystick;
left_joystick_origin_info = {};
right_joystick_origin_info = {};
left_joystick_origin_error = vr::VRInput()->GetOriginTrackedDeviceInfo(m_left_joystick, &left_joystick_origin_info, sizeof(left_joystick_origin_info));
right_joystick_origin_error = vr::VRInput()->GetOriginTrackedDeviceInfo(m_right_joystick, &right_joystick_origin_info, sizeof(right_joystick_origin_info));
if (left_joystick_origin_error != vr::EVRInputError::VRInputError_None || right_joystick_origin_error != vr::EVRInputError::VRInputError_None) {
return false;
}
// Instead of manually going through the devices,
// We do this. The order of the devices isn't always guaranteed to be
// Left, and then right. Using the input state handles will always
// Get us the correct device indices.
m_controllers.push_back(left_joystick_origin_info.trackedDeviceIndex);
m_controllers.push_back(right_joystick_origin_info.trackedDeviceIndex);
m_controllers_set.insert(left_joystick_origin_info.trackedDeviceIndex);
m_controllers_set.insert(right_joystick_origin_info.trackedDeviceIndex);
spdlog::info("Left Hand: {}", left_joystick_origin_info.trackedDeviceIndex);
spdlog::info("Right Hand: {}", right_joystick_origin_info.trackedDeviceIndex);
m_openvr->left_controller_index = left_joystick_origin_info.trackedDeviceIndex;
m_openvr->right_controller_index = right_joystick_origin_info.trackedDeviceIndex;
} else if (get_runtime()->is_openxr()) {
// ezpz
m_controllers.push_back(1);
m_controllers.push_back(2);
m_controllers_set.insert(1);
m_controllers_set.insert(2);
spdlog::info("Left Hand: {}", 1);
spdlog::info("Right Hand: {}", 2);
}
return true;
}
bool VR::is_any_action_down() {
ZoneScopedN(__FUNCTION__);
if (!m_runtime->ready()) {
return false;
}
const auto left_axis = get_left_stick_axis();
const auto right_axis = get_right_stick_axis();
if (glm::length(left_axis) >= m_joystick_deadzone->value()) {
return true;
}
if (glm::length(right_axis) >= m_joystick_deadzone->value()) {
return true;
}
const auto left_joystick = get_left_joystick();
const auto right_joystick = get_right_joystick();
for (auto& it : m_action_handles) {
// These are too easy to trigger
if (it.second == m_action_thumbrest_touch_left || it.second == m_action_thumbrest_touch_right) {
continue;
}
if (it.second == m_action_a_button_touch_left || it.second == m_action_a_button_touch_right) {
continue;
}
if (it.second == m_action_b_button_touch_left || it.second == m_action_b_button_touch_right) {
continue;
}
if (is_action_active(it.second, left_joystick) || is_action_active(it.second, right_joystick)) {
return true;
}
}
return false;
}
bool VR::on_message(HWND wnd, UINT message, WPARAM w_param, LPARAM l_param) {
ZoneScopedN(__FUNCTION__);
if (message == WM_DEVICECHANGE && !m_spoofed_gamepad_connection) {
spdlog::info("[VR] Received WM_DEVICECHANGE");
m_last_xinput_spoof_sent = std::chrono::steady_clock::now();
}
return true;
}
void VR::on_xinput_get_state(uint32_t* retval, uint32_t user_index, XINPUT_STATE* state) {
ZoneScopedN(__FUNCTION__);
if (std::chrono::steady_clock::now() - m_last_engine_tick > std::chrono::seconds(1)) {
SPDLOG_INFO_EVERY_N_SEC(1, "[VR] XInputGetState called, but engine tick hasn't been called in over a second. Is the game loading?");
update_action_states();
}
if (*retval == ERROR_SUCCESS) {
// Once here for normal gamepads, and once for the spoofed gamepad at the end
update_imgui_state_from_xinput_state(*state, false);
}
const auto now = std::chrono::steady_clock::now();
if (now - m_last_xinput_update > std::chrono::seconds(2)) {
m_lowest_xinput_user_index = user_index;
}
if (user_index < m_lowest_xinput_user_index) {
m_lowest_xinput_user_index = user_index;
spdlog::info("[VR] Changed lowest XInput user index to {}", user_index);
}
if (user_index != m_lowest_xinput_user_index) {
if (!m_spoofed_gamepad_connection && is_using_controllers()) {
spdlog::info("[VR] XInputGetState called, but user index is {}", user_index);
}
return;
}
if (!m_spoofed_gamepad_connection) {
spdlog::info("[VR] Successfully spoofed gamepad connection @ {}", user_index);
}
m_last_xinput_update = now;
m_spoofed_gamepad_connection = true;
auto runtime = get_runtime();
auto do_pause_select = [&]() {
if (!runtime->ready()) {
return;
}
if (runtime->handle_pause) {
// Spoof the start button being pressed
state->Gamepad.wButtons |= XINPUT_GAMEPAD_START;
*retval = ERROR_SUCCESS;
runtime->handle_pause = false;
runtime->handle_select_button = false;
}
if (runtime->handle_select_button) {
// Spoof the back button being pressed
state->Gamepad.wButtons |= XINPUT_GAMEPAD_BACK;
*retval = ERROR_SUCCESS;
runtime->handle_select_button = false;
runtime->handle_pause = false;
}
};
do_pause_select();
if (is_using_controllers_within(std::chrono::minutes(5))) {
*retval = ERROR_SUCCESS;
}
if (!is_using_controllers()) {
return;
}
// Clear button state for VR controllers
if (is_using_controllers_within(std::chrono::seconds(5))) {
state->Gamepad.wButtons = 0;
state->Gamepad.bLeftTrigger = 0;
state->Gamepad.bRightTrigger = 0;
state->Gamepad.sThumbLX = 0;
state->Gamepad.sThumbLY = 0;
state->Gamepad.sThumbRX = 0;
state->Gamepad.sThumbRY = 0;
}
const auto left_joystick = get_left_joystick();
const auto right_joystick = get_right_joystick();
const auto wants_swap = m_swap_controllers->value();
runtime->handle_pause_select(is_action_active_any_joystick(m_action_system_button));
do_pause_select();
const auto& a_button_left = !wants_swap ? m_action_a_button_left : m_action_a_button_right;
const auto& a_button_right = !wants_swap ? m_action_a_button_right : m_action_a_button_left;
const auto is_right_a_button_down = is_action_active_any_joystick(a_button_right);
const auto is_left_a_button_down = is_action_active_any_joystick(a_button_left);
if (is_right_a_button_down) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_A;
}
if (is_left_a_button_down) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_B;
}
const auto& b_button_left = !wants_swap ? m_action_b_button_left : m_action_b_button_right;
const auto& b_button_right = !wants_swap ? m_action_b_button_right : m_action_b_button_left;
const auto is_right_b_button_down = is_action_active_any_joystick(b_button_right);
const auto is_left_b_button_down = is_action_active_any_joystick(b_button_left);
if (is_right_b_button_down) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_X;
}
if (is_left_b_button_down) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_Y;
}
const auto is_left_joystick_click_down = is_action_active(m_action_joystick_click, left_joystick);
const auto is_right_joystick_click_down = is_action_active(m_action_joystick_click, right_joystick);
if (is_left_joystick_click_down) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_THUMB;
}
if (is_right_joystick_click_down) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_THUMB;
}
const auto is_left_trigger_down = is_action_active(m_action_trigger, left_joystick);
const auto is_right_trigger_down = is_action_active(m_action_trigger, right_joystick);
if (is_left_trigger_down) {
state->Gamepad.bLeftTrigger = 255;
}
if (is_right_trigger_down) {
state->Gamepad.bRightTrigger = 255;
}
const auto is_right_grip_down = is_action_active(m_action_grip, right_joystick);
const auto is_left_grip_down = is_action_active(m_action_grip, left_joystick);
if (is_right_grip_down) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_SHOULDER;
}
if (is_left_grip_down) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_SHOULDER;
}
const auto is_dpad_up_down = is_action_active_any_joystick(m_action_dpad_up);
if (is_dpad_up_down) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP;
}
const auto is_dpad_right_down = is_action_active_any_joystick(m_action_dpad_right);
if (is_dpad_right_down) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT;
}
const auto is_dpad_down_down = is_action_active_any_joystick(m_action_dpad_down);
if (is_dpad_down_down) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN;
}
const auto is_dpad_left_down = is_action_active_any_joystick(m_action_dpad_left);
if (is_dpad_left_down) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT;
}
const auto left_joystick_axis = get_joystick_axis(left_joystick);
const auto right_joystick_axis = get_joystick_axis(right_joystick);
const auto true_left_joystick_axis = get_joystick_axis(m_left_joystick);
const auto true_right_joystick_axis = get_joystick_axis(m_right_joystick);
state->Gamepad.sThumbLX = (int16_t)std::clamp<float>(((float)state->Gamepad.sThumbLX + left_joystick_axis.x * 32767.0f), -32767.0f, 32767.0f);
state->Gamepad.sThumbLY = (int16_t)std::clamp<float>(((float)state->Gamepad.sThumbLY + left_joystick_axis.y * 32767.0f), -32767.0f, 32767.0f);
state->Gamepad.sThumbRX = (int16_t)std::clamp<float>(((float)state->Gamepad.sThumbRX + right_joystick_axis.x * 32767.0f), -32767.0f, 32767.0f);
state->Gamepad.sThumbRY = (int16_t)std::clamp<float>(((float)state->Gamepad.sThumbRY + right_joystick_axis.y * 32767.0f), -32767.0f, 32767.0f);
bool already_dpad_shifted{false};
bool true_left_joystick_as_dpad{false};
bool true_right_joystick_as_dpad{false};
if (m_dpad_gesture_state.direction != DPadGestureState::Direction::NONE) {
already_dpad_shifted = true;
if ((m_dpad_gesture_state.direction & DPadGestureState::Direction::UP) != 0) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP;
}
if ((m_dpad_gesture_state.direction & DPadGestureState::Direction::RIGHT) != 0) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT;
}
if ((m_dpad_gesture_state.direction & DPadGestureState::Direction::DOWN) != 0) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN;
}
if ((m_dpad_gesture_state.direction & DPadGestureState::Direction::LEFT) != 0) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT;
}
DPadMethod dpad_method = get_dpad_method();
if (dpad_method == DPadMethod::GESTURE_HEAD){
true_left_joystick_as_dpad = true;
}
else if (dpad_method == DPadMethod::GESTURE_HEAD_RIGHT) {
true_right_joystick_as_dpad = true;
}
std::scoped_lock _{m_dpad_gesture_state.mtx};
m_dpad_gesture_state.direction = DPadGestureState::Direction::NONE;
}
// Touching the thumbrest allows us to use the thumbstick as a dpad. Additional options are for controllers without capacitives/games that rely solely on DPad
if (!already_dpad_shifted && m_dpad_shifting->value()) {
bool button_touch_inactive{true};
bool thumbrest_check{false};
DPadMethod dpad_method = get_dpad_method();
if (dpad_method == DPadMethod::RIGHT_TOUCH) {
thumbrest_check = is_action_active_any_joystick(m_action_thumbrest_touch_right);
button_touch_inactive = !is_action_active_any_joystick(m_action_a_button_touch_right) && !is_action_active_any_joystick(m_action_b_button_touch_right);
}
if (dpad_method == DPadMethod::LEFT_TOUCH) {
thumbrest_check = is_action_active_any_joystick(m_action_thumbrest_touch_left);
button_touch_inactive = !is_action_active_any_joystick(m_action_a_button_touch_left) && !is_action_active_any_joystick(m_action_b_button_touch_left);
}
// Toggling UEVR menu using L3 + R3 has higher priority
const auto dpad_active = (is_right_joystick_click_down && (dpad_method == DPadMethod::RIGHT_JOYSTICK_CLICK) && (! is_left_joystick_click_down))
|| (is_left_joystick_click_down && (dpad_method == DPadMethod::LEFT_JOYSTICK_CLICK) && (! is_right_joystick_click_down))
|| (button_touch_inactive && thumbrest_check) || dpad_method == DPadMethod::LEFT_JOYSTICK || dpad_method == DPadMethod::RIGHT_JOYSTICK;
if (dpad_active) {
float ty{0.0f};
float tx{0.0f};
//SHORT ThumbY{0};
//SHORT ThumbX{0};
// If someone is accidentally touching both thumbrests while also moving a joystick, this will default to left joystick.
if (dpad_method == DPadMethod::RIGHT_TOUCH || dpad_method == DPadMethod::LEFT_JOYSTICK || dpad_method == DPadMethod::RIGHT_JOYSTICK_CLICK) {
//ThumbY = state->Gamepad.sThumbLY;
//ThumbX = state->Gamepad.sThumbLX;
ty = true_left_joystick_axis.y;
tx = true_left_joystick_axis.x;
true_left_joystick_as_dpad = true;
}
else if (dpad_method == DPadMethod::LEFT_TOUCH || dpad_method == DPadMethod::RIGHT_JOYSTICK || dpad_method == DPadMethod::LEFT_JOYSTICK_CLICK) {
//ThumbY = state->Gamepad.sThumbRY;
//ThumbX = state->Gamepad.sThumbRX;
ty = true_right_joystick_axis.y;
tx = true_right_joystick_axis.x;
true_right_joystick_as_dpad = true;
}
if (ty >= 0.5f) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_UP;
}
if (ty <= -0.5f) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_DOWN;
}
if (tx >= 0.5f) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_RIGHT;
}
if (tx <= -0.5f) {
state->Gamepad.wButtons |= XINPUT_GAMEPAD_DPAD_LEFT;
}
if(dpad_method == DPadMethod::RIGHT_JOYSTICK_CLICK)
{
state->Gamepad.wButtons &= ~XINPUT_GAMEPAD_RIGHT_THUMB;
}
else if(dpad_method == DPadMethod::LEFT_JOYSTICK_CLICK)
{
state->Gamepad.wButtons &= ~XINPUT_GAMEPAD_LEFT_THUMB;
}
}
}
// Zero out the thumbstick values
if (true_left_joystick_as_dpad) {
if (!wants_swap) {
state->Gamepad.sThumbLY = 0;
state->Gamepad.sThumbLX = 0;
} else {
state->Gamepad.sThumbRY = 0;
state->Gamepad.sThumbRX = 0;
}
}
else if (true_right_joystick_as_dpad) {
if (!wants_swap) {
state->Gamepad.sThumbRY = 0;
state->Gamepad.sThumbRX = 0;
} else {
state->Gamepad.sThumbLY = 0;
state->Gamepad.sThumbLX = 0;
}
}