Use vulkano_util instead
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 6m30s
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 6m30s
This commit is contained in:
parent
f486486be3
commit
a4a6c0c60a
9 changed files with 137 additions and 415 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
@ -1256,6 +1256,7 @@ dependencies = [
|
|||
"thiserror 2.0.12",
|
||||
"vulkano",
|
||||
"vulkano-shaders",
|
||||
"vulkano-util",
|
||||
"winit",
|
||||
]
|
||||
|
||||
|
@ -1655,6 +1656,17 @@ dependencies = [
|
|||
"vulkano",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vulkano-util"
|
||||
version = "0.35.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e25dc54fd5e14a0e01c7282f9b5d9c6e133745e1df3228b0352366e34c83bb6b"
|
||||
dependencies = [
|
||||
"foldhash",
|
||||
"vulkano",
|
||||
"winit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.5.0"
|
||||
|
|
|
@ -12,6 +12,7 @@ winit = { version = "0.30", features = ["rwh_06"] }
|
|||
|
||||
vulkano = "0.35"
|
||||
vulkano-shaders = "0.35"
|
||||
vulkano-util = "0.35"
|
||||
|
||||
# Math
|
||||
glam = { version = "0.30" }
|
||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -1,14 +1,34 @@
|
|||
use vulkano::device::{DeviceExtensions, DeviceFeatures};
|
||||
use vulkano_util::context::{VulkanoConfig, VulkanoContext};
|
||||
use winit::event_loop::{ControlFlow, EventLoop};
|
||||
|
||||
mod render;
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let device_extensions = DeviceExtensions {
|
||||
khr_swapchain: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let device_features = DeviceFeatures {
|
||||
dynamic_rendering: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let vulkano_config = VulkanoConfig {
|
||||
print_device_name: true,
|
||||
device_extensions,
|
||||
device_features,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let vulkano_context = VulkanoContext::new(vulkano_config);
|
||||
|
||||
let event_loop = EventLoop::new().unwrap();
|
||||
event_loop.set_control_flow(ControlFlow::Poll);
|
||||
|
||||
let vulkan_context = render::vulkan_context::VulkanContext::from(&event_loop);
|
||||
let mut app = render::app::App::from(vulkan_context);
|
||||
let mut app = render::app::App::from(vulkano_context);
|
||||
|
||||
match event_loop.run_app(&mut app) {
|
||||
Ok(_) => {}
|
||||
|
|
|
@ -1,28 +1,32 @@
|
|||
use crate::render::scene::Scene;
|
||||
use crate::render::vulkan_context::VulkanContext;
|
||||
use crate::render::window_render_context::WindowRenderContext;
|
||||
use std::sync::Arc;
|
||||
use vulkano::command_buffer::{RenderingAttachmentInfo, RenderingInfo};
|
||||
use vulkano::command_buffer::{
|
||||
AutoCommandBufferBuilder, CommandBufferUsage, RenderingAttachmentInfo, RenderingInfo,
|
||||
};
|
||||
use vulkano::pipeline::graphics::viewport::Viewport;
|
||||
use vulkano::render_pass::{AttachmentLoadOp, AttachmentStoreOp};
|
||||
use vulkano::swapchain::{SwapchainPresentInfo, acquire_next_image};
|
||||
use vulkano::swapchain::{PresentMode, SwapchainPresentInfo};
|
||||
use vulkano::sync::GpuFuture;
|
||||
use vulkano::{Validated, VulkanError, sync};
|
||||
use vulkano_util::context::VulkanoContext;
|
||||
use vulkano_util::window::{VulkanoWindows, WindowDescriptor};
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event_loop::ActiveEventLoop;
|
||||
use winit::window::WindowId;
|
||||
|
||||
use super::vulkan_context::VulkanContext;
|
||||
|
||||
pub struct App {
|
||||
vulkan_context: VulkanContext,
|
||||
window_render_context: Option<WindowRenderContext>,
|
||||
vulkano_windows: VulkanoWindows,
|
||||
scene: Option<Scene>,
|
||||
}
|
||||
|
||||
impl From<VulkanContext> for App {
|
||||
fn from(vulkan_context: VulkanContext) -> Self {
|
||||
impl From<VulkanoContext> for App {
|
||||
fn from(vulkano_context: VulkanoContext) -> Self {
|
||||
Self {
|
||||
vulkan_context,
|
||||
window_render_context: None,
|
||||
vulkan_context: VulkanContext::new(vulkano_context),
|
||||
vulkano_windows: VulkanoWindows::default(),
|
||||
scene: None,
|
||||
}
|
||||
}
|
||||
|
@ -30,26 +34,23 @@ impl From<VulkanContext> for App {
|
|||
|
||||
impl ApplicationHandler for App {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
let window_attributes = winit::window::Window::default_attributes()
|
||||
.with_title("Rust ASH Test")
|
||||
.with_inner_size(winit::dpi::PhysicalSize::new(
|
||||
f64::from(800),
|
||||
f64::from(600),
|
||||
));
|
||||
self.vulkano_windows.create_window(
|
||||
event_loop,
|
||||
self.vulkan_context.vulkano_context(),
|
||||
&WindowDescriptor {
|
||||
title: "Rust ASH Test".to_string(),
|
||||
width: 800.0,
|
||||
height: 600.0,
|
||||
present_mode: PresentMode::Fifo,
|
||||
..Default::default()
|
||||
},
|
||||
|_| {},
|
||||
);
|
||||
|
||||
let window = Arc::new(event_loop.create_window(window_attributes).unwrap());
|
||||
|
||||
let surface = self.vulkan_context.create_surface(window.clone());
|
||||
|
||||
self.window_render_context = Some(WindowRenderContext::new(
|
||||
window,
|
||||
surface,
|
||||
&self.vulkan_context.device,
|
||||
));
|
||||
self.scene = Some(
|
||||
Scene::load(
|
||||
&self.vulkan_context,
|
||||
self.window_render_context.as_ref().unwrap(),
|
||||
&self.vulkano_windows.get_primary_renderer_mut().unwrap(),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
|
@ -61,45 +62,27 @@ impl ApplicationHandler for App {
|
|||
log::debug!("The close button was pressed; stopping");
|
||||
event_loop.exit();
|
||||
}
|
||||
WindowEvent::Resized(_) => {
|
||||
let rcx = self.window_render_context.as_mut().unwrap();
|
||||
rcx.recreate_swapchain = true;
|
||||
}
|
||||
WindowEvent::RedrawRequested => {
|
||||
let (image_index, acquire_future) = {
|
||||
let rcx = self.window_render_context.as_mut().unwrap();
|
||||
let window_size = rcx.window.inner_size();
|
||||
let renderer = self.vulkano_windows.get_primary_renderer_mut().unwrap();
|
||||
let acquire_future = renderer.acquire(None, |_| {}).unwrap();
|
||||
|
||||
if window_size.width == 0 || window_size.height == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
rcx.previous_frame_end.as_mut().unwrap().cleanup_finished();
|
||||
rcx.update_swapchain().unwrap();
|
||||
|
||||
let (image_index, suboptimal, acquire_future) =
|
||||
match acquire_next_image(rcx.swapchain.clone(), None)
|
||||
.map_err(Validated::unwrap)
|
||||
{
|
||||
Ok(r) => r,
|
||||
Err(VulkanError::OutOfDate) => {
|
||||
rcx.recreate_swapchain = true;
|
||||
return;
|
||||
}
|
||||
Err(e) => panic!("failed to acquire next image: {e}"),
|
||||
};
|
||||
|
||||
if suboptimal {
|
||||
rcx.recreate_swapchain = true;
|
||||
}
|
||||
|
||||
(image_index, acquire_future)
|
||||
};
|
||||
|
||||
let mut builder = self.vulkan_context.create_render_builder();
|
||||
let mut builder = AutoCommandBufferBuilder::primary(
|
||||
self.vulkan_context.command_buffer_allocator().clone(),
|
||||
self.vulkan_context
|
||||
.vulkano_context()
|
||||
.graphics_queue()
|
||||
.queue_family_index(),
|
||||
CommandBufferUsage::OneTimeSubmit,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
{
|
||||
let rcx = self.window_render_context.as_ref().unwrap();
|
||||
let viewport = Viewport {
|
||||
offset: [0.0, 0.0],
|
||||
extent: renderer.resolution(),
|
||||
depth_range: 0.0..=1.0,
|
||||
};
|
||||
|
||||
builder
|
||||
.begin_rendering(RenderingInfo {
|
||||
color_attachments: vec![Some(RenderingAttachmentInfo {
|
||||
|
@ -107,23 +90,19 @@ impl ApplicationHandler for App {
|
|||
store_op: AttachmentStoreOp::Store,
|
||||
clear_value: Some([0.0, 0.0, 0.0, 1.0].into()),
|
||||
..RenderingAttachmentInfo::image_view(
|
||||
rcx.attachment_image_views[image_index as usize].clone(),
|
||||
renderer.swapchain_image_view().clone(),
|
||||
)
|
||||
})],
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap()
|
||||
.set_viewport(0, [rcx.viewport.clone()].into_iter().collect())
|
||||
.set_viewport(0, [viewport].into_iter().collect())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if let Some(scene) = self.scene.as_ref() {
|
||||
scene
|
||||
.render(
|
||||
&self.vulkan_context,
|
||||
&self.window_render_context.as_ref().unwrap(),
|
||||
&mut builder,
|
||||
)
|
||||
.render(&self.vulkan_context, &renderer, &mut builder)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
|
@ -131,48 +110,24 @@ impl ApplicationHandler for App {
|
|||
|
||||
let command_buffer = builder.build().unwrap();
|
||||
|
||||
{
|
||||
let rcx = self.window_render_context.as_mut().unwrap();
|
||||
let future = acquire_future
|
||||
.then_execute(
|
||||
self.vulkan_context
|
||||
.vulkano_context()
|
||||
.graphics_queue()
|
||||
.clone(),
|
||||
command_buffer,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let future = rcx
|
||||
.previous_frame_end
|
||||
.take()
|
||||
.unwrap()
|
||||
.join(acquire_future)
|
||||
.then_execute(self.vulkan_context.graphics_queue.clone(), command_buffer)
|
||||
.unwrap()
|
||||
.then_swapchain_present(
|
||||
self.vulkan_context.graphics_queue.clone(),
|
||||
SwapchainPresentInfo::swapchain_image_index(
|
||||
rcx.swapchain.clone(),
|
||||
image_index,
|
||||
),
|
||||
)
|
||||
.then_signal_fence_and_flush();
|
||||
|
||||
match future.map_err(Validated::unwrap) {
|
||||
Ok(future) => {
|
||||
rcx.previous_frame_end = Some(future.boxed());
|
||||
}
|
||||
Err(VulkanError::OutOfDate) => {
|
||||
rcx.recreate_swapchain = true;
|
||||
rcx.previous_frame_end =
|
||||
Some(sync::now(self.vulkan_context.device.clone()).boxed());
|
||||
}
|
||||
Err(e) => {
|
||||
println!("failed to flush future: {e}");
|
||||
rcx.previous_frame_end =
|
||||
Some(sync::now(self.vulkan_context.device.clone()).boxed());
|
||||
}
|
||||
}
|
||||
}
|
||||
renderer.present(future.boxed(), true);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
|
||||
let rcx = self.window_render_context.as_mut().unwrap();
|
||||
rcx.window.request_redraw();
|
||||
let window = self.vulkano_windows.get_primary_window().unwrap();
|
||||
window.request_redraw();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
pub mod app;
|
||||
pub mod pipelines;
|
||||
pub mod scene;
|
||||
pub mod vulkan_context;
|
||||
pub mod window_render_context;
|
||||
pub mod vertex;
|
||||
pub mod vulkan_context;
|
||||
|
|
|
@ -5,6 +5,7 @@ use vulkano::descriptor_set::layout::{
|
|||
DescriptorSetLayoutBinding, DescriptorSetLayoutCreateInfo, DescriptorType,
|
||||
};
|
||||
use vulkano::device::Device;
|
||||
use vulkano::format::Format;
|
||||
use vulkano::pipeline::graphics::GraphicsPipelineCreateInfo;
|
||||
use vulkano::pipeline::graphics::color_blend::{ColorBlendAttachmentState, ColorBlendState};
|
||||
use vulkano::pipeline::graphics::input_assembly::InputAssemblyState;
|
||||
|
@ -40,7 +41,7 @@ pub mod shaders {
|
|||
|
||||
pub fn create_triangle_pipeline(
|
||||
device: &Arc<Device>,
|
||||
swapchain: &Arc<Swapchain>,
|
||||
swapchain_format: Format,
|
||||
) -> Result<Arc<GraphicsPipeline>, Box<dyn Error>> {
|
||||
let (vs, fs) = load_shaders(device)?;
|
||||
let vertex_input_state = Vertex2D::per_vertex().definition(&vs)?;
|
||||
|
@ -71,7 +72,7 @@ pub fn create_triangle_pipeline(
|
|||
let layout = PipelineLayout::new(device.clone(), create_info)?;
|
||||
|
||||
let subpass = PipelineRenderingCreateInfo {
|
||||
color_attachment_formats: vec![Some(swapchain.image_format())],
|
||||
color_attachment_formats: vec![Some(swapchain_format)],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
|
|
|
@ -6,14 +6,14 @@ use std::time::Instant;
|
|||
use vulkano::buffer::{Buffer, BufferCreateInfo, BufferUsage, Subbuffer};
|
||||
use vulkano::command_buffer::{AutoCommandBufferBuilder, PrimaryAutoCommandBuffer};
|
||||
use vulkano::descriptor_set::{DescriptorSet, WriteDescriptorSet};
|
||||
use vulkano::memory::allocator::{AllocationCreateInfo, MemoryTypeFilter};
|
||||
use vulkano::memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator};
|
||||
use vulkano::pipeline::{GraphicsPipeline, Pipeline, PipelineBindPoint};
|
||||
use vulkano_util::renderer::VulkanoWindowRenderer;
|
||||
|
||||
use crate::render::vertex::Vertex2D;
|
||||
use crate::render::pipelines::triangle_pipeline::create_triangle_pipeline;
|
||||
use crate::render::vertex::Vertex2D;
|
||||
|
||||
use super::vulkan_context::VulkanContext;
|
||||
use super::window_render_context::WindowRenderContext;
|
||||
|
||||
const VERTICES: [Vertex2D; 12] = [
|
||||
// Triangle en haut à gauche
|
||||
|
@ -79,13 +79,17 @@ pub struct Scene {
|
|||
|
||||
impl Scene {
|
||||
pub fn load(
|
||||
vulkan_context: &VulkanContext,
|
||||
window_render_context: &WindowRenderContext,
|
||||
vulkano_context: &VulkanContext,
|
||||
vulkano_window_renderer: &VulkanoWindowRenderer,
|
||||
) -> Result<Self, Box<dyn Error>> {
|
||||
let pipeline =
|
||||
create_triangle_pipeline(&vulkan_context.device, &window_render_context.swapchain)?;
|
||||
let vertex_buffer =
|
||||
Vertex2D::create_buffer(Vec::from_iter(VERTICES), &vulkan_context.memory_allocator)?;
|
||||
let pipeline = create_triangle_pipeline(
|
||||
&vulkano_context.vulkano_context().device(),
|
||||
vulkano_window_renderer.swapchain_format(),
|
||||
)?;
|
||||
let vertex_buffer = Vertex2D::create_buffer(
|
||||
Vec::from_iter(VERTICES),
|
||||
&vulkano_context.vulkano_context().memory_allocator(),
|
||||
)?;
|
||||
|
||||
Ok(Scene {
|
||||
pipeline,
|
||||
|
@ -97,16 +101,19 @@ impl Scene {
|
|||
pub fn render(
|
||||
&self,
|
||||
vulkan_context: &VulkanContext,
|
||||
window_render_context: &WindowRenderContext,
|
||||
vulkano_window_renderer: &VulkanoWindowRenderer,
|
||||
builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let vertex_count = self.vertex_buffer.len() as u32;
|
||||
let instance_count = vertex_count / 3;
|
||||
|
||||
let uniform_buffer = self.get_uniform_buffer(vulkan_context, window_render_context);
|
||||
let uniform_buffer = self.get_uniform_buffer(
|
||||
&vulkan_context.vulkano_context().memory_allocator(),
|
||||
vulkano_window_renderer.aspect_ratio(),
|
||||
);
|
||||
let layout = &self.pipeline.layout().set_layouts()[0];
|
||||
let descriptor_set = DescriptorSet::new(
|
||||
vulkan_context.descriptor_set_allocator.clone(),
|
||||
vulkan_context.descriptor_set_allocator().clone(),
|
||||
layout.clone(),
|
||||
[WriteDescriptorSet::buffer(0, uniform_buffer)],
|
||||
[],
|
||||
|
@ -131,18 +138,15 @@ impl Scene {
|
|||
|
||||
fn get_uniform_buffer(
|
||||
&self,
|
||||
vulkan_context: &VulkanContext,
|
||||
window_render_context: &WindowRenderContext,
|
||||
memory_allocator: &Arc<StandardMemoryAllocator>,
|
||||
aspect_ratio: f32,
|
||||
) -> Subbuffer<vs::MVPData> {
|
||||
let swapchain = &window_render_context.swapchain;
|
||||
let elapsed = self.rotation_start.elapsed();
|
||||
let rotation = elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1_000_000_000.0;
|
||||
let rotation = Mat3::from_rotation_y(rotation as f32);
|
||||
|
||||
// NOTE: This teapot was meant for OpenGL where the origin is at the lower left
|
||||
// instead the origin is at the upper left in Vulkan, so we reverse the Y axis.
|
||||
let aspect_ratio = swapchain.image_extent()[0] as f32 / swapchain.image_extent()[1] as f32;
|
||||
|
||||
let proj = Mat4::perspective_rh_gl(std::f32::consts::FRAC_PI_2, aspect_ratio, 0.01, 100.0);
|
||||
let view = Mat4::look_at_rh(
|
||||
Vec3::new(0.3, 0.3, 1.0),
|
||||
|
@ -158,7 +162,7 @@ impl Scene {
|
|||
};
|
||||
|
||||
Buffer::from_data(
|
||||
vulkan_context.memory_allocator.clone(),
|
||||
memory_allocator.clone(),
|
||||
BufferCreateInfo {
|
||||
usage: BufferUsage::UNIFORM_BUFFER,
|
||||
..Default::default()
|
||||
|
|
|
@ -1,213 +1,45 @@
|
|||
use std::{any::Any, sync::Arc};
|
||||
use std::sync::Arc;
|
||||
|
||||
use vulkano::{
|
||||
Version, VulkanLibrary,
|
||||
command_buffer::{
|
||||
AutoCommandBufferBuilder, CommandBufferUsage, PrimaryAutoCommandBuffer,
|
||||
allocator::StandardCommandBufferAllocator,
|
||||
},
|
||||
command_buffer::allocator::StandardCommandBufferAllocator,
|
||||
descriptor_set::allocator::StandardDescriptorSetAllocator,
|
||||
device::{
|
||||
Device, DeviceCreateInfo, DeviceExtensions, DeviceFeatures, Queue, QueueCreateInfo,
|
||||
QueueFlags,
|
||||
physical::{PhysicalDevice, PhysicalDeviceType},
|
||||
},
|
||||
instance::{Instance, InstanceCreateFlags, InstanceCreateInfo, InstanceExtensions},
|
||||
memory::allocator::StandardMemoryAllocator,
|
||||
swapchain::Surface,
|
||||
};
|
||||
use winit::{
|
||||
event_loop::EventLoop,
|
||||
raw_window_handle::{HasDisplayHandle, HasWindowHandle},
|
||||
};
|
||||
use vulkano_util::context::VulkanoContext;
|
||||
|
||||
pub struct VulkanContext {
|
||||
instance: Arc<Instance>,
|
||||
pub device: Arc<Device>,
|
||||
pub graphics_queue: Arc<Queue>,
|
||||
|
||||
pub memory_allocator: Arc<StandardMemoryAllocator>,
|
||||
pub command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
|
||||
pub descriptor_set_allocator: Arc<StandardDescriptorSetAllocator>,
|
||||
vulkano_context: VulkanoContext,
|
||||
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
|
||||
descriptor_set_allocator: Arc<StandardDescriptorSetAllocator>,
|
||||
}
|
||||
|
||||
impl From<&EventLoop<()>> for VulkanContext {
|
||||
fn from(event_loop: &EventLoop<()>) -> Self {
|
||||
let library = load_library();
|
||||
|
||||
let enabled_extensions = Surface::required_extensions(event_loop).unwrap();
|
||||
log::debug!("Surface required extensions: {enabled_extensions:?}");
|
||||
|
||||
let instance = create_instance(library.clone(), enabled_extensions);
|
||||
|
||||
let (device, mut queues) = pick_graphics_device(&instance, event_loop);
|
||||
let graphics_queue = queues.next().unwrap();
|
||||
|
||||
let memory_allocator = Arc::new(StandardMemoryAllocator::new_default(device.clone()));
|
||||
|
||||
impl VulkanContext {
|
||||
pub fn new(vulkano_context: VulkanoContext) -> Self {
|
||||
let command_buffer_allocator = Arc::new(StandardCommandBufferAllocator::new(
|
||||
device.clone(),
|
||||
vulkano_context.device().clone(),
|
||||
Default::default(),
|
||||
));
|
||||
|
||||
let descriptor_set_allocator = Arc::new(StandardDescriptorSetAllocator::new(
|
||||
device.clone(),
|
||||
vulkano_context.device().clone(),
|
||||
Default::default(),
|
||||
));
|
||||
|
||||
Self {
|
||||
instance,
|
||||
device,
|
||||
graphics_queue,
|
||||
memory_allocator,
|
||||
vulkano_context,
|
||||
command_buffer_allocator,
|
||||
descriptor_set_allocator,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl VulkanContext {
|
||||
pub fn create_surface(
|
||||
&self,
|
||||
window: Arc<impl HasWindowHandle + HasDisplayHandle + Any + Send + Sync>,
|
||||
) -> Arc<Surface> {
|
||||
Surface::from_window(self.instance.clone(), window).unwrap()
|
||||
pub fn vulkano_context(&self) -> &VulkanoContext {
|
||||
&self.vulkano_context
|
||||
}
|
||||
|
||||
pub fn create_render_builder(&self) -> AutoCommandBufferBuilder<PrimaryAutoCommandBuffer> {
|
||||
AutoCommandBufferBuilder::primary(
|
||||
self.command_buffer_allocator.clone(),
|
||||
self.graphics_queue.queue_family_index(),
|
||||
CommandBufferUsage::OneTimeSubmit,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
fn load_library() -> Arc<VulkanLibrary> {
|
||||
let library = VulkanLibrary::new().unwrap();
|
||||
|
||||
log::debug!("Available layer:");
|
||||
for layer in library.layer_properties().unwrap() {
|
||||
log::debug!(
|
||||
"\t - Layer name: {}, Description: {}, Implementation Version: {}, Vulkan Version: {}",
|
||||
layer.name(),
|
||||
layer.description(),
|
||||
layer.implementation_version(),
|
||||
layer.vulkan_version()
|
||||
);
|
||||
pub fn command_buffer_allocator(&self) -> &Arc<StandardCommandBufferAllocator> {
|
||||
&self.command_buffer_allocator
|
||||
}
|
||||
|
||||
library
|
||||
}
|
||||
|
||||
fn create_instance(
|
||||
library: Arc<VulkanLibrary>,
|
||||
required_extensions: InstanceExtensions,
|
||||
) -> Arc<Instance> {
|
||||
Instance::new(
|
||||
library,
|
||||
InstanceCreateInfo {
|
||||
// Enable enumerating devices that use non-conformant Vulkan implementations.
|
||||
// (e.g. MoltenVK)
|
||||
flags: InstanceCreateFlags::ENUMERATE_PORTABILITY,
|
||||
enabled_extensions: required_extensions,
|
||||
enabled_layers: vec![String::from("VK_LAYER_KHRONOS_validation")],
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn find_physical_device_queue_family_indexes(
|
||||
physical_device: &Arc<PhysicalDevice>,
|
||||
event_loop: &EventLoop<()>,
|
||||
) -> Option<u32> {
|
||||
let mut graphic_queue_family_index = None;
|
||||
|
||||
for (i, queue_family_property) in physical_device.queue_family_properties().iter().enumerate() {
|
||||
if queue_family_property
|
||||
.queue_flags
|
||||
.intersects(QueueFlags::GRAPHICS)
|
||||
&& physical_device
|
||||
.presentation_support(i as u32, event_loop)
|
||||
.unwrap()
|
||||
{
|
||||
graphic_queue_family_index = Some(i as u32);
|
||||
}
|
||||
pub fn descriptor_set_allocator(&self) -> &Arc<StandardDescriptorSetAllocator> {
|
||||
&self.descriptor_set_allocator
|
||||
}
|
||||
|
||||
graphic_queue_family_index
|
||||
}
|
||||
|
||||
fn pick_physical_device_and_queue_family_indexes(
|
||||
instance: &Arc<Instance>,
|
||||
event_loop: &EventLoop<()>,
|
||||
device_extensions: &DeviceExtensions,
|
||||
) -> Option<(Arc<PhysicalDevice>, u32)> {
|
||||
instance
|
||||
.enumerate_physical_devices()
|
||||
.unwrap()
|
||||
.filter(|p| {
|
||||
p.api_version() >= Version::V1_3 || p.supported_extensions().khr_dynamic_rendering
|
||||
})
|
||||
.filter(|p| p.supported_extensions().contains(device_extensions))
|
||||
.filter_map(|p| {
|
||||
find_physical_device_queue_family_indexes(&p, event_loop)
|
||||
.and_then(|indexes| Some((p, indexes)))
|
||||
})
|
||||
.min_by_key(|(p, _)| match p.properties().device_type {
|
||||
PhysicalDeviceType::DiscreteGpu => 0,
|
||||
PhysicalDeviceType::IntegratedGpu => 1,
|
||||
PhysicalDeviceType::VirtualGpu => 2,
|
||||
PhysicalDeviceType::Cpu => 3,
|
||||
PhysicalDeviceType::Other => 4,
|
||||
_ => 5,
|
||||
})
|
||||
}
|
||||
|
||||
fn pick_graphics_device(
|
||||
instance: &Arc<Instance>,
|
||||
event_loop: &EventLoop<()>,
|
||||
) -> (
|
||||
Arc<Device>,
|
||||
impl ExactSizeIterator<Item = Arc<Queue>> + use<>,
|
||||
) {
|
||||
let mut device_extensions = DeviceExtensions {
|
||||
khr_swapchain: true,
|
||||
..DeviceExtensions::empty()
|
||||
};
|
||||
|
||||
let (physical_device, graphics_family_index) =
|
||||
pick_physical_device_and_queue_family_indexes(instance, event_loop, &device_extensions)
|
||||
.unwrap();
|
||||
|
||||
log::debug!(
|
||||
"Using device: {} (type: {:?})",
|
||||
physical_device.properties().device_name,
|
||||
physical_device.properties().device_type,
|
||||
);
|
||||
|
||||
if physical_device.api_version() < Version::V1_3 {
|
||||
device_extensions.khr_dynamic_rendering = true;
|
||||
}
|
||||
|
||||
log::debug!("Using device extensions: {:#?}", device_extensions);
|
||||
|
||||
Device::new(
|
||||
physical_device,
|
||||
DeviceCreateInfo {
|
||||
queue_create_infos: vec![QueueCreateInfo {
|
||||
queue_family_index: graphics_family_index,
|
||||
..Default::default()
|
||||
}],
|
||||
enabled_extensions: device_extensions,
|
||||
enabled_features: DeviceFeatures {
|
||||
dynamic_rendering: true,
|
||||
..DeviceFeatures::empty()
|
||||
},
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
|
|
@ -1,102 +0,0 @@
|
|||
use std::sync::Arc;
|
||||
use vulkano::device::Device;
|
||||
use vulkano::image::view::ImageView;
|
||||
use vulkano::image::{Image, ImageUsage};
|
||||
use vulkano::pipeline::graphics::viewport::Viewport;
|
||||
use vulkano::swapchain::{Surface, Swapchain, SwapchainCreateInfo};
|
||||
use vulkano::sync::GpuFuture;
|
||||
use vulkano::{Validated, VulkanError, sync};
|
||||
use winit::window::Window;
|
||||
|
||||
pub struct WindowRenderContext {
|
||||
pub window: Arc<Window>,
|
||||
pub swapchain: Arc<Swapchain>,
|
||||
pub attachment_image_views: Vec<Arc<ImageView>>,
|
||||
pub viewport: Viewport,
|
||||
pub recreate_swapchain: bool,
|
||||
pub previous_frame_end: Option<Box<dyn GpuFuture>>,
|
||||
}
|
||||
|
||||
impl WindowRenderContext {
|
||||
pub fn new(window: Arc<Window>, surface: Arc<Surface>, device: &Arc<Device>) -> Self {
|
||||
let window_size = window.inner_size();
|
||||
|
||||
let (swapchain, images) = {
|
||||
let surface_capabilities = device
|
||||
.physical_device()
|
||||
.surface_capabilities(&surface, Default::default())
|
||||
.unwrap();
|
||||
|
||||
let (image_format, _) = device
|
||||
.physical_device()
|
||||
.surface_formats(&surface, Default::default())
|
||||
.unwrap()[0];
|
||||
|
||||
Swapchain::new(
|
||||
device.clone(),
|
||||
surface,
|
||||
SwapchainCreateInfo {
|
||||
// 2 because with some graphics driver, it crash on fullscreen because fullscreen need to min image to works.
|
||||
min_image_count: surface_capabilities.min_image_count.max(2),
|
||||
image_format,
|
||||
image_extent: window_size.into(),
|
||||
image_usage: ImageUsage::COLOR_ATTACHMENT,
|
||||
composite_alpha: surface_capabilities
|
||||
.supported_composite_alpha
|
||||
.into_iter()
|
||||
.next()
|
||||
.unwrap(),
|
||||
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
let attachment_image_views = window_size_dependent_setup(&images);
|
||||
|
||||
let viewport = Viewport {
|
||||
offset: [0.0, 0.0],
|
||||
extent: window_size.into(),
|
||||
depth_range: 0.0..=1.0,
|
||||
};
|
||||
|
||||
let recreate_swapchain = false;
|
||||
let previous_frame_end = Some(sync::now(device.clone()).boxed());
|
||||
|
||||
Self {
|
||||
window,
|
||||
swapchain,
|
||||
attachment_image_views,
|
||||
viewport,
|
||||
recreate_swapchain,
|
||||
previous_frame_end,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_swapchain(&mut self) -> Result<(), Validated<VulkanError>> {
|
||||
if !self.recreate_swapchain {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let window_size = self.window.inner_size();
|
||||
let (new_swapchain, new_images) = self.swapchain.recreate(SwapchainCreateInfo {
|
||||
image_extent: window_size.into(),
|
||||
..self.swapchain.create_info()
|
||||
})?;
|
||||
|
||||
self.swapchain = new_swapchain;
|
||||
self.attachment_image_views = window_size_dependent_setup(&new_images);
|
||||
self.viewport.extent = window_size.into();
|
||||
self.recreate_swapchain = false;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn window_size_dependent_setup(images: &[Arc<Image>]) -> Vec<Arc<ImageView>> {
|
||||
images
|
||||
.iter()
|
||||
.map(|image| ImageView::new_default(image.clone()).unwrap())
|
||||
.collect::<Vec<_>>()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue