gizmo-renderer 0.8.0

A custom ECS and physics engine aimed for realistic simulations.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
use super::*;

/// Web'de dahili render çözünürlüğü üst sınırı. 4K/Retina canvas'larda tam
/// çözünürlükte render performansı katlederdi; aspect'i koruyarak buna caplenir
/// ve sonuç CSS ile tekrar %100'e ölçeklenir (kasıtlı kalite/performans dengesi).
/// **Native'de no-op** (verbatim döner) — sadece wasm hedefinde etkindir.
///
/// `Renderer::new` VE `Renderer::resize` bu tek fonksiyondan geçer; yoksa ilk
/// `Resized` olayı (tarayıcı canvas'ı büyüdüğünde) cap'i sessizce delerdi.
pub(crate) fn cap_web_render_size(
    mut size: winit::dpi::PhysicalSize<u32>,
) -> winit::dpi::PhysicalSize<u32> {
    // Boş boyut (WASM'da canvas 0x0 olabilir) → en az 1x1 garanti (native'de de güvenli).
    if size.width == 0 || size.height == 0 {
        size = winit::dpi::PhysicalSize::new(1280, 720);
    }
    #[cfg(target_arch = "wasm32")]
    if size.width > 640 || size.height > 360 {
        let aspect = size.width as f32 / size.height as f32;
        if aspect > 1.0 {
            size.width = 640;
            size.height = ((640.0 / aspect) as u32).max(1);
        } else {
            size.height = 360;
            size.width = ((360.0 * aspect) as u32).max(1);
        }
    }
    size
}

impl Renderer {
    pub async fn new(window: Arc<Window>) -> Self {
        let size = cap_web_render_size(window.inner_size());

        #[cfg(target_arch = "wasm32")]
        let backends = wgpu::Backends::BROWSER_WEBGPU | wgpu::Backends::GL;
        #[cfg(not(target_arch = "wasm32"))]
        let backends = wgpu::Backends::all();

        log::info!("[Renderer] Window size: {}x{}", size.width, size.height);
        log::info!("[Renderer] Backends: {:?}", backends);

        let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
            backends,
            flags: wgpu::InstanceFlags::default(),
            memory_budget_thresholds: Default::default(),
            backend_options: Default::default(),
            display: None,
        });

        // Enumerate available adapters for diagnostic info
        #[cfg(not(target_arch = "wasm32"))]
        {
            let adapters = instance.enumerate_adapters(backends).await;
            log::info!("[Renderer] {} adapter bulundu", adapters.len());
            for (i, a) in adapters.iter().enumerate() {
                let info = a.get_info();
                log::info!(
                    "[Renderer]   Adapter {}: {} ({:?}, {:?})",
                    i,
                    info.name,
                    info.backend,
                    info.device_type
                );
            }
        }

        let surface = instance
            .create_surface(window.clone())
            .expect("Surface oluşturulamadı!");

        log::info!("[Renderer] Surface oluşturuldu, adapter aranıyor...");

        let adapter = instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                compatible_surface: Some(&surface),
                force_fallback_adapter: false,
            })
            .await;

        let adapter = match adapter {
            Ok(a) => {
                let info = a.get_info();
                log::info!(
                    "[Renderer] Adapter bulundu: {} ({:?})",
                    info.name,
                    info.backend
                );
                a
            }
            Err(_) => {
                log::warn!(
                    "[Renderer] Surface uyumlu adapter bulunamadı, surface'siz deneniyor..."
                );
                // Surface'siz adapter dene
                match instance
                    .request_adapter(&wgpu::RequestAdapterOptions {
                        power_preference: wgpu::PowerPreference::HighPerformance,
                        compatible_surface: None,
                        force_fallback_adapter: false,
                    })
                    .await
                {
                    Ok(a) => {
                        let info = a.get_info();
                        log::info!(
                            "[Renderer] Surface'siz adapter bulundu: {} ({:?})",
                            info.name,
                            info.backend
                        );
                        a
                    }
                    Err(_) => {
                        log::error!(
                            "[Renderer] Hiçbir adapter bulunamadı! Backends: {:?}",
                            backends
                        );
                        panic!(
                            "GPU adapter bulunamadı! Backends: {:?}, Window size: {}x{}",
                            backends, size.width, size.height
                        );
                    }
                }
            }
        };

        #[cfg(not(target_arch = "wasm32"))]
        let (device, queue) = adapter
            .request_device(
                &wgpu::DeviceDescriptor {
                    required_features: wgpu::Features::POLYGON_MODE_LINE,
                    required_limits: wgpu::Limits {
                        max_bind_groups: 6,
                        max_storage_buffers_per_shader_stage: 8,
                        max_storage_buffer_binding_size: 256 << 20, // 256 MB buffer limit
                        ..wgpu::Limits::default()
                    },
                    label: None,
                    experimental_features: wgpu::ExperimentalFeatures::default(),
                    memory_hints: wgpu::MemoryHints::default(),
                    trace: wgpu::Trace::Off,
                },
            )
            .await
            .unwrap();

        #[cfg(target_arch = "wasm32")]
        let (device, queue) = {
            let mut limits = adapter.limits();
            limits.max_bind_groups = limits.max_bind_groups.max(4);
            limits.max_storage_buffers_per_shader_stage = limits.max_storage_buffers_per_shader_stage.max(8);
            limits.max_storage_buffer_binding_size = limits.max_storage_buffer_binding_size.max(128 << 20);

            match adapter
                .request_device(
                    &wgpu::DeviceDescriptor {
                        required_features: wgpu::Features::empty(),
                        required_limits: limits.clone(),
                        label: None,
                        experimental_features: wgpu::ExperimentalFeatures::default(),
                        memory_hints: wgpu::MemoryHints::default(),
                        trace: wgpu::Trace::Off,
                    },
                )
                .await
            {
                Ok(dq) => dq,
                Err(e) => {
                    log::warn!("[Renderer] request_device with custom adapter.limits() failed: {:?}", e);
                    match adapter
                        .request_device(
                            &wgpu::DeviceDescriptor {
                                required_features: wgpu::Features::empty(),
                                required_limits: wgpu::Limits::downlevel_webgl2_defaults(),
                                label: None,
                                experimental_features: wgpu::ExperimentalFeatures::default(),
                                memory_hints: wgpu::MemoryHints::default(),
                                trace: wgpu::Trace::Off,
                            },
                        )
                        .await
                    {
                        Ok(dq) => dq,
                        Err(e2) => {
                            log::warn!("[Renderer] request_device with downlevel_webgl2_defaults failed: {:?}", e2);
                            adapter
                                .request_device(
                                    &wgpu::DeviceDescriptor {
                                        required_features: wgpu::Features::empty(),
                                        required_limits: wgpu::Limits::default(),
                                        label: None,
                                        experimental_features: wgpu::ExperimentalFeatures::default(),
                                        memory_hints: wgpu::MemoryHints::default(),
                                        trace: wgpu::Trace::Off,
                                    },
                                )
                                .await
                                .unwrap_or_else(|e3| {
                                    panic!("Fatal: Failed to request wgpu device on WASM: {:?}", e3);
                                })
                        }
                    }
                }
            }
        };

        let surface_caps = surface.get_capabilities(&adapter);
        let surface_format = surface_caps
            .formats
            .iter()
            .copied()
            .find(|f| f.is_srgb())
            .unwrap_or(surface_caps.formats[0]);

        let config = wgpu::SurfaceConfiguration {
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
            format: surface_format,
            width: size.width,
            height: size.height,
            // VSync tercihi: Mailbox (uncapped FPS) varsa kullan, yoksa Fifo (VSync)
            present_mode: wgpu::PresentMode::AutoNoVsync,
            alpha_mode: surface_caps.alpha_modes[0],
            view_formats: vec![],
            desired_maximum_frame_latency: 2,
        };

        Self::finish_construction(device, queue, Some(surface), config, size)
    }

    /// Probes whether a GPU adapter is available for headless rendering.
    ///
    /// Unlike [`Renderer::new_headless`] — which panics when no adapter can be
    /// acquired (the correct fail-fast contract for a real headless render) — this
    /// never panics. It lets GPU-dependent tests skip gracefully on machines with
    /// no GPU (e.g. CI runners) instead of aborting the whole test binary.
    pub async fn headless_adapter_available() -> bool {
        let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
            backends: wgpu::Backends::all(),
            flags: wgpu::InstanceFlags::default(),
            memory_budget_thresholds: Default::default(),
            backend_options: Default::default(),
            display: None,
        });
        instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                compatible_surface: None,
                force_fallback_adapter: false,
            })
            .await
            .is_ok()
    }

    /// Constructs a Renderer with **no window/surface** — every render target is an
    /// offscreen texture. Enables headless GPU servers, CI rendering and
    /// deterministic render harnesses. Shares [`Renderer::finish_construction`] with
    /// the windowed [`Renderer::new`], so every GPU subsystem initialises identically.
    ///
    /// `format` defaults to `Rgba8UnormSrgb` when `None`.
    pub async fn new_headless(width: u32, height: u32, format: Option<wgpu::TextureFormat>) -> Self {
        let width = width.max(1);
        let height = height.max(1);

        let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
            backends: wgpu::Backends::all(),
            flags: wgpu::InstanceFlags::default(),
            memory_budget_thresholds: Default::default(),
            backend_options: Default::default(),
            display: None,
        });

        let adapter = instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                compatible_surface: None,
                force_fallback_adapter: false,
            })
            .await
            .expect("Headless Renderer: hiçbir GPU adapter bulunamadı");

        let (device, queue) = adapter
            .request_device(&wgpu::DeviceDescriptor {
                required_features: wgpu::Features::POLYGON_MODE_LINE,
                required_limits: wgpu::Limits {
                    max_bind_groups: 6,
                    max_storage_buffers_per_shader_stage: 8,
                    max_storage_buffer_binding_size: 256 << 20,
                    ..wgpu::Limits::default()
                },
                label: None,
                experimental_features: wgpu::ExperimentalFeatures::default(),
                memory_hints: wgpu::MemoryHints::default(),
                trace: wgpu::Trace::Off,
            })
            .await
            .expect("Headless Renderer: request_device başarısız");

        let config = wgpu::SurfaceConfiguration {
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
            format: format.unwrap_or(wgpu::TextureFormat::Rgba8UnormSrgb),
            width,
            height,
            present_mode: wgpu::PresentMode::AutoNoVsync,
            alpha_mode: wgpu::CompositeAlphaMode::Auto,
            view_formats: vec![],
            desired_maximum_frame_latency: 2,
        };

        Self::finish_construction(
            device,
            queue,
            None,
            config,
            winit::dpi::PhysicalSize::new(width, height),
        )
    }

    /// Surface-agnostic tail of construction: configures the surface, builds the
    /// depth texture, all pipelines, post-process and GPU subsystems, then
    /// assembles the `Renderer`. Shared by `new` (windowed) and the forthcoming
    /// headless path so subsystem init order is identical regardless of how the
    /// surface/device were acquired.
    fn finish_construction(
        device: wgpu::Device,
        queue: wgpu::Queue,
        surface: Option<wgpu::Surface<'static>>,
        config: wgpu::SurfaceConfiguration,
        size: winit::dpi::PhysicalSize<u32>,
    ) -> Self {
        if let Some(ref surface) = surface {
            surface.configure(&device, &config);
        }

        let depth_texture_view = Self::create_depth_texture(&device, config.width, config.height);

        let scene = crate::pipeline::build_scene_pipelines(&device);
        let post_res = crate::post_process::build_post_process_resources(
            &device,
            config.format,
            config.width,
            config.height,
            &depth_texture_view,
        );

        // GPU particle buffer boyutu — ihtiyaca göre ayarlanabilir
        #[cfg(not(target_arch = "wasm32"))]
        let gpu_particles = {
            let max_particles: u32 = 100_000;
            Some(crate::gpu_particles::GpuParticleSystem::new(
                &device,
                max_particles,
                &scene.global_bind_group_layout,
                wgpu::TextureFormat::Rgba16Float,
            ))
        };
        #[cfg(target_arch = "wasm32")]
        let gpu_particles: Option<crate::gpu_particles::GpuParticleSystem> = None;

        #[cfg(not(target_arch = "wasm32"))]
        let gpu_physics = {
            let max_physics_spheres: u32 = 50_000;
            let mut physics = crate::gpu_physics::GpuPhysicsSystem::new(
                &device,
                max_physics_spheres,
                &scene.global_bind_group_layout,
                wgpu::TextureFormat::Rgba16Float,
                wgpu::TextureFormat::Depth32Float,
            );
            physics.enable_debug(&device, 0);
            Some(physics)
        };
        #[cfg(target_arch = "wasm32")]
        let gpu_physics: Option<crate::gpu_physics::GpuPhysicsSystem> = None;

        #[cfg(not(target_arch = "wasm32"))]
        let gpu_fluid = Some(crate::gpu_fluid::GpuFluidSystem::new(
            &device,
            &queue,
            100_000,
            &scene.global_bind_group_layout,
            post_res.hdr_texture.format(),
            config.width,
            config.height,
        ));
        #[cfg(target_arch = "wasm32")]
        let gpu_fluid: Option<crate::gpu_fluid::GpuFluidSystem> = None;
        let debug_renderer = Some(crate::debug_renderer::GizmoRendererSystem::new(
            &device,
            &scene.global_bind_group_layout,
            wgpu::TextureFormat::Rgba16Float,
            wgpu::TextureFormat::Depth32Float,
        ));

        let scene_state = SceneState {
            render_pipeline: scene.render_pipeline,
            render_double_sided_pipeline: scene.render_double_sided_pipeline,
            wireframe_pipeline: scene.wireframe_pipeline,
            unlit_pipeline: scene.unlit_pipeline,
            sky_pipeline: scene.sky_pipeline,
            water_pipeline: scene.water_pipeline,
            shadow_pipeline: scene.shadow_pipeline,
            transparent_pipeline: scene.transparent_pipeline,
            grid_pipeline: scene.grid_pipeline,
            shadow_texture_view: scene.shadow_texture_view,
            shadow_cascade_layer_views: scene.shadow_cascade_layer_views,
            shadow_depth_texture: scene.shadow_depth_texture,
            point_shadow_depth_texture: scene.point_shadow_depth_texture,
            point_shadow_cube_view: scene.point_shadow_cube_view,
            point_shadow_face_views: scene.point_shadow_face_views,
            shadow_pass_bind_group_layout: scene.shadow_pass_bind_group_layout,
            shadow_cascade_uniform_buffers: scene.shadow_cascade_uniform_buffers,
            shadow_pass_bind_groups: scene.shadow_pass_bind_groups,
            point_shadow_uniform_buffers: scene.point_shadow_uniform_buffers,
            point_shadow_pass_bind_groups: scene.point_shadow_pass_bind_groups,
            global_uniform_buffer: scene.global_uniform_buffer,
            global_bind_group_layout: scene.global_bind_group_layout,
            global_bind_group: scene.global_bind_group,
            shadow_bind_group_layout: scene.shadow_bind_group_layout,
            shadow_bind_group: scene.shadow_bind_group,
            texture_bind_group_layout: scene.texture_bind_group_layout,
            skeleton_bind_group_layout: scene.skeleton_bind_group_layout,
            dummy_skeleton_bind_group: scene.dummy_skeleton_bind_group,
            instance_bind_group_layout: scene.instance_bind_group_layout,
            instance_buffer: scene.instance_buffer,
            instance_bind_group: scene.instance_bind_group,
            instance_capacity: scene.instance_capacity,
        };

        #[cfg(not(target_arch = "wasm32"))]
        let deferred = Some(crate::deferred::DeferredState::new(
            &device,
            &scene_state,
            size.width,
            size.height,
        ));
        #[cfg(target_arch = "wasm32")]
        let deferred: Option<crate::deferred::DeferredState> = None;

        #[cfg(not(target_arch = "wasm32"))]
        let gpu_cull = Some(crate::gpu_cull::GpuCullState::new(
            &device,
            &scene_state,
            scene_state.instance_capacity as u32,
        ));
        #[cfg(target_arch = "wasm32")]
        let gpu_cull: Option<crate::gpu_cull::GpuCullState> = None;

        let ssao = deferred.as_ref().map(|def| {
            crate::ssao::SsaoState::new(&device, &queue, &scene_state, def, size.width, size.height)
        });

        let ssr = deferred.as_ref().map(|def| {
            crate::ssr::SsrState::new(
                &device,
                &scene_state,
                def,
                &post_res.hdr_texture_view,
                size.width,
                size.height,
            )
        });

        let ssgi = deferred.as_ref().map(|def| {
            crate::ssgi::SsgiState::new(
                &device,
                &scene_state,
                def,
                &post_res.hdr_texture_view,
                size.width,
                size.height,
            )
        });

        let volumetric = deferred.as_ref().map(|def| {
            crate::volumetric::VolumetricState::new(
                &device,
                &scene_state,
                def,
                size.width,
                size.height,
            )
        });

        let decal = deferred
            .as_ref()
            .map(|def| crate::decal::DecalState::new(&device, &scene_state, def));

        let taa = if let Some(ref def) = deferred {
            Some(crate::taa::TaaState::new(
                &device,
                &post_res.hdr_texture_view,
                &def.world_position_view,
                size.width,
                size.height,
            ))
        } else {
            None
        };

        let post_state = PostProcessState {
            hdr_texture: post_res.hdr_texture,
            hdr_texture_view: post_res.hdr_texture_view,
            hdr_bind_group: post_res.hdr_bind_group,
            bloom_extract_texture_view: post_res.bloom_extract_texture_view,
            bloom_extract_bind_group: post_res.bloom_extract_bind_group,
            bloom_blur_texture_view: post_res.bloom_blur_texture_view,
            bloom_blur_bind_group: post_res.bloom_blur_bind_group,
            post_bind_group_layout: post_res.post_bind_group_layout,
            bloom_extract_pipeline: post_res.bloom_extract_pipeline,
            bloom_blur_pipeline: post_res.bloom_blur_pipeline,
            composite_pipeline: post_res.composite_pipeline,
            blur_params_buffer: post_res.blur_params_buffer,
            blur_params_bind_group_layout: post_res.blur_params_bind_group_layout,
            blur_h_bind_group: post_res.blur_h_bind_group,
            blur_v_bind_group: post_res.blur_v_bind_group,
            composite_bloom_bind_group_layout: post_res.composite_bloom_bind_group_layout,
            composite_bloom_bind_group: post_res.composite_bloom_bind_group,
            post_params_buffer: post_res.post_params_buffer,
            post_params_bind_group_layout: post_res.post_params_bind_group_layout,
            post_params_bind_group: post_res.post_params_bind_group,
        };

        // === FXAA Başlatma ===
        let fxaa = Some(crate::fxaa::FxaaState::new(
            &device,
            config.format,
            size.width,
            size.height,
        ));

        Self {
            surface,
            device,
            queue,
            config,
            size,
            depth_texture_view,
            scene: scene_state,
            post: post_state,
            deferred,
            gpu_cull,
            ssao,
            ssr,
            ssgi,
            volumetric,
            decal,
            taa,
            fxaa,
            gpu_particles,
            gpu_physics,
            gpu_fluid,
            smoke: None,
            debug_renderer,
            asset_manager: std::sync::RwLock::new(crate::asset::AssetManager::new()),
            web_profile: crate::web_profile::WebProfile::auto(),
            shading_mode: 0,
            // Studio Neutral (1), not Sunset Gold (0): the saturated-orange sunset
            // was the default for EVERY scene, tinting the sky orange and washing
            // albedo warm (a blue cube read as pink). Neutral shows true colours by
            // default; scenes wanting a mood set the preset explicitly.
            environment_preset: 1,
            environment_preset_2: 0,
            environment_blend_t: 0.0,
            bloom_intensity: 0.8,
            bloom_threshold: 0.85,
            exposure: 1.15,
            // Depth of field is an opt-in artistic effect, not a default: with it
            // ON, any scene whose subject isn't near `dof_focus_dist` (4.5 m) is
            // uniformly blurred — e.g. bevy_3d_scene's camera sits ~10 m out, so
            // the whole frame was smeared. Off by default; scenes enable it and
            // set the focus distance themselves.
            dof_enabled: false,
            dof_focus_dist: 4.5,
            dof_focus_range: 2.0,
            dof_blur_size: 4.0,
            // Off by default: chromatic aberration splits the R/B channels at every
            // high-contrast edge, so plain geometry (e.g. the ground plane against
            // the sky) got out-of-place red/blue fringes. Opt-in artistic effect,
            // like DoF — scenes that want the cinematic look set it themselves.
            chromatic_aberration: 0.0,
            film_grain_intensity: 0.012, // Photographic film grain — ince (statik olduğu için düşük; yüksekte düz yüzeylerde sabit gürültü kaplaması görünür)
            point_shadows_enabled: false,
            // The 100k-particle fluid system above is allocated but idle: its SSFR
            // water surface only composites when a scene opts in. Otherwise every
            // non-fluid scene got a stray mottled ocean drawn over it.
            fluid_enabled: false,
        }
    }
}