use std::cmp::min;
use crate::{
command::FamilyId,
core::DeviceId,
memory::{DynamicConfig, HeapsConfig, LinearConfig},
};
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Config<D = BasicDevicesConfigure, H = BasicHeapsConfigure, Q = OneGraphicsQueue> {
pub devices: D,
pub heaps: H,
pub queues: Q,
}
pub unsafe trait QueuesConfigure {
type Priorities: AsRef<[f32]>;
type Families: IntoIterator<Item = (FamilyId, Self::Priorities)>;
fn configure(
&self,
device: DeviceId,
families: &[impl rendy_core::hal::queue::QueueFamily],
) -> Self::Families;
}
#[derive(Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OneGraphicsQueue;
unsafe impl QueuesConfigure for OneGraphicsQueue {
type Priorities = [f32; 1];
type Families = Option<(FamilyId, [f32; 1])>;
fn configure(
&self,
device: DeviceId,
families: &[impl rendy_core::hal::queue::QueueFamily],
) -> Option<(FamilyId, [f32; 1])> {
families
.iter()
.find(|f| f.queue_type().supports_graphics() && f.max_queues() > 0)
.map(|f| {
(
FamilyId {
device,
index: f.id().0,
},
[1.0],
)
})
}
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SavedQueueConfig(Vec<(usize, Vec<f32>)>);
unsafe impl QueuesConfigure for SavedQueueConfig {
type Priorities = Vec<f32>;
type Families = Vec<(FamilyId, Vec<f32>)>;
fn configure(
&self,
device: DeviceId,
_: &[impl rendy_core::hal::queue::QueueFamily],
) -> Vec<(FamilyId, Vec<f32>)> {
self.0
.iter()
.map(|(id, vec)| (FamilyId { device, index: *id }, vec.clone()))
.collect()
}
}
pub unsafe trait HeapsConfigure {
type Types: IntoIterator<Item = (rendy_core::hal::memory::Properties, u32, HeapsConfig)>;
type Heaps: IntoIterator<Item = u64>;
fn configure(
&self,
properties: &rendy_core::hal::adapter::MemoryProperties,
) -> (Self::Types, Self::Heaps);
}
#[derive(Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BasicHeapsConfigure;
unsafe impl HeapsConfigure for BasicHeapsConfigure {
type Types = Vec<(rendy_core::hal::memory::Properties, u32, HeapsConfig)>;
type Heaps = Vec<u64>;
fn configure(
&self,
properties: &rendy_core::hal::adapter::MemoryProperties,
) -> (Self::Types, Self::Heaps) {
let _1mb = 1024 * 1024;
let _32mb = 32 * _1mb;
let _128mb = 128 * _1mb;
let types = properties
.memory_types
.iter()
.map(|mt| {
let config = HeapsConfig {
linear: if mt
.properties
.contains(rendy_core::hal::memory::Properties::CPU_VISIBLE)
{
Some(LinearConfig {
linear_size: min(_128mb, properties.memory_heaps[mt.heap_index] / 16),
})
} else {
None
},
dynamic: Some(DynamicConfig {
block_size_granularity: 256.min(
(properties.memory_heaps[mt.heap_index] / 4096).next_power_of_two(),
),
min_device_allocation: _1mb
.min(properties.memory_heaps[mt.heap_index] / 1048)
.next_power_of_two(),
max_chunk_size: _32mb.min(
(properties.memory_heaps[mt.heap_index] / 128).next_power_of_two(),
),
}),
};
(mt.properties, mt.heap_index as u32, config)
})
.collect();
let heaps = properties.memory_heaps.iter().cloned().collect();
(types, heaps)
}
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SavedHeapsConfig {
types: Vec<(rendy_core::hal::memory::Properties, u32, HeapsConfig)>,
heaps: Vec<u64>,
}
unsafe impl HeapsConfigure for SavedHeapsConfig {
type Types = Vec<(rendy_core::hal::memory::Properties, u32, HeapsConfig)>;
type Heaps = Vec<u64>;
fn configure(
&self,
_properties: &rendy_core::hal::adapter::MemoryProperties,
) -> (Self::Types, Self::Heaps) {
(self.types.clone(), self.heaps.clone())
}
}
pub trait DevicesConfigure {
fn pick<B>(&self, adapters: &[rendy_core::hal::adapter::Adapter<B>]) -> usize
where
B: rendy_core::hal::Backend;
}
#[derive(Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BasicDevicesConfigure;
impl DevicesConfigure for BasicDevicesConfigure {
fn pick<B>(&self, adapters: &[rendy_core::hal::adapter::Adapter<B>]) -> usize
where
B: rendy_core::hal::Backend,
{
adapters
.iter()
.enumerate()
.min_by_key(|(_, adapter)| match adapter.info.device_type {
rendy_core::hal::adapter::DeviceType::DiscreteGpu => 0,
rendy_core::hal::adapter::DeviceType::IntegratedGpu => 1,
rendy_core::hal::adapter::DeviceType::VirtualGpu => 2,
rendy_core::hal::adapter::DeviceType::Cpu => 3,
_ => 4,
})
.expect("No adapters present")
.0
}
}