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 12 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.
2 changes: 1 addition & 1 deletion crates/bin/bake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
kajiya-asset = { path = "../../lib/kajiya-asset" }
kajiya-asset = { path = "../../lib/kajiya-asset", default-features = false }

anyhow = "1.0"
async-channel = "1.6"
Expand Down
4 changes: 2 additions & 2 deletions crates/bin/hello/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
kajiya = { path = "../../lib/kajiya" }
kajiya-simple = { path = "../../lib/kajiya-simple" }
kajiya = { path = "../../lib/kajiya", default-features = false }
kajiya-simple = { path = "../../lib/kajiya-simple", default-features = false }
anyhow = "1.0"
6 changes: 4 additions & 2 deletions crates/bin/view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
kajiya = { path = "../../lib/kajiya" }
kajiya-simple = { path = "../../lib/kajiya-simple", features = ["dear-imgui"] }
kajiya = { path = "../../lib/kajiya", default-features = false }
kajiya-simple = { path = "../../lib/kajiya-simple", default-features = false, features = ["dear-imgui"] }

anyhow = "1.0"
dolly = "0.1"
Expand All @@ -18,4 +18,6 @@ serde = { version = "1.0", features = ["derive"] }
structopt = "0.3"

[features]
default = ["ray-tracing"]
ray-tracing = ["kajiya/ray-tracing", "kajiya-simple/ray-tracing"]
dlss = ["kajiya/dlss"]
6 changes: 5 additions & 1 deletion crates/lib/kajiya-asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
kajiya-backend = { path = "../kajiya-backend" }
kajiya-backend = { path = "../kajiya-backend", default-features = false }

anyhow = "1.0"
base64 = "0.12"
Expand All @@ -18,3 +18,7 @@ image = { version = "0.23.13", default-features = false, features = ["gif", "jpe
log = "0.4"
turbosloth = { git = "https://github.com/h3r2tic/turbosloth.git", rev = "92030af" }
urlencoding = "2.1"

[features]
default = ["ray-tracing"]
ray-tracing = ["kajiya-backend/ray-tracing"]
2 changes: 1 addition & 1 deletion 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 Down
23 changes: 15 additions & 8 deletions crates/lib/kajiya-backend/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,16 @@ impl Device {
vk::KhrImagelessFramebufferFn::name().as_ptr(),
vk::KhrImageFormatListFn::name().as_ptr(),
vk::KhrDescriptorUpdateTemplateFn::name().as_ptr(),
vk::KhrDrawIndirectCountFn::name().as_ptr(),
#[cfg(feature = "ray-tracing")]
{
vk::KhrDrawIndirectCountFn::name().as_ptr()
},
expenses marked this conversation as resolved.
Show resolved Hide resolved
// Rust-GPU
vk::KhrShaderFloat16Int8Fn::name().as_ptr(),
vk::KhrVulkanMemoryModelFn::name().as_ptr(),
#[cfg(feature = "ray-tracing")]
{
vk::KhrVulkanMemoryModelFn::name().as_ptr()
},
// DLSS
#[cfg(feature = "dlss")]
{
Expand Down Expand Up @@ -311,9 +317,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 +331,21 @@ impl Device {

assert!(shader_float16_int8.shader_int8 != 0);

assert!(vulkan_memory_model.vulkan_memory_model != 0);

#[cfg(feature = "ray-tracing")]
{
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);

expenses marked this conversation as resolved.
Show resolved Hide resolved
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
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: 3 additions & 0 deletions crates/lib/kajiya-backend/src/vulkan/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use std::{
};

pub const MAX_DESCRIPTOR_SETS: usize = 4;
#[cfg(feature = "ray-tracing")]
pub const MAX_BINDLESS_DESCRIPTOR_COUNT: usize = 512 * 1024;
#[cfg(not(feature = "ray-tracing"))]
pub const MAX_BINDLESS_DESCRIPTOR_COUNT: usize = 96;
expenses marked this conversation as resolved.
Show resolved Hide resolved

type DescriptorSetLayout = HashMap<u32, rspirv_reflect::DescriptorInfo>;
type StageDescriptorSetLayouts = HashMap<u32, DescriptorSetLayout>;
Expand Down
6 changes: 5 additions & 1 deletion crates/lib/kajiya-imgui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
kajiya = { path = "../kajiya" }
kajiya = { path = "../kajiya", default-features = false }

ash-imgui = { path = "../ash-imgui" }
imgui = { version = "0.7" }
imgui-winit-support = { version = "0.7", default-features = false, features = ["winit-25"] }
log = "0.4"
parking_lot = "0.11"
winit = "0.25"

[features]
default = ["ray-tracing"]
ray-tracing = ["kajiya/ray-tracing"]
4 changes: 2 additions & 2 deletions crates/lib/kajiya-rg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
kajiya-backend = { path = "../kajiya-backend" }
kajiya-backend = { path = "../kajiya-backend", default-features = false }

anyhow = "1.0"
arrayvec = "0.5"
Expand All @@ -19,4 +19,4 @@ turbosloth = { git = "https://github.com/h3r2tic/turbosloth.git", rev = "92030af
[features]
default = ["ray-tracing"]
#default = []
ray-tracing = []
ray-tracing = ["kajiya-backend/ray-tracing"]
6 changes: 4 additions & 2 deletions crates/lib/kajiya-simple/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
kajiya = { path = "../kajiya" }
kajiya-imgui = { path = "../kajiya-imgui", optional = true }
kajiya = { path = "../kajiya", default-features = false }
kajiya-imgui = { path = "../kajiya-imgui", default-features = false, optional = true }

anyhow = "1.0"
glam = { version = "0.18", features = ["serde"] }
Expand All @@ -20,6 +20,8 @@ puffin_http = { version = "0.8.0", optional = true }
imgui = { version = "0.7", optional = true }

[features]
default = ["ray-tracing"]
ray-tracing = ["kajiya/ray-tracing", "kajiya-imgui/ray-tracing"]
dear-imgui = [
"imgui",
"kajiya-imgui",
Expand Down
9 changes: 5 additions & 4 deletions crates/lib/kajiya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
kajiya-asset = { path = "../kajiya-asset" }
kajiya-backend = { path = "../kajiya-backend" }
kajiya-rg = { path = "../kajiya-rg" }
kajiya-asset = { path = "../kajiya-asset", default-features = false }
kajiya-backend = { path = "../kajiya-backend", default-features = false }
kajiya-rg = { path = "../kajiya-rg", default-features = false }
rust-shaders-shared = { path = "../rust-shaders-shared" }

anyhow = "1.0"
Expand All @@ -31,5 +31,6 @@ wchar = "0.10"
easy-parallel = "3.1.0"

[features]
default = []
default = ["ray-tracing"]
ray-tracing = ["kajiya-backend/ray-tracing", "kajiya-rg/ray-tracing", "kajiya-asset/ray-tracing"]
dlss = [ "ngx_dlss", "kajiya-backend/dlss" ]
1 change: 1 addition & 0 deletions crates/lib/kajiya/src/default_world_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl WorldRenderer {
}

// Build an empty TLAS to create the resources. We'll update it at runtime.
#[cfg(feature = "ray-tracing")]
world_renderer.build_ray_tracing_top_level_acceleration();

Ok(world_renderer)
Expand Down
3 changes: 2 additions & 1 deletion crates/lib/kajiya/src/renderers/csgi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl CsgiRenderer {
rg: &mut rg::TemporalRenderGraph,
sky_cube: &rg::Handle<Image>,
bindless_descriptor_set: vk::DescriptorSet,
tlas: &rg::Handle<RayTracingAcceleration>,
#[cfg(feature = "ray-tracing")] tlas: &rg::Handle<RayTracingAcceleration>,
) -> CsgiVolume {
let mut direct_cascades: [rg::Handle<Image>; CASCADE_COUNT] = array_init::array_init(|i| {
rg.get_or_create_temporal(
Expand Down Expand Up @@ -261,6 +261,7 @@ impl CsgiRenderer {
//
// Either is going to leave the GPU quite under-utilized though, and would be a nice fit for async compute.

#[cfg(feature = "ray-tracing")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a bit messy; ideally the entire csgi renderer should be unused when ray-tracing is disabled. I suspect you're only after the resources that it creates -- and I also suspect it's those resources that are too large on your macbook, so that you ended up clamping texture size.

We can side-step all that by not disabling the whole renderer, and only creating placeholder resources for where it needs to be passed in -- something like a black 1x1x1 volume.

for cascade_i in 0..CASCADE_COUNT {
if 0 == ((1 << cascade_i) & cascade_update_mask) {
continue;
Expand Down
3 changes: 2 additions & 1 deletion crates/lib/kajiya/src/renderers/rtdgi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl RtdgiRenderer {
reprojection_map: &rg::Handle<Image>,
sky_cube: &rg::Handle<Image>,
bindless_descriptor_set: vk::DescriptorSet,
tlas: &rg::Handle<RayTracingAcceleration>,
#[cfg(feature = "ray-tracing")] tlas: &rg::Handle<RayTracingAcceleration>,
csgi_volume: &csgi::CsgiVolume,

// TODO: calculate specialized SSAO
Expand Down Expand Up @@ -242,6 +242,7 @@ impl RtdgiRenderer {
vk_sync::AccessType::ComputeShaderReadSampledImageOrUniformTexelBuffer,
);

#[cfg(feature = "ray-tracing")]
SimpleRenderPass::new_rt(
rg.add_pass("rtdgi trace"),
ShaderSource::hlsl("/shaders/rtdgi/trace_diffuse.rgen.hlsl"),
Expand Down
3 changes: 2 additions & 1 deletion crates/lib/kajiya/src/renderers/rtr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl RtrRenderer {
reprojection_map: &rg::Handle<Image>,
sky_cube: &rg::Handle<Image>,
bindless_descriptor_set: vk::DescriptorSet,
tlas: &rg::Handle<RayTracingAcceleration>,
#[cfg(feature = "ray-tracing")] tlas: &rg::Handle<RayTracingAcceleration>,
csgi_volume: &csgi::CsgiVolume,
rtdgi: &rg::Handle<Image>,
) -> TracedRtr {
Expand Down Expand Up @@ -107,6 +107,7 @@ impl RtrRenderer {
vk_sync::AccessType::ComputeShaderReadSampledImageOrUniformTexelBuffer,
);

#[cfg(feature = "ray-tracing")]
SimpleRenderPass::new_rt(
rg.add_pass("reflection trace"),
ShaderSource::hlsl("/shaders/rtr/reflection.rgen.hlsl"),
Expand Down
3 changes: 2 additions & 1 deletion crates/lib/kajiya/src/renderers/shadows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ use super::GbufferDepth;
pub fn trace_sun_shadow_mask(
rg: &mut RenderGraph,
gbuffer_depth: &GbufferDepth,
tlas: &rg::Handle<RayTracingAcceleration>,
#[cfg(feature = "ray-tracing")] tlas: &rg::Handle<RayTracingAcceleration>,
bindless_descriptor_set: vk::DescriptorSet,
) -> rg::Handle<Image> {
let mut output_img = rg.create(gbuffer_depth.depth.desc().format(vk::Format::R8_UNORM));

#[cfg(feature = "ray-tracing")]
SimpleRenderPass::new_rt(
rg.add_pass("trace shadow mask"),
ShaderSource::hlsl("/shaders/rt/trace_sun_shadow_mask.rgen.hlsl"),
Expand Down
Loading