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

Update to WGPU 0.10 #2556

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: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ keywords = ["game", "engine", "gamedev", "graphics", "bevy"]
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/bevyengine/bevy"
resolver = "2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will do nothing for crates depending on Bevy and it doesn't seem needed here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was required for WGPU to build, but I'll have to double-check.

Copy link
Member

@mockersf mockersf Jul 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resolver v2 let's you specify features per target. While this PR doesn't need it, maybe it's needed internally by wgpu? That would be unfortunate...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes WGPU needs it to compile now. I do remember it was a little difficult to make things work before without the new feature resolver, but it did work. If it is a problem for Bevy, we could discuss it with the WGPU folks, maybe. I don't know if it would be possible to go back to not needing it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolver = "2" will become the default in the 2021 edition.

Copy link
Member

@mockersf mockersf Jul 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a dependency can't specify to use the new resolver for the dependent crate, it's a little painful for now for libs to require it... This will change with edition 2021 in October 🎉


[workspace]
exclude = ["benches"]
Expand Down Expand Up @@ -109,6 +110,7 @@ serde = { version = "1", features = ["derive"] }
# Needed to poll Task examples
futures-lite = "1.11.3"


[[example]]
name = "hello_world"
path = "examples/hello_world.rs"
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_render/src/render_graph/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
LoadOp, Operations, PassDescriptor, RenderPassColorAttachment,
RenderPassDepthStencilAttachment, TextureAttachment,
},
texture::{Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsage},
texture::{Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages},
Color,
};
use bevy_ecs::{reflect::ReflectComponent, world::World};
Expand Down Expand Up @@ -126,7 +126,7 @@ pub(crate) fn add_base_graph(config: &BaseRenderGraphConfig, world: &mut World)
dimension: TextureDimension::D2,
format: TextureFormat::Depth32Float, /* PERF: vulkan docs recommend using 24
* bit depth for better performance */
usage: TextureUsage::OUTPUT_ATTACHMENT,
usage: TextureUsages::OUTPUT_ATTACHMENT,
},
),
);
Expand Down Expand Up @@ -220,7 +220,7 @@ pub(crate) fn add_base_graph(config: &BaseRenderGraphConfig, world: &mut World)
sample_count: msaa.samples,
dimension: TextureDimension::D2,
format: TextureFormat::default(),
usage: TextureUsage::OUTPUT_ATTACHMENT,
usage: TextureUsages::OUTPUT_ATTACHMENT,
},
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Node for WindowSwapChainNode {

let render_resource_context = render_context.resources_mut();

// create window swapchain when window is resized or created
// reconfigure surface window is resized or created
if self
.window_created_event_reader
.iter(window_created_events)
Expand All @@ -62,10 +62,10 @@ impl Node for WindowSwapChainNode {
.iter(window_resized_events)
.any(|e| e.id == window.id())
{
render_resource_context.create_swap_chain(window);
render_resource_context.configure_surface(window);
}

let swap_chain_texture = render_resource_context.next_swap_chain_texture(window);
let swap_chain_texture = render_resource_context.next_surface_frame(window);
output.set(
WINDOW_TEXTURE,
RenderResourceId::Texture(swap_chain_texture),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ impl HeadlessRenderResourceContext {
}

impl RenderResourceContext for HeadlessRenderResourceContext {
fn create_swap_chain(&self, _window: &Window) {}
fn configure_surface(&self, _window: &Window) {}

fn next_swap_chain_texture(&self, _window: &Window) -> TextureId {
fn next_surface_frame(&self, _window: &Window) -> TextureId {
TextureId::new()
}

fn drop_swap_chain_texture(&self, _render_resource: TextureId) {}
fn drop_surface_frame(&self, _render_resource: TextureId) {}

fn drop_all_swap_chain_textures(&self) {}
fn drop_all_surface_frames(&self) {}

fn create_sampler(&self, _sampler_descriptor: &SamplerDescriptor) -> SamplerId {
SamplerId::new()
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_render/src/renderer/render_resource_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use downcast_rs::{impl_downcast, Downcast};
use std::ops::Range;

pub trait RenderResourceContext: Downcast + Send + Sync + 'static {
fn create_swap_chain(&self, window: &Window);
fn next_swap_chain_texture(&self, window: &Window) -> TextureId;
fn drop_swap_chain_texture(&self, resource: TextureId);
fn drop_all_swap_chain_textures(&self);
fn configure_surface(&self, window: &Window);
fn next_surface_frame(&self, window: &Window) -> TextureId;
fn drop_surface_frame(&self, resource: TextureId);
fn drop_all_surface_frames(&self);
fn create_sampler(&self, sampler_descriptor: &SamplerDescriptor) -> SamplerId;
fn create_texture(&self, texture_descriptor: TextureDescriptor) -> TextureId;
fn create_buffer(&self, buffer_info: BufferInfo) -> BufferId;
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_render/src/shader/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ pub fn glsl_to_spirv(
impl Into<shaderc::ShaderKind> for ShaderStage {
fn into(self) -> shaderc::ShaderKind {
match self {
ShaderStage::Vertex => shaderc::ShaderKind::Vertex,
ShaderStage::Fragment => shaderc::ShaderKind::Fragment,
ShaderStages::VERTEX => shaderc::ShaderKind::Vertex,
ShaderStages::FRAGMENT => shaderc::ShaderKind::Fragment,
ShaderStage::Compute => shaderc::ShaderKind::Compute,
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_render/src/texture/texture_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Extent3d, Texture, TextureDimension, TextureFormat, TextureUsage};
use super::{Extent3d, Texture, TextureDimension, TextureFormat, TextureUsages};

/// Describes a texture
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
Expand All @@ -8,7 +8,7 @@ pub struct TextureDescriptor {
pub sample_count: u32,
pub dimension: TextureDimension,
pub format: TextureFormat,
pub usage: TextureUsage,
pub usage: TextureUsages,
}

impl From<&Texture> for TextureDescriptor {
Expand All @@ -19,7 +19,7 @@ impl From<&Texture> for TextureDescriptor {
sample_count: 1,
dimension: texture.dimension,
format: texture.format,
usage: TextureUsage::SAMPLED | TextureUsage::COPY_DST,
usage: TextureUsages::SAMPLED | TextureUsages::COPY_DST,
}
}
}
Expand All @@ -36,7 +36,7 @@ impl Default for TextureDescriptor {
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::Rgba8UnormSrgb,
usage: TextureUsage::SAMPLED | TextureUsage::COPY_DST,
usage: TextureUsages::SAMPLED | TextureUsages::COPY_DST,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/texture/texture_dimension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl Default for TextureFormat {

bitflags::bitflags! {
#[repr(transparent)]
pub struct TextureUsage: u32 {
pub struct TextureUsages: u32 {
const COPY_SRC = 1;
const COPY_DST = 2;
const SAMPLED = 4;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bevy_winit = { path = "../bevy_winit", optional = true, version = "0.5.0" }
bevy_utils = { path = "../bevy_utils", version = "0.5.0" }

# other
wgpu = "0.9"
wgpu = { version = "0.10.0", features = ["spirv"] }
zicklag marked this conversation as resolved.
Show resolved Hide resolved
futures-lite = "1.4.0"
crossbeam-channel = "0.5.0"
crossbeam-utils = "0.8.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ impl WgpuResourceDiagnosticsPlugin {
DiagnosticId::from_u128(305855369913076220671125671543184691267);
pub const SHADER_MODULES: DiagnosticId =
DiagnosticId::from_u128(287681470908132753275843248383768232237);
pub const SWAP_CHAINS: DiagnosticId =
DiagnosticId::from_u128(199253035828743332241465305105689014605);
pub const SWAP_CHAIN_OUTPUTS: DiagnosticId =
pub const SURFACE_FRAMES: DiagnosticId =
DiagnosticId::from_u128(112048874168736161226721327099863374234);
pub const TEXTURES: DiagnosticId =
DiagnosticId::from_u128(305955424195390184883220102469231911115);
Expand All @@ -47,10 +45,8 @@ impl WgpuResourceDiagnosticsPlugin {
10,
));

diagnostics.add(Diagnostic::new(Self::SWAP_CHAINS, "swap_chains", 10));

diagnostics.add(Diagnostic::new(
Self::SWAP_CHAIN_OUTPUTS,
Self::SURFACE_FRAMES,
"swap_chain_outputs",
10,
));
Expand Down Expand Up @@ -99,19 +95,10 @@ impl WgpuResourceDiagnosticsPlugin {
);

diagnostics.add_measurement(
Self::SWAP_CHAINS,
render_resource_context
.resources
.window_swap_chains
.read()
.len() as f64,
);

diagnostics.add_measurement(
Self::SWAP_CHAIN_OUTPUTS,
Self::SURFACE_FRAMES,
render_resource_context
.resources
.swap_chain_frames
.surface_frames
.read()
.len() as f64,
);
Expand Down
3 changes: 0 additions & 3 deletions crates/bevy_wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ pub enum WgpuFeature {
TimestampQuery,
PipelineStatisticsQuery,
MappablePrimaryBuffers,
SampledTextureBindingArray,
SampledTextureArrayDynamicIndexing,
SampledTextureArrayNonUniformIndexing,
UnsizedBindingArray,
MultiDrawIndirect,
MultiDrawIndirectCount,
Expand Down
11 changes: 9 additions & 2 deletions crates/bevy_wgpu/src/renderer/wgpu_render_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,15 @@ fn get_texture_view<'a>(
panic!("Color attachment {} does not exist.", name);
}
},
TextureAttachment::Id(render_resource) => refs.textures.get(render_resource).unwrap_or_else(|| &refs.swap_chain_frames.get(render_resource).unwrap().output.view),
TextureAttachment::Input(_) => panic!("Encountered unset `TextureAttachment::Input`. The `RenderGraph` executor should always set `TextureAttachment::Inputs` to `TextureAttachment::RenderResource` before running. This is a bug, please report it!"),
TextureAttachment::Id(render_resource) => refs
.textures
.get(render_resource)
.unwrap_or_else(|| &refs.surface_frames.get(render_resource).unwrap().0),
TextureAttachment::Input(_) => panic!(
"Encountered unset `TextureAttachment::Input`. The `RenderGraph` executor should \
always set `TextureAttachment::Inputs` to `TextureAttachment::RenderResource` before \
running. This is a bug, please report it!"
),
}
}

Expand Down
60 changes: 28 additions & 32 deletions crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl WgpuRenderResourceContext {
y: source_origin[1],
z: source_origin[2],
},
aspect: wgpu::TextureAspect::All,
},
wgpu::ImageCopyTexture {
texture: destination,
Expand All @@ -103,6 +104,7 @@ impl WgpuRenderResourceContext {
y: destination_origin[1],
z: destination_origin[2],
},
aspect: wgpu::TextureAspect::All,
},
size.wgpu_into(),
)
Expand Down Expand Up @@ -134,6 +136,7 @@ impl WgpuRenderResourceContext {
y: source_origin[1],
z: source_origin[2],
},
aspect: wgpu::TextureAspect::All,
},
wgpu::ImageCopyBuffer {
buffer: destination,
Expand Down Expand Up @@ -181,6 +184,7 @@ impl WgpuRenderResourceContext {
y: destination_origin[1],
z: destination_origin[2],
},
aspect: wgpu::TextureAspect::All,
},
size.wgpu_into(),
);
Expand All @@ -206,11 +210,11 @@ impl WgpuRenderResourceContext {
let shader_stage = if binding.shader_stage
== BindingShaderStage::VERTEX | BindingShaderStage::FRAGMENT
{
wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT
wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT
} else if binding.shader_stage == BindingShaderStage::VERTEX {
wgpu::ShaderStage::VERTEX
wgpu::ShaderStages::VERTEX
} else if binding.shader_stage == BindingShaderStage::FRAGMENT {
wgpu::ShaderStage::FRAGMENT
wgpu::ShaderStages::FRAGMENT
} else {
panic!("Invalid binding shader stage.")
};
Expand All @@ -230,14 +234,15 @@ impl WgpuRenderResourceContext {
bind_group_layouts.insert(descriptor.id, bind_group_layout);
}

fn try_next_swap_chain_texture(&self, window_id: bevy_window::WindowId) -> Option<TextureId> {
let mut window_swap_chains = self.resources.window_swap_chains.write();
let mut swap_chain_outputs = self.resources.swap_chain_frames.write();
fn try_next_surface_frame(&self, window_id: bevy_window::WindowId) -> Option<TextureId> {
let mut window_surfaces = self.resources.window_surfaces.write();
let mut surface_frames = self.resources.surface_frames.write();

let window_swap_chain = window_swap_chains.get_mut(&window_id).unwrap();
let next_texture = window_swap_chain.get_current_frame().ok()?;
let window_surface = window_surfaces.get_mut(&window_id).unwrap();
let next_texture = window_surface.get_current_frame().ok()?;
let view = next_texture.output.texture.create_view(&Default::default());
let id = TextureId::new();
swap_chain_outputs.insert(id, next_texture);
surface_frames.insert(id, (view, next_texture));
Some(id)
}
}
Expand Down Expand Up @@ -339,7 +344,6 @@ impl RenderResourceContext for WgpuRenderResourceContext {
.create_shader_module(&wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::SpirV(spirv),
flags: Default::default(),
});
shader_modules.insert(shader_handle.clone_weak(), shader_module);
}
Expand All @@ -358,43 +362,35 @@ impl RenderResourceContext for WgpuRenderResourceContext {
self.create_shader_module_from_source(shader_handle, shader);
}

fn create_swap_chain(&self, window: &Window) {
fn configure_surface(&self, window: &Window) {
let surfaces = self.resources.window_surfaces.read();
let mut window_swap_chains = self.resources.window_swap_chains.write();

let swap_chain_descriptor: wgpu::SwapChainDescriptor = window.wgpu_into();
let surface_configuration: wgpu::SurfaceConfiguration = window.wgpu_into();
let surface = surfaces
.get(&window.id())
.expect("No surface found for window.");
let swap_chain = self
.device
.create_swap_chain(surface, &swap_chain_descriptor);

window_swap_chains.insert(window.id(), swap_chain);
surface.configure(&self.device, &surface_configuration);
}

fn next_swap_chain_texture(&self, window: &bevy_window::Window) -> TextureId {
if let Some(texture_id) = self.try_next_swap_chain_texture(window.id()) {
fn next_surface_frame(&self, window: &bevy_window::Window) -> TextureId {
if let Some(texture_id) = self.try_next_surface_frame(window.id()) {
texture_id
} else {
self.resources
.window_swap_chains
.write()
.remove(&window.id());
self.create_swap_chain(window);
self.try_next_swap_chain_texture(window.id())
self.resources.window_surfaces.write().remove(&window.id());
self.configure_surface(window);
self.try_next_surface_frame(window.id())
.expect("Failed to acquire next swap chain texture!")
}
}

fn drop_swap_chain_texture(&self, texture: TextureId) {
let mut swap_chain_outputs = self.resources.swap_chain_frames.write();
swap_chain_outputs.remove(&texture);
fn drop_surface_frame(&self, texture: TextureId) {
let mut surface_frames = self.resources.surface_frames.write();
surface_frames.remove(&texture);
}

fn drop_all_swap_chain_textures(&self) {
let mut swap_chain_outputs = self.resources.swap_chain_frames.write();
swap_chain_outputs.clear();
fn drop_all_surface_frames(&self) {
let mut surface_frames = self.resources.surface_frames.write();
surface_frames.clear();
}

fn set_asset_resource_untyped(
Expand Down
16 changes: 8 additions & 8 deletions crates/bevy_wgpu/src/wgpu_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ pub struct WgpuRenderer {
impl WgpuRenderer {
pub async fn new(options: WgpuOptions) -> Self {
let backend = match options.backend {
WgpuBackend::Auto => wgpu::BackendBit::PRIMARY,
WgpuBackend::Vulkan => wgpu::BackendBit::VULKAN,
WgpuBackend::Metal => wgpu::BackendBit::METAL,
WgpuBackend::Dx12 => wgpu::BackendBit::DX12,
WgpuBackend::Dx11 => wgpu::BackendBit::DX11,
WgpuBackend::Gl => wgpu::BackendBit::GL,
WgpuBackend::BrowserWgpu => wgpu::BackendBit::BROWSER_WEBGPU,
WgpuBackend::Auto => wgpu::Backends::PRIMARY,
WgpuBackend::Vulkan => wgpu::Backends::VULKAN,
WgpuBackend::Metal => wgpu::Backends::METAL,
WgpuBackend::Dx12 => wgpu::Backends::DX12,
WgpuBackend::Dx11 => wgpu::Backends::DX11,
WgpuBackend::Gl => wgpu::Backends::GL,
WgpuBackend::BrowserWgpu => wgpu::Backends::BROWSER_WEBGPU,
};
let instance = wgpu::Instance::new(backend);

Expand Down Expand Up @@ -129,7 +129,7 @@ impl WgpuRenderer {
let render_resource_context = world
.get_resource::<Box<dyn RenderResourceContext>>()
.unwrap();
render_resource_context.drop_all_swap_chain_textures();
render_resource_context.drop_all_surface_frames();
render_resource_context.remove_stale_bind_groups();
}
}
Loading