| ... | @@ -45,15 +45,28 @@ pub struct Scene3DRenderer { | ... | @@ -45,15 +45,28 @@ pub struct Scene3DRenderer { |
| 45 | shadow_face_bgl: wgpu::BindGroupLayout, | 45 | shadow_face_bgl: wgpu::BindGroupLayout, |
| 46 | /// 1x1 map bound when no light casts shadows. | 46 | /// 1x1 map bound when no light casts shadows. |
| 47 | shadow_dummy: wgpu::TextureView, | 47 | shadow_dummy: wgpu::TextureView, |
| | 48 | /// Immutable default `ShadowUniform` bound when no light casts shadows. |
| | 49 | shadow_dummy_ubuf: wgpu::Buffer, |
| 48 | /// Cube maps and depth buffer reused across renders; grown, never shrunk. | 50 | /// Cube maps and depth buffer reused across renders; grown, never shrunk. |
| 49 | shadow_targets: Mutex<Option<ShadowTargets>>, | 51 | shadow_targets: Mutex<Option<ShadowTargets>>, |
| | 52 | /// Main-pass `Depth32Float` targets pooled by `[width, height]`. |
| | 53 | depth_targets: Mutex<Lru<[u32; 2], DepthTarget>>, |
| | 54 | } |
| | 55 | |
| | 56 | #[derive(Clone)] |
| | 57 | struct DepthTarget { |
| | 58 | tex: wgpu::Texture, |
| | 59 | view: wgpu::TextureView, |
| 50 | } | 60 | } |
| 51 | | 61 | |
| 52 | #[derive(Clone)] | 62 | #[derive(Clone)] |
| 53 | struct ShadowTargets { | 63 | struct ShadowTargets { |
| 54 | maps: wgpu::Texture, | 64 | /// Array layers of the shadow cube-map texture (six per shadow light). |
| 55 | /// Array layers of `maps` (six per shadow light). | | |
| 56 | layers: u32, | 65 | layers: u32, |
| | 66 | /// `D2Array` view over all shadow-map layers. |
| | 67 | array_view: wgpu::TextureView, |
| | 68 | /// Per-layer `D2` views used as shadow-pass color attachments. |
| | 69 | layer_views: Vec<wgpu::TextureView>, |
| 57 | depth: wgpu::TextureView, | 70 | depth: wgpu::TextureView, |
| 58 | } | 71 | } |
| 59 | | 72 | |
| ... | @@ -63,6 +76,8 @@ const FACE_BYTES: usize = 80; | ... | @@ -63,6 +76,8 @@ const FACE_BYTES: usize = 80; |
| 63 | | 76 | |
| 64 | /// Most meshes kept resident on the GPU across frames. | 77 | /// Most meshes kept resident on the GPU across frames. |
| 65 | const MESH_CACHE_CAP: usize = 64; | 78 | const MESH_CACHE_CAP: usize = 64; |
| | 79 | /// Most main-pass depth targets pooled across distinct `Renderer3D` sizes. |
| | 80 | const DEPTH_CACHE_CAP: usize = 8; |
| 66 | | 81 | |
| 67 | /// `(base shape hash, image-plane aspect bits, wireframe)`; everything `build_shape_mesh` and the | 82 | /// `(base shape hash, image-plane aspect bits, wireframe)`; everything `build_shape_mesh` and the |
| 68 | /// edge conversion read, so animated transforms and materials reuse one upload. | 83 | /// edge conversion read, so animated transforms and materials reuse one upload. |
| ... | @@ -494,8 +509,8 @@ impl Scene3DRenderer { | ... | @@ -494,8 +509,8 @@ impl Scene3DRenderer { |
| 494 | visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, | 509 | visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, |
| 495 | ty: wgpu::BindingType::Buffer { | 510 | ty: wgpu::BindingType::Buffer { |
| 496 | ty: wgpu::BufferBindingType::Uniform, | 511 | ty: wgpu::BufferBindingType::Uniform, |
| 497 | has_dynamic_offset: false, | 512 | has_dynamic_offset: true, |
| 498 | min_binding_size: None, | 513 | min_binding_size: std::num::NonZeroU64::new(std::mem::size_of::<MeshUniform>() as u64), |
| 499 | }, | 514 | }, |
| 500 | count: None, | 515 | count: None, |
| 501 | }, | 516 | }, |
| ... | @@ -683,6 +698,22 @@ impl Scene3DRenderer { | ... | @@ -683,6 +698,22 @@ impl Scene3DRenderer { |
| 683 | dimension: Some(wgpu::TextureViewDimension::D2Array), | 698 | dimension: Some(wgpu::TextureViewDimension::D2Array), |
| 684 | ..Default::default() | 699 | ..Default::default() |
| 685 | }); | 700 | }); |
| | 701 | let shadow_dummy_uni = ShadowUniform { |
| | 702 | face_vp: [mat4_identity(); 6 * MAX_SHADOW_LIGHTS], |
| | 703 | point: [[-1.0, 0.0, 0.0, 0.0]; MAX_LIGHTS], |
| | 704 | }; |
| | 705 | let shadow_dummy_ubuf = device.create_buffer(&wgpu::BufferDescriptor { |
| | 706 | label: Some("scene3d-shadow-dummy-u"), |
| | 707 | size: std::mem::size_of::<ShadowUniform>() as u64, |
| | 708 | usage: wgpu::BufferUsages::UNIFORM, |
| | 709 | mapped_at_creation: true, |
| | 710 | }); |
| | 711 | shadow_dummy_ubuf |
| | 712 | .slice(..) |
| | 713 | .get_mapped_range_mut() |
| | 714 | .expect("mapped shadow dummy uniform buffer") |
| | 715 | .copy_from_slice(bytemuck::bytes_of(&shadow_dummy_uni)); |
| | 716 | shadow_dummy_ubuf.unmap(); |
| 686 | Self { | 717 | Self { |
| 687 | bgl, | 718 | bgl, |
| 688 | pipelines, | 719 | pipelines, |
| ... | @@ -693,7 +724,9 @@ impl Scene3DRenderer { | ... | @@ -693,7 +724,9 @@ impl Scene3DRenderer { |
| 693 | shadow_caster_bgl, | 724 | shadow_caster_bgl, |
| 694 | shadow_face_bgl, | 725 | shadow_face_bgl, |
| 695 | shadow_dummy, | 726 | shadow_dummy, |
| | 727 | shadow_dummy_ubuf, |
| 696 | shadow_targets: Mutex::new(None), | 728 | shadow_targets: Mutex::new(None), |
| | 729 | depth_targets: Mutex::new(Lru::new(DEPTH_CACHE_CAP)), |
| 697 | } | 730 | } |
| 698 | } | 731 | } |
| 699 | | 732 | |
| ... | @@ -713,12 +746,18 @@ impl Scene3DRenderer { | ... | @@ -713,12 +746,18 @@ impl Scene3DRenderer { |
| 713 | .get_or_insert_with(key, || Arc::clone(&built)) | 746 | .get_or_insert_with(key, || Arc::clone(&built)) |
| 714 | } | 747 | } |
| 715 | | 748 | |
| 716 | /// Drops the cached shadow cube-map pool and memoized `Text3D` CPU meshes. | 749 | /// Drops the cached shadow cube-map pool, main-pass depth target pool, and memoized `Text3D` CPU meshes. |
| 717 | pub fn clear_shadow_targets(&self) { | 750 | pub fn clear_shadow_targets(&self) { |
| 718 | *self.shadow_targets.lock().unwrap() = None; | 751 | *self.shadow_targets.lock().unwrap() = None; |
| | 752 | self.depth_targets.lock().unwrap().clear(); |
| 719 | self.text3d_mesh_cache.lock().unwrap().clear(); | 753 | self.text3d_mesh_cache.lock().unwrap().clear(); |
| 720 | } | 754 | } |
| 721 | | 755 | |
| | 756 | /// Drops all pooled 3D render targets (`shadow_targets` and `depth_targets`) and `Text3D` CPU meshes. |
| | 757 | pub fn clear_targets(&self) { |
| | 758 | self.clear_shadow_targets(); |
| | 759 | } |
| | 760 | |
| 722 | /// Total GPU VRAM in bytes currently occupied by `shadow_targets` (R32Float layers + Depth32Float). | 761 | /// Total GPU VRAM in bytes currently occupied by `shadow_targets` (R32Float layers + Depth32Float). |
| 723 | pub fn shadow_vram_bytes(&self) -> u64 { | 762 | pub fn shadow_vram_bytes(&self) -> u64 { |
| 724 | self.shadow_targets.lock().unwrap().as_ref().map_or(0, |t| { | 763 | self.shadow_targets.lock().unwrap().as_ref().map_or(0, |t| { |
| ... | @@ -727,6 +766,17 @@ impl Scene3DRenderer { | ... | @@ -727,6 +766,17 @@ impl Scene3DRenderer { |
| 727 | }) | 766 | }) |
| 728 | } | 767 | } |
| 729 | | 768 | |
| | 769 | /// Total GPU VRAM in bytes currently occupied by pooled main-pass `Depth32Float` targets. |
| | 770 | pub fn depth_vram_bytes(&self) -> u64 { |
| | 771 | self.depth_targets |
| | 772 | .lock() |
| | 773 | .unwrap() |
| | 774 | .entries |
| | 775 | .values() |
| | 776 | .map(|(_, dt)| u64::from(dt.tex.width()) * u64::from(dt.tex.height()) * 4) |
| | 777 | .sum() |
| | 778 | } |
| | 779 | |
| 730 | #[cfg(test)] | 780 | #[cfg(test)] |
| 731 | pub(crate) fn gpu_mesh_count(&self) -> usize { | 781 | pub(crate) fn gpu_mesh_count(&self) -> usize { |
| 732 | self.meshes.lock().unwrap().entries.len() | 782 | self.meshes.lock().unwrap().entries.len() |
| ... | @@ -749,21 +799,26 @@ impl Scene3DRenderer { | ... | @@ -749,21 +799,26 @@ impl Scene3DRenderer { |
| 749 | shadows_enabled: bool, | 799 | shadows_enabled: bool, |
| 750 | meshes: &[DrawMesh], | 800 | meshes: &[DrawMesh], |
| 751 | ) { | 801 | ) { |
| 752 | let depth_tex = device.create_texture(&wgpu::TextureDescriptor { | 802 | let depth_size = [dst.size[0].max(1), dst.size[1].max(1)]; |
| 753 | label: Some("scene3d-depth"), | 803 | let depth_target = self.depth_targets.lock().unwrap().get_or_insert_with(depth_size, || { |
| 754 | size: wgpu::Extent3d { | 804 | let tex = device.create_texture(&wgpu::TextureDescriptor { |
| 755 | width: dst.size[0].max(1), | 805 | label: Some("scene3d-depth"), |
| 756 | height: dst.size[1].max(1), | 806 | size: wgpu::Extent3d { |
| 757 | depth_or_array_layers: 1, | 807 | width: depth_size[0], |
| 758 | }, | 808 | height: depth_size[1], |
| 759 | mip_level_count: 1, | 809 | depth_or_array_layers: 1, |
| 760 | sample_count: 1, | 810 | }, |
| 761 | dimension: wgpu::TextureDimension::D2, | 811 | mip_level_count: 1, |
| 762 | format: wgpu::TextureFormat::Depth32Float, | 812 | sample_count: 1, |
| 763 | usage: wgpu::TextureUsages::RENDER_ATTACHMENT, | 813 | dimension: wgpu::TextureDimension::D2, |
| 764 | view_formats: &[], | 814 | format: wgpu::TextureFormat::Depth32Float, |
| | 815 | usage: wgpu::TextureUsages::RENDER_ATTACHMENT, |
| | 816 | view_formats: &[], |
| | 817 | }); |
| | 818 | let view = tex.create_view(&wgpu::TextureViewDescriptor::default()); |
| | 819 | DepthTarget { tex, view } |
| 765 | }); | 820 | }); |
| 766 | let depth_view = depth_tex.create_view(&wgpu::TextureViewDescriptor::default()); | 821 | let depth_view = &depth_target.view; |
| 767 | | 822 | |
| 768 | // Point-light indices that get a cube shadow map, in slot order. | 823 | // Point-light indices that get a cube shadow map, in slot order. |
| 769 | let shadow_lights: Vec<usize> = if shadows_enabled { | 824 | let shadow_lights: Vec<usize> = if shadows_enabled { |
| ... | @@ -774,25 +829,32 @@ impl Scene3DRenderer { | ... | @@ -774,25 +829,32 @@ impl Scene3DRenderer { |
| 774 | } else { | 829 | } else { |
| 775 | Vec::new() | 830 | Vec::new() |
| 776 | }; | 831 | }; |
| 777 | let mut shadow_uni = ShadowUniform { | 832 | let mut shadow_face_vps = [mat4_identity(); 6 * MAX_SHADOW_LIGHTS]; |
| 778 | face_vp: [mat4_identity(); 6 * MAX_SHADOW_LIGHTS], | 833 | let shadow_ubuf_owned = (!shadow_lights.is_empty()).then(|| { |
| 779 | point: [[-1.0, 0.0, 0.0, 0.0]; MAX_LIGHTS], | 834 | let mut shadow_uni = ShadowUniform { |
| 780 | }; | 835 | face_vp: [mat4_identity(); 6 * MAX_SHADOW_LIGHTS], |
| 781 | // Receivers visible to the camera are at most `cam_far` from it, so that bounds the light's reach. | 836 | point: [[-1.0, 0.0, 0.0, 0.0]; MAX_LIGHTS], |
| 782 | let distance_to_cam = |p: [f32; 3]| (0..3).map(|i| (p[i] - cam_pos[i]).powi(2)).sum::<f32>().sqrt(); | 837 | }; |
| 783 | for (slot, &li) in shadow_lights.iter().enumerate() { | 838 | // Receivers visible to the camera are at most `cam_far` from it, so that bounds the light's reach. |
| 784 | let pl = &lights.points[li]; | 839 | let distance_to_cam = |p: [f32; 3]| (0..3).map(|i| (p[i] - cam_pos[i]).powi(2)).sum::<f32>().sqrt(); |
| 785 | let far = cam_far + distance_to_cam(pl.pos); | 840 | for (slot, &li) in shadow_lights.iter().enumerate() { |
| 786 | shadow_uni.face_vp[slot * 6..slot * 6 + 6].copy_from_slice(&cube_face_view_projections(pl.pos, far)); | 841 | let pl = &lights.points[li]; |
| 787 | shadow_uni.point[li] = [slot as f32, pl.shadow_density.unwrap_or(0.0), 0.0, 0.0]; | 842 | let far = cam_far + distance_to_cam(pl.pos); |
| 788 | } | 843 | let vps = cube_face_view_projections(pl.pos, far); |
| 789 | let shadow_ubuf = device.create_buffer(&wgpu::BufferDescriptor { | 844 | shadow_uni.face_vp[slot * 6..slot * 6 + 6].copy_from_slice(&vps); |
| 790 | label: Some("scene3d-shadow-u"), | 845 | shadow_face_vps[slot * 6..slot * 6 + 6].copy_from_slice(&vps); |
| 791 | size: std::mem::size_of::<ShadowUniform>() as u64, | 846 | shadow_uni.point[li] = [slot as f32, pl.shadow_density.unwrap_or(0.0), 0.0, 0.0]; |
| 792 | usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, | 847 | } |
| 793 | mapped_at_creation: false, | 848 | let buf = device.create_buffer(&wgpu::BufferDescriptor { |
| | 849 | label: Some("scene3d-shadow-u"), |
| | 850 | size: std::mem::size_of::<ShadowUniform>() as u64, |
| | 851 | usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, |
| | 852 | mapped_at_creation: false, |
| | 853 | }); |
| | 854 | queue.write_buffer(&buf, 0, bytemuck::bytes_of(&shadow_uni)); |
| | 855 | buf |
| 794 | }); | 856 | }); |
| 795 | queue.write_buffer(&shadow_ubuf, 0, bytemuck::bytes_of(&shadow_uni)); | 857 | let shadow_ubuf = shadow_ubuf_owned.as_ref().unwrap_or(&self.shadow_dummy_ubuf); |
| 796 | let shadow_targets = (!shadow_lights.is_empty()).then(|| { | 858 | let shadow_targets = (!shadow_lights.is_empty()).then(|| { |
| 797 | let layers = 6 * shadow_lights.len() as u32; | 859 | let layers = 6 * shadow_lights.len() as u32; |
| 798 | let mut cache = self.shadow_targets.lock().unwrap(); | 860 | let mut cache = self.shadow_targets.lock().unwrap(); |
| ... | @@ -811,14 +873,30 @@ impl Scene3DRenderer { | ... | @@ -811,14 +873,30 @@ impl Scene3DRenderer { |
| 811 | view_formats: &[], | 873 | view_formats: &[], |
| 812 | }) | 874 | }) |
| 813 | }; | 875 | }; |
| | 876 | let maps = target( |
| | 877 | "scene3d-shadow-maps", |
| | 878 | wgpu::TextureFormat::R32Float, |
| | 879 | wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, |
| | 880 | layers, |
| | 881 | ); |
| | 882 | let array_view = maps.create_view(&wgpu::TextureViewDescriptor { |
| | 883 | dimension: Some(wgpu::TextureViewDimension::D2Array), |
| | 884 | ..Default::default() |
| | 885 | }); |
| | 886 | let layer_views = (0..layers) |
| | 887 | .map(|layer| { |
| | 888 | maps.create_view(&wgpu::TextureViewDescriptor { |
| | 889 | dimension: Some(wgpu::TextureViewDimension::D2), |
| | 890 | base_array_layer: layer, |
| | 891 | array_layer_count: Some(1), |
| | 892 | ..Default::default() |
| | 893 | }) |
| | 894 | }) |
| | 895 | .collect(); |
| 814 | let fresh = ShadowTargets { | 896 | let fresh = ShadowTargets { |
| 815 | maps: target( | | |
| 816 | "scene3d-shadow-maps", | | |
| 817 | wgpu::TextureFormat::R32Float, | | |
| 818 | wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, | | |
| 819 | layers, | | |
| 820 | ), | | |
| 821 | layers, | 897 | layers, |
| | 898 | array_view, |
| | 899 | layer_views, |
| 822 | depth: target( | 900 | depth: target( |
| 823 | "scene3d-shadow-depth", | 901 | "scene3d-shadow-depth", |
| 824 | wgpu::TextureFormat::Depth32Float, | 902 | wgpu::TextureFormat::Depth32Float, |
| ... | @@ -832,12 +910,7 @@ impl Scene3DRenderer { | ... | @@ -832,12 +910,7 @@ impl Scene3DRenderer { |
| 832 | } | 910 | } |
| 833 | } | 911 | } |
| 834 | }); | 912 | }); |
| 835 | let shadow_view = shadow_targets.as_ref().map(|t| { | 913 | let shadow_view = shadow_targets.as_ref().map(|t| &t.array_view); |
| 836 | t.maps.create_view(&wgpu::TextureViewDescriptor { | | |
| 837 | dimension: Some(wgpu::TextureViewDimension::D2Array), | | |
| 838 | ..Default::default() | | |
| 839 | }) | | |
| 840 | }); | | |
| 841 | // One buffer holds every caster and face uniform of this render at dynamic offsets: casters | 914 | // One buffer holds every caster and face uniform of this render at dynamic offsets: casters |
| 842 | // first (at most one per mesh), then six faces per light. | 915 | // first (at most one per mesh), then six faces per light. |
| 843 | let align = device.limits().min_uniform_buffer_offset_alignment; | 916 | let align = device.limits().min_uniform_buffer_offset_alignment; |
| ... | @@ -852,8 +925,35 @@ impl Scene3DRenderer { | ... | @@ -852,8 +925,35 @@ impl Scene3DRenderer { |
| 852 | mapped_at_creation: false, | 925 | mapped_at_creation: false, |
| 853 | }) | 926 | }) |
| 854 | }); | 927 | }); |
| | 928 | let caster_bg = shadow_dyn_buf.as_ref().map(|buf| { |
| | 929 | device.create_bind_group(&wgpu::BindGroupDescriptor { |
| | 930 | label: Some("scene3d-shadow-caster"), |
| | 931 | layout: &self.shadow_caster_bgl, |
| | 932 | entries: &[wgpu::BindGroupEntry { |
| | 933 | binding: 0, |
| | 934 | resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { |
| | 935 | buffer: buf, |
| | 936 | offset: 0, |
| | 937 | size: std::num::NonZeroU64::new(CASTER_BYTES as u64), |
| | 938 | }), |
| | 939 | }], |
| | 940 | }) |
| | 941 | }); |
| 855 | let mut n_casters = 0; | 942 | let mut n_casters = 0; |
| 856 | | 943 | |
| | 944 | // One dynamic-offset uniform buffer holds every mesh's `MeshUniform` for this render call. |
| | 945 | let mesh_uni_size = std::mem::size_of::<MeshUniform>(); |
| | 946 | let mesh_uni_stride = (mesh_uni_size as u32).next_multiple_of(align) as usize; |
| | 947 | let mut mesh_uniform_bytes = vec![0u8; meshes.len() * mesh_uni_stride]; |
| | 948 | let mesh_dyn_buf = (!meshes.is_empty()).then(|| { |
| | 949 | device.create_buffer(&wgpu::BufferDescriptor { |
| | 950 | label: Some("scene3d-mesh-dyn"), |
| | 951 | size: mesh_uniform_bytes.len() as u64, |
| | 952 | usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, |
| | 953 | mapped_at_creation: false, |
| | 954 | }) |
| | 955 | }); |
| | 956 | |
| 857 | // Sort back-to-front in camera view space so semi-transparent planes blend cleanly. | 957 | // Sort back-to-front in camera view space so semi-transparent planes blend cleanly. |
| 858 | let mut order: Vec<(usize, f32)> = meshes | 958 | let mut order: Vec<(usize, f32)> = meshes |
| 859 | .iter() | 959 | .iter() |
| ... | @@ -873,8 +973,9 @@ impl Scene3DRenderer { | ... | @@ -873,8 +973,9 @@ impl Scene3DRenderer { |
| 873 | /// Skip the `[back-facing, front-facing]` pass. | 973 | /// Skip the `[back-facing, front-facing]` pass. |
| 874 | cull: [bool; 2], | 974 | cull: [bool; 2], |
| 875 | bg: wgpu::BindGroup, | 975 | bg: wgpu::BindGroup, |
| 876 | /// Shadow-pass bind group and dynamic uniform offset; `None` for meshes that cast no shadow. | 976 | uni_offset: u32, |
| 877 | caster: Option<(wgpu::BindGroup, u32)>, | 977 | /// Dynamic offset into `shadow_dyn_buf`; `None` for meshes that cast no shadow. |
| | 978 | caster_offset: Option<u32>, |
| 878 | vbuf: wgpu::Buffer, | 979 | vbuf: wgpu::Buffer, |
| 879 | ibuf: wgpu::Buffer, | 980 | ibuf: wgpu::Buffer, |
| 880 | idx_count: u32, | 981 | idx_count: u32, |
| ... | @@ -994,61 +1095,58 @@ impl Scene3DRenderer { | ... | @@ -994,61 +1095,58 @@ impl Scene3DRenderer { |
| 994 | spot_dir, | 1095 | spot_dir, |
| 995 | spot_col, | 1096 | spot_col, |
| 996 | }; | 1097 | }; |
| 997 | let ubuf = device.create_buffer(&wgpu::BufferDescriptor { | 1098 | let mesh_buf = mesh_dyn_buf.as_ref().expect("mesh_dyn_buf allocated when meshes non-empty"); |
| 998 | label: Some("scene3d-u"), | 1099 | let uni_offset = draw_items.len() * mesh_uni_stride; |
| 999 | size: std::mem::size_of::<MeshUniform>() as u64, | 1100 | mesh_uniform_bytes[uni_offset..uni_offset + mesh_uni_size].copy_from_slice(bytemuck::bytes_of(&uni)); |
| 1000 | usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, | | |
| 1001 | mapped_at_creation: false, | | |
| 1002 | }); | | |
| 1003 | queue.write_buffer(&ubuf, 0, bytemuck::bytes_of(&uni)); | | |
| 1004 | let tex_view = m.tex.as_ref().map_or(&dummy.view, |t| &t.view); | 1101 | let tex_view = m.tex.as_ref().map_or(&dummy.view, |t| &t.view); |
| 1005 | let bump_view = bump_src.map_or(&dummy.view, |t| &t.view); | 1102 | let bump_view = bump_src.map_or(&dummy.view, |t| &t.view); |
| 1006 | let bg = device.create_bind_group(&wgpu::BindGroupDescriptor { | 1103 | let bg = device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 1007 | label: Some("scene3d-bg"), | 1104 | label: Some("scene3d-bg"), |
| 1008 | layout: &self.bgl, | 1105 | layout: &self.bgl, |
| 1009 | entries: &[ | 1106 | entries: &[ |
| 1010 | wgpu::BindGroupEntry { binding: 0, resource: ubuf.as_entire_binding() }, | 1107 | wgpu::BindGroupEntry { |
| | 1108 | binding: 0, |
| | 1109 | resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { |
| | 1110 | buffer: mesh_buf, |
| | 1111 | offset: 0, |
| | 1112 | size: std::num::NonZeroU64::new(mesh_uni_size as u64), |
| | 1113 | }), |
| | 1114 | }, |
| 1011 | wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(tex_view) }, | 1115 | wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(tex_view) }, |
| 1012 | wgpu::BindGroupEntry { binding: 2, resource: wgpu::BindingResource::Sampler(&self.sampler) }, | 1116 | wgpu::BindGroupEntry { binding: 2, resource: wgpu::BindingResource::Sampler(&self.sampler) }, |
| 1013 | wgpu::BindGroupEntry { binding: 3, resource: shadow_ubuf.as_entire_binding() }, | 1117 | wgpu::BindGroupEntry { binding: 3, resource: shadow_ubuf.as_entire_binding() }, |
| 1014 | wgpu::BindGroupEntry { | 1118 | wgpu::BindGroupEntry { |
| 1015 | binding: 4, | 1119 | binding: 4, |
| 1016 | resource: wgpu::BindingResource::TextureView(shadow_view.as_ref().unwrap_or(&self.shadow_dummy)), | 1120 | resource: wgpu::BindingResource::TextureView(shadow_view.unwrap_or(&self.shadow_dummy)), |
| 1017 | }, | 1121 | }, |
| 1018 | wgpu::BindGroupEntry { binding: 5, resource: wgpu::BindingResource::TextureView(bump_view) }, | 1122 | wgpu::BindGroupEntry { binding: 5, resource: wgpu::BindingResource::TextureView(bump_view) }, |
| 1019 | ], | 1123 | ], |
| 1020 | }); | 1124 | }); |
| 1021 | // Fusion's RendererOpenGL casts solid geometry shadows regardless of texture/material alpha. | 1125 | // Fusion's RendererOpenGL casts solid geometry shadows regardless of texture/material alpha. |
| 1022 | let caster = shadow_dyn_buf.as_ref().filter(|_| m.casts_shadow && !fx.wireframe).map(|buf| { | 1126 | let caster_offset = shadow_dyn_buf.as_ref().filter(|_| m.casts_shadow && !fx.wireframe).map(|_| { |
| 1023 | let offset = n_casters * uniform_stride; | 1127 | let offset = n_casters * uniform_stride; |
| 1024 | shadow_uniform_bytes[offset..offset + CASTER_BYTES].copy_from_slice(bytemuck::cast_slice(&m.world)); | 1128 | shadow_uniform_bytes[offset..offset + CASTER_BYTES].copy_from_slice(bytemuck::cast_slice(&m.world)); |
| 1025 | n_casters += 1; | 1129 | n_casters += 1; |
| 1026 | let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { | 1130 | offset as u32 |
| 1027 | label: Some("scene3d-shadow-caster"), | | |
| 1028 | layout: &self.shadow_caster_bgl, | | |
| 1029 | entries: &[wgpu::BindGroupEntry { | | |
| 1030 | binding: 0, | | |
| 1031 | resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { | | |
| 1032 | buffer: buf, | | |
| 1033 | offset: 0, | | |
| 1034 | size: std::num::NonZeroU64::new(CASTER_BYTES as u64), | | |
| 1035 | }), | | |
| 1036 | }], | | |
| 1037 | }); | | |
| 1038 | (bind_group, offset as u32) | | |
| 1039 | }); | 1131 | }); |
| 1040 | draw_items.push(GpuDraw { | 1132 | draw_items.push(GpuDraw { |
| 1041 | wireframe: fx.wireframe, | 1133 | wireframe: fx.wireframe, |
| 1042 | cull: [fx.cull_back, fx.cull_front], | 1134 | cull: [fx.cull_back, fx.cull_front], |
| 1043 | bg, | 1135 | bg, |
| 1044 | caster, | 1136 | uni_offset: uni_offset as u32, |
| | 1137 | caster_offset, |
| 1045 | vbuf, | 1138 | vbuf, |
| 1046 | ibuf, | 1139 | ibuf, |
| 1047 | idx_count, | 1140 | idx_count, |
| 1048 | }); | 1141 | }); |
| 1049 | } | 1142 | } |
| | 1143 | if let Some(mesh_buf) = &mesh_dyn_buf { |
| | 1144 | if !draw_items.is_empty() { |
| | 1145 | queue.write_buffer(mesh_buf, 0, &mesh_uniform_bytes[..draw_items.len() * mesh_uni_stride]); |
| | 1146 | } |
| | 1147 | } |
| 1050 | | 1148 | |
| 1051 | if let (Some(targets), Some(buf)) = (&shadow_targets, &shadow_dyn_buf) { | 1149 | if let (Some(targets), Some(buf), Some(caster_bg)) = (&shadow_targets, &shadow_dyn_buf, &caster_bg) { |
| 1052 | let face_bg = device.create_bind_group(&wgpu::BindGroupDescriptor { | 1150 | let face_bg = device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 1053 | label: Some("scene3d-shadow-face"), | 1151 | label: Some("scene3d-shadow-face"), |
| 1054 | layout: &self.shadow_face_bgl, | 1152 | layout: &self.shadow_face_bgl, |
| ... | @@ -1066,20 +1164,15 @@ impl Scene3DRenderer { | ... | @@ -1066,20 +1164,15 @@ impl Scene3DRenderer { |
| 1066 | for face in 0..6 { | 1164 | for face in 0..6 { |
| 1067 | let layer = slot * 6 + face; | 1165 | let layer = slot * 6 + face; |
| 1068 | let mut uniform = [0.0_f32; FACE_BYTES / 4]; | 1166 | let mut uniform = [0.0_f32; FACE_BYTES / 4]; |
| 1069 | uniform[..16].copy_from_slice(&shadow_uni.face_vp[layer]); | 1167 | uniform[..16].copy_from_slice(&shadow_face_vps[layer]); |
| 1070 | uniform[16..19].copy_from_slice(&pos); | 1168 | uniform[16..19].copy_from_slice(&pos); |
| 1071 | let face_offset = face_base + layer * uniform_stride; | 1169 | let face_offset = face_base + layer * uniform_stride; |
| 1072 | shadow_uniform_bytes[face_offset..face_offset + FACE_BYTES].copy_from_slice(bytemuck::cast_slice(&uniform)); | 1170 | shadow_uniform_bytes[face_offset..face_offset + FACE_BYTES].copy_from_slice(bytemuck::cast_slice(&uniform)); |
| 1073 | let layer_view = targets.maps.create_view(&wgpu::TextureViewDescriptor { | 1171 | let layer_view = &targets.layer_views[layer]; |
| 1074 | dimension: Some(wgpu::TextureViewDimension::D2), | | |
| 1075 | base_array_layer: layer as u32, | | |
| 1076 | array_layer_count: Some(1), | | |
| 1077 | ..Default::default() | | |
| 1078 | }); | | |
| 1079 | let mut pass = enc.begin_render_pass(&wgpu::RenderPassDescriptor { | 1172 | let mut pass = enc.begin_render_pass(&wgpu::RenderPassDescriptor { |
| 1080 | label: Some("scene3d-shadow-pass"), | 1173 | label: Some("scene3d-shadow-pass"), |
| 1081 | color_attachments: &[Some(wgpu::RenderPassColorAttachment { | 1174 | color_attachments: &[Some(wgpu::RenderPassColorAttachment { |
| 1082 | view: &layer_view, | 1175 | view: layer_view, |
| 1083 | depth_slice: None, | 1176 | depth_slice: None, |
| 1084 | resolve_target: None, | 1177 | resolve_target: None, |
| 1085 | ops: wgpu::Operations { | 1178 | ops: wgpu::Operations { |
| ... | @@ -1102,8 +1195,8 @@ impl Scene3DRenderer { | ... | @@ -1102,8 +1195,8 @@ impl Scene3DRenderer { |
| 1102 | pass.set_pipeline(&self.shadow_pipeline); | 1195 | pass.set_pipeline(&self.shadow_pipeline); |
| 1103 | pass.set_bind_group(1, &face_bg, &[face_offset as u32]); | 1196 | pass.set_bind_group(1, &face_bg, &[face_offset as u32]); |
| 1104 | for d in &draw_items { | 1197 | for d in &draw_items { |
| 1105 | let Some((caster, caster_offset)) = &d.caster else { continue }; | 1198 | let Some(caster_offset) = d.caster_offset else { continue }; |
| 1106 | pass.set_bind_group(0, caster, &[*caster_offset]); | 1199 | pass.set_bind_group(0, caster_bg, &[caster_offset]); |
| 1107 | pass.set_vertex_buffer(0, d.vbuf.slice(..)); | 1200 | pass.set_vertex_buffer(0, d.vbuf.slice(..)); |
| 1108 | pass.set_index_buffer(d.ibuf.slice(..), wgpu::IndexFormat::Uint32); | 1201 | pass.set_index_buffer(d.ibuf.slice(..), wgpu::IndexFormat::Uint32); |
| 1109 | pass.draw_indexed(0..d.idx_count, 0, 0..1); | 1202 | pass.draw_indexed(0..d.idx_count, 0, 0..1); |
| ... | @@ -1125,7 +1218,7 @@ impl Scene3DRenderer { | ... | @@ -1125,7 +1218,7 @@ impl Scene3DRenderer { |
| 1125 | }, | 1218 | }, |
| 1126 | })], | 1219 | })], |
| 1127 | depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { | 1220 | depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { |
| 1128 | view: &depth_view, | 1221 | view: depth_view, |
| 1129 | depth_ops: Some(wgpu::Operations { | 1222 | depth_ops: Some(wgpu::Operations { |
| 1130 | load: wgpu::LoadOp::Clear(1.0), | 1223 | load: wgpu::LoadOp::Clear(1.0), |
| 1131 | store: wgpu::StoreOp::Discard, | 1224 | store: wgpu::StoreOp::Discard, |
| ... | @@ -1137,7 +1230,7 @@ impl Scene3DRenderer { | ... | @@ -1137,7 +1230,7 @@ impl Scene3DRenderer { |
| 1137 | multiview_mask: None, | 1230 | multiview_mask: None, |
| 1138 | }); | 1231 | }); |
| 1139 | for d in &draw_items { | 1232 | for d in &draw_items { |
| 1140 | pass.set_bind_group(0, &d.bg, &[]); | 1233 | pass.set_bind_group(0, &d.bg, &[d.uni_offset]); |
| 1141 | pass.set_vertex_buffer(0, d.vbuf.slice(..)); | 1234 | pass.set_vertex_buffer(0, d.vbuf.slice(..)); |
| 1142 | pass.set_index_buffer(d.ibuf.slice(..), wgpu::IndexFormat::Uint32); | 1235 | pass.set_index_buffer(d.ibuf.slice(..), wgpu::IndexFormat::Uint32); |
| 1143 | for (pipeline, culled) in self.pipelines[d.wireframe as usize].iter().zip(d.cull) { | 1236 | for (pipeline, culled) in self.pipelines[d.wireframe as usize].iter().zip(d.cull) { |
| ... | @@ -1524,6 +1617,69 @@ mod tests { | ... | @@ -1524,6 +1617,69 @@ mod tests { |
| 1524 | | 1617 | |
| 1525 | renderer.clear_shadow_targets(); | 1618 | renderer.clear_shadow_targets(); |
| 1526 | assert_eq!(renderer.shadow_vram_bytes(), 0); | 1619 | assert_eq!(renderer.shadow_vram_bytes(), 0); |
| | 1620 | assert_eq!(renderer.depth_vram_bytes(), 0); |
| 1527 | assert_eq!(renderer.text3d_mesh_cache.lock().unwrap().entries.len(), 0); | 1621 | assert_eq!(renderer.text3d_mesh_cache.lock().unwrap().entries.len(), 0); |
| 1528 | } | 1622 | } |
| | 1623 | |
| | 1624 | #[test] |
| | 1625 | fn depth_targets_are_reused_by_size_and_cleared_on_clear_targets() { |
| | 1626 | let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); |
| | 1627 | let Ok(adapter) = pollster::block_on(instance.request_adapter(&Default::default())) else { return }; |
| | 1628 | let Ok((device, queue)) = pollster::block_on(adapter.request_device(&Default::default())) else { return }; |
| | 1629 | let renderer = Scene3DRenderer::new(&device); |
| | 1630 | let make_tex = |w, h| { |
| | 1631 | CachedTexture::from_texture( |
| | 1632 | device.create_texture(&wgpu::TextureDescriptor { |
| | 1633 | label: None, |
| | 1634 | size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, |
| | 1635 | mip_level_count: 1, |
| | 1636 | sample_count: 1, |
| | 1637 | dimension: wgpu::TextureDimension::D2, |
| | 1638 | format: wgpu::TextureFormat::Rgba8Unorm, |
| | 1639 | usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT, |
| | 1640 | view_formats: &[], |
| | 1641 | }), |
| | 1642 | [w, h], |
| | 1643 | 0, |
| | 1644 | ) |
| | 1645 | }; |
| | 1646 | let dst_a = make_tex(16, 16); |
| | 1647 | let dst_b = make_tex(32, 16); |
| | 1648 | let dummy = make_tex(8, 8); |
| | 1649 | let render_empty = |dst: &CachedTexture| { |
| | 1650 | let mut enc = device.create_command_encoder(&Default::default()); |
| | 1651 | renderer.render_meshes( |
| | 1652 | &device, |
| | 1653 | &queue, |
| | 1654 | &mut enc, |
| | 1655 | dst, |
| | 1656 | &dummy, |
| | 1657 | mat4_identity(), |
| | 1658 | mat4_identity(), |
| | 1659 | [0.0, 0.0, 5.0], |
| | 1660 | 100.0, |
| | 1661 | SceneLights::default(), |
| | 1662 | false, |
| | 1663 | false, |
| | 1664 | &[], |
| | 1665 | ); |
| | 1666 | queue.submit(Some(enc.finish())); |
| | 1667 | }; |
| | 1668 | |
| | 1669 | render_empty(&dst_a); |
| | 1670 | assert_eq!(renderer.depth_targets.lock().unwrap().entries.len(), 1); |
| | 1671 | assert_eq!(renderer.depth_vram_bytes(), 16 * 16 * 4); |
| | 1672 | |
| | 1673 | render_empty(&dst_a); |
| | 1674 | assert_eq!(renderer.depth_targets.lock().unwrap().entries.len(), 1); |
| | 1675 | assert_eq!(renderer.depth_vram_bytes(), 16 * 16 * 4); |
| | 1676 | |
| | 1677 | render_empty(&dst_b); |
| | 1678 | assert_eq!(renderer.depth_targets.lock().unwrap().entries.len(), 2); |
| | 1679 | assert_eq!(renderer.depth_vram_bytes(), (16 * 16 + 32 * 16) * 4); |
| | 1680 | |
| | 1681 | renderer.clear_targets(); |
| | 1682 | assert_eq!(renderer.depth_targets.lock().unwrap().entries.len(), 0); |
| | 1683 | assert_eq!(renderer.depth_vram_bytes(), 0); |
| | 1684 | } |
| 1529 | } | 1685 | } |