Use proper mipmaps for distortion shader

This commit is contained in:
Patrick Nicolas
2024-09-11 23:41:10 +02:00
parent 97aa8e3947
commit 50747d8fd4
6 changed files with 2894 additions and 377 deletions
@@ -1,302 +0,0 @@
From 27ead38036b39cae803fa03008e8d08b0ff73fb8 Mon Sep 17 00:00:00 2001
From: Patrick Nicolas <patricknicolas@laposte.net>
Date: Sun, 11 Aug 2024 18:37:48 +0200
Subject: [PATCH] c/compute: Prevent moire artefacts in distortion
If distortion shrinks some areas too much, the shader will only sample a
few pixels and cause moire.
Change the shader so that it averages values between samples.
This is similar to what mipmaps would do, except that creating mipmaps
on a compute queue is not trivial, and accessing them in compute shaders
may be complex too.
---
src/xrt/compositor/main/comp_renderer.c | 3 +
src/xrt/compositor/render/render_compute.c | 6 ++
src/xrt/compositor/render/render_interface.h | 3 +
src/xrt/compositor/shaders/distortion.comp | 87 ++++++++++++++++----
src/xrt/compositor/util/comp_render.h | 2 +
src/xrt/compositor/util/comp_render_cs.c | 8 ++
6 files changed, 91 insertions(+), 18 deletions(-)
diff --git a/src/xrt/compositor/main/comp_renderer.c b/src/xrt/compositor/main/comp_renderer.c
index 4716c8a71..a39e4275e 100644
--- a/src/xrt/compositor/main/comp_renderer.c
+++ b/src/xrt/compositor/main/comp_renderer.c
@@ -1038,6 +1038,9 @@ dispatch_compute(struct comp_renderer *r,
// Scratch image covers the whole image.
struct xrt_normalized_rect layer_norm_rect = {.x = 0.0f, .y = 0.0f, .w = 1.0f, .h = 1.0f};
+ data.cs.scratch_size[i].x = c->scratch.views[i].info.width;
+ data.cs.scratch_size[i].y = c->scratch.views[i].info.height;
+
comp_render_cs_add_view( //
&data, // data
&world_poses[i], // world_pose
diff --git a/src/xrt/compositor/render/render_compute.c b/src/xrt/compositor/render/render_compute.c
index dadb35ddc..3c3effd7d 100644
--- a/src/xrt/compositor/render/render_compute.c
+++ b/src/xrt/compositor/render/render_compute.c
@@ -455,6 +455,7 @@ render_compute_projection_timewarp(struct render_compute *crc,
VkSampler src_samplers[XRT_MAX_VIEWS],
VkImageView src_image_views[XRT_MAX_VIEWS],
const struct xrt_normalized_rect src_norm_rects[XRT_MAX_VIEWS],
+ const struct xrt_vec2 src_sizes[XRT_MAX_VIEWS],
const struct xrt_pose src_poses[XRT_MAX_VIEWS],
const struct xrt_fov src_fovs[XRT_MAX_VIEWS],
const struct xrt_pose new_poses[XRT_MAX_VIEWS],
@@ -488,6 +489,8 @@ render_compute_projection_timewarp(struct render_compute *crc,
data->pre_transforms[i] = r->distortion.uv_to_tanangle[i];
data->transforms[i] = time_warp_matrix[i];
data->post_transforms[i] = src_norm_rects[i];
+ data->source_sizes[i].x = src_sizes[i].x;
+ data->source_sizes[i].y = src_sizes[i].y;
}
/*
@@ -592,6 +595,7 @@ render_compute_projection(struct render_compute *crc,
VkSampler src_samplers[XRT_MAX_VIEWS],
VkImageView src_image_views[XRT_MAX_VIEWS],
const struct xrt_normalized_rect src_norm_rects[XRT_MAX_VIEWS],
+ const struct xrt_vec2 src_sizes[XRT_MAX_VIEWS],
VkImage target_image,
VkImageView target_image_view,
const struct render_viewport_data views[XRT_MAX_VIEWS])
@@ -611,6 +615,8 @@ render_compute_projection(struct render_compute *crc,
for (uint32_t i = 0; i < crc->r->view_count; ++i) {
data->views[i] = views[i];
data->post_transforms[i] = src_norm_rects[i];
+ data->source_sizes[i].x = src_sizes[i].x;
+ data->source_sizes[i].y = src_sizes[i].y;
}
diff --git a/src/xrt/compositor/render/render_interface.h b/src/xrt/compositor/render/render_interface.h
index f1a7a941a..fcb6dcc44 100644
--- a/src/xrt/compositor/render/render_interface.h
+++ b/src/xrt/compositor/render/render_interface.h
@@ -1227,6 +1227,7 @@ struct render_compute_distortion_ubo_data
struct xrt_normalized_rect pre_transforms[XRT_MAX_VIEWS];
struct xrt_normalized_rect post_transforms[XRT_MAX_VIEWS];
struct xrt_matrix_4x4 transforms[XRT_MAX_VIEWS];
+ struct xrt_rect_f32 source_sizes[XRT_MAX_VIEWS];
};
/*!
@@ -1294,6 +1295,7 @@ render_compute_projection_timewarp(struct render_compute *crc,
VkSampler src_samplers[XRT_MAX_VIEWS],
VkImageView src_image_views[XRT_MAX_VIEWS],
const struct xrt_normalized_rect src_rects[XRT_MAX_VIEWS],
+ const struct xrt_vec2 src_sizes[XRT_MAX_VIEWS],
const struct xrt_pose src_poses[XRT_MAX_VIEWS],
const struct xrt_fov src_fovs[XRT_MAX_VIEWS],
const struct xrt_pose new_poses[XRT_MAX_VIEWS],
@@ -1309,6 +1311,7 @@ render_compute_projection(struct render_compute *crc,
VkSampler src_samplers[XRT_MAX_VIEWS], //
VkImageView src_image_views[XRT_MAX_VIEWS], //
const struct xrt_normalized_rect src_rects[XRT_MAX_VIEWS], //
+ const struct xrt_vec2 src_sizes[XRT_MAX_VIEWS], //
VkImage target_image, //
VkImageView target_image_view, //
const struct render_viewport_data views[XRT_MAX_VIEWS]); //
diff --git a/src/xrt/compositor/shaders/distortion.comp b/src/xrt/compositor/shaders/distortion.comp
index 08b44d4cf..69d0a41b0 100644
--- a/src/xrt/compositor/shaders/distortion.comp
+++ b/src/xrt/compositor/shaders/distortion.comp
@@ -25,14 +25,12 @@ layout(set = 0, binding = 3, std140) uniform restrict Config
vec4 pre_transform[2];
vec4 post_transform[2];
mat4 transform[2];
+ vec2 src_sizes[2];
} ubo;
-vec2 position_to_uv(ivec2 extent, uint ix, uint iy)
+vec2 position_to_uv(ivec2 extent, vec2 xy)
{
- // Turn the index into floating point.
- vec2 xy = vec2(float(ix), float(iy));
-
// The inverse of the extent of the target image is the pixel size in [0 .. 1] space.
vec2 extent_pixel_size = vec2(1.0 / float(extent.x), 1.0 / float(extent.y));
@@ -102,6 +100,17 @@ vec2 transform_uv(vec2 uv, uint iz)
}
}
+vec2 distorted_uv(ivec2 extent, vec2 xy, uint iz)
+{
+ vec2 dist_uv = position_to_uv(extent, xy);
+
+ // Only read red channel, we don't do color aberration
+ vec2 r_uv = texture(distortion[iz + 0], dist_uv).xy;
+
+ // Do any transformation needed.
+ return transform_uv(r_uv, iz);
+}
+
void main()
{
uint ix = gl_GlobalInvocationID.x;
@@ -110,29 +119,71 @@ void main()
ivec2 offset = ivec2(ubo.views[iz].xy);
ivec2 extent = ivec2(ubo.views[iz].zw);
+ ivec2 src_extent = ivec2(ubo.src_sizes[iz]);
if (ix >= extent.x || iy >= extent.y) {
return;
}
- vec2 dist_uv = position_to_uv(extent, ix, iy);
+ // extent of the area to sample in normalized coordinates
+ vec2 u0 = distorted_uv(extent, max(vec2(0, 0), vec2(ix - 0.5, iy - 0.5)), iz);
+ vec2 u1 = distorted_uv(extent, min(extent, vec2(ix + 0.5, iy + 0.5)), iz);
- vec2 r_uv = texture(distortion[iz + 0], dist_uv).xy;
- vec2 g_uv = texture(distortion[iz + 2], dist_uv).xy;
- vec2 b_uv = texture(distortion[iz + 4], dist_uv).xy;
+ if (u0.y > u1.y)
+ {
+ float tmp = u0.y;
+ u0.y = u1.y;
+ u1.y = tmp;
+ }
- // Do any transformation needed.
- r_uv = transform_uv(r_uv, iz);
- g_uv = transform_uv(g_uv, iz);
- b_uv = transform_uv(b_uv, iz);
+ // extent in pixel coordinates
+ vec2 p0 = u0 * (src_extent - 1);
+ vec2 p1 = u1 * (src_extent - 1);
+
+ // shrink by 1/2 pixel to prevent overlap
+ p0 += vec2(0.5);
+ p1 -= vec2(0.5);
+
+ // iteration area
+ vec2 i0, i1;
+ // If there is less than one pixel, just sample it
+ if (p1.x - p0.x <= 1)
+ i0.x = i1.x = distorted_uv(extent, vec2(ix, iy), iz).x * src_extent.x;
+ else
+ {
+ i0.x = floor(p0.x);
+ i1.x = ceil(p1.x);
+ }
+ if (p1.y - p0.y <= 1)
+ i0.y = i1.y = distorted_uv(extent, vec2(ix, iy), iz).y * src_extent.y;
+ else
+ {
+ i0.y = floor(p0.y);
+ i1.y = ceil(p1.y);
+ }
- // Sample the source with distorted and chromatic-aberration corrected samples.
- vec4 colour = vec4(
- texture(source[iz], r_uv).r,
- texture(source[iz], g_uv).g,
- texture(source[iz], b_uv).b,
- 1);
+ vec4 colour = vec4(0, 0, 0, 1);
+
+ float count = 0;
+ for (float y = i0.y ; y <= i1.y ; y++) {
+ float wy = 1;
+ if (y < p0.y)
+ wy *= 1 - (p0.y - y);
+ if (y > p1.y)
+ wy *= 1 - (y - p1.y);
+ for (float x = i0.x; x <= i1.x; x++) {
+ float w = wy;
+ if (x < p0.x)
+ w *= 1 - (p0.x - x);
+ if (x > p1.x)
+ w *= 1 - (x - p1.x);
+ vec2 uv = (vec2(x, y) + 0.5) / src_extent;
+ colour.rgb += w * texture(source[iz], uv).rgb;
+ count += w;
+ }
+ }
+ colour /= count;
// Do colour correction here since there are no automatic conversion in hardware available.
colour = vec4(from_linear_to_srgb(colour.rgb), 1);
diff --git a/src/xrt/compositor/util/comp_render.h b/src/xrt/compositor/util/comp_render.h
index 653cc0e3d..384b304d7 100644
--- a/src/xrt/compositor/util/comp_render.h
+++ b/src/xrt/compositor/util/comp_render.h
@@ -126,6 +126,8 @@ struct comp_render_dispatch_data
// Target image view for distortion.
VkImageView target_unorm_view;
+
+ struct xrt_vec2 scratch_size[XRT_MAX_VIEWS];
} cs;
};
diff --git a/src/xrt/compositor/util/comp_render_cs.c b/src/xrt/compositor/util/comp_render_cs.c
index 8201407ae..ea9d1f258 100644
--- a/src/xrt/compositor/util/comp_render_cs.c
+++ b/src/xrt/compositor/util/comp_render_cs.c
@@ -350,6 +350,7 @@ do_cs_distortion_from_scratch(struct render_compute *crc, const struct comp_rend
VkImageView src_image_views[XRT_MAX_VIEWS];
VkSampler src_samplers[XRT_MAX_VIEWS];
struct xrt_normalized_rect src_norm_rects[XRT_MAX_VIEWS];
+ struct xrt_vec2 src_sizes[XRT_MAX_VIEWS];
for (uint32_t i = 0; i < d->view_count; i++) {
// Data to be filled in.
@@ -367,6 +368,7 @@ do_cs_distortion_from_scratch(struct render_compute *crc, const struct comp_rend
src_image_views[i] = src_image_view;
src_samplers[i] = clamp_to_border_black;
src_norm_rects[i] = src_norm_rect;
+ src_sizes[i] = d->cs.scratch_size[i];
}
render_compute_projection( //
@@ -374,6 +376,7 @@ do_cs_distortion_from_scratch(struct render_compute *crc, const struct comp_rend
src_samplers, // src_samplers
src_image_views, // src_image_views
src_norm_rects, // src_rects
+ src_sizes, // src_sizes
d->cs.target_image, // target_image
d->cs.target_unorm_view, // target_image_view
target_viewport_datas); // views
@@ -402,6 +405,7 @@ do_cs_distortion_for_layer(struct render_compute *crc,
struct xrt_normalized_rect src_norm_rects[XRT_MAX_VIEWS];
struct xrt_pose src_poses[XRT_MAX_VIEWS];
struct xrt_fov src_fovs[XRT_MAX_VIEWS];
+ struct xrt_vec2 src_sizes[XRT_MAX_VIEWS];
VkSampler src_samplers[XRT_MAX_VIEWS];
VkImageView src_image_views[XRT_MAX_VIEWS];
@@ -435,6 +439,8 @@ do_cs_distortion_for_layer(struct render_compute *crc,
src_norm_rects[i] = src_norm_rect;
src_poses[i] = src_pose;
src_fovs[i] = src_fov;
+ src_sizes[i].x = layer->sc_array[i]->vkic.info.width;
+ src_sizes[i].y = layer->sc_array[i]->vkic.info.height;
src_samplers[i] = clamp_to_border_black;
src_image_views[i] = src_image_view;
}
@@ -445,6 +451,7 @@ do_cs_distortion_for_layer(struct render_compute *crc,
src_samplers, //
src_image_views, //
src_norm_rects, //
+ src_sizes, //
d->cs.target_image, //
d->cs.target_unorm_view, //
target_viewport_datas); //
@@ -454,6 +461,7 @@ do_cs_distortion_for_layer(struct render_compute *crc,
src_samplers, //
src_image_views, //
src_norm_rects, //
+ src_sizes, //
src_poses, //
src_fovs, //
world_poses, //
--
2.46.0
File diff suppressed because it is too large Load Diff
+63 -74
View File
@@ -49,14 +49,19 @@ static const std::array pool_sizes =
{
vk::DescriptorPoolSize{
.type = vk::DescriptorType::eStorageImage,
.descriptorCount = 2,
.descriptorCount = 4,
}};
static const vk::DescriptorSetLayoutBinding layout_bindings{
.binding = 0,
.descriptorType = vk::DescriptorType::eStorageImage,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eCompute};
static const std::array<vk::DescriptorSetLayoutBinding, 2> layout_bindings{
vk::DescriptorSetLayoutBinding{.binding = 0,
.descriptorType = vk::DescriptorType::eStorageImage,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eCompute},
vk::DescriptorSetLayoutBinding{.binding = 1,
.descriptorType = vk::DescriptorType::eStorageImage,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eCompute},
};
wivrn_foveation_renderer::wivrn_foveation_renderer(wivrn_vk_bundle & vk, vk::raii::CommandPool & cmd_pool) :
vk(vk),
@@ -67,7 +72,7 @@ wivrn_foveation_renderer::wivrn_foveation_renderer(wivrn_vk_bundle & vk, vk::rai
.poolSizeCount = pool_sizes.size(),
.pPoolSizes = pool_sizes.data(),
}),
ds_layout(vk.device, vk::DescriptorSetLayoutCreateInfo{.bindingCount = 1, .pBindings = &layout_bindings})
ds_layout(vk.device, vk::DescriptorSetLayoutCreateInfo{.bindingCount = layout_bindings.size(), .pBindings = layout_bindings.data()})
{
cmd_buf = std::move(vk.device.allocateCommandBuffers(
{.commandPool = *cmd_pool,
@@ -141,32 +146,21 @@ void wivrn_foveation_renderer::render_distortion_images(std::array<to_headset::f
cmd_buf.reset();
cmd_buf.begin(vk::CommandBufferBeginInfo{});
std::array im_barriers0 = {
vk::ImageMemoryBarrier{
.srcAccessMask = vk::AccessFlagBits::eNone,
.dstAccessMask = vk::AccessFlagBits::eMemoryWrite,
.oldLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
.newLayout = vk::ImageLayout::eGeneral,
.image = images[0],
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1},
},
vk::ImageMemoryBarrier{
.srcAccessMask = vk::AccessFlagBits::eNone,
.dstAccessMask = vk::AccessFlagBits::eMemoryWrite,
.oldLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
.newLayout = vk::ImageLayout::eGeneral,
.image = images[1],
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1},
},
};
// images: left position, right position, left derivate, right derivate
std::array<vk::ImageMemoryBarrier, 4> im_barriers;
im_barriers.fill({
.srcAccessMask = vk::AccessFlagBits::eNone,
.dstAccessMask = vk::AccessFlagBits::eMemoryWrite,
.oldLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
.newLayout = vk::ImageLayout::eGeneral,
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1},
});
for (size_t i = 0; i < im_barriers.size(); ++i)
im_barriers[i].image = images[i];
cmd_buf.pipelineBarrier(
vk::PipelineStageFlagBits::eTopOfPipe,
@@ -174,26 +168,40 @@ void wivrn_foveation_renderer::render_distortion_images(std::array<to_headset::f
{},
nullptr,
nullptr,
im_barriers0);
im_barriers);
// Monado's distortion shader is patched to only consider the red channel's image
// Monado's distortion shader is patched to read red as position and green as derivate
for (int eye = 0; eye < 2; ++eye)
{
// Descriptor set
vk::DescriptorImageInfo image_info{
.imageView = image_views[eye],
.imageLayout = vk::ImageLayout::eGeneral,
};
vk.device.updateDescriptorSets(
vk::WriteDescriptorSet{
.dstSet = ds[eye],
.dstBinding = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eStorageImage,
.pImageInfo = &image_info,
std::array image_info{
vk::DescriptorImageInfo{
.imageView = image_views[eye],
.imageLayout = vk::ImageLayout::eGeneral,
},
nullptr);
vk::DescriptorImageInfo{
.imageView = image_views[eye + 2],
.imageLayout = vk::ImageLayout::eGeneral,
},
};
vk.device.updateDescriptorSets({
vk::WriteDescriptorSet{
.dstSet = ds[eye],
.dstBinding = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eStorageImage,
.pImageInfo = &image_info[0],
},
vk::WriteDescriptorSet{
.dstSet = ds[eye],
.dstBinding = 1,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eStorageImage,
.pImageInfo = &image_info[1],
},
},
nullptr);
auto foveation = foveations[eye];
@@ -210,32 +218,13 @@ void wivrn_foveation_renderer::render_distortion_images(std::array<to_headset::f
cmd_buf.dispatch(dispatch_group_count, dispatch_group_count, 1);
}
std::array im_barriers1 = {
vk::ImageMemoryBarrier{
.srcAccessMask = vk::AccessFlagBits::eMemoryWrite,
.dstAccessMask = vk::AccessFlagBits::eNone,
.oldLayout = vk::ImageLayout::eGeneral,
.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
.image = images[0],
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1},
},
vk::ImageMemoryBarrier{
.srcAccessMask = vk::AccessFlagBits::eMemoryWrite,
.dstAccessMask = vk::AccessFlagBits::eNone,
.oldLayout = vk::ImageLayout::eGeneral,
.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
.image = images[1],
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1},
},
};
for (auto & barrier: im_barriers)
{
barrier.srcAccessMask = vk::AccessFlagBits::eMemoryWrite;
barrier.dstAccessMask = vk::AccessFlagBits::eNone;
barrier.oldLayout = vk::ImageLayout::eGeneral;
barrier.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
}
cmd_buf.pipelineBarrier(
vk::PipelineStageFlagBits::eComputeShader,
@@ -243,7 +232,7 @@ void wivrn_foveation_renderer::render_distortion_images(std::array<to_headset::f
{},
nullptr,
nullptr,
im_barriers1);
im_barriers);
cmd_buf.end();
}
+16 -1
View File
@@ -87,6 +87,12 @@ static double foveate(double a, double b, double λ, double c, double x)
// df⁻¹(x)/dx = 1/scale for x = c
}
static double foveate_lod(double a, double b, double /*λ*/, double /*c*/, double x)
{
// derivate of foveate * scale
return std::max(0., log2(1 / (cos(a * x + b) * cos(a * x + b))));
}
static std::tuple<float, float> solve_foveation(float λ, float c)
{
// Compute a and b for the foveation function such that:
@@ -150,17 +156,20 @@ bool wivrn_hmd::wivrn_hmd_compute_distortion(xrt_device * xdev, uint32_t view_in
// result is in the input coordinates (from the application)
const auto & param = ((wivrn_hmd *)xdev)->foveation_parameters[view_index];
xrt_vec2 out;
xrt_vec2 lod;
if (param.x.scale < 1)
{
u = 2 * u - 1;
out.x = foveate(param.x.a, param.x.b, param.x.scale, param.x.center, u);
lod.x = foveate_lod(param.x.a, param.x.b, param.x.scale, param.x.center, u);
out.x = std::clamp<float>((1 + out.x) / 2, 0, 1);
}
else
{
out.x = u;
lod.x = 0;
}
if (param.y.scale < 1)
@@ -168,15 +177,19 @@ bool wivrn_hmd::wivrn_hmd_compute_distortion(xrt_device * xdev, uint32_t view_in
v = 2 * v - 1;
out.y = foveate(param.y.a, param.y.b, param.y.scale, param.y.center, v);
lod.y = foveate_lod(param.y.a, param.y.b, param.y.scale, param.y.center, v);
out.y = std::clamp<float>((1 + out.y) / 2, 0, 1);
}
else
{
out.y = v;
lod.y = 0;
}
U_LOG_W("%f %f (%f %f)", lod.x, lod.y, u, v);
result->r = out;
result->g = out;
result->g = lod;
result->b = out;
return true;
@@ -216,7 +229,9 @@ wivrn_hmd::wivrn_hmd(std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx,
const auto config = configuration::read_user_configuration();
auto eye_width = info.recommended_eye_width;
eye_width = ((eye_width + 3) / 4) * 4;
auto eye_height = info.recommended_eye_height;
eye_height = ((eye_height + 3) / 4) * 4;
// Setup info.
hmd->view_count = 2;
+3
View File
@@ -259,6 +259,9 @@ int inner_main(int argc, char * argv[], bool use_systemd, bool show_instructions
// there is no need for oversampling.
setenv("XRT_COMPOSITOR_SCALE_PERCENTAGE", "100", false);
// Enable mipmaps for distortion
setenv("XRT_DISTORTION_MIP_LEVELS", "0", false);
// FIXME: synchronization fails on gfx pipeline
setenv("XRT_COMPOSITOR_COMPUTE", "1", true);
+9
View File
@@ -21,6 +21,7 @@
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
layout(set = 0, binding = 0, rgba8) uniform writeonly restrict image2D target;
layout(set = 0, binding = 1, rgba8) uniform writeonly restrict image2D dtarget;
layout(push_constant) uniform PushConstants
{
@@ -36,6 +37,10 @@ float foveate(float a, float b, float scale, float c, float x) {
return scale / a * tan(a * x + b) + c;
}
float foveate_lod(float a, float b, float scale, float c, float x) {
return max(0, log2(1 / (cos(a * x + b) * cos(a * x + b))));
}
void main()
{
ivec2 coords = ivec2(gl_GlobalInvocationID.x, gl_GlobalInvocationID.y);
@@ -44,18 +49,22 @@ void main()
float(coords.x),
float(coords.y)
) * one_over_dim;
vec2 duv = vec2(1.0);
if (pcs.scale.x < 1.0) {
float u = uv.x * 2.0 - 1.0;
float val = foveate(pcs.a.x, pcs.b.x, pcs.scale.x, pcs.center.x, u);
duv.x = foveate_lod(pcs.a.x, pcs.b.x, pcs.scale.x, pcs.center.x, u);
uv.x = clamp((1.0 + val) / 2.0, 0.0, 1.0);
}
if (pcs.scale.y < 1.0) {
float v = uv.y * 2.0 - 1.0;
float val = foveate(pcs.a.y, pcs.b.y, pcs.scale.y, pcs.center.y, v);
duv.y= foveate_lod(pcs.a.y, pcs.b.y, pcs.scale.y, pcs.center.y, v);
uv.y = clamp((1.0 + val) / 2.0, 0.0, 1.0);
}
imageStore(target, coords, vec4(uv.x, uv.y, 0.0, 0.0));
imageStore(dtarget, coords, vec4(duv.x, duv.y, 0.0, 0.0));
}