Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gpui: Enable MSAA to Path render for Anti-Aliasing #22812

Merged
merged 19 commits into from
Jan 29, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update
huacnlee committed Jan 10, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit d8f6dddf2a2322070847adb99056d8ca2f15c78c
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -352,9 +352,9 @@ async-watch = "0.3.1"
async_zip = { version = "0.0.17", features = ["deflate", "deflate64"] }
base64 = "0.22"
bitflags = "2.6.0"
blade-graphics = { git = "https://github.com/kvark/blade", rev = "091a8401033847bb9b6ace3fcf70448d069621c5" }
blade-macros = { git = "https://github.com/kvark/blade", rev = "091a8401033847bb9b6ace3fcf70448d069621c5" }
blade-util = { git = "https://github.com/kvark/blade", rev = "091a8401033847bb9b6ace3fcf70448d069621c5" }
blade-graphics = { git = "https://github.com/kvark/blade", rev = "b16f5c7bd873c7126f48c82c39e7ae64602ae74f" }
blade-macros = { git = "https://github.com/kvark/blade", rev = "b16f5c7bd873c7126f48c82c39e7ae64602ae74f" }
blade-util = { git = "https://github.com/kvark/blade", rev = "b16f5c7bd873c7126f48c82c39e7ae64602ae74f" }
blake3 = "1.5.3"
bytes = "1.0"
cargo_metadata = "0.19"
2 changes: 1 addition & 1 deletion crates/gpui/Cargo.toml
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ license = "Apache-2.0"
workspace = true

[features]
default = ["http_client", "font-kit", "wayland", "x11"]
default = ["http_client", "font-kit", "wayland", "x11", "macos-blade"]
huacnlee marked this conversation as resolved.
Show resolved Hide resolved
test-support = [
"backtrace",
"collections/test-support",
60 changes: 31 additions & 29 deletions crates/gpui/src/platform/blade/blade_atlas.rs
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ struct BladeAtlasState {
tiles_by_key: FxHashMap<AtlasKey, AtlasTile>,
initializations: Vec<AtlasTextureId>,
uploads: Vec<PendingUpload>,
sample_count: u32,
path_sample_count: u32,
}

#[cfg(gles)]
@@ -47,7 +47,7 @@ pub struct BladeTextureInfo {
}

impl BladeAtlas {
pub(crate) fn new(gpu: &Arc<gpu::Context>, sample_count: u32) -> Self {
pub(crate) fn new(gpu: &Arc<gpu::Context>, path_sample_count: u32) -> Self {
BladeAtlas(Mutex::new(BladeAtlasState {
gpu: Arc::clone(gpu),
upload_belt: BufferBelt::new(BufferBeltDescriptor {
@@ -59,7 +59,7 @@ impl BladeAtlas {
tiles_by_key: Default::default(),
initializations: Vec::new(),
uploads: Vec::new(),
sample_count,
path_sample_count,
}))
}

@@ -208,32 +208,10 @@ impl BladeAtlasState {
}
}

let raw = self.gpu.create_texture(gpu::TextureDesc {
name: "atlas",
format,
size: gpu::Extent {
width: size.width.into(),
height: size.height.into(),
depth: 1,
},
array_layer_count: 1,
mip_level_count: 1,
sample_count: 1,
dimension: gpu::TextureDimension::D2,
usage,
});
let raw_view = self.gpu.create_texture_view(
raw,
gpu::TextureViewDesc {
name: "",
format,
dimension: gpu::ViewDimension::D2,
subresources: &Default::default(),
},
);
let msaa_view = if self.sample_count > 1 {
// We currently only enable MSAA for path textures.
let msaa_view = if self.path_sample_count > 1 && kind == AtlasTextureKind::Path {
let msaa = self.gpu.create_texture(gpu::TextureDesc {
name: "msaa texture",
name: "msaa path texture",
format,
size: gpu::Extent {
width: size.width.into(),
@@ -242,7 +220,7 @@ impl BladeAtlasState {
},
array_layer_count: 1,
mip_level_count: 1,
sample_count: self.sample_count,
sample_count: self.path_sample_count,
dimension: gpu::TextureDimension::D2,
usage: gpu::TextureUsage::TARGET,
});
@@ -260,6 +238,30 @@ impl BladeAtlasState {
None
};

let raw = self.gpu.create_texture(gpu::TextureDesc {
name: "atlas",
format,
size: gpu::Extent {
width: size.width.into(),
height: size.height.into(),
depth: 1,
},
array_layer_count: 1,
mip_level_count: 1,
sample_count: 1,
dimension: gpu::TextureDimension::D2,
usage,
});
let raw_view = self.gpu.create_texture_view(
raw,
gpu::TextureViewDesc {
name: "",
format,
dimension: gpu::ViewDimension::D2,
subresources: &Default::default(),
},
);

let texture_list = &mut self.storage[kind];
let index = texture_list.free_list.pop();

25 changes: 12 additions & 13 deletions crates/gpui/src/platform/blade/blade_renderer.rs
Original file line number Diff line number Diff line change
@@ -13,8 +13,6 @@ use bytemuck::{Pod, Zeroable};
use collections::HashMap;
#[cfg(target_os = "macos")]
use media::core_video::CVMetalTextureCache;
#[cfg(target_os = "macos")]
use objc2_metal::MTLDevice;
use std::{mem, sync::Arc};

const MAX_FRAME_TIME_MS: u32 = 10000;
@@ -129,7 +127,7 @@ struct BladePipelines {
}

impl BladePipelines {
fn new(gpu: &gpu::Context, surface_info: gpu::SurfaceInfo, sample_count: u32) -> Self {
fn new(gpu: &gpu::Context, surface_info: gpu::SurfaceInfo, path_sample_count: u32) -> Self {
use gpu::ShaderData as _;

log::info!(
@@ -210,7 +208,7 @@ impl BladePipelines {
write_mask: gpu::ColorWrites::default(),
}],
multisample_state: gpu::MultisampleState {
sample_count,
sample_count: path_sample_count,
..Default::default()
},
}),
@@ -343,21 +341,22 @@ impl BladeRenderer {
.create_surface_configured(window, surface_config)
.unwrap();

// Determine the sample count based on the device's capabilities.
// macOS use 4x MSAA, all devices support it.
// https://developer.apple.com/documentation/metal/mtldevice/1433355-supportstexturesamplecount
#[cfg(target_os = "macos")]
let sample_count = 4;
huacnlee marked this conversation as resolved.
Show resolved Hide resolved

// Determine on non-macOS platforms, until Blade supports querying sample counts.
#[cfg(not(target_os = "macos"))]
let mut sample_count = 1;
#[cfg(target_os = "macos")]
#[cfg(not(target_os = "macos"))]
for &n in &[4, 2] {
if context.gpu.metal_device().supportsTextureSampleCount(n) {
if context.gpu.supports_texture_sample_count(n) {
sample_count = n as _;
break;
}
}

// TODO: Determine on non-macOS platforms, until Blade supports querying sample counts.
#[cfg(not(target_os = "macos"))]
let sample_count = 4;

let command_encoder = context.gpu.create_command_encoder(gpu::CommandEncoderDesc {
name: "main",
buffer_count: 2,
@@ -519,7 +518,7 @@ impl BladeRenderer {

let vertex_buf = unsafe { self.instance_belt.alloc_typed(&vertices, &self.gpu) };
let frame_view = tex_info.raw_view;
let render_target = if let Some(msaa_view) = tex_info.msaa_view {
let color_target = if let Some(msaa_view) = tex_info.msaa_view {
gpu::RenderTargetSet {
huacnlee marked this conversation as resolved.
Show resolved Hide resolved
colors: &[gpu::RenderTarget {
view: msaa_view,
@@ -539,7 +538,7 @@ impl BladeRenderer {
}
};

if let mut pass = self.command_encoder.render("paths", render_target) {
if let mut pass = self.command_encoder.render("paths", color_target) {
let mut encoder = pass.with(&self.pipelines.path_rasterization);
encoder.bind(
0,
9 changes: 5 additions & 4 deletions crates/gpui/src/platform/mac/metal_atlas.rs
Original file line number Diff line number Diff line change
@@ -13,14 +13,14 @@ use std::borrow::Cow;
pub(crate) struct MetalAtlas(Mutex<MetalAtlasState>);

impl MetalAtlas {
pub(crate) fn new(device: Device, sample_count: u32) -> Self {
pub(crate) fn new(device: Device, path_sample_count: u32) -> Self {
MetalAtlas(Mutex::new(MetalAtlasState {
device: AssertSend(device),
monochrome_textures: Default::default(),
polychrome_textures: Default::default(),
path_textures: Default::default(),
tiles_by_key: Default::default(),
sample_count,
path_sample_count,
}))
}

@@ -59,7 +59,7 @@ struct MetalAtlasState {
polychrome_textures: AtlasTextureList<MetalAtlasTexture>,
path_textures: AtlasTextureList<MetalAtlasTexture>,
tiles_by_key: FxHashMap<AtlasKey, AtlasTile>,
sample_count: u32,
path_sample_count: u32,
}

impl PlatformAtlas for MetalAtlas {
@@ -182,7 +182,8 @@ impl MetalAtlasState {
texture_descriptor.set_usage(usage);
let metal_texture = self.device.new_texture(&texture_descriptor);

let msaa_texture = if self.sample_count > 1 {
// We currently only enable MSAA for path textures.
let msaa_texture = if self.path_sample_count > 1 && kind == AtlasTextureKind::Path {
let mut descriptor = texture_descriptor.clone();
descriptor.set_texture_type(metal::MTLTextureType::D2Multisample);
descriptor.set_storage_mode(metal::MTLStorageMode::Private);
19 changes: 7 additions & 12 deletions crates/gpui/src/platform/mac/metal_renderer.rs
Original file line number Diff line number Diff line change
@@ -107,6 +107,10 @@ pub(crate) struct MetalRenderer {
core_video_texture_cache: CVMetalTextureCache,
}

// macOS use 4x MSAA, all devices support it.
// https://developer.apple.com/documentation/metal/mtldevice/1433355-supportstexturesamplecount
const SAMPLE_COUNT: u32 = 4;

impl MetalRenderer {
pub fn new(instance_buffer_pool: Arc<Mutex<InstanceBufferPool>>) -> Self {
// Prefer low‐power integrated GPUs on Intel Mac. On Apple
@@ -119,15 +123,6 @@ impl MetalRenderer {
std::process::exit(1);
};

// Determine the sample count based on the device's capabilities.
let mut sample_count = 1;
for &n in &[4, 2] {
if device.supports_texture_sample_count(n) {
sample_count = n as _;
break;
}
}

let layer = metal::MetalLayer::new();
layer.set_device(&device);
layer.set_pixel_format(MTLPixelFormat::BGRA8Unorm);
@@ -179,7 +174,7 @@ impl MetalRenderer {
"path_rasterization_vertex",
"path_rasterization_fragment",
MTLPixelFormat::R16Float,
sample_count,
SAMPLE_COUNT,
);
let path_sprites_pipeline_state = build_pipeline_state(
&device,
@@ -239,7 +234,7 @@ impl MetalRenderer {
);

let command_queue = device.new_command_queue();
let sprite_atlas = Arc::new(MetalAtlas::new(device.clone(), sample_count));
let sprite_atlas = Arc::new(MetalAtlas::new(device.clone(), SAMPLE_COUNT));
let core_video_texture_cache =
unsafe { CVMetalTextureCache::new(device.as_ptr()).unwrap() };

@@ -548,13 +543,13 @@ impl MetalRenderer {
color_attachment.set_resolve_texture(Some(&texture));
color_attachment.set_load_action(metal::MTLLoadAction::Clear);
color_attachment.set_store_action(metal::MTLStoreAction::MultisampleResolve);
color_attachment.set_resolve_level(0);
} else {
color_attachment.set_texture(Some(&texture));
color_attachment.set_load_action(metal::MTLLoadAction::Clear);
color_attachment.set_store_action(metal::MTLStoreAction::Store);
}
color_attachment.set_clear_color(metal::MTLClearColor::new(0., 0., 0., 1.));

let command_encoder = command_buffer.new_render_command_encoder(render_pass_descriptor);
command_encoder.set_render_pipeline_state(&self.paths_rasterization_pipeline_state);
command_encoder.set_vertex_buffer(