refactor: remove redundant runtime ownership

Amp-Thread-ID: https://ampcode.com/threads/T-019f9d91-77c1-7206-a60f-ed6554ce92ab

Co-authored-by: Heaust Azure <heaust.azure@gmail.com>
This commit is contained in:
Amp
2026-07-29 02:58:55 +00:00
co-authored by heaust
parent 204cb744df
commit 092b319d75
5 changed files with 14 additions and 15 deletions
+2 -8
View File
@@ -1,3 +1,5 @@
#![cfg(target_arch = "wasm32")]
use std::sync::mpsc::{self, Sender};
use wasm_bindgen::closure::Closure;
use wasm_bindgen::prelude::*;
@@ -5,16 +7,11 @@ use wasm_bindgen::JsCast;
use crate::command_ring::CommandRing;
use crate::message::WindowEvent;
#[cfg(target_arch = "wasm32")]
use crate::platform::web;
#[cfg(target_arch = "wasm32")]
use crate::platform::web::worker::MainWorker;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_futures::spawn_local;
#[cfg(target_arch = "wasm32")]
use web_sys::AddEventListenerOptions;
#[cfg(target_arch = "wasm32")]
pub struct EventListeners {
_resize_listener: Closure<dyn FnMut()>,
_pointer_listener: Closure<dyn FnMut(web_sys::PointerEvent)>,
@@ -24,7 +21,6 @@ pub struct EventListeners {
}
/// Setup default window event listeners that forward events to the worker thread
#[cfg(target_arch = "wasm32")]
pub fn setup_event_listeners(
worker_chan: &Sender<WindowEvent>,
canvas: &web_sys::HtmlCanvasElement,
@@ -147,14 +143,12 @@ pub fn setup_event_listeners(
}
/// Runtime resources required to keep a WASM application running.
#[cfg(target_arch = "wasm32")]
pub struct WebAppRuntime {
worker: MainWorker,
_event_listeners: EventListeners,
ring: Box<CommandRing>,
}
#[cfg(target_arch = "wasm32")]
impl WebAppRuntime {
/// Initialize the web worker, canvas ownership, and event listeners.
pub fn new<T: crate::renderer::scene::Scene + 'static>(
+5 -3
View File
@@ -5,11 +5,13 @@ use wasm_bindgen::{prelude::Closure, JsCast, JsValue};
use wasm_bindgen_futures::spawn_local;
use web_sys::DedicatedWorkerGlobalScope;
#[cfg(target_arch = "wasm32")]
use crate::render_data::RenderDataConfig;
use crate::{
command_ring::CommandRing,
gltf::{install_imported, ModelBounds},
message::{camera_drag, CameraDrag, DrainEventError, MouseMessage, ResizeMessage, WindowEvent},
render_data::{InstanceHandle, InstanceType, MeshHandle, RenderData, RenderDataConfig},
render_data::{InstanceHandle, InstanceType, MeshHandle, RenderData},
renderer::scene::Scene,
};
@@ -26,6 +28,7 @@ pub use pipeline_library::PipelineLibrary;
const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
#[cfg(any(target_arch = "wasm32", test))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct DeviceFeaturePlan {
hard: wgpu::Features,
@@ -33,6 +36,7 @@ struct DeviceFeaturePlan {
profiling_enabled: bool,
}
#[cfg(any(target_arch = "wasm32", test))]
fn device_feature_plan(profile: bool, supported: wgpu::Features) -> DeviceFeaturePlan {
let hard = wgpu::Features::INDIRECT_FIRST_INSTANCE;
let profiling = profiler::Profiler::requested_features(profile, supported);
@@ -1290,7 +1294,6 @@ pub struct RendererContext {
}
pub struct Renderer<T: scene::Scene> {
canvas: web_sys::OffscreenCanvas,
events_chan: Receiver<WindowEvent>,
context: RendererContext,
resources: PipelineLibrary,
@@ -2416,7 +2419,6 @@ impl<T: Scene + 'static> Renderer<T> {
Self::ensure_gltf_pipelines(&mut resources, &context);
Self {
canvas,
events_chan,
context,
scene,
+5
View File
@@ -5,8 +5,11 @@ use std::{
use wasm_bindgen::JsValue;
pub const MAX_PROFILE_PASSES: usize = crate::render_graph::MAX_EXECUTIONS;
#[cfg(any(target_arch = "wasm32", test))]
const SLOT_COUNT: usize = 4;
#[cfg(any(target_arch = "wasm32", test))]
const QUERY_COUNT: u32 = (MAX_PROFILE_PASSES * 2) as u32;
#[cfg(any(target_arch = "wasm32", test))]
const RESOLVE_SIZE: u64 = QUERY_COUNT as u64 * 8;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -78,6 +81,7 @@ pub(crate) struct Profiler {
}
impl Profiler {
#[cfg(any(target_arch = "wasm32", test))]
pub fn requested_features(requested: bool, supported: wgpu::Features) -> wgpu::Features {
if requested && supported.contains(wgpu::Features::TIMESTAMP_QUERY) {
wgpu::Features::TIMESTAMP_QUERY
@@ -85,6 +89,7 @@ impl Profiler {
wgpu::Features::empty()
}
}
#[cfg(target_arch = "wasm32")]
pub async fn new(requested: bool, device: &wgpu::Device, queue: &wgpu::Queue) -> Self {
let available = requested && device.features().contains(wgpu::Features::TIMESTAMP_QUERY);
let mut slots = Vec::new();
-1
View File
@@ -24,7 +24,6 @@ const CONTROL_BYTES: u32 = 256;
const SLOT_BYTES: u32 = 64;
const SNAPSHOT_HEADER_BYTES: usize = 64;
const DATA_OFFSET: usize = 448;
const DESCRIPTOR_BYTES: usize = 32;
const STREAMS: usize = 12;
const SCHEMA_FLAGS: u32 = 3; // dense arrays | affine transforms