| ... | ... | @@ -25,7 +25,7 @@ use std::sync::{Arc, Mutex}; |
| 25 | 25 | use crate::cache::CachedTexture; |
| 26 | 26 | use mesh::{apply_heightmap_displace, build_shape_mesh, triangle_edges}; |
| 27 | 27 | use model::NodeId; |
| 28 | | use shader::{MeshUniform, ShadowUniform, MAX_DIR_LIGHTS, MAX_LIGHTS, MAX_SHADOW_LIGHTS, SHADER_3D, SHADOW_MAP_SIZE, SHADOW_SHADER}; |
| 28 | use shader::{MeshUniform, ShadowUniform, MAX_DIR_LIGHTS, MAX_LIGHTS, MAX_SHADOW_LIGHTS, SHADER_3D, SHADOW_MAP_SIZE, SHADOW_SHADER, UV_XFORM_WGSL}; |
| 29 | 29 | |
| 30 | 30 | pub type Mat4 = [f32; 16]; // Column-major 4x4 |
| 31 | 31 | |
| ... | ... | @@ -37,12 +37,28 @@ pub struct Scene3DRenderer { |
| 37 | 37 | sampler: wgpu::Sampler, |
| 38 | 38 | meshes: Mutex<Lru<MeshKey, Option<GpuMesh>>>, |
| 39 | 39 | shadow_pipeline: wgpu::RenderPipeline, |
| 40 | | /// Bound for `Caster` (group 0) and `Face` (group 1) uniforms of the shadow pass. |
| 41 | | shadow_uniform_bgl: wgpu::BindGroupLayout, |
| 40 | /// `Caster` uniform (dynamic offset), material texture and sampler: group 0 of the shadow pass. |
| 41 | shadow_caster_bgl: wgpu::BindGroupLayout, |
| 42 | /// `Face` uniform (dynamic offset): group 1 of the shadow pass. |
| 43 | shadow_face_bgl: wgpu::BindGroupLayout, |
| 42 | 44 | /// 1x1 map bound when no light casts shadows. |
| 43 | 45 | shadow_dummy: wgpu::TextureView, |
| 46 | /// Cube maps and depth buffer reused across renders; grown, never shrunk. |
| 47 | shadow_targets: Mutex<Option<ShadowTargets>>, |
| 44 | 48 | } |
| 45 | 49 | |
| 50 | #[derive(Clone)] |
| 51 | struct ShadowTargets { |
| 52 | maps: wgpu::Texture, |
| 53 | /// Array layers of `maps` (six per shadow light). |
| 54 | layers: u32, |
| 55 | depth: wgpu::TextureView, |
| 56 | } |
| 57 | |
| 58 | /// Bytes of the shadow pass's `Caster` and `Face` uniforms. |
| 59 | const CASTER_BYTES: usize = 128; |
| 60 | const FACE_BYTES: usize = 80; |
| 61 | |
| 46 | 62 | /// Most meshes kept resident on the GPU across frames. |
| 47 | 63 | const MESH_CACHE_CAP: usize = 64; |
| 48 | 64 | |
| ... | ... | @@ -378,8 +394,8 @@ fn mesh_key(base_shape: &ShapeKind, aspect: f32, wireframe: bool) -> MeshKey { |
| 378 | 394 | } |
| 379 | 395 | |
| 380 | 396 | /// View-projections of the six 90 degree faces (`+X -X +Y -Y +Z -Z`) around `eye`. |
| 381 | | fn cube_face_view_projections(eye: [f32; 3]) -> [Mat4; 6] { |
| 382 | | let proj = mat4_perspective_wgpu(std::f32::consts::FRAC_PI_2, 1.0, 0.01, 1000.0); |
| 397 | fn cube_face_view_projections(eye: [f32; 3], far: f32) -> [Mat4; 6] { |
| 398 | let proj = mat4_perspective_wgpu(std::f32::consts::FRAC_PI_2, 1.0, 0.01, far); |
| 383 | 399 | [ |
| 384 | 400 | ([1.0, 0.0, 0.0], [0.0, 1.0, 0.0]), |
| 385 | 401 | ([-1.0, 0.0, 0.0], [0.0, 1.0, 0.0]), |
| ... | ... | @@ -407,7 +423,7 @@ impl Scene3DRenderer { |
| 407 | 423 | let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { |
| 408 | 424 | label: Some("scene3d-shader"), |
| 409 | 425 | source: wgpu::ShaderSource::Wgsl( |
| 410 | | SHADER_3D |
| 426 | format!("{UV_XFORM_WGSL}{SHADER_3D}") |
| 411 | 427 | .replace("MAX_SHADOW_LIGHTS", &MAX_SHADOW_LIGHTS.to_string()) |
| 412 | 428 | .replace("MAX_DIR_LIGHTS", &MAX_DIR_LIGHTS.to_string()) |
| 413 | 429 | .replace("MAX_LIGHTS", &MAX_LIGHTS.to_string()) |
| ... | ... | @@ -531,24 +547,47 @@ impl Scene3DRenderer { |
| 531 | 547 | }); |
| 532 | 548 | let shadow_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { |
| 533 | 549 | label: Some("scene3d-shadow-shader"), |
| 534 | | source: wgpu::ShaderSource::Wgsl(SHADOW_SHADER.into()), |
| 550 | source: wgpu::ShaderSource::Wgsl(format!("{UV_XFORM_WGSL}{SHADOW_SHADER}").into()), |
| 535 | 551 | }); |
| 536 | | let shadow_uniform_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { |
| 537 | | label: Some("scene3d-shadow-bgl"), |
| 538 | | entries: &[wgpu::BindGroupLayoutEntry { |
| 539 | | binding: 0, |
| 540 | | visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, |
| 541 | | ty: wgpu::BindingType::Buffer { |
| 542 | | ty: wgpu::BufferBindingType::Uniform, |
| 543 | | has_dynamic_offset: false, |
| 544 | | min_binding_size: None, |
| 552 | let dynamic_uniform = |binding| wgpu::BindGroupLayoutEntry { |
| 553 | binding, |
| 554 | visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, |
| 555 | ty: wgpu::BindingType::Buffer { |
| 556 | ty: wgpu::BufferBindingType::Uniform, |
| 557 | has_dynamic_offset: true, |
| 558 | min_binding_size: None, |
| 559 | }, |
| 560 | count: None, |
| 561 | }; |
| 562 | let shadow_caster_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { |
| 563 | label: Some("scene3d-shadow-caster-bgl"), |
| 564 | entries: &[ |
| 565 | dynamic_uniform(0), |
| 566 | wgpu::BindGroupLayoutEntry { |
| 567 | binding: 1, |
| 568 | visibility: wgpu::ShaderStages::FRAGMENT, |
| 569 | ty: wgpu::BindingType::Texture { |
| 570 | sample_type: wgpu::TextureSampleType::Float { filterable: true }, |
| 571 | view_dimension: wgpu::TextureViewDimension::D2, |
| 572 | multisampled: false, |
| 573 | }, |
| 574 | count: None, |
| 575 | }, |
| 576 | wgpu::BindGroupLayoutEntry { |
| 577 | binding: 2, |
| 578 | visibility: wgpu::ShaderStages::FRAGMENT, |
| 579 | ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), |
| 580 | count: None, |
| 545 | 581 | }, |
| 546 | | count: None, |
| 547 | | }], |
| 582 | ], |
| 583 | }); |
| 584 | let shadow_face_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { |
| 585 | label: Some("scene3d-shadow-face-bgl"), |
| 586 | entries: &[dynamic_uniform(0)], |
| 548 | 587 | }); |
| 549 | 588 | let shadow_pl = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { |
| 550 | 589 | label: Some("scene3d-shadow-pl"), |
| 551 | | bind_group_layouts: &[Some(&shadow_uniform_bgl), Some(&shadow_uniform_bgl)], |
| 590 | bind_group_layouts: &[Some(&shadow_caster_bgl), Some(&shadow_face_bgl)], |
| 552 | 591 | immediate_size: 0, |
| 553 | 592 | }); |
| 554 | 593 | let shadow_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { |
| ... | ... | @@ -603,8 +642,10 @@ impl Scene3DRenderer { |
| 603 | 642 | sampler, |
| 604 | 643 | meshes: Mutex::new(Lru::new(MESH_CACHE_CAP)), |
| 605 | 644 | shadow_pipeline, |
| 606 | | shadow_uniform_bgl, |
| 645 | shadow_caster_bgl, |
| 646 | shadow_face_bgl, |
| 607 | 647 | shadow_dummy, |
| 648 | shadow_targets: Mutex::new(None), |
| 608 | 649 | } |
| 609 | 650 | } |
| 610 | 651 | |
| ... | ... | @@ -619,6 +660,7 @@ impl Scene3DRenderer { |
| 619 | 660 | view_proj: Mat4, |
| 620 | 661 | view: Mat4, |
| 621 | 662 | cam_pos: [f32; 3], |
| 663 | cam_far: f32, |
| 622 | 664 | lights: SceneLights, |
| 623 | 665 | lighting_enabled: bool, |
| 624 | 666 | shadows_enabled: bool, |
| ... | ... | @@ -653,9 +695,12 @@ impl Scene3DRenderer { |
| 653 | 695 | face_vp: [mat4_identity(); 6 * MAX_SHADOW_LIGHTS], |
| 654 | 696 | point: [[-1.0, 0.0, 0.0, 0.0]; MAX_LIGHTS], |
| 655 | 697 | }; |
| 698 | // Receivers visible to the camera are at most `cam_far` from it, so that bounds the light's reach. |
| 699 | let distance_to_cam = |p: [f32; 3]| (0..3).map(|i| (p[i] - cam_pos[i]).powi(2)).sum::<f32>().sqrt(); |
| 656 | 700 | for (slot, &li) in shadow_lights.iter().enumerate() { |
| 657 | 701 | let pl = &lights.points[li]; |
| 658 | | shadow_uni.face_vp[slot * 6..slot * 6 + 6].copy_from_slice(&cube_face_view_projections(pl.pos)); |
| 702 | let far = cam_far + distance_to_cam(pl.pos); |
| 703 | shadow_uni.face_vp[slot * 6..slot * 6 + 6].copy_from_slice(&cube_face_view_projections(pl.pos, far)); |
| 659 | 704 | shadow_uni.point[li] = [slot as f32, pl.shadow_density.unwrap_or(0.0), 0.0, 0.0]; |
| 660 | 705 | } |
| 661 | 706 | let shadow_ubuf = device.create_buffer(&wgpu::BufferDescriptor { |
| ... | ... | @@ -665,42 +710,65 @@ impl Scene3DRenderer { |
| 665 | 710 | mapped_at_creation: false, |
| 666 | 711 | }); |
| 667 | 712 | queue.write_buffer(&shadow_ubuf, 0, bytemuck::bytes_of(&shadow_uni)); |
| 668 | | let shadow_map = (!shadow_lights.is_empty()).then(|| { |
| 669 | | device.create_texture(&wgpu::TextureDescriptor { |
| 670 | | label: Some("scene3d-shadow-maps"), |
| 671 | | size: wgpu::Extent3d { |
| 672 | | width: SHADOW_MAP_SIZE, |
| 673 | | height: SHADOW_MAP_SIZE, |
| 674 | | depth_or_array_layers: 6 * shadow_lights.len() as u32, |
| 675 | | }, |
| 676 | | mip_level_count: 1, |
| 677 | | sample_count: 1, |
| 678 | | dimension: wgpu::TextureDimension::D2, |
| 679 | | format: wgpu::TextureFormat::R32Float, |
| 680 | | usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, |
| 681 | | view_formats: &[], |
| 682 | | }) |
| 713 | let shadow_targets = (!shadow_lights.is_empty()).then(|| { |
| 714 | let layers = 6 * shadow_lights.len() as u32; |
| 715 | let mut cache = self.shadow_targets.lock().unwrap(); |
| 716 | match cache.as_ref() { |
| 717 | Some(t) if t.layers >= layers => t.clone(), |
| 718 | _ => { |
| 719 | let target = |label, format, usage, depth_or_array_layers| { |
| 720 | device.create_texture(&wgpu::TextureDescriptor { |
| 721 | label: Some(label), |
| 722 | size: wgpu::Extent3d { width: SHADOW_MAP_SIZE, height: SHADOW_MAP_SIZE, depth_or_array_layers }, |
| 723 | mip_level_count: 1, |
| 724 | sample_count: 1, |
| 725 | dimension: wgpu::TextureDimension::D2, |
| 726 | format, |
| 727 | usage, |
| 728 | view_formats: &[], |
| 729 | }) |
| 730 | }; |
| 731 | let fresh = ShadowTargets { |
| 732 | maps: target( |
| 733 | "scene3d-shadow-maps", |
| 734 | wgpu::TextureFormat::R32Float, |
| 735 | wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, |
| 736 | layers, |
| 737 | ), |
| 738 | layers, |
| 739 | depth: target( |
| 740 | "scene3d-shadow-depth", |
| 741 | wgpu::TextureFormat::Depth32Float, |
| 742 | wgpu::TextureUsages::RENDER_ATTACHMENT, |
| 743 | 1, |
| 744 | ) |
| 745 | .create_view(&wgpu::TextureViewDescriptor::default()), |
| 746 | }; |
| 747 | *cache = Some(fresh.clone()); |
| 748 | fresh |
| 749 | } |
| 750 | } |
| 683 | 751 | }); |
| 684 | | let shadow_view = shadow_map.as_ref().map(|t| { |
| 685 | | t.create_view(&wgpu::TextureViewDescriptor { |
| 752 | let shadow_view = shadow_targets.as_ref().map(|t| { |
| 753 | t.maps.create_view(&wgpu::TextureViewDescriptor { |
| 686 | 754 | dimension: Some(wgpu::TextureViewDimension::D2Array), |
| 687 | 755 | ..Default::default() |
| 688 | 756 | }) |
| 689 | 757 | }); |
| 690 | | let uniform_bind_group = |label: &str, bytes: &[u8]| { |
| 691 | | let buf = device.create_buffer(&wgpu::BufferDescriptor { |
| 692 | | label: Some(label), |
| 693 | | size: bytes.len() as u64, |
| 758 | // One buffer holds every caster and face uniform of this render at dynamic offsets: casters |
| 759 | // first (at most one per mesh), then six faces per light. |
| 760 | let uniform_stride = (CASTER_BYTES as u32).next_multiple_of(device.limits().min_uniform_buffer_offset_alignment) as usize; |
| 761 | let face_base = meshes.len() * uniform_stride; |
| 762 | let mut shadow_uniform_bytes = vec![0u8; if shadow_lights.is_empty() { 0 } else { face_base + shadow_lights.len() * 6 * uniform_stride }]; |
| 763 | let shadow_dyn_buf = (!shadow_lights.is_empty()).then(|| { |
| 764 | device.create_buffer(&wgpu::BufferDescriptor { |
| 765 | label: Some("scene3d-shadow-dyn"), |
| 766 | size: shadow_uniform_bytes.len() as u64, |
| 694 | 767 | usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, |
| 695 | 768 | mapped_at_creation: false, |
| 696 | | }); |
| 697 | | queue.write_buffer(&buf, 0, bytes); |
| 698 | | device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 699 | | label: Some(label), |
| 700 | | layout: &self.shadow_uniform_bgl, |
| 701 | | entries: &[wgpu::BindGroupEntry { binding: 0, resource: buf.as_entire_binding() }], |
| 702 | 769 | }) |
| 703 | | }; |
| 770 | }); |
| 771 | let mut n_casters = 0; |
| 704 | 772 | |
| 705 | 773 | // Sort back-to-front in camera view space so semi-transparent planes blend cleanly. |
| 706 | 774 | let mut order: Vec<(usize, f32)> = meshes |
| ... | ... | @@ -721,8 +789,8 @@ impl Scene3DRenderer { |
| 721 | 789 | /// Skip the `[back-facing, front-facing]` pass. |
| 722 | 790 | cull: [bool; 2], |
| 723 | 791 | bg: wgpu::BindGroup, |
| 724 | | /// `model` for the shadow pass; `None` for meshes that cast no shadow. |
| 725 | | caster: Option<wgpu::BindGroup>, |
| 792 | /// Shadow-pass bind group and dynamic uniform offset; `None` for meshes that cast no shadow. |
| 793 | caster: Option<(wgpu::BindGroup, u32)>, |
| 726 | 794 | vbuf: wgpu::Buffer, |
| 727 | 795 | ibuf: wgpu::Buffer, |
| 728 | 796 | idx_count: u32, |
| ... | ... | @@ -862,42 +930,71 @@ impl Scene3DRenderer { |
| 862 | 930 | }, |
| 863 | 931 | ], |
| 864 | 932 | }); |
| 933 | // A fully transparent mesh casts no shadow; textured ones are alpha-tested in the pass. |
| 934 | let caster = shadow_dyn_buf.as_ref().filter(|_| !fx.wireframe && m.diffuse_col[3] > 1e-3).map(|buf| { |
| 935 | let mut data = [0.0_f32; CASTER_BYTES / 4]; |
| 936 | data[..16].copy_from_slice(&m.world); |
| 937 | data[16..20].copy_from_slice(&fx.uv.m); |
| 938 | data[20..24].copy_from_slice(&uni.uv_off_wrap); |
| 939 | data[24..28].copy_from_slice(&dod); |
| 940 | data[28..30].copy_from_slice(&[if m.tex.is_some() && !fx.bump_only { 1.0 } else { 0.0 }, m.diffuse_col[3]]); |
| 941 | let offset = n_casters * uniform_stride; |
| 942 | shadow_uniform_bytes[offset..offset + CASTER_BYTES].copy_from_slice(bytemuck::cast_slice(&data)); |
| 943 | n_casters += 1; |
| 944 | let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 945 | label: Some("scene3d-shadow-caster"), |
| 946 | layout: &self.shadow_caster_bgl, |
| 947 | entries: &[ |
| 948 | wgpu::BindGroupEntry { |
| 949 | binding: 0, |
| 950 | resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { |
| 951 | buffer: buf, |
| 952 | offset: 0, |
| 953 | size: std::num::NonZeroU64::new(CASTER_BYTES as u64), |
| 954 | }), |
| 955 | }, |
| 956 | wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(tex_view) }, |
| 957 | wgpu::BindGroupEntry { binding: 2, resource: wgpu::BindingResource::Sampler(&self.sampler) }, |
| 958 | ], |
| 959 | }); |
| 960 | (bind_group, offset as u32) |
| 961 | }); |
| 865 | 962 | draw_items.push(GpuDraw { |
| 866 | 963 | wireframe: fx.wireframe, |
| 867 | 964 | cull: [fx.cull_back, fx.cull_front], |
| 868 | 965 | bg, |
| 869 | | caster: (!fx.wireframe && !shadow_lights.is_empty()) |
| 870 | | .then(|| uniform_bind_group("scene3d-caster", bytemuck::cast_slice(&m.world))), |
| 966 | caster, |
| 871 | 967 | vbuf, |
| 872 | 968 | ibuf, |
| 873 | 969 | idx_count, |
| 874 | 970 | }); |
| 875 | 971 | } |
| 876 | 972 | |
| 877 | | if let Some(map) = &shadow_map { |
| 878 | | let shadow_depth_view = device |
| 879 | | .create_texture(&wgpu::TextureDescriptor { |
| 880 | | label: Some("scene3d-shadow-depth"), |
| 881 | | size: wgpu::Extent3d { width: SHADOW_MAP_SIZE, height: SHADOW_MAP_SIZE, depth_or_array_layers: 1 }, |
| 882 | | mip_level_count: 1, |
| 883 | | sample_count: 1, |
| 884 | | dimension: wgpu::TextureDimension::D2, |
| 885 | | format: wgpu::TextureFormat::Depth32Float, |
| 886 | | usage: wgpu::TextureUsages::RENDER_ATTACHMENT, |
| 887 | | view_formats: &[], |
| 888 | | }) |
| 889 | | .create_view(&wgpu::TextureViewDescriptor::default()); |
| 973 | if let (Some(targets), Some(buf)) = (&shadow_targets, &shadow_dyn_buf) { |
| 974 | let face_bg = device.create_bind_group(&wgpu::BindGroupDescriptor { |
| 975 | label: Some("scene3d-shadow-face"), |
| 976 | layout: &self.shadow_face_bgl, |
| 977 | entries: &[wgpu::BindGroupEntry { |
| 978 | binding: 0, |
| 979 | resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { |
| 980 | buffer: buf, |
| 981 | offset: 0, |
| 982 | size: std::num::NonZeroU64::new(FACE_BYTES as u64), |
| 983 | }), |
| 984 | }], |
| 985 | }); |
| 890 | 986 | for (slot, &li) in shadow_lights.iter().enumerate() { |
| 891 | 987 | let pos = lights.points[li].pos; |
| 892 | 988 | for face in 0..6 { |
| 893 | | let layer = (slot * 6 + face) as u32; |
| 894 | | let mut uniform = [0.0_f32; 20]; |
| 895 | | uniform[..16].copy_from_slice(&shadow_uni.face_vp[layer as usize]); |
| 989 | let layer = slot * 6 + face; |
| 990 | let mut uniform = [0.0_f32; FACE_BYTES / 4]; |
| 991 | uniform[..16].copy_from_slice(&shadow_uni.face_vp[layer]); |
| 896 | 992 | uniform[16..19].copy_from_slice(&pos); |
| 897 | | let face_bg = uniform_bind_group("scene3d-shadow-face", bytemuck::cast_slice(&uniform)); |
| 898 | | let layer_view = map.create_view(&wgpu::TextureViewDescriptor { |
| 993 | let face_offset = face_base + layer * uniform_stride; |
| 994 | shadow_uniform_bytes[face_offset..face_offset + FACE_BYTES].copy_from_slice(bytemuck::cast_slice(&uniform)); |
| 995 | let layer_view = targets.maps.create_view(&wgpu::TextureViewDescriptor { |
| 899 | 996 | dimension: Some(wgpu::TextureViewDimension::D2), |
| 900 | | base_array_layer: layer, |
| 997 | base_array_layer: layer as u32, |
| 901 | 998 | array_layer_count: Some(1), |
| 902 | 999 | ..Default::default() |
| 903 | 1000 | }); |
| ... | ... | @@ -913,7 +1010,7 @@ impl Scene3DRenderer { |
| 913 | 1010 | }, |
| 914 | 1011 | })], |
| 915 | 1012 | depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { |
| 916 | | view: &shadow_depth_view, |
| 1013 | view: &targets.depth, |
| 917 | 1014 | depth_ops: Some(wgpu::Operations { |
| 918 | 1015 | load: wgpu::LoadOp::Clear(1.0), |
| 919 | 1016 | store: wgpu::StoreOp::Discard, |
| ... | ... | @@ -925,16 +1022,17 @@ impl Scene3DRenderer { |
| 925 | 1022 | multiview_mask: None, |
| 926 | 1023 | }); |
| 927 | 1024 | pass.set_pipeline(&self.shadow_pipeline); |
| 928 | | pass.set_bind_group(1, &face_bg, &[]); |
| 1025 | pass.set_bind_group(1, &face_bg, &[face_offset as u32]); |
| 929 | 1026 | for d in &draw_items { |
| 930 | | let Some(caster) = &d.caster else { continue }; |
| 931 | | pass.set_bind_group(0, caster, &[]); |
| 1027 | let Some((caster, caster_offset)) = &d.caster else { continue }; |
| 1028 | pass.set_bind_group(0, caster, &[*caster_offset]); |
| 932 | 1029 | pass.set_vertex_buffer(0, d.vbuf.slice(..)); |
| 933 | 1030 | pass.set_index_buffer(d.ibuf.slice(..), wgpu::IndexFormat::Uint32); |
| 934 | 1031 | pass.draw_indexed(0..d.idx_count, 0, 0..1); |
| 935 | 1032 | } |
| 936 | 1033 | } |
| 937 | 1034 | } |
| 1035 | queue.write_buffer(buf, 0, &shadow_uniform_bytes); |
| 938 | 1036 | } |
| 939 | 1037 | |
| 940 | 1038 | let mut pass = enc.begin_render_pass(&wgpu::RenderPassDescriptor { |
| ... | ... | @@ -1129,7 +1227,7 @@ mod tests { |
| 1129 | 1227 | let frame = |meshes: &[DrawMesh]| { |
| 1130 | 1228 | let mut enc = device.create_command_encoder(&Default::default()); |
| 1131 | 1229 | let (vp, cam) = (mat4_identity(), [0.0, 0.0, 5.0]); |
| 1132 | | renderer.render_meshes(&device, &queue, &mut enc, &dst, &dummy, vp, vp, cam, SceneLights::default(), false, false, meshes); |
| 1230 | renderer.render_meshes(&device, &queue, &mut enc, &dst, &dummy, vp, vp, cam, 1000.0, SceneLights::default(), false, false, meshes); |
| 1133 | 1231 | queue.submit(Some(enc.finish())); |
| 1134 | 1232 | renderer.meshes.lock().unwrap().entries.len() |
| 1135 | 1233 | }; |
| ... | ... | @@ -1193,4 +1291,33 @@ mod tests { |
| 1193 | 1291 | let dark_pixels = |buf: &[u8]| (0..64 * 64).filter(|&i| buf[i * 4 + 3] > 200 && buf[i * 4] < 40).count(); |
| 1194 | 1292 | assert!(dark_pixels(&with_shad) > dark_pixels(&no_shad) + 15, "shadows should darken receiver: with={} vs without={}", dark_pixels(&with_shad), dark_pixels(&no_shad)); |
| 1195 | 1293 | } |
| 1294 | |
| 1295 | #[test] |
| 1296 | fn transparent_textured_casters_leave_no_shadow() { |
| 1297 | let dark_receiver_pixels = |card_alpha: u8, shadows: u8| -> Option<usize> { |
| 1298 | let src = format!( |
| 1299 | r#"Composition {{ CurrentTime = 0, RenderRange = {{ 0, 1 }}, GlobalRange = {{ 0, 1 }}, Tools = ordered() {{ |
| 1300 | Flat = MtlBlinn {{ Inputs = {{ ["Specular.Intensity"] = Input {{ Value = 0, }}, }}, }}, |
| 1301 | Bg = Background {{ Inputs = {{ Width = Input {{ Value = 8, }}, Height = Input {{ Value = 8, }}, TopLeftRed = Input {{ Value = 1, }}, TopLeftAlpha = Input {{ Value = {card_alpha}, }}, }}, }}, |
| 1302 | Tex = Texture2DOperator {{ Inputs = {{ Input = Input {{ SourceOp = "Bg", Source = "Output", }}, }}, }}, |
| 1303 | Recv = Shape3D {{ Inputs = {{ ["SurfacePlaneInputs.Width"] = Input {{ Value = 8, }}, ["Transform3DOp.Translate.Z"] = Input {{ Value = -2, }}, MaterialInput = Input {{ SourceOp = "Flat", Source = "MaterialOutput", }}, }}, }}, |
| 1304 | Card = ImagePlane3D {{ Inputs = {{ ["Transform3DOp.Translate.X"] = Input {{ Value = 0.4, }}, MaterialInput = Input {{ SourceOp = "Tex", Source = "MaterialOutput", }}, }}, }}, |
| 1305 | Pt = LightPoint {{ Inputs = {{ ["Transform3DOp.Translate.X"] = Input {{ Value = 1.6, }}, ["Transform3DOp.Translate.Z"] = Input {{ Value = 2.0, }}, ["ShadowLightInputs3D.ShadowsEnabled"] = Input {{ Value = 1, }}, ["ShadowLightInputs3D.ShadowDensity"] = Input {{ Value = 1.0, }}, }}, }}, |
| 1306 | Cam = Camera3D {{ Inputs = {{ ["Transform3DOp.Translate.Z"] = Input {{ Value = 4.5, }}, ApertureW = Input {{ Value = 1.0, }}, ApertureH = Input {{ Value = 1.0, }}, }}, }}, |
| 1307 | M = Merge3D {{ Inputs = {{ SceneInput1 = Input {{ SourceOp = "Recv", Source = "Output", }}, SceneInput2 = Input {{ SourceOp = "Card", Source = "Output", }}, SceneInput3 = Input {{ SourceOp = "Pt", Source = "Output", }}, SceneInput4 = Input {{ SourceOp = "Cam", Source = "Output", }}, }}, }}, |
| 1308 | R = Renderer3D {{ Inputs = {{ Width = Input {{ Value = 64, }}, Height = Input {{ Value = 64, }}, RendererType = Input {{ Value = FuID {{ "RendererOpenGL" }}, }}, ["RendererOpenGL.LightingEnabled"] = Input {{ Value = 1, }}, ["RendererOpenGL.ShadowsEnabled"] = Input {{ Value = {shadows}, }}, SceneInput = Input {{ SourceOp = "M", Source = "Output", }}, }}, }}, |
| 1309 | }} }}"# |
| 1310 | ); |
| 1311 | let comp = Comp::from_fusion_str(&src).expect("parse shadow comp"); |
| 1312 | let mut eng = Engine::new_headless().ok()?; |
| 1313 | let (tex, _) = eng.evaluate(&comp, comp.find_node("R").expect("R"), 0.0); |
| 1314 | let buf = eng.readback_rgba(&tex).ok()?; |
| 1315 | Some((0..64 * 64).filter(|&i| buf[i * 4 + 3] > 200 && buf[i * 4] < 40).count()) |
| 1316 | }; |
| 1317 | let (Some(opaque), Some(clear), Some(off)) = (dark_receiver_pixels(255, 1), dark_receiver_pixels(0, 1), dark_receiver_pixels(0, 0)) else { |
| 1318 | return; |
| 1319 | }; |
| 1320 | assert!(opaque > off + 15, "opaque card must shadow: {opaque} vs {off}"); |
| 1321 | assert_eq!(clear, off, "a fully transparent card casts no shadow"); |
| 1322 | } |
| 1196 | 1323 | } |