Multiple foveation fixes
fix math for center and convergence calculation add WIVRN_FOVEATION_OFFSET variable to change the vertical center offset set default vertical center offset to 10° below horizontal add WIVRN_FOVEATION_DISTANCE variable to set convergence distance set default convergence distance to 1m fix cache invalidation condition Co-authored-by: Baplar <baplar@gmail.com>
This commit is contained in:
co-authored by
Baplar
parent
d459c9489e
commit
6ad4e8ae51
@@ -62,7 +62,12 @@ void render_distortion_images_fini(struct render_resources * r)
|
||||
DF(Memory, r->distortion.device_memory);
|
||||
}
|
||||
|
||||
static double foveate(double a, double b, double λ, double c, double x)
|
||||
// a, b: parameters computed by solve_foveation
|
||||
// λ: pixel ratio between full size and foveated image (in range ]0,1[)
|
||||
// c: coordinates in -1,1 range of the full size image where pixel ratio must be 1:1
|
||||
// x: coordinates in -1,1 range of the foveated image
|
||||
// result: full size image coordinates in -1,1 range
|
||||
static double defoveate(double a, double b, double λ, double c, double x)
|
||||
{
|
||||
// In order to save encoding, transmit and decoding time, only a portion of the image is encoded in full resolution.
|
||||
// on each axis, foveated coordinates are defined by the following formula.
|
||||
@@ -137,9 +142,14 @@ static std::tuple<float, float> solve_foveation(float λ, float c)
|
||||
return {a, b(a)};
|
||||
}
|
||||
|
||||
static bool is_zero_quat(xrt_quat q)
|
||||
{
|
||||
return q.x == 0 and q.y == 0 and q.z == 0 and q.w == 0;
|
||||
}
|
||||
|
||||
static xrt_vec2 yaw_pitch(xrt_quat q)
|
||||
{
|
||||
if (q.x == 0 and q.y == 0 and q.z == 0 and q.w == 0)
|
||||
if (is_zero_quat(q))
|
||||
return xrt_vec2{};
|
||||
|
||||
float sine_theta = std::clamp(-2.0f * (q.y * q.z - q.w * q.x), -1.0f, 1.0f);
|
||||
@@ -160,17 +170,19 @@ static xrt_vec2 yaw_pitch(xrt_quat q)
|
||||
|
||||
static float angles_to_center(float e, float l, float r)
|
||||
{
|
||||
e = tan(e);
|
||||
l = tan(l);
|
||||
r = tan(r);
|
||||
return (e - l) / (r - l) * 2 - 1;
|
||||
}
|
||||
|
||||
static float convergence_angle(float eye_x, float gaze_yaw)
|
||||
static float convergence_angle(float distance, float eye_x, float gaze_yaw)
|
||||
{
|
||||
const float c = 0.5; // simutaled convergence distance
|
||||
auto b = c * std::sin(gaze_yaw) - eye_x;
|
||||
return std::asin(b / c);
|
||||
auto b = distance * std::sin(gaze_yaw) - eye_x;
|
||||
return std::asin(b / distance);
|
||||
}
|
||||
|
||||
void fill_param_2d(
|
||||
static void fill_param_2d(
|
||||
float c,
|
||||
size_t foveated_dim,
|
||||
size_t source_dim,
|
||||
@@ -185,7 +197,7 @@ void fill_param_2d(
|
||||
for (size_t i = 1; i < foveated_dim; ++i)
|
||||
{
|
||||
double u = (i * 2.) / foveated_dim - 1;
|
||||
auto f = foveate(a, b, scale, c, u);
|
||||
auto f = defoveate(a, b, scale, c, u);
|
||||
uint16_t n = std::clamp<uint16_t>((f * 0.5 + 0.5) * source_dim + 0.5, 0, source_dim);
|
||||
assert(n > last);
|
||||
size_t count = n - last;
|
||||
@@ -212,6 +224,44 @@ void fill_param_2d(
|
||||
out.resize(count * 2 - 1);
|
||||
}
|
||||
|
||||
static float get_angle_offset()
|
||||
{
|
||||
if (const char * cvar = std::getenv("WIVRN_FOVEATION_OFFSET"))
|
||||
{
|
||||
std::string_view var(cvar);
|
||||
float res;
|
||||
auto e = std::from_chars(var.begin(), var.end(), res);
|
||||
if (e.ec == std::errc())
|
||||
{
|
||||
// No clamping, we don’t know the range
|
||||
// of actual values for all headsets for sure.
|
||||
return -res * M_PI / 180;
|
||||
}
|
||||
else
|
||||
U_LOG_W("Malformed WIVRN_FOVEATION_OFFSET, must be a number (angle in °)");
|
||||
}
|
||||
// normal sight line is between 10° and 15° below horizontal
|
||||
// https://apps.dtic.mil/sti/tr/pdf/AD0758339.pdf pages 393-394
|
||||
// testing shows 10° looks better
|
||||
return 10 * M_PI / 180;
|
||||
}
|
||||
|
||||
static float get_convergence_distance()
|
||||
{
|
||||
if (const char * cvar = std::getenv("WIVRN_FOVEATION_DISTANCE"))
|
||||
{
|
||||
std::string_view var(cvar);
|
||||
float res;
|
||||
auto e = std::from_chars(var.begin(), var.end(), res);
|
||||
if (e.ec == std::errc())
|
||||
return std::max(res, 0.05f);
|
||||
else
|
||||
U_LOG_W("Malformed WIVRN_FOVEATION_DISTANCE, must be a number (distance in meters)");
|
||||
}
|
||||
// 1m by default
|
||||
return 1;
|
||||
}
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
|
||||
@@ -219,33 +269,29 @@ void wivrn_foveation::compute_params(
|
||||
xrt_rect src[2],
|
||||
const xrt_fov fovs[2])
|
||||
{
|
||||
xrt_vec2 tan_center[2]{};
|
||||
auto e = yaw_pitch(gaze);
|
||||
for (size_t i = 0; i < 2; ++i)
|
||||
{
|
||||
xrt_quat view_quat{
|
||||
.x = views[i].pose.orientation.x,
|
||||
.y = views[i].pose.orientation.y,
|
||||
.z = views[i].pose.orientation.z,
|
||||
.w = views[i].pose.orientation.w};
|
||||
auto view = yaw_pitch(view_quat);
|
||||
|
||||
auto angle_x = convergence_angle(views[i].pose.position.x, e.x);
|
||||
tan_center[i].x = angles_to_center(view.x + angle_x, views[i].fov.angleLeft, views[i].fov.angleRight);
|
||||
tan_center[i].y = angles_to_center(-view.y - e.y, views[i].fov.angleUp, views[i].fov.angleDown);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 2; ++i)
|
||||
{
|
||||
const auto & fov = fovs[i];
|
||||
|
||||
if (foveated_width < src[i].extent.w)
|
||||
fill_param_2d(tan_center[i].x, foveated_width, src[i].extent.w, params[i].x);
|
||||
{
|
||||
auto angle_x = convergence_angle(convergence_distance, eye_x[i], e.x);
|
||||
auto center = angles_to_center(angle_x, fov.angle_left, fov.angle_right);
|
||||
fill_param_2d(center, foveated_width, src[i].extent.w, params[i].x);
|
||||
}
|
||||
else
|
||||
params[i].x = {uint16_t(src[i].extent.w)};
|
||||
if (foveated_height < src[i].extent.h)
|
||||
{
|
||||
auto offset_y = (views[i].fov.angleDown + views[i].fov.angleUp) / 2;
|
||||
fill_param_2d(tan_center[i].y + offset_y, foveated_height, src[i].extent.h, params[i].y);
|
||||
auto angle_y = e.y;
|
||||
if (is_zero_quat(gaze))
|
||||
{
|
||||
// Natural gaze is not straight forward, adjust the angle
|
||||
angle_y += angle_offset;
|
||||
}
|
||||
auto center = angles_to_center(-angle_y, fov.angle_up, fov.angle_down);
|
||||
fill_param_2d(center, foveated_height, src[i].extent.h, params[i].y);
|
||||
}
|
||||
else
|
||||
params[i].y = {uint16_t(src[i].extent.h)};
|
||||
@@ -255,6 +301,8 @@ void wivrn_foveation::compute_params(
|
||||
wivrn_foveation::wivrn_foveation(wivrn_vk_bundle & bundle, const xrt_hmd_parts & hmd) :
|
||||
foveated_width(hmd.screens[0].w_pixels / 2),
|
||||
foveated_height(hmd.screens[0].h_pixels),
|
||||
angle_offset(get_angle_offset()),
|
||||
convergence_distance(get_convergence_distance()),
|
||||
command_pool(bundle.device, vk::CommandPoolCreateInfo{.queueFamilyIndex = bundle.queue_family_index}),
|
||||
cmd(std::move(bundle.device.allocateCommandBuffers({
|
||||
.commandPool = *command_pool,
|
||||
@@ -282,7 +330,8 @@ void wivrn_foveation::update_tracking(const from_headset::tracking & tracking, c
|
||||
|
||||
const uint8_t orientation_ok = from_headset::tracking::orientation_valid | from_headset::tracking::orientation_tracked;
|
||||
|
||||
views = tracking.views;
|
||||
eye_x[0] = tracking.views[0].pose.position.x;
|
||||
eye_x[1] = tracking.views[1].pose.position.x;
|
||||
|
||||
std::optional<xrt_quat> head;
|
||||
|
||||
@@ -357,30 +406,14 @@ static bool operator==(const T & a, const T & b)
|
||||
static_assert(std::has_unique_object_representations_v<T>);
|
||||
return std::memcmp(&a, &b, sizeof(T)) == 0;
|
||||
}
|
||||
bool operator==(const xrt_quat & a, const xrt_quat & b)
|
||||
static bool operator==(const xrt_quat & a, const xrt_quat & b)
|
||||
{
|
||||
return a.x == b.x and a.y == b.y and a.z == b.z and a.w == b.w;
|
||||
}
|
||||
bool operator==(const XrQuaternionf & a, const XrQuaternionf & b)
|
||||
{
|
||||
return a.x == b.x and a.y == b.y and a.z == b.z and a.w == b.w;
|
||||
}
|
||||
bool operator==(const xrt_fov & a, const xrt_fov & b)
|
||||
static bool operator==(const xrt_fov & a, const xrt_fov & b)
|
||||
{
|
||||
return a.angle_left == b.angle_left and a.angle_right == b.angle_right and a.angle_up == b.angle_up and a.angle_down == b.angle_down;
|
||||
}
|
||||
bool operator==(const XrFovf & a, const XrFovf & b)
|
||||
{
|
||||
return a.angleLeft == b.angleLeft and a.angleRight == b.angleRight and a.angleUp == b.angleUp and a.angleDown == b.angleDown;
|
||||
}
|
||||
bool operator==(const XrVector3f & a, const XrVector3f & b)
|
||||
{
|
||||
return a.x == b.x and a.y == b.y and a.z == b.z;
|
||||
}
|
||||
bool operator==(const wivrn::from_headset::tracking::view & a, const wivrn::from_headset::tracking::view & b)
|
||||
{
|
||||
return a.fov == b.fov and a.pose.position == b.pose.position and a.pose.orientation == b.pose.orientation;
|
||||
}
|
||||
|
||||
vk::CommandBuffer wivrn_foveation::update_foveation_buffer(
|
||||
vk::Buffer target,
|
||||
@@ -400,11 +433,16 @@ vk::CommandBuffer wivrn_foveation::update_foveation_buffer(
|
||||
}
|
||||
|
||||
// Check if the last value is still valid
|
||||
if (last.flip_y == flip_y and last.src[0] == source[0] and last.src[1] == source[1] and last.fovs[0] == fovs[0] and last.fovs[1] == fovs[1])
|
||||
return nullptr;
|
||||
|
||||
std::lock_guard lock(mutex);
|
||||
if (last.gaze == gaze and last.views[0] == views[0] and last.views[1] == views[1])
|
||||
if (last.flip_y == flip_y //
|
||||
and last.src[0] == source[0] //
|
||||
and last.src[1] == source[1] //
|
||||
and last.fovs[0] == fovs[0] //
|
||||
and last.fovs[1] == fovs[1] //
|
||||
and last.gaze == gaze //
|
||||
and std::abs(last.eye_x[0] - eye_x[0]) < 0.0005 //
|
||||
and std::abs(last.eye_x[1] - eye_x[1]) < 0.0005 //
|
||||
)
|
||||
return nullptr;
|
||||
|
||||
last = {
|
||||
@@ -412,7 +450,7 @@ vk::CommandBuffer wivrn_foveation::update_foveation_buffer(
|
||||
.flip_y = flip_y,
|
||||
.src = {source[0], source[1]},
|
||||
.fovs = {fovs[0], fovs[1]},
|
||||
.views = views,
|
||||
.eye_x = {eye_x[0], eye_x[1]},
|
||||
};
|
||||
|
||||
compute_params(source, fovs);
|
||||
|
||||
@@ -41,7 +41,12 @@ class wivrn_foveation
|
||||
const size_t foveated_width; // per eye
|
||||
const size_t foveated_height;
|
||||
|
||||
std::array<from_headset::tracking::view, 2> views = {};
|
||||
// Natural vertical gaze angle
|
||||
const float angle_offset;
|
||||
// Optionally defined from environment variables
|
||||
const float convergence_distance;
|
||||
|
||||
float eye_x[2] = {}; // eye x position
|
||||
xrt_quat gaze = {};
|
||||
std::array<to_headset::foveation_parameter, 2> params;
|
||||
|
||||
@@ -57,7 +62,7 @@ class wivrn_foveation
|
||||
bool flip_y = false;
|
||||
xrt_rect src[2] = {};
|
||||
xrt_fov fovs[2] = {};
|
||||
std::array<from_headset::tracking::view, 2> views = {};
|
||||
float eye_x[2] = {};
|
||||
};
|
||||
P last;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user