Skip to content

Commit b1e2645

Browse files
committed
Small code cleanup
1 parent d416ded commit b1e2645

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

eframe/src/native/run.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ pub fn run_wgpu(
212212

213213
// SAFETY: `window` must outlive `painter`.
214214
#[allow(unsafe_code)]
215-
let mut painter = unsafe { egui_wgpu::winit::Painter::new(&window) };
215+
let mut painter = unsafe {
216+
egui_wgpu::winit::Painter::new(&window, native_options.multisampling.max(1) as _)
217+
};
216218

217219
let mut integration = epi_integration::EpiIntegration::new(
218220
painter.max_texture_side(),

egui-wgpu/src/egui.wgsl

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct Locals {
1111
};
1212
[[group(0), binding(0)]] var<uniform> r_locals: Locals;
1313

14+
// 0-1 from 0-255
1415
fn linear_from_srgb(srgb: vec3<f32>) -> vec3<f32> {
1516
let cutoff = srgb < vec3<f32>(10.31475);
1617
let lower = srgb / vec3<f32>(3294.6);

egui-wgpu/src/renderer.rs

+16-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use std::{borrow::Cow, collections::HashMap, num::NonZeroU32};
44

5-
use bytemuck::{Pod, Zeroable};
65
use egui::epaint::Primitive;
76
use wgpu;
87
use wgpu::util::DeviceExt as _;
@@ -35,20 +34,17 @@ impl ScreenDescriptor {
3534
}
3635

3736
/// Uniform buffer used when rendering.
38-
#[derive(Clone, Copy, Debug)]
37+
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
3938
#[repr(C)]
4039
struct UniformBuffer {
4140
screen_size_in_points: [f32; 2],
4241
}
4342

44-
unsafe impl Pod for UniformBuffer {}
45-
46-
unsafe impl Zeroable for UniformBuffer {}
47-
4843
/// Wraps the buffers and includes additional information.
4944
#[derive(Debug)]
5045
struct SizedBuffer {
5146
buffer: wgpu::Buffer,
47+
/// number of bytes
5248
size: usize,
5349
}
5450

@@ -275,8 +271,8 @@ impl RenderPass {
275271
index_buffer,
276272
) in paint_jobs
277273
.iter()
278-
.zip(self.vertex_buffers.iter())
279-
.zip(self.index_buffers.iter())
274+
.zip(&self.vertex_buffers)
275+
.zip(&self.index_buffers)
280276
{
281277
// Transform clip rect to physical pixels.
282278
let clip_min_x = pixels_per_point * clip_rect.min.x;
@@ -342,24 +338,27 @@ impl RenderPass {
342338
id: egui::TextureId,
343339
image_delta: &egui::epaint::ImageDelta,
344340
) {
341+
let width = image_delta.image.width() as u32;
342+
let height = image_delta.image.height() as u32;
343+
345344
let size = wgpu::Extent3d {
346-
width: image_delta.image.size()[0] as u32,
347-
height: image_delta.image.size()[1] as u32,
345+
width,
346+
height,
348347
depth_or_array_layers: 1,
349348
};
350349

351350
let data_color32 = match &image_delta.image {
352351
egui::ImageData::Color(image) => {
353352
assert_eq!(
354-
image.width() * image.height(),
353+
width as usize * height as usize,
355354
image.pixels.len(),
356355
"Mismatch between texture size and texel count"
357356
);
358357
Cow::Borrowed(&image.pixels)
359358
}
360359
egui::ImageData::Font(image) => {
361360
assert_eq!(
362-
image.width() * image.height(),
361+
width as usize * height as usize,
363362
image.pixels.len(),
364363
"Mismatch between texture size and texel count"
365364
);
@@ -379,8 +378,8 @@ impl RenderPass {
379378
data_bytes,
380379
wgpu::ImageDataLayout {
381380
offset: 0,
382-
bytes_per_row: NonZeroU32::new((4 * image_delta.image.width()) as u32),
383-
rows_per_image: NonZeroU32::new(image_delta.image.height() as u32),
381+
bytes_per_row: NonZeroU32::new(4 * width),
382+
rows_per_image: NonZeroU32::new(height),
384383
},
385384
size,
386385
);
@@ -451,9 +450,6 @@ impl RenderPass {
451450
paint_jobs: &[egui::epaint::ClippedPrimitive],
452451
screen_descriptor: &ScreenDescriptor,
453452
) {
454-
let index_size = self.index_buffers.len();
455-
let vertex_size = self.vertex_buffers.len();
456-
457453
let screen_size_in_points = screen_descriptor.screen_size_in_points();
458454

459455
self.update_buffer(
@@ -470,7 +466,7 @@ impl RenderPass {
470466
match primitive {
471467
Primitive::Mesh(mesh) => {
472468
let data: &[u8] = bytemuck::cast_slice(&mesh.indices);
473-
if i < index_size {
469+
if i < self.index_buffers.len() {
474470
self.update_buffer(device, queue, &BufferType::Index, i, data);
475471
} else {
476472
let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
@@ -485,7 +481,7 @@ impl RenderPass {
485481
}
486482

487483
let data: &[u8] = bytemuck::cast_slice(&mesh.vertices);
488-
if i < vertex_size {
484+
if i < self.vertex_buffers.len() {
489485
self.update_buffer(device, queue, &BufferType::Vertex, i, data);
490486
} else {
491487
let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
@@ -501,7 +497,7 @@ impl RenderPass {
501497
}
502498
}
503499
Primitive::Callback(_) => {
504-
tracing::warn!("Painting callbacks not supported by egui-wgpu");
500+
tracing::warn!("Painting callbacks not supported by egui-wgpu (yet)");
505501
}
506502
}
507503
}

egui-wgpu/src/winit.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ impl Painter {
1616
///
1717
/// # Safety
1818
/// The given `window` must outlive the returned [`Painter`].
19-
pub unsafe fn new(window: &winit::window::Window) -> Self {
19+
pub unsafe fn new(window: &winit::window::Window, msaa_samples: u32) -> Self {
2020
let instance = wgpu::Instance::new(wgpu::Backends::PRIMARY | wgpu::Backends::GL);
2121
let surface = instance.create_surface(&window);
2222

23-
// WGPU 0.11+ support force fallback (if HW implementation not supported), set it to true or false (optional).
2423
let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
2524
power_preference: wgpu::PowerPreference::HighPerformance,
2625
compatible_surface: Some(&surface),
@@ -49,7 +48,7 @@ impl Painter {
4948
};
5049
surface.configure(&device, &surface_config);
5150

52-
let egui_rpass = renderer::RenderPass::new(&device, surface_format, 1);
51+
let egui_rpass = renderer::RenderPass::new(&device, surface_format, msaa_samples);
5352

5453
Self {
5554
device,
@@ -64,9 +63,9 @@ impl Painter {
6463
self.device.limits().max_texture_dimension_2d as usize
6564
}
6665

67-
pub fn on_window_resized(&mut self, width: u32, height: u32) {
68-
self.surface_config.width = width;
69-
self.surface_config.height = height;
66+
pub fn on_window_resized(&mut self, width_in_pixels: u32, height_in_pixels: u32) {
67+
self.surface_config.width = width_in_pixels;
68+
self.surface_config.height = height_in_pixels;
7069
self.surface.configure(&self.device, &self.surface_config);
7170
}
7271

0 commit comments

Comments
 (0)