-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add Skybox Motion Vectors #13617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Skybox Motion Vectors #13617
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ca10a1f
skeleton code
pcwalton 1c68888
Skybox motion vectors
aevyrie f1f162d
fix pipeline crashes
aevyrie e5090ef
revert skybox example changes
aevyrie 008d714
revert motion blur example change
aevyrie fe0a21a
Merge branch 'main' into skybox-prepass
aevyrie 8acdf26
Mix merge breakages to prepass imports
aevyrie 25ecf0e
Review feedback
aevyrie 1282a73
Fix unused import
aevyrie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| #![warn(missing_docs)] | ||
|
|
||
| //! Adds motion vector support to skyboxes. See [`SkyboxPrepassPipeline`] for details. | ||
|
|
||
| use bevy_asset::Handle; | ||
| use bevy_ecs::{ | ||
| component::Component, | ||
| entity::Entity, | ||
| query::{Has, With}, | ||
| system::{Commands, Query, Res, ResMut, Resource}, | ||
| world::{FromWorld, World}, | ||
| }; | ||
| use bevy_render::{ | ||
| render_resource::{ | ||
| binding_types::uniform_buffer, BindGroup, BindGroupEntries, BindGroupLayout, | ||
| BindGroupLayoutEntries, CachedRenderPipelineId, CompareFunction, DepthStencilState, | ||
| FragmentState, MultisampleState, PipelineCache, RenderPipelineDescriptor, Shader, | ||
| ShaderStages, SpecializedRenderPipeline, SpecializedRenderPipelines, | ||
| }, | ||
| renderer::RenderDevice, | ||
| view::{Msaa, ViewUniform, ViewUniforms}, | ||
| }; | ||
| use bevy_utils::prelude::default; | ||
|
|
||
| use crate::{ | ||
| core_3d::CORE_3D_DEPTH_FORMAT, | ||
| prepass::{ | ||
| prepass_target_descriptors, MotionVectorPrepass, NormalPrepass, PreviousViewData, | ||
| PreviousViewUniforms, | ||
| }, | ||
| Skybox, | ||
| }; | ||
|
|
||
| pub const SKYBOX_PREPASS_SHADER_HANDLE: Handle<Shader> = Handle::weak_from_u128(376510055324461154); | ||
|
|
||
| /// This pipeline writes motion vectors to the prepass for all [`Skybox`]es. | ||
| /// | ||
| /// This allows features like motion blur and TAA to work correctly on the skybox. Without this, for | ||
| /// example, motion blur would not be applied to the skybox when the camera is rotated and motion | ||
| /// blur is enabled. | ||
| #[derive(Resource)] | ||
| pub struct SkyboxPrepassPipeline { | ||
| bind_group_layout: BindGroupLayout, | ||
| } | ||
|
|
||
| /// Used to specialize the [`SkyboxPrepassPipeline`]. | ||
| #[derive(PartialEq, Eq, Hash, Clone, Copy)] | ||
| pub struct SkyboxPrepassPipelineKey { | ||
| samples: u32, | ||
| normal_prepass: bool, | ||
| } | ||
|
|
||
| /// Stores the ID for a camera's specialized pipeline, so it can be retrieved from the | ||
| /// [`PipelineCache`]. | ||
| #[derive(Component)] | ||
| pub struct RenderSkyboxPrepassPipeline(pub CachedRenderPipelineId); | ||
|
|
||
| /// Stores the [`SkyboxPrepassPipeline`] bind group for a camera. This is later used by the prepass | ||
| /// render graph node to add this binding to the prepass's render pass. | ||
| #[derive(Component)] | ||
| pub struct SkyboxPrepassBindGroup(pub BindGroup); | ||
|
|
||
| impl FromWorld for SkyboxPrepassPipeline { | ||
| fn from_world(world: &mut World) -> Self { | ||
| let render_device = world.resource::<RenderDevice>(); | ||
|
|
||
| Self { | ||
| bind_group_layout: render_device.create_bind_group_layout( | ||
| "skybox_prepass_bind_group_layout", | ||
| &BindGroupLayoutEntries::sequential( | ||
| ShaderStages::FRAGMENT, | ||
| ( | ||
| uniform_buffer::<ViewUniform>(true), | ||
| uniform_buffer::<PreviousViewData>(true), | ||
| ), | ||
| ), | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl SpecializedRenderPipeline for SkyboxPrepassPipeline { | ||
| type Key = SkyboxPrepassPipelineKey; | ||
|
|
||
| fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor { | ||
| RenderPipelineDescriptor { | ||
| label: Some("skybox_prepass_pipeline".into()), | ||
| layout: vec![self.bind_group_layout.clone()], | ||
| push_constant_ranges: vec![], | ||
| vertex: crate::fullscreen_vertex_shader::fullscreen_shader_vertex_state(), | ||
| primitive: default(), | ||
| depth_stencil: Some(DepthStencilState { | ||
| format: CORE_3D_DEPTH_FORMAT, | ||
| depth_write_enabled: false, | ||
| depth_compare: CompareFunction::GreaterEqual, | ||
| stencil: default(), | ||
| bias: default(), | ||
| }), | ||
| multisample: MultisampleState { | ||
| count: key.samples, | ||
| mask: !0, | ||
| alpha_to_coverage_enabled: false, | ||
| }, | ||
| fragment: Some(FragmentState { | ||
| shader: SKYBOX_PREPASS_SHADER_HANDLE, | ||
| shader_defs: vec![], | ||
| entry_point: "fragment".into(), | ||
| targets: prepass_target_descriptors(key.normal_prepass, true, false), | ||
| }), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Specialize and cache the [`SkyboxPrepassPipeline`] for each camera with a [`Skybox`]. | ||
| pub fn prepare_skybox_prepass_pipelines( | ||
| mut commands: Commands, | ||
| pipeline_cache: Res<PipelineCache>, | ||
| mut pipelines: ResMut<SpecializedRenderPipelines<SkyboxPrepassPipeline>>, | ||
| msaa: Res<Msaa>, | ||
| pipeline: Res<SkyboxPrepassPipeline>, | ||
| views: Query<(Entity, Has<NormalPrepass>), (With<Skybox>, With<MotionVectorPrepass>)>, | ||
| ) { | ||
| for (entity, normal_prepass) in &views { | ||
| let pipeline_key = SkyboxPrepassPipelineKey { | ||
| samples: msaa.samples(), | ||
| normal_prepass, | ||
| }; | ||
|
|
||
| let render_skybox_prepass_pipeline = | ||
| pipelines.specialize(&pipeline_cache, &pipeline, pipeline_key); | ||
| commands | ||
| .entity(entity) | ||
| .insert(RenderSkyboxPrepassPipeline(render_skybox_prepass_pipeline)); | ||
| } | ||
| } | ||
|
|
||
| /// Creates the required bind groups for the [`SkyboxPrepassPipeline`]. This binds the view uniforms | ||
| /// from the CPU for access in the prepass shader on the GPU, allowing us to compute camera motion | ||
| /// between frames. This is then stored in the [`SkyboxPrepassBindGroup`] component on the camera. | ||
| pub fn prepare_skybox_prepass_bind_groups( | ||
| mut commands: Commands, | ||
| pipeline: Res<SkyboxPrepassPipeline>, | ||
| view_uniforms: Res<ViewUniforms>, | ||
| prev_view_uniforms: Res<PreviousViewUniforms>, | ||
| render_device: Res<RenderDevice>, | ||
| views: Query<Entity, (With<Skybox>, With<MotionVectorPrepass>)>, | ||
| ) { | ||
| for entity in &views { | ||
| let (Some(view_uniforms), Some(prev_view_uniforms)) = ( | ||
| view_uniforms.uniforms.binding(), | ||
| prev_view_uniforms.uniforms.binding(), | ||
| ) else { | ||
| continue; | ||
| }; | ||
| let bind_group = render_device.create_bind_group( | ||
| "skybox_prepass_bind_group", | ||
| &pipeline.bind_group_layout, | ||
| &BindGroupEntries::sequential((view_uniforms, prev_view_uniforms)), | ||
| ); | ||
|
|
||
| commands | ||
| .entity(entity) | ||
| .insert(SkyboxPrepassBindGroup(bind_group)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.