From b1fdb157d0f3b6274e083c97987f655167c44cb1 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Thu, 26 Nov 2026 01:18:03 +0200 Subject: [PATCH 1/2] context: clamp to_window_coords result to window bounds Without the clamp a widget corner outside the window (e.g. when a ruis::window is dragged past the app window's left or top edge) produces a negative value which wraps to a huge number when cast to uint32_t, corrupting the scissor rectangle so the widget's contents disappear instead of being clipped at the window edge (the GL variant already clamps). --- src/ruis/render/vulkan/context.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/ruis/render/vulkan/context.cpp b/src/ruis/render/vulkan/context.cpp index d5ce288..999d98b 100644 --- a/src/ruis/render/vulkan/context.cpp +++ b/src/ruis/render/vulkan/context.cpp @@ -21,6 +21,7 @@ along with this program. If not, see . #include "context.hpp" +#include #include #include @@ -725,9 +726,15 @@ r4::vector2 context::to_window_coords(const ruis::vec2& point) const const auto p = r4::vector2(point.x(), point.y()); const auto w = this->window_dims.x(); const auto h = this->window_dims.y(); + // clamp the result to the window bounds (same as the GL variant): without the + // clamp a point outside the window would produce a negative value which, when + // cast to uint32_t, wraps around to a huge number and corrupts the scissor + // rectangle (the widget's contents would disappear instead of being clipped) + const auto fx = std::min(static_cast(w), std::max(0.0f, (p.x() + 1.0f) * ndc_to_unit_scale * static_cast(w))); + const auto fy = std::min(static_cast(h), std::max(0.0f, (p.y() + 1.0f) * ndc_to_unit_scale * static_cast(h))); r4::vector2 result; - result.x() = static_cast((p.x() + 1.0f) * ndc_to_unit_scale * static_cast(w)); - result.y() = static_cast((p.y() + 1.0f) * ndc_to_unit_scale * static_cast(h)); + result.x() = static_cast(fx); + result.y() = static_cast(fy); return result; } From a42ddf1136222b48af2f09e0f5c8203bc808ed27 Mon Sep 17 00:00:00 2001 From: Ivan Gagis Date: Thu, 26 Nov 2026 01:31:42 +0200 Subject: [PATCH 2/2] context: address review - do the NDC->window conversion in ruis::real using vector ops --- src/ruis/render/vulkan/context.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/ruis/render/vulkan/context.cpp b/src/ruis/render/vulkan/context.cpp index 999d98b..00dce02 100644 --- a/src/ruis/render/vulkan/context.cpp +++ b/src/ruis/render/vulkan/context.cpp @@ -44,7 +44,7 @@ using namespace ruis::render::vulkan; namespace { // maps NDC (-1 .. 1) to the unit range (0 .. 1) -constexpr float ndc_to_unit_scale = 0.5f; +constexpr ruis::real ndc_to_unit_scale = ruis::real(0.5); // number of bits per blend factor in the packed blend key (15 values fit in 4, // use 5 for simplicity) @@ -723,19 +723,15 @@ void context::clear_framebuffer_stencil() r4::vector2 context::to_window_coords(const ruis::vec2& point) const { // NDC -> window space using the swapchain (physical) dimensions - const auto p = r4::vector2(point.x(), point.y()); - const auto w = this->window_dims.x(); - const auto h = this->window_dims.y(); + const auto dims = this->window_dims.to(); // clamp the result to the window bounds (same as the GL variant): without the // clamp a point outside the window would produce a negative value which, when // cast to uint32_t, wraps around to a huge number and corrupts the scissor // rectangle (the widget's contents would disappear instead of being clipped) - const auto fx = std::min(static_cast(w), std::max(0.0f, (p.x() + 1.0f) * ndc_to_unit_scale * static_cast(w))); - const auto fy = std::min(static_cast(h), std::max(0.0f, (p.y() + 1.0f) * ndc_to_unit_scale * static_cast(h))); - r4::vector2 result; - result.x() = static_cast(fx); - result.y() = static_cast(fy); - return result; + auto res = (point + ruis::vec2(1, 1)) * ndc_to_unit_scale; + res.comp_multiply(dims); + res = min(dims, max(res, ruis::vec2(0, 0))); + return round(res).to(); } bool context::is_scissor_enabled() const noexcept