Update
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 0s

This commit is contained in:
Florian RICHER 2024-11-18 17:37:34 +01:00
parent f67804abd9
commit 504fdada42
5 changed files with 245 additions and 63 deletions

View file

@ -1,8 +1,6 @@
use crate::vulkan::vk_shader_module::VkShaderModule;
use crate::vulkan::{VkDevice, VkInstance, VkPhysicalDevice, VkSurface, VkSwapchain};
use crate::vulkan::vk_render_pass::VkRenderPass;
use crate::vulkan::{VkDevice, VkGraphicsPipeline, VkInstance, VkPhysicalDevice, VkSurface, VkSwapchain};
use ash::vk;
use ash::vk::PrimitiveTopology;
use std::ffi::{CStr, CString};
use std::sync::Arc;
pub struct VkRenderContext {
@ -29,7 +27,7 @@ impl VkRenderContext {
Some(vk::QueueFlags::GRAPHICS),
Some(&surface),
)
.ok_or_else(|| anyhow::anyhow!("Unable to find physical device"))?;
.ok_or_else(|| anyhow::anyhow!("Unable to find physical device"))?;
log::debug!(
"Selected queue {properties:#?} for physical device {:?}",
physical_device.properties.device_name_as_c_str()
@ -48,69 +46,51 @@ impl VkRenderContext {
&physical_device,
)?);
let shader_entry_name = CStr::from_bytes_with_nul(b"main\0")?;
let render_pass = Arc::new(VkRenderPass::new(
device.clone(),
swapchain.clone(),
)?);
let vert_shader_module =
VkShaderModule::from_spv_file(device.clone(), "res/shaders/main.vert.spv")?;
let pipeline = Arc::new(VkGraphicsPipeline::new(
device.clone(),
swapchain.clone(),
render_pass.clone(),
)?);
let vert_shader_info = vk::PipelineShaderStageCreateInfo::default()
.module(vert_shader_module.handle)
.name(shader_entry_name)
.stage(vk::ShaderStageFlags::VERTEX);
let framebuffers = swapchain.present_image_views
.as_ref()
.ok_or_else(|| anyhow::anyhow!("No present image views found"))?
.iter()
.map(|present_image_view| {
let attachments = [*present_image_view];
let framebuffer_info = vk::FramebufferCreateInfo::default()
.render_pass(render_pass.handle)
.width(swapchain.surface_resolution.width)
.height(swapchain.surface_resolution.height)
.attachments(&attachments)
.layers(1);
let frag_shader_module =
VkShaderModule::from_spv_file(device.clone(), "res/shaders/main.frag.spv")?;
unsafe { device.handle.create_framebuffer(&framebuffer_info, None).unwrap() }
})
.collect::<Vec<_>>();
let frag_shader_info = vk::PipelineShaderStageCreateInfo::default()
.module(frag_shader_module.handle)
.name(shader_entry_name)
.stage(vk::ShaderStageFlags::FRAGMENT);
let command_pool_info = vk::CommandPoolCreateInfo::default()
.queue_family_index(device.queue_family_index);
let command_pool = unsafe { device.handle.create_command_pool(&command_pool_info, None)? };
let shader_stage_create_infos = [vert_shader_info, frag_shader_info];
let command_buffer_info = vk::CommandBufferAllocateInfo::default()
.command_pool(command_pool)
.level(vk::CommandBufferLevel::PRIMARY)
.command_buffer_count(framebuffers.len() as u32);
let vertex_input_info = vk::PipelineVertexInputStateCreateInfo::default();
// Destroyed with command pool
let command_buffers = unsafe { device.handle.allocate_command_buffers(&command_buffer_info)? };
let input_assembly = vk::PipelineInputAssemblyStateCreateInfo::default()
.topology(PrimitiveTopology::TRIANGLE_LIST);
let viewport = vk::Viewport::default()
.width(swapchain.surface_resolution.width as f32)
.height(swapchain.surface_resolution.height as f32)
.max_depth(1.0);
let scissor = vk::Rect2D::default().extent(swapchain.surface_resolution);
let viewport_state = vk::PipelineViewportStateCreateInfo::default()
.viewports(&[viewport])
.scissors(&[scissor]);
let rasterizer = vk::PipelineRasterizationStateCreateInfo::default()
.polygon_mode(vk::PolygonMode::FILL)
.cull_mode(vk::CullModeFlags::BACK)
.front_face(vk::FrontFace::CLOCKWISE)
.line_width(1.0);
let multisampling = vk::PipelineMultisampleStateCreateInfo::default()
.rasterization_samples(vk::SampleCountFlags::TYPE_1)
.min_sample_shading(1.0);
let color_blend_attachment = vk::PipelineColorBlendAttachmentState::default()
.color_write_mask(vk::ColorComponentFlags::RGBA);
let color_blending =
vk::PipelineColorBlendStateCreateInfo::default().attachments(&[color_blend_attachment]);
let dynamic_state = vk::PipelineDynamicStateCreateInfo::default()
.dynamic_states(&[vk::DynamicState::VIEWPORT, vk::DynamicState::LINE_WIDTH]);
let pipeline_layout_info = vk::PipelineLayoutCreateInfo::default();
let pipeline_layout = unsafe {
device
.handle
.create_pipeline_layout(&pipeline_layout_info, None)?
};
unsafe { device.handle.destroy_pipeline_layout(pipeline_layout, None) };
unsafe { device.handle.destroy_command_pool(command_pool, None) };
for framebuffer in framebuffers {
unsafe { device.handle.destroy_framebuffer(framebuffer, None) };
}
Ok(Self {
instance,