| 1 | #[path = "app_icon.rs"] |
| 2 | mod app_icon; |
| 3 | |
| 4 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 5 | pub enum Backend { |
| 6 | Dx12Hardware, |
| 7 | Dx12Software, |
| 8 | Glow, |
| 9 | } |
| 10 | |
| 11 | pub fn try_backends<E>( |
| 12 | windows: bool, |
| 13 | mut attempt: impl FnMut(Backend) -> Result<(), (E, bool)>, |
| 14 | ) -> Result<(), E> { |
| 15 | let order: &[Backend] = if windows { |
| 16 | &[Backend::Dx12Hardware, Backend::Dx12Software, Backend::Glow] |
| 17 | } else { |
| 18 | &[Backend::Glow] |
| 19 | }; |
| 20 | let mut failure = None; |
| 21 | for &backend in order { |
| 22 | match attempt(backend) { |
| 23 | Ok(()) => return Ok(()), |
| 24 | Err((error, retry)) => { |
| 25 | if !retry { |
| 26 | return Err(error); |
| 27 | } |
| 28 | failure = Some(error); |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | Err(failure.expect("렌더러 시도 목록은 비어 있지 않음")) |
| 33 | } |
| 34 | |
| 35 | pub fn options(backend: Backend, mut options: eframe::NativeOptions) -> eframe::NativeOptions { |
| 36 | options.viewport = options |
| 37 | .viewport |
| 38 | .with_app_id("zipelo") |
| 39 | .with_icon(app_icon::load()); |
| 40 | options.renderer = eframe::Renderer::Glow; |
| 41 | options.run_and_return = true; |
| 42 | #[cfg(target_os = "windows")] |
| 43 | if backend != Backend::Glow { |
| 44 | use eframe::{egui_wgpu, wgpu}; |
| 45 | use std::sync::Arc; |
| 46 | let mut setup = egui_wgpu::WgpuSetupCreateNew::without_display_handle(); |
| 47 | setup.instance_descriptor.backends = wgpu::Backends::DX12; |
| 48 | setup |
| 49 | .instance_descriptor |
| 50 | .backend_options |
| 51 | .dx12 |
| 52 | .shader_compiler = wgpu::Dx12Compiler::Fxc; |
| 53 | setup.native_adapter_selector = Some(Arc::new(move |adapters, surface| { |
| 54 | adapters |
| 55 | .iter() |
| 56 | .find(|adapter| { |
| 57 | let software = adapter.get_info().device_type == wgpu::DeviceType::Cpu; |
| 58 | software == (backend == Backend::Dx12Software) |
| 59 | && surface.is_none_or(|surface| { |
| 60 | !surface.get_capabilities(adapter).formats.is_empty() |
| 61 | }) |
| 62 | }) |
| 63 | .cloned() |
| 64 | .ok_or_else(|| "사용 가능한 DirectX 화면 장치가 없음".into()) |
| 65 | })); |
| 66 | options.renderer = eframe::Renderer::Wgpu; |
| 67 | options.wgpu_options.wgpu_setup = egui_wgpu::WgpuSetup::CreateNew(setup); |
| 68 | } |
| 69 | #[cfg(not(target_os = "windows"))] |
| 70 | let _ = backend; |
| 71 | options |
| 72 | } |
| 73 | |
| 74 | pub fn run_native( |
| 75 | title: &str, |
| 76 | native_options: eframe::NativeOptions, |
| 77 | mut create: impl FnMut() -> eframe::AppCreator<'static>, |
| 78 | ) -> eframe::Result { |
| 79 | use std::{cell::Cell, rc::Rc}; |
| 80 | try_backends(cfg!(target_os = "windows"), |backend| { |
| 81 | let started = Rc::new(Cell::new(false)); |
| 82 | let entered = started.clone(); |
| 83 | let creator = create(); |
| 84 | let result = eframe::run_native( |
| 85 | title, |
| 86 | options(backend, native_options.clone()), |
| 87 | Box::new(move |context| { |
| 88 | entered.set(true); |
| 89 | creator(context) |
| 90 | }), |
| 91 | ); |
| 92 | // 앱 생성 뒤에는 작업이나 창을 중복 실행하지 않음. |
| 93 | result.map_err(|error| (error, !started.get())) |
| 94 | }) |
| 95 | } |