Skip to content

Commit

Permalink
Demo fixes 3 (#28)
Browse files Browse the repository at this point in the history
- force current scene max priority
- change default realm to [ipfs](https://sdk-team-cdn.decentraland.org/ipfs/goerli-plaza-main)
- add config options
  - msaa
  - scene max concurrent thread count
  - scene loop max time
  • Loading branch information
robtfm authored Jun 6, 2023
1 parent 20c3d34 commit 0563ddb
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 17 deletions.
16 changes: 14 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,26 @@ This project's goals are to:

# Arguments

`cargo run --release -- [--server serverpath] [--vsync true|false] [--log_fps true|false]`
`cargo run --release -- [--server serverpath] [--vsync true|false] [--log_fps true|false] [--msaa 1|2|4|8]`

`--server https://sdk-test-scenes.decentraland.zone`
- specify the content server, defaults to the sdk test server.

`--vsync [true|false]`
- disable/enable vsync. defaults to off.

`--log_fps [true|false]`
`--msaa [1,2,4,8]`
- set the number of multisamples. higher values make for nicer graphics but takes more gpu power. defaults to 4.

`--threads n`
- set the max simultaneous thread count for scene javascript execution. higher will allow better performance for distant scenes, but requires more cpu power. defaults to 4.
- also accessible via console command `/scene_threads`

`--millis n`
- set the max time per frame which the renderer will wait for scene execution. higher values will increase distant scene performance at the possible cost of frame rate (particularly with lower thread count).
- note: if used together with `--vsync true` the time should be at least equal to 1000/monitor sync rate (i.e. 17 for 60hz, 6 for 144hz) to avoid very jerky behaviour.
- also accessible via console command `/scene_millis`
- defaults to 12, around 80fps

# Testing

Expand Down
78 changes: 71 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,40 @@ macro_rules! dcl_assert {
pub struct GraphicsSettings {
vsync: bool,
log_fps: bool,
msaa: usize,
}

impl Default for GraphicsSettings {
fn default() -> Self {
Self {
vsync: false,
log_fps: true,
msaa: 4,
}
}
}

#[derive(Serialize, Deserialize, Resource)]
pub struct AppConfig {
server: String,
profile: UserProfile,
graphics: GraphicsSettings,
pub server: String,
pub profile: UserProfile,
pub graphics: GraphicsSettings,
pub scene_threads: usize,
pub scene_loop_millis: u64,
}

impl Default for AppConfig {
fn default() -> Self {
Self {
server: "https://sdk-test-scenes.decentraland.zone".to_owned(),
server: "https://sdk-team-cdn.decentraland.org/ipfs/goerli-plaza-main".to_owned(),
profile: UserProfile {
version: 1,
content: Default::default(),
base_url: "https://sdk-test-scenes.decentraland.zone/content/contents/".to_owned(),
base_url: "https://peer.decentraland.zone/content/contents/".to_owned(),
},
graphics: Default::default(),
scene_threads: 4,
scene_loop_millis: 12, // ~80fps
}
}
}
Expand Down Expand Up @@ -125,7 +131,19 @@ fn main() {
.value_from_str("--log_fps")
.ok()
.unwrap_or(base_config.graphics.log_fps),
msaa: args
.value_from_str::<_, usize>("--msaa")
.ok()
.unwrap_or(base_config.graphics.msaa),
},
scene_threads: args
.value_from_str("--threads")
.ok()
.unwrap_or(base_config.scene_threads),
scene_loop_millis: args
.value_from_str("--millis")
.ok()
.unwrap_or(base_config.scene_loop_millis),
};

let remaining = args.finish();
Expand Down Expand Up @@ -153,7 +171,21 @@ fn main() {
false => bevy::window::PresentMode::AutoNoVsync,
};

app.add_plugins(
let msaa = match final_config.graphics.msaa {
1 => Msaa::Off,
2 => Msaa::Sample2,
4 => Msaa::Sample4,
8 => Msaa::Sample8,
_ => {
warnings.push(
"Invalid msaa sample count, must be one of (1, 2, 4, 8). Defaulting to Off"
.to_owned(),
);
Msaa::Off
}
};

app.insert_resource(msaa).add_plugins(
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
Expand Down Expand Up @@ -200,6 +232,8 @@ fn main() {

app.add_console_command::<ChangeLocationCommand, _>(change_location);
app.add_console_command::<SceneDistanceCommand, _>(scene_distance);
app.add_console_command::<SceneThreadsCommand, _>(scene_threads);
app.add_console_command::<SceneMillisCommand, _>(scene_millis);

// replay any warnings
for warning in warnings {
Expand Down Expand Up @@ -313,6 +347,36 @@ fn scene_distance(
if let Some(Ok(command)) = input.take() {
let distance = command.distance.unwrap_or(100.0);
scene_load_distance.0 = distance;
input.reply_failed("set scene load distance to {distance}");
input.reply_ok("set scene load distance to {distance}");
}
}

// set thread count
#[derive(clap::Parser, ConsoleCommand)]
#[command(name = "/scene_threads")]
struct SceneThreadsCommand {
threads: Option<usize>,
}

fn scene_threads(mut input: ConsoleCommand<SceneThreadsCommand>, mut config: ResMut<AppConfig>) {
if let Some(Ok(command)) = input.take() {
let threads = command.threads.unwrap_or(4);
config.scene_threads = threads;
input.reply_ok("scene simultaneous thread count set to {threads}");
}
}

// set loop millis
#[derive(clap::Parser, ConsoleCommand)]
#[command(name = "/scene_millis")]
struct SceneMillisCommand {
millis: Option<u64>,
}

fn scene_millis(mut input: ConsoleCommand<SceneMillisCommand>, mut config: ResMut<AppConfig>) {
if let Some(Ok(command)) = input.take() {
let millis = command.millis.unwrap_or(12);
config.scene_loop_millis = millis;
input.reply_ok("scene loop max ms set to {millis}");
}
}
24 changes: 16 additions & 8 deletions src/scene_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
SceneEntityId,
},
ipfs::SceneIpfsLocation,
AppConfig,
};

use self::{
Expand Down Expand Up @@ -222,7 +223,7 @@ fn run_scene_loop(world: &mut World) {
#[cfg(debug_assertions)]
let millis = 10000;
#[cfg(not(debug_assertions))]
let millis = 12;
let millis = world.resource::<AppConfig>().scene_loop_millis;
let end_time = last_end_time + Duration::from_millis(millis);

// allow at least 1 ms until i sort out the frame timings properly
Expand Down Expand Up @@ -265,15 +266,16 @@ fn run_scene_loop(world: &mut World) {

fn update_scene_priority(
mut scenes: Query<(Entity, &GlobalTransform, &mut RendererSceneContext), Without<SceneLoading>>,
camera: Query<&GlobalTransform, With<PrimaryUser>>,
player: Query<(Entity, &GlobalTransform), With<PrimaryUser>>,
mut updates: ResMut<SceneUpdates>,
time: Res<Time>,
containing_scene: ContainingScene,
) {
updates.eligible_jobs = 0;

let camera_translation = camera
let (player_scene, player_translation) = player
.get_single()
.map(|gt| gt.translation())
.map(|(e, gt)| (containing_scene.get(e), gt.translation()))
.unwrap_or_default();

// check all in-flight scenes still exist
Expand All @@ -287,8 +289,13 @@ fn update_scene_priority(
!context.in_flight && !context.broken && context.blocked.is_empty()
})
.filter_map(|(ent, transform, mut context)| {
let distance = (transform.translation() - camera_translation).length();
context.priority = distance;
// TODO clamp to scene bounds instead of using distance to scene origin
let distance = (transform.translation() - player_translation).length();
context.priority = if Some(ent) == player_scene {
0.0
} else {
distance
};
let not_yet_run = context.last_sent < time.elapsed_seconds();

(!context.in_flight && not_yet_run).then(|| {
Expand All @@ -315,7 +322,7 @@ fn update_scene_priority(
// - reduce bevy async thread pool
// - reduce bevy primary thread pool
// - see if we can get v8 single threaded / no native threads working
const MAX_CONCURRENT_SCENES: usize = 8;
// const MAX_CONCURRENT_SCENES: usize = 8;

// helper to get the scene entity containing a given world position
#[derive(SystemParam)]
Expand Down Expand Up @@ -380,10 +387,11 @@ fn send_scene_updates(
time: Res<Time>,
player: Query<&Transform, With<PrimaryUser>>,
camera: Query<&Transform, With<PrimaryCamera>>,
config: Res<AppConfig>,
) {
let updates = &mut *updates;

if updates.jobs_in_flight.len() == MAX_CONCURRENT_SCENES {
if updates.jobs_in_flight.len() == config.scene_threads {
return;
}

Expand Down

0 comments on commit 0563ddb

Please sign in to comment.