020: first triangle
This commit is contained in:
parent
87b53d546a
commit
483be0c669
4 changed files with 67 additions and 28 deletions
|
|
@ -38,17 +38,17 @@ impl BufferAllocator {
|
|||
&mut self,
|
||||
device: &ash::Device,
|
||||
size: u64,
|
||||
flags: vk::BufferUsageFlags,
|
||||
location: MemoryLocation,
|
||||
) -> Result<Buffer, EngineError> {
|
||||
let vk_info = vk::BufferCreateInfo::default()
|
||||
.size(size)
|
||||
.usage(vk::BufferUsageFlags::STORAGE_BUFFER);
|
||||
let vk_info = vk::BufferCreateInfo::default().size(size).usage(flags);
|
||||
|
||||
let buffer = unsafe { device.create_buffer(&vk_info, None) }.unwrap();
|
||||
let requirements = unsafe { device.get_buffer_memory_requirements(buffer) };
|
||||
let allocation = self.allocator.allocate(&AllocationCreateDesc {
|
||||
name: "Example allocation",
|
||||
requirements,
|
||||
location: MemoryLocation::CpuToGpu,
|
||||
location,
|
||||
linear: true, // Buffers are always linear
|
||||
allocation_scheme: AllocationScheme::GpuAllocatorManaged,
|
||||
})?;
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ pub struct EngineInstance {
|
|||
pub surface_format: vk::SurfaceFormatKHR,
|
||||
pub physical_device: vk::PhysicalDevice,
|
||||
pub physical_device_properties: vk::PhysicalDeviceProperties,
|
||||
pub physical_device_features: vk::PhysicalDeviceFeatures,
|
||||
pub physical_device_memory_properties: vk::PhysicalDeviceMemoryProperties,
|
||||
pub device: ash::Device,
|
||||
pub queues: Queues,
|
||||
|
|
@ -69,8 +70,8 @@ impl EngineInstance {
|
|||
let surface = Surface::init(&entry, &ash_instance, &window)?;
|
||||
// let (surface, surface_loader) = Self::init_surface(&entry, &ash_instance, &window)?;
|
||||
|
||||
log::debug!("Init device");
|
||||
let (physical_device, physical_device_properties) =
|
||||
log::debug!("Select physical device");
|
||||
let (physical_device, physical_device_properties, physical_device_features) =
|
||||
Self::select_physical_device(&ash_instance)?;
|
||||
let physical_device_memory_properties =
|
||||
unsafe { ash_instance.get_physical_device_memory_properties(physical_device) };
|
||||
|
|
@ -88,9 +89,15 @@ impl EngineInstance {
|
|||
let queue_families = QueueFamilies::init(&ash_instance, physical_device, &surface)?;
|
||||
let queueinfo = queue_families.make_creation_info()?;
|
||||
|
||||
let features = vk::PhysicalDeviceFeatures::default().fill_mode_non_solid(true);
|
||||
log::debug!("Create device");
|
||||
let device =
|
||||
Self::create_device(&ash_instance, physical_device, &layers_pointers, &queueinfo)?;
|
||||
let device = Self::create_device(
|
||||
&ash_instance,
|
||||
physical_device,
|
||||
&layers_pointers,
|
||||
&queueinfo,
|
||||
&features,
|
||||
)?;
|
||||
|
||||
log::debug!("Create queues");
|
||||
let queues = queue_families.create_queues(&device)?;
|
||||
|
|
@ -113,21 +120,37 @@ impl EngineInstance {
|
|||
|
||||
let pbuflen1: u32 = 16;
|
||||
let pbuflen2: u32 = 20;
|
||||
let nvertex: u32 = 1;
|
||||
let ninstance: u32 = 6;
|
||||
let nvertex: u32 = 6;
|
||||
let ninstance: u32 = 1;
|
||||
|
||||
log::debug!("Create allocator");
|
||||
let mut allocator = BufferAllocator::init(&ash_instance, physical_device, &device)?;
|
||||
let mut buffer1 = allocator.create_buffer(&device, pbuflen1 as u64 * ninstance as u64)?;
|
||||
let mut buffer1 = allocator.create_buffer(
|
||||
&device,
|
||||
pbuflen1 as u64 * nvertex as u64,
|
||||
vk::BufferUsageFlags::VERTEX_BUFFER,
|
||||
gpu_allocator::MemoryLocation::CpuToGpu,
|
||||
)?;
|
||||
unsafe {
|
||||
buffer1.fill(&[
|
||||
0.4f32, -0.2f32, 0.0f32, 1.0f32, 0.2f32, 0.0f32, 0.0f32, 1.0f32, -0.4f32, 0.2f32,
|
||||
0.0f32, 1.0f32, 0.5f32, 0.0f32, 0.0f32, 1.0f32, 0.0f32, 0.2f32, 0.0f32, 1.0f32,
|
||||
-0.5f32, 0.0f32, 0.0f32, 1.0f32,
|
||||
0.5f32, 0.0f32, 0.0f32, 1.0f32, 0.0f32, 0.2f32, 0.0f32, 1.0f32, -0.5f32, 0.0f32,
|
||||
0.0f32, 1.0f32, -0.9f32, -0.9f32, 0.0f32, 1.0f32, 0.3f32, -0.8f32, 0.0f32, 1.0f32,
|
||||
0.0f32, -0.6f32, 0.0f32, 1.0f32,
|
||||
])
|
||||
};
|
||||
let mut buffer2 = allocator.create_buffer(
|
||||
&device,
|
||||
pbuflen2 as u64 * nvertex as u64,
|
||||
vk::BufferUsageFlags::VERTEX_BUFFER,
|
||||
gpu_allocator::MemoryLocation::CpuToGpu,
|
||||
)?;
|
||||
unsafe {
|
||||
buffer2.fill(&[
|
||||
1.0f32, 0.0f32, 1.0f32, 0.0f32, 1.0f32, 1.0f32, 0.0f32, 1.0f32, 0.0f32, 1.0f32,
|
||||
1.0f32, 0.0f32, 1.0f32, 0.0f32, 1.0f32, 1.0f32, 0.8f32, 0.7f32, 0.0f32, 1.0f32,
|
||||
1.0f32, 1.0f32, 0.0f32, 0.0f32, 1.0f32, 1.0f32, 0.0f32, 0.0f32, 1.0f32, 1.0f32,
|
||||
])
|
||||
};
|
||||
let mut buffer2 = allocator.create_buffer(&device, pbuflen2 as u64 * nvertex as u64)?;
|
||||
unsafe { buffer2.fill(&[15.0f32, 0.0f32, 1.0f32, 0.0f32, 1.0f32]) };
|
||||
|
||||
let vertex_attrib_descs = [
|
||||
vk::VertexInputAttributeDescription {
|
||||
|
|
@ -153,7 +176,7 @@ impl EngineInstance {
|
|||
vk::VertexInputBindingDescription {
|
||||
binding: 0,
|
||||
stride: pbuflen1,
|
||||
input_rate: vk::VertexInputRate::INSTANCE,
|
||||
input_rate: vk::VertexInputRate::VERTEX,
|
||||
},
|
||||
vk::VertexInputBindingDescription {
|
||||
binding: 1,
|
||||
|
|
@ -169,6 +192,8 @@ impl EngineInstance {
|
|||
&renderpass,
|
||||
&vertex_attrib_descs,
|
||||
&vertex_binding_descs,
|
||||
vk::PrimitiveTopology::TRIANGLE_LIST,
|
||||
vk::PolygonMode::FILL,
|
||||
)?;
|
||||
|
||||
log::debug!("Init pools");
|
||||
|
|
@ -199,6 +224,7 @@ impl EngineInstance {
|
|||
debug: std::mem::ManuallyDrop::new(debug),
|
||||
physical_device,
|
||||
physical_device_properties,
|
||||
physical_device_features,
|
||||
physical_device_memory_properties,
|
||||
device,
|
||||
queues,
|
||||
|
|
@ -279,7 +305,14 @@ impl EngineInstance {
|
|||
|
||||
fn select_physical_device(
|
||||
instance: &ash::Instance,
|
||||
) -> Result<(vk::PhysicalDevice, vk::PhysicalDeviceProperties), EngineError> {
|
||||
) -> Result<
|
||||
(
|
||||
vk::PhysicalDevice,
|
||||
vk::PhysicalDeviceProperties,
|
||||
vk::PhysicalDeviceFeatures,
|
||||
),
|
||||
EngineError,
|
||||
> {
|
||||
let phys_devs: Vec<vk::PhysicalDevice> = unsafe { instance.enumerate_physical_devices()? };
|
||||
|
||||
let mut primary = None;
|
||||
|
|
@ -289,23 +322,25 @@ impl EngineInstance {
|
|||
|
||||
for p in phys_devs {
|
||||
let properties = unsafe { instance.get_physical_device_properties(p) };
|
||||
let features = unsafe { instance.get_physical_device_features(p) };
|
||||
let tuplet = (p, properties, features);
|
||||
|
||||
match properties.device_type {
|
||||
vk::PhysicalDeviceType::DISCRETE_GPU => {
|
||||
// if a dedicated is present, prioritize it
|
||||
primary = Some((p, properties));
|
||||
primary = Some(tuplet);
|
||||
break;
|
||||
}
|
||||
vk::PhysicalDeviceType::INTEGRATED_GPU => {
|
||||
// integrated as secondary
|
||||
secondary = Some((p, properties));
|
||||
secondary = Some(tuplet);
|
||||
}
|
||||
vk::PhysicalDeviceType::VIRTUAL_GPU => {
|
||||
// virtual as tertiary
|
||||
tertiary = Some((p, properties));
|
||||
tertiary = Some(tuplet);
|
||||
}
|
||||
_ => {
|
||||
fallback = Some((p, properties));
|
||||
fallback = Some(tuplet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -331,6 +366,7 @@ impl EngineInstance {
|
|||
physical_device: vk::PhysicalDevice,
|
||||
enabled_layer_names: &[*const i8],
|
||||
queue_infos: &[vk::DeviceQueueCreateInfo],
|
||||
features: &vk::PhysicalDeviceFeatures,
|
||||
) -> Result<ash::Device, EngineError> {
|
||||
//TODO: dedicated function
|
||||
let device_extension_names = Self::device_extensions();
|
||||
|
|
@ -339,7 +375,8 @@ impl EngineInstance {
|
|||
let device_create_info = vk::DeviceCreateInfo::default()
|
||||
.queue_create_infos(&queue_infos)
|
||||
.enabled_extension_names(&device_extension_name_pointers)
|
||||
.enabled_layer_names(enabled_layer_names); //deprecated, only for compatibility reason
|
||||
.enabled_layer_names(enabled_layer_names) //deprecated, only for compatibility reason
|
||||
.enabled_features(features);
|
||||
|
||||
let logical_device =
|
||||
unsafe { instance.create_device(physical_device, &device_create_info, None)? };
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@ impl Pipeline {
|
|||
renderpass: &vk::RenderPass,
|
||||
vertex_attrib_descs: &[vk::VertexInputAttributeDescription],
|
||||
vertex_binding_descs: &[vk::VertexInputBindingDescription],
|
||||
topology: vk::PrimitiveTopology,
|
||||
polygon_mode: vk::PolygonMode,
|
||||
) -> Result<Pipeline, vk::Result> {
|
||||
let vertexshader_createinfo = vk::ShaderModuleCreateInfo::default()
|
||||
.code(vk_shader_macros::include_glsl!("./shaders/shader.vert", kind: vert));
|
||||
|
|
@ -48,8 +50,8 @@ impl Pipeline {
|
|||
.vertex_attribute_descriptions(&vertex_attrib_descs)
|
||||
.vertex_binding_descriptions(&vertex_binding_descs);
|
||||
|
||||
let input_assembly_info = vk::PipelineInputAssemblyStateCreateInfo::default()
|
||||
.topology(vk::PrimitiveTopology::POINT_LIST);
|
||||
let input_assembly_info =
|
||||
vk::PipelineInputAssemblyStateCreateInfo::default().topology(topology);
|
||||
let viewports = [vk::Viewport {
|
||||
x: 0.,
|
||||
y: 0.,
|
||||
|
|
@ -70,7 +72,7 @@ impl Pipeline {
|
|||
.line_width(1.0)
|
||||
.front_face(vk::FrontFace::COUNTER_CLOCKWISE)
|
||||
.cull_mode(vk::CullModeFlags::NONE)
|
||||
.polygon_mode(vk::PolygonMode::FILL);
|
||||
.polygon_mode(polygon_mode);
|
||||
let multisampler_info = vk::PipelineMultisampleStateCreateInfo::default()
|
||||
.rasterization_samples(vk::SampleCountFlags::TYPE_1);
|
||||
let colourblend_attachments = [vk::PipelineColorBlendAttachmentState::default()
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ use instance::{window::Resolution, Window};
|
|||
|
||||
use ash::vk;
|
||||
|
||||
pub const WINDOW_WIDTH: u32 = 1280;
|
||||
pub const WINDOW_HEIGHT: u32 = 720;
|
||||
pub const WINDOW_WIDTH: u32 = 800;
|
||||
pub const WINDOW_HEIGHT: u32 = 600;
|
||||
|
||||
pub const DEFAULT_RES: Resolution = Resolution {
|
||||
width: WINDOW_WIDTH,
|
||||
|
|
|
|||
Loading…
Reference in a new issue