Skip to content
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

Add handling for more naga capabilities #9000

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl Plugin for RenderPlugin {

render_app
.insert_resource(instance)
.insert_resource(PipelineCache::new(device.clone()))
.insert_resource(PipelineCache::new(render_adapter.clone(), device.clone()))
.insert_resource(device)
.insert_resource(queue)
.insert_resource(render_adapter)
Expand Down
32 changes: 26 additions & 6 deletions crates/bevy_render/src/render_resource/pipeline_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
RawComputePipelineDescriptor, RawFragmentState, RawRenderPipelineDescriptor,
RawVertexState, RenderPipeline, RenderPipelineDescriptor, Shader, ShaderImport, Source,
},
renderer::RenderDevice,
renderer::{RenderAdapter, RenderDevice},
Extract,
};
use bevy_asset::{AssetEvent, AssetId, Assets};
Expand All @@ -26,6 +26,7 @@ use std::{
use thiserror::Error;
#[cfg(feature = "shader_format_spirv")]
use wgpu::util::make_spirv;
use wgpu::DownlevelFlags;
use wgpu::{
Features, PipelineLayoutDescriptor, PushConstantRange, ShaderModuleDescriptor,
VertexBufferLayout as RawVertexBufferLayout,
Expand Down Expand Up @@ -168,8 +169,8 @@ impl ShaderDefVal {
}

impl ShaderCache {
fn new(render_device: &RenderDevice) -> Self {
const CAPABILITIES: &[(Features, Capabilities)] = &[
fn new(render_adapter: &RenderAdapter, render_device: &RenderDevice) -> Self {
const FEATURES_CAPABILITIES: &[(Features, Capabilities)] = &[
(Features::PUSH_CONSTANTS, Capabilities::PUSH_CONSTANT),
(Features::SHADER_F64, Capabilities::FLOAT64),
(
Expand All @@ -188,14 +189,33 @@ impl ShaderCache {
Features::UNIFORM_BUFFER_AND_STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING,
Capabilities::UNIFORM_BUFFER_AND_STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING,
),
(
Features::TEXTURE_FORMAT_16BIT_NORM,
Capabilities::STORAGE_TEXTURE_16BIT_NORM_FORMATS,
),
(Features::MULTIVIEW, Capabilities::MULTIVIEW),
(
Features::SHADER_EARLY_DEPTH_TEST,
Capabilities::EARLY_DEPTH_TEST,
),
];
const DOWNLEVEL_FLAGS_CAPABILITIES: &[(DownlevelFlags, Capabilities)] = &[(
DownlevelFlags::MULTISAMPLED_SHADING,
Capabilities::MULTISAMPLED_SHADING,
)];
let features = render_device.features();
let mut capabilities = Capabilities::empty();
for (feature, capability) in CAPABILITIES {
for (feature, capability) in FEATURES_CAPABILITIES {
if features.contains(*feature) {
capabilities |= *capability;
}
}
let downlevel_flags = render_adapter.get_downlevel_capabilities().flags;
for (downlevel_flag, capability) in DOWNLEVEL_FLAGS_CAPABILITIES {
if downlevel_flags.contains(*downlevel_flag) {
capabilities |= *capability;
}
}

#[cfg(debug_assertions)]
let composer = naga_oil::compose::Composer::default();
Expand Down Expand Up @@ -484,9 +504,9 @@ impl PipelineCache {
}

/// Create a new pipeline cache associated with the given render device.
pub fn new(device: RenderDevice) -> Self {
pub fn new(adapter: RenderAdapter, device: RenderDevice) -> Self {
Self {
shader_cache: ShaderCache::new(&device),
shader_cache: ShaderCache::new(&adapter, &device),
device,
layout_cache: default(),
waiting_pipelines: default(),
Expand Down
26 changes: 21 additions & 5 deletions examples/shader/shader_prepass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() {
},
))
.add_systems(Startup, setup)
.add_systems(Update, (rotate, toggle_prepass_view))
.add_systems(Update, (rotate, toggle_prepass_view, toggle_multisampling))
// Disabling MSAA for maximum compatibility. Shader prepass with MSAA needs GPU capability MULTISAMPLED_SHADING
.insert_resource(Msaa::Off)
.run();
Expand Down Expand Up @@ -142,10 +142,12 @@ fn setup(
commands.spawn(
TextBundle::from_sections(vec![
TextSection::new("Prepass Output: transparent\n", style.clone()),
TextSection::new("MSAA: Off\n", style.clone()),
TextSection::new("\n\n", style.clone()),
TextSection::new("Controls\n", style.clone()),
TextSection::new("---------------\n", style.clone()),
TextSection::new("Space - Change output\n", style),
TextSection::new("Space - Change output\n", style.clone()),
TextSection::new("M - Change MSAA\n", style),
])
.with_style(Style {
position_type: PositionType::Absolute,
Expand Down Expand Up @@ -242,9 +244,6 @@ fn toggle_prepass_view(
};
let mut text = text.single_mut();
text.sections[0].value = format!("Prepass Output: {label}\n");
for section in &mut text.sections {
section.style.color = Color::WHITE;
}

let handle = material_handle.single();
let mat = materials.get_mut(handle).unwrap();
Expand All @@ -253,3 +252,20 @@ fn toggle_prepass_view(
mat.settings.show_motion_vectors = (*prepass_view == 3) as u32;
}
}

fn toggle_multisampling(
keycode: Res<Input<KeyCode>>,
mut text: Query<&mut Text>,
mut msaa: ResMut<Msaa>,
) {
if keycode.just_pressed(KeyCode::M) {
*msaa = match *msaa {
Msaa::Off => Msaa::Sample2,
Msaa::Sample2 => Msaa::Sample4,
Msaa::Sample4 => Msaa::Sample8,
Msaa::Sample8 => Msaa::Off,
};
let mut text = text.single_mut();
text.sections[1].value = format!("MSAA: {:?}\n", *msaa);
}
}