pre vertex transfer buffer

This commit is contained in:
zomseffen 2024-04-18 19:00:01 +02:00
parent 017cfcf82f
commit ff4302837e
5 changed files with 160 additions and 15 deletions

View file

@ -1,20 +1,11 @@
#version 450
vec2 positions[3] = vec2[](
vec2(0.0, -0.5),
vec2(0.5, 0.5),
vec2(-0.5, 0.5)
);
vec3 colors[3] = vec3[](
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, 1.0)
);
layout(location = 0) in vec2 inPosition;
layout(location = 1) in vec3 inColor;
layout(location = 0) out vec3 fragColor;
void main() {
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
fragColor = colors[gl_VertexIndex];
gl_Position = vec4(inPosition, 0.0, 1.0);
fragColor = inColor;
}

Binary file not shown.

View file

@ -27,4 +27,7 @@ pub struct AppData {
pub in_flight_fences: Vec<vk::Fence>,
pub images_in_flight: Vec<vk::Fence>,
pub vertex_buffer: vk::Buffer,
pub vertex_buffer_memory: vk::DeviceMemory,
}

View file

@ -27,10 +27,16 @@ use vulkanalia::vk::ExtDebugUtilsExtension;
use vulkanalia::vk::KhrSurfaceExtension;
use vulkanalia::vk::KhrSwapchainExtension;
use cgmath::{vec2, vec3};
use std::mem::size_of;
use std::ptr::copy_nonoverlapping as memcpy;
pub mod app_data;
pub mod errors;
pub mod swapchain;
pub mod queue_family_indices;
pub mod vertex;
const PORTABILITY_MACOS_VERSION: Version = Version::new(1, 3, 216);
const VALIDATION_ENABLED: bool =
@ -42,6 +48,12 @@ const DEVICE_EXTENSIONS: &[vk::ExtensionName] = &[vk::KHR_SWAPCHAIN_EXTENSION.na
const MAX_FRAMES_IN_FLIGHT: usize = 2;
static VERTICES: [vertex::Vertex; 3] = [
vertex::Vertex::new(vec2(0.0, -0.5), vec3(1.0, 0.0, 0.0)),
vertex::Vertex::new(vec2(0.5, 0.5), vec3(0.0, 1.0, 0.0)),
vertex::Vertex::new(vec2(-0.5, 0.5), vec3(0.0, 0.0, 1.0)),
];
fn main() -> Result<()> {
pretty_env_logger::init();
@ -118,6 +130,8 @@ impl App {
create_command_pool(&instance, &device, &mut data)?;
create_vertex_buffer(&instance, &device, &mut data)?;
create_command_buffers(&device, &mut data)?;
create_sync_objects(&device, &mut data)?;
@ -191,6 +205,9 @@ impl App {
unsafe fn destroy(&mut self) {
self.destroy_swapchain();
self.device.destroy_buffer(self.data.vertex_buffer, None);
self.device.free_memory(self.data.vertex_buffer_memory, None);
self.data.in_flight_fences
.iter()
.for_each(|f| self.device.destroy_fence(*f, None));
@ -477,7 +494,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu
.module(frag_shader_module)
.name(b"main\0");
let vertex_input_state = vk::PipelineVertexInputStateCreateInfo::builder();
let binding_descriptions = &[vertex::Vertex::binding_description()];
let attribute_descriptions = vertex::Vertex::attribute_descriptions();
let vertex_input_state = vk::PipelineVertexInputStateCreateInfo::builder()
.vertex_binding_descriptions(binding_descriptions)
.vertex_attribute_descriptions(&attribute_descriptions);
let input_assembly_state = vk::PipelineInputAssemblyStateCreateInfo::builder()
.topology(vk::PrimitiveTopology::TRIANGLE_LIST)
@ -690,7 +711,8 @@ unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppData)
device.cmd_bind_pipeline(
*command_buffer, vk::PipelineBindPoint::GRAPHICS, data.pipeline);
device.cmd_draw(*command_buffer, 3, 1, 0, 0);
device.cmd_bind_vertex_buffers(*command_buffer, 0, &[data.vertex_buffer], &[0]);
device.cmd_draw(*command_buffer, VERTICES.len() as u32, 1, 0, 0);
device.cmd_end_render_pass(*command_buffer);
@ -721,3 +743,86 @@ unsafe fn create_sync_objects(device: &Device, data: &mut app_data::AppData) ->
Ok(())
}
unsafe fn create_buffer(
instance: &Instance,
device: &Device,
data: &app_data::AppData,
size: vk::DeviceSize,
usage: vk::BufferUsageFlags,
properties: vk::MemoryPropertyFlags,
) -> Result<(vk::Buffer, vk::DeviceMemory)> {
let buffer_info = vk::BufferCreateInfo::builder()
.size(size)
.usage(usage)
.sharing_mode(vk::SharingMode::EXCLUSIVE);
let buffer = device.create_buffer(&buffer_info, None)?;
let requirements = device.get_buffer_memory_requirements(buffer);
let memory_info = vk::MemoryAllocateInfo::builder()
.allocation_size(requirements.size)
.memory_type_index(get_memory_type_index(
instance,
data,
properties,
requirements,
)?);
let buffer_memory = device.allocate_memory(&memory_info, None)?;
device.bind_buffer_memory(buffer, buffer_memory, 0)?;
Ok((buffer, buffer_memory))
}
unsafe fn create_vertex_buffer(
instance: &Instance,
device: &Device,
data: &mut app_data::AppData,
) -> Result<()> {
let size = (size_of::<vertex::Vertex>() * VERTICES.len()) as u64;
let (vertex_buffer, vertex_buffer_memory) = create_buffer(
instance,
device,
data,
size,
vk::BufferUsageFlags::VERTEX_BUFFER,
vk::MemoryPropertyFlags::HOST_COHERENT | vk::MemoryPropertyFlags::HOST_VISIBLE,
)?;
data.vertex_buffer = vertex_buffer;
data.vertex_buffer_memory = vertex_buffer_memory;
let memory = device.map_memory(
vertex_buffer_memory,
0,
size,
vk::MemoryMapFlags::empty(),
)?;
memcpy(VERTICES.as_ptr(), memory.cast(), VERTICES.len());
device.unmap_memory(vertex_buffer_memory);
Ok(())
}
unsafe fn get_memory_type_index(
instance: &Instance,
data: &app_data::AppData,
properties: vk::MemoryPropertyFlags,
requirements: vk::MemoryRequirements,
) -> Result<u32> {
let memory = instance.get_physical_device_memory_properties(data.physical_device);
(0..memory.memory_type_count)
.find(|i| {
let suitable = (requirements.memory_type_bits & (1 << i)) != 0;
let memory_type = memory.memory_types[*i as usize];
suitable && memory_type.property_flags.contains(properties)
})
.ok_or_else(|| anyhow!("Failed to find suitable memory type."))
}

46
src/vertex.rs Normal file
View file

@ -0,0 +1,46 @@
use vulkanalia::prelude::v1_0::*;
use std::mem::size_of;
use cgmath;
type Vec2 = cgmath::Vector2<f32>;
type Vec3 = cgmath::Vector3<f32>;
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Vertex {
pub pos: Vec2,
pub color: Vec3,
}
impl Vertex {
pub const fn new(pos: Vec2, color: Vec3) -> Self {
Self { pos, color }
}
pub fn binding_description() -> vk::VertexInputBindingDescription {
vk::VertexInputBindingDescription::builder()
.binding(0)
.stride(size_of::<Vertex>() as u32)
.input_rate(vk::VertexInputRate::VERTEX)
.build()
}
pub fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 2] {
let pos = vk::VertexInputAttributeDescription::builder()
.binding(0)
.location(0)
.format(vk::Format::R32G32_SFLOAT)
.offset(0)
.build();
let color = vk::VertexInputAttributeDescription::builder()
.binding(0)
.location(1)
.format(vk::Format::R32G32B32_SFLOAT)
.offset(size_of::<Vec2>() as u32)
.build();
[pos, color]
}
}