Use two layers for image::atlas only on Gl backend

This commit is contained in:
Héctor Ramón Jiménez 2024-02-19 08:18:51 +01:00
parent 4272090d36
commit 04df889cac
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
5 changed files with 46 additions and 24 deletions

View file

@ -87,7 +87,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
}); });
let surface = instance.create_surface(window.clone())?; let surface = instance.create_surface(window.clone())?;
let (format, (device, queue)) = let (format, adapter, device, queue) =
futures::futures::executor::block_on(async { futures::futures::executor::block_on(async {
let adapter = wgpu::util::initialize_adapter_from_env_or_default( let adapter = wgpu::util::initialize_adapter_from_env_or_default(
&instance, &instance,
@ -107,15 +107,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let capabilities = surface.get_capabilities(&adapter); let capabilities = surface.get_capabilities(&adapter);
( let (device, queue) = adapter
capabilities
.formats
.iter()
.copied()
.find(wgpu::TextureFormat::is_srgb)
.or_else(|| capabilities.formats.first().copied())
.expect("Get preferred format"),
adapter
.request_device( .request_device(
&wgpu::DeviceDescriptor { &wgpu::DeviceDescriptor {
label: None, label: None,
@ -126,7 +118,19 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
None, None,
) )
.await .await
.expect("Request device"), .expect("Request device");
(
capabilities
.formats
.iter()
.copied()
.find(wgpu::TextureFormat::is_srgb)
.or_else(|| capabilities.formats.first().copied())
.expect("Get preferred format"),
adapter,
device,
queue,
) )
}); });
@ -153,7 +157,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize iced // Initialize iced
let mut debug = Debug::new(); let mut debug = Debug::new();
let mut renderer = Renderer::new( let mut renderer = Renderer::new(
Backend::new(&device, &queue, Settings::default(), format), Backend::new(&adapter, &device, &queue, Settings::default(), format),
Font::default(), Font::default(),
Pixels(16.0), Pixels(16.0),
); );

View file

@ -27,7 +27,6 @@ pub struct Backend {
text_pipeline: text::Pipeline, text_pipeline: text::Pipeline,
triangle_pipeline: triangle::Pipeline, triangle_pipeline: triangle::Pipeline,
pipeline_storage: pipeline::Storage, pipeline_storage: pipeline::Storage,
#[cfg(any(feature = "image", feature = "svg"))] #[cfg(any(feature = "image", feature = "svg"))]
image_pipeline: image::Pipeline, image_pipeline: image::Pipeline,
} }
@ -35,6 +34,7 @@ pub struct Backend {
impl Backend { impl Backend {
/// Creates a new [`Backend`]. /// Creates a new [`Backend`].
pub fn new( pub fn new(
_adapter: &wgpu::Adapter,
device: &wgpu::Device, device: &wgpu::Device,
queue: &wgpu::Queue, queue: &wgpu::Queue,
settings: Settings, settings: Settings,
@ -46,7 +46,11 @@ impl Backend {
triangle::Pipeline::new(device, format, settings.antialiasing); triangle::Pipeline::new(device, format, settings.antialiasing);
#[cfg(any(feature = "image", feature = "svg"))] #[cfg(any(feature = "image", feature = "svg"))]
let image_pipeline = image::Pipeline::new(device, format); let image_pipeline = {
let backend = _adapter.get_info().backend;
image::Pipeline::new(device, format, backend)
};
Self { Self {
quad_pipeline, quad_pipeline,

View file

@ -176,7 +176,11 @@ impl Data {
} }
impl Pipeline { impl Pipeline {
pub fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self { pub fn new(
device: &wgpu::Device,
format: wgpu::TextureFormat,
backend: wgpu::Backend,
) -> Self {
let nearest_sampler = device.create_sampler(&wgpu::SamplerDescriptor { let nearest_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
address_mode_u: wgpu::AddressMode::ClampToEdge, address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge, address_mode_v: wgpu::AddressMode::ClampToEdge,
@ -318,7 +322,7 @@ impl Pipeline {
multiview: None, multiview: None,
}); });
let texture_atlas = Atlas::new(device); let texture_atlas = Atlas::new(device, backend);
let texture = device.create_bind_group(&wgpu::BindGroupDescriptor { let texture = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("iced_wgpu::image texture atlas bind group"), label: Some("iced_wgpu::image texture atlas bind group"),

View file

@ -23,11 +23,15 @@ pub struct Atlas {
} }
impl Atlas { impl Atlas {
pub fn new(device: &wgpu::Device) -> Self { pub fn new(device: &wgpu::Device, backend: wgpu::Backend) -> Self {
// We start with 2 layers, to help wgpu figure out that this texture is `GL_TEXTURE_2D_ARRAY` rather let layers = match backend {
// than `GL_TEXTURE_2D` // On the GL backend we start with 2 layers, to help wgpu figure
// out that this texture is `GL_TEXTURE_2D_ARRAY` rather than `GL_TEXTURE_2D`
// https://github.com/gfx-rs/wgpu/blob/004e3efe84a320d9331371ed31fa50baa2414911/wgpu-hal/src/gles/mod.rs#L371 // https://github.com/gfx-rs/wgpu/blob/004e3efe84a320d9331371ed31fa50baa2414911/wgpu-hal/src/gles/mod.rs#L371
let layers = vec![Layer::Empty, Layer::Empty]; wgpu::Backend::Gl => vec![Layer::Empty, Layer::Empty],
_ => vec![Layer::Empty],
};
let extent = wgpu::Extent3d { let extent = wgpu::Extent3d {
width: SIZE, width: SIZE,
height: SIZE, height: SIZE,

View file

@ -146,7 +146,13 @@ impl Compositor {
/// Creates a new rendering [`Backend`] for this [`Compositor`]. /// Creates a new rendering [`Backend`] for this [`Compositor`].
pub fn create_backend(&self) -> Backend { pub fn create_backend(&self) -> Backend {
Backend::new(&self.device, &self.queue, self.settings, self.format) Backend::new(
&self.adapter,
&self.device,
&self.queue,
self.settings,
self.format,
)
} }
} }