Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

Shadow maps fit a GPU with little memory of its own (#66): on Vulkan raea reads the largest heap the GPU calls its own (device-local) and, when a scene's <Shadows> gives no size, makes the sun's and spots' maps 2048 a side under 2 GB and 1024 under 1 GB instead of 4096, saying so once. An AMD APU's own memory on Vulkan is the carve-out its firmware leaves it (often 512 MB), and wgpu's allocator never puts a GPU-only texture anywhere else, so a sun's four 4096 cascades, 268 MB of depth, are the likeliest cause of #66's OutOfMemory on a Ryzen 7 4700U (not reproduced: there's no such machine here). On an RTX 4070 the blocks example's first frame holds 571 MB of GPU memory with 4096 maps, 370 MB with 2048 and 319 MB with 1024 (Vulkan, wgpu's allocated total, a build of this change with the heap size forced). The heap read gave 12012 MB on the 4070 and 32606 MB on lavapipe, which keep 4096; DX12 and Metal draw in the system's shared memory and keep 4096 too. A <Shadows mapSize> still wins.

shinyobjectz85f20faaf0c6
diff --git a/Cargo.lock b/Cargo.lock
index 93a28e55..a4cf1d35 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -15264,6 +15264,7 @@ dependencies = [
"raea-io-media",
"raea-sys-layers",
"serde_json",
+ "wgpu",
]
[[package]]
diff --git a/runtime/scene3d/src/camera.rs b/runtime/scene3d/src/camera.rs
index 00524b77..9a68b7a4 100644
--- a/runtime/scene3d/src/camera.rs
+++ b/runtime/scene3d/src/camera.rs
@@ -20,9 +20,10 @@ pub fn camera(world: &mut World, e: Entity, vals: &BTreeMap<String, V>) {
// its order, clear and viewport among the cameras on its target (<Camera3d order clear_color viewport>)
raea_sys_frame::camera::camera_pass(world, e, vals);
// shadow quality (a <Shadows>): the sun's and spots' maps are 4096 texels a side unless it says otherwise (Bevy's
- // 2048 speckles and blurs a film's shadows); point lights' cube maps half that, 1024 by default as Bevy's
+ // 2048 speckles and blurs a film's shadows; smaller on a GPU with little memory of its own, raea_sys_frame::shadow);
+ // point lights' cube maps half that, 1024 by default as Bevy's
if !preview() {
- let size = match vals.get("shadow_map") { Some(V::N(n)) => *n as usize, _ => 4096 };
+ let size = match vals.get("shadow_map") { Some(V::N(n)) => *n as usize, _ => raea_sys_frame::shadow::shadow_map_default(world) };
if let Some(mut m) = world.get_resource_mut::<bevy::light::DirectionalLightShadowMap>() {
m.size = size;
}
diff --git a/systems/frame/Cargo.toml b/systems/frame/Cargo.toml
index 8f985caf..03c5d7ff 100644
--- a/systems/frame/Cargo.toml
+++ b/systems/frame/Cargo.toml
@@ -19,3 +19,7 @@ raea-core-scene = { path = "../../core/scene" }
raea-sys-layers = { path = "../layers" }
serde_json = "1"
raea-io-media = { path = "../../io/media", default-features = false, optional = true }
+
+# the Vulkan device's own memory, for the shadow maps' default size (shadow.rs)
+[target.'cfg(any(windows, target_os = "linux"))'.dependencies]
+wgpu = "=30.0.1"
diff --git a/systems/frame/src/lib.rs b/systems/frame/src/lib.rs
index 10979e39..b208bcf1 100644
--- a/systems/frame/src/lib.rs
+++ b/systems/frame/src/lib.rs
@@ -18,6 +18,8 @@ pub mod gpu;
pub mod layer;
/// the part under a point of the frame (docs/preview.feature PV1a)
pub mod pick;
+/// the shadow maps' size when a scene doesn't give one (smaller where the GPU has little memory of its own)
+pub mod shadow;
/// a free camera over the scene (docs/preview.feature PV1c)
pub mod view;
/// pipelined updates: the render world drawing the last frame while the main world runs this one
diff --git a/systems/frame/src/shadow.rs b/systems/frame/src/shadow.rs
new file mode 100644
index 00000000..9d0661b3
--- /dev/null
+++ b/systems/frame/src/shadow.rs
@@ -0,0 +1,57 @@
+//! The sun's and spots' shadow map size when a <Shadows> doesn't give one: 4096 texels a side, a film's, unless the
+//! GPU has little memory of its own. On Vulkan an AMD APU's own memory is the carve-out its firmware leaves it (often
+//! 512 MB), and wgpu's allocator never puts what the GPU draws into anywhere else, so a sun's four 4096 cascades
+//! (256 MB of depth) ran the first frame out of memory (#66: a Ryzen 7 4700U). Under 2 GB they're 2048, under 1 GB
+//! 1024, said once; a <Shadows mapSize> still wins. DX12 and Metal draw in the system's shared memory and keep 4096.
+
+use std::sync::{Once, OnceLock};
+
+use bevy::prelude::*;
+
+pub fn shadow_map_default(world: &World) -> usize {
+ static OWN: OnceLock<Option<u64>> = OnceLock::new();
+ let Some(own) = *OWN.get_or_init(|| own_memory(world)) else { return 4096 };
+ let size = size_for(own);
+ if size < 4096 {
+ static SAID: Once = Once::new();
+ SAID.call_once(|| eprintln!("raea: the GPU has {} MB of its own memory (Vulkan): shadow maps are {size} texels a side, not 4096 (<Shadows mapSize> sets them)", own >> 20));
+ }
+ size
+}
+
+/// 4096 a side with 2 GB of the GPU's own memory or more, 2048 under that, 1024 under 1 GB
+fn size_for(own: u64) -> usize {
+ match own >> 20 {
+ m if m < 1024 => 1024,
+ m if m < 2048 => 2048,
+ _ => 4096,
+ }
+}
+
+/// The largest heap Vulkan calls the GPU's own (device-local), when Bevy draws on Vulkan
+#[cfg(any(windows, target_os = "linux"))]
+fn own_memory(world: &World) -> Option<u64> {
+ let adapter = world.get_resource::<bevy::render::renderer::RenderAdapter>()?;
+ // SAFETY: reads the physical device's memory properties; nothing is created, kept or destroyed
+ let hal = unsafe { adapter.as_hal::<wgpu::hal::api::Vulkan>() }?;
+ let props = unsafe { hal.shared_instance().raw_instance().get_physical_device_memory_properties(hal.raw_physical_device()) };
+ // DEVICE_LOCAL is bit 0 of a heap's flags
+ props.memory_heaps[..props.memory_heap_count as usize].iter().filter(|h| h.flags.as_raw() & 1 != 0).map(|h| h.size).max()
+}
+
+#[cfg(not(any(windows, target_os = "linux")))]
+fn own_memory(_: &World) -> Option<u64> {
+ None
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn small_gpu_memory_gets_smaller_shadow_maps() {
+ let mb = |m: u64| m << 20;
+ assert_eq!(super::size_for(mb(512)), 1024);
+ assert_eq!(super::size_for(mb(1536)), 2048);
+ assert_eq!(super::size_for(mb(2048)), 4096);
+ assert_eq!(super::size_for(mb(12282)), 4096);
+ }
+}