Skip to content

Commit

Permalink
Fix some best practices warnings and add optional allocation priority…
Browse files Browse the repository at this point in the history
… extension
  • Loading branch information
ilyas-taouaou committed Oct 30, 2024
1 parent 943643e commit ab70c41
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 13 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Features:

- Multi window support.
- Resolution scaling.
- Automatic shader compilation with include support.

## Compatibility

Expand All @@ -17,10 +18,15 @@ Windows or Linux with Vulkan API 1.3 capable GPU driver with the following featu
- dynamic_rendering
- synchronization2
- buffer_device_address
- buffer_device_address_capture_replay (debug only)
- descriptor_indexing
- scalar_block_layout

Optional extensions:

- debug_utils (debug only)
- buffer_device_address_capture_replay (debug only)
- memory_priority and pageable_device_local_memory

## Contributions

Contributions are welcome especially bug fixes, will reviewed in the next live streams.
Contributions are welcome especially bug fixes.
3 changes: 1 addition & 2 deletions engine/devres/shaders/shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ void main() {
Instance instance = pushConstants.instanceBuffer.instances[gl_InstanceIndex];
Camera camera = pushConstants.cameraBuffer.cameras[0];

vec3 position = vertex.position;
mat4 mvp = camera.projection * camera.view * instance.model;
gl_Position = mvp * vec4(position, 1.0);
gl_Position = mvp * vec4(vertex.position, 1.0);
fragColor = vertex.color;
}
9 changes: 9 additions & 0 deletions engine/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct BufferAttributes {
pub usage: vk::BufferUsageFlags,
pub location: MemoryLocation,
pub allocation_scheme: AllocationScheme,
pub allocation_priority: f32,
}

pub struct Buffer {
Expand Down Expand Up @@ -60,6 +61,14 @@ impl Buffer {
allocation_scheme: attributes.allocation_scheme,
})?;

if let Some(ref extension) = attributes.context.pageable_device_local_memory_extension {
(extension.fp().set_device_memory_priority_ext)(
attributes.context.device.handle(),
allocation.memory(),
attributes.allocation_priority,
);
}

attributes.context.device.bind_buffer_memory(
handle,
allocation.memory(),
Expand Down
16 changes: 15 additions & 1 deletion engine/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::sync::Arc;
pub struct ImageAttributes {
pub location: MemoryLocation,
pub allocation_scheme: AllocationScheme,
pub allocation_priority: f32,
pub linear: bool,
pub extent: vk::Extent3D,
pub format: vk::Format,
Expand Down Expand Up @@ -86,6 +87,16 @@ impl Image {
allocation_scheme: attributes.allocation_scheme,
})?;

if let Some(ref extension) = context.pageable_device_local_memory_extension {
unsafe {
(extension.fp().set_device_memory_priority_ext)(
context.device.handle(),
allocation.memory(),
attributes.allocation_priority,
);
}
}

unsafe {
context
.device
Expand Down Expand Up @@ -115,6 +126,7 @@ impl Image {
name: &str,
extent: vk::Extent2D,
format: vk::Format,
allocation_priority: f32,
) -> Result<Image> {
Image::new(
context,
Expand All @@ -131,6 +143,7 @@ impl Image {
.aspect_mask(vk::ImageAspectFlags::COLOR)
.level_count(1)
.layer_count(1),
allocation_priority,
},
)
}
Expand All @@ -157,6 +170,7 @@ impl Image {
.aspect_mask(vk::ImageAspectFlags::DEPTH)
.level_count(1)
.layer_count(1),
allocation_priority: 1.0,
},
)
}
Expand Down Expand Up @@ -245,7 +259,7 @@ impl ImageLayoutState {

pub fn present() -> Self {
Self {
access: vk::AccessFlags2::TRANSFER_READ,
access: vk::AccessFlags2::empty(),
layout: vk::ImageLayout::PRESENT_SRC_KHR,
stage: vk::PipelineStageFlags2::TRANSFER,
queue_family: QUEUE_FAMILY_IGNORED,
Expand Down
2 changes: 2 additions & 0 deletions engine/src/renderer/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ impl Geometry {
| vk::BufferUsageFlags::TRANSFER_DST,
location: MemoryLocation::GpuOnly,
allocation_scheme: AllocationScheme::GpuAllocatorManaged,
allocation_priority: 1.0,
},
)?;

Expand All @@ -186,6 +187,7 @@ impl Geometry {
usage: vk::BufferUsageFlags::INDEX_BUFFER | vk::BufferUsageFlags::TRANSFER_DST,
location: MemoryLocation::GpuOnly,
allocation_scheme: AllocationScheme::GpuAllocatorManaged,
allocation_priority: 1.0,
},
)?;

Expand Down
4 changes: 4 additions & 0 deletions engine/src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ impl Renderer {
"render_target",
attributes.extent,
attributes.format,
1.0,
)
})
.collect::<Result<Vec<_>>>()?;
Expand Down Expand Up @@ -229,6 +230,7 @@ impl Renderer {
| vk::BufferUsageFlags::TRANSFER_DST,
location: MemoryLocation::GpuOnly,
allocation_scheme: AllocationScheme::GpuAllocatorManaged,
allocation_priority: 1.0,
},
)?;

Expand Down Expand Up @@ -268,6 +270,7 @@ impl Renderer {
| vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS,
location: MemoryLocation::CpuToGpu,
allocation_scheme: AllocationScheme::GpuAllocatorManaged,
allocation_priority: 1.0,
},
)?;
camera_buffer.write(&gpu_cameras, 0)?;
Expand Down Expand Up @@ -302,6 +305,7 @@ impl Renderer {
"render_target",
resolution,
self.attributes.format,
1.0,
)?;
frame.depth_buffer = Image::new_depth_buffer(
self.context.clone(),
Expand Down
1 change: 1 addition & 0 deletions engine/src/renderer/staging_belt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl StagingBelt {
usage: vk::BufferUsageFlags::TRANSFER_SRC,
location: MemoryLocation::CpuToGpu,
allocation_scheme: AllocationScheme::GpuAllocatorManaged,
allocation_priority: 1.0,
},
)?;
Ok(Self {
Expand Down
3 changes: 2 additions & 1 deletion engine/src/renderer/swapchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Swapchain {
.image_sharing_mode(vk::SharingMode::EXCLUSIVE)
.pre_transform(vk::SurfaceTransformFlagsKHR::IDENTITY)
.composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE)
.present_mode(vk::PresentModeKHR::MAILBOX)
.present_mode(vk::PresentModeKHR::FIFO)
.clipped(true)
.old_swapchain(self.handle),
None,
Expand Down Expand Up @@ -116,6 +116,7 @@ impl Swapchain {
.aspect_mask(vk::ImageAspectFlags::COLOR)
.level_count(1)
.layer_count(1),
allocation_priority: 1.0,
},
)?)
})
Expand Down
64 changes: 59 additions & 5 deletions engine/src/rendering_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use winit::window::Window;

pub struct RenderingContext {
pub queues: Vec<vk::Queue>,
pub pageable_device_local_memory_extension:
Option<ash::ext::pageable_device_local_memory::Device>,
pub swapchain_extension: ash::khr::swapchain::Device,
pub device: ash::Device,
pub queue_family_indices: HashSet<u32>,
Expand All @@ -39,6 +41,8 @@ pub struct PhysicalDevice {
pub features: vk::PhysicalDeviceFeatures,
pub vulkan12_features: vk::PhysicalDeviceVulkan12Features<'static>,
pub vulkan13_features: vk::PhysicalDeviceVulkan13Features<'static>,
pub pageable_device_local_memory_features:
vk::PhysicalDevicePageableDeviceLocalMemoryFeaturesEXT<'static>,
pub memory_properties: vk::PhysicalDeviceMemoryProperties,
pub queue_families: Vec<QueueFamily>,
}
Expand Down Expand Up @@ -113,14 +117,33 @@ impl RenderingContext {
let raw_display_handle = attributes.compatibility_window.display_handle()?.as_raw();
let raw_window_handle = attributes.compatibility_window.window_handle()?.as_raw();

let available_extensions = entry
.enumerate_instance_extension_properties(None)?
.into_iter()
.map(|extension| {
let name = extension.extension_name;
std::ffi::CStr::from_ptr(name.as_ptr())
.to_str()
.unwrap()
.to_string()
})
.collect::<HashSet<_>>();

let mut extensions =
ash_window::enumerate_required_extensions(raw_display_handle)?.to_vec();

if cfg!(debug_assertions) {
if available_extensions.contains(ash::ext::debug_utils::NAME.to_str()?) {
extensions.push(ash::ext::debug_utils::NAME.as_ptr());
}
}

let instance = entry.create_instance(
&vk::InstanceCreateInfo::default()
.application_info(
&vk::ApplicationInfo::default().api_version(vk::API_VERSION_1_3),
)
.enabled_extension_names(ash_window::enumerate_required_extensions(
raw_display_handle,
)?),
.enabled_extension_names(&extensions),
None,
)?;

Expand All @@ -141,9 +164,12 @@ impl RenderingContext {
let properties = instance.get_physical_device_properties(handle);
let mut vulkan12_features = vk::PhysicalDeviceVulkan12Features::default();
let mut vulkan13_features = vk::PhysicalDeviceVulkan13Features::default();
let mut pageable_device_local_memory_features =
vk::PhysicalDevicePageableDeviceLocalMemoryFeaturesEXT::default();
let mut features = vk::PhysicalDeviceFeatures2::default()
.push_next(&mut vulkan12_features)
.push_next(&mut vulkan13_features);
.push_next(&mut vulkan13_features)
.push_next(&mut pageable_device_local_memory_features);
instance.get_physical_device_features2(handle, &mut features);
let features = features.features;
let memory_properties = instance.get_physical_device_memory_properties(handle);
Expand All @@ -165,6 +191,7 @@ impl RenderingContext {
features,
vulkan12_features,
vulkan13_features,
pageable_device_local_memory_features,
memory_properties,
queue_families,
}
Expand Down Expand Up @@ -215,11 +242,25 @@ impl RenderingContext {
.buffer_device_address_capture_replay
== vk::TRUE;

let is_pageable_device_local_memory_supported = physical_device
.pageable_device_local_memory_features
.pageable_device_local_memory
== vk::TRUE;

let mut device_extensions = vec![ash::khr::swapchain::NAME.as_ptr()];

let mut pageable_device_local_memory_extension = None;

if is_pageable_device_local_memory_supported {
device_extensions.push(ash::ext::memory_priority::NAME.as_ptr());
device_extensions.push(ash::ext::pageable_device_local_memory::NAME.as_ptr());
}

let device = instance.create_device(
physical_device.handle,
&vk::DeviceCreateInfo::default()
.queue_create_infos(&queue_create_infos)
.enabled_extension_names(&[ash::khr::swapchain::NAME.as_ptr()])
.enabled_extension_names(&device_extensions)
.push_next(
&mut vk::PhysicalDeviceVulkan12Features::default()
.buffer_device_address(true)
Expand All @@ -233,10 +274,22 @@ impl RenderingContext {
&mut vk::PhysicalDeviceVulkan13Features::default()
.dynamic_rendering(true)
.synchronization2(true),
)
.push_next(
&mut vk::PhysicalDevicePageableDeviceLocalMemoryFeaturesEXT::default()
.pageable_device_local_memory(
is_pageable_device_local_memory_supported,
),
),
None,
)?;

if is_pageable_device_local_memory_supported {
pageable_device_local_memory_extension = Some(
ash::ext::pageable_device_local_memory::Device::new(&instance, &device),
);
}

let swapchain_extension = ash::khr::swapchain::Device::new(&instance, &device);

let queues = queue_family_indices
Expand All @@ -257,6 +310,7 @@ impl RenderingContext {
instance,
entry,
swapchain_extension,
pageable_device_local_memory_extension,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl ApplicationHandler for App {
let primary_window_attributes = WindowAttributes::default().with_title("Primary window");
let primary_window_renderer_attributes = WindowRendererAttributes {
format: vk::Format::R16G16B16A16_SFLOAT,
depth_format: vk::Format::D32_SFLOAT,
depth_format: vk::Format::D16_UNORM,
clear_color: vk::ClearColorValue {
float32: [0.0, 0.0, 0.0, 1.0],
},
Expand All @@ -29,7 +29,7 @@ impl ApplicationHandler for App {
WindowAttributes::default().with_title("Secondary window");
let secondary_window_renderer_attributes = WindowRendererAttributes {
format: vk::Format::R16G16B16A16_SFLOAT,
depth_format: vk::Format::D32_SFLOAT,
depth_format: vk::Format::D16_UNORM,
clear_color: vk::ClearColorValue {
float32: [0.0, 0.0, 0.0, 1.0],
},
Expand Down

0 comments on commit ab70c41

Please sign in to comment.