Skip to content

Commit bfd1d4b

Browse files
committed
Wgpu 0.15 (#7356)
# Objective Update Bevy to wgpu 0.15. ## Changelog - Update to wgpu 0.15, wgpu-hal 0.15.1, and naga 0.11 - Users can now use the [DirectX Shader Compiler](https://github.com/microsoft/DirectXShaderCompiler) (DXC) on Windows with DX12 for faster shader compilation and ShaderModel 6.0+ support (requires `dxcompiler.dll` and `dxil.dll`, which are included in DXC downloads from [here](https://github.com/microsoft/DirectXShaderCompiler/releases/latest)) ## Migration Guide ### WGSL Top-Level `let` is now `const` All top level constants are now declared with `const`, catching up with the wgsl spec. `let` is no longer allowed at the global scope, only within functions. ```diff -let SOME_CONSTANT = 12.0; +const SOME_CONSTANT = 12.0; ``` #### `TextureDescriptor` and `SurfaceConfiguration` now requires a `view_formats` field The new `view_formats` field in the `TextureDescriptor` is used to specify a list of formats the texture can be re-interpreted to in a texture view. Currently only changing srgb-ness is allowed (ex. `Rgba8Unorm` <=> `Rgba8UnormSrgb`). You should set `view_formats` to `&[]` (empty) unless you have a specific reason not to. #### The DirectX Shader Compiler (DXC) is now supported on DX12 DXC is now the default shader compiler when using the DX12 backend. DXC is Microsoft's replacement for their legacy FXC compiler, and is faster, less buggy, and allows for modern shader features to be used (ShaderModel 6.0+). DXC requires `dxcompiler.dll` and `dxil.dll` to be available, otherwise it will log a warning and fall back to FXC. You can get `dxcompiler.dll` and `dxil.dll` by downloading the latest release from [Microsoft's DirectXShaderCompiler github repo](https://github.com/microsoft/DirectXShaderCompiler/releases/latest) and copying them into your project's root directory. These must be included when you distribute your Bevy game/app/etc if you plan on supporting the DX12 backend and are using DXC. `WgpuSettings` now has a `dx12_shader_compiler` field which can be used to choose between either FXC or DXC (if you pass None for the paths for DXC, it will check for the .dlls in the working directory).
1 parent 3999365 commit bfd1d4b

File tree

21 files changed

+123
-56
lines changed

21 files changed

+123
-56
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Cargo.lock
77
/.idea
88
/.vscode
99
/benches/target
10+
dxcompiler.dll
11+
dxil.dll
1012

1113
# Generated by "examples/scene/scene.rs"
1214
assets/scenes/load_scene_example-new.scn.ron

crates/bevy_core_pipeline/src/bloom/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ fn prepare_bloom_textures(
580580
dimension: TextureDimension::D2,
581581
format: ViewTarget::TEXTURE_FORMAT_HDR,
582582
usage: TextureUsages::RENDER_ATTACHMENT | TextureUsages::TEXTURE_BINDING,
583+
view_formats: &[],
583584
};
584585

585586
texture_descriptor.label = Some("bloom_texture_a");

crates/bevy_core_pipeline/src/core_3d/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ pub fn prepare_core_3d_depth_textures(
308308
// PERF: vulkan docs recommend using 24 bit depth for better performance
309309
format: TextureFormat::Depth32Float,
310310
usage,
311+
view_formats: &[],
311312
};
312313

313314
texture_cache.get(&render_device, descriptor)

crates/bevy_core_pipeline/src/fxaa/fxaa.wgsl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,48 @@ var samp: sampler;
1515

1616
// Trims the algorithm from processing darks.
1717
#ifdef EDGE_THRESH_MIN_LOW
18-
let EDGE_THRESHOLD_MIN: f32 = 0.0833;
18+
const EDGE_THRESHOLD_MIN: f32 = 0.0833;
1919
#endif
2020

2121
#ifdef EDGE_THRESH_MIN_MEDIUM
22-
let EDGE_THRESHOLD_MIN: f32 = 0.0625;
22+
const EDGE_THRESHOLD_MIN: f32 = 0.0625;
2323
#endif
2424

2525
#ifdef EDGE_THRESH_MIN_HIGH
26-
let EDGE_THRESHOLD_MIN: f32 = 0.0312;
26+
const EDGE_THRESHOLD_MIN: f32 = 0.0312;
2727
#endif
2828

2929
#ifdef EDGE_THRESH_MIN_ULTRA
30-
let EDGE_THRESHOLD_MIN: f32 = 0.0156;
30+
const EDGE_THRESHOLD_MIN: f32 = 0.0156;
3131
#endif
3232

3333
#ifdef EDGE_THRESH_MIN_EXTREME
34-
let EDGE_THRESHOLD_MIN: f32 = 0.0078;
34+
const EDGE_THRESHOLD_MIN: f32 = 0.0078;
3535
#endif
3636

3737
// The minimum amount of local contrast required to apply algorithm.
3838
#ifdef EDGE_THRESH_LOW
39-
let EDGE_THRESHOLD_MAX: f32 = 0.250;
39+
const EDGE_THRESHOLD_MAX: f32 = 0.250;
4040
#endif
4141

4242
#ifdef EDGE_THRESH_MEDIUM
43-
let EDGE_THRESHOLD_MAX: f32 = 0.166;
43+
const EDGE_THRESHOLD_MAX: f32 = 0.166;
4444
#endif
4545

4646
#ifdef EDGE_THRESH_HIGH
47-
let EDGE_THRESHOLD_MAX: f32 = 0.125;
47+
const EDGE_THRESHOLD_MAX: f32 = 0.125;
4848
#endif
4949

5050
#ifdef EDGE_THRESH_ULTRA
51-
let EDGE_THRESHOLD_MAX: f32 = 0.063;
51+
const EDGE_THRESHOLD_MAX: f32 = 0.063;
5252
#endif
5353

5454
#ifdef EDGE_THRESH_EXTREME
55-
let EDGE_THRESHOLD_MAX: f32 = 0.031;
55+
const EDGE_THRESHOLD_MAX: f32 = 0.031;
5656
#endif
5757

58-
let ITERATIONS: i32 = 12; //default is 12
59-
let SUBPIXEL_QUALITY: f32 = 0.75;
58+
const ITERATIONS: i32 = 12; //default is 12
59+
const SUBPIXEL_QUALITY: f32 = 0.75;
6060
// #define QUALITY(q) ((q) < 5 ? 1.0 : ((q) > 5 ? ((q) < 10 ? 2.0 : ((q) < 11 ? 4.0 : 8.0)) : 1.5))
6161
fn QUALITY(q: i32) -> f32 {
6262
switch (q) {

crates/bevy_pbr/src/prepass/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ pub fn prepare_prepass_textures(
411411
usage: TextureUsages::COPY_DST
412412
| TextureUsages::RENDER_ATTACHMENT
413413
| TextureUsages::TEXTURE_BINDING,
414+
view_formats: &[],
414415
};
415416
texture_cache.get(&render_device, descriptor)
416417
})
@@ -432,6 +433,7 @@ pub fn prepare_prepass_textures(
432433
format: NORMAL_PREPASS_FORMAT,
433434
usage: TextureUsages::RENDER_ATTACHMENT
434435
| TextureUsages::TEXTURE_BINDING,
436+
view_formats: &[],
435437
},
436438
)
437439
})

crates/bevy_pbr/src/render/clustered_forward.wgsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn fragment_cluster_index(frag_coord: vec2<f32>, view_z: f32, is_orthographic: b
2727
}
2828

2929
// this must match CLUSTER_COUNT_SIZE in light.rs
30-
let CLUSTER_COUNT_SIZE = 9u;
30+
const CLUSTER_COUNT_SIZE = 9u;
3131
fn unpack_offset_and_counts(cluster_index: u32) -> vec3<u32> {
3232
#if AVAILABLE_STORAGE_BUFFER_BINDINGS >= 3
3333
return cluster_offsets_and_counts.data[cluster_index].xyz;

crates/bevy_pbr/src/render/light.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,7 @@ pub fn prepare_lights(
10431043
format: SHADOW_FORMAT,
10441044
label: Some("point_light_shadow_map_texture"),
10451045
usage: TextureUsages::RENDER_ATTACHMENT | TextureUsages::TEXTURE_BINDING,
1046+
view_formats: &[],
10461047
},
10471048
);
10481049
let directional_light_depth_texture = texture_cache.get(
@@ -1063,6 +1064,7 @@ pub fn prepare_lights(
10631064
format: SHADOW_FORMAT,
10641065
label: Some("directional_light_shadow_map_texture"),
10651066
usage: TextureUsages::RENDER_ATTACHMENT | TextureUsages::TEXTURE_BINDING,
1067+
view_formats: &[],
10661068
},
10671069
);
10681070
let mut view_lights = Vec::new();

crates/bevy_pbr/src/render/mesh_types.wgsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ struct SkinnedMesh {
1313
};
1414
#endif
1515

16-
let MESH_FLAGS_SHADOW_RECEIVER_BIT: u32 = 1u;
16+
const MESH_FLAGS_SHADOW_RECEIVER_BIT: u32 = 1u;
1717
// 2^31 - if the flag is set, the sign is positive, else it is negative
18-
let MESH_FLAGS_SIGN_DETERMINANT_MODEL_3X3_BIT: u32 = 2147483648u;
18+
const MESH_FLAGS_SIGN_DETERMINANT_MODEL_3X3_BIT: u32 = 2147483648u;

crates/bevy_pbr/src/render/mesh_view_types.wgsl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ struct PointLight {
2525
spot_light_tan_angle: f32,
2626
};
2727

28-
let POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT: u32 = 1u;
29-
let POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE: u32 = 2u;
28+
const POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT: u32 = 1u;
29+
const POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE: u32 = 2u;
3030

3131
struct DirectionalCascade {
3232
view_projection: mat4x4<f32>,
@@ -47,7 +47,7 @@ struct DirectionalLight {
4747
depth_texture_base_index: u32,
4848
};
4949

50-
let DIRECTIONAL_LIGHT_FLAGS_SHADOWS_ENABLED_BIT: u32 = 1u;
50+
const DIRECTIONAL_LIGHT_FLAGS_SHADOWS_ENABLED_BIT: u32 = 1u;
5151

5252
struct Lights {
5353
// NOTE: this array size must be kept in sync with the constants defined in bevy_pbr/src/render/light.rs
@@ -89,11 +89,11 @@ struct Fog {
8989
}
9090

9191
// Important: These must be kept in sync with `fog.rs`
92-
let FOG_MODE_OFF: u32 = 0u;
93-
let FOG_MODE_LINEAR: u32 = 1u;
94-
let FOG_MODE_EXPONENTIAL: u32 = 2u;
95-
let FOG_MODE_EXPONENTIAL_SQUARED: u32 = 3u;
96-
let FOG_MODE_ATMOSPHERIC: u32 = 4u;
92+
const FOG_MODE_OFF: u32 = 0u;
93+
const FOG_MODE_LINEAR: u32 = 1u;
94+
const FOG_MODE_EXPONENTIAL: u32 = 2u;
95+
const FOG_MODE_EXPONENTIAL_SQUARED: u32 = 3u;
96+
const FOG_MODE_ATMOSPHERIC: u32 = 4u;
9797

9898
#if AVAILABLE_STORAGE_BUFFER_BINDINGS >= 3
9999
struct PointLights {

crates/bevy_pbr/src/render/pbr_types.wgsl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ struct StandardMaterial {
1111
alpha_cutoff: f32,
1212
};
1313

14-
let STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT: u32 = 1u;
15-
let STANDARD_MATERIAL_FLAGS_EMISSIVE_TEXTURE_BIT: u32 = 2u;
16-
let STANDARD_MATERIAL_FLAGS_METALLIC_ROUGHNESS_TEXTURE_BIT: u32 = 4u;
17-
let STANDARD_MATERIAL_FLAGS_OCCLUSION_TEXTURE_BIT: u32 = 8u;
18-
let STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT: u32 = 16u;
19-
let STANDARD_MATERIAL_FLAGS_UNLIT_BIT: u32 = 32u;
20-
let STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP: u32 = 64u;
21-
let STANDARD_MATERIAL_FLAGS_FLIP_NORMAL_MAP_Y: u32 = 128u;
22-
let STANDARD_MATERIAL_FLAGS_FOG_ENABLED_BIT: u32 = 256u;
23-
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS: u32 = 3758096384u; // (0b111u32 << 29)
24-
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE: u32 = 0u; // (0u32 << 29)
25-
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK: u32 = 536870912u; // (1u32 << 29)
26-
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND: u32 = 1073741824u; // (2u32 << 29)
27-
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_PREMULTIPLIED: u32 = 1610612736u; // (3u32 << 29)
28-
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD: u32 = 2147483648u; // (4u32 << 29)
29-
let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MULTIPLY: u32 = 2684354560u; // (5u32 << 29)
14+
const STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT: u32 = 1u;
15+
const STANDARD_MATERIAL_FLAGS_EMISSIVE_TEXTURE_BIT: u32 = 2u;
16+
const STANDARD_MATERIAL_FLAGS_METALLIC_ROUGHNESS_TEXTURE_BIT: u32 = 4u;
17+
const STANDARD_MATERIAL_FLAGS_OCCLUSION_TEXTURE_BIT: u32 = 8u;
18+
const STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT: u32 = 16u;
19+
const STANDARD_MATERIAL_FLAGS_UNLIT_BIT: u32 = 32u;
20+
const STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP: u32 = 64u;
21+
const STANDARD_MATERIAL_FLAGS_FLIP_NORMAL_MAP_Y: u32 = 128u;
22+
const STANDARD_MATERIAL_FLAGS_FOG_ENABLED_BIT: u32 = 256u;
23+
const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS: u32 = 3758096384u; // (0b111u32 << 29)
24+
const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE: u32 = 0u; // (0u32 << 29)
25+
const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK: u32 = 536870912u; // (1u32 << 29)
26+
const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND: u32 = 1073741824u; // (2u32 << 29)
27+
const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_PREMULTIPLIED: u32 = 1610612736u; // (3u32 << 29)
28+
const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_ADD: u32 = 2147483648u; // (4u32 << 29)
29+
const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MULTIPLY: u32 = 2684354560u; // (5u32 << 29)
3030
// ↑ To calculate/verify the values above, use the following playground:
3131
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7792f8dd6fc6a8d4d0b6b1776898a7f4
3232

crates/bevy_pbr/src/render/utils.wgsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#define_import_path bevy_pbr::utils
22

3-
let PI: f32 = 3.141592653589793;
3+
const PI: f32 = 3.141592653589793;
44

55
fn hsv2rgb(hue: f32, saturation: f32, value: f32) -> vec3<f32> {
66
let rgb = clamp(

crates/bevy_render/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ bevy_tasks = { path = "../bevy_tasks", version = "0.9.0" }
5050
image = { version = "0.24", default-features = false }
5151

5252
# misc
53-
wgpu = { version = "0.14.0", features = ["spirv"] }
54-
wgpu-hal = "0.14.1"
53+
wgpu = { version = "0.15.0", features = ["spirv"] }
54+
wgpu-hal = "0.15.1"
5555
codespan-reporting = "0.11.0"
56-
naga = { version = "0.10.0", features = ["glsl-in", "spv-in", "spv-out", "wgsl-in", "wgsl-out"] }
56+
naga = { version = "0.11.0", features = ["glsl-in", "spv-in", "spv-out", "wgsl-in", "wgsl-out"] }
5757
serde = { version = "1", features = ["derive"] }
5858
bitflags = "1.2.1"
5959
smallvec = { version = "1.6", features = ["union", "const_generics"] }

crates/bevy_render/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,16 @@ impl Plugin for RenderPlugin {
148148
let primary_window = system_state.get(&app.world);
149149

150150
if let Some(backends) = self.wgpu_settings.backends {
151-
let instance = wgpu::Instance::new(backends);
151+
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
152+
backends,
153+
dx12_shader_compiler: self.wgpu_settings.dx12_shader_compiler.clone(),
154+
});
152155
let surface = primary_window.get_single().ok().map(|wrapper| unsafe {
153156
// SAFETY: Plugins should be set up on the main thread.
154157
let handle = wrapper.get_handle();
155-
instance.create_surface(&handle)
158+
instance
159+
.create_surface(&handle)
160+
.expect("Failed to create wgpu surface")
156161
});
157162

158163
let request_adapter_options = wgpu::RequestAdapterOptions {

crates/bevy_render/src/renderer/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ pub async fn initialize_renderer(
251251
max_buffer_size: limits
252252
.max_buffer_size
253253
.min(constrained_limits.max_buffer_size),
254+
max_bindings_per_bind_group: limits
255+
.max_bindings_per_bind_group
256+
.min(constrained_limits.max_bindings_per_bind_group),
254257
};
255258
}
256259

crates/bevy_render/src/settings.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::borrow::Cow;
22

3-
pub use wgpu::{Backends, Features as WgpuFeatures, Limits as WgpuLimits, PowerPreference};
3+
pub use wgpu::{
4+
Backends, Dx12Compiler, Features as WgpuFeatures, Limits as WgpuLimits, PowerPreference,
5+
};
46

57
/// Configures the priority used when automatically configuring the features/limits of `wgpu`.
68
#[derive(Clone)]
@@ -37,6 +39,8 @@ pub struct WgpuSettings {
3739
pub limits: WgpuLimits,
3840
/// The constraints on limits allowed regardless of what the adapter/backend supports
3941
pub constrained_limits: Option<WgpuLimits>,
42+
/// The shader compiler to use for the DX12 backend.
43+
pub dx12_shader_compiler: Dx12Compiler,
4044
}
4145

4246
impl Default for WgpuSettings {
@@ -65,6 +69,12 @@ impl Default for WgpuSettings {
6569
limits
6670
};
6771

72+
let dx12_compiler =
73+
wgpu::util::dx12_shader_compiler_from_env().unwrap_or(Dx12Compiler::Dxc {
74+
dxil_path: None,
75+
dxc_path: None,
76+
});
77+
6878
Self {
6979
device_label: Default::default(),
7080
backends,
@@ -74,6 +84,7 @@ impl Default for WgpuSettings {
7484
disabled_features: None,
7585
limits,
7686
constrained_limits: None,
87+
dx12_shader_compiler: dx12_compiler,
7788
}
7889
}
7990
}

crates/bevy_render/src/texture/image.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ impl Default for Image {
184184
mip_level_count: 1,
185185
sample_count: 1,
186186
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
187+
view_formats: &[],
187188
},
188189
sampler_descriptor: ImageSampler::Default,
189190
texture_view_descriptor: None,

crates/bevy_render/src/view/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ fn prepare_view_targets(
338338
format: main_texture_format,
339339
usage: TextureUsages::RENDER_ATTACHMENT
340340
| TextureUsages::TEXTURE_BINDING,
341+
// TODO: Consider changing this if main_texture_format is not sRGB
342+
view_formats: &[],
341343
};
342344
MainTargetTextures {
343345
a: texture_cache
@@ -370,6 +372,7 @@ fn prepare_view_targets(
370372
dimension: TextureDimension::D2,
371373
format: main_texture_format,
372374
usage: TextureUsages::RENDER_ATTACHMENT,
375+
view_formats: &[],
373376
},
374377
)
375378
.default_view

0 commit comments

Comments
 (0)