Skip to content

Commit

Permalink
Add DEFAULT_DEPTH_FORMAT constant
Browse files Browse the repository at this point in the history
  • Loading branch information
Gordon-F committed Jan 15, 2022
1 parent 9cc7a35 commit df9c4e2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 28 deletions.
5 changes: 2 additions & 3 deletions crates/bevy_core_pipeline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use bevy_render::{
},
render_resource::*,
renderer::RenderDevice,
texture::TextureCache,
texture::{TextureCache, DEFAULT_DEPTH_FORMAT},
view::{ExtractedView, Msaa, ViewDepthTexture},
RenderApp, RenderStage, RenderWorld,
};
Expand Down Expand Up @@ -384,8 +384,7 @@ pub fn prepare_core_views_system(
mip_level_count: 1,
sample_count: msaa.samples,
dimension: TextureDimension::D2,
format: TextureFormat::Depth32Float, /* PERF: vulkan docs recommend using 24
* bit depth for better performance */
format: DEFAULT_DEPTH_FORMAT,
usage: TextureUsages::RENDER_ATTACHMENT,
},
);
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub const MAX_POINT_LIGHT_SHADOW_MAPS: usize = 10;
pub const MAX_DIRECTIONAL_LIGHTS: usize = 1;
pub const POINT_SHADOW_LAYERS: u32 = (6 * MAX_POINT_LIGHT_SHADOW_MAPS) as u32;
pub const DIRECTIONAL_SHADOW_LAYERS: u32 = MAX_DIRECTIONAL_LIGHTS as u32;
pub const SHADOW_FORMAT: TextureFormat = TextureFormat::Depth32Float;
pub const SHADOW_FORMAT: TextureFormat = DEFAULT_DEPTH_FORMAT;

pub struct ShadowPipeline {
pub view_layout: BindGroupLayout,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use bevy_render::{
render_phase::{EntityRenderCommand, RenderCommandResult, TrackedRenderPass},
render_resource::{std140::AsStd140, *},
renderer::{RenderDevice, RenderQueue},
texture::{BevyDefault, GpuImage, Image, TextureFormatPixelInfo},
texture::{BevyDefault, GpuImage, Image, TextureFormatPixelInfo, DEFAULT_DEPTH_FORMAT},
view::{ComputedVisibility, ViewUniform, ViewUniformOffset, ViewUniforms},
RenderApp, RenderStage,
};
Expand Down Expand Up @@ -527,7 +527,7 @@ impl SpecializedPipeline for MeshPipeline {
strip_index_format: None,
},
depth_stencil: Some(DepthStencilState {
format: TextureFormat::Depth32Float,
format: DEFAULT_DEPTH_FORMAT,
depth_write_enabled,
depth_compare: CompareFunction::Greater,
stencil: StencilState {
Expand Down
85 changes: 63 additions & 22 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub mod prelude {
};
}

use bevy_utils::tracing::debug;
use bevy_utils::tracing::{debug, info};
pub use once_cell;

use crate::{
Expand All @@ -38,13 +38,16 @@ use crate::{
render_graph::RenderGraph,
render_resource::{RenderPipelineCache, Shader, ShaderLoader},
renderer::render_system,
texture::ImagePlugin,
texture::{BevyDefault as _, ImagePlugin, DEFAULT_DEPTH_FORMAT},
view::{ViewPlugin, WindowRenderPlugin},
};
use bevy_app::{App, AppLabel, Plugin};
use bevy_asset::{AddAsset, AssetServer};
use bevy_ecs::prelude::*;
use std::ops::{Deref, DerefMut};
use std::{
ops::{Deref, DerefMut},
sync::Arc,
};

/// Contains the default Bevy rendering backend based on wgpu.
#[derive(Default)]
Expand Down Expand Up @@ -113,35 +116,72 @@ impl Plugin for RenderPlugin {
.get_resource::<options::WgpuOptions>()
.cloned()
.unwrap_or_default();

app.add_asset::<Shader>()
.init_asset_loader::<ShaderLoader>()
.register_type::<Color>();

if let Some(backends) = options.backends {
let instance = wgpu::Instance::new(backends);
let surface = {
let world = app.world.cell();
let windows = world.get_resource_mut::<bevy_window::Windows>().unwrap();
let raw_handle = windows.get_primary().map(|window| unsafe {
let handle = window.raw_window_handle().get_handle();
instance.create_surface(&handle)
});
raw_handle
};
let request_adapter_options = wgpu::RequestAdapterOptions {
power_preference: options.power_preference,
compatible_surface: surface.as_ref(),
..Default::default()
};
let adapter = Arc::new({
#[cfg(not(target_arch = "wasm32"))]
{
let mut selected_adapter = None;
for adapter in instance.enumerate_adapters(backends) {
let default_texture_format_features = adapter
.get_texture_format_features(wgpu::TextureFormat::bevy_default());
let default_depth_format_features =
adapter.get_texture_format_features(DEFAULT_DEPTH_FORMAT);

let requested_device_type = match options.power_preference {
wgpu::PowerPreference::LowPower => wgpu::DeviceType::IntegratedGpu,
wgpu::PowerPreference::HighPerformance => wgpu::DeviceType::DiscreteGpu,
};
if default_texture_format_features
.allowed_usages
.contains(wgpu::TextureUsages::RENDER_ATTACHMENT)
&& default_depth_format_features
.allowed_usages
.contains(wgpu::TextureUsages::RENDER_ATTACHMENT)
&& adapter.get_info().device_type == requested_device_type
{
selected_adapter = Some(adapter);
} else {
continue;
}
}

selected_adapter.expect(
"Unable to find a GPU! Make sure you have installed required drivers!",
)
}

#[cfg(target_arch = "wasm32")]
{
let request_adapter_options = wgpu::RequestAdapterOptions {
power_preference: options.power_preference,
compatible_surface: None,
..Default::default()
};
instance
.request_adapter(request_adapter_options)
.await
.expect(
"Unable to find a GPU! Make sure you have installed required drivers!",
)
}
});
info!("{:?}", adapter.get_info());
let (device, queue) = futures_lite::future::block_on(renderer::initialize_renderer(
&instance,
&adapter,
&mut options,
&request_adapter_options,
));
debug!("Configured wgpu adapter Limits: {:#?}", &options.limits);
debug!("Configured wgpu adapter Features: {:#?}", &options.features);
debug!("Configured wgpu adapter Limits: {:#?}", &adapter.limits());
debug!(
"Configured wgpu adapter Features: {:#?}",
&adapter.features()
);
app.insert_resource(device.clone())
.insert_resource(adapter.clone())
.insert_resource(queue.clone())
.insert_resource(options.clone())
.init_resource::<ScratchRenderWorld>()
Expand Down Expand Up @@ -170,6 +210,7 @@ impl Plugin for RenderPlugin {
.add_stage(RenderStage::Cleanup, SystemStage::parallel())
.insert_resource(instance)
.insert_resource(device)
.insert_resource(adapter)
.insert_resource(queue)
.insert_resource(options)
.insert_resource(render_pipeline_cache)
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_render/src/texture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ impl Plugin for ImagePlugin {
}
}

// PERF: vulkan docs recommend using 24 bit depth for better performance
pub const DEFAULT_DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;

pub trait BevyDefault {
fn bevy_default() -> Self;
}
Expand Down

0 comments on commit df9c4e2

Please sign in to comment.