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