-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmod.rs
More file actions
262 lines (219 loc) · 8.73 KB
/
mod.rs
File metadata and controls
262 lines (219 loc) · 8.73 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
pub mod gfx;
use winit::{WindowBuilder, EventsLoop};
use vulkano_win::{Window, VkSurfaceBuild, required_extensions};
use vulkano::command_buffer::CommandBufferBuilder;
use vulkano::device::{Queue, Device, DeviceExtensions};
use vulkano::format;
use vulkano::framebuffer::{RenderPass, RenderPassAbstract, Framebuffer, FramebufferAbstract};
use vulkano::image::attachment::{AttachmentImageAccess, AttachmentImage};
use vulkano::image::SwapchainImage;
use vulkano::instance::{Instance, PhysicalDevice};
use vulkano::memory::pool::StdMemoryPoolAlloc;
use vulkano::swapchain::{Swapchain, SurfaceTransform, PresentMode};
use vulkano::sync::GpuFuture;
use std::clone::Clone;
use std::sync::Arc;
use std::time::Duration;
use config::Config;
pub use self::gfx::GraphicsState;
use core::MINIMUM_RESOLUTION;
type FinalFramebuffer = Framebuffer<Arc<RenderPassAbstract + Send + Sync>, (((), Arc<SwapchainImage>), AttachmentImageAccess<format::D16Unorm, StdMemoryPoolAlloc>)>;
/// Handles the rendering of the graphics state.
pub struct Renderer {
/// Engine configuration
config: Arc<Config>,
/// Current Window
window: Arc<Window>,
// Vulkan API
instance: Arc<Instance>,
physical_device: usize,
device: Arc<Device>,
swapchain: Arc<Swapchain>,
images: Vec<Arc<SwapchainImage>>,
depth_buffer: Arc<AttachmentImage<format::D16Unorm>>,
render_pass: Arc<RenderPassAbstract + Send + Sync>,
framebuffers: Vec<Arc<FinalFramebuffer>>,
queue: Arc<Queue>
}
impl Renderer {
pub fn new(window_builder: WindowBuilder, config: Arc<Config>) -> (Renderer, Arc<Window>, Arc<EventsLoop>) {
// Create Vulkan Instance, Physical Device
let instance = {
let extensions = required_extensions();
Instance::new(None, &extensions, &[]).expect("Failed to create Vulkan instance.")
};
let ins = instance.clone();
let physical = PhysicalDevice::enumerate(&ins)
.next()
.expect("No vulkan device is available.");
let physical_device = physical.index();
// Create Window
let events_loop = EventsLoop::new();
let window = Arc::new(window_builder.build_vk_surface(&events_loop, instance.clone()).unwrap());
// Queue ID for Device generation
let queue = physical
.queue_families()
.find(|&q| q.supports_graphics() && window.surface().is_supported(q).unwrap_or(false))
.expect("Couldn't find a graphical queue family.");
// Logical Device
let (device, mut queues) = {
let device_ext = DeviceExtensions {
khr_swapchain: true,
..DeviceExtensions::none()
};
Device::new(&physical,
physical.supported_features(),
&device_ext,
[(queue, 0.5)].iter().cloned())
.expect("failed to create device")
};
// Device Queue
let queue = queues.next().unwrap();
// Swapchain, Swapchain Images
let (swapchain, images) =
create_swapchain(&window, physical, &device, &queue, None, &config);
let depth_buffer = AttachmentImage::transient(device.clone(), images[0].dimensions(), format::D16Unorm).unwrap();
// Render Pass
let render_pass: Arc<RenderPassAbstract + Send + Sync> = Arc::new(
single_pass_renderpass!(device.clone(),
attachments: {
color: {
load: Clear,
store: Store,
format: swapchain.format(),
samples: 1,
},
depth: {
load: Clear,
store: DontCare,
format: format::Format::D16Unorm,
samples: 1,
}
},
pass: {
color: [color],
depth_stencil: {depth}
}
).unwrap()
);
let framebuffers = images.iter().map(|image| {
Arc::new(Framebuffer::start(render_pass.clone())
.add(image.clone()).unwrap()
.add(depth_buffer.clone()).unwrap()
.build().unwrap())
}).collect::<Vec<_>>();
(
Renderer {
config,
window: window.clone(),
instance,
physical_device,
device,
swapchain,
images,
depth_buffer,
framebuffers,
render_pass,
queue
},
window,
Arc::new(events_loop)
)
}
pub fn resize(&mut self) {
let (swapchain, images) =
create_swapchain(&self.window,
PhysicalDevice::from_index(&self.instance, self.physical_device).unwrap(),
&self.device,
&self.queue,
Some(&self.swapchain),
&self.config);
self.swapchain = swapchain;
self.depth_buffer = AttachmentImage::transient(self.device.clone(), images[0].dimensions(), format::D16Unorm).unwrap();
self.images = images;
self.framebuffers = self.images.iter().map(|image| {
Arc::new(Framebuffer::start(self.render_pass.clone())
.add(image.clone()).unwrap()
.add(self.depth_buffer.clone()).unwrap()
.build().unwrap())
}).collect::<Vec<_>>();
}
pub fn render(&mut self, gfx: &GraphicsState) {
/*
// @TODO - For each node in gfxstate
let command_buffers = self.framebuffers
.iter()
.map(|framebuffer| {
let cmd = PrimaryCommandBufferBuilder::new(&self.device, self.queue.family())
.draw_inline(&self.render_pass,
&framebuffer,
render_pass::ClearValues {
color: [0.2, 0.4, 0.8, 1.0],
depth: 1.0,
})
.draw_end()
.build();
// renderstate.render(cmd)
cmd
})
.collect::<Vec<_>>();
let image_num = self.swapchain
.acquire_next_image(Duration::new(1, 0))
.unwrap();
// @TODO build command buffers with threads and submit the changes in main thread (here)
self.submissions
.push(submit(&command_buffers[image_num], &self.queue).unwrap());
self.swapchain.present(&self.queue, image_num).unwrap();
*/
}
}
/// Sets up and creates a swapchain
fn create_swapchain(window: &Window,
physical_device: PhysicalDevice,
device: &Arc<Device>,
queue: &Arc<Queue>,
old: Option<&Arc<Swapchain>>,
config: &Config)
-> (Arc<Swapchain>, Vec<Arc<SwapchainImage>>) {
{
let caps = window
.surface()
.capabilities(physical_device)
.expect("failed to get surface capabilities");
let dimensions = if config.window.resolution[0] <= MINIMUM_RESOLUTION[0] ||
config.window.resolution[1] <= MINIMUM_RESOLUTION[1] {
let min = caps.min_image_extent;
let extent = caps.current_extent.unwrap_or(MINIMUM_RESOLUTION);
if extent[0] < min[0] || extent[1] < min[1] {
min
}
else {
extent
}
} else {
config.window.resolution
};
let present = if config.graphics.vsync &&
caps.present_modes.supports(PresentMode::Mailbox) {
PresentMode::Mailbox
} else {
caps.present_modes.iter().next().unwrap()
};
let alpha = caps.supported_composite_alpha.iter().next().unwrap();
let format = caps.supported_formats[0].0;
Swapchain::new(device.clone(),
window.surface().clone(),
caps.min_image_count,
format,
dimensions,
1,
caps.supported_usage_flags,
queue,
SurfaceTransform::Identity,
alpha,
present,
true,
old)
.expect("failed to create swapchain")
}
}