Skip to content

Commit

Permalink
Small code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed May 11, 2022
1 parent d416ded commit d36c541
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
4 changes: 3 additions & 1 deletion eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ pub fn run_wgpu(

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

let mut integration = epi_integration::EpiIntegration::new(
painter.max_texture_side(),
Expand Down
1 change: 1 addition & 0 deletions egui-wgpu/src/egui.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct Locals {
};
[[group(0), binding(0)]] var<uniform> r_locals: Locals;

// 0-1 from 0-255
fn linear_from_srgb(srgb: vec3<f32>) -> vec3<f32> {
let cutoff = srgb < vec3<f32>(10.31475);
let lower = srgb / vec3<f32>(3294.6);
Expand Down
36 changes: 16 additions & 20 deletions egui-wgpu/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

use bytemuck::{Pod, Zeroable};
use egui::epaint::Primitive;
use wgpu;
use wgpu::util::DeviceExt as _;
Expand Down Expand Up @@ -35,20 +34,17 @@ impl ScreenDescriptor {
}

/// Uniform buffer used when rendering.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
#[repr(C)]
struct UniformBuffer {
screen_size_in_points: [f32; 2],
}

unsafe impl Pod for UniformBuffer {}

unsafe impl Zeroable for UniformBuffer {}

/// Wraps the buffers and includes additional information.
#[derive(Debug)]
struct SizedBuffer {
buffer: wgpu::Buffer,
/// number of bytes
size: usize,
}

Expand Down Expand Up @@ -275,8 +271,8 @@ impl RenderPass {
index_buffer,
) in paint_jobs
.iter()
.zip(self.vertex_buffers.iter())
.zip(self.index_buffers.iter())
.zip(&self.vertex_buffers)
.zip(&self.index_buffers)
{
// Transform clip rect to physical pixels.
let clip_min_x = pixels_per_point * clip_rect.min.x;
Expand Down Expand Up @@ -342,24 +338,27 @@ impl RenderPass {
id: egui::TextureId,
image_delta: &egui::epaint::ImageDelta,
) {
let width = image_delta.image.width() as u32;
let height = image_delta.image.width() as u32;

let size = wgpu::Extent3d {
width: image_delta.image.size()[0] as u32,
height: image_delta.image.size()[1] as u32,
width,
height,
depth_or_array_layers: 1,
};

let data_color32 = match &image_delta.image {
egui::ImageData::Color(image) => {
assert_eq!(
image.width() * image.height(),
width as usize * height as usize,
image.pixels.len(),
"Mismatch between texture size and texel count"
);
Cow::Borrowed(&image.pixels)
}
egui::ImageData::Font(image) => {
assert_eq!(
image.width() * image.height(),
width as usize * height as usize,
image.pixels.len(),
"Mismatch between texture size and texel count"
);
Expand All @@ -379,8 +378,8 @@ impl RenderPass {
data_bytes,
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: NonZeroU32::new((4 * image_delta.image.width()) as u32),
rows_per_image: NonZeroU32::new(image_delta.image.height() as u32),
bytes_per_row: NonZeroU32::new(4 * width),
rows_per_image: NonZeroU32::new(height),
},
size,
);
Expand Down Expand Up @@ -451,9 +450,6 @@ impl RenderPass {
paint_jobs: &[egui::epaint::ClippedPrimitive],
screen_descriptor: &ScreenDescriptor,
) {
let index_size = self.index_buffers.len();
let vertex_size = self.vertex_buffers.len();

let screen_size_in_points = screen_descriptor.screen_size_in_points();

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

let data: &[u8] = bytemuck::cast_slice(&mesh.vertices);
if i < vertex_size {
if i < self.vertex_buffers.len() {
self.update_buffer(device, queue, &BufferType::Vertex, i, data);
} else {
let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
Expand All @@ -501,7 +497,7 @@ impl RenderPass {
}
}
Primitive::Callback(_) => {
tracing::warn!("Painting callbacks not supported by egui-wgpu");
tracing::warn!("Painting callbacks not supported by egui-wgpu (yet)");
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions egui-wgpu/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ impl Painter {
///
/// # Safety
/// The given `window` must outlive the returned [`Painter`].
pub unsafe fn new(window: &winit::window::Window) -> Self {
pub unsafe fn new(window: &winit::window::Window, msaa_samples: u32) -> Self {
let instance = wgpu::Instance::new(wgpu::Backends::PRIMARY | wgpu::Backends::GL);
let surface = instance.create_surface(&window);

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

let egui_rpass = renderer::RenderPass::new(&device, surface_format, 1);
let egui_rpass = renderer::RenderPass::new(&device, surface_format, msaa_samples);

Self {
device,
Expand All @@ -64,9 +63,9 @@ impl Painter {
self.device.limits().max_texture_dimension_2d as usize
}

pub fn on_window_resized(&mut self, width: u32, height: u32) {
self.surface_config.width = width;
self.surface_config.height = height;
pub fn on_window_resized(&mut self, width_in_pixels: u32, height_in_pixels: u32) {
self.surface_config.width = width_in_pixels;
self.surface_config.height = height_in_pixels;
self.surface.configure(&self.device, &self.surface_config);
}

Expand Down

0 comments on commit d36c541

Please sign in to comment.