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

Add support for macOS (and other GPUs without hardware ray tracing support) #46

Merged
merged 17 commits into from
Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
11 changes: 6 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Operating systems:
* `uuid-dev`
* In case the bundled `libdxcompiler.so` doesn't work: https://github.com/microsoft/DirectXShaderCompiler#downloads

### (Some) MacOS dependencies

* `ossp-uuid` (`brew install ossp-uuid`)

## Building and running

To build `kajiya` and its tools, [you need Rust](https://www.rust-lang.org/tools/install).
Expand Down
Empty file modified assets/fonts/LICENSE.txt
100644 → 100755
Empty file.
4 changes: 1 addition & 3 deletions crates/lib/kajiya-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ derive_builder = { version = "0.9", default-features = false }
futures = "0.3"
glam = "0.18"
gpu-allocator = { git = "https://github.com/Traverse-Research/gpu-allocator.git", rev = "e66d062cbd73a6c98834fc3e3acef98318097156" }
hassle-rs = "0.4"
hassle-rs = "0.5"
hotwatch = "0.4"
lazy_static = "1.4"
log = "0.4"
Expand All @@ -36,7 +36,5 @@ turbosloth = { git = "https://github.com/h3r2tic/turbosloth.git", rev = "92030af
vk-sync = { git = "https://github.com/h3r2tic/vk-sync-rs", rev = "cb5bbf2" }

[features]
default = ["ray-tracing"]
#default = []
ray-tracing = []
dlss = []
7 changes: 1 addition & 6 deletions crates/lib/kajiya-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,4 @@ pub use file::{canonical_path_from_vfs, normalized_path_from_vfs, set_vfs_mount_
pub use gpu_allocator;
pub use rspirv_reflect;
pub use vk_sync;
pub use vulkan::{
device::Device,
image::*,
shader::{MAX_BINDLESS_DESCRIPTOR_COUNT, MAX_DESCRIPTOR_SETS},
RenderBackend,
};
pub use vulkan::{device::Device, image::*, shader::MAX_DESCRIPTOR_SETS, RenderBackend};
126 changes: 74 additions & 52 deletions crates/lib/kajiya-backend/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use std::{
sync::Arc,
};

pub const RESERVED_DESCRIPTOR_COUNT: u32 = 32;
expenses marked this conversation as resolved.
Show resolved Hide resolved

pub struct Queue {
pub raw: vk::Queue,
pub family: QueueFamily,
Expand Down Expand Up @@ -145,6 +147,8 @@ pub struct Device {
pub ray_tracing_pipeline_properties: vk::PhysicalDeviceRayTracingPipelinePropertiesKHR,

frames: [Mutex<Arc<DeviceFrame>>; 2],

ray_tracing_enabled: bool,
}

// Allowing `Send` on `frames` is technically unsound. There are some checks
Expand All @@ -156,8 +160,26 @@ unsafe impl Send for Device {}
unsafe impl Sync for Device {}

impl Device {
fn extension_names(pdevice: &Arc<PhysicalDevice>) -> Vec<*const i8> {
let mut device_extension_names_raw = vec![
pub fn create(pdevice: &Arc<PhysicalDevice>) -> Result<Arc<Self>> {
let supported_extensions: HashSet<String> = unsafe {
let extension_properties = pdevice
.instance
.raw
.enumerate_device_extension_properties(pdevice.raw)?;
debug!("Extension properties:\n{:#?}", &extension_properties);

extension_properties
.iter()
.map(|ext| {
std::ffi::CStr::from_ptr(ext.extension_name.as_ptr() as *const c_char)
.to_string_lossy()
.as_ref()
.to_owned()
})
.collect()
};

let mut device_extension_names = vec![
vk::ExtDescriptorIndexingFn::name().as_ptr(),
vk::ExtScalarBlockLayoutFn::name().as_ptr(),
vk::KhrMaintenance1Fn::name().as_ptr(),
Expand All @@ -168,10 +190,8 @@ impl Device {
vk::KhrImagelessFramebufferFn::name().as_ptr(),
vk::KhrImageFormatListFn::name().as_ptr(),
vk::KhrDescriptorUpdateTemplateFn::name().as_ptr(),
vk::KhrDrawIndirectCountFn::name().as_ptr(),
// Rust-GPU
vk::KhrShaderFloat16Int8Fn::name().as_ptr(),
vk::KhrVulkanMemoryModelFn::name().as_ptr(),
// DLSS
#[cfg(feature = "dlss")]
{
Expand All @@ -185,55 +205,47 @@ impl Device {
vk::NvxImageViewHandleFn::name().as_ptr(),
];

#[cfg(feature = "ray-tracing")]
{
device_extension_names_raw.extend(
[
vk::KhrPipelineLibraryFn::name().as_ptr(), // rt dep
vk::KhrDeferredHostOperationsFn::name().as_ptr(), // rt dep
vk::KhrBufferDeviceAddressFn::name().as_ptr(), // rt dep
vk::KhrAccelerationStructureFn::name().as_ptr(),
vk::KhrRayTracingPipelineFn::name().as_ptr(),
//vk::KhrRayQueryFn::name().as_ptr(),
]
.iter(),
);
}
let ray_tracing_extensions = [
vk::KhrVulkanMemoryModelFn::name().as_ptr(), // used in ray tracing shaders
vk::KhrPipelineLibraryFn::name().as_ptr(), // rt dep
vk::KhrDeferredHostOperationsFn::name().as_ptr(), // rt dep
vk::KhrBufferDeviceAddressFn::name().as_ptr(), // rt dep
vk::KhrAccelerationStructureFn::name().as_ptr(),
vk::KhrRayTracingPipelineFn::name().as_ptr(),
];

if pdevice.presentation_requested {
device_extension_names_raw.push(khr::Swapchain::name().as_ptr());
}
let ray_tracing_enabled = unsafe {
ray_tracing_extensions.iter().all(|ext| {
let ext = std::ffi::CStr::from_ptr(*ext).to_string_lossy();

device_extension_names_raw
}
let supported = supported_extensions.contains(ext.as_ref());

pub fn create(pdevice: &Arc<PhysicalDevice>) -> Result<Arc<Self>> {
let device_extension_names = Self::extension_names(pdevice);
if !supported {
log::info!("Ray tracing extension not supported: {}", ext);
}

unsafe {
let extension_properties = pdevice
.instance
.raw
.enumerate_device_extension_properties(pdevice.raw)?;
debug!("Extension properties:\n{:#?}", &extension_properties);
supported
})
};

let supported_extensions: HashSet<String> = extension_properties
.iter()
.map(|ext| {
std::ffi::CStr::from_ptr(ext.extension_name.as_ptr() as *const c_char)
.to_string_lossy()
.as_ref()
.to_owned()
})
.collect();
if ray_tracing_enabled {
log::info!("All ray tracing extension are supported");
h3r2tic marked this conversation as resolved.
Show resolved Hide resolved

device_extension_names.extend(ray_tracing_extensions.iter());
}

if pdevice.presentation_requested {
device_extension_names.push(khr::Swapchain::name().as_ptr());
}

unsafe {
for &ext in &device_extension_names {
let ext = std::ffi::CStr::from_ptr(ext).to_string_lossy();
if !supported_extensions.contains(ext.as_ref()) {
panic!("Device extension not supported: {}", ext);
}
}
};
}

let priorities = [1.0];

Expand Down Expand Up @@ -264,11 +276,9 @@ impl Device {
let mut get_buffer_device_address_features =
ash::vk::PhysicalDeviceBufferDeviceAddressFeatures::default();

#[cfg(feature = "ray-tracing")]
let mut acceleration_structure_features =
ash::vk::PhysicalDeviceAccelerationStructureFeaturesKHR::default();

#[cfg(feature = "ray-tracing")]
let mut ray_tracing_pipeline_features =
ash::vk::PhysicalDeviceRayTracingPipelineFeaturesKHR::default();

Expand All @@ -283,8 +293,7 @@ impl Device {
.push_next(&mut vulkan_memory_model)
.push_next(&mut get_buffer_device_address_features);

#[cfg(feature = "ray-tracing")]
{
if ray_tracing_enabled {
features2 = features2
.push_next(&mut acceleration_structure_features)
.push_next(&mut ray_tracing_pipeline_features);
Expand All @@ -311,9 +320,7 @@ impl Device {

assert!(descriptor_indexing.shader_uniform_texel_buffer_array_dynamic_indexing != 0);
assert!(descriptor_indexing.shader_storage_texel_buffer_array_dynamic_indexing != 0);
assert!(descriptor_indexing.shader_uniform_buffer_array_non_uniform_indexing != 0);
assert!(descriptor_indexing.shader_sampled_image_array_non_uniform_indexing != 0);
assert!(descriptor_indexing.shader_storage_buffer_array_non_uniform_indexing != 0);
assert!(descriptor_indexing.shader_storage_image_array_non_uniform_indexing != 0);
assert!(descriptor_indexing.shader_uniform_texel_buffer_array_non_uniform_indexing != 0);
assert!(descriptor_indexing.shader_storage_texel_buffer_array_non_uniform_indexing != 0);
Expand All @@ -327,18 +334,20 @@ impl Device {

assert!(shader_float16_int8.shader_int8 != 0);

assert!(vulkan_memory_model.vulkan_memory_model != 0);
if ray_tracing_enabled {
assert!(descriptor_indexing.shader_uniform_buffer_array_non_uniform_indexing != 0);
assert!(descriptor_indexing.shader_storage_buffer_array_non_uniform_indexing != 0);

assert!(vulkan_memory_model.vulkan_memory_model != 0);

#[cfg(feature = "ray-tracing")]
{
assert!(acceleration_structure_features.acceleration_structure != 0);
assert!(acceleration_structure_features.descriptor_binding_acceleration_structure_update_after_bind != 0);

assert!(ray_tracing_pipeline_features.ray_tracing_pipeline != 0);
assert!(ray_tracing_pipeline_features.ray_tracing_pipeline_trace_rays_indirect != 0);
}

assert!(get_buffer_device_address_features.buffer_device_address != 0);
assert!(get_buffer_device_address_features.buffer_device_address != 0);
}
}

let device_create_info = vk::DeviceCreateInfo::builder()
Expand Down Expand Up @@ -412,6 +421,7 @@ impl Device {
Mutex::new(Arc::new(frame1)),
//Mutex::new(Arc::new(frame2)),
],
ray_tracing_enabled,
}))
}
}
Expand Down Expand Up @@ -601,6 +611,18 @@ impl Device {
pub fn debug_utils(&self) -> Option<&DebugUtils> {
self.instance.debug_utils.as_ref()
}

pub fn max_bindless_descriptor_count(&self) -> u32 {
self.pdevice
.properties
.limits
.max_per_stage_descriptor_sampled_images
- RESERVED_DESCRIPTOR_COUNT
expenses marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn ray_tracing_enabled(&self) -> bool {
self.ray_tracing_enabled
}
}

impl Drop for Device {
Expand Down
32 changes: 31 additions & 1 deletion crates/lib/kajiya-backend/src/vulkan/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,41 @@ impl Default for ImageViewDesc {
impl Device {
pub fn create_image(
&self,
desc: ImageDesc,
mut desc: ImageDesc,
initial_data: Vec<ImageSubResourceData>,
) -> Result<Image, BackendError> {
log::info!("Creating an image: {:?}", desc);

let limits = self.pdevice.properties.limits;

let max_dimension = if desc.extent[2] > 1 {
limits
.max_image_dimension1_d
.min(limits.max_image_dimension2_d)
.min(limits.max_image_dimension3_d)
} else if desc.extent[1] > 1 {
limits
.max_image_dimension1_d
.min(limits.max_image_dimension2_d)
} else {
limits.max_image_dimension1_d
};

for i in 0..3 {
let dimension = desc.extent[i];

if dimension > max_dimension {
log::warn!(
"Dimension {} ({}) exceeds max dimension {}. Adjusting to the max. {:?}",
i,
dimension,
max_dimension,
desc
);
desc.extent[i] = max_dimension;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be here 😅 The image creation function cannot just go and create something else, as it will most likely break things up the chain, where code depends on the specific sizes of images. Best case some rendering is glitchy; worst case, we get infinite loops and driver crashes.

Where does this trigger for you? It should probably be higher-level logic that deals with those limits 👀

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, just checked and the problematic volume texture is coming from CSGI because the indirect cascade texture is

[
    VOLUME_DIMS * TOTAL_SUBRAY_COUNT as u32),
    VOLUME_DIMS,
    VOLUME_DIMS,
]

or [3456, 64, 64].

Can this be 1x1x1 as well?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I believe it can!


let create_info = get_image_create_info(&desc, !initial_data.is_empty());

/*let allocation_info = vk_mem::AllocationCreateInfo {
Expand Down
3 changes: 1 addition & 2 deletions crates/lib/kajiya-backend/src/vulkan/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use std::{
};

pub const MAX_DESCRIPTOR_SETS: usize = 4;
pub const MAX_BINDLESS_DESCRIPTOR_COUNT: usize = 512 * 1024;

type DescriptorSetLayout = HashMap<u32, rspirv_reflect::DescriptorInfo>;
type StageDescriptorSetLayouts = HashMap<u32, DescriptorSetLayout>;
Expand Down Expand Up @@ -196,7 +195,7 @@ pub fn create_descriptor_set_layouts(
rspirv_reflect::DescriptorDimensionality::Single => 1,
rspirv_reflect::DescriptorDimensionality::Array(size) => size,
rspirv_reflect::DescriptorDimensionality::RuntimeArray => {
MAX_BINDLESS_DESCRIPTOR_COUNT as u32
device.max_bindless_descriptor_count()
}
};

Expand Down
5 changes: 0 additions & 5 deletions crates/lib/kajiya-rg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,3 @@ log = "0.4"
parking_lot = "0.11"
puffin = "0.11.0"
turbosloth = { git = "https://github.com/h3r2tic/turbosloth.git", rev = "92030af" }

[features]
default = ["ray-tracing"]
#default = []
ray-tracing = []
4 changes: 4 additions & 0 deletions crates/lib/kajiya-rg/src/temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ impl TemporalRenderGraph {
temporal_state: state,
}
}

pub fn device(&self) -> &Device {
self.device.as_ref()
}
}

pub trait GetOrCreateTemporal<Desc: ResourceDesc> {
Expand Down
Loading