This commit is contained in:
1 parent
f29712d158
commit
3e053a81ab
123 files changed
+31930
-1404
No files matched your search
+1172
File diff suppressed because it is too large.
Load diff
+272
@@ -0,0 +1,272 @@
|
||||
#define_import_path bevy_render::view
|
||||
|
||||
struct ColorGrading {
|
||||
balance: mat3x3<f32>,
|
||||
saturation: vec3<f32>,
|
||||
contrast: vec3<f32>,
|
||||
gamma: vec3<f32>,
|
||||
gain: vec3<f32>,
|
||||
lift: vec3<f32>,
|
||||
midtone_range: vec2<f32>,
|
||||
exposure: f32,
|
||||
hue: f32,
|
||||
post_saturation: f32,
|
||||
}
|
||||
|
||||
struct View {
|
||||
clip_from_world: mat4x4<f32>,
|
||||
unjittered_clip_from_world: mat4x4<f32>,
|
||||
world_from_clip: mat4x4<f32>,
|
||||
world_from_view: mat4x4<f32>,
|
||||
view_from_world: mat4x4<f32>,
|
||||
// Typically a column-major right-handed projection matrix, one of either:
|
||||
//
|
||||
// Perspective (infinite reverse z)
|
||||
// ```
|
||||
// f = 1 / tan(fov_y_radians / 2)
|
||||
//
|
||||
// ⎡ f / aspect 0 0 0 ⎤
|
||||
// ⎢ 0 f 0 0 ⎥
|
||||
// ⎢ 0 0 0 near ⎥
|
||||
// ⎣ 0 0 -1 0 ⎦
|
||||
// ```
|
||||
//
|
||||
// Orthographic
|
||||
// ```
|
||||
// w = right - left
|
||||
// h = top - bottom
|
||||
// d = far - near
|
||||
// cw = -right - left
|
||||
// ch = -top - bottom
|
||||
//
|
||||
// ⎡ 2 / w 0 0 cw / w ⎤
|
||||
// ⎢ 0 2 / h 0 ch / h ⎥
|
||||
// ⎢ 0 0 1 / d far / d ⎥
|
||||
// ⎣ 0 0 0 1 ⎦
|
||||
// ```
|
||||
//
|
||||
// `clip_from_view[3][3] == 1.0` is the standard way to check if a projection is orthographic
|
||||
//
|
||||
// Wgsl matrices are column major, so for example getting the near plane of a perspective projection is `clip_from_view[3][2]`
|
||||
//
|
||||
// Custom projections are also possible however.
|
||||
clip_from_view: mat4x4<f32>,
|
||||
view_from_clip: mat4x4<f32>,
|
||||
world_position: vec3<f32>,
|
||||
exposure: f32,
|
||||
// viewport(x_origin, y_origin, width, height)
|
||||
viewport: vec4<f32>,
|
||||
main_pass_viewport: vec4<f32>,
|
||||
// 6 world-space half spaces (normal: vec3, distance: f32) ordered left, right, top, bottom, near, far.
|
||||
// The normal vectors point towards the interior of the frustum.
|
||||
// A half space contains `p` if `normal.dot(p) + distance > 0.`
|
||||
frustum: array<vec4<f32>, 6>,
|
||||
color_grading: ColorGrading,
|
||||
mip_bias: f32,
|
||||
frame_count: u32,
|
||||
};
|
||||
|
||||
/// World space:
|
||||
/// +y is up
|
||||
|
||||
/// View space:
|
||||
/// -z is forward, +x is right, +y is up
|
||||
/// Forward is from the camera position into the scene.
|
||||
/// (0.0, 0.0, -1.0) is linear distance of 1.0 in front of the camera's view relative to the camera's rotation
|
||||
/// (0.0, 1.0, 0.0) is linear distance of 1.0 above the camera's view relative to the camera's rotation
|
||||
|
||||
/// NDC (normalized device coordinate):
|
||||
/// https://www.w3.org/TR/webgpu/#coordinate-systems
|
||||
/// (-1.0, -1.0) in NDC is located at the bottom-left corner of NDC
|
||||
/// (1.0, 1.0) in NDC is located at the top-right corner of NDC
|
||||
/// Z is depth where:
|
||||
/// 1.0 is near clipping plane
|
||||
/// Perspective projection: 0.0 is inf far away
|
||||
/// Orthographic projection: 0.0 is far clipping plane
|
||||
|
||||
/// Clip space:
|
||||
/// This is NDC before the perspective divide, still in homogenous coordinate space.
|
||||
/// Dividing a clip space point by its w component yields a point in NDC space.
|
||||
|
||||
/// UV space:
|
||||
/// 0.0, 0.0 is the top left
|
||||
/// 1.0, 1.0 is the bottom right
|
||||
|
||||
|
||||
// -----------------
|
||||
// TO WORLD --------
|
||||
// -----------------
|
||||
|
||||
/// Convert a view space position to world space
|
||||
fn position_view_to_world(view_pos: vec3<f32>, world_from_view: mat4x4<f32>) -> vec3<f32> {
|
||||
let world_pos = world_from_view * vec4(view_pos, 1.0);
|
||||
return world_pos.xyz;
|
||||
}
|
||||
|
||||
/// Convert a clip space position to world space
|
||||
fn position_clip_to_world(clip_pos: vec4<f32>, world_from_clip: mat4x4<f32>) -> vec3<f32> {
|
||||
let world_pos = world_from_clip * clip_pos;
|
||||
return world_pos.xyz;
|
||||
}
|
||||
|
||||
/// Convert a ndc space position to world space
|
||||
fn position_ndc_to_world(ndc_pos: vec3<f32>, world_from_clip: mat4x4<f32>) -> vec3<f32> {
|
||||
let world_pos = world_from_clip * vec4(ndc_pos, 1.0);
|
||||
return world_pos.xyz / world_pos.w;
|
||||
}
|
||||
|
||||
/// Convert a view space direction to world space
|
||||
fn direction_view_to_world(view_dir: vec3<f32>, world_from_view: mat4x4<f32>) -> vec3<f32> {
|
||||
let world_dir = world_from_view * vec4(view_dir, 0.0);
|
||||
return world_dir.xyz;
|
||||
}
|
||||
|
||||
/// Convert a clip space direction to world space
|
||||
fn direction_clip_to_world(clip_dir: vec4<f32>, world_from_clip: mat4x4<f32>) -> vec3<f32> {
|
||||
let world_dir = world_from_clip * clip_dir;
|
||||
return world_dir.xyz;
|
||||
}
|
||||
|
||||
// -----------------
|
||||
// TO VIEW ---------
|
||||
// -----------------
|
||||
|
||||
/// Convert a world space position to view space
|
||||
fn position_world_to_view(world_pos: vec3<f32>, view_from_world: mat4x4<f32>) -> vec3<f32> {
|
||||
let view_pos = view_from_world * vec4(world_pos, 1.0);
|
||||
return view_pos.xyz;
|
||||
}
|
||||
|
||||
/// Convert a clip space position to view space
|
||||
fn position_clip_to_view(clip_pos: vec4<f32>, view_from_clip: mat4x4<f32>) -> vec3<f32> {
|
||||
let view_pos = view_from_clip * clip_pos;
|
||||
return view_pos.xyz;
|
||||
}
|
||||
|
||||
/// Convert a ndc space position to view space
|
||||
fn position_ndc_to_view(ndc_pos: vec3<f32>, view_from_clip: mat4x4<f32>) -> vec3<f32> {
|
||||
let view_pos = view_from_clip * vec4(ndc_pos, 1.0);
|
||||
return view_pos.xyz / view_pos.w;
|
||||
}
|
||||
|
||||
/// Convert a world space direction to view space
|
||||
fn direction_world_to_view(world_dir: vec3<f32>, view_from_world: mat4x4<f32>) -> vec3<f32> {
|
||||
let view_dir = view_from_world * vec4(world_dir, 0.0);
|
||||
return view_dir.xyz;
|
||||
}
|
||||
|
||||
/// Convert a clip space direction to view space
|
||||
fn direction_clip_to_view(clip_dir: vec4<f32>, view_from_clip: mat4x4<f32>) -> vec3<f32> {
|
||||
let view_dir = view_from_clip * clip_dir;
|
||||
return view_dir.xyz;
|
||||
}
|
||||
|
||||
// -----------------
|
||||
// TO CLIP ---------
|
||||
// -----------------
|
||||
|
||||
/// Convert a world space position to clip space
|
||||
fn position_world_to_clip(world_pos: vec3<f32>, clip_from_world: mat4x4<f32>) -> vec4<f32> {
|
||||
let clip_pos = clip_from_world * vec4(world_pos, 1.0);
|
||||
return clip_pos;
|
||||
}
|
||||
|
||||
/// Convert a view space position to clip space
|
||||
fn position_view_to_clip(view_pos: vec3<f32>, clip_from_view: mat4x4<f32>) -> vec4<f32> {
|
||||
let clip_pos = clip_from_view * vec4(view_pos, 1.0);
|
||||
return clip_pos;
|
||||
}
|
||||
|
||||
/// Convert a world space direction to clip space
|
||||
fn direction_world_to_clip(world_dir: vec3<f32>, clip_from_world: mat4x4<f32>) -> vec4<f32> {
|
||||
let clip_dir = clip_from_world * vec4(world_dir, 0.0);
|
||||
return clip_dir;
|
||||
}
|
||||
|
||||
/// Convert a view space direction to clip space
|
||||
fn direction_view_to_clip(view_dir: vec3<f32>, clip_from_view: mat4x4<f32>) -> vec4<f32> {
|
||||
let clip_dir = clip_from_view * vec4(view_dir, 0.0);
|
||||
return clip_dir;
|
||||
}
|
||||
|
||||
// -----------------
|
||||
// TO NDC ----------
|
||||
// -----------------
|
||||
|
||||
/// Convert a world space position to ndc space
|
||||
fn position_world_to_ndc(world_pos: vec3<f32>, clip_from_world: mat4x4<f32>) -> vec3<f32> {
|
||||
let ndc_pos = clip_from_world * vec4(world_pos, 1.0);
|
||||
return ndc_pos.xyz / ndc_pos.w;
|
||||
}
|
||||
|
||||
/// Convert a view space position to ndc space
|
||||
fn position_view_to_ndc(view_pos: vec3<f32>, clip_from_view: mat4x4<f32>) -> vec3<f32> {
|
||||
let ndc_pos = clip_from_view * vec4(view_pos, 1.0);
|
||||
return ndc_pos.xyz / ndc_pos.w;
|
||||
}
|
||||
|
||||
// -----------------
|
||||
// DEPTH -----------
|
||||
// -----------------
|
||||
|
||||
/// Retrieve the perspective camera near clipping plane
|
||||
fn perspective_camera_near(clip_from_view: mat4x4<f32>) -> f32 {
|
||||
return clip_from_view[3][2];
|
||||
}
|
||||
|
||||
/// Convert ndc depth to linear view z.
|
||||
/// Note: Depth values in front of the camera will be negative as -z is forward
|
||||
fn depth_ndc_to_view_z(ndc_depth: f32, clip_from_view: mat4x4<f32>, view_from_clip: mat4x4<f32>) -> f32 {
|
||||
#ifdef VIEW_PROJECTION_PERSPECTIVE
|
||||
return -perspective_camera_near(clip_from_view) / ndc_depth;
|
||||
#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC
|
||||
return -(clip_from_view[3][2] - ndc_depth) / clip_from_view[2][2];
|
||||
#else
|
||||
let view_pos = view_from_clip * vec4(0.0, 0.0, ndc_depth, 1.0);
|
||||
return view_pos.z / view_pos.w;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Convert linear view z to ndc depth.
|
||||
/// Note: View z input should be negative for values in front of the camera as -z is forward
|
||||
fn view_z_to_depth_ndc(view_z: f32, clip_from_view: mat4x4<f32>) -> f32 {
|
||||
#ifdef VIEW_PROJECTION_PERSPECTIVE
|
||||
return -perspective_camera_near(clip_from_view) / view_z;
|
||||
#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC
|
||||
return clip_from_view[3][2] + view_z * clip_from_view[2][2];
|
||||
#else
|
||||
let ndc_pos = clip_from_view * vec4(0.0, 0.0, view_z, 1.0);
|
||||
return ndc_pos.z / ndc_pos.w;
|
||||
#endif
|
||||
}
|
||||
|
||||
// -----------------
|
||||
// UV --------------
|
||||
// -----------------
|
||||
|
||||
/// Convert ndc space xy coordinate [-1.0 .. 1.0] to uv [0.0 .. 1.0]
|
||||
fn ndc_to_uv(ndc: vec2<f32>) -> vec2<f32> {
|
||||
return ndc * vec2(0.5, -0.5) + vec2(0.5);
|
||||
}
|
||||
|
||||
/// Convert uv [0.0 .. 1.0] coordinate to ndc space xy [-1.0 .. 1.0]
|
||||
fn uv_to_ndc(uv: vec2<f32>) -> vec2<f32> {
|
||||
return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0);
|
||||
}
|
||||
|
||||
/// returns the (0.0, 0.0) .. (1.0, 1.0) position within the viewport for the current render target
|
||||
/// [0 .. render target viewport size] eg. [(0.0, 0.0) .. (1280.0, 720.0)] to [(0.0, 0.0) .. (1.0, 1.0)]
|
||||
fn frag_coord_to_uv(frag_coord: vec2<f32>, viewport: vec4<f32>) -> vec2<f32> {
|
||||
return (frag_coord - viewport.xy) / viewport.zw;
|
||||
}
|
||||
|
||||
/// Convert frag coord to ndc
|
||||
fn frag_coord_to_ndc(frag_coord: vec4<f32>, viewport: vec4<f32>) -> vec3<f32> {
|
||||
return vec3(uv_to_ndc(frag_coord_to_uv(frag_coord.xy, viewport)), frag_coord.z);
|
||||
}
|
||||
|
||||
/// Convert ndc space xy coordinate [-1.0 .. 1.0] to [0 .. render target
|
||||
/// viewport size]
|
||||
fn ndc_to_frag_coord(ndc: vec2<f32>, viewport: vec4<f32>) -> vec2<f32> {
|
||||
return ndc_to_uv(ndc) * viewport.zw;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
use core::any::TypeId;
|
||||
|
||||
use bevy_ecs::{component::Component, entity::Entity, prelude::ReflectComponent};
|
||||
use bevy_reflect::{prelude::ReflectDefault, Reflect};
|
||||
use bevy_utils::TypeIdMap;
|
||||
|
||||
use crate::sync_world::MainEntity;
|
||||
|
||||
mod range;
|
||||
use bevy_camera::visibility::*;
|
||||
pub use range::*;
|
||||
|
||||
/// Collection of entities visible from the current view.
|
||||
///
|
||||
/// This component is extracted from [`VisibleEntities`].
|
||||
#[derive(Clone, Component, Default, Debug, Reflect)]
|
||||
#[reflect(Component, Default, Debug, Clone)]
|
||||
pub struct RenderVisibleEntities {
|
||||
#[reflect(ignore, clone)]
|
||||
pub entities: TypeIdMap<Vec<(Entity, MainEntity)>>,
|
||||
}
|
||||
|
||||
impl RenderVisibleEntities {
|
||||
pub fn get<QF>(&self) -> &[(Entity, MainEntity)]
|
||||
where
|
||||
QF: 'static,
|
||||
{
|
||||
match self.entities.get(&TypeId::of::<QF>()) {
|
||||
Some(entities) => &entities[..],
|
||||
None => &[],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter<QF>(&self) -> impl DoubleEndedIterator<Item = &(Entity, MainEntity)>
|
||||
where
|
||||
QF: 'static,
|
||||
{
|
||||
self.get::<QF>().iter()
|
||||
}
|
||||
|
||||
pub fn len<QF>(&self) -> usize
|
||||
where
|
||||
QF: 'static,
|
||||
{
|
||||
self.get::<QF>().len()
|
||||
}
|
||||
|
||||
pub fn is_empty<QF>(&self) -> bool
|
||||
where
|
||||
QF: 'static,
|
||||
{
|
||||
self.get::<QF>().is_empty()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
//! Specific distances from the camera in which entities are visible, also known
|
||||
//! as *hierarchical levels of detail* or *HLOD*s.
|
||||
|
||||
use super::VisibilityRange;
|
||||
use bevy_app::{App, Plugin};
|
||||
use bevy_ecs::{
|
||||
entity::Entity,
|
||||
lifecycle::RemovedComponents,
|
||||
query::Changed,
|
||||
resource::Resource,
|
||||
schedule::IntoScheduleConfigs as _,
|
||||
system::{Query, Res, ResMut},
|
||||
};
|
||||
use bevy_math::{vec4, Vec4};
|
||||
use bevy_platform::collections::HashMap;
|
||||
use bevy_utils::prelude::default;
|
||||
use nonmax::NonMaxU16;
|
||||
use wgpu::{BufferBindingType, BufferUsages};
|
||||
|
||||
use crate::{
|
||||
render_resource::BufferVec,
|
||||
renderer::{RenderDevice, RenderQueue},
|
||||
sync_world::{MainEntity, MainEntityHashMap},
|
||||
Extract, ExtractSchedule, Render, RenderApp, RenderSystems,
|
||||
};
|
||||
|
||||
/// We need at least 4 storage buffer bindings available to enable the
|
||||
/// visibility range buffer.
|
||||
///
|
||||
/// Even though we only use one storage buffer, the first 3 available storage
|
||||
/// buffers will go to various light-related buffers. We will grab the fourth
|
||||
/// buffer slot.
|
||||
pub const VISIBILITY_RANGES_STORAGE_BUFFER_COUNT: u32 = 4;
|
||||
|
||||
/// The size of the visibility ranges buffer in elements (not bytes) when fewer
|
||||
/// than 6 storage buffers are available and we're forced to use a uniform
|
||||
/// buffer instead (most notably, on WebGL 2).
|
||||
const VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE: usize = 64;
|
||||
|
||||
/// A plugin that enables [`RenderVisibilityRanges`]s, which allow entities to be
|
||||
/// hidden or shown based on distance to the camera.
|
||||
pub struct RenderVisibilityRangePlugin;
|
||||
|
||||
impl Plugin for RenderVisibilityRangePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
|
||||
return;
|
||||
};
|
||||
|
||||
render_app
|
||||
.init_resource::<RenderVisibilityRanges>()
|
||||
.add_systems(ExtractSchedule, extract_visibility_ranges)
|
||||
.add_systems(
|
||||
Render,
|
||||
write_render_visibility_ranges.in_set(RenderSystems::PrepareResourcesFlush),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Stores information related to [`VisibilityRange`]s in the render world.
|
||||
#[derive(Resource)]
|
||||
pub struct RenderVisibilityRanges {
|
||||
/// Information corresponding to each entity.
|
||||
entities: MainEntityHashMap<RenderVisibilityEntityInfo>,
|
||||
|
||||
/// Maps a [`VisibilityRange`] to its index within the `buffer`.
|
||||
///
|
||||
/// This map allows us to deduplicate identical visibility ranges, which
|
||||
/// saves GPU memory.
|
||||
range_to_index: HashMap<VisibilityRange, NonMaxU16>,
|
||||
|
||||
/// The GPU buffer that stores [`VisibilityRange`]s.
|
||||
///
|
||||
/// Each [`Vec4`] contains the start margin start, start margin end, end
|
||||
/// margin start, and end margin end distances, in that order.
|
||||
buffer: BufferVec<Vec4>,
|
||||
|
||||
/// True if the buffer has been changed since the last frame and needs to be
|
||||
/// reuploaded to the GPU.
|
||||
buffer_dirty: bool,
|
||||
}
|
||||
|
||||
/// Per-entity information related to [`VisibilityRange`]s.
|
||||
struct RenderVisibilityEntityInfo {
|
||||
/// The index of the range within the GPU buffer.
|
||||
buffer_index: NonMaxU16,
|
||||
/// True if the range is abrupt: i.e. has no crossfade.
|
||||
is_abrupt: bool,
|
||||
}
|
||||
|
||||
impl Default for RenderVisibilityRanges {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
entities: default(),
|
||||
range_to_index: default(),
|
||||
buffer: BufferVec::new(
|
||||
BufferUsages::STORAGE | BufferUsages::UNIFORM | BufferUsages::VERTEX,
|
||||
),
|
||||
buffer_dirty: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderVisibilityRanges {
|
||||
/// Clears out the [`RenderVisibilityRanges`] in preparation for a new
|
||||
/// frame.
|
||||
fn clear(&mut self) {
|
||||
self.entities.clear();
|
||||
self.range_to_index.clear();
|
||||
self.buffer.clear();
|
||||
self.buffer_dirty = true;
|
||||
}
|
||||
|
||||
/// Inserts a new entity into the [`RenderVisibilityRanges`].
|
||||
fn insert(&mut self, entity: MainEntity, visibility_range: &VisibilityRange) {
|
||||
// Grab a slot in the GPU buffer, or take the existing one if there
|
||||
// already is one.
|
||||
let buffer_index = *self
|
||||
.range_to_index
|
||||
.entry(visibility_range.clone())
|
||||
.or_insert_with(|| {
|
||||
NonMaxU16::try_from(self.buffer.push(vec4(
|
||||
visibility_range.start_margin.start,
|
||||
visibility_range.start_margin.end,
|
||||
visibility_range.end_margin.start,
|
||||
visibility_range.end_margin.end,
|
||||
)) as u16)
|
||||
.unwrap_or_default()
|
||||
});
|
||||
|
||||
self.entities.insert(
|
||||
entity,
|
||||
RenderVisibilityEntityInfo {
|
||||
buffer_index,
|
||||
is_abrupt: visibility_range.is_abrupt(),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the index in the GPU buffer corresponding to the visible range
|
||||
/// for the given entity.
|
||||
///
|
||||
/// If the entity has no visible range, returns `None`.
|
||||
#[inline]
|
||||
pub fn lod_index_for_entity(&self, entity: MainEntity) -> Option<NonMaxU16> {
|
||||
self.entities.get(&entity).map(|info| info.buffer_index)
|
||||
}
|
||||
|
||||
/// Returns true if the entity has a visibility range and it isn't abrupt:
|
||||
/// i.e. if it has a crossfade.
|
||||
#[inline]
|
||||
pub fn entity_has_crossfading_visibility_ranges(&self, entity: MainEntity) -> bool {
|
||||
self.entities
|
||||
.get(&entity)
|
||||
.is_some_and(|info| !info.is_abrupt)
|
||||
}
|
||||
|
||||
/// Returns a reference to the GPU buffer that stores visibility ranges.
|
||||
#[inline]
|
||||
pub fn buffer(&self) -> &BufferVec<Vec4> {
|
||||
&self.buffer
|
||||
}
|
||||
}
|
||||
|
||||
/// Extracts all [`VisibilityRange`] components from the main world to the
|
||||
/// render world and inserts them into [`RenderVisibilityRanges`].
|
||||
pub fn extract_visibility_ranges(
|
||||
mut render_visibility_ranges: ResMut<RenderVisibilityRanges>,
|
||||
visibility_ranges_query: Extract<Query<(Entity, &VisibilityRange)>>,
|
||||
changed_ranges_query: Extract<Query<Entity, Changed<VisibilityRange>>>,
|
||||
mut removed_visibility_ranges: Extract<RemovedComponents<VisibilityRange>>,
|
||||
) {
|
||||
if changed_ranges_query.is_empty() && removed_visibility_ranges.read().next().is_none() {
|
||||
return;
|
||||
}
|
||||
|
||||
render_visibility_ranges.clear();
|
||||
for (entity, visibility_range) in visibility_ranges_query.iter() {
|
||||
render_visibility_ranges.insert(entity.into(), visibility_range);
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes the [`RenderVisibilityRanges`] table to the GPU.
|
||||
pub fn write_render_visibility_ranges(
|
||||
render_device: Res<RenderDevice>,
|
||||
render_queue: Res<RenderQueue>,
|
||||
mut render_visibility_ranges: ResMut<RenderVisibilityRanges>,
|
||||
) {
|
||||
// If there haven't been any changes, early out.
|
||||
if !render_visibility_ranges.buffer_dirty {
|
||||
return;
|
||||
}
|
||||
|
||||
// Mess with the length of the buffer to meet API requirements if necessary.
|
||||
match render_device.get_supported_read_only_binding_type(VISIBILITY_RANGES_STORAGE_BUFFER_COUNT)
|
||||
{
|
||||
// If we're using a uniform buffer, we must have *exactly*
|
||||
// `VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE` elements.
|
||||
BufferBindingType::Uniform
|
||||
if render_visibility_ranges.buffer.len() > VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE =>
|
||||
{
|
||||
render_visibility_ranges
|
||||
.buffer
|
||||
.truncate(VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE);
|
||||
}
|
||||
BufferBindingType::Uniform
|
||||
if render_visibility_ranges.buffer.len() < VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE =>
|
||||
{
|
||||
while render_visibility_ranges.buffer.len() < VISIBILITY_RANGE_UNIFORM_BUFFER_SIZE {
|
||||
render_visibility_ranges.buffer.push(default());
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, if we're using a storage buffer, just ensure there's
|
||||
// something in the buffer, or else it won't get allocated.
|
||||
BufferBindingType::Storage { .. } if render_visibility_ranges.buffer.is_empty() => {
|
||||
render_visibility_ranges.buffer.push(default());
|
||||
}
|
||||
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Schedule the write.
|
||||
render_visibility_ranges
|
||||
.buffer
|
||||
.write_buffer(&render_device, &render_queue);
|
||||
render_visibility_ranges.buffer_dirty = false;
|
||||
}
|
||||
+490
@@ -0,0 +1,490 @@
|
||||
use crate::renderer::WgpuWrapper;
|
||||
use crate::{
|
||||
render_resource::{SurfaceTexture, TextureView},
|
||||
renderer::{RenderAdapter, RenderDevice, RenderInstance},
|
||||
Extract, ExtractSchedule, Render, RenderApp, RenderSystems,
|
||||
};
|
||||
use bevy_app::{App, Plugin};
|
||||
use bevy_ecs::{entity::EntityHashMap, prelude::*};
|
||||
use bevy_platform::collections::HashSet;
|
||||
use bevy_utils::default;
|
||||
use bevy_window::{
|
||||
CompositeAlphaMode, PresentMode, PrimaryWindow, RawHandleWrapper, Window, WindowClosing,
|
||||
};
|
||||
use core::{
|
||||
num::NonZero,
|
||||
ops::{Deref, DerefMut},
|
||||
};
|
||||
use tracing::{debug, info, warn};
|
||||
use wgpu::{
|
||||
SurfaceConfiguration, SurfaceTargetUnsafe, TextureFormat, TextureUsages, TextureViewDescriptor,
|
||||
};
|
||||
|
||||
pub mod screenshot;
|
||||
|
||||
use screenshot::ScreenshotPlugin;
|
||||
|
||||
pub struct WindowRenderPlugin;
|
||||
|
||||
impl Plugin for WindowRenderPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins(ScreenshotPlugin);
|
||||
|
||||
if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
|
||||
render_app
|
||||
.init_resource::<ExtractedWindows>()
|
||||
.init_resource::<WindowSurfaces>()
|
||||
.add_systems(ExtractSchedule, extract_windows)
|
||||
.add_systems(
|
||||
Render,
|
||||
create_surfaces
|
||||
.run_if(need_surface_configuration)
|
||||
.before(prepare_windows),
|
||||
)
|
||||
.add_systems(Render, prepare_windows.in_set(RenderSystems::ManageViews));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ExtractedWindow {
|
||||
/// An entity that contains the components in [`Window`].
|
||||
pub entity: Entity,
|
||||
pub handle: RawHandleWrapper,
|
||||
pub physical_width: u32,
|
||||
pub physical_height: u32,
|
||||
pub present_mode: PresentMode,
|
||||
pub desired_maximum_frame_latency: Option<NonZero<u32>>,
|
||||
/// Note: this will not always be the swap chain texture view. When taking a screenshot,
|
||||
/// this will point to an alternative texture instead to allow for copying the render result
|
||||
/// to CPU memory.
|
||||
pub swap_chain_texture_view: Option<TextureView>,
|
||||
pub swap_chain_texture: Option<SurfaceTexture>,
|
||||
pub swap_chain_texture_format: Option<TextureFormat>,
|
||||
pub swap_chain_texture_view_format: Option<TextureFormat>,
|
||||
pub size_changed: bool,
|
||||
pub present_mode_changed: bool,
|
||||
pub alpha_mode: CompositeAlphaMode,
|
||||
/// Whether this window needs an initial buffer commit.
|
||||
///
|
||||
/// On Wayland, windows must present at least once before they are shown.
|
||||
/// See <https://wayland.app/protocols/xdg-shell#xdg_surface>
|
||||
pub needs_initial_present: bool,
|
||||
}
|
||||
|
||||
impl ExtractedWindow {
|
||||
fn set_swapchain_texture(&mut self, frame: wgpu::SurfaceTexture) {
|
||||
self.swap_chain_texture_view_format = Some(frame.texture.format().add_srgb_suffix());
|
||||
let texture_view_descriptor = TextureViewDescriptor {
|
||||
format: self.swap_chain_texture_view_format,
|
||||
..default()
|
||||
};
|
||||
self.swap_chain_texture_view = Some(TextureView::from(
|
||||
frame.texture.create_view(&texture_view_descriptor),
|
||||
));
|
||||
self.swap_chain_texture = Some(SurfaceTexture::from(frame));
|
||||
}
|
||||
|
||||
fn has_swapchain_texture(&self) -> bool {
|
||||
self.swap_chain_texture_view.is_some() && self.swap_chain_texture.is_some()
|
||||
}
|
||||
|
||||
pub fn present(&mut self) {
|
||||
if let Some(surface_texture) = self.swap_chain_texture.take() {
|
||||
// TODO(clean): winit docs recommends calling pre_present_notify before this.
|
||||
// though `present()` doesn't present the frame, it schedules it to be presented
|
||||
// by wgpu.
|
||||
// https://docs.rs/winit/0.29.9/wasm32-unknown-unknown/winit/window/struct.Window.html#method.pre_present_notify
|
||||
surface_texture.present();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Resource)]
|
||||
pub struct ExtractedWindows {
|
||||
pub primary: Option<Entity>,
|
||||
pub windows: EntityHashMap<ExtractedWindow>,
|
||||
}
|
||||
|
||||
impl Deref for ExtractedWindows {
|
||||
type Target = EntityHashMap<ExtractedWindow>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.windows
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for ExtractedWindows {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.windows
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_windows(
|
||||
mut extracted_windows: ResMut<ExtractedWindows>,
|
||||
mut closing: Extract<MessageReader<WindowClosing>>,
|
||||
windows: Extract<Query<(Entity, &Window, &RawHandleWrapper, Option<&PrimaryWindow>)>>,
|
||||
mut removed: Extract<RemovedComponents<RawHandleWrapper>>,
|
||||
mut window_surfaces: ResMut<WindowSurfaces>,
|
||||
) {
|
||||
for (entity, window, handle, primary) in windows.iter() {
|
||||
if primary.is_some() {
|
||||
extracted_windows.primary = Some(entity);
|
||||
}
|
||||
|
||||
let (new_width, new_height) = (
|
||||
window.resolution.physical_width().max(1),
|
||||
window.resolution.physical_height().max(1),
|
||||
);
|
||||
|
||||
let extracted_window = extracted_windows.entry(entity).or_insert(ExtractedWindow {
|
||||
entity,
|
||||
handle: handle.clone(),
|
||||
physical_width: new_width,
|
||||
physical_height: new_height,
|
||||
present_mode: window.present_mode,
|
||||
desired_maximum_frame_latency: window.desired_maximum_frame_latency,
|
||||
swap_chain_texture: None,
|
||||
swap_chain_texture_view: None,
|
||||
size_changed: false,
|
||||
swap_chain_texture_format: None,
|
||||
swap_chain_texture_view_format: None,
|
||||
present_mode_changed: false,
|
||||
alpha_mode: window.composite_alpha_mode,
|
||||
needs_initial_present: true,
|
||||
});
|
||||
|
||||
if extracted_window.swap_chain_texture.is_none() {
|
||||
// If we called present on the previous swap-chain texture last update,
|
||||
// then drop the swap chain frame here, otherwise we can keep it for the
|
||||
// next update as an optimization. `prepare_windows` will only acquire a new
|
||||
// swap chain texture if needed.
|
||||
extracted_window.swap_chain_texture_view = None;
|
||||
}
|
||||
extracted_window.size_changed = new_width != extracted_window.physical_width
|
||||
|| new_height != extracted_window.physical_height;
|
||||
extracted_window.present_mode_changed =
|
||||
window.present_mode != extracted_window.present_mode;
|
||||
|
||||
if extracted_window.size_changed {
|
||||
debug!(
|
||||
"Window size changed from {}x{} to {}x{}",
|
||||
extracted_window.physical_width,
|
||||
extracted_window.physical_height,
|
||||
new_width,
|
||||
new_height
|
||||
);
|
||||
extracted_window.physical_width = new_width;
|
||||
extracted_window.physical_height = new_height;
|
||||
}
|
||||
|
||||
if extracted_window.present_mode_changed {
|
||||
debug!(
|
||||
"Window Present Mode changed from {:?} to {:?}",
|
||||
extracted_window.present_mode, window.present_mode
|
||||
);
|
||||
extracted_window.present_mode = window.present_mode;
|
||||
}
|
||||
}
|
||||
|
||||
for closing_window in closing.read() {
|
||||
extracted_windows.remove(&closing_window.window);
|
||||
window_surfaces.remove(&closing_window.window);
|
||||
}
|
||||
for removed_window in removed.read() {
|
||||
extracted_windows.remove(&removed_window);
|
||||
window_surfaces.remove(&removed_window);
|
||||
}
|
||||
}
|
||||
|
||||
struct SurfaceData {
|
||||
// TODO: what lifetime should this be?
|
||||
surface: WgpuWrapper<wgpu::Surface<'static>>,
|
||||
configuration: SurfaceConfiguration,
|
||||
texture_view_format: Option<TextureFormat>,
|
||||
}
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
pub struct WindowSurfaces {
|
||||
surfaces: EntityHashMap<SurfaceData>,
|
||||
/// List of windows that we have already called the initial `configure_surface` for
|
||||
configured_windows: HashSet<Entity>,
|
||||
}
|
||||
|
||||
impl WindowSurfaces {
|
||||
fn remove(&mut self, window: &Entity) {
|
||||
self.surfaces.remove(window);
|
||||
self.configured_windows.remove(window);
|
||||
}
|
||||
}
|
||||
|
||||
/// (re)configures window surfaces, and obtains a swapchain texture for rendering.
|
||||
///
|
||||
/// NOTE: `get_current_texture` in `prepare_windows` can take a long time if the GPU workload is
|
||||
/// the performance bottleneck. This can be seen in profiles as multiple prepare-set systems all
|
||||
/// taking an unusually long time to complete, and all finishing at about the same time as the
|
||||
/// `prepare_windows` system. Improvements in bevy are planned to avoid this happening when it
|
||||
/// should not but it will still happen as it is easy for a user to create a large GPU workload
|
||||
/// relative to the GPU performance and/or CPU workload.
|
||||
/// This can be caused by many reasons, but several of them are:
|
||||
/// - GPU workload is more than your current GPU can manage
|
||||
/// - Error / performance bug in your custom shaders
|
||||
/// - wgpu was unable to detect a proper GPU hardware-accelerated device given the chosen
|
||||
/// [`Backends`](crate::settings::Backends), [`WgpuLimits`](crate::settings::WgpuLimits),
|
||||
/// and/or [`WgpuFeatures`](crate::settings::WgpuFeatures). For example, on Windows currently
|
||||
/// `DirectX 11` is not supported by wgpu 0.12 and so if your GPU/drivers do not support Vulkan,
|
||||
/// it may be that a software renderer called "Microsoft Basic Render Driver" using `DirectX 12`
|
||||
/// will be chosen and performance will be very poor. This is visible in a log message that is
|
||||
/// output during renderer initialization.
|
||||
/// Another alternative is to try to use [`ANGLE`](https://github.com/gfx-rs/wgpu#angle) and
|
||||
/// [`Backends::GL`](crate::settings::Backends::GL) with the `gles` feature enabled if your
|
||||
/// GPU/drivers support `OpenGL 4.3` / `OpenGL ES 3.0` or later.
|
||||
pub fn prepare_windows(
|
||||
mut windows: ResMut<ExtractedWindows>,
|
||||
mut window_surfaces: ResMut<WindowSurfaces>,
|
||||
render_device: Res<RenderDevice>,
|
||||
#[cfg(target_os = "linux")] render_instance: Res<RenderInstance>,
|
||||
) {
|
||||
for window in windows.windows.values_mut() {
|
||||
let window_surfaces = window_surfaces.deref_mut();
|
||||
let Some(surface_data) = window_surfaces.surfaces.get(&window.entity) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// We didn't present the previous frame, so we can keep using our existing swapchain texture.
|
||||
if window.has_swapchain_texture() && !window.size_changed && !window.present_mode_changed {
|
||||
continue;
|
||||
}
|
||||
|
||||
// A recurring issue is hitting `wgpu::SurfaceError::Timeout` on certain Linux
|
||||
// mesa driver implementations. This seems to be a quirk of some drivers.
|
||||
// We'd rather keep panicking when not on Linux mesa, because in those case,
|
||||
// the `Timeout` is still probably the symptom of a degraded unrecoverable
|
||||
// application state.
|
||||
// see https://github.com/bevyengine/bevy/pull/5957
|
||||
// and https://github.com/gfx-rs/wgpu/issues/1218
|
||||
#[cfg(target_os = "linux")]
|
||||
let may_erroneously_timeout = || {
|
||||
render_instance
|
||||
.enumerate_adapters(wgpu::Backends::VULKAN)
|
||||
.iter()
|
||||
.any(|adapter| {
|
||||
let name = adapter.get_info().name;
|
||||
name.starts_with("Radeon")
|
||||
|| name.starts_with("AMD")
|
||||
|| name.starts_with("Intel")
|
||||
})
|
||||
};
|
||||
|
||||
let surface = &surface_data.surface;
|
||||
match surface.get_current_texture() {
|
||||
Ok(frame) => {
|
||||
window.set_swapchain_texture(frame);
|
||||
}
|
||||
Err(wgpu::SurfaceError::Outdated) => {
|
||||
render_device.configure_surface(surface, &surface_data.configuration);
|
||||
let frame = match surface.get_current_texture() {
|
||||
Ok(frame) => frame,
|
||||
Err(err) => {
|
||||
// This is a common occurrence on X11 and Xwayland with NVIDIA drivers
|
||||
// when opening and resizing the window.
|
||||
warn!("Couldn't get swap chain texture after configuring. Cause: '{err}'");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
window.set_swapchain_texture(frame);
|
||||
}
|
||||
#[cfg(target_os = "linux")]
|
||||
Err(wgpu::SurfaceError::Timeout) => {
|
||||
if may_erroneously_timeout() {
|
||||
tracing::trace!(
|
||||
"Couldn't get swap chain texture. This is probably a quirk \
|
||||
of your Linux GPU driver, so it can be safely ignored."
|
||||
);
|
||||
} else {
|
||||
warn!(
|
||||
"Couldn't get swap chain texture due to a timeout; skipping this frame."
|
||||
);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Err(err) => {
|
||||
panic!("Couldn't get swap chain texture, operation unrecoverable: {err}");
|
||||
}
|
||||
}
|
||||
window.swap_chain_texture_format = Some(surface_data.configuration.format);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn need_surface_configuration(
|
||||
windows: Res<ExtractedWindows>,
|
||||
window_surfaces: Res<WindowSurfaces>,
|
||||
) -> bool {
|
||||
for window in windows.windows.values() {
|
||||
if !window_surfaces.configured_windows.contains(&window.entity)
|
||||
|| window.size_changed
|
||||
|| window.present_mode_changed
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
// 2 is wgpu's default/what we've been using so far.
|
||||
// 1 is the minimum, but may cause lower framerates due to the cpu waiting for the gpu to finish
|
||||
// all work for the previous frame before starting work on the next frame, which then means the gpu
|
||||
// has to wait for the cpu to finish to start on the next frame.
|
||||
const DEFAULT_DESIRED_MAXIMUM_FRAME_LATENCY: u32 = 2;
|
||||
|
||||
/// Creates window surfaces.
|
||||
pub fn create_surfaces(
|
||||
// By accessing a NonSend resource, we tell the scheduler to put this system on the main thread,
|
||||
// which is necessary for some OS's
|
||||
#[cfg(any(target_os = "macos", target_os = "ios"))] _marker: bevy_ecs::system::NonSendMarker,
|
||||
mut windows: ResMut<ExtractedWindows>,
|
||||
mut window_surfaces: ResMut<WindowSurfaces>,
|
||||
render_instance: Res<RenderInstance>,
|
||||
render_adapter: Res<RenderAdapter>,
|
||||
render_device: Res<RenderDevice>,
|
||||
) {
|
||||
for window in windows.windows.values_mut() {
|
||||
let data = window_surfaces
|
||||
.surfaces
|
||||
.entry(window.entity)
|
||||
.or_insert_with(|| {
|
||||
let surface_target = SurfaceTargetUnsafe::RawHandle {
|
||||
raw_display_handle: window.handle.get_display_handle(),
|
||||
raw_window_handle: window.handle.get_window_handle(),
|
||||
};
|
||||
// SAFETY: The window handles in ExtractedWindows will always be valid objects to create surfaces on
|
||||
let surface = unsafe {
|
||||
// NOTE: On some OSes this MUST be called from the main thread.
|
||||
// As of wgpu 0.15, only fallible if the given window is a HTML canvas and obtaining a WebGPU or WebGL2 context fails.
|
||||
render_instance
|
||||
.create_surface_unsafe(surface_target)
|
||||
.expect("Failed to create wgpu surface")
|
||||
};
|
||||
let caps = surface.get_capabilities(&render_adapter);
|
||||
let present_mode = present_mode(window, &caps);
|
||||
let formats = caps.formats;
|
||||
// For future HDR output support, we'll need to request a format that supports HDR,
|
||||
// but as of wgpu 0.15 that is not yet supported.
|
||||
// Prefer sRGB formats for surfaces, but fall back to first available format if no sRGB formats are available.
|
||||
let mut format = *formats.first().expect("No supported formats for surface");
|
||||
for available_format in formats {
|
||||
// Rgba8UnormSrgb and Bgra8UnormSrgb and the only sRGB formats wgpu exposes that we can use for surfaces.
|
||||
if available_format == TextureFormat::Rgba8UnormSrgb
|
||||
|| available_format == TextureFormat::Bgra8UnormSrgb
|
||||
{
|
||||
format = available_format;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let texture_view_format = if !format.is_srgb() {
|
||||
Some(format.add_srgb_suffix())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let configuration = SurfaceConfiguration {
|
||||
format,
|
||||
width: window.physical_width,
|
||||
height: window.physical_height,
|
||||
usage: TextureUsages::RENDER_ATTACHMENT,
|
||||
present_mode,
|
||||
desired_maximum_frame_latency: window
|
||||
.desired_maximum_frame_latency
|
||||
.map(NonZero::<u32>::get)
|
||||
.unwrap_or(DEFAULT_DESIRED_MAXIMUM_FRAME_LATENCY),
|
||||
alpha_mode: match window.alpha_mode {
|
||||
CompositeAlphaMode::Auto => wgpu::CompositeAlphaMode::Auto,
|
||||
CompositeAlphaMode::Opaque => wgpu::CompositeAlphaMode::Opaque,
|
||||
CompositeAlphaMode::PreMultiplied => {
|
||||
wgpu::CompositeAlphaMode::PreMultiplied
|
||||
}
|
||||
CompositeAlphaMode::PostMultiplied => {
|
||||
wgpu::CompositeAlphaMode::PostMultiplied
|
||||
}
|
||||
CompositeAlphaMode::Inherit => wgpu::CompositeAlphaMode::Inherit,
|
||||
},
|
||||
view_formats: match texture_view_format {
|
||||
Some(format) => vec![format],
|
||||
None => vec![],
|
||||
},
|
||||
};
|
||||
|
||||
render_device.configure_surface(&surface, &configuration);
|
||||
|
||||
SurfaceData {
|
||||
surface: WgpuWrapper::new(surface),
|
||||
configuration,
|
||||
texture_view_format,
|
||||
}
|
||||
});
|
||||
|
||||
if window.size_changed || window.present_mode_changed {
|
||||
// normally this is dropped on present but we double check here to be safe as failure to
|
||||
// drop it will cause validation errors in wgpu
|
||||
drop(window.swap_chain_texture.take());
|
||||
#[cfg_attr(
|
||||
target_arch = "wasm32",
|
||||
expect(clippy::drop_non_drop, reason = "texture views are not drop on wasm")
|
||||
)]
|
||||
drop(window.swap_chain_texture_view.take());
|
||||
|
||||
data.configuration.width = window.physical_width;
|
||||
data.configuration.height = window.physical_height;
|
||||
let caps = data.surface.get_capabilities(&render_adapter);
|
||||
data.configuration.present_mode = present_mode(window, &caps);
|
||||
render_device.configure_surface(&data.surface, &data.configuration);
|
||||
}
|
||||
|
||||
window_surfaces.configured_windows.insert(window.entity);
|
||||
}
|
||||
}
|
||||
|
||||
fn present_mode(
|
||||
window: &mut ExtractedWindow,
|
||||
caps: &wgpu::SurfaceCapabilities,
|
||||
) -> wgpu::PresentMode {
|
||||
let present_mode = match window.present_mode {
|
||||
PresentMode::Fifo => wgpu::PresentMode::Fifo,
|
||||
PresentMode::FifoRelaxed => wgpu::PresentMode::FifoRelaxed,
|
||||
PresentMode::Mailbox => wgpu::PresentMode::Mailbox,
|
||||
PresentMode::Immediate => wgpu::PresentMode::Immediate,
|
||||
PresentMode::AutoVsync => wgpu::PresentMode::AutoVsync,
|
||||
PresentMode::AutoNoVsync => wgpu::PresentMode::AutoNoVsync,
|
||||
};
|
||||
let fallbacks = match present_mode {
|
||||
wgpu::PresentMode::AutoVsync => {
|
||||
&[wgpu::PresentMode::FifoRelaxed, wgpu::PresentMode::Fifo][..]
|
||||
}
|
||||
wgpu::PresentMode::AutoNoVsync => &[
|
||||
wgpu::PresentMode::Immediate,
|
||||
wgpu::PresentMode::Mailbox,
|
||||
wgpu::PresentMode::Fifo,
|
||||
][..],
|
||||
wgpu::PresentMode::Mailbox => &[
|
||||
wgpu::PresentMode::Mailbox,
|
||||
wgpu::PresentMode::Immediate,
|
||||
wgpu::PresentMode::Fifo,
|
||||
][..],
|
||||
// Always end in FIFO to make sure it's always supported
|
||||
x => &[x, wgpu::PresentMode::Fifo][..],
|
||||
};
|
||||
let new_present_mode = fallbacks
|
||||
.iter()
|
||||
.copied()
|
||||
.find(|fallback| caps.present_modes.contains(fallback))
|
||||
.unwrap_or_else(|| {
|
||||
unreachable!(
|
||||
"Fallback system failed to choose present mode. \
|
||||
This is a bug. Mode: {:?}, Options: {:?}",
|
||||
window.present_mode, &caps.present_modes
|
||||
);
|
||||
});
|
||||
if new_present_mode != present_mode && fallbacks.contains(&present_mode) {
|
||||
info!("PresentMode {present_mode:?} requested but not available. Falling back to {new_present_mode:?}");
|
||||
}
|
||||
new_present_mode
|
||||
}
|
||||
@@ -0,0 +1,695 @@
|
||||
use super::ExtractedWindows;
|
||||
use crate::{
|
||||
gpu_readback,
|
||||
render_asset::RenderAssets,
|
||||
render_resource::{
|
||||
binding_types::texture_2d, BindGroup, BindGroupEntries, BindGroupLayoutDescriptor,
|
||||
BindGroupLayoutEntries, Buffer, BufferUsages, CachedRenderPipelineId, FragmentState,
|
||||
PipelineCache, RenderPipelineDescriptor, SpecializedRenderPipeline,
|
||||
SpecializedRenderPipelines, Texture, TextureUsages, TextureView, VertexState,
|
||||
},
|
||||
renderer::RenderDevice,
|
||||
texture::{GpuImage, ManualTextureViews, OutputColorAttachment},
|
||||
view::{prepare_view_attachments, prepare_view_targets, ViewTargetAttachments, WindowSurfaces},
|
||||
ExtractSchedule, MainWorld, Render, RenderApp, RenderStartup, RenderSystems,
|
||||
};
|
||||
use alloc::{borrow::Cow, sync::Arc};
|
||||
use bevy_app::{First, Plugin, Update};
|
||||
use bevy_asset::{embedded_asset, load_embedded_asset, AssetServer, Handle, RenderAssetUsages};
|
||||
use bevy_camera::{ManualTextureViewHandle, NormalizedRenderTarget, RenderTarget};
|
||||
use bevy_derive::{Deref, DerefMut};
|
||||
use bevy_ecs::{
|
||||
entity::EntityHashMap, message::message_update_system, prelude::*, system::SystemState,
|
||||
};
|
||||
use bevy_image::{Image, TextureFormatPixelInfo, ToExtents};
|
||||
use bevy_platform::collections::HashSet;
|
||||
use bevy_reflect::Reflect;
|
||||
use bevy_shader::Shader;
|
||||
use bevy_tasks::AsyncComputeTaskPool;
|
||||
use bevy_utils::default;
|
||||
use bevy_window::{PrimaryWindow, WindowRef};
|
||||
use core::ops::Deref;
|
||||
use std::{
|
||||
path::Path,
|
||||
sync::{
|
||||
mpsc::{Receiver, Sender},
|
||||
Mutex,
|
||||
},
|
||||
};
|
||||
use tracing::{error, info, warn};
|
||||
use wgpu::{CommandEncoder, Extent3d, TextureFormat};
|
||||
|
||||
#[derive(EntityEvent, Reflect, Deref, DerefMut, Debug)]
|
||||
#[reflect(Debug)]
|
||||
pub struct ScreenshotCaptured {
|
||||
pub entity: Entity,
|
||||
#[deref]
|
||||
pub image: Image,
|
||||
}
|
||||
|
||||
/// A component that signals to the renderer to capture a screenshot this frame.
|
||||
///
|
||||
/// This component should be spawned on a new entity with an observer that will trigger
|
||||
/// with [`ScreenshotCaptured`] when the screenshot is ready.
|
||||
///
|
||||
/// Screenshots are captured asynchronously and may not be available immediately after the frame
|
||||
/// that the component is spawned on. The observer should be used to handle the screenshot when it
|
||||
/// is ready.
|
||||
///
|
||||
/// Note that the screenshot entity will be despawned after the screenshot is captured and the
|
||||
/// observer is triggered.
|
||||
///
|
||||
/// # Usage
|
||||
///
|
||||
/// ```
|
||||
/// # use bevy_ecs::prelude::*;
|
||||
/// # use bevy_render::view::screenshot::{save_to_disk, Screenshot};
|
||||
///
|
||||
/// fn take_screenshot(mut commands: Commands) {
|
||||
/// commands.spawn(Screenshot::primary_window())
|
||||
/// .observe(save_to_disk("screenshot.png"));
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Component, Deref, DerefMut, Reflect, Debug)]
|
||||
#[reflect(Component, Debug)]
|
||||
pub struct Screenshot(pub RenderTarget);
|
||||
|
||||
/// A marker component that indicates that a screenshot is currently being captured.
|
||||
#[derive(Component, Default)]
|
||||
pub struct Capturing;
|
||||
|
||||
/// A marker component that indicates that a screenshot has been captured, the image is ready, and
|
||||
/// the screenshot entity can be despawned.
|
||||
#[derive(Component, Default)]
|
||||
pub struct Captured;
|
||||
|
||||
impl Screenshot {
|
||||
/// Capture a screenshot of the provided window entity.
|
||||
pub fn window(window: Entity) -> Self {
|
||||
Self(RenderTarget::Window(WindowRef::Entity(window)))
|
||||
}
|
||||
|
||||
/// Capture a screenshot of the primary window, if one exists.
|
||||
pub fn primary_window() -> Self {
|
||||
Self(RenderTarget::Window(WindowRef::Primary))
|
||||
}
|
||||
|
||||
/// Capture a screenshot of the provided render target image.
|
||||
pub fn image(image: Handle<Image>) -> Self {
|
||||
Self(RenderTarget::Image(image.into()))
|
||||
}
|
||||
|
||||
/// Capture a screenshot of the provided manual texture view.
|
||||
pub fn texture_view(texture_view: ManualTextureViewHandle) -> Self {
|
||||
Self(RenderTarget::TextureView(texture_view))
|
||||
}
|
||||
}
|
||||
|
||||
struct ScreenshotPreparedState {
|
||||
pub texture: Texture,
|
||||
pub buffer: Buffer,
|
||||
pub bind_group: BindGroup,
|
||||
pub pipeline_id: CachedRenderPipelineId,
|
||||
pub size: Extent3d,
|
||||
}
|
||||
|
||||
#[derive(Resource, Deref, DerefMut)]
|
||||
pub struct CapturedScreenshots(pub Arc<Mutex<Receiver<(Entity, Image)>>>);
|
||||
|
||||
#[derive(Resource, Deref, DerefMut, Default)]
|
||||
struct RenderScreenshotTargets(EntityHashMap<NormalizedRenderTarget>);
|
||||
|
||||
#[derive(Resource, Deref, DerefMut, Default)]
|
||||
struct RenderScreenshotsPrepared(EntityHashMap<ScreenshotPreparedState>);
|
||||
|
||||
#[derive(Resource, Deref, DerefMut)]
|
||||
struct RenderScreenshotsSender(Sender<(Entity, Image)>);
|
||||
|
||||
/// Saves the captured screenshot to disk at the provided path.
|
||||
pub fn save_to_disk(path: impl AsRef<Path>) -> impl FnMut(On<ScreenshotCaptured>) {
|
||||
let path = path.as_ref().to_owned();
|
||||
move |screenshot_captured| {
|
||||
let img = screenshot_captured.image.clone();
|
||||
match img.try_into_dynamic() {
|
||||
Ok(dyn_img) => match image::ImageFormat::from_path(&path) {
|
||||
Ok(format) => {
|
||||
// discard the alpha channel which stores brightness values when HDR is enabled to make sure
|
||||
// the screenshot looks right
|
||||
let img = dyn_img.to_rgb8();
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
match img.save_with_format(&path, format) {
|
||||
Ok(_) => info!("Screenshot saved to {}", path.display()),
|
||||
Err(e) => error!("Cannot save screenshot, IO error: {e}"),
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
let save_screenshot = || {
|
||||
use image::EncodableLayout;
|
||||
use wasm_bindgen::{JsCast, JsValue};
|
||||
|
||||
let mut image_buffer = std::io::Cursor::new(Vec::new());
|
||||
img.write_to(&mut image_buffer, format)
|
||||
.map_err(|e| JsValue::from_str(&format!("{e}")))?;
|
||||
|
||||
let parts = js_sys::Array::of1(
|
||||
&js_sys::Uint8Array::new_from_slice(
|
||||
image_buffer.into_inner().as_bytes(),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
let blob = web_sys::Blob::new_with_u8_array_sequence(&parts)?;
|
||||
let url = web_sys::Url::create_object_url_with_blob(&blob)?;
|
||||
let window = web_sys::window().unwrap();
|
||||
let document = window.document().unwrap();
|
||||
let link = document.create_element("a")?;
|
||||
link.set_attribute("href", &url)?;
|
||||
link.set_attribute(
|
||||
"download",
|
||||
path.file_name()
|
||||
.and_then(|filename| filename.to_str())
|
||||
.ok_or_else(|| JsValue::from_str("Invalid filename"))?,
|
||||
)?;
|
||||
let html_element = link.dyn_into::<web_sys::HtmlElement>()?;
|
||||
html_element.click();
|
||||
web_sys::Url::revoke_object_url(&url)?;
|
||||
Ok::<(), JsValue>(())
|
||||
};
|
||||
|
||||
match (save_screenshot)() {
|
||||
Ok(_) => info!("Screenshot saved to {}", path.display()),
|
||||
Err(e) => error!("Cannot save screenshot, error: {e:?}"),
|
||||
};
|
||||
}
|
||||
}
|
||||
Err(e) => error!("Cannot save screenshot, requested format not recognized: {e}"),
|
||||
},
|
||||
Err(e) => error!("Cannot save screenshot, screen format cannot be understood: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_screenshots(mut commands: Commands, screenshots: Query<Entity, With<Captured>>) {
|
||||
for entity in screenshots.iter() {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn trigger_screenshots(
|
||||
mut commands: Commands,
|
||||
captured_screenshots: ResMut<CapturedScreenshots>,
|
||||
) {
|
||||
let captured_screenshots = captured_screenshots.lock().unwrap();
|
||||
while let Ok((entity, image)) = captured_screenshots.try_recv() {
|
||||
commands.entity(entity).insert(Captured);
|
||||
commands.trigger(ScreenshotCaptured { image, entity });
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_screenshots(
|
||||
mut targets: ResMut<RenderScreenshotTargets>,
|
||||
mut main_world: ResMut<MainWorld>,
|
||||
mut system_state: Local<
|
||||
Option<
|
||||
SystemState<(
|
||||
Commands,
|
||||
Query<Entity, With<PrimaryWindow>>,
|
||||
Query<(Entity, &Screenshot), Without<Capturing>>,
|
||||
)>,
|
||||
>,
|
||||
>,
|
||||
mut seen_targets: Local<HashSet<NormalizedRenderTarget>>,
|
||||
) {
|
||||
if system_state.is_none() {
|
||||
*system_state = Some(SystemState::new(&mut main_world));
|
||||
}
|
||||
let system_state = system_state.as_mut().unwrap();
|
||||
let (mut commands, primary_window, screenshots) = system_state.get_mut(&mut main_world);
|
||||
|
||||
targets.clear();
|
||||
seen_targets.clear();
|
||||
|
||||
let primary_window = primary_window.iter().next();
|
||||
|
||||
for (entity, screenshot) in screenshots.iter() {
|
||||
let render_target = screenshot.0.clone();
|
||||
let Some(render_target) = render_target.normalize(primary_window) else {
|
||||
warn!(
|
||||
"Unknown render target for screenshot, skipping: {:?}",
|
||||
render_target
|
||||
);
|
||||
continue;
|
||||
};
|
||||
if seen_targets.contains(&render_target) {
|
||||
warn!(
|
||||
"Duplicate render target for screenshot, skipping entity {}: {:?}",
|
||||
entity, render_target
|
||||
);
|
||||
// If we don't despawn the entity here, it will be captured again in the next frame
|
||||
commands.entity(entity).despawn();
|
||||
continue;
|
||||
}
|
||||
seen_targets.insert(render_target.clone());
|
||||
targets.insert(entity, render_target);
|
||||
commands.entity(entity).insert(Capturing);
|
||||
}
|
||||
|
||||
system_state.apply(&mut main_world);
|
||||
}
|
||||
|
||||
fn prepare_screenshots(
|
||||
targets: Res<RenderScreenshotTargets>,
|
||||
mut prepared: ResMut<RenderScreenshotsPrepared>,
|
||||
window_surfaces: Res<WindowSurfaces>,
|
||||
render_device: Res<RenderDevice>,
|
||||
screenshot_pipeline: Res<ScreenshotToScreenPipeline>,
|
||||
pipeline_cache: Res<PipelineCache>,
|
||||
mut pipelines: ResMut<SpecializedRenderPipelines<ScreenshotToScreenPipeline>>,
|
||||
images: Res<RenderAssets<GpuImage>>,
|
||||
manual_texture_views: Res<ManualTextureViews>,
|
||||
mut view_target_attachments: ResMut<ViewTargetAttachments>,
|
||||
) {
|
||||
prepared.clear();
|
||||
for (entity, target) in targets.iter() {
|
||||
match target {
|
||||
NormalizedRenderTarget::Window(window) => {
|
||||
let window = window.entity();
|
||||
let Some(surface_data) = window_surfaces.surfaces.get(&window) else {
|
||||
warn!("Unknown window for screenshot, skipping: {}", window);
|
||||
continue;
|
||||
};
|
||||
let view_format = surface_data
|
||||
.texture_view_format
|
||||
.unwrap_or(surface_data.configuration.format);
|
||||
let size = Extent3d {
|
||||
width: surface_data.configuration.width,
|
||||
height: surface_data.configuration.height,
|
||||
..default()
|
||||
};
|
||||
let (texture_view, state) = prepare_screenshot_state(
|
||||
size,
|
||||
view_format,
|
||||
&render_device,
|
||||
&screenshot_pipeline,
|
||||
&pipeline_cache,
|
||||
&mut pipelines,
|
||||
);
|
||||
prepared.insert(*entity, state);
|
||||
view_target_attachments.insert(
|
||||
target.clone(),
|
||||
OutputColorAttachment::new(texture_view.clone(), view_format),
|
||||
);
|
||||
}
|
||||
NormalizedRenderTarget::Image(image) => {
|
||||
let Some(gpu_image) = images.get(&image.handle) else {
|
||||
warn!("Unknown image for screenshot, skipping: {:?}", image);
|
||||
continue;
|
||||
};
|
||||
let view_format = gpu_image
|
||||
.texture_view_format
|
||||
.unwrap_or(gpu_image.texture_format);
|
||||
let (texture_view, state) = prepare_screenshot_state(
|
||||
gpu_image.size,
|
||||
view_format,
|
||||
&render_device,
|
||||
&screenshot_pipeline,
|
||||
&pipeline_cache,
|
||||
&mut pipelines,
|
||||
);
|
||||
prepared.insert(*entity, state);
|
||||
view_target_attachments.insert(
|
||||
target.clone(),
|
||||
OutputColorAttachment::new(texture_view.clone(), view_format),
|
||||
);
|
||||
}
|
||||
NormalizedRenderTarget::TextureView(texture_view) => {
|
||||
let Some(manual_texture_view) = manual_texture_views.get(texture_view) else {
|
||||
warn!(
|
||||
"Unknown manual texture view for screenshot, skipping: {:?}",
|
||||
texture_view
|
||||
);
|
||||
continue;
|
||||
};
|
||||
let view_format = manual_texture_view.view_format;
|
||||
let size = manual_texture_view.size.to_extents();
|
||||
let (texture_view, state) = prepare_screenshot_state(
|
||||
size,
|
||||
view_format,
|
||||
&render_device,
|
||||
&screenshot_pipeline,
|
||||
&pipeline_cache,
|
||||
&mut pipelines,
|
||||
);
|
||||
prepared.insert(*entity, state);
|
||||
view_target_attachments.insert(
|
||||
target.clone(),
|
||||
OutputColorAttachment::new(texture_view.clone(), view_format),
|
||||
);
|
||||
}
|
||||
NormalizedRenderTarget::None { .. } => {
|
||||
// Nothing to screenshot!
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn prepare_screenshot_state(
|
||||
size: Extent3d,
|
||||
format: TextureFormat,
|
||||
render_device: &RenderDevice,
|
||||
pipeline: &ScreenshotToScreenPipeline,
|
||||
pipeline_cache: &PipelineCache,
|
||||
pipelines: &mut SpecializedRenderPipelines<ScreenshotToScreenPipeline>,
|
||||
) -> (TextureView, ScreenshotPreparedState) {
|
||||
let texture = render_device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some("screenshot-capture-rendertarget"),
|
||||
size,
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format,
|
||||
usage: TextureUsages::RENDER_ATTACHMENT
|
||||
| TextureUsages::COPY_SRC
|
||||
| TextureUsages::TEXTURE_BINDING,
|
||||
view_formats: &[],
|
||||
});
|
||||
let texture_view = texture.create_view(&Default::default());
|
||||
let buffer = render_device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("screenshot-transfer-buffer"),
|
||||
size: gpu_readback::get_aligned_size(size, format.pixel_size().unwrap_or(0) as u32) as u64,
|
||||
usage: BufferUsages::MAP_READ | BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
let bind_group = render_device.create_bind_group(
|
||||
"screenshot-to-screen-bind-group",
|
||||
&pipeline_cache.get_bind_group_layout(&pipeline.bind_group_layout),
|
||||
&BindGroupEntries::single(&texture_view),
|
||||
);
|
||||
let pipeline_id = pipelines.specialize(pipeline_cache, pipeline, format);
|
||||
|
||||
(
|
||||
texture_view,
|
||||
ScreenshotPreparedState {
|
||||
texture,
|
||||
buffer,
|
||||
bind_group,
|
||||
pipeline_id,
|
||||
size,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub struct ScreenshotPlugin;
|
||||
|
||||
impl Plugin for ScreenshotPlugin {
|
||||
fn build(&self, app: &mut bevy_app::App) {
|
||||
embedded_asset!(app, "screenshot.wgsl");
|
||||
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
app.insert_resource(CapturedScreenshots(Arc::new(Mutex::new(rx))))
|
||||
.add_systems(
|
||||
First,
|
||||
clear_screenshots
|
||||
.after(message_update_system)
|
||||
.before(ApplyDeferred),
|
||||
)
|
||||
.add_systems(Update, trigger_screenshots);
|
||||
|
||||
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
|
||||
return;
|
||||
};
|
||||
|
||||
render_app
|
||||
.insert_resource(RenderScreenshotsSender(tx))
|
||||
.init_resource::<RenderScreenshotTargets>()
|
||||
.init_resource::<RenderScreenshotsPrepared>()
|
||||
.init_resource::<SpecializedRenderPipelines<ScreenshotToScreenPipeline>>()
|
||||
.add_systems(RenderStartup, init_screenshot_to_screen_pipeline)
|
||||
.add_systems(ExtractSchedule, extract_screenshots.ambiguous_with_all())
|
||||
.add_systems(
|
||||
Render,
|
||||
prepare_screenshots
|
||||
.after(prepare_view_attachments)
|
||||
.before(prepare_view_targets)
|
||||
.in_set(RenderSystems::ManageViews),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct ScreenshotToScreenPipeline {
|
||||
pub bind_group_layout: BindGroupLayoutDescriptor,
|
||||
pub shader: Handle<Shader>,
|
||||
}
|
||||
|
||||
pub fn init_screenshot_to_screen_pipeline(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
let bind_group_layout = BindGroupLayoutDescriptor::new(
|
||||
"screenshot-to-screen-bgl",
|
||||
&BindGroupLayoutEntries::single(
|
||||
wgpu::ShaderStages::FRAGMENT,
|
||||
texture_2d(wgpu::TextureSampleType::Float { filterable: false }),
|
||||
),
|
||||
);
|
||||
|
||||
let shader = load_embedded_asset!(asset_server.as_ref(), "screenshot.wgsl");
|
||||
|
||||
commands.insert_resource(ScreenshotToScreenPipeline {
|
||||
bind_group_layout,
|
||||
shader,
|
||||
});
|
||||
}
|
||||
|
||||
impl SpecializedRenderPipeline for ScreenshotToScreenPipeline {
|
||||
type Key = TextureFormat;
|
||||
|
||||
fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
|
||||
RenderPipelineDescriptor {
|
||||
label: Some(Cow::Borrowed("screenshot-to-screen")),
|
||||
layout: vec![self.bind_group_layout.clone()],
|
||||
vertex: VertexState {
|
||||
shader: self.shader.clone(),
|
||||
..default()
|
||||
},
|
||||
primitive: wgpu::PrimitiveState {
|
||||
cull_mode: Some(wgpu::Face::Back),
|
||||
..Default::default()
|
||||
},
|
||||
multisample: Default::default(),
|
||||
fragment: Some(FragmentState {
|
||||
shader: self.shader.clone(),
|
||||
targets: vec![Some(wgpu::ColorTargetState {
|
||||
format: key,
|
||||
blend: None,
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
..default()
|
||||
}),
|
||||
..default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn submit_screenshot_commands(world: &World, encoder: &mut CommandEncoder) {
|
||||
let targets = world.resource::<RenderScreenshotTargets>();
|
||||
let prepared = world.resource::<RenderScreenshotsPrepared>();
|
||||
let pipelines = world.resource::<PipelineCache>();
|
||||
let gpu_images = world.resource::<RenderAssets<GpuImage>>();
|
||||
let windows = world.resource::<ExtractedWindows>();
|
||||
let manual_texture_views = world.resource::<ManualTextureViews>();
|
||||
|
||||
for (entity, render_target) in targets.iter() {
|
||||
match render_target {
|
||||
NormalizedRenderTarget::Window(window) => {
|
||||
let window = window.entity();
|
||||
let Some(window) = windows.get(&window) else {
|
||||
continue;
|
||||
};
|
||||
let width = window.physical_width;
|
||||
let height = window.physical_height;
|
||||
let Some(texture_format) = window.swap_chain_texture_view_format else {
|
||||
continue;
|
||||
};
|
||||
let Some(swap_chain_texture_view) = window.swap_chain_texture_view.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
render_screenshot(
|
||||
encoder,
|
||||
prepared,
|
||||
pipelines,
|
||||
entity,
|
||||
width,
|
||||
height,
|
||||
texture_format,
|
||||
swap_chain_texture_view,
|
||||
);
|
||||
}
|
||||
NormalizedRenderTarget::Image(image) => {
|
||||
let Some(gpu_image) = gpu_images.get(&image.handle) else {
|
||||
warn!("Unknown image for screenshot, skipping: {:?}", image);
|
||||
continue;
|
||||
};
|
||||
let width = gpu_image.size.width;
|
||||
let height = gpu_image.size.height;
|
||||
let texture_format = gpu_image.texture_format;
|
||||
let texture_view = gpu_image.texture_view.deref();
|
||||
render_screenshot(
|
||||
encoder,
|
||||
prepared,
|
||||
pipelines,
|
||||
entity,
|
||||
width,
|
||||
height,
|
||||
texture_format,
|
||||
texture_view,
|
||||
);
|
||||
}
|
||||
NormalizedRenderTarget::TextureView(texture_view) => {
|
||||
let Some(texture_view) = manual_texture_views.get(texture_view) else {
|
||||
warn!(
|
||||
"Unknown manual texture view for screenshot, skipping: {:?}",
|
||||
texture_view
|
||||
);
|
||||
continue;
|
||||
};
|
||||
let width = texture_view.size.x;
|
||||
let height = texture_view.size.y;
|
||||
let texture_format = texture_view.view_format;
|
||||
let texture_view = texture_view.texture_view.deref();
|
||||
render_screenshot(
|
||||
encoder,
|
||||
prepared,
|
||||
pipelines,
|
||||
entity,
|
||||
width,
|
||||
height,
|
||||
texture_format,
|
||||
texture_view,
|
||||
);
|
||||
}
|
||||
NormalizedRenderTarget::None { .. } => {
|
||||
// Nothing to screenshot!
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn render_screenshot(
|
||||
encoder: &mut CommandEncoder,
|
||||
prepared: &RenderScreenshotsPrepared,
|
||||
pipelines: &PipelineCache,
|
||||
entity: &Entity,
|
||||
width: u32,
|
||||
height: u32,
|
||||
texture_format: TextureFormat,
|
||||
texture_view: &wgpu::TextureView,
|
||||
) {
|
||||
if let Some(prepared_state) = &prepared.get(entity) {
|
||||
let extent = Extent3d {
|
||||
width,
|
||||
height,
|
||||
depth_or_array_layers: 1,
|
||||
};
|
||||
encoder.copy_texture_to_buffer(
|
||||
prepared_state.texture.as_image_copy(),
|
||||
wgpu::TexelCopyBufferInfo {
|
||||
buffer: &prepared_state.buffer,
|
||||
layout: gpu_readback::layout_data(extent, texture_format),
|
||||
},
|
||||
extent,
|
||||
);
|
||||
|
||||
if let Some(pipeline) = pipelines.get_render_pipeline(prepared_state.pipeline_id) {
|
||||
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("screenshot_to_screen_pass"),
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
view: texture_view,
|
||||
depth_slice: None,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Load,
|
||||
store: wgpu::StoreOp::Store,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
timestamp_writes: None,
|
||||
occlusion_query_set: None,
|
||||
});
|
||||
pass.set_pipeline(pipeline);
|
||||
pass.set_bind_group(0, &prepared_state.bind_group, &[]);
|
||||
pass.draw(0..3, 0..1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn collect_screenshots(world: &mut World) {
|
||||
#[cfg(feature = "trace")]
|
||||
let _span = tracing::info_span!("collect_screenshots").entered();
|
||||
|
||||
let sender = world.resource::<RenderScreenshotsSender>().deref().clone();
|
||||
let prepared = world.resource::<RenderScreenshotsPrepared>();
|
||||
|
||||
for (entity, prepared) in prepared.iter() {
|
||||
let entity = *entity;
|
||||
let sender = sender.clone();
|
||||
let width = prepared.size.width;
|
||||
let height = prepared.size.height;
|
||||
let texture_format = prepared.texture.format();
|
||||
let Ok(pixel_size) = texture_format.pixel_size() else {
|
||||
continue;
|
||||
};
|
||||
let buffer = prepared.buffer.clone();
|
||||
|
||||
let finish = async move {
|
||||
let (tx, rx) = async_channel::bounded(1);
|
||||
let buffer_slice = buffer.slice(..);
|
||||
// The polling for this map call is done every frame when the command queue is submitted.
|
||||
buffer_slice.map_async(wgpu::MapMode::Read, move |result| {
|
||||
if let Err(err) = result {
|
||||
panic!("{}", err.to_string());
|
||||
}
|
||||
tx.try_send(()).unwrap();
|
||||
});
|
||||
rx.recv().await.unwrap();
|
||||
let data = buffer_slice.get_mapped_range();
|
||||
// we immediately move the data to CPU memory to avoid holding the mapped view for long
|
||||
let mut result = Vec::from(&*data);
|
||||
drop(data);
|
||||
|
||||
if result.len() != ((width * height) as usize * pixel_size) {
|
||||
// Our buffer has been padded because we needed to align to a multiple of 256.
|
||||
// We remove this padding here
|
||||
let initial_row_bytes = width as usize * pixel_size;
|
||||
let buffered_row_bytes =
|
||||
gpu_readback::align_byte_size(width * pixel_size as u32) as usize;
|
||||
|
||||
let mut take_offset = buffered_row_bytes;
|
||||
let mut place_offset = initial_row_bytes;
|
||||
for _ in 1..height {
|
||||
result.copy_within(take_offset..take_offset + buffered_row_bytes, place_offset);
|
||||
take_offset += buffered_row_bytes;
|
||||
place_offset += initial_row_bytes;
|
||||
}
|
||||
result.truncate(initial_row_bytes * height as usize);
|
||||
}
|
||||
|
||||
if let Err(e) = sender.send((
|
||||
entity,
|
||||
Image::new(
|
||||
Extent3d {
|
||||
width,
|
||||
height,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
wgpu::TextureDimension::D2,
|
||||
result,
|
||||
texture_format,
|
||||
RenderAssetUsages::RENDER_WORLD,
|
||||
),
|
||||
)) {
|
||||
error!("Failed to send screenshot: {}", e);
|
||||
}
|
||||
};
|
||||
|
||||
AsyncComputeTaskPool::get().spawn(finish).detach();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// This vertex shader will create a triangle that will cover the entire screen
|
||||
// with minimal effort, avoiding the need for a vertex buffer etc.
|
||||
@vertex
|
||||
fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4<f32> {
|
||||
let x = f32((in_vertex_index & 1u) << 2u);
|
||||
let y = f32((in_vertex_index & 2u) << 1u);
|
||||
return vec4<f32>(x - 1.0, y - 1.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
@group(0) @binding(0) var t: texture_2d<f32>;
|
||||
|
||||
@fragment
|
||||
fn fs_main(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> {
|
||||
let coords = floor(pos.xy);
|
||||
return textureLoad(t, vec2<i32>(coords), 0i);
|
||||
}
|
||||
Reference in new issue
Block a user