Skip to content

Commit a9f0774

Browse files
tychedeliacatilac
andauthored
Reliably clear to default clear color (#137)
Co-authored-by: Moon Davé <moon@softmoon.world>
1 parent 1f69b66 commit a9f0774

2 files changed

Lines changed: 29 additions & 13 deletions

File tree

crates/processing_render/src/graphics.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ use crate::{
3636
};
3737
use processing_core::error::{ProcessingError, Result};
3838

39+
pub const DEFAULT_CLEAR_COLOR: Color = Color::srgba_u8(208, 208, 208, 255);
40+
3941
pub struct GraphicsPlugin;
4042

4143
impl Plugin for GraphicsPlugin {
@@ -445,16 +447,6 @@ pub fn begin_draw(In(entity): In<Entity>, mut state_query: Query<&mut RenderStat
445447
}
446448

447449
pub fn flush(app: &mut App, entity: Entity) -> Result<()> {
448-
// f there's nothing to render, skip the whole render pass. this avoids some issues on
449-
// macos with msaa resolve where nothing is rendered
450-
let is_empty = graphics_mut!(app, entity)
451-
.get::<CommandBuffer>()
452-
.map(|c| c.commands.is_empty())
453-
.unwrap_or(true);
454-
if is_empty {
455-
return Ok(());
456-
}
457-
458450
graphics_mut!(app, entity).insert(Flush);
459451
app.update();
460452
graphics_mut!(app, entity).remove::<Flush>();
@@ -483,6 +475,26 @@ pub fn end_draw(app: &mut App, entity: Entity) -> Result<()> {
483475
present(app, entity)
484476
}
485477

478+
/// Do some work on the GPU to ensure that the render target texture is initialized and can be read
479+
/// from/written to.
480+
///
481+
/// This is necessary on some platforms (notably macOS) to avoid issues with the first few frames of
482+
/// rendering being corrupted or not appearing at all.
483+
///
484+
// TODO: why is metal particularly affected by this? can we remove this?
485+
pub fn warmup(app: &mut App, entity: Entity) -> Result<()> {
486+
for _ in 0..3 {
487+
app.world_mut()
488+
.run_system_cached_with(
489+
record_command,
490+
(entity, DrawCommand::BackgroundColor(DEFAULT_CLEAR_COLOR)),
491+
)
492+
.unwrap()?;
493+
flush(app, entity)?;
494+
}
495+
Ok(())
496+
}
497+
486498
pub fn record_command(
487499
In((graphics_entity, cmd)): In<(Entity, DrawCommand)>,
488500
mut graphics_query: Query<&mut CommandBuffer>,

crates/processing_render/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,17 @@ pub fn graphics_create(
386386
height: u32,
387387
texture_format: TextureFormat,
388388
) -> error::Result<Entity> {
389-
app_mut(|app| {
390-
app.world_mut()
389+
app_mut(|app| -> error::Result<Entity> {
390+
let entity = app
391+
.world_mut()
391392
.run_system_cached_with(
392393
graphics::create,
393394
(width, height, surface_entity, texture_format),
394395
)
395-
.unwrap()
396+
.unwrap()?;
397+
398+
graphics::warmup(app, entity)?;
399+
Ok(entity)
396400
})
397401
}
398402

0 commit comments

Comments
 (0)