Skip to content

Commit

Permalink
Merge branch 'master' into winit-0.29
Browse files Browse the repository at this point in the history
  • Loading branch information
attackgoat committed Dec 28, 2023
2 parents b1940ea + 704760b commit 028bc0f
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 85 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Changed `KeyBuf` implementation functions to take values instead of borrows
- Updated `winit` to v0.29

### Changed

- Upgraded to spirq v1.0.2

## [0.9.0] - 2023-09-07

### Fixed
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ profile-with-tracy = ["profiling/profile-with-tracy"]
ash = ">=0.37.1"
ash-window = "0.12"
derive_builder = "0.12"
gpu-allocator = "0.22"
gpu-allocator = "0.24"
log = "0.4"
ordered-float = "3.4"
ordered-float = "4.1"
parking_lot = "0.12"
paste = "1.0"
profiling = "1.0"
raw-window-handle = "0.5"
spirq = ">=0.6.2"
spirq = "1.0.2"
vk-sync = { version = "0.4.0", package = "vk-sync-fork" } # // SEE: https://github.com/gwihlidal/vk-sync-rs/pull/4 -> https://github.com/expenses/vk-sync-rs
winit = { version = "0.29", features = ["rwh_05"] }

Expand Down
4 changes: 3 additions & 1 deletion contrib/screen-13-hot/examples/glsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ lazy_static! {
fn main() -> Result<(), DisplayError> {
pretty_env_logger::init();

let event_loop = EventLoop::new().build()?;
let event_loop = EventLoop::new()
.desired_surface_format(|formats| EventLoopBuilder::linear_surface_format(formats).unwrap())
.build()?;

// Create a compute pipeline - the same as normal except for "Hot" prefixes and we provide the
// shader source code path instead of the shader source code bytes
Expand Down
21 changes: 1 addition & 20 deletions examples/imgui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,7 @@ fn main() -> Result<(), DisplayError> {

// Screen 13 things we need for this demo
let event_loop = EventLoop::new()
.desired_surface_format(|formats| {
// HACK: Pick non-sRGB until the API for selecting it is reworked to include a device
// instance
for (
idx,
vk::SurfaceFormatKHR {
color_space,
format,
},
) in formats.iter().copied().enumerate()
{
if format == vk::Format::R8G8B8A8_UNORM
&& color_space == vk::ColorSpaceKHR::SRGB_NONLINEAR
{
return idx;
}
}

0
})
.desired_surface_format(|formats| EventLoopBuilder::linear_surface_format(formats).unwrap())
.desired_swapchain_image_count(2)
.build()?;
let display = ComputePresenter::new(&event_loop.device)?;
Expand Down
4 changes: 3 additions & 1 deletion examples/rt_triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ fn create_ray_trace_pipeline(device: &Arc<Device>) -> Result<Arc<RayTracePipelin
fn main() -> anyhow::Result<()> {
pretty_env_logger::init();

let event_loop = EventLoop::new().build()?;
let event_loop = EventLoop::new()
.desired_surface_format(|formats| EventLoopBuilder::linear_surface_format(formats).unwrap())
.build()?;
let mut pool = HashPool::new(&event_loop.device);

// ------------------------------------------------------------------------------------------ //
Expand Down
3 changes: 1 addition & 2 deletions examples/skeletal-anim/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,7 @@ impl Model {

assert!(indices.index_count() < u16::MAX as usize);

let indices = indices
.as_u16().unwrap();
let indices = indices.as_u16().unwrap();
let index_data = cast_slice(&indices);
let vertex_data = part.vertex_data();

Expand Down
21 changes: 1 addition & 20 deletions examples/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,7 @@ fn main() -> anyhow::Result<()> {
// Create Screen 13 things any similar program might need
let event_loop = EventLoop::new()
.window(|builder| builder.with_inner_size(LogicalSize::new(1024.0f64, 768.0f64)))
.desired_surface_format(|formats| {
// HACK: Pick non-sRGB until the API for selecting it is reworked to include a device
// instance
for (
idx,
vk::SurfaceFormatKHR {
color_space,
format,
},
) in formats.iter().copied().enumerate()
{
if format == vk::Format::R8G8B8A8_UNORM
&& color_space == vk::ColorSpaceKHR::SRGB_NONLINEAR
{
return idx;
}
}

0
})
.desired_surface_format(|formats| EventLoopBuilder::linear_surface_format(formats).unwrap())
.build()?;
let display = ComputePresenter::new(&event_loop.device)?;
let mut imgui = ImGui::new(&event_loop.device);
Expand Down
1 change: 1 addition & 0 deletions src/driver/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ impl Device {
..Default::default()
},
buffer_device_address: true,
allocation_sizes: Default::default(),
})
.map_err(|err| {
warn!("{err}");
Expand Down
32 changes: 22 additions & 10 deletions src/driver/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use {
log::{debug, error, trace, warn},
ordered_float::OrderedFloat,
spirq::{
ty::{ScalarType, Type},
DescriptorType, EntryPoint, ReflectConfig, Variable,
entry_point::EntryPoint,
ty::{DescriptorType, ScalarType, SpirvType, Type},
var::Variable,
ReflectConfig,
},
std::{
collections::{BTreeMap, HashMap},
Expand Down Expand Up @@ -914,7 +916,9 @@ impl Shader {
})
.flatten()
.map(|push_const| {
push_const.offset..push_const.offset + push_const.ty.nbyte().unwrap_or_default()
let offset = push_const.offset.unwrap_or_default();
let size = push_const.ty.nbyte().unwrap_or_default();
offset..offset + size
})
.reduce(|a, b| a.start.min(b.start)..a.end.max(b.end))
.map(|push_const| vk::PushConstantRange {
Expand All @@ -938,13 +942,17 @@ impl Shader {
spec.constant_id,
spec_info.data[spec.offset as usize..spec.offset as usize + spec.size]
.try_into()
.map_err(|_| DriverError::InvalidData)?,
.map_err(|err| {
error!("Unable to specialize spirv: {err}");

DriverError::InvalidData
})?,
);
}
}

let entry_points = config.reflect().map_err(|_| {
error!("Unable to reflect spirv");
let entry_points = config.reflect().map_err(|err| {
error!("Unable to reflect spirv: {err}");

DriverError::InvalidData
})?;
Expand All @@ -968,21 +976,25 @@ impl Shader {

fn scalar_format(ty: &ScalarType, byte_len: u32) -> vk::Format {
match ty {
ScalarType::Float(_) => match byte_len {
ScalarType::Float { .. } => match byte_len {
4 => vk::Format::R32_SFLOAT,
8 => vk::Format::R32G32_SFLOAT,
12 => vk::Format::R32G32B32_SFLOAT,
16 => vk::Format::R32G32B32A32_SFLOAT,
_ => unimplemented!("byte_len {byte_len}"),
},
ScalarType::Signed(_) => match byte_len {
ScalarType::Integer {
is_signed: true, ..
} => match byte_len {
4 => vk::Format::R32_SINT,
8 => vk::Format::R32G32_SINT,
12 => vk::Format::R32G32B32_SINT,
16 => vk::Format::R32G32B32A32_SINT,
_ => unimplemented!("byte_len {byte_len}"),
},
ScalarType::Unsigned(_) => match byte_len {
ScalarType::Integer {
is_signed: false, ..
} => match byte_len {
4 => vk::Format::R32_UINT,
8 => vk::Format::R32G32_UINT,
12 => vk::Format::R32G32B32_UINT,
Expand Down Expand Up @@ -1032,7 +1044,7 @@ impl Shader {
location,
binding,
format: match ty {
Type::Scalar(ty) => scalar_format(ty, ty.nbyte() as _),
Type::Scalar(ty) => scalar_format(ty, ty.nbyte().unwrap_or_default() as _),
Type::Vector(ty) => scalar_format(&ty.scalar_ty, byte_stride),
_ => unimplemented!("{:?}", ty),
},
Expand Down
63 changes: 35 additions & 28 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use {
},
};

/// Function type for selection of swapchain surface image format.
pub type SelectSurfaceFormatFn = dyn FnOnce(&[vk::SurfaceFormatKHR]) -> vk::SurfaceFormatKHR;

/// Describes a screen mode for display.
pub enum FullscreenMode {
/// A display mode which retains other operating system windows behind the current window.
Expand Down Expand Up @@ -246,7 +249,7 @@ pub struct EventLoopBuilder {
device_info: DeviceInfoBuilder,
event_loop: winit::event_loop::EventLoop<()>,
resolver_pool: Option<Box<dyn ResolverPool>>,
surface_format_fn: Option<Box<dyn FnOnce(&[vk::SurfaceFormatKHR]) -> usize>>,
surface_format_fn: Option<Box<SelectSurfaceFormatFn>>,
swapchain_info: SwapchainInfoBuilder,
window: WindowBuilder,
}
Expand Down Expand Up @@ -291,9 +294,11 @@ impl EventLoopBuilder {
}

/// A function to select the desired swapchain surface image format.
///
/// By default sRGB will be selected unless it is not available.
pub fn desired_surface_format<F>(mut self, surface_format_fn: F) -> Self
where
F: FnOnce(&[vk::SurfaceFormatKHR]) -> usize + 'static,
F: 'static + FnOnce(&[vk::SurfaceFormatKHR]) -> vk::SurfaceFormatKHR,
{
let surface_format_fn = Box::new(surface_format_fn);
self.surface_format_fn = Some(surface_format_fn);
Expand Down Expand Up @@ -485,18 +490,14 @@ impl EventLoopBuilder {
);
}

let surface_format_fn = self
.surface_format_fn
.unwrap_or_else(|| Box::new(Self::select_swapchain_format));
let mut surface_format_idx = surface_format_fn(&surface_formats);

if surface_format_idx >= surface_formats.len() {
warn!("invalid surface format selected");

surface_format_idx = 0;
}

let surface_format = surface_formats[surface_format_idx];
let surface_format_fn = self.surface_format_fn.unwrap_or_else(|| {
Box::new(|formats| {
Self::srgb_surface_format(formats)
.or_else(|| Self::linear_surface_format(formats))
.unwrap_or(formats[0])
})
});
let surface_format = surface_format_fn(&surface_formats);
let swapchain = Swapchain::new(
&device,
surface,
Expand All @@ -519,29 +520,35 @@ impl EventLoopBuilder {
})
}

fn select_swapchain_format(formats: &[vk::SurfaceFormatKHR]) -> usize {
for (idx, format) in formats.iter().copied().enumerate() {
if format.color_space != vk::ColorSpaceKHR::SRGB_NONLINEAR {
continue;
}

/// Helper function to automatically select the best UNORM format.
pub fn linear_surface_format(formats: &[vk::SurfaceFormatKHR]) -> Option<vk::SurfaceFormatKHR> {
for swapchain in formats.iter().copied() {
if matches!(
format.format,
vk::Format::R8G8B8A8_SRGB | vk::Format::B8G8R8A8_SRGB
swapchain.format,
vk::Format::R8G8B8A8_UNORM | vk::Format::B8G8R8A8_UNORM
) {
return idx;
return Some(swapchain);
}
}

for (idx, format) in formats.iter().copied().enumerate() {
None
}

/// Helper function to automatically select the best sRGB format.
pub fn srgb_surface_format(formats: &[vk::SurfaceFormatKHR]) -> Option<vk::SurfaceFormatKHR> {
for swapchain in formats.iter().copied() {
if swapchain.color_space != vk::ColorSpaceKHR::SRGB_NONLINEAR {
continue;
}

if matches!(
format.format,
vk::Format::R8G8B8A8_UNORM | vk::Format::B8G8R8A8_UNORM
swapchain.format,
vk::Format::R8G8B8A8_SRGB | vk::Format::B8G8R8A8_SRGB
) {
return idx;
return Some(swapchain);
}
}

0
None
}
}

0 comments on commit 028bc0f

Please sign in to comment.