Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion app/src/main/rust/cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ bytemuck = { version = "1.25", features = ["derive"] }
glam = { version = "0.33", features = ["bytemuck", "mint"] }
mint = "0.5"
openxr = { git = "https://github.com/Ralith/openxrs.git", rev = "294809e4fe7f6f9729c80526f5320d7d70408984", default-features = false, features = ["mint"] }
vk-graph = { version = "0.14.5" }
vk-graph = { git = "https://github.com/attackgoat/vk-graph.git", branch = "descriptor-set" }
egui = { version = "0.35.0", features = [ "bytemuck" ] }

gltf = { version = "1.4", features = [ "extras", "names" ] }
Expand Down
50 changes: 40 additions & 10 deletions app/src/main/rust/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use log::warn;
use vk_graph::driver::ash::vk::DeviceSize;
use vk_graph::driver::buffer::BufferInfo;
use vk_graph::pool::Pool;
use {
std::{
time::{Instant, Duration},
Expand Down Expand Up @@ -35,6 +39,7 @@ use {
},
log::info,
};
use crate::render::renderer::{RenderingError, XrCameraUBO};

pub struct XrSession {
pub(crate) running: bool,
Expand Down Expand Up @@ -163,7 +168,20 @@ pub fn main_loop(env: &mut Env<'_>, ctx: Arc<JniContext>, raw_asset_manager: *mu
let mut context = XrContext::new(Arc::clone(&ctx));
let mut renderer = Renderer::new(&context, internal_files_directory.as_path());
let input = InputState::new(&context.instance, &context.session.session);
let mut scene = Scene::load(&context.instance.device, &asset_manager);

let mut mapped_camera_buffers = Vec::new();
let mut camera_buffers = Vec::new();
(0..context.swapchain.enumerate_images().unwrap().len()).for_each(|_| {
let mut buffer = Buffer::create(&context.instance.device, BufferInfo::builder()
.host_writable(true)
.size(size_of::<XrCameraUBO>() as DeviceSize)
.usage(BufferUsageFlags::UNIFORM_BUFFER))
.expect("Failed to allocate camera buffer");
mapped_camera_buffers.push(buffer.mapped_slice_mut().as_mut_ptr() as *mut XrCameraUBO);
camera_buffers.push(Arc::new(buffer));
});

let mut scene = Scene::load(&context.instance.device, &camera_buffers, &asset_manager);
// if you're looking how to load assets n shit, it's in the scene ^^

let names: Vec<&str> = scene.assets.animated_asset.animation_names().collect();
Expand Down Expand Up @@ -202,7 +220,7 @@ pub fn main_loop(env: &mut Env<'_>, ctx: Arc<JniContext>, raw_asset_manager: *mu
Ok(frame) => frame,
Err(err) => {
match err {
renderer::RenderingError::Sleeping => {
RenderingError::Sleeping => {
sleep(Duration::from_millis(100));
log::trace!("sleeping...")
},
Expand Down Expand Up @@ -275,6 +293,15 @@ pub fn main_loop(env: &mut Env<'_>, ctx: Arc<JniContext>, raw_asset_manager: *mu
}
Err(err) => log::error!("Bad skin supplied from java-side: {:?}", err)
}


if let Some(ref skin) = *scene.assets.skin.read().unwrap() {
scene.assets.animated_instance.override_textures(&skin.texture, &camera_buffers);
scene.assets.left_controller_scene_instance.override_textures(&skin.texture, &camera_buffers);
scene.assets.slim_left_controller_scene_instance.override_textures(&skin.texture, &camera_buffers);
scene.assets.right_controller_scene_instance.override_textures(&skin.texture, &camera_buffers);
scene.assets.slim_right_controller_scene_instance.override_textures(&skin.texture, &camera_buffers);
}
}

animator.advance(delta_time, &scene.assets.animated_asset.animations[animator.clip_index]);
Expand Down Expand Up @@ -333,23 +360,26 @@ pub fn main_loop(env: &mut Env<'_>, ctx: Arc<JniContext>, raw_asset_manager: *mu
}
}

let payload = renderer::FramePayload {
view_matrices: [
unsafe {
let mapped_camera_buffer = mapped_camera_buffers[renderer.frame_in_flight];
(*mapped_camera_buffer).view_matrices = [
renderer::view_transform(views[0]) * world_to_stage,
renderer::view_transform(views[1]) * world_to_stage,
],
projection_matrices: [
];
(*mapped_camera_buffer).projection_matrices = [
renderer::projection_transform(views[0]),
renderer::projection_transform(views[1]),
],
];
}

let payload = renderer::FramePayload {
camera_buffer: &camera_buffers[renderer.frame_in_flight],
xr_views: &views,
};

let result = renderer.draw(&mut context, active_frame, payload, |graph, draw_payload| {
scene.record(graph, draw_payload);
if let Some(ref skin) = *scene.assets.skin.read().unwrap() {
scene.assets.animated_instance.record_with_transform_override_texture(graph, draw_payload, &Mat4::IDENTITY, skin.texture.clone());
}
scene.assets.animated_instance.record_with_transform(graph, draw_payload, &Mat4::IDENTITY);
if let Some(ref last_surface_texture) = last_surface_texture {
surface_manager.record_with_transform(graph, last_surface_texture.image.clone(), draw_payload, surface_transform);
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/rust/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ impl InputState {
}

if let Ok(state) = self.actions.left_thumbstick.state(session, xr::Path::NULL) {
movement[1] = -state.current_state.y; // for some reason down on the thumbstick makes y positive? idk
// my stick drift is actually so bad I can't test with this enabled
// movement[1] = -state.current_state.y; // for some reason down on the thumbstick makes y positive? idk
}

let mut right_click_state = false;
Expand Down
38 changes: 18 additions & 20 deletions app/src/main/rust/src/render/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::Path;
use std::sync::Arc;
use vk_graph::driver::buffer::Buffer;
use vk_graph::node::{AnyBufferNode, AnyImageNode};
use {
crate::{
app::XrContext,
Expand All @@ -16,8 +19,7 @@ use {
vk_graph::{
cmd::ClearColorValue,
driver::{
ash::vk::{self, BufferUsageFlags, DeviceSize},
buffer::BufferInfo,
ash::vk::{self},
device::Device,
fence::Fence,
image::{ImageInfo, SampleCount},
Expand All @@ -38,9 +40,11 @@ pub enum RenderingError {
}

pub struct Renderer<'a> {
resolution: vk::Extent2D,
pub resolution: vk::Extent2D,

pool: LazyPool,
pub pool: LazyPool,
pub frame_in_flight: usize,
max_frames_in_flight: usize,
swapchain_queues: Box<[Option<Fence>]>,
swapchain_rect: xr::Rect2Di,
fixture_path: &'a Path,
Expand Down Expand Up @@ -73,15 +77,15 @@ pub struct ActiveFrame {
}

pub struct FramePayload<'a> {
pub view_matrices: [Mat4; 2],
pub projection_matrices: [Mat4; 2],
pub camera_buffer: &'a Arc<Buffer>,
pub xr_views: &'a [openxr::View],
}

pub struct DrawPayload<'a> {
pub camera_ubo: &'a vk_graph::node::AnyBufferNode,
pub color_target: &'a vk_graph::node::AnyImageNode,
pub depth_target: &'a vk_graph::node::AnyImageNode,
pub frame_in_flight: usize,
pub camera_ubo: &'a AnyBufferNode,
pub color_target: &'a AnyImageNode,
pub depth_target: &'a AnyImageNode,
}

pub const VIEW_MASK: u32 = !(!0 << 2);
Expand Down Expand Up @@ -123,22 +127,13 @@ impl<'a> Renderer<'a> {
).into_builder().sample_count(MSAA_COUNT)).unwrap().with_debug_name("main depth image")
);

let camera_data = XrCameraUBO {
view_matrices: payload.view_matrices,
projection_matrices: payload.projection_matrices,
};
let mut ubo_buffer = self.pool.resource(BufferInfo::builder()
.host_writable(true)
.size(size_of::<XrCameraUBO>() as DeviceSize)
.usage(BufferUsageFlags::UNIFORM_BUFFER)
).map_err(|_| RenderingError::DriverError)?;
ubo_buffer.copy_from_slice(0, bytemuck::bytes_of(&camera_data));
let camera_ubo_node = graph.bind_resource(ubo_buffer);
let camera_ubo_node = graph.bind_resource(payload.camera_buffer);

graph.clear_color_image(swapchain_image, ClearColorValue::WHITE_ALPHA_ONE);
graph.clear_depth_stencil_image(depth_target, 1.0, 0);

let draw_payload = DrawPayload {
frame_in_flight: self.frame_in_flight,
camera_ubo: &camera_ubo_node.into(),
color_target: &swapchain_image.into(),
depth_target: &depth_target.into(),
Expand Down Expand Up @@ -172,6 +167,7 @@ impl<'a> Renderer<'a> {
])
]
).map_err(|_| FailedToEndStream)?;
self.frame_in_flight = (self.frame_in_flight + 1) % self.max_frames_in_flight;
#[cfg(feature = "profiled")]
profiling::finish_frame!();

Expand All @@ -197,6 +193,8 @@ impl<'a> Renderer<'a> {
Renderer {
resolution,
pool,
frame_in_flight: 0,
max_frames_in_flight: swapchain_image_count,
swapchain_queues,
swapchain_rect,
fixture_path,
Expand Down
Loading