···2121#include "rift_distortion.h"
22222323#include "math/m_relation_history.h"
2424+#include "math/m_clock_tracking.h"
2425#include "math/m_api.h"
2526#include "math/m_vec2.h"
2627#include "math/m_mathinclude.h" // IWYU pragma: keep
···182183 if (hmd->sensor_thread.initialized)
183184 os_thread_helper_stop_and_wait(&hmd->sensor_thread);
184185186186+ if (hmd->clock_tracker)
187187+ m_clock_windowed_skew_tracker_destroy(hmd->clock_tracker);
188188+185189 m_relation_history_destroy(&hmd->relation_hist);
186190187191 if (hmd->lens_distortions)
···297301 * We unpack them in the higher 21 bit values first and then shift
298302 * them down to the lower in order to get the sign bits correct.
299303 *
300300- * "Inspired" (code copied verbatim) from OpenHMD's rift driver
304304+ * Code taken/reformated from OpenHMD's rift driver
301305 */
302306static void
303307rift_decode_sample(const uint8_t *in, int32_t *out)
···367371 return 0;
368372 }
369373374374+ int64_t remote_sample_timestamp_ns = (int64_t)report.sample_timestamp * 1000;
375375+376376+ // ignore samples that are behind the latest current sample
377377+ if (remote_sample_timestamp_ns < hmd->last_sample_time_ns) {
378378+ return 0;
379379+ }
380380+381381+ hmd->last_sample_time_ns = remote_sample_timestamp_ns;
382382+383383+ m_clock_windowed_skew_tracker_push(hmd->clock_tracker, os_monotonic_get_ns(),
384384+ remote_sample_timestamp_ns);
385385+386386+ int64_t local_timestamp_ns;
387387+ // if we havent synchronized our clocks, just do nothing
388388+ if (!m_clock_windowed_skew_tracker_to_local(hmd->clock_tracker, remote_sample_timestamp_ns,
389389+ &local_timestamp_ns)) {
390390+ return 0;
391391+ }
392392+370393 struct dk2_sample_pack sample_pack = report.samples[report.num_samples >= 2 ? 1 : 0];
371394372395 int32_t accel_raw[3], gyro_raw[3];
···377400 rift_sample_to_imu_space(accel_raw, &accel);
378401 rift_sample_to_imu_space(gyro_raw, &gyro);
379402380380- uint64_t sample_timestamp_ns = (uint64_t)report.sample_timestamp * 1000;
381381-382382- HMD_INFO(hmd, "sample timestamp: %ld", sample_timestamp_ns);
383383- m_imu_3dof_update(&hmd->fusion, sample_timestamp_ns, &accel, &gyro);
403403+ m_imu_3dof_update(&hmd->fusion, local_timestamp_ns, &accel, &gyro);
384404385405 struct xrt_space_relation relation = XRT_SPACE_RELATION_ZERO;
386406 relation.relation_flags = (enum xrt_space_relation_flags)(XRT_SPACE_RELATION_ORIENTATION_TRACKED_BIT |
387407 XRT_SPACE_RELATION_ORIENTATION_VALID_BIT);
388408 relation.pose.orientation = hmd->fusion.rot;
389389- m_relation_history_push(hmd->relation_hist, &relation, os_monotonic_get_ns());
409409+ m_relation_history_push(hmd->relation_hist, &relation, local_timestamp_ns);
390410391411 break;
392412 }
···579599580600 hmd->extra_display_info.icd = info.lens_horizontal_separation_meters;
581601582582- for (int i = 0; i < 2; i++) {
583583- info.fov[i] = 93;
584584- }
602602+ // hardcode some "okay" values
603603+ info.fov[0] = 93;
604604+ info.fov[1] = 93;
585605586606 if (!u_device_setup_split_side_by_side(&hmd->base, &info)) {
587607 HMD_ERROR(hmd, "Failed to setup basic device info");
···619639 goto error;
620640 }
621641622622- m_imu_3dof_init(&hmd->fusion, 0);
642642+ m_imu_3dof_init(&hmd->fusion, M_IMU_3DOF_USE_GRAVITY_DUR_300MS);
643643+ hmd->clock_tracker = m_clock_windowed_skew_tracker_alloc(64);
623644624645 result = os_thread_helper_start(&hmd->sensor_thread, sensor_thread, hmd);
625646
+59-1
src/xrt/drivers/rift/rift_interface.h
···105105 RIFT_LENS_TYPE_B = 1,
106106};
107107108108+#define IN_REPORT_DK2 11
109109+108110#define CATMULL_COEFFICIENTS 11
109111#define CHROMATIC_ABBERATION_COEFFEICENT_COUNT 4
110112···193195 } RIFT_PACKED data;
194196} RIFT_PACKED;
195197196196-#define IN_REPORT_DK2 11
197198struct dk2_report_keepalive_mux
198199{
199200 uint16_t command;
···201202 uint16_t interval;
202203} RIFT_PACKED;
203204205205+enum rift_display_mode
206206+{
207207+ RIFT_DISPLAY_MODE_GLOBAL,
208208+ RIFT_DISPLAY_MODE_ROLLING_TOP_BOTTOM,
209209+ RIFT_DISPLAY_MODE_ROLLING_LEFT_RIGHT,
210210+ RIFT_DISPLAY_MODE_ROLLING_RIGHT_LEFT,
211211+};
212212+213213+enum rift_display_limit
214214+{
215215+ RIFT_DISPLAY_LIMIT_ACL_OFF = 0,
216216+ RIFT_DISPLAY_LIMIT_ACL_30 = 1,
217217+ RIFT_DISPLAY_LIMIT_ACL_25 = 2,
218218+ RIFT_DISPLAY_LIMIT_ACL_50 = 3,
219219+};
220220+221221+enum rift_display_flags
222222+{
223223+ RIFT_DISPLAY_USE_ROLLING = 1 << 6,
224224+ RIFT_DISPLAY_REVERSE_ROLLING = 1 << 7,
225225+ RIFT_DISPLAY_HIGH_BRIGHTNESS = 1 << 8,
226226+ RIFT_DISPLAY_SELF_REFRESH = 1 << 9,
227227+ RIFT_DISPLAY_READ_PIXEL = 1 << 10,
228228+ RIFT_DISPLAY_DIRECT_PENTILE = 1 << 11,
229229+};
230230+231231+struct rift_display_report
232232+{
233233+ uint16_t command_id;
234234+ // relative brightness setting independent of pixel persistence, only effective when high brightness is disabled
235235+ uint8_t brightness;
236236+ // a set of flags, ordered from LSB -> MSB
237237+ // - panel mode/shutter type (4 bits), read only, see rift_display_mode
238238+ // - current limit (2 bits), see rift_display_limit
239239+ // - use rolling (1 bit)
240240+ // - reverse rolling (1 bit), unavailable on released DK2 firmware for unknown reason
241241+ // - high brightness (1 bit), unavailable on released DK2 firmware for unpublished reason
242242+ // - self refresh (1 bit)
243243+ // - read pixel (1 bit)
244244+ // - direct pentile (1 bit)
245245+ uint32_t flags;
246246+ // the length of time in rows that the display is lit each frame, defaults to the full size of the display, full
247247+ // persistence
248248+ uint16_t persistence;
249249+ // the offset in rows from vsync that the panel is lit when using global shutter, no effect in rolling shutter,
250250+ // disabled on released DK2 firmware for unknown reason
251251+ uint16_t lighting_offset;
252252+ // the time in microseconds it is estimated for a pixel to settle to one value after it is set, read only
253253+ uint16_t pixel_settle;
254254+ // the number of rows including active area and blanking period used with persistence and lightingoffset, read
255255+ // only
256256+ uint16_t total_rows;
257257+} RIFT_PACKED;
258258+204259struct dk2_sensor_sample
205260{
206261 uint8_t data[8];
···328383329384 struct os_hid_device *hid_dev;
330385 struct os_thread_helper sensor_thread;
386386+ int64_t last_sample_time_ns;
387387+331388 struct m_imu_3dof fusion;
389389+ struct m_clock_windowed_skew_tracker *clock_tracker;
332390333391 int64_t last_keepalive_time;
334392 enum rift_variant variant;