Skip to content

Commit 404da54

Browse files
authored
Merge pull request #4 from nonl4331/dev
2 parents dad1c64 + 6d431e5 commit 404da54

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+5287
-4213
lines changed

Cargo.toml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "frontend"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
[workspace]
8+
members = ["crates/*"]
9+
10+
[dependencies]
11+
clap = { version = "4.1.8", features = ["derive", "wrap_help"] }
12+
gui = { path = "./crates/gui", optional = true }
13+
implementations = { path = "./crates/implementations" }
14+
indicatif = "0.17.3"
15+
loader = { path = "./crates/loader" }
16+
output = { path = "./crates/output" }
17+
rand = { version = "0.8.3", features = [ "small_rng" ] }
18+
rand_seeder = "0.2.2"
19+
rayon = "1.5.1"
20+
region = { path = "./crates/region" }
21+
vulkano = { version = "0.28.0", optional = true }
22+
vulkano-shaders = { version = "0.28.0", optional = true }
23+
vulkano-win = { version = "0.28.0", optional = true }
24+
winit = { version = "0.26.1", optional = true }
25+
26+
[features]
27+
f64 = ["implementations/f64"]
28+
gui = ["dep:vulkano", "dep:vulkano-win", "dep:vulkano-shaders", "dep:winit", "dep:gui"]

LICENSE

-674
This file was deleted.

crates/gui/Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "gui"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
vulkano = { version = "0.28.0" }
8+
vulkano-shaders = { version = "0.28.0" }
9+
vulkano-win = { version = "0.28.0" }
10+
winit = { version = "0.26.1" }
11+
implementations = { path = "../implementations" }
12+
indicatif = "0.17.3"

frontend/src/gui.rs crates/gui/src/gui.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::rendering::*;
1+
use crate::rendering::CpuRendering;
2+
use crate::rendering::RenderInfo;
3+
use std::sync::atomic::AtomicBool;
24
use std::sync::Arc;
35
use vulkano::{
46
command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage, PrimaryAutoCommandBuffer},
@@ -40,10 +42,11 @@ pub struct Gui {
4042
render_info: RenderInfo,
4143
combined_buffer: Arc<StorageImage>,
4244
presentation_finished: Option<Box<dyn GpuFuture + 'static>>,
45+
exit: Arc<AtomicBool>,
4346
}
4447

4548
impl Gui {
46-
pub fn new(instance: &Arc<Instance>, width: u32, height: u32) -> Self {
49+
pub fn new(instance: &Arc<Instance>, width: u32, height: u32, exit: Arc<AtomicBool>) -> Self {
4750
let event_loop: EventLoop<RenderEvent> = EventLoop::with_user_event();
4851
let surface = WindowBuilder::new()
4952
.build_vk_surface(&event_loop, instance.clone())
@@ -139,9 +142,7 @@ impl Gui {
139142
let cpu_rendering = CpuRendering::new(&physical_device, device.clone(), width, height);
140143

141144
mod cs {
142-
vulkano_shaders::shader! {
143-
ty: "compute",
144-
src:
145+
vulkano_shaders::shader! {ty: "compute",src:
145146
"#version 460
146147
147148
layout(local_size_x = 32, local_size_y = 32) in;
@@ -151,8 +152,8 @@ layout(set = 0, binding = 0, rgba32f) uniform readonly image2D cpu_input;
151152
layout(set = 0, binding = 1, rgba8) uniform writeonly image2D image_output;
152153
153154
void main() {
154-
vec4 data = sqrt(imageLoad(cpu_input, ivec2(gl_GlobalInvocationID.xy)));
155-
imageStore(image_output, ivec2(gl_GlobalInvocationID.xy), data);
155+
vec4 data = sqrt(imageLoad(cpu_input, ivec2(gl_GlobalInvocationID.xy)));
156+
imageStore(image_output, ivec2(gl_GlobalInvocationID.xy), data);
156157
}"}
157158
}
158159

@@ -201,6 +202,7 @@ void main() {
201202
presentation_command_buffers,
202203
combined_buffer,
203204
presentation_finished: None,
205+
exit,
204206
}
205207
}
206208

@@ -213,14 +215,14 @@ void main() {
213215
Event::DeviceEvent {
214216
event: winit::event::DeviceEvent::Key(key),
215217
..
216-
} => match key.virtual_keycode {
217-
Some(code) => {
218+
} => {
219+
if let Some(code) = key.virtual_keycode {
218220
if code == winit::event::VirtualKeyCode::Escape {
221+
self.exit.store(true, std::sync::atomic::Ordering::Relaxed);
219222
*control_flow = ControlFlow::Exit;
220223
}
221224
}
222-
None => {}
223-
},
225+
}
224226
Event::WindowEvent {
225227
event: WindowEvent::CloseRequested,
226228
..
@@ -246,9 +248,8 @@ void main() {
246248
}
247249

248250
fn update(&mut self) {
249-
match self.presentation_finished.as_mut() {
250-
Some(future) => future.cleanup_finished(),
251-
None => {}
251+
if let Some(future) = self.presentation_finished.as_mut() {
252+
future.cleanup_finished()
252253
}
253254
self.presentation_finished = Some(sync::now(self.device.clone()).boxed());
254255

@@ -260,7 +261,7 @@ void main() {
260261
return;
261262
}
262263
Err(e) => {
263-
panic!("Failed to acquire next image: {:?}", e)
264+
panic!("Failed to acquire next image: {e:?}")
264265
}
265266
};
266267

@@ -328,7 +329,7 @@ void main() {
328329
self.presentation_finished = Some(sync::now(self.device.clone()).boxed());
329330
}
330331
Err(e) => {
331-
println!("Failed to flush future: {:?}", e);
332+
println!("Failed to flush future: {e:?}");
332333
self.presentation_finished = Some(sync::now(self.device.clone()).boxed());
333334
}
334335
}
@@ -339,7 +340,7 @@ void main() {
339340
match self.swapchain.recreate().dimensions(dimensions).build() {
340341
Ok(r) => r,
341342
Err(SwapchainCreationError::UnsupportedDimensions) => return,
342-
Err(e) => panic!("Failed to recreate swapchain: {:?}", e),
343+
Err(e) => panic!("Failed to recreate swapchain: {e:?}"),
343344
};
344345
let extent: [u32; 2] = self.surface.window().inner_size().into();
345346

crates/gui/src/lib.rs

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
mod gui;
2+
mod rendering;
3+
4+
use implementations::SamplerProgress;
5+
use indicatif::ProgressBar;
6+
use indicatif::ProgressStyle;
7+
8+
use {
9+
std::sync::{
10+
atomic::{AtomicBool, AtomicU64, Ordering},
11+
Arc,
12+
},
13+
vulkano::{
14+
buffer::CpuAccessibleBuffer,
15+
command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage, PrimaryAutoCommandBuffer},
16+
device::{Device, Queue},
17+
image::StorageImage,
18+
sync::{self, GpuFuture},
19+
},
20+
winit::event_loop::EventLoopProxy,
21+
};
22+
23+
pub use crate::gui::{Gui, RenderEvent};
24+
pub use crate::rendering::Future;
25+
26+
pub struct Data {
27+
pub queue: Arc<Queue>,
28+
pub device: Arc<Device>,
29+
pub to_sc: rendering::Future,
30+
pub from_sc: rendering::Future,
31+
pub command_buffers: [Arc<PrimaryAutoCommandBuffer>; 2],
32+
pub buffer: Arc<CpuAccessibleBuffer<[f32]>>,
33+
pub sc_index: Arc<AtomicBool>,
34+
pub samples: Arc<AtomicU64>,
35+
pub total_samples: u64,
36+
pub rays_shot: Arc<AtomicU64>,
37+
pub event_proxy: EventLoopProxy<RenderEvent>,
38+
pub exit: Arc<AtomicBool>,
39+
pub bar: ProgressBar,
40+
}
41+
42+
impl Data {
43+
pub fn new(
44+
queue: Arc<Queue>,
45+
device: Arc<Device>,
46+
to_sc: rendering::Future,
47+
from_sc: rendering::Future,
48+
command_buffers: [Arc<PrimaryAutoCommandBuffer>; 2],
49+
buffer: Arc<CpuAccessibleBuffer<[f32]>>,
50+
sc_index: Arc<AtomicBool>,
51+
samples: Arc<AtomicU64>,
52+
total_samples: u64,
53+
rays_shot: Arc<AtomicU64>,
54+
exit: Arc<AtomicBool>,
55+
event_proxy: EventLoopProxy<RenderEvent>,
56+
) -> Self {
57+
Data {
58+
queue,
59+
device,
60+
to_sc,
61+
from_sc,
62+
command_buffers,
63+
buffer,
64+
sc_index,
65+
samples,
66+
total_samples,
67+
rays_shot,
68+
event_proxy,
69+
exit,
70+
bar: ProgressBar::new(total_samples).with_style(
71+
ProgressStyle::default_bar()
72+
.template("[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}")
73+
.unwrap(),
74+
),
75+
}
76+
}
77+
}
78+
79+
pub fn create_command_buffers(
80+
device: Arc<Device>,
81+
queue: Arc<Queue>,
82+
buffer: Arc<CpuAccessibleBuffer<[f32]>>,
83+
sc: [Arc<StorageImage>; 2],
84+
) -> [Arc<PrimaryAutoCommandBuffer>; 2] {
85+
let mut command_buffer_0 = None;
86+
let mut command_buffer_1 = None;
87+
for (i, sc_image) in sc.iter().enumerate() {
88+
let mut builder = AutoCommandBufferBuilder::primary(
89+
device.clone(),
90+
queue.family(),
91+
CommandBufferUsage::MultipleSubmit,
92+
)
93+
.unwrap();
94+
95+
builder
96+
.copy_buffer_to_image(buffer.clone(), sc_image.clone())
97+
.unwrap();
98+
if i == 0 {
99+
command_buffer_0 = Some(builder.build().unwrap());
100+
} else {
101+
command_buffer_1 = Some(builder.build().unwrap());
102+
}
103+
}
104+
105+
[
106+
Arc::new(command_buffer_0.unwrap()),
107+
Arc::new(command_buffer_1.unwrap()),
108+
]
109+
}
110+
111+
pub fn sample_update(data: &mut Data, previous: &SamplerProgress, i: u64) -> bool {
112+
if data.exit.load(Ordering::Relaxed) {
113+
return true;
114+
}
115+
// update infomation about the rays shot and samples completed in the current render
116+
data.samples.fetch_add(1, Ordering::Relaxed);
117+
data.rays_shot
118+
.fetch_add(previous.rays_shot, Ordering::Relaxed);
119+
120+
// wait on from_sc future if is_some()
121+
match &*data.from_sc.lock().unwrap() {
122+
Some(future) => {
123+
future.wait(None).unwrap();
124+
}
125+
None => {}
126+
}
127+
match &*data.to_sc.lock().unwrap() {
128+
Some(future) => {
129+
future.wait(None).unwrap();
130+
}
131+
None => {}
132+
}
133+
134+
{
135+
// get access to CpuAccessibleBuffer
136+
let mut buf = data.buffer.write().unwrap();
137+
buf.chunks_mut(4)
138+
.zip(previous.current_image.chunks(3))
139+
.for_each(|(pres, acc)| {
140+
pres[0] += (acc[0] as f32 - pres[0]) / i as f32;
141+
pres[1] += (acc[1] as f32 - pres[1]) / i as f32;
142+
pres[2] += (acc[2] as f32 - pres[2]) / i as f32;
143+
pres[3] = 1.0;
144+
});
145+
}
146+
147+
// copy to cpu swapchain
148+
let command_buffer =
149+
data.command_buffers[data.sc_index.load(Ordering::Relaxed) as usize].clone();
150+
151+
// copy to swapchain and store op in to_sc future
152+
{
153+
let to_sc = &mut *data.to_sc.lock().unwrap();
154+
*to_sc = Some(
155+
match to_sc.take() {
156+
Some(future) => future
157+
.then_execute(data.queue.clone(), command_buffer)
158+
.unwrap()
159+
.boxed_send_sync(),
160+
None => sync::now(data.device.clone())
161+
.then_execute(data.queue.clone(), command_buffer)
162+
.unwrap()
163+
.boxed_send_sync(),
164+
}
165+
.then_signal_fence_and_flush()
166+
.unwrap(), // change to match
167+
);
168+
}
169+
170+
// modify sc_index to !sc_index
171+
data.sc_index
172+
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |x| Some(!x))
173+
.unwrap();
174+
175+
data.bar.set_position(data.samples.load(Ordering::Relaxed));
176+
if data.samples.load(Ordering::Relaxed) == data.total_samples {
177+
data.bar.abandon()
178+
}
179+
180+
// signal sample is ready to be presented
181+
data.event_proxy
182+
.send_event(RenderEvent::SampleCompleted)
183+
.is_err()
184+
}
File renamed without changes.

implementations/Cargo.toml crates/implementations/Cargo.toml

+13-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@ edition = "2021"
77

88
[dependencies]
99
image = "0.24.3"
10-
proc = {path = "./proc"}
10+
proc = { path = "./proc" }
1111
rand = { version = "0.8.3", features = [ "small_rng" ] }
1212
rayon = "1.5.1"
13-
rt_core = {path = "../rt_core"}
13+
rt_core = { path = "../rt_core" }
14+
bumpalo = {version="3.12.0", features=["collections"]}
15+
num_cpus = "1.15"
16+
region = { path = "../region"}
17+
statrs = "0.16.0"
18+
clap = { version = "4.1.8", features = [ "derive" ] }
19+
20+
1421

1522
[dev-dependencies]
1623
chrono = "0.4.19"
17-
statrs = "0.16.0"
24+
statrs = "0.16.0"
25+
26+
[features]
27+
f64 = ["rt_core/f64"]
File renamed without changes.

implementations/proc/src/lib.rs crates/implementations/proc/src/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub fn derive_primitive(tokens: TokenStream) -> TokenStream {
122122

123123
let func_names_primitive = [
124124
(
125-
quote!(get_int(&self, __one: &Ray) -> Option<SurfaceIntersection #ty_generics>),
125+
quote!(get_int(&self, __one: &Ray) -> Option<SurfaceIntersection<Self::Material>>),
126126
quote!(get_int(__one)),
127127
),
128128
(
@@ -177,7 +177,10 @@ pub fn derive_primitive(tokens: TokenStream) -> TokenStream {
177177
});
178178

179179
quote! {
180-
impl #impl_generics Primitive #ty_generics for #enum_name #ty_generics #where_clause {#( #functions_primitive )*}
180+
impl #impl_generics Primitive for #enum_name #ty_generics #where_clause {
181+
type Material = M;
182+
#( #functions_primitive )*
183+
}
181184
impl #impl_generics AABound for #enum_name #ty_generics #where_clause { #( #functions_aabound )*}
182185
}
183186
.into()

implementations/src/acceleration/aabb.rs crates/implementations/src/acceleration/aabb.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utility::gamma;
2-
use rt_core::{Float, Ray, Vec3};
2+
use rt_core::*;
33

44
pub trait AABound {
55
fn get_aabb(&self) -> AABB;

0 commit comments

Comments
 (0)