Guest User

Untitled

a guest
Mar 31st, 2025
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1.  
  2. use egui::RawInput;
  3. use winit::event::{Event, WindowEvent};
  4. use winit::event_loop::{ControlFlow, EventLoop};
  5. use winit::window::WindowBuilder;
  6. use wgpu::{Instance, Device, Queue, RequestAdapterOptions};
  7. use egui_wgpu::Renderer;
  8. use egui_winit::{State, egui, egui::viewport::ViewportId};
  9.  
  10. use std::process;
  11.  
  12. async fn run(event_loop: EventLoop<()>, window: winit::window::Window) {
  13. let instance = Instance::new(wgpu::InstanceDescriptor {
  14. backends: wgpu::Backends::all(),
  15. ..Default::default()
  16. });
  17.  
  18. let adapter = instance.request_adapter(&RequestAdapterOptions {
  19. power_preference: wgpu::PowerPreference::HighPerformance,
  20. compatible_surface: None,
  21. force_fallback_adapter: false,
  22. })
  23. .await
  24. .expect("Failed to find an adapter");
  25.  
  26. let (device, queue) = adapter.request_device(&wgpu::DeviceDescriptor {
  27. label: None,
  28. required_features: wgpu::Features::empty(),
  29. required_limits: wgpu::Limits::default(),
  30. }, None)
  31. .await
  32. .expect("Failed to create device and queue");
  33.  
  34. let mut state = State::new(
  35. egui::Context::default(),
  36. ViewportId::ROOT,
  37. &window,
  38. None,
  39. None,
  40. );
  41.  
  42.  
  43. let mut renderer = Renderer::new(&device, wgpu::TextureFormat::Bgra8UnormSrgb, None, 1);
  44.  
  45.  
  46. event_loop.run(move |event, control_flow| {
  47. match event {
  48. Event::WindowEvent { event, .. } => match event {
  49. WindowEvent::CloseRequested => {
  50. std::process::exit(0);
  51. }
  52. WindowEvent::RedrawRequested => {
  53.  
  54. let raw_input = RawInput::default();
  55.  
  56.  
  57. state.egui_ctx().run(raw_input, move |ctx| {
  58. egui::CentralPanel::default().show(ctx, |ui| {
  59. ui.label("Hello from Egui!");
  60. if ui.button("Close").clicked() {
  61. std::process::exit(0);
  62. }
  63. });
  64. });
  65.  
  66.  
  67. state.handle_platform_output(&window, Default::default());
  68. }
  69. _ => {}
  70. },
  71. _ => {}
  72. }
  73.  
  74.  
  75. });
  76.  
  77. }
  78.  
  79. fn main() {
  80.  
  81. let event_loop = EventLoop::new().expect("Failed to create event loop");
  82.  
  83.  
  84. let window = WindowBuilder::new()
  85. .with_title("Egui + WGPU")
  86. .build(&event_loop)
  87. .expect("Failed to create window");
  88.  
  89.  
  90. pollster::block_on(run(event_loop, window));
  91. }
Advertisement
Add Comment
Please, Sign In to add comment