{"record":{"id":"ffb9b6e53ddb4969","repo":"emilk/egui","slug":"failed-to-fit-multisamples-option-of-native-option","errorCode":null,"errorMessage":"failed to fit multisamples option of native_options into u8","messagePattern":"failed to fit multisamples option of native_options into u8","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/eframe/src/native/glow_integration.rs","lineNumber":1083,"sourceCode":"        /*  opengl setup flow goes like this:\n            1. we create a configuration for opengl \"Display\" / \"Config\" creation\n            2. choose between special extensions like glx or egl or wgl and use them to create config/display\n            3. opengl context configuration\n            4. opengl context creation\n        */\n        // start building config for gl display\n        let config_template_builder = glutin::config::ConfigTemplateBuilder::new()\n            .prefer_hardware_accelerated(hardware_acceleration)\n            .with_depth_size(native_options.depth_buffer)\n            .with_stencil_size(native_options.stencil_buffer)\n            .with_transparency(native_options.viewport.transparent.unwrap_or(false));\n        // we don't know if multi sampling option is set. so, check if its more than 0.\n        let config_template_builder = if native_options.multisampling > 0 {\n            config_template_builder.with_multisampling(\n                native_options\n                    .multisampling\n                    .try_into()\n                    .expect(\"failed to fit multisamples option of native_options into u8\"),\n            )\n        } else {\n            config_template_builder\n        };\n\n        log::debug!(\"trying to create glutin Display with config: {config_template_builder:?}\");\n\n        // Create GL display. This may probably create a window too on most platforms. Definitely on `MS windows`. Never on Android.\n        let display_builder = glutin_winit::DisplayBuilder::new()\n            // we might want to expose this option to users in the future. maybe using an env var or using native_options.\n            //\n            // The justification for FallbackEgl over PreferEgl is at https://github.com/emilk/egui/pull/2526#issuecomment-1400229576 .\n            .with_preference(glutin_winit::ApiPreference::FallbackEgl)\n            .with_window_attributes(Some(egui_winit::create_winit_window_attributes(\n                egui_ctx,\n                viewport_builder.clone(),\n            )));\n","sourceCodeStart":1065,"sourceCodeEnd":1101,"githubUrl":"https://github.com/emilk/egui/blob/d802a982ce959839d92f4f6718fdffa41143110e/crates/eframe/src/native/glow_integration.rs#L1065-L1101","documentation":"NativeOptions.multisampling is a usize, but glutin's ConfigTemplateBuilder::with_multisampling takes a u8. The try_into().expect() panics during run_native setup when the configured sample count exceeds 255 (u8::MAX). MSAA sample counts are only meaningful as small powers of two (2/4/8), so any value over 255 is a configuration mistake.","triggerScenarios":"run_native with NativeOptions { multisampling: 256 or higher, .. } — e.g. 512 or 1024 — on any platform using the Glow renderer.","commonSituations":"Copying a 'quality level' number from another API, computing multisampling from a formula that can grow unbounded, or mistaking the field for a general quality knob.","solutions":["Set multisampling to a valid MSAA sample count: 0 (disabled), 2, 4, or 8.","Clamp user-supplied config before calling run_native: options.multisampling = options.multisampling.min(8).","Validate options early in main() with a clear error message instead of letting eframe panic."],"exampleFix":"// before\nlet options = eframe::NativeOptions { multisampling: 1024, ..Default::default() };\n// after\nlet options = eframe::NativeOptions { multisampling: 8, ..Default::default() };","handlingStrategy":"validation","validationCode":"fn validate_options(options: &eframe::NativeOptions) -> Result<(), String> {\n    let ms = options.multisampling;\n    if ms > u8::MAX as usize {\n        return Err(format!(\"multisampling must be 0-255, got {ms}\"));\n    }\n    if ms != 0 && !ms.is_power_of_two() {\n        return Err(format!(\"multisampling must be 0 or a power of two, got {ms}\"));\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp untrusted config: options.multisampling = options.multisampling.min(8).","Only expose 0/2/4/8 as choices in your app's settings UI.","Validate NativeOptions once in main() before run_native with clear error messages."],"tags":["rust","eframe","msaa","native-options","config-validation","panic"],"backgroundTag":"config-value-out-of-range","analyzedSha":"d802a982ce959839d92f4f6718fdffa41143110e","analyzedAt":"2026-08-16T20:07:53.954Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}