From 9da79d2d8155c9fb62c274f2395850607afb5b8c Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Mon, 17 Aug 2020 20:29:46 -0400 Subject: [PATCH] Clarifies scaling logic. --- .../Mac/Clock Signal/ScanTarget/CSScanTarget.mm | 6 +++--- .../Mac/Clock Signal/ScanTarget/ScanTarget.metal | 11 +++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm b/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm index 31f82e7aa..ed73e99f9 100644 --- a/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm +++ b/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm @@ -194,12 +194,12 @@ using BufferingScanTarget = Outputs::Display::BufferingScanTarget; // Set the aspect ratio multiplier. uniforms()->aspectRatioMultiplier = float(modals.aspect_ratio / viewAspectRatio); - // Also work out the proper zoom. Somehow? + // Also work out the proper zoom. const double fitWidthZoom = (viewAspectRatio / modals.aspect_ratio) / modals.visible_area.size.width; const double fitHeightZoom = 1.0 / modals.visible_area.size.height; - - // The differing signs for offset below reflect the inverted coordinate system in Metal. uniforms()->zoom = float(std::min(fitWidthZoom, fitHeightZoom)); + + // Store the offset. uniforms()->offset.x = -modals.visible_area.origin.x; uniforms()->offset.y = -modals.visible_area.origin.y; } diff --git a/OSBindings/Mac/Clock Signal/ScanTarget/ScanTarget.metal b/OSBindings/Mac/Clock Signal/ScanTarget/ScanTarget.metal index 32faf8b6c..ac11d6ffc 100644 --- a/OSBindings/Mac/Clock Signal/ScanTarget/ScanTarget.metal +++ b/OSBindings/Mac/Clock Signal/ScanTarget/ScanTarget.metal @@ -101,9 +101,16 @@ vertex SourceInterpolator scanToDisplay( constant Uniforms &uniforms [[buffer(1) ) / 32.0; // Hence determine this quad's real shape, using vertexID to pick a corner. + + // position2d is now in the range [0, 1]. float2 position2d = start + (float(vertexID&2) * 0.5) * tangent + (float(vertexID&1) - 0.5) * normal * uniforms.lineWidth; - position2d = (position2d + uniforms.offset) * uniforms.zoom * float2(uniforms.aspectRatioMultiplier, 1.0); - position2d = position2d * float2(2.0, -2.0) + float2(-1.0, 1.0); + + // Apply the requested offset and zoom, to map the desired area to the range [0, 1]. + position2d = (position2d + uniforms.offset) * uniforms.zoom; + + // Remap from [0, 1] to Metal's [-1, 1] and then apply the aspect ratio correction. + position2d = (position2d * float2(2.0, -2.0) + float2(-1.0, 1.0)) * float2(uniforms.aspectRatioMultiplier, 1.0); + output.position = float4( position2d, 0.0,