Browse Source

various lints, add cgmath, add basis for Buffer

master
Alison Watson 1 year ago
parent
commit
e25b79ba00
13 changed files with 195 additions and 118 deletions
  1. +15
    -7
      Cargo.toml
  2. +1
    -6
      source/hal/context.rs
  3. +2
    -6
      source/hal/event.rs
  4. +2
    -7
      source/hal/window.rs
  5. +22
    -19
      source/main.rs
  6. +5
    -1
      source/render.rs
  7. +42
    -0
      source/render/buffer.rs
  8. +1
    -3
      source/render/cmd.rs
  9. +1
    -4
      source/render/image.rs
  10. +1
    -2
      source/render/instance.rs
  11. +48
    -47
      source/render/pipeline.rs
  12. +16
    -16
      source/render/swapchain.rs
  13. +39
    -0
      source/render/vertex.rs

+ 15
- 7
Cargo.toml View File

@@ -15,19 +15,27 @@ build = "source/build.rs"
default = ["color-log"]
color-log = ["termcolor"]

[dependencies]
ash = "0.29"
sdl2-sys = "0.32"
serde_yaml = "0.8"
[dependencies.ash]
version = "0.29"

[dependencies.termcolor]
version = "1.0"
optional = true
[dependencies.cgmath]
version = "0.17"
features = ["swizzle"]

[dependencies.sdl2-sys]
version = "0.32"

[dependencies.serde]
version = "1.0"
features = ["derive"]

[dependencies.serde_yaml]
version = "0.8"

[dependencies.termcolor]
version = "1.0"
optional = true

[profile.dev]
opt-level = 1



+ 1
- 6
source/hal/context.rs View File

@@ -1,10 +1,5 @@
use crate::hal::ErrSdl;
use sdl2_sys::{
SDL_INIT_VIDEO,
SDL_Init,
SDL_Quit,
SDL_SetMainReady,
};
use sdl2_sys::{SDL_Init, SDL_Quit, SDL_SetMainReady, SDL_INIT_VIDEO};

pub struct Context;



+ 2
- 6
source/hal/event.rs View File

@@ -1,9 +1,5 @@
use sdl2_sys::{
SDL_Event,
SDL_EventType,
SDL_PollEvent,
SDL_WindowEvent,
SDL_WindowEventID,
SDL_Event, SDL_EventType, SDL_PollEvent, SDL_WindowEvent, SDL_WindowEventID,
};
use std::os::raw::c_int;

@@ -32,7 +28,7 @@ fn read_in_window(evt: SDL_WindowEvent) -> Option<Event> {
match unsafe { std::mem::transmute(c_int::from(evt.event)) } {
SDL_WindowEventID::SDL_WINDOWEVENT_RESIZED => {
Some(Event::Resize(evt.data1, evt.data2))
},
}
_ => None,
}
}


+ 2
- 7
source/hal/window.rs View File

@@ -4,14 +4,9 @@ use ash::{
vk::{self, Handle},
};
use sdl2_sys::{
SDL_CreateWindow,
SDL_DestroyWindow,
SDL_Vulkan_CreateSurface,
SDL_Vulkan_GetInstanceExtensions,
SDL_CreateWindow, SDL_DestroyWindow, SDL_Vulkan_CreateSurface,
SDL_Vulkan_GetInstanceExtensions, SDL_Window, SDL_WindowFlags, SDL_bool,
SDL_WINDOWPOS_UNDEFINED_MASK,
SDL_Window,
SDL_WindowFlags,
SDL_bool,
};
use std::{
marker::PhantomData,


+ 22
- 19
source/main.rs View File

@@ -10,6 +10,7 @@ use crate::{
util::{log, meta},
};
use ash::{version::DeviceV1_0, vk};
use cgmath::{Vector2, Vector3};
use std::rc::Rc;

struct Pipelines {
@@ -44,7 +45,7 @@ impl<'a> SwapchainData<'a> {
let swapchain = Swapchain::create(
device.clone(),
&phys_device,
surface.clone(),
surface,
conf,
qf_info,
)?;
@@ -62,40 +63,42 @@ impl<'a> SwapchainData<'a> {
device.clone(),
&[
PipelineCreateInfo::info_basic(
shader_vert.clone(),
shader_frag.clone(),
layout.clone(),
shader_vert,
shader_frag,
layout,
render_pass.clone(),
swapchain.extent,
),
],
)?;

let pipelines = Pipelines::create_from(pipelines.clone());
let pipelines = Pipelines::create_from(pipelines);

let framebuffers = Framebuffer::create_all(
device.clone(),
device,
render_pass.clone(),
&image_views,
swapchain.extent
swapchain.extent,
)?;

let cmd_buffers = CommandPool::allocate_and_bind_buffers(
cmd_pool.clone(),
cmd_pool,
&framebuffers,
render_pass.clone(),
pipelines.main.clone(),
render_pass,
pipelines.main,
swapchain.extent,
)?;

Ok(Self {
swapchain,
image_f,
cmd_buffers,
})
Ok(Self { swapchain, image_f, cmd_buffers })
}
}

const VERTICES: &[Vertex] = &[
Vertex { pos: Vector2::new( 0.0, -0.5), color: Vector3::new(1.0, 0.0, 0.0) },
Vertex { pos: Vector2::new( 0.5, 0.5), color: Vector3::new(0.0, 1.0, 0.0) },
Vertex { pos: Vector2::new(-0.5, 0.5), color: Vector3::new(0.0, 0.0, 1.0) },
];

const MAIN_VERT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/main.vert.o"));
const MAIN_FRAG: &[u8] =
@@ -209,7 +212,7 @@ fn fallback_main(

let qf_info = QueueFamilyInfo::collect(&instance, &surface, &phys_device)?;

let device = Device::create(instance.clone(), &phys_device, &qf_info)?;
let device = Device::create(instance, &phys_device, &qf_info)?;

let image_avail_s = Semaphore::create_all(device.clone(), concur_frames)?;
let render_fini_s = Semaphore::create_all(device.clone(), concur_frames)?;
@@ -246,7 +249,7 @@ fn fallback_main(
for event in hal::EventIterator {
match event {
hal::Event::Quit => break 'main_loop,
hal::Event::Resize(_, _) => {},
hal::Event::Resize(_, _) => {}
}
}

@@ -263,7 +266,7 @@ fn fallback_main(
);

match frame {
Ok(()) => {},
Ok(()) => {}
Err(vk::Result::ERROR_OUT_OF_DATE_KHR) => {
seize_device(lg, &device);
sc_data = SwapchainData::create(
@@ -280,7 +283,7 @@ fn fallback_main(
}
Err(res) => {
lg!(lg, Level::Error, "Error rendering frame: {}", res);
},
}
}

cur_c_frame = (cur_c_frame + 1) % concur_frames;


+ 5
- 1
source/render.rs View File

@@ -1,3 +1,4 @@
mod buffer;
mod cmd;
mod conf;
mod device;
@@ -16,11 +17,13 @@ mod shader;
mod spir;
mod surface;
mod swapchain;
mod vertex;

pub use self::{
buffer::Buffer,
cmd::{CommandBuffer, CommandPool},
conf::{Conf, PresentMode},
device::{ErrDeviceCreate, ErrPhysicalDeviceGet, Device, PhysicalDevice},
device::{Device, ErrDeviceCreate, ErrPhysicalDeviceGet, PhysicalDevice},
fence::Fence,
framebuffer::Framebuffer,
image::OwnedImage,
@@ -36,6 +39,7 @@ pub use self::{
spir::Spir,
surface::Surface,
swapchain::Swapchain,
vertex::Vertex,
};

// EOF

+ 42
- 0
source/render/buffer.rs View File

@@ -0,0 +1,42 @@
use crate::render::{Device, Vertex};
use ash::{version::DeviceV1_0, vk};
use std::rc::Rc;

pub struct Buffer {
handle: vk::Buffer,

pub device: Rc<Device>,
}

impl Buffer {
pub fn create_vertex(
device: Rc<Device>,
num_vertices: usize,
) -> Result<Rc<Self>, vk::Result> {
let create_info = vk::BufferCreateInfo {
size: (std::mem::size_of::<Vertex>() * num_vertices) as u64,
usage: vk::BufferUsageFlags::VERTEX_BUFFER,
sharing_mode: vk::SharingMode::EXCLUSIVE,
..Default::default()
};
let handle = unsafe { device.create_buffer(&create_info, None)? };
Ok(Rc::new(Self { handle, device }))
}
}

impl Drop for Buffer {
fn drop(&mut self) {
unsafe {
self.device.destroy_buffer(self.handle, None);
}
}
}

impl std::ops::Deref for Buffer {
type Target = vk::Buffer;
fn deref(&self) -> &Self::Target {
&self.handle
}
}

// EOF

+ 1
- 3
source/render/cmd.rs View File

@@ -85,9 +85,7 @@ impl CommandBuffer {

let clear_values = [
vk::ClearValue {
color: vk::ClearColorValue {
float32: [0.0, 0.0, 0.0, 1.0],
}
color: vk::ClearColorValue { float32: [0.0, 0.0, 0.0, 1.0] }
}
];



+ 1
- 4
source/render/image.rs View File

@@ -9,10 +9,7 @@ pub struct OwnedImage {
}

impl OwnedImage {
pub fn own(
swapchain: Rc<Swapchain>,
handle: vk::Image,
) -> Rc<Self> {
pub fn own(swapchain: Rc<Swapchain>, handle: vk::Image) -> Rc<Self> {
Rc::new(Self { handle, swapchain })
}
}


+ 1
- 2
source/render/instance.rs View File

@@ -4,10 +4,9 @@ use crate::{
util::meta,
};
use ash::{
InstanceError,
extensions::khr,
version::{EntryV1_0, InstanceV1_0},
vk, vk_make_version,
vk, vk_make_version, InstanceError,
};
use std::{os::raw::c_char, rc::Rc};



+ 48
- 47
source/render/pipeline.rs View File

@@ -1,4 +1,4 @@
use crate::render::{Device, PipelineLayout, RenderPass, ShaderModule};
use crate::render::{Device, PipelineLayout, RenderPass, ShaderModule, Vertex};
use ash::{version::DeviceV1_0, vk};
use std::rc::Rc;

@@ -33,6 +33,8 @@ impl PipelineCreateInfo {
render_pass: Rc<RenderPass>,
extent: vk::Extent2D,
) -> Self {
let bind_desc = Vertex::bind_descr();
let attr_desc = Vertex::attr_descr();
Self {
layout,
render_pass,
@@ -51,7 +53,13 @@ impl PipelineCreateInfo {
..Default::default()
},
],
vertex_input_state: vk::PipelineVertexInputStateCreateInfo::default(),
vertex_input_state: vk::PipelineVertexInputStateCreateInfo {
vertex_binding_description_count: bind_desc.len() as u32,
p_vertex_binding_descriptions: bind_desc.as_ptr(),
vertex_attribute_description_count: attr_desc.len() as u32,
p_vertex_attribute_descriptions: attr_desc.as_ptr(),
..Default::default()
},
input_assembly_state: vk::PipelineInputAssemblyStateCreateInfo {
topology: vk::PrimitiveTopology::TRIANGLE_LIST,
primitive_restart_enable: vk::FALSE,
@@ -98,53 +106,46 @@ impl Pipeline {
device: Rc<Device>,
infos: &[PipelineCreateInfo],
) -> Result<Vec<Rc<Self>>, vk::Result> {
let viewport_states =
infos
.iter()
.map(|info| {
vk::PipelineViewportStateCreateInfo {
viewport_count: info.viewports.len() as u32,
p_viewports: info.viewports.as_ptr(),
scissor_count: info.scissors.len() as u32,
p_scissors: info.scissors.as_ptr(),
..Default::default()
}
})
.collect::<Vec<_>>();
let viewport_states = infos
.iter()
.map(|info| vk::PipelineViewportStateCreateInfo {
viewport_count: info.viewports.len() as u32,
p_viewports: info.viewports.as_ptr(),
scissor_count: info.scissors.len() as u32,
p_scissors: info.scissors.as_ptr(),
..Default::default()
})
.collect::<Vec<_>>();

let color_blend_states =
infos
.iter()
.map(|info| {
vk::PipelineColorBlendStateCreateInfo {
attachment_count: info.color_blend_attachments.len() as u32,
p_attachments: info.color_blend_attachments.as_ptr(),
..Default::default()
}
})
.collect::<Vec<_>>();
let color_blend_states = infos
.iter()
.map(|info| vk::PipelineColorBlendStateCreateInfo {
attachment_count: info.color_blend_attachments.len() as u32,
p_attachments: info.color_blend_attachments.as_ptr(),
..Default::default()
})
.collect::<Vec<_>>();

let create_info =
infos
.iter()
.zip(viewport_states.iter())
.zip(color_blend_states.iter())
.map(|((info, viewport_state), color_blend_state)| {
vk::GraphicsPipelineCreateInfo {
layout: **info.layout,
render_pass: **info.render_pass,
stage_count: info.shader_stages.len() as u32,
p_stages: info.shader_stages.as_ptr(),
p_vertex_input_state: &info.vertex_input_state,
p_input_assembly_state: &info.input_assembly_state,
p_viewport_state: viewport_state,
p_rasterization_state: &info.rasterization_state,
p_multisample_state: &info.multisample_state,
p_color_blend_state: color_blend_state,
..Default::default()
}
})
.collect::<Vec<_>>();
let create_info = infos
.iter()
.zip(viewport_states.iter())
.zip(color_blend_states.iter())
.map(|((info, viewport_state), color_blend_state)| {
vk::GraphicsPipelineCreateInfo {
layout: **info.layout,
render_pass: **info.render_pass,
stage_count: info.shader_stages.len() as u32,
p_stages: info.shader_stages.as_ptr(),
p_vertex_input_state: &info.vertex_input_state,
p_input_assembly_state: &info.input_assembly_state,
p_viewport_state: viewport_state,
p_rasterization_state: &info.rasterization_state,
p_multisample_state: &info.multisample_state,
p_color_blend_state: color_blend_state,
..Default::default()
}
})
.collect::<Vec<_>>();

let pipelines = unsafe {
device


+ 16
- 16
source/render/swapchain.rs View File

@@ -30,7 +30,8 @@ impl Swapchain {
};

let formats = unsafe {
device.instance
device
.instance
.surface_ext
.get_physical_device_surface_formats(**phys_device, **surface)?
};
@@ -42,21 +43,20 @@ impl Swapchain {
)?
};

let extent =
if capabilities.current_extent.width != u32::max_value() {
capabilities.current_extent
} else {
vk::Extent2D {
width: capabilities
.min_image_extent
.width
.max(capabilities.max_image_extent.width),
height: capabilities
.min_image_extent
.height
.max(capabilities.max_image_extent.height),
}
};
let extent = if capabilities.current_extent.width != u32::max_value() {
capabilities.current_extent
} else {
vk::Extent2D {
width: capabilities
.min_image_extent
.width
.max(capabilities.max_image_extent.width),
height: capabilities
.min_image_extent
.height
.max(capabilities.max_image_extent.height),
}
};

let format = *formats
.iter()


+ 39
- 0
source/render/vertex.rs View File

@@ -0,0 +1,39 @@
use ash::vk;
use cgmath::{Vector2, Vector3};

#[repr(C, packed)]
pub struct Vertex {
pub pos: Vector2<f32>,
pub color: Vector3<f32>,
}

impl Vertex {
pub const fn bind_descr() -> [vk::VertexInputBindingDescription; 1] {
[
vk::VertexInputBindingDescription {
binding: 0,
stride: std::mem::size_of::<Self>() as u32,
input_rate: vk::VertexInputRate::VERTEX,
}
]
}

pub const fn attr_descr() -> [vk::VertexInputAttributeDescription; 2] {
[
vk::VertexInputAttributeDescription {
location: 0,
binding: 0,
format: vk::Format::R32G32_SFLOAT,
offset: 0,
},
vk::VertexInputAttributeDescription {
location: 1,
binding: 0,
format: vk::Format::R32G32B32_SFLOAT,
offset: std::mem::size_of::<Vector2<f32>>() as u32,
},
]
}
}

// EOF

Loading…
Cancel
Save