Skip to content

Commit 6e14ed2

Browse files
committed
Enable wgpu device limits (#1544)
Follow up on #547 and #1401 Co-authored-by: Zhixing Zhang <me@neoto.xin>
1 parent 6a0968b commit 6e14ed2

File tree

4 files changed

+110
-50
lines changed

4 files changed

+110
-50
lines changed

crates/bevy_wgpu/src/lib.rs

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use bevy_render::{
1717
};
1818
use futures_lite::future;
1919
use renderer::WgpuRenderResourceContext;
20+
use std::borrow::Cow;
2021

2122
#[derive(Clone, Copy)]
2223
pub enum WgpuFeature {
@@ -41,54 +42,43 @@ pub enum WgpuFeature {
4142
VertexAttribute64Bit,
4243
}
4344

44-
impl From<WgpuFeature> for wgpu::Features {
45-
fn from(value: WgpuFeature) -> Self {
46-
match value {
47-
WgpuFeature::DepthClamping => wgpu::Features::DEPTH_CLAMPING,
48-
WgpuFeature::TextureCompressionBc => wgpu::Features::TEXTURE_COMPRESSION_BC,
49-
WgpuFeature::TimestampQuery => wgpu::Features::TIMESTAMP_QUERY,
50-
WgpuFeature::PipelineStatisticsQuery => wgpu::Features::PIPELINE_STATISTICS_QUERY,
51-
WgpuFeature::MappablePrimaryBuffers => wgpu::Features::MAPPABLE_PRIMARY_BUFFERS,
52-
WgpuFeature::SampledTextureBindingArray => {
53-
wgpu::Features::SAMPLED_TEXTURE_BINDING_ARRAY
54-
}
55-
WgpuFeature::SampledTextureArrayDynamicIndexing => {
56-
wgpu::Features::SAMPLED_TEXTURE_ARRAY_DYNAMIC_INDEXING
57-
}
58-
WgpuFeature::SampledTextureArrayNonUniformIndexing => {
59-
wgpu::Features::SAMPLED_TEXTURE_ARRAY_NON_UNIFORM_INDEXING
60-
}
61-
WgpuFeature::UnsizedBindingArray => wgpu::Features::UNSIZED_BINDING_ARRAY,
62-
WgpuFeature::MultiDrawIndirect => wgpu::Features::MULTI_DRAW_INDIRECT,
63-
WgpuFeature::MultiDrawIndirectCount => wgpu::Features::MULTI_DRAW_INDIRECT_COUNT,
64-
WgpuFeature::PushConstants => wgpu::Features::PUSH_CONSTANTS,
65-
WgpuFeature::AddressModeClampToBorder => wgpu::Features::ADDRESS_MODE_CLAMP_TO_BORDER,
66-
WgpuFeature::NonFillPolygonMode => wgpu::Features::NON_FILL_POLYGON_MODE,
67-
WgpuFeature::TextureCompressionEtc2 => wgpu::Features::TEXTURE_COMPRESSION_ETC2,
68-
WgpuFeature::TextureCompressionAstcLdr => wgpu::Features::TEXTURE_COMPRESSION_ASTC_LDR,
69-
WgpuFeature::TextureAdapterSpecificFormatFeatures => {
70-
wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES
71-
}
72-
WgpuFeature::ShaderFloat64 => wgpu::Features::SHADER_FLOAT64,
73-
WgpuFeature::VertexAttribute64Bit => wgpu::Features::VERTEX_ATTRIBUTE_64BIT,
74-
}
75-
}
45+
#[derive(Default, Clone)]
46+
pub struct WgpuFeatures {
47+
pub features: Vec<WgpuFeature>,
7648
}
7749

78-
impl From<WgpuFeatures> for wgpu::Features {
79-
fn from(features: WgpuFeatures) -> Self {
80-
features
81-
.features
82-
.iter()
83-
.fold(wgpu::Features::empty(), |wgpu_features, feature| {
84-
wgpu_features | (*feature).into()
85-
})
86-
}
50+
#[derive(Debug, Clone)]
51+
pub struct WgpuLimits {
52+
pub max_bind_groups: u32,
53+
pub max_dynamic_uniform_buffers_per_pipeline_layout: u32,
54+
pub max_dynamic_storage_buffers_per_pipeline_layout: u32,
55+
pub max_sampled_textures_per_shader_stage: u32,
56+
pub max_samplers_per_shader_stage: u32,
57+
pub max_storage_buffers_per_shader_stage: u32,
58+
pub max_storage_textures_per_shader_stage: u32,
59+
pub max_uniform_buffers_per_shader_stage: u32,
60+
pub max_uniform_buffer_binding_size: u32,
61+
pub max_push_constant_size: u32,
8762
}
8863

89-
#[derive(Default, Clone)]
90-
pub struct WgpuFeatures {
91-
pub features: Vec<WgpuFeature>,
64+
impl Default for WgpuLimits {
65+
fn default() -> Self {
66+
let default = wgpu::Limits::default();
67+
WgpuLimits {
68+
max_bind_groups: default.max_bind_groups,
69+
max_dynamic_uniform_buffers_per_pipeline_layout: default
70+
.max_dynamic_uniform_buffers_per_pipeline_layout,
71+
max_dynamic_storage_buffers_per_pipeline_layout: default
72+
.max_dynamic_storage_buffers_per_pipeline_layout,
73+
max_sampled_textures_per_shader_stage: default.max_sampled_textures_per_shader_stage,
74+
max_samplers_per_shader_stage: default.max_samplers_per_shader_stage,
75+
max_storage_buffers_per_shader_stage: default.max_storage_buffers_per_shader_stage,
76+
max_storage_textures_per_shader_stage: default.max_storage_textures_per_shader_stage,
77+
max_uniform_buffers_per_shader_stage: default.max_uniform_buffers_per_shader_stage,
78+
max_uniform_buffer_binding_size: default.max_uniform_buffer_binding_size,
79+
max_push_constant_size: default.max_push_constant_size,
80+
}
81+
}
9282
}
9383

9484
#[derive(Default)]
@@ -120,9 +110,11 @@ pub fn get_wgpu_render_system(resources: &mut Resources) -> impl FnMut(&mut Worl
120110

121111
#[derive(Default, Clone)]
122112
pub struct WgpuOptions {
113+
pub device_label: Option<Cow<'static, str>>,
123114
pub backend: WgpuBackend,
124115
pub power_pref: WgpuPowerOptions,
125116
pub features: WgpuFeatures,
117+
pub limits: WgpuLimits,
126118
}
127119

128120
#[derive(Clone)]

crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ pub struct WgpuRenderResourceContext {
2525
pub resources: WgpuResources,
2626
}
2727

28-
pub const BIND_BUFFER_ALIGNMENT: usize = 256;
29-
pub const TEXTURE_ALIGNMENT: usize = 256;
28+
pub const COPY_BYTES_PER_ROW_ALIGNMENT: usize = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as usize;
29+
pub const BIND_BUFFER_ALIGNMENT: usize = wgpu::BIND_BUFFER_ALIGNMENT as usize;
30+
pub const COPY_BUFFER_ALIGNMENT: usize = wgpu::COPY_BUFFER_ALIGNMENT as usize;
31+
pub const PUSH_CONSTANT_ALIGNMENT: u32 = wgpu::PUSH_CONSTANT_ALIGNMENT;
3032

3133
impl WgpuRenderResourceContext {
3234
pub fn new(device: Arc<wgpu::Device>) -> Self {
@@ -657,7 +659,7 @@ impl RenderResourceContext for WgpuRenderResourceContext {
657659
}
658660

659661
fn get_aligned_texture_size(&self, size: usize) -> usize {
660-
(size + TEXTURE_ALIGNMENT - 1) & !(TEXTURE_ALIGNMENT - 1)
662+
(size + COPY_BYTES_PER_ROW_ALIGNMENT - 1) & !(COPY_BYTES_PER_ROW_ALIGNMENT - 1)
661663
}
662664

663665
fn get_aligned_uniform_size(&self, size: usize, dynamic: bool) -> usize {

crates/bevy_wgpu/src/wgpu_renderer.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
22
renderer::{WgpuRenderGraphExecutor, WgpuRenderResourceContext},
3+
wgpu_type_converter::WgpuInto,
34
WgpuBackend, WgpuOptions, WgpuPowerOptions,
45
};
56
use bevy_app::{prelude::*, ManualEventReader};
@@ -53,9 +54,9 @@ impl WgpuRenderer {
5354
let (device, queue) = adapter
5455
.request_device(
5556
&wgpu::DeviceDescriptor {
56-
label: None,
57-
features: options.features.into(),
58-
limits: wgpu::Limits::default(),
57+
label: options.device_label.as_ref().map(|a| a.as_ref()),
58+
features: options.features.wgpu_into(),
59+
limits: options.limits.wgpu_into(),
5960
},
6061
trace_path,
6162
)

crates/bevy_wgpu/src/wgpu_type_converter.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::{WgpuFeature, WgpuFeatures, WgpuLimits};
12
use bevy_render::{
23
color::Color,
34
pass::{LoadOp, Operations},
@@ -647,3 +648,67 @@ impl WgpuFrom<&Window> for wgpu::SwapChainDescriptor {
647648
}
648649
}
649650
}
651+
652+
impl WgpuFrom<WgpuFeature> for wgpu::Features {
653+
fn from(value: WgpuFeature) -> Self {
654+
match value {
655+
WgpuFeature::DepthClamping => wgpu::Features::DEPTH_CLAMPING,
656+
WgpuFeature::TextureCompressionBc => wgpu::Features::TEXTURE_COMPRESSION_BC,
657+
WgpuFeature::TimestampQuery => wgpu::Features::TIMESTAMP_QUERY,
658+
WgpuFeature::PipelineStatisticsQuery => wgpu::Features::PIPELINE_STATISTICS_QUERY,
659+
WgpuFeature::MappablePrimaryBuffers => wgpu::Features::MAPPABLE_PRIMARY_BUFFERS,
660+
WgpuFeature::SampledTextureBindingArray => {
661+
wgpu::Features::SAMPLED_TEXTURE_BINDING_ARRAY
662+
}
663+
WgpuFeature::SampledTextureArrayDynamicIndexing => {
664+
wgpu::Features::SAMPLED_TEXTURE_ARRAY_DYNAMIC_INDEXING
665+
}
666+
WgpuFeature::SampledTextureArrayNonUniformIndexing => {
667+
wgpu::Features::SAMPLED_TEXTURE_ARRAY_NON_UNIFORM_INDEXING
668+
}
669+
WgpuFeature::UnsizedBindingArray => wgpu::Features::UNSIZED_BINDING_ARRAY,
670+
WgpuFeature::MultiDrawIndirect => wgpu::Features::MULTI_DRAW_INDIRECT,
671+
WgpuFeature::MultiDrawIndirectCount => wgpu::Features::MULTI_DRAW_INDIRECT_COUNT,
672+
WgpuFeature::PushConstants => wgpu::Features::PUSH_CONSTANTS,
673+
WgpuFeature::AddressModeClampToBorder => wgpu::Features::ADDRESS_MODE_CLAMP_TO_BORDER,
674+
WgpuFeature::NonFillPolygonMode => wgpu::Features::NON_FILL_POLYGON_MODE,
675+
WgpuFeature::TextureCompressionEtc2 => wgpu::Features::TEXTURE_COMPRESSION_ETC2,
676+
WgpuFeature::TextureCompressionAstcLdr => wgpu::Features::TEXTURE_COMPRESSION_ASTC_LDR,
677+
WgpuFeature::TextureAdapterSpecificFormatFeatures => {
678+
wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES
679+
}
680+
WgpuFeature::ShaderFloat64 => wgpu::Features::SHADER_FLOAT64,
681+
WgpuFeature::VertexAttribute64Bit => wgpu::Features::VERTEX_ATTRIBUTE_64BIT,
682+
}
683+
}
684+
}
685+
686+
impl WgpuFrom<WgpuFeatures> for wgpu::Features {
687+
fn from(features: WgpuFeatures) -> Self {
688+
features
689+
.features
690+
.iter()
691+
.fold(wgpu::Features::empty(), |wgpu_features, feature| {
692+
wgpu_features | (*feature).wgpu_into()
693+
})
694+
}
695+
}
696+
697+
impl WgpuFrom<WgpuLimits> for wgpu::Limits {
698+
fn from(val: WgpuLimits) -> Self {
699+
wgpu::Limits {
700+
max_bind_groups: val.max_bind_groups,
701+
max_dynamic_uniform_buffers_per_pipeline_layout: val
702+
.max_dynamic_uniform_buffers_per_pipeline_layout,
703+
max_dynamic_storage_buffers_per_pipeline_layout: val
704+
.max_dynamic_storage_buffers_per_pipeline_layout,
705+
max_sampled_textures_per_shader_stage: val.max_sampled_textures_per_shader_stage,
706+
max_samplers_per_shader_stage: val.max_samplers_per_shader_stage,
707+
max_storage_buffers_per_shader_stage: val.max_storage_buffers_per_shader_stage,
708+
max_storage_textures_per_shader_stage: val.max_storage_textures_per_shader_stage,
709+
max_uniform_buffers_per_shader_stage: val.max_uniform_buffers_per_shader_stage,
710+
max_uniform_buffer_binding_size: val.max_uniform_buffer_binding_size,
711+
max_push_constant_size: val.max_push_constant_size,
712+
}
713+
}
714+
}

0 commit comments

Comments
 (0)