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

Enable wgpu device features and limits #1401

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
1 change: 1 addition & 0 deletions crates/bevy_wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ futures-lite = "1.4.0"
crossbeam-channel = "0.5.0"
crossbeam-utils = "0.8.1"
parking_lot = "0.11.0"
bitflags = "1"
71 changes: 70 additions & 1 deletion crates/bevy_wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ pub fn get_wgpu_render_system(resources: &mut Resources) -> impl FnMut(&mut Worl
}

#[derive(Default, Clone)]
pub struct WgpuOptions {
pub struct WgpuOptions<'a> {
pub name: Option<&'a str>,
pub backend: WgpuBackend,
pub power_pref: WgpuPowerOptions,
pub features: WgpuFeatures,
pub limits: WgpuLimits,
}

#[derive(Clone)]
Expand Down Expand Up @@ -95,3 +98,69 @@ impl Default for WgpuPowerOptions {
WgpuPowerOptions::HighPerformance
}
}

bitflags::bitflags! {
pub struct WgpuFeatures: u64 {
const DEPTH_CLAMPING = 0x0000_0000_0000_0001;
const TEXTURE_COMPRESSION_BC = 0x0000_0000_0000_0002;
const TIMESTAMP_QUERY = 0x0000_0000_0000_0004;
const PIPELINE_STATISTICS_QUERY = 0x0000_0000_0000_0008;
const MAPPABLE_PRIMARY_BUFFERS = 0x0000_0000_0001_0000;
const SAMPLED_TEXTURE_BINDING_ARRAY = 0x0000_0000_0002_0000;
const SAMPLED_TEXTURE_ARRAY_DYNAMIC_INDEXING = 0x0000_0000_0004_0000;
const SAMPLED_TEXTURE_ARRAY_NON_UNIFORM_INDEXING = 0x0000_0000_0008_0000;
const UNSIZED_BINDING_ARRAY = 0x0000_0000_0010_0000;
const MULTI_DRAW_INDIRECT = 0x0000_0000_0020_0000;
const MULTI_DRAW_INDIRECT_COUNT = 0x0000_0000_0040_0000;
const PUSH_CONSTANTS = 0x0000_0000_0080_0000;
const ADDRESS_MODE_CLAMP_TO_BORDER = 0x0000_0000_0100_0000;
const NON_FILL_POLYGON_MODE = 0x0000_0000_0200_0000;
const TEXTURE_COMPRESSION_ETC2 = 0x0000_0000_0400_0000;
const TEXTURE_COMPRESSION_ASTC_LDR = 0x0000_0000_0800_0000;
const TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES = 0x0000_0000_1000_0000;
const SHADER_FLOAT64 = 0x0000_0000_2000_0000;
const VERTEX_ATTRIBUTE_64BIT = 0x0000_0000_4000_0000;
const ALL_WEBGPU = 0x0000_0000_0000_FFFF;
const ALL_NATIVE = 0xFFFF_FFFF_FFFF_0000;
}
}

impl Default for WgpuFeatures {
fn default() -> Self {
WgpuFeatures::empty()
}
}

#[derive(Debug, Clone)]
pub struct WgpuLimits {
pub max_bind_groups: u32,
pub max_dynamic_uniform_buffers_per_pipeline_layout: u32,
pub max_dynamic_storage_buffers_per_pipeline_layout: u32,
pub max_sampled_textures_per_shader_stage: u32,
pub max_samplers_per_shader_stage: u32,
pub max_storage_buffers_per_shader_stage: u32,
pub max_storage_textures_per_shader_stage: u32,
pub max_uniform_buffers_per_shader_stage: u32,
pub max_uniform_buffer_binding_size: u32,
pub max_push_constant_size: u32,
}

impl Default for WgpuLimits {
fn default() -> Self {
let default = wgpu::Limits::default();
WgpuLimits {
max_bind_groups: default.max_bind_groups,
max_dynamic_uniform_buffers_per_pipeline_layout: default
.max_dynamic_uniform_buffers_per_pipeline_layout,
max_dynamic_storage_buffers_per_pipeline_layout: default
.max_dynamic_storage_buffers_per_pipeline_layout,
max_sampled_textures_per_shader_stage: default.max_sampled_textures_per_shader_stage,
max_samplers_per_shader_stage: default.max_samplers_per_shader_stage,
max_storage_buffers_per_shader_stage: default.max_storage_buffers_per_shader_stage,
max_storage_textures_per_shader_stage: default.max_storage_textures_per_shader_stage,
max_uniform_buffers_per_shader_stage: default.max_uniform_buffers_per_shader_stage,
max_uniform_buffer_binding_size: default.max_uniform_buffer_binding_size,
max_push_constant_size: default.max_push_constant_size,
}
}
}
8 changes: 5 additions & 3 deletions crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ pub struct WgpuRenderResourceContext {
pub resources: WgpuResources,
}

pub const BIND_BUFFER_ALIGNMENT: usize = 256;
pub const TEXTURE_ALIGNMENT: usize = 256;
pub const COPY_BYTES_PER_ROW_ALIGNMENT: usize = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as usize;
pub const BIND_BUFFER_ALIGNMENT: usize = wgpu::BIND_BUFFER_ALIGNMENT as usize;
pub const COPY_BUFFER_ALIGNMENT: usize = wgpu::COPY_BUFFER_ALIGNMENT as usize;
pub const PUSH_CONSTANT_ALIGNMENT: u32 = wgpu::PUSH_CONSTANT_ALIGNMENT;

impl WgpuRenderResourceContext {
pub fn new(device: Arc<wgpu::Device>) -> Self {
Expand Down Expand Up @@ -657,7 +659,7 @@ impl RenderResourceContext for WgpuRenderResourceContext {
}

fn get_aligned_texture_size(&self, size: usize) -> usize {
(size + TEXTURE_ALIGNMENT - 1) & !(TEXTURE_ALIGNMENT - 1)
(size + COPY_BYTES_PER_ROW_ALIGNMENT - 1) & !(COPY_BYTES_PER_ROW_ALIGNMENT - 1)
}

fn get_aligned_uniform_size(&self, size: usize, dynamic: bool) -> usize {
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_wgpu/src/wgpu_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
renderer::{WgpuRenderGraphExecutor, WgpuRenderResourceContext},
wgpu_type_converter::WgpuInto,
WgpuBackend, WgpuOptions, WgpuPowerOptions,
};
use bevy_app::{prelude::*, ManualEventReader};
Expand All @@ -21,7 +22,7 @@ pub struct WgpuRenderer {
}

impl WgpuRenderer {
pub async fn new(options: WgpuOptions) -> Self {
pub async fn new(options: WgpuOptions<'_>) -> Self {
let backend = match options.backend {
WgpuBackend::Auto => wgpu::BackendBit::PRIMARY,
WgpuBackend::Vulkan => wgpu::BackendBit::VULKAN,
Expand Down Expand Up @@ -53,9 +54,9 @@ impl WgpuRenderer {
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
label: options.name,
features: options.features.wgpu_into(),
limits: options.limits.wgpu_into(),
},
trace_path,
)
Expand Down
25 changes: 25 additions & 0 deletions crates/bevy_wgpu/src/wgpu_type_converter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::{WgpuFeatures, WgpuLimits};
use bevy_render::{
color::Color,
pass::{LoadOp, Operations},
Expand Down Expand Up @@ -647,3 +648,27 @@ impl WgpuFrom<&Window> for wgpu::SwapChainDescriptor {
}
}
}

impl WgpuFrom<WgpuFeatures> for wgpu::Features {
fn from(val: WgpuFeatures) -> Self {
wgpu::Features::from_bits(val.bits).unwrap()
}
}
impl WgpuFrom<WgpuLimits> for wgpu::Limits {
fn from(val: WgpuLimits) -> Self {
wgpu::Limits {
max_bind_groups: val.max_bind_groups,
max_dynamic_uniform_buffers_per_pipeline_layout: val
.max_dynamic_uniform_buffers_per_pipeline_layout,
max_dynamic_storage_buffers_per_pipeline_layout: val
.max_dynamic_storage_buffers_per_pipeline_layout,
max_sampled_textures_per_shader_stage: val.max_sampled_textures_per_shader_stage,
max_samplers_per_shader_stage: val.max_samplers_per_shader_stage,
max_storage_buffers_per_shader_stage: val.max_storage_buffers_per_shader_stage,
max_storage_textures_per_shader_stage: val.max_storage_textures_per_shader_stage,
max_uniform_buffers_per_shader_stage: val.max_uniform_buffers_per_shader_stage,
max_uniform_buffer_binding_size: val.max_uniform_buffer_binding_size,
max_push_constant_size: val.max_push_constant_size,
}
}
}