From fbda7aab230dfce5804c2d4bd3b27e7d146beefd Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Fri, 7 Aug 2020 22:20:01 -0400 Subject: [PATCH] Does just enough to get the correct (aspect ratio aside) output of scan outlines. So, up next, can I start streaming these things? --- .../Clock Signal/ScanTarget/CSScanTarget.mm | 12 +++++------ .../Clock Signal/ScanTarget/ScanTarget.metal | 21 ++++++++++++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm b/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm index a806938c7..76f774a6f 100644 --- a/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm +++ b/OSBindings/Mac/Clock Signal/ScanTarget/CSScanTarget.mm @@ -42,10 +42,10 @@ constexpr size_t NumBufferedScans = 2048; // Install the standard quad. constexpr float vertices[] = { - -0.9f, -0.9f, - -0.9f, 0.9f, - 0.9f, -0.9f, - 0.9f, 0.9f, + 0.0f, 0.0f, + 0.0f, 1.0f, + 1.0f, 0.0f, + 1.0f, 1.0f, }; _quadBuffer = [view.device newBufferWithBytes:vertices length:sizeof(vertices) options:MTLResourceCPUCacheModeDefaultCache]; @@ -53,7 +53,7 @@ constexpr size_t NumBufferedScans = 2048; _uniformsBuffer = [view.device newBufferWithLength:16 options:MTLResourceCPUCacheModeWriteCombined]; const Uniforms testUniforms = { .scale = {1024, 1024}, - .lineWidth = 0.1f + .lineWidth = 1.0f / 312.0f }; [self setUniforms:testUniforms]; @@ -99,7 +99,7 @@ constexpr size_t NumBufferedScans = 2048; scans[1].scan.end_points[0].x = 0; scans[1].scan.end_points[0].y = 768; scans[1].scan.end_points[1].x = 512; - scans[1].scan.end_points[1].y = 128; + scans[1].scan.end_points[1].y = 512; memcpy(_scansBuffer.contents, scans, sizeof(scans)); } diff --git a/OSBindings/Mac/Clock Signal/ScanTarget/ScanTarget.metal b/OSBindings/Mac/Clock Signal/ScanTarget/ScanTarget.metal index e95180d9c..e4610583e 100644 --- a/OSBindings/Mac/Clock Signal/ScanTarget/ScanTarget.metal +++ b/OSBindings/Mac/Clock Signal/ScanTarget/ScanTarget.metal @@ -53,8 +53,27 @@ vertex ColouredVertex scanVertexMain( QuadInputVertex vert [[stage_in]], constant Uniforms &uniforms [[buffer(1)]], constant Scan *scans [[buffer(2)]], ushort instance [[instance_id]]) { + // Unpack start and end vertices. + const float2 start = float2( + float(scans[instance].startPosition & 0xffff) / float(uniforms.scale.x), + float(scans[instance].startPosition >> 16) / float(uniforms.scale.y) + ); + const float2 end = float2( + float(scans[instance].endPosition & 0xffff) / float(uniforms.scale.x), + float(scans[instance].endPosition >> 16) / float(uniforms.scale.y) + ); + + // Calculate the tangent and normal. + const float2 tangent = end - start; + const float2 normal = float2(-tangent.y, tangent.x) / length(tangent); + + // Hence determine this quad's real shape. ColouredVertex output; - output.position = float4(vert.position * uniforms.lineWidth, 0.0, 1.0); + output.position = float4( + (start + vert.position.x * tangent + vert.position.y * normal * uniforms.lineWidth) * float2(2.0, -2.0) + float2(-1.0, 1.0), + 0.0, + 1.0 + ); return output; }