mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 05:46:03 +00:00
feat: Cardboard VR mode with ARM64 SteamVR (#704)
* feat: add Cardboard mode with side-by-side lens-distorted eyes and 3DoF head tracking * fix: derive the Cardboard field of view from the panel and recentre fully when far from upright * feat: share the Cardboard head pose with the guest and add a SteamVR driver for it * feat: run SteamVR's compositor on Turnip/KGSL with a display-extension layer and a driver-direct HMD * feat: show SteamVR frames in Cardboard mode through a driver-side presenter and a fake direct-mode display * feat: install and start SteamVR for Cardboard mode from the session drawer * docs: describe Cardboard mode and the SteamVR path * feat: drive SteamVR hands from the game pad and keep the Cardboard headset out of standby * docs: record where the SteamVR dashboard stops * fix: map Cardboard head tracking to the screen's axes and keep turns about the real vertical as yaw * fix: give Cardboard its previous picture back when SteamVR stops * fix: report sem_otime from emulated SysV semaphores so SteamVR's web helper can start * fix: bridge SteamVR's shared semaphores across processes and keep Steam alive while SteamVR runs * feat: present SteamVR straight to DroidDeck's compositor at panel resolution * docs: update Cardboard VR notes for the dashboard, semaphore bridge and direct presenter * fix: keep Wine and FEX signal handlers in the VR layer; add semaphore tracing * fix: clear a stale SteamVR active marker on status * fix: clear only an old SteamVR active marker * fix: unblock SteamVR clients on Turnip and keep Steam's web helper alive across SteamVR runs * fix: judge shared waits before a submit's own signals and on Vulkan 1.0 devices * docs: record VR games, SteamVR Home and OpenXR results on the Thor * docs: add Cardboard VR learnings from the Thor bring-up * docs: add Cardboard VR screenshots from the Thor * fix: keep the VR layer's display stand-in to SteamVR runs and clear a leftover marker at session start * fix: report SteamVR start and stop failures from the helper's exit code
This commit is contained in:
1 parent
92d038b428
commit
f4b0ab6867
57 files changed
+17799
-15
No files matched your search
@@ -0,0 +1,27 @@
|
||||
Copyright (c) 2015, Valve Corporation
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
@@ -0,0 +1,666 @@
|
||||
// SteamVR driver for DroidDeck's Cardboard mode: one 3DoF HMD whose pose is the phone's rotation
|
||||
// sensor, read from the head pose file the app shares with the guest
|
||||
// (app/src/main/cpp/waylandcomp/src/headpose_shm.h, /run/droiddeck/droiddeck-headpose).
|
||||
//
|
||||
// The HMD renders an undistorted side-by-side pair: the app's Cardboard pass, in its side-by-side
|
||||
// mode, does the lens distortion for the viewer the user picked. So the projection here is exactly
|
||||
// the frustum that pass samples (tan_half_fov, eye_aspect from the pose file), and the distortion is
|
||||
// the identity.
|
||||
//
|
||||
// How the frames reach the screen is the open question this driver is for: see docs/cardboard-vr.md.
|
||||
// It exposes IVRDisplayComponent with the display "on the desktop" (extended mode) at the session's
|
||||
// size, and logs what vrserver / vrcompositor ask of it.
|
||||
#include "openvr_driver.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <atomic>
|
||||
#include <cstdarg>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../../../app/src/main/cpp/waylandcomp/src/headpose_shm.h"
|
||||
#include "presenter.h"
|
||||
|
||||
#define DD_EXPORT extern "C" __attribute__((visibility("default")))
|
||||
|
||||
using namespace vr;
|
||||
|
||||
namespace {
|
||||
|
||||
const char *const kSettingsSection = "driver_droiddeck";
|
||||
const char *const kPosePath = "/run/droiddeck/" DROIDDECK_HEADPOSE_FILE;
|
||||
|
||||
void Log(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
|
||||
void Log(const char *fmt, ...) {
|
||||
char buf[1024];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||
va_end(ap);
|
||||
if (VRDriverLog()) VRDriverLog()->Log(buf);
|
||||
fprintf(stderr, "[droiddeck] %s\n", buf);
|
||||
}
|
||||
|
||||
// The shared pose file, mapped read-only; reads follow the seqlock in headpose_shm.h.
|
||||
class PoseFile {
|
||||
public:
|
||||
bool Read(droiddeck_headpose *out) {
|
||||
if (!map_ && !Open()) return false;
|
||||
for (int tries = 0; tries < 8; tries++) {
|
||||
uint32_t a = __atomic_load_n(&map_->seq, __ATOMIC_ACQUIRE);
|
||||
if (a & 1u) continue;
|
||||
memcpy(out, (const void *)map_, sizeof(*out));
|
||||
__atomic_thread_fence(__ATOMIC_ACQUIRE);
|
||||
if (__atomic_load_n(&map_->seq, __ATOMIC_RELAXED) == a)
|
||||
return out->magic == DROIDDECK_HEADPOSE_MAGIC && out->version == DROIDDECK_HEADPOSE_VERSION;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
~PoseFile() { if (map_) munmap((void *)map_, sizeof(*map_)); }
|
||||
|
||||
private:
|
||||
bool Open() {
|
||||
int fd = open(kPosePath, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return false;
|
||||
void *p = mmap(nullptr, sizeof(droiddeck_headpose), PROT_READ, MAP_SHARED, fd, 0);
|
||||
close(fd);
|
||||
if (p == MAP_FAILED) return false;
|
||||
map_ = (volatile droiddeck_headpose *)p;
|
||||
Log("head pose file mapped: %s", kPosePath);
|
||||
return true;
|
||||
}
|
||||
volatile droiddeck_headpose *map_ = nullptr;
|
||||
};
|
||||
|
||||
HmdQuaternion_t QuatFromMatrix(const float m[9]) {
|
||||
// Row-major rotation -> quaternion (Shepperd's method).
|
||||
HmdQuaternion_t q;
|
||||
float tr = m[0] + m[4] + m[8];
|
||||
if (tr > 0.0f) {
|
||||
float s = sqrtf(tr + 1.0f) * 2.0f;
|
||||
q.w = 0.25f * s; q.x = (m[7] - m[5]) / s; q.y = (m[2] - m[6]) / s; q.z = (m[3] - m[1]) / s;
|
||||
} else if (m[0] > m[4] && m[0] > m[8]) {
|
||||
float s = sqrtf(1.0f + m[0] - m[4] - m[8]) * 2.0f;
|
||||
q.w = (m[7] - m[5]) / s; q.x = 0.25f * s; q.y = (m[1] + m[3]) / s; q.z = (m[2] + m[6]) / s;
|
||||
} else if (m[4] > m[8]) {
|
||||
float s = sqrtf(1.0f + m[4] - m[0] - m[8]) * 2.0f;
|
||||
q.w = (m[2] - m[6]) / s; q.x = (m[1] + m[3]) / s; q.y = 0.25f * s; q.z = (m[5] + m[7]) / s;
|
||||
} else {
|
||||
float s = sqrtf(1.0f + m[8] - m[0] - m[4]) * 2.0f;
|
||||
q.w = (m[3] - m[1]) / s; q.x = (m[2] + m[6]) / s; q.y = (m[5] + m[7]) / s; q.z = 0.25f * s;
|
||||
}
|
||||
return q;
|
||||
}
|
||||
|
||||
// IVRVirtualDisplay: the compositor renders the final frame into its own backbuffer and hands it
|
||||
// here instead of scanning it out. What the handle is on Linux is what this logs (experiment).
|
||||
class VirtualDisplay : public IVRVirtualDisplay {
|
||||
public:
|
||||
void Present(const PresentInfo_t *info, uint32_t size) override {
|
||||
frames_++;
|
||||
if (frames_ <= 5 || frames_ % 600 == 0)
|
||||
Log("virtual display Present #%llu: handle 0x%llx vsync %d frame %llu (info %u bytes)",
|
||||
(unsigned long long)frames_, (unsigned long long)info->backbufferTextureHandle, (int)info->vsync,
|
||||
(unsigned long long)info->nFrameId, size);
|
||||
last_ = std::chrono::steady_clock::now();
|
||||
}
|
||||
void WaitForPresent() override {}
|
||||
bool GetTimeSinceLastVsync(float *seconds, uint64_t *counter) override {
|
||||
// A free-running 60 Hz vsync.
|
||||
using namespace std::chrono;
|
||||
double t = duration<double>(steady_clock::now().time_since_epoch()).count();
|
||||
double period = 1.0 / 60.0;
|
||||
*seconds = (float)fmod(t, period);
|
||||
*counter = (uint64_t)(t / period);
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t frames_ = 0;
|
||||
std::chrono::steady_clock::time_point last_;
|
||||
};
|
||||
|
||||
// The vsync grid SteamVR paces against in driver-direct mode (Prop_DriverDirectModeSendsVsyncEvents):
|
||||
// a free-running 60 Hz tick, declared a little ahead of time (ALVR's announcer, alvr/server_openvr/src/vsync.rs).
|
||||
class Vsync {
|
||||
public:
|
||||
static constexpr std::chrono::nanoseconds kInterval{16666667};
|
||||
static constexpr std::chrono::microseconds kLead{2000};
|
||||
|
||||
static void Start() {
|
||||
if (running_.exchange(true)) return;
|
||||
thread_ = std::thread([] {
|
||||
auto next = std::chrono::steady_clock::now() + kInterval;
|
||||
while (running_) {
|
||||
auto announce = next - kLead;
|
||||
std::this_thread::sleep_until(announce);
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
double offset = std::chrono::duration<double>(announce - now).count();
|
||||
VRServerDriverHost()->VsyncEvent(offset);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
last_ = next;
|
||||
}
|
||||
next += kInterval;
|
||||
now = std::chrono::steady_clock::now();
|
||||
while (next <= now) next += kInterval;
|
||||
}
|
||||
});
|
||||
}
|
||||
static void Stop() {
|
||||
running_ = false;
|
||||
if (thread_.joinable()) thread_.join();
|
||||
}
|
||||
static std::chrono::steady_clock::time_point LastTick() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return last_;
|
||||
}
|
||||
|
||||
private:
|
||||
static inline std::atomic<bool> running_{false};
|
||||
static inline std::thread thread_;
|
||||
static inline std::mutex mutex_;
|
||||
static inline std::chrono::steady_clock::time_point last_{};
|
||||
};
|
||||
|
||||
// IVRDriverDirectModeComponent: SteamVR renders each eye into textures this driver allocates
|
||||
// (shared Vulkan images from SteamVR's IPC resource manager, dmabufs on Linux) and hands them
|
||||
// over per frame. This is how ALVR gets frames on Linux; it avoids the compositor's own direct mode,
|
||||
// which needs VK_KHR_display - something Turnip on KGSL refuses outright.
|
||||
class DirectMode : public IVRDriverDirectModeComponent {
|
||||
public:
|
||||
struct Set {
|
||||
SharedTextureHandle_t handles[3] = {};
|
||||
int fds[3] = {-1, -1, -1};
|
||||
uint32_t pid = 0, index = 0;
|
||||
SwapTextureSetDesc_t desc = {};
|
||||
};
|
||||
|
||||
void CreateSwapTextureSet(uint32_t pid, const SwapTextureSetDesc_t *d, SwapTextureSet_t *out) override {
|
||||
Log("CreateSwapTextureSet pid %u: format %u, %ux%u, %u samples", pid, d->nFormat, d->nWidth, d->nHeight, d->nSampleCount);
|
||||
Set set;
|
||||
set.pid = pid;
|
||||
set.desc = *d;
|
||||
const uint32_t usage = 0x1 /* TRANSFER_SRC */ | 0x4 /* SAMPLED */ | 0x80 /* INPUT_ATTACHMENT */;
|
||||
const uint32_t create = 0x8 /* MUTABLE_FORMAT */;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
SharedTextureHandle_t h = 0;
|
||||
if (!VRIPCResourceManager()->NewSharedVulkanImage(d->nFormat, d->nWidth, d->nHeight, true, false, true, 1, 1,
|
||||
create, usage, &h)) {
|
||||
Log("NewSharedVulkanImage failed");
|
||||
return;
|
||||
}
|
||||
uint64_t ipc = 0;
|
||||
VRIPCResourceManager()->RefResource(h, &ipc);
|
||||
int fd = -1;
|
||||
if (!VRIPCResourceManager()->ReceiveSharedFd(ipc, &fd)) Log("ReceiveSharedFd failed");
|
||||
set.handles[i] = h;
|
||||
set.fds[i] = fd;
|
||||
out->rSharedTextureHandles[i] = h;
|
||||
}
|
||||
out->unTextureFlags = 0;
|
||||
Log(" -> handles 0x%llx 0x%llx 0x%llx, fds %d %d %d", (unsigned long long)set.handles[0],
|
||||
(unsigned long long)set.handles[1], (unsigned long long)set.handles[2], set.fds[0], set.fds[1], set.fds[2]);
|
||||
droiddeck::Presenter::AddTextures(set.handles, set.fds, d->nFormat, d->nWidth, d->nHeight);
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
sets_.push_back(set);
|
||||
}
|
||||
|
||||
void DestroySwapTextureSet(SharedTextureHandle_t h) override {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
for (auto it = sets_.begin(); it != sets_.end(); ++it) {
|
||||
if (it->handles[0] == h || it->handles[1] == h || it->handles[2] == h) {
|
||||
Release(*it);
|
||||
sets_.erase(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DestroyAllSwapTextureSets(uint32_t pid) override {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
for (auto it = sets_.begin(); it != sets_.end();) {
|
||||
if (it->pid == pid) { Release(*it); it = sets_.erase(it); } else ++it;
|
||||
}
|
||||
}
|
||||
|
||||
void GetNextSwapTextureSetIndex(SharedTextureHandle_t handles[2], uint32_t (*indices)[2]) override {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
for (int e = 0; e < 2; e++) {
|
||||
Set *s = Find(handles[e]);
|
||||
if (!s) { (*indices)[e] = 0; continue; }
|
||||
s->index = (s->index + 1) % 3;
|
||||
(*indices)[e] = s->index;
|
||||
}
|
||||
}
|
||||
|
||||
void SubmitLayer(const SubmitLayerPerEye_t (&eyes)[2]) override {
|
||||
layers_++;
|
||||
// The first layer of a frame is the compositor's own picture; overlays after it are not drawn yet.
|
||||
if (layerInFrame_++ == 0) {
|
||||
for (int e = 0; e < 2; e++) {
|
||||
frame_.texture[e] = eyes[e].hTexture;
|
||||
frame_.bounds[e][0] = eyes[e].bounds.uMin; frame_.bounds[e][1] = eyes[e].bounds.vMin;
|
||||
frame_.bounds[e][2] = eyes[e].bounds.uMax; frame_.bounds[e][3] = eyes[e].bounds.vMax;
|
||||
}
|
||||
}
|
||||
if (frames_ < 3)
|
||||
Log("SubmitLayer: textures 0x%llx / 0x%llx, bounds %.2f,%.2f-%.2f,%.2f",
|
||||
(unsigned long long)eyes[0].hTexture, (unsigned long long)eyes[1].hTexture,
|
||||
eyes[0].bounds.uMin, eyes[0].bounds.vMin, eyes[0].bounds.uMax, eyes[0].bounds.vMax);
|
||||
}
|
||||
|
||||
void Present(SharedTextureHandle_t sync) override {
|
||||
frames_++;
|
||||
if (layerInFrame_) droiddeck::Presenter::Submit(frame_);
|
||||
if (layerInFrame_ != lastLayerCount_) {
|
||||
static int said;
|
||||
if (said++ < 20) Log("frame %llu: %d layer(s) (was %d)", (unsigned long long)frames_, layerInFrame_, lastLayerCount_);
|
||||
lastLayerCount_ = layerInFrame_;
|
||||
}
|
||||
layerInFrame_ = 0;
|
||||
if (frames_ <= 3 || frames_ % 600 == 0)
|
||||
Log("Present #%llu (%llu layers so far), sync 0x%llx", (unsigned long long)frames_,
|
||||
(unsigned long long)layers_, (unsigned long long)sync);
|
||||
}
|
||||
|
||||
void PostPresent(const Throttling_t *t) override {
|
||||
// Hold until the tick after the last one declared (ALVR's pacing): a slow frame costs one frame.
|
||||
auto last = Vsync::LastTick();
|
||||
auto until = last + Vsync::kInterval * (1 + (t ? t->nFramesToThrottle : 0));
|
||||
if (until > std::chrono::steady_clock::now()) std::this_thread::sleep_until(until);
|
||||
}
|
||||
|
||||
void GetFrameTiming(DriverDirectMode_FrameTiming *t) override { t->m_nReprojectionFlags = 0; }
|
||||
|
||||
private:
|
||||
Set *Find(SharedTextureHandle_t h) {
|
||||
for (auto &s : sets_)
|
||||
for (int i = 0; i < 3; i++)
|
||||
if (s.handles[i] == h) return &s;
|
||||
return nullptr;
|
||||
}
|
||||
static void Release(Set &s) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (s.handles[i]) droiddeck::Presenter::RemoveTexture(s.handles[i]);
|
||||
if (s.fds[i] >= 0) close(s.fds[i]);
|
||||
if (s.handles[i]) VRIPCResourceManager()->UnrefResource(s.handles[i]);
|
||||
}
|
||||
}
|
||||
|
||||
std::mutex mutex_;
|
||||
std::vector<Set> sets_;
|
||||
uint64_t frames_ = 0, layers_ = 0;
|
||||
int layerInFrame_ = 0, lastLayerCount_ = -1;
|
||||
droiddeck::EyeFrame frame_{};
|
||||
};
|
||||
|
||||
// The session's game pad, as the app publishes it for the guest: the absolute-state snapshot at the
|
||||
// head of fake input ring 0 (app/src/main/cpp/fakeinput_steam.cpp, FakeInputRingHeader), under a
|
||||
// seqlock. FAKE_EVDEV_MEMFD_PATHS names the rings in every session process's environment.
|
||||
struct PadState {
|
||||
uint32_t buttons = 0; // bit order of FakeInputWriter.BUTTON_MAP; bit 11 = the QAM button
|
||||
int16_t axes[8] = {}; // X, Y, RX, RY (+-32767), GAS = right trigger, BRAKE = left (0..255), HAT0X, HAT0Y
|
||||
};
|
||||
enum : uint32_t {
|
||||
PAD_A = 1u << 0, PAD_B = 1u << 1, PAD_X = 1u << 2, PAD_Y = 1u << 3, PAD_LB = 1u << 4, PAD_RB = 1u << 5,
|
||||
PAD_SELECT = 1u << 6, PAD_START = 1u << 7, PAD_L3 = 1u << 8, PAD_R3 = 1u << 9, PAD_GUIDE = 1u << 10,
|
||||
};
|
||||
|
||||
class PadRing {
|
||||
public:
|
||||
bool Read(PadState *out) {
|
||||
if (!map_ && !Open()) return false;
|
||||
for (int tries = 0; tries < 8; tries++) {
|
||||
uint64_t a = __atomic_load_n((const uint64_t *)(map_ + 32), __ATOMIC_ACQUIRE);
|
||||
if (a & 1u) continue;
|
||||
memcpy(&out->buttons, (const void *)(map_ + 40), 4);
|
||||
memcpy(out->axes, (const void *)(map_ + 44), 16);
|
||||
__atomic_thread_fence(__ATOMIC_ACQUIRE);
|
||||
if (__atomic_load_n((const uint64_t *)(map_ + 32), __ATOMIC_RELAXED) == a) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
~PadRing() { if (map_) munmap((void *)map_, 64); }
|
||||
|
||||
private:
|
||||
bool Open() {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
if (now < retry_) return false;
|
||||
retry_ = now + std::chrono::seconds(2);
|
||||
std::string path;
|
||||
if (const char *env = getenv("DROIDDECK_VR_PAD_RING")) path = env;
|
||||
if (path.empty()) {
|
||||
const char *paths = getenv("FAKE_EVDEV_MEMFD_PATHS"); // "0=/path;1=/path;..."
|
||||
if (paths && !strncmp(paths, "0=", 2)) path.assign(paths + 2, strcspn(paths + 2, ";"));
|
||||
}
|
||||
if (path.empty()) {
|
||||
if (const char *dir = getenv("BL_LAUNCH_DIR")) path = std::string(dir) + "/dev/fakeinput-rings/ring0";
|
||||
}
|
||||
if (path.empty()) return false;
|
||||
int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return false;
|
||||
void *p = mmap(nullptr, 64, PROT_READ, MAP_SHARED, fd, 0);
|
||||
close(fd);
|
||||
if (p == MAP_FAILED) return false;
|
||||
uint32_t magic;
|
||||
memcpy(&magic, p, 4);
|
||||
if (magic != 0x46494252u) { munmap(p, 64); return false; }
|
||||
map_ = (const volatile uint8_t *)p;
|
||||
Log("game pad ring mapped: %s", path.c_str());
|
||||
return true;
|
||||
}
|
||||
const volatile uint8_t *map_ = nullptr;
|
||||
std::chrono::steady_clock::time_point retry_{};
|
||||
};
|
||||
|
||||
// One hand: a Vive-profile controller driven by the game pad. 3DoF, so it is a gaze pointer: it sits
|
||||
// low in front of its side of the body and points where the head looks, and SteamVR's laser follows the
|
||||
// view. Right hand: right stick = trackpad, A = trackpad click, RT = trigger, RB = grip, Start = menu,
|
||||
// the Steam/guide button = system. Left: left stick, X, LT, LB, Select.
|
||||
class CardboardController : public ITrackedDeviceServerDriver {
|
||||
public:
|
||||
explicit CardboardController(bool right) : right_(right) {}
|
||||
|
||||
EVRInitError Activate(uint32_t id) override {
|
||||
id_ = id;
|
||||
PropertyContainerHandle_t c = VRProperties()->TrackedDeviceToPropertyContainer(id_);
|
||||
VRProperties()->SetStringProperty(c, Prop_ModelNumber_String, "DroidDeck Pad Hand");
|
||||
VRProperties()->SetStringProperty(c, Prop_ManufacturerName_String, "DroidDeck");
|
||||
VRProperties()->SetStringProperty(c, Prop_TrackingSystemName_String, "droiddeck");
|
||||
VRProperties()->SetStringProperty(c, Prop_RenderModelName_String, "generic_controller");
|
||||
VRProperties()->SetStringProperty(c, Prop_ControllerType_String, "vive_controller");
|
||||
VRProperties()->SetStringProperty(c, Prop_InputProfilePath_String, "{htc}/input/vive_controller_profile.json");
|
||||
VRProperties()->SetInt32Property(c, Prop_ControllerRoleHint_Int32,
|
||||
right_ ? TrackedControllerRole_RightHand : TrackedControllerRole_LeftHand);
|
||||
VRProperties()->SetInt32Property(c, Prop_DeviceClass_Int32, TrackedDeviceClass_Controller);
|
||||
auto in = VRDriverInput();
|
||||
in->CreateBooleanComponent(c, "/input/system/click", &system_);
|
||||
in->CreateBooleanComponent(c, "/input/application_menu/click", &menu_);
|
||||
in->CreateBooleanComponent(c, "/input/grip/click", &grip_);
|
||||
in->CreateBooleanComponent(c, "/input/trigger/click", &triggerClick_);
|
||||
in->CreateScalarComponent(c, "/input/trigger/value", &trigger_, VRScalarType_Absolute, VRScalarUnits_NormalizedOneSided);
|
||||
in->CreateScalarComponent(c, "/input/trackpad/x", &padX_, VRScalarType_Absolute, VRScalarUnits_NormalizedTwoSided);
|
||||
in->CreateScalarComponent(c, "/input/trackpad/y", &padY_, VRScalarType_Absolute, VRScalarUnits_NormalizedTwoSided);
|
||||
in->CreateBooleanComponent(c, "/input/trackpad/click", &padClick_);
|
||||
in->CreateBooleanComponent(c, "/input/trackpad/touch", &padTouch_);
|
||||
in->CreateHapticComponent(c, "/output/haptic", &haptic_);
|
||||
Log("%s hand activated (game pad)", right_ ? "right" : "left");
|
||||
return VRInitError_None;
|
||||
}
|
||||
void Deactivate() override { id_ = k_unTrackedDeviceIndexInvalid; }
|
||||
void EnterStandby() override {}
|
||||
void *GetComponent(const char *) override { return nullptr; }
|
||||
void DebugRequest(const char *, char *resp, uint32_t size) override { if (size) resp[0] = 0; }
|
||||
|
||||
DriverPose_t GetPose() override { return pose_; }
|
||||
|
||||
// From the driver's pose thread: the head's orientation and the pad.
|
||||
void Update(const HmdQuaternion_t &head, const PadState *pad) {
|
||||
if (id_ == k_unTrackedDeviceIndexInvalid) return;
|
||||
DriverPose_t p = {};
|
||||
p.qWorldFromDriverRotation.w = 1;
|
||||
p.qDriverFromHeadRotation.w = 1;
|
||||
p.qRotation = head;
|
||||
// Low and in front of its side, in head space, then into the room with the head's rotation.
|
||||
const double off[3] = {right_ ? 0.18 : -0.18, -0.35, -0.30};
|
||||
double m[3][3];
|
||||
const double w = head.w, x = head.x, y = head.y, z = head.z;
|
||||
m[0][0] = 1 - 2 * (y * y + z * z); m[0][1] = 2 * (x * y - z * w); m[0][2] = 2 * (x * z + y * w);
|
||||
m[1][0] = 2 * (x * y + z * w); m[1][1] = 1 - 2 * (x * x + z * z); m[1][2] = 2 * (y * z - x * w);
|
||||
m[2][0] = 2 * (x * z - y * w); m[2][1] = 2 * (y * z + x * w); m[2][2] = 1 - 2 * (x * x + y * y);
|
||||
for (int i = 0; i < 3; i++) p.vecPosition[i] = (i == 1 ? 1.6 : 0.0) + m[i][0] * off[0] + m[i][1] * off[1] + m[i][2] * off[2];
|
||||
p.poseIsValid = true;
|
||||
p.result = TrackingResult_Running_OK;
|
||||
p.deviceIsConnected = true;
|
||||
pose_ = p;
|
||||
VRServerDriverHost()->TrackedDevicePoseUpdated(id_, p, sizeof(p));
|
||||
if (!pad) return;
|
||||
auto in = VRDriverInput();
|
||||
const uint32_t b = pad->buttons;
|
||||
const float stickX = pad->axes[right_ ? 2 : 0] / 32767.0f, stickY = -pad->axes[right_ ? 3 : 1] / 32767.0f;
|
||||
const float trig = pad->axes[right_ ? 4 : 5] / 255.0f;
|
||||
const bool touch = stickX * stickX + stickY * stickY > 0.01f;
|
||||
in->UpdateBooleanComponent(system_, right_ && (b & PAD_GUIDE), 0);
|
||||
in->UpdateBooleanComponent(menu_, (b & (right_ ? PAD_START : PAD_SELECT)) != 0, 0);
|
||||
in->UpdateBooleanComponent(grip_, (b & (right_ ? PAD_RB : PAD_LB)) != 0, 0);
|
||||
in->UpdateScalarComponent(trigger_, trig, 0);
|
||||
in->UpdateBooleanComponent(triggerClick_, trig > 0.6f, 0);
|
||||
in->UpdateScalarComponent(padX_, touch ? stickX : 0.0f, 0);
|
||||
in->UpdateScalarComponent(padY_, touch ? stickY : 0.0f, 0);
|
||||
in->UpdateBooleanComponent(padTouch_, touch || (b & (right_ ? PAD_A : PAD_X)), 0);
|
||||
in->UpdateBooleanComponent(padClick_, (b & (right_ ? (PAD_A | PAD_R3) : (PAD_X | PAD_L3))) != 0, 0);
|
||||
}
|
||||
|
||||
private:
|
||||
bool right_;
|
||||
uint32_t id_ = k_unTrackedDeviceIndexInvalid;
|
||||
DriverPose_t pose_ = {};
|
||||
VRInputComponentHandle_t system_ = 0, menu_ = 0, grip_ = 0, triggerClick_ = 0, trigger_ = 0, padX_ = 0, padY_ = 0,
|
||||
padClick_ = 0, padTouch_ = 0, haptic_ = 0;
|
||||
};
|
||||
|
||||
class CardboardHmd : public ITrackedDeviceServerDriver, public IVRDisplayComponent {
|
||||
public:
|
||||
EVRInitError Activate(uint32_t unObjectId) override {
|
||||
id_ = unObjectId;
|
||||
PropertyContainerHandle_t c = VRProperties()->TrackedDeviceToPropertyContainer(id_);
|
||||
width_ = VRSettings()->GetInt32(kSettingsSection, "windowWidth");
|
||||
height_ = VRSettings()->GetInt32(kSettingsSection, "windowHeight");
|
||||
if (width_ <= 0) width_ = 1920;
|
||||
if (height_ <= 0) height_ = 1080;
|
||||
float hz = VRSettings()->GetFloat(kSettingsSection, "displayFrequency");
|
||||
if (hz <= 0.0f) hz = 60.0f;
|
||||
// Until the app has drawn an eye: the Cardboard pass's defaults for a 1920x1080 panel.
|
||||
tanHalf_ = 1.0f;
|
||||
eyeAspect_ = (width_ * 0.5f) / height_;
|
||||
droiddeck_headpose hp;
|
||||
if (pose_.Read(&hp) && hp.tan_half_fov > 0.0f) {
|
||||
tanHalf_ = hp.tan_half_fov;
|
||||
if (hp.eye_aspect > 0.0f) eyeAspect_ = hp.eye_aspect;
|
||||
}
|
||||
VRProperties()->SetStringProperty(c, Prop_ModelNumber_String, "DroidDeck Cardboard");
|
||||
VRProperties()->SetStringProperty(c, Prop_ManufacturerName_String, "DroidDeck");
|
||||
VRProperties()->SetStringProperty(c, Prop_TrackingSystemName_String, "droiddeck");
|
||||
VRProperties()->SetFloatProperty(c, Prop_UserIpdMeters_Float, 0.064f);
|
||||
VRProperties()->SetFloatProperty(c, Prop_UserHeadToEyeDepthMeters_Float, 0.0f);
|
||||
VRProperties()->SetFloatProperty(c, Prop_DisplayFrequency_Float, hz);
|
||||
VRProperties()->SetFloatProperty(c, Prop_SecondsFromVsyncToPhotons_Float, 0.011f);
|
||||
VRProperties()->SetUint64Property(c, Prop_CurrentUniverseId_Uint64, 2);
|
||||
// Off the desktop: with a direct mode component that sends SteamVR's compositor down the
|
||||
// driver-direct path; on it, the compositor opens its own direct mode window (VK_KHR_display).
|
||||
VRProperties()->SetBoolProperty(c, Prop_IsOnDesktop_Bool, false);
|
||||
VRProperties()->SetBoolProperty(c, Prop_DisplayDebugMode_Bool, false);
|
||||
directMode_ = VRSettings()->GetBool(kSettingsSection, "directMode");
|
||||
VRProperties()->SetBoolProperty(c, Prop_HasDriverDirectModeComponent_Bool, directMode_);
|
||||
if (directMode_) {
|
||||
VRProperties()->SetBoolProperty(c, Prop_DriverDirectModeSendsVsyncEvents_Bool, true);
|
||||
VRProperties()->SetBoolProperty(c, Prop_Hmd_AllowsClientToControlTextureIndex, true);
|
||||
}
|
||||
VRProperties()->SetBoolProperty(c, Prop_HasDisplayComponent_Bool, true);
|
||||
virtualDisplay_ = VRSettings()->GetBool(kSettingsSection, "virtualDisplay");
|
||||
VRProperties()->SetBoolProperty(c, Prop_HasVirtualDisplayComponent_Bool, virtualDisplay_);
|
||||
// A proximity sensor that reads "worn" while Cardboard mode is on: otherwise SteamVR sees a headset
|
||||
// lying still (a phone on a table between sessions) and puts the display in standby, after which
|
||||
// the compositor submits no layers at all.
|
||||
VRProperties()->SetBoolProperty(c, Prop_ContainsProximitySensor_Bool, true);
|
||||
VRDriverInput()->CreateBooleanComponent(c, "/proximity", &proximity_);
|
||||
VRDriverInput()->UpdateBooleanComponent(proximity_, true, 0);
|
||||
Log("HMD activated: %dx%d at %.0f Hz, eye tan(half fov) %.3f, eye aspect %.3f", width_, height_, hz, tanHalf_, eyeAspect_);
|
||||
|
||||
running_ = true;
|
||||
thread_ = std::thread([this] { PoseLoop(); });
|
||||
if (directMode_) Vsync::Start();
|
||||
return VRInitError_None;
|
||||
}
|
||||
|
||||
void Deactivate() override {
|
||||
Vsync::Stop();
|
||||
droiddeck::Presenter::Shutdown();
|
||||
running_ = false;
|
||||
if (thread_.joinable()) thread_.join();
|
||||
id_ = k_unTrackedDeviceIndexInvalid;
|
||||
}
|
||||
|
||||
void EnterStandby() override {}
|
||||
|
||||
void *GetComponent(const char *name) override {
|
||||
Log("component asked for: %s", name);
|
||||
if (!strncmp(name, "IVRDisplayComponent_", 20)) return static_cast<IVRDisplayComponent *>(this);
|
||||
if (virtualDisplay_ && !strcmp(name, IVRVirtualDisplay_Version)) return static_cast<IVRVirtualDisplay *>(&vd_);
|
||||
if (directMode_ && !strncmp(name, "IVRDriverDirectModeComponent_", 29)) return static_cast<IVRDriverDirectModeComponent *>(&dm_);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void DebugRequest(const char *, char *resp, uint32_t size) override { if (size) resp[0] = 0; }
|
||||
|
||||
DriverPose_t GetPose() override {
|
||||
DriverPose_t p = {};
|
||||
p.qWorldFromDriverRotation.w = 1;
|
||||
p.qDriverFromHeadRotation.w = 1;
|
||||
p.qRotation.w = 1;
|
||||
p.vecPosition[1] = 1.6; // standing eye height: 3DoF, the head never moves
|
||||
droiddeck_headpose hp;
|
||||
bool ok = pose_.Read(&hp);
|
||||
if (ok) {
|
||||
p.qRotation = QuatFromMatrix(hp.rot);
|
||||
p.poseTimeOffset = 0;
|
||||
if (hp.tan_half_fov > 0.0f) tanHalf_ = hp.tan_half_fov;
|
||||
if (hp.eye_aspect > 0.0f) eyeAspect_ = hp.eye_aspect;
|
||||
}
|
||||
p.poseIsValid = true;
|
||||
p.result = TrackingResult_Running_OK;
|
||||
p.deviceIsConnected = true;
|
||||
return p;
|
||||
}
|
||||
|
||||
// ---- IVRDisplayComponent
|
||||
void GetWindowBounds(int32_t *x, int32_t *y, uint32_t *w, uint32_t *h) override {
|
||||
*x = 0; *y = 0; *w = (uint32_t)width_; *h = (uint32_t)height_;
|
||||
}
|
||||
bool IsDisplayOnDesktop() override { return !directMode_; }
|
||||
bool IsDisplayRealDisplay() override { return false; }
|
||||
void GetRecommendedRenderTargetSize(uint32_t *w, uint32_t *h) override {
|
||||
// Rendered a little larger than the panel: the lens pass magnifies the middle.
|
||||
*w = (uint32_t)(width_ / 2 * 1.4f);
|
||||
*h = (uint32_t)(height_ * 1.4f);
|
||||
}
|
||||
void GetEyeOutputViewport(EVREye eye, uint32_t *x, uint32_t *y, uint32_t *w, uint32_t *h) override {
|
||||
*y = 0; *w = (uint32_t)width_ / 2; *h = (uint32_t)height_;
|
||||
*x = eye == Eye_Left ? 0 : (uint32_t)width_ / 2;
|
||||
}
|
||||
void GetProjectionRaw(EVREye, float *left, float *right, float *top, float *bottom) override {
|
||||
// Exactly what the Cardboard pass samples from each half of a side-by-side frame.
|
||||
*left = -tanHalf_ * eyeAspect_; *right = tanHalf_ * eyeAspect_;
|
||||
*top = -tanHalf_; *bottom = tanHalf_;
|
||||
}
|
||||
DistortionCoordinates_t ComputeDistortion(EVREye, float u, float v) override {
|
||||
DistortionCoordinates_t d;
|
||||
d.rfRed[0] = d.rfGreen[0] = d.rfBlue[0] = u;
|
||||
d.rfRed[1] = d.rfGreen[1] = d.rfBlue[1] = v;
|
||||
return d;
|
||||
}
|
||||
bool ComputeInverseDistortion(HmdVector2_t *result, EVREye, uint32_t, float u, float v) override {
|
||||
result->v[0] = u; result->v[1] = v;
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
void PoseLoop() {
|
||||
uint32_t lastSeq = 0;
|
||||
auto lastSent = std::chrono::steady_clock::now();
|
||||
while (running_) {
|
||||
droiddeck_headpose hp;
|
||||
bool fresh = pose_.Read(&hp) && hp.seq != lastSeq;
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
// A new sample, or a heartbeat so SteamVR never thinks the HMD went away.
|
||||
PadState pad;
|
||||
const bool havePad = pad_.Read(&pad);
|
||||
const bool padChanged = havePad && (pad.buttons != lastPad_.buttons || memcmp(pad.axes, lastPad_.axes, sizeof(pad.axes)));
|
||||
if ((fresh || padChanged || now - lastSent > std::chrono::milliseconds(100)) && id_ != k_unTrackedDeviceIndexInvalid) {
|
||||
if (fresh) {
|
||||
lastSeq = hp.seq;
|
||||
const bool worn = (hp.flags & DROIDDECK_HEADPOSE_ENABLED) != 0;
|
||||
if (worn != worn_) {
|
||||
worn_ = worn;
|
||||
VRDriverInput()->UpdateBooleanComponent(proximity_, worn, 0);
|
||||
Log("headset %s (Cardboard mode %s)", worn ? "worn" : "taken off", worn ? "on" : "off");
|
||||
}
|
||||
}
|
||||
DriverPose_t head = GetPose();
|
||||
VRServerDriverHost()->TrackedDevicePoseUpdated(id_, head, sizeof(DriverPose_t));
|
||||
for (CardboardController *hand : hands_) if (hand) hand->Update(head.qRotation, havePad ? &pad : nullptr);
|
||||
if (havePad) lastPad_ = pad;
|
||||
lastSent = now;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(2));
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t id_ = k_unTrackedDeviceIndexInvalid;
|
||||
int32_t width_ = 1920, height_ = 1080;
|
||||
float tanHalf_ = 1.0f, eyeAspect_ = 0.889f;
|
||||
public:
|
||||
void SetHands(CardboardController *left, CardboardController *right) { hands_[0] = left; hands_[1] = right; }
|
||||
|
||||
private:
|
||||
PoseFile pose_;
|
||||
PadRing pad_;
|
||||
PadState lastPad_;
|
||||
VRInputComponentHandle_t proximity_ = 0;
|
||||
bool worn_ = true;
|
||||
CardboardController *hands_[2] = {nullptr, nullptr};
|
||||
bool virtualDisplay_ = false, directMode_ = false;
|
||||
VirtualDisplay vd_;
|
||||
DirectMode dm_;
|
||||
std::atomic<bool> running_{false};
|
||||
std::thread thread_;
|
||||
};
|
||||
|
||||
class Provider : public IServerTrackedDeviceProvider {
|
||||
public:
|
||||
EVRInitError Init(IVRDriverContext *ctx) override {
|
||||
VR_INIT_SERVER_DRIVER_CONTEXT(ctx);
|
||||
Log("DroidDeck Cardboard driver loaded");
|
||||
if (!VRServerDriverHost()->TrackedDeviceAdded("droiddeck-cardboard-hmd", TrackedDeviceClass_HMD, &hmd_))
|
||||
Log("TrackedDeviceAdded failed");
|
||||
if (VRSettings()->GetBool(kSettingsSection, "gamepadControllers")) {
|
||||
VRServerDriverHost()->TrackedDeviceAdded("droiddeck-pad-left", TrackedDeviceClass_Controller, &left_);
|
||||
VRServerDriverHost()->TrackedDeviceAdded("droiddeck-pad-right", TrackedDeviceClass_Controller, &right_);
|
||||
hmd_.SetHands(&left_, &right_);
|
||||
}
|
||||
return VRInitError_None;
|
||||
}
|
||||
void Cleanup() override { VR_CLEANUP_SERVER_DRIVER_CONTEXT(); }
|
||||
const char *const *GetInterfaceVersions() override { return k_InterfaceVersions; }
|
||||
void RunFrame() override {
|
||||
VREvent_t e;
|
||||
while (VRServerDriverHost()->PollNextEvent(&e, sizeof(e))) {}
|
||||
}
|
||||
bool ShouldBlockStandbyMode() override { return false; }
|
||||
void EnterStandby() override {}
|
||||
void LeaveStandby() override {}
|
||||
|
||||
private:
|
||||
CardboardHmd hmd_;
|
||||
CardboardController left_{false}, right_{true};
|
||||
};
|
||||
|
||||
Provider g_provider;
|
||||
|
||||
} // namespace
|
||||
|
||||
DD_EXPORT void *HmdDriverFactory(const char *interfaceName, int *returnCode) {
|
||||
if (!strcmp(interfaceName, IServerTrackedDeviceProvider_Version)) return &g_provider;
|
||||
if (returnCode) *returnCode = VRInitError_Init_InterfaceNotFound;
|
||||
return nullptr;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,607 @@
|
||||
// See presenter.h.
|
||||
#define VK_NO_PROTOTYPES
|
||||
#include <vulkan/vulkan_core.h>
|
||||
typedef struct _XDisplay Display;
|
||||
typedef unsigned long Window;
|
||||
typedef unsigned long VisualID;
|
||||
#include <vulkan/vulkan_xlib.h>
|
||||
#include <wayland-client.h>
|
||||
#include <vulkan/vulkan_wayland.h>
|
||||
#include "xdg-shell-client-protocol.h"
|
||||
|
||||
#include "presenter.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <dlfcn.h>
|
||||
#include <fcntl.h>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
#include "openvr_driver.h"
|
||||
|
||||
namespace droiddeck {
|
||||
namespace {
|
||||
|
||||
void Log(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
|
||||
void Log(const char *fmt, ...) {
|
||||
char buf[1024];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||
va_end(ap);
|
||||
if (vr::VRDriverLog()) vr::VRDriverLog()->Log(buf);
|
||||
fprintf(stderr, "[droiddeck] %s\n", buf);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- Xlib, loaded at runtime
|
||||
|
||||
struct X11 {
|
||||
void *lib = nullptr;
|
||||
Display *(*OpenDisplay)(const char *);
|
||||
Window (*DefaultRootWindow)(Display *);
|
||||
int (*DefaultScreen)(Display *);
|
||||
int (*DisplayWidth)(Display *, int);
|
||||
int (*DisplayHeight)(Display *, int);
|
||||
Window (*CreateSimpleWindow)(Display *, Window, int, int, unsigned, unsigned, unsigned, unsigned long, unsigned long);
|
||||
unsigned long (*InternAtom)(Display *, const char *, int);
|
||||
int (*ChangeProperty)(Display *, Window, unsigned long, unsigned long, int, int, const unsigned char *, int);
|
||||
int (*MapWindow)(Display *, Window);
|
||||
int (*StoreName)(Display *, Window, const char *);
|
||||
int (*Flush)(Display *);
|
||||
|
||||
bool Load() {
|
||||
lib = dlopen("libX11.so.6", RTLD_NOW | RTLD_LOCAL);
|
||||
if (!lib) return false;
|
||||
#define X(name) if (!(*(void **)&name = dlsym(lib, "X" #name))) return false;
|
||||
X(OpenDisplay) X(DefaultRootWindow) X(DefaultScreen) X(DisplayWidth) X(DisplayHeight)
|
||||
X(CreateSimpleWindow) X(InternAtom) X(ChangeProperty) X(MapWindow) X(StoreName) X(Flush)
|
||||
#undef X
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------- Vulkan, loaded at runtime
|
||||
|
||||
#define VK_GLOBAL(X) X(CreateInstance)
|
||||
#define VK_INSTANCE(X) \
|
||||
X(DestroyInstance) X(EnumeratePhysicalDevices) X(GetPhysicalDeviceQueueFamilyProperties) \
|
||||
X(GetPhysicalDeviceMemoryProperties) X(CreateDevice) X(GetDeviceProcAddr) X(CreateXlibSurfaceKHR) \
|
||||
X(CreateWaylandSurfaceKHR) \
|
||||
X(GetPhysicalDeviceSurfaceSupportKHR) X(GetPhysicalDeviceSurfaceCapabilitiesKHR) \
|
||||
X(GetPhysicalDeviceSurfaceFormatsKHR) X(GetPhysicalDeviceSurfacePresentModesKHR) X(GetPhysicalDeviceProperties)
|
||||
#define VK_DEVICE(X) \
|
||||
X(GetDeviceQueue) X(CreateSwapchainKHR) X(DestroySwapchainKHR) X(GetSwapchainImagesKHR) X(AcquireNextImageKHR) \
|
||||
X(QueuePresentKHR) X(CreateCommandPool) X(AllocateCommandBuffers) X(BeginCommandBuffer) X(EndCommandBuffer) \
|
||||
X(ResetCommandBuffer) X(CmdPipelineBarrier) X(CmdBlitImage) X(CmdClearColorImage) X(QueueSubmit) X(CreateFence) \
|
||||
X(WaitForFences) X(ResetFences) X(CreateSemaphore) X(CreateImage) X(DestroyImage) X(GetImageMemoryRequirements) \
|
||||
X(AllocateMemory) X(FreeMemory) X(BindImageMemory) X(DeviceWaitIdle)
|
||||
|
||||
struct Vk {
|
||||
void *lib = nullptr;
|
||||
PFN_vkGetInstanceProcAddr GetInstanceProcAddr = nullptr;
|
||||
#define X(n) PFN_vk##n n = nullptr;
|
||||
VK_GLOBAL(X) VK_INSTANCE(X) VK_DEVICE(X)
|
||||
#undef X
|
||||
};
|
||||
|
||||
struct Imported {
|
||||
uint64_t handle;
|
||||
VkImage image = VK_NULL_HANDLE;
|
||||
VkDeviceMemory memory = VK_NULL_HANDLE;
|
||||
uint32_t width = 0, height = 0;
|
||||
};
|
||||
|
||||
struct PendingImport {
|
||||
uint64_t handle;
|
||||
int fd;
|
||||
uint32_t format, width, height;
|
||||
};
|
||||
|
||||
VkFormat UnormOf(uint32_t f) {
|
||||
switch (f) {
|
||||
case VK_FORMAT_R8G8B8A8_SRGB: return VK_FORMAT_R8G8B8A8_UNORM;
|
||||
case VK_FORMAT_B8G8R8A8_SRGB: return VK_FORMAT_B8G8R8A8_UNORM;
|
||||
default: return (VkFormat)f;
|
||||
}
|
||||
}
|
||||
|
||||
class Impl {
|
||||
public:
|
||||
static Impl &Get() {
|
||||
static Impl impl;
|
||||
return impl;
|
||||
}
|
||||
|
||||
void Add(const uint64_t handles[3], const int fds[3], uint32_t format, uint32_t w, uint32_t h) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (fds[i] < 0) continue;
|
||||
int fd = dup(fds[i]); // the import owns its fd; the driver keeps its own
|
||||
if (fd >= 0) pending_.push_back({handles[i], fd, format, w, h});
|
||||
}
|
||||
EnsureThread();
|
||||
cv_.notify_one();
|
||||
}
|
||||
|
||||
void Remove(uint64_t handle) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
removals_.push_back(handle);
|
||||
cv_.notify_one();
|
||||
}
|
||||
|
||||
void Submit(const EyeFrame &f) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
frame_ = f;
|
||||
frameAt_ = std::chrono::steady_clock::now();
|
||||
haveFrame_ = true;
|
||||
submitted_++;
|
||||
cv_.notify_one();
|
||||
}
|
||||
|
||||
void Shutdown() {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
exit_ = true;
|
||||
cv_.notify_one();
|
||||
}
|
||||
if (thread_.joinable()) thread_.join();
|
||||
}
|
||||
|
||||
private:
|
||||
void EnsureThread() {
|
||||
if (!started_) {
|
||||
started_ = true;
|
||||
thread_ = std::thread([this] { Run(); });
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ the presenting thread
|
||||
|
||||
void Run() {
|
||||
const char *mode = getenv("DROIDDECK_VR_PRESENT");
|
||||
wayland_ = !(mode && !strcmp(mode, "x11")) && InitWayland();
|
||||
if (!wayland_ && !InitWindow()) {
|
||||
Log("presenter: unavailable, frames are not shown");
|
||||
return;
|
||||
}
|
||||
if (!InitVulkan()) {
|
||||
Log("presenter: unavailable, frames are not shown");
|
||||
return;
|
||||
}
|
||||
uint64_t shown = 0;
|
||||
auto lastLog = std::chrono::steady_clock::now();
|
||||
while (true) {
|
||||
EyeFrame f;
|
||||
std::chrono::steady_clock::time_point at;
|
||||
std::vector<PendingImport> imports;
|
||||
std::vector<uint64_t> removals;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cv_.wait(lock, [this] { return exit_ || haveFrame_ || !pending_.empty() || !removals_.empty(); });
|
||||
if (exit_) break;
|
||||
imports.swap(pending_);
|
||||
removals.swap(removals_);
|
||||
f = frame_;
|
||||
at = frameAt_;
|
||||
bool draw = haveFrame_;
|
||||
haveFrame_ = false;
|
||||
if (!draw) f.texture[0] = f.texture[1] = 0;
|
||||
}
|
||||
if (wl_) { wl_display_dispatch_pending(wl_); wl_display_flush(wl_); }
|
||||
for (auto &p : imports) Import(p);
|
||||
for (auto h : removals) Forget(h);
|
||||
// SteamVR hands the textures over at Present with no fence the driver can wait on: give the
|
||||
// compositor's GPU work a moment to land before copying, as ALVR does (a torn or half-drawn
|
||||
// frame otherwise).
|
||||
if (f.texture[0]) std::this_thread::sleep_until(at + guard_);
|
||||
if (f.texture[0] && Draw(f)) shown++;
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
if (now - lastLog > std::chrono::seconds(10)) {
|
||||
Log("presenter: %llu frames shown, %llu submitted (last 10 s)", (unsigned long long)shown,
|
||||
(unsigned long long)submitted_.exchange(0));
|
||||
shown = 0;
|
||||
lastLog = now;
|
||||
}
|
||||
}
|
||||
if (dev_) vk_.DeviceWaitIdle(dev_);
|
||||
}
|
||||
|
||||
std::chrono::microseconds guard_{4000};
|
||||
|
||||
// ------------------------------------------------------------ Wayland: straight to DroidDeck's compositor
|
||||
// The compositor the app runs (wayland-0 in the session's runtime dir, beside gamescope's socket)
|
||||
// takes a fullscreen toplevel at the panel's size: one hop and one scale fewer than the X11 overlay
|
||||
// through gamescope, whose output is the session's (often smaller) resolution.
|
||||
struct wl_display *wl_ = nullptr;
|
||||
struct wl_compositor *wlComp_ = nullptr;
|
||||
struct xdg_wm_base *wm_ = nullptr;
|
||||
struct wl_surface *wlSurf_ = nullptr;
|
||||
struct xdg_surface *xdgSurf_ = nullptr;
|
||||
struct xdg_toplevel *toplevel_ = nullptr;
|
||||
bool wayland_ = false, configured_ = false;
|
||||
int32_t cfgW_ = 0, cfgH_ = 0;
|
||||
|
||||
static void OnGlobal(void *data, struct wl_registry *reg, uint32_t name, const char *iface, uint32_t version) {
|
||||
Impl *self = (Impl *)data;
|
||||
if (!strcmp(iface, wl_compositor_interface.name))
|
||||
self->wlComp_ = (struct wl_compositor *)wl_registry_bind(reg, name, &wl_compositor_interface, version < 4 ? version : 4);
|
||||
else if (!strcmp(iface, xdg_wm_base_interface.name))
|
||||
self->wm_ = (struct xdg_wm_base *)wl_registry_bind(reg, name, &xdg_wm_base_interface, 1);
|
||||
}
|
||||
static void OnGlobalRemove(void *, struct wl_registry *, uint32_t) {}
|
||||
static void OnPing(void *, struct xdg_wm_base *wm, uint32_t serial) { xdg_wm_base_pong(wm, serial); }
|
||||
static void OnSurfaceConfigure(void *data, struct xdg_surface *xs, uint32_t serial) {
|
||||
xdg_surface_ack_configure(xs, serial);
|
||||
((Impl *)data)->configured_ = true;
|
||||
}
|
||||
static void OnToplevelConfigure(void *data, struct xdg_toplevel *, int32_t w, int32_t h, struct wl_array *) {
|
||||
Impl *self = (Impl *)data;
|
||||
if (w > 0 && h > 0) { self->cfgW_ = w; self->cfgH_ = h; }
|
||||
}
|
||||
static void OnToplevelClose(void *, struct xdg_toplevel *) {}
|
||||
|
||||
bool InitWayland() {
|
||||
const char *name = getenv("DROIDDECK_VR_WAYLAND_DISPLAY");
|
||||
if (!name || !*name) name = "wayland-0";
|
||||
wl_ = wl_display_connect(name);
|
||||
if (!wl_) { Log("presenter: no Wayland display %s; using the X11 overlay", name); return false; }
|
||||
static const struct wl_registry_listener regListener = {OnGlobal, OnGlobalRemove};
|
||||
struct wl_registry *reg = wl_display_get_registry(wl_);
|
||||
wl_registry_add_listener(reg, ®Listener, this);
|
||||
wl_display_roundtrip(wl_);
|
||||
if (!wlComp_ || !wm_) { Log("presenter: %s lacks wl_compositor or xdg_wm_base", name); return false; }
|
||||
static const struct xdg_wm_base_listener wmListener = {OnPing};
|
||||
xdg_wm_base_add_listener(wm_, &wmListener, this);
|
||||
wlSurf_ = wl_compositor_create_surface(wlComp_);
|
||||
xdgSurf_ = xdg_wm_base_get_xdg_surface(wm_, wlSurf_);
|
||||
static const struct xdg_surface_listener xsListener = {OnSurfaceConfigure};
|
||||
xdg_surface_add_listener(xdgSurf_, &xsListener, this);
|
||||
toplevel_ = xdg_surface_get_toplevel(xdgSurf_);
|
||||
static const struct xdg_toplevel_listener tlListener = {OnToplevelConfigure, OnToplevelClose, nullptr, nullptr};
|
||||
xdg_toplevel_add_listener(toplevel_, &tlListener, this);
|
||||
xdg_toplevel_set_title(toplevel_, "DroidDeck Cardboard VR");
|
||||
xdg_toplevel_set_app_id(toplevel_, "droiddeck-cardboard-vr");
|
||||
xdg_toplevel_set_fullscreen(toplevel_, nullptr);
|
||||
wl_surface_commit(wlSurf_);
|
||||
for (int i = 0; i < 20 && !configured_; i++) wl_display_roundtrip(wl_);
|
||||
// The size: the panel the Cardboard pass draws (two eyes, from the shared pose file), so nothing
|
||||
// scales the picture down on the way. The compositor's configure carries the session's output
|
||||
// size, which is often smaller; it is the fallback.
|
||||
width_ = height_ = 0;
|
||||
int fd = open("/run/droiddeck/droiddeck-headpose", O_RDONLY | O_CLOEXEC);
|
||||
if (fd >= 0) {
|
||||
uint32_t eye[2] = {0, 0};
|
||||
if (pread(fd, eye, sizeof(eye), 72) == (ssize_t)sizeof(eye) && eye[0] && eye[1]) { width_ = eye[0] * 2; height_ = eye[1]; }
|
||||
close(fd);
|
||||
}
|
||||
if (!width_ && cfgW_ > 0 && cfgH_ > 0) { width_ = (uint32_t)cfgW_; height_ = (uint32_t)cfgH_; }
|
||||
if (!width_) { width_ = 1920; height_ = 1080; }
|
||||
Log("presenter: %ux%u fullscreen window on Wayland display %s (configured %s)", width_, height_, name, configured_ ? "yes" : "no");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InitWindow() {
|
||||
if (const char *g = getenv("DROIDDECK_VR_PRESENT_GUARD_MS")) guard_ = std::chrono::microseconds((long)(atof(g) * 1000));
|
||||
if (!x_.Load()) { Log("presenter: no libX11"); return false; }
|
||||
// gamescope's first Xwayland (:0) is where its overlays (mangoapp, Steam's) live; SteamVR's
|
||||
// processes inherit the games' display (:1), so it is not taken from DISPLAY.
|
||||
const char *name = getenv("DROIDDECK_VR_DISPLAY");
|
||||
if (!name || !*name) name = ":0";
|
||||
dpy_ = x_.OpenDisplay(name);
|
||||
if (!dpy_) { Log("presenter: cannot open X display %s", name); return false; }
|
||||
int scr = x_.DefaultScreen(dpy_);
|
||||
width_ = (uint32_t)x_.DisplayWidth(dpy_, scr);
|
||||
height_ = (uint32_t)x_.DisplayHeight(dpy_, scr);
|
||||
win_ = x_.CreateSimpleWindow(dpy_, x_.DefaultRootWindow(dpy_), 0, 0, width_, height_, 0, 0, 0);
|
||||
x_.StoreName(dpy_, win_, "DroidDeck Cardboard VR");
|
||||
// gamescope shows an external overlay above whatever has focus (mangoapp is one).
|
||||
// gamescope composites one external overlay (mangoapp, the perf HUD, usually holds it) and one Steam
|
||||
// overlay; the Steam overlay slot is free while nothing is shown over a game.
|
||||
const char *atom = getenv("DROIDDECK_VR_OVERLAY_ATOM");
|
||||
if (!atom || !*atom) atom = "STEAM_OVERLAY";
|
||||
unsigned long cardinal = 6; // XA_CARDINAL
|
||||
unsigned long one = 1; // format 32 properties are longs in Xlib
|
||||
x_.ChangeProperty(dpy_, win_, x_.InternAtom(dpy_, atom, 0), cardinal, 32, 0 /* PropModeReplace */,
|
||||
(const unsigned char *)&one, 1);
|
||||
x_.MapWindow(dpy_, win_);
|
||||
x_.Flush(dpy_);
|
||||
Log("presenter: %ux%u overlay window (%s) on X display %s", width_, height_, atom, name);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InitVulkan() {
|
||||
vk_.lib = dlopen("libvulkan.so.1", RTLD_NOW | RTLD_LOCAL);
|
||||
if (!vk_.lib) { Log("presenter: no libvulkan"); return false; }
|
||||
vk_.GetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)dlsym(vk_.lib, "vkGetInstanceProcAddr");
|
||||
#define X(n) vk_.n = (PFN_vk##n)vk_.GetInstanceProcAddr(VK_NULL_HANDLE, "vk" #n);
|
||||
VK_GLOBAL(X)
|
||||
#undef X
|
||||
const char *iexts[] = {"VK_KHR_surface", wayland_ ? "VK_KHR_wayland_surface" : "VK_KHR_xlib_surface"};
|
||||
VkApplicationInfo app = {VK_STRUCTURE_TYPE_APPLICATION_INFO, nullptr, "droiddeck-vr-presenter", 1, nullptr, 0, VK_API_VERSION_1_1};
|
||||
VkInstanceCreateInfo ici = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, nullptr, 0, &app, 0, nullptr, 2, iexts};
|
||||
VkResult r = vk_.CreateInstance(&ici, nullptr, &inst_);
|
||||
if (r != VK_SUCCESS) { Log("presenter: vkCreateInstance %d", r); return false; }
|
||||
#define X(n) vk_.n = (PFN_vk##n)vk_.GetInstanceProcAddr(inst_, "vk" #n);
|
||||
VK_INSTANCE(X)
|
||||
#undef X
|
||||
uint32_t n = 1;
|
||||
VkPhysicalDevice pds[4];
|
||||
n = 4;
|
||||
if (vk_.EnumeratePhysicalDevices(inst_, &n, pds) < 0 || n == 0) { Log("presenter: no GPU"); return false; }
|
||||
pd_ = pds[0];
|
||||
VkPhysicalDeviceProperties props;
|
||||
vk_.GetPhysicalDeviceProperties(pd_, &props);
|
||||
vk_.GetPhysicalDeviceMemoryProperties(pd_, &mem_);
|
||||
|
||||
if (wayland_) {
|
||||
VkWaylandSurfaceCreateInfoKHR wci = {VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR, nullptr, 0, wl_, wlSurf_};
|
||||
if ((r = vk_.CreateWaylandSurfaceKHR(inst_, &wci, nullptr, &surface_)) != VK_SUCCESS) { Log("presenter: wayland surface %d", r); return false; }
|
||||
} else {
|
||||
VkXlibSurfaceCreateInfoKHR sci = {VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR, nullptr, 0, dpy_, win_};
|
||||
if ((r = vk_.CreateXlibSurfaceKHR(inst_, &sci, nullptr, &surface_)) != VK_SUCCESS) { Log("presenter: xlib surface %d", r); return false; }
|
||||
}
|
||||
|
||||
uint32_t nq = 0;
|
||||
vk_.GetPhysicalDeviceQueueFamilyProperties(pd_, &nq, nullptr);
|
||||
std::vector<VkQueueFamilyProperties> qf(nq);
|
||||
vk_.GetPhysicalDeviceQueueFamilyProperties(pd_, &nq, qf.data());
|
||||
qfam_ = UINT32_MAX;
|
||||
for (uint32_t i = 0; i < nq; i++) {
|
||||
VkBool32 present = VK_FALSE;
|
||||
vk_.GetPhysicalDeviceSurfaceSupportKHR(pd_, i, surface_, &present);
|
||||
if ((qf[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) && present) { qfam_ = i; break; }
|
||||
}
|
||||
if (qfam_ == UINT32_MAX) { Log("presenter: no queue can present"); return false; }
|
||||
float prio = 1.0f;
|
||||
VkDeviceQueueCreateInfo qci = {VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, nullptr, 0, qfam_, 1, &prio};
|
||||
const char *dexts[] = {"VK_KHR_swapchain", "VK_KHR_external_memory_fd"};
|
||||
VkDeviceCreateInfo dci = {VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, nullptr, 0, 1, &qci, 0, nullptr, 2, dexts, nullptr};
|
||||
if ((r = vk_.CreateDevice(pd_, &dci, nullptr, &dev_)) != VK_SUCCESS) { Log("presenter: vkCreateDevice %d", r); return false; }
|
||||
#define X(n) vk_.n = (PFN_vk##n)vk_.GetDeviceProcAddr(dev_, "vk" #n);
|
||||
VK_DEVICE(X)
|
||||
#undef X
|
||||
vk_.GetDeviceQueue(dev_, qfam_, 0, &queue_);
|
||||
|
||||
VkCommandPoolCreateInfo pci = {VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, nullptr,
|
||||
VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, qfam_};
|
||||
vk_.CreateCommandPool(dev_, &pci, nullptr, &pool_);
|
||||
VkCommandBufferAllocateInfo cai = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, nullptr, pool_, VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1};
|
||||
vk_.AllocateCommandBuffers(dev_, &cai, &cmd_);
|
||||
VkFenceCreateInfo fci = {VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, nullptr, VK_FENCE_CREATE_SIGNALED_BIT};
|
||||
vk_.CreateFence(dev_, &fci, nullptr, &fence_);
|
||||
VkSemaphoreCreateInfo semci = {VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, nullptr, 0};
|
||||
vk_.CreateSemaphore(dev_, &semci, nullptr, &acquired_);
|
||||
vk_.CreateSemaphore(dev_, &semci, nullptr, &rendered_);
|
||||
if (!CreateSwapchain()) return false;
|
||||
Log("presenter: Vulkan on %s, queue family %u", props.deviceName, qfam_);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CreateSwapchain() {
|
||||
VkSurfaceCapabilitiesKHR caps;
|
||||
vk_.GetPhysicalDeviceSurfaceCapabilitiesKHR(pd_, surface_, &caps);
|
||||
uint32_t nf = 0;
|
||||
vk_.GetPhysicalDeviceSurfaceFormatsKHR(pd_, surface_, &nf, nullptr);
|
||||
std::vector<VkSurfaceFormatKHR> fmts(nf);
|
||||
vk_.GetPhysicalDeviceSurfaceFormatsKHR(pd_, surface_, &nf, fmts.data());
|
||||
VkSurfaceFormatKHR fmt = fmts.empty() ? VkSurfaceFormatKHR{VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR} : fmts[0];
|
||||
for (auto &f : fmts)
|
||||
if (f.format == VK_FORMAT_B8G8R8A8_UNORM || f.format == VK_FORMAT_R8G8B8A8_UNORM) { fmt = f; break; }
|
||||
uint32_t npm = 0;
|
||||
vk_.GetPhysicalDeviceSurfacePresentModesKHR(pd_, surface_, &npm, nullptr);
|
||||
std::vector<VkPresentModeKHR> pms(npm);
|
||||
vk_.GetPhysicalDeviceSurfacePresentModesKHR(pd_, surface_, &npm, pms.data());
|
||||
VkPresentModeKHR pm = VK_PRESENT_MODE_FIFO_KHR;
|
||||
for (auto m : pms) if (m == VK_PRESENT_MODE_MAILBOX_KHR) pm = m;
|
||||
VkExtent2D ext = caps.currentExtent.width != UINT32_MAX ? caps.currentExtent : VkExtent2D{width_, height_};
|
||||
extent_ = ext;
|
||||
uint32_t count = caps.minImageCount + 1;
|
||||
if (caps.maxImageCount && count > caps.maxImageCount) count = caps.maxImageCount;
|
||||
VkCompositeAlphaFlagBitsKHR alpha = (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR)
|
||||
? VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR : (VkCompositeAlphaFlagBitsKHR)(caps.supportedCompositeAlpha & -caps.supportedCompositeAlpha);
|
||||
VkSwapchainCreateInfoKHR sci = {VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR};
|
||||
sci.surface = surface_;
|
||||
sci.minImageCount = count;
|
||||
sci.imageFormat = fmt.format;
|
||||
sci.imageColorSpace = fmt.colorSpace;
|
||||
sci.imageExtent = ext;
|
||||
sci.imageArrayLayers = 1;
|
||||
sci.imageUsage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
||||
sci.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
sci.preTransform = caps.currentTransform;
|
||||
sci.compositeAlpha = alpha;
|
||||
sci.presentMode = pm;
|
||||
sci.clipped = VK_TRUE;
|
||||
VkResult r = vk_.CreateSwapchainKHR(dev_, &sci, nullptr, &swapchain_);
|
||||
if (r != VK_SUCCESS) { Log("presenter: vkCreateSwapchainKHR %d", r); return false; }
|
||||
uint32_t ni = 0;
|
||||
vk_.GetSwapchainImagesKHR(dev_, swapchain_, &ni, nullptr);
|
||||
images_.resize(ni);
|
||||
vk_.GetSwapchainImagesKHR(dev_, swapchain_, &ni, images_.data());
|
||||
Log("presenter: swapchain %ux%u, format %d, %u images, %s", ext.width, ext.height, fmt.format, ni,
|
||||
pm == VK_PRESENT_MODE_MAILBOX_KHR ? "mailbox" : "fifo");
|
||||
return true;
|
||||
}
|
||||
|
||||
int MemoryType(uint32_t bits) {
|
||||
for (uint32_t i = 0; i < mem_.memoryTypeCount; i++)
|
||||
if ((bits & (1u << i)) && (mem_.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) return (int)i;
|
||||
for (uint32_t i = 0; i < mem_.memoryTypeCount; i++) if (bits & (1u << i)) return (int)i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Imports one of SteamVR's shared images with the parameters it was made with (ALVR's recipe):
|
||||
// optimal tiling, mutable format (which keeps it uncompressed), opaque fd, dedicated memory. UNORM so a
|
||||
// blit copies the bytes rather than decoding sRGB.
|
||||
void Import(const PendingImport &p) {
|
||||
Imported im{p.handle};
|
||||
im.width = p.width;
|
||||
im.height = p.height;
|
||||
// The same format list SteamVR's mutable sRGB images carry: with it Turnip keeps UBWC compression on,
|
||||
// and the import has to agree on that or it reads compressed data as plain pixels.
|
||||
const char *fl = getenv("DROIDDECK_VR_IMPORT_FORMAT_LIST");
|
||||
const bool formatList = !fl || strcmp(fl, "0") != 0;
|
||||
VkFormat formats[2] = {(VkFormat)p.format, UnormOf(p.format)};
|
||||
VkImageFormatListCreateInfo list = {VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO, nullptr, 2, formats};
|
||||
VkExternalMemoryImageCreateInfo ext = {VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO, formatList ? &list : nullptr,
|
||||
VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT};
|
||||
VkImageCreateInfo ici = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, &ext, VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, VK_IMAGE_TYPE_2D,
|
||||
UnormOf(p.format), {p.width, p.height, 1}, 1, 1, VK_SAMPLE_COUNT_1_BIT, VK_IMAGE_TILING_OPTIMAL,
|
||||
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
|
||||
VK_SHARING_MODE_EXCLUSIVE, 0, nullptr, VK_IMAGE_LAYOUT_UNDEFINED};
|
||||
VkResult r = vk_.CreateImage(dev_, &ici, nullptr, &im.image);
|
||||
if (r != VK_SUCCESS) { Log("presenter: import image %d", r); close(p.fd); return; }
|
||||
VkMemoryRequirements req;
|
||||
vk_.GetImageMemoryRequirements(dev_, im.image, &req);
|
||||
VkImportMemoryFdInfoKHR imp = {VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR, nullptr, VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT, p.fd};
|
||||
VkMemoryDedicatedAllocateInfo ded = {VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO, &imp, im.image, VK_NULL_HANDLE};
|
||||
VkMemoryAllocateInfo mai = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, &ded, req.size, (uint32_t)MemoryType(req.memoryTypeBits)};
|
||||
r = vk_.AllocateMemory(dev_, &mai, nullptr, &im.memory);
|
||||
if (r != VK_SUCCESS) {
|
||||
Log("presenter: import memory (fd %d, %llu bytes) %d", p.fd, (unsigned long long)req.size, r);
|
||||
close(p.fd);
|
||||
vk_.DestroyImage(dev_, im.image, nullptr);
|
||||
return;
|
||||
}
|
||||
vk_.BindImageMemory(dev_, im.image, im.memory, 0);
|
||||
textures_.push_back(im);
|
||||
Log("presenter: imported texture 0x%llx (%ux%u, format %u)", (unsigned long long)p.handle, p.width, p.height, p.format);
|
||||
}
|
||||
|
||||
void Forget(uint64_t handle) {
|
||||
for (auto it = textures_.begin(); it != textures_.end(); ++it) {
|
||||
if (it->handle == handle) {
|
||||
vk_.DeviceWaitIdle(dev_);
|
||||
vk_.DestroyImage(dev_, it->image, nullptr);
|
||||
vk_.FreeMemory(dev_, it->memory, nullptr);
|
||||
textures_.erase(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const Imported *Find(uint64_t h) const {
|
||||
for (auto &t : textures_) if (t.handle == h) return &t;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static VkImageMemoryBarrier Barrier(VkImage img, VkImageLayout from, VkImageLayout to, VkAccessFlags src, VkAccessFlags dst,
|
||||
uint32_t srcQ = VK_QUEUE_FAMILY_IGNORED, uint32_t dstQ = VK_QUEUE_FAMILY_IGNORED) {
|
||||
VkImageMemoryBarrier b = {VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, nullptr, src, dst, from, to, srcQ, dstQ, img,
|
||||
{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
|
||||
return b;
|
||||
}
|
||||
|
||||
bool Draw(const EyeFrame &f) {
|
||||
const Imported *eye[2] = {Find(f.texture[0]), Find(f.texture[1])};
|
||||
if (!eye[0] || !eye[1]) return false;
|
||||
vk_.WaitForFences(dev_, 1, &fence_, VK_TRUE, UINT64_MAX);
|
||||
uint32_t idx = 0;
|
||||
VkResult r = vk_.AcquireNextImageKHR(dev_, swapchain_, 100000000ull, acquired_, VK_NULL_HANDLE, &idx);
|
||||
if (r != VK_SUCCESS && r != VK_SUBOPTIMAL_KHR) {
|
||||
static int said;
|
||||
if (said++ < 3) Log("presenter: acquire %d", r);
|
||||
return false;
|
||||
}
|
||||
vk_.ResetFences(dev_, 1, &fence_);
|
||||
vk_.ResetCommandBuffer(cmd_, 0);
|
||||
VkCommandBufferBeginInfo bi = {VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, nullptr, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT};
|
||||
vk_.BeginCommandBuffer(cmd_, &bi);
|
||||
VkImage dst = images_[idx];
|
||||
// The eye textures come from SteamVR's compositor (another process): take them from the external
|
||||
// queue family as they are (GENERAL keeps the contents; they are uncompressed).
|
||||
VkImageMemoryBarrier pre[3] = {
|
||||
Barrier(dst, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_ACCESS_TRANSFER_WRITE_BIT),
|
||||
Barrier(eye[0]->image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, 0, VK_ACCESS_TRANSFER_READ_BIT,
|
||||
VK_QUEUE_FAMILY_EXTERNAL, qfam_),
|
||||
Barrier(eye[1]->image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, 0, VK_ACCESS_TRANSFER_READ_BIT,
|
||||
VK_QUEUE_FAMILY_EXTERNAL, qfam_)};
|
||||
vk_.CmdPipelineBarrier(cmd_, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr,
|
||||
eye[0] == eye[1] ? 2 : 3, pre);
|
||||
VkClearColorValue black = {{0, 0, 0, 1}};
|
||||
VkImageSubresourceRange range = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
||||
vk_.CmdClearColorImage(cmd_, dst, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &black, 1, &range);
|
||||
VkMemoryBarrier mb = {VK_STRUCTURE_TYPE_MEMORY_BARRIER, nullptr, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT};
|
||||
vk_.CmdPipelineBarrier(cmd_, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1, &mb, 0, nullptr, 0, nullptr);
|
||||
const int32_t half = (int32_t)extent_.width / 2;
|
||||
for (int e = 0; e < 2; e++) {
|
||||
const float *b = f.bounds[e];
|
||||
VkImageBlit blit = {};
|
||||
blit.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
|
||||
blit.srcOffsets[0] = {(int32_t)(b[0] * eye[e]->width), (int32_t)(b[1] * eye[e]->height), 0};
|
||||
blit.srcOffsets[1] = {(int32_t)(b[2] * eye[e]->width), (int32_t)(b[3] * eye[e]->height), 1};
|
||||
blit.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
|
||||
blit.dstOffsets[0] = {e * half, 0, 0};
|
||||
blit.dstOffsets[1] = {e * half + half, (int32_t)extent_.height, 1};
|
||||
vk_.CmdBlitImage(cmd_, eye[e]->image, VK_IMAGE_LAYOUT_GENERAL, dst, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit,
|
||||
VK_FILTER_LINEAR);
|
||||
}
|
||||
VkImageMemoryBarrier post[3] = {
|
||||
Barrier(dst, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_ACCESS_TRANSFER_WRITE_BIT, 0),
|
||||
Barrier(eye[0]->image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, VK_ACCESS_TRANSFER_READ_BIT, 0, qfam_,
|
||||
VK_QUEUE_FAMILY_EXTERNAL),
|
||||
Barrier(eye[1]->image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, VK_ACCESS_TRANSFER_READ_BIT, 0, qfam_,
|
||||
VK_QUEUE_FAMILY_EXTERNAL)};
|
||||
vk_.CmdPipelineBarrier(cmd_, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr,
|
||||
eye[0] == eye[1] ? 2 : 3, post);
|
||||
vk_.EndCommandBuffer(cmd_);
|
||||
VkPipelineStageFlags wait = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
||||
VkSubmitInfo si = {VK_STRUCTURE_TYPE_SUBMIT_INFO, nullptr, 1, &acquired_, &wait, 1, &cmd_, 1, &rendered_};
|
||||
if ((r = vk_.QueueSubmit(queue_, 1, &si, fence_)) != VK_SUCCESS) { Log("presenter: submit %d", r); return false; }
|
||||
VkPresentInfoKHR pi = {VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, nullptr, 1, &rendered_, 1, &swapchain_, &idx, nullptr};
|
||||
r = vk_.QueuePresentKHR(queue_, &pi);
|
||||
if (r != VK_SUCCESS && r != VK_SUBOPTIMAL_KHR) {
|
||||
static int said;
|
||||
if (said++ < 3) Log("presenter: present %d", r);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::mutex mutex_;
|
||||
std::condition_variable cv_;
|
||||
std::vector<PendingImport> pending_;
|
||||
std::vector<uint64_t> removals_;
|
||||
EyeFrame frame_{};
|
||||
std::chrono::steady_clock::time_point frameAt_{};
|
||||
bool haveFrame_ = false, exit_ = false, started_ = false;
|
||||
std::atomic<uint64_t> submitted_{0};
|
||||
std::thread thread_;
|
||||
|
||||
X11 x_;
|
||||
Display *dpy_ = nullptr;
|
||||
Window win_ = 0;
|
||||
uint32_t width_ = 0, height_ = 0;
|
||||
Vk vk_;
|
||||
VkInstance inst_ = VK_NULL_HANDLE;
|
||||
VkPhysicalDevice pd_ = VK_NULL_HANDLE;
|
||||
VkPhysicalDeviceMemoryProperties mem_{};
|
||||
VkSurfaceKHR surface_ = VK_NULL_HANDLE;
|
||||
uint32_t qfam_ = 0;
|
||||
VkDevice dev_ = VK_NULL_HANDLE;
|
||||
VkQueue queue_ = VK_NULL_HANDLE;
|
||||
VkCommandPool pool_ = VK_NULL_HANDLE;
|
||||
VkCommandBuffer cmd_ = VK_NULL_HANDLE;
|
||||
VkFence fence_ = VK_NULL_HANDLE;
|
||||
VkSemaphore acquired_ = VK_NULL_HANDLE, rendered_ = VK_NULL_HANDLE;
|
||||
VkSwapchainKHR swapchain_ = VK_NULL_HANDLE;
|
||||
VkExtent2D extent_{};
|
||||
std::vector<VkImage> images_;
|
||||
std::vector<Imported> textures_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
void Presenter::AddTextures(const uint64_t h[3], const int fds[3], uint32_t f, uint32_t w, uint32_t hh) { Impl::Get().Add(h, fds, f, w, hh); }
|
||||
void Presenter::RemoveTexture(uint64_t h) { Impl::Get().Remove(h); }
|
||||
void Presenter::Submit(const EyeFrame &f) { Impl::Get().Submit(f); }
|
||||
void Presenter::Shutdown() { Impl::Get().Shutdown(); }
|
||||
|
||||
} // namespace droiddeck
|
||||
@@ -0,0 +1,34 @@
|
||||
// Puts SteamVR's eye textures on the screen for the Cardboard driver (driver_droiddeck.cpp).
|
||||
//
|
||||
// The driver-direct component hands each frame over as two eye textures: shared Vulkan images,
|
||||
// dmabufs on Linux. The presenter imports them into a Vulkan device of its own and draws them side by
|
||||
// side into a fullscreen X11 window on gamescope's Xwayland, marked as an external overlay so gamescope
|
||||
// shows it above whatever app has focus. DroidDeck's compositor, in Cardboard mode with a
|
||||
// side-by-side source, then puts the lens distortion on it for the viewer.
|
||||
//
|
||||
// SteamVR compositor -> eye textures -> [presenter: blit L|R] -> X11 overlay -> gamescope
|
||||
// -> DroidDeck compositor -> Cardboard lens pass -> panel
|
||||
//
|
||||
// Everything is loaded at runtime (libvulkan.so.1, libX11.so.6): the driver links against nothing
|
||||
// the cross build image lacks.
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
namespace droiddeck {
|
||||
|
||||
struct EyeFrame {
|
||||
uint64_t texture[2]; // SharedTextureHandle_t per eye
|
||||
float bounds[2][4]; // uMin, vMin, uMax, vMax per eye
|
||||
};
|
||||
|
||||
class Presenter {
|
||||
public:
|
||||
// Called from CreateSwapTextureSet: the three images of one set, as SteamVR made them.
|
||||
static void AddTextures(const uint64_t handles[3], const int fds[3], uint32_t vkFormat, uint32_t width, uint32_t height);
|
||||
static void RemoveTexture(uint64_t handle);
|
||||
// Called from Present: the frame's eye textures; returns at once (a thread of its own presents).
|
||||
static void Submit(const EyeFrame &frame);
|
||||
static void Shutdown();
|
||||
};
|
||||
|
||||
} // namespace droiddeck
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,34 @@
|
||||
/* Link-time stand-in for libwayland-client.so.0 (tools/vr/build.sh): the symbols the presenter uses, so
|
||||
* the driver links with -z defs against a library the build machine does not have. Never loaded: the
|
||||
* runtime's real libwayland-client.so.0 satisfies the dependency on the device. */
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "wayland-util.h"
|
||||
|
||||
struct wl_display;
|
||||
struct wl_proxy;
|
||||
struct wl_event_queue;
|
||||
|
||||
struct wl_display *wl_display_connect(const char *name) { return NULL; }
|
||||
void wl_display_disconnect(struct wl_display *d) {}
|
||||
int wl_display_roundtrip(struct wl_display *d) { return -1; }
|
||||
int wl_display_dispatch(struct wl_display *d) { return -1; }
|
||||
int wl_display_dispatch_pending(struct wl_display *d) { return -1; }
|
||||
int wl_display_flush(struct wl_display *d) { return -1; }
|
||||
int wl_display_get_error(struct wl_display *d) { return 0; }
|
||||
struct wl_proxy *wl_proxy_marshal_flags(struct wl_proxy *p, uint32_t opcode, const struct wl_interface *i, uint32_t v,
|
||||
uint32_t flags, ...) { return NULL; }
|
||||
int wl_proxy_add_listener(struct wl_proxy *p, void (**impl)(void), void *data) { return -1; }
|
||||
uint32_t wl_proxy_get_version(struct wl_proxy *p) { return 0; }
|
||||
void wl_proxy_destroy(struct wl_proxy *p) {}
|
||||
void wl_proxy_set_user_data(struct wl_proxy *p, void *d) {}
|
||||
void *wl_proxy_get_user_data(struct wl_proxy *p) { return NULL; }
|
||||
|
||||
const struct wl_interface wl_registry_interface;
|
||||
const struct wl_interface wl_compositor_interface;
|
||||
const struct wl_interface wl_surface_interface;
|
||||
const struct wl_interface wl_output_interface;
|
||||
const struct wl_interface wl_seat_interface;
|
||||
const struct wl_interface wl_callback_interface;
|
||||
const struct wl_interface wl_region_interface;
|
||||
const struct wl_interface wl_buffer_interface;
|
||||
Reference in new issue
Block a user