Merge pull request #275 from zed-industries/fix-selection-artifacts

Antonio Scandurra created

Use 16-bit float to store path windings

Change summary

crates/gpui/src/platform/mac/renderer.rs           | 4 ++--
crates/gpui/src/platform/mac/shaders/shaders.metal | 6 ++----
2 files changed, 4 insertions(+), 6 deletions(-)

Detailed changes

crates/gpui/src/platform/mac/renderer.rs 🔗

@@ -107,7 +107,7 @@ impl Renderer {
             "path_atlas",
             "path_atlas_vertex",
             "path_atlas_fragment",
-            MTLPixelFormat::R8Unorm,
+            MTLPixelFormat::R16Float,
         );
         Self {
             sprite_cache,
@@ -827,7 +827,7 @@ fn build_path_atlas_texture_descriptor() -> metal::TextureDescriptor {
     let texture_descriptor = metal::TextureDescriptor::new();
     texture_descriptor.set_width(2048);
     texture_descriptor.set_height(2048);
-    texture_descriptor.set_pixel_format(MTLPixelFormat::R8Unorm);
+    texture_descriptor.set_pixel_format(MTLPixelFormat::R16Float);
     texture_descriptor
         .set_usage(metal::MTLTextureUsage::RenderTarget | metal::MTLTextureUsage::ShaderRead);
     texture_descriptor.set_storage_mode(metal::MTLStorageMode::Private);

crates/gpui/src/platform/mac/shaders/shaders.metal 🔗

@@ -205,8 +205,6 @@ vertex SpriteFragmentInput sprite_vertex(
     };
 }
 
-#define MAX_WINDINGS 32.
-
 fragment float4 sprite_fragment(
     SpriteFragmentInput input [[stage_in]],
     texture2d<float> atlas [[ texture(GPUISpriteFragmentInputIndexAtlas) ]]
@@ -216,7 +214,7 @@ fragment float4 sprite_fragment(
     float4 sample = atlas.sample(atlas_sampler, input.atlas_position);
     float mask;
     if (input.compute_winding) {
-        mask = 1. - abs(1. - fmod(sample.r * MAX_WINDINGS, 2.));
+        mask = 1. - abs(1. - fmod(sample.r, 2.));
     } else {
         mask = sample.a;
     }
@@ -303,6 +301,6 @@ fragment float4 path_atlas_fragment(
     );
     float f = (input.st_position.x * input.st_position.x) - input.st_position.y;
     float distance = f / length(gradient);
-    float alpha = saturate(0.5 - distance) / MAX_WINDINGS;
+    float alpha = saturate(0.5 - distance);
     return float4(alpha, 0., 0., 1.);
 }