From 78fbbba5e928e4a6f15a03a1c04d06916a947673 Mon Sep 17 00:00:00 2001 From: Terrence Cole Date: Mon, 14 Oct 2019 18:57:05 -0700 Subject: [PATCH] Support Limits::max_bind_groups (#348) * Pass max_bind_count down into Binder. * Use the existing hub. * Remove accidental newline. --- Cargo.lock | 1 + wgpu-native/Cargo.toml | 1 + wgpu-native/src/command/bind.rs | 18 +++++++++++++----- wgpu-native/src/command/mod.rs | 1 + wgpu-native/src/command/render.rs | 3 ++- wgpu-native/src/device.rs | 2 ++ wgpu-native/src/instance.rs | 7 ++++++- wgpu-native/src/lib.rs | 1 + 8 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b96b76e51..5b740d5ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -924,6 +924,7 @@ dependencies = [ "rendy-descriptor 0.4.0 (git+https://github.com/amethyst/rendy?rev=e8ffcabc2bc74fbb282d4f71fa55c28d0ec31c86)", "rendy-memory 0.4.0 (git+https://github.com/amethyst/rendy?rev=e8ffcabc2bc74fbb282d4f71fa55c28d0ec31c86)", "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/wgpu-native/Cargo.toml b/wgpu-native/Cargo.toml index 349fed0b4..0b62ef017 100644 --- a/wgpu-native/Cargo.toml +++ b/wgpu-native/Cargo.toml @@ -35,6 +35,7 @@ raw-window-handle = "0.3" rendy-memory = { git = "https://github.com/amethyst/rendy", rev = "e8ffcabc2bc74fbb282d4f71fa55c28d0ec31c86" } rendy-descriptor = { git = "https://github.com/amethyst/rendy", rev = "e8ffcabc2bc74fbb282d4f71fa55c28d0ec31c86" } serde = { version = "1.0", features = ["serde_derive"], optional = true } +smallvec = "0.6" vec_map = "0.8" [target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies] diff --git a/wgpu-native/src/command/bind.rs b/wgpu-native/src/command/bind.rs index 308051539..ce7d77e21 100644 --- a/wgpu-native/src/command/bind.rs +++ b/wgpu-native/src/command/bind.rs @@ -13,13 +13,14 @@ use crate::{ }; use log::trace; +use smallvec::{smallvec, SmallVec}; use std::convert::identity; -pub const MAX_BIND_GROUPS: usize = 4; +pub const DEFAULT_BIND_GROUPS: usize = 4; type BindGroupMask = u8; -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct BindGroupPair { layout_id: BindGroupLayoutId, group_id: Stored, @@ -53,7 +54,7 @@ where } } -#[derive(Default, Debug)] +#[derive(Clone, Default, Debug)] pub struct BindGroupEntry { expected_layout_id: Option, provided: Option, @@ -140,10 +141,17 @@ impl BindGroupEntry { #[derive(Default, Debug)] pub struct Binder { pub(crate) pipeline_layout_id: Option, //TODO: strongly `Stored` - pub(crate) entries: [BindGroupEntry; MAX_BIND_GROUPS], + pub(crate) entries: SmallVec<[BindGroupEntry; DEFAULT_BIND_GROUPS]>, } impl Binder { + pub(crate) fn new(max_bind_groups: u32) -> Self { + Self { + pipeline_layout_id: None, + entries: smallvec![Default::default(); max_bind_groups as usize] + } + } + pub(crate) fn reset_expectations(&mut self, length: usize) { for entry in self.entries[length ..].iter_mut() { entry.expected_layout_id = None; @@ -175,7 +183,7 @@ impl Binder { let compatible_count = self.compatible_count(); if index < compatible_count { let end = compatible_count - .min(if was_compatible { index + 1 } else { MAX_BIND_GROUPS }); + .min(if was_compatible { index + 1 } else { self.entries.len() }); trace!("\t\tbinding up to {}", end); Some(( self.pipeline_layout_id?, diff --git a/wgpu-native/src/command/mod.rs b/wgpu-native/src/command/mod.rs index 3e42a975f..ba6e0bbe8 100644 --- a/wgpu-native/src/command/mod.rs +++ b/wgpu-native/src/command/mod.rs @@ -712,6 +712,7 @@ pub fn command_encoder_begin_render_pass( }, context, sample_count, + cmb.features.max_bind_groups, ) }; hub.render_passes.register_identity(id_in, pass, &mut token) diff --git a/wgpu-native/src/command/render.rs b/wgpu-native/src/command/render.rs index f3d5b804d..35dc72ca3 100644 --- a/wgpu-native/src/command/render.rs +++ b/wgpu-native/src/command/render.rs @@ -136,12 +136,13 @@ impl RenderPass { cmb_id: Stored, context: RenderPassContext, sample_count: u8, + max_bind_groups: u32, ) -> Self { RenderPass { raw, cmb_id, context, - binder: Binder::default(), + binder: Binder::new(max_bind_groups), trackers: TrackerSet::new(B::VARIANT), blend_color_status: OptionalState::Unused, stencil_reference_status: OptionalState::Unused, diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 722e5638b..c8bebc958 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -513,6 +513,7 @@ impl Device { queue_group: hal::queue::QueueGroup, mem_props: hal::adapter::MemoryProperties, supports_texture_d24_s8: bool, + max_bind_groups: u32, ) -> Self { // don't start submission index at zero let life_guard = LifeGuard::new(); @@ -559,6 +560,7 @@ impl Device { ready_to_map: Vec::new(), }), features: Features { + max_bind_groups, supports_texture_d24_s8, } } diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 7d077021d..75a1e50ec 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -429,7 +429,7 @@ pub extern "C" fn wgpu_request_adapter(desc: Option<&RequestAdapterOptions>) -> pub fn adapter_request_device( adapter_id: AdapterId, - _desc: &DeviceDescriptor, + desc: &DeviceDescriptor, id_in: Input, ) -> Output { let hub = B::hub(); @@ -462,6 +462,10 @@ pub fn adapter_request_device( BIND_BUFFER_ALIGNMENT % limits.min_uniform_buffer_offset_alignment, "Adapter uniform buffer offset alignment not compatible with WGPU" ); + assert!( + u32::from(limits.max_bound_descriptor_sets) >= desc.limits.max_bind_groups, + "Adapter does not support the requested max_bind_groups" + ); let mem_props = adapter.physical_device.memory_properties(); @@ -477,6 +481,7 @@ pub fn adapter_request_device( gpu.queue_groups.swap_remove(0), mem_props, supports_texture_d24_s8, + desc.limits.max_bind_groups, ) }; diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index 335c2106f..7bdf0b759 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -223,5 +223,6 @@ macro_rules! gfx_select { #[derive(Clone, Copy, Debug)] pub(crate) struct Features { + pub max_bind_groups: u32, pub supports_texture_d24_s8: bool, }