Skip to content

Commit

Permalink
Rework mapped memory, add BufferContents trait with bytemuck (#1853)
Browse files Browse the repository at this point in the history
* Rework mapped memory, add `BufferContents` trait with bytemuck

* Platform fix
  • Loading branch information
Rua authored Mar 6, 2022
1 parent 6061fb7 commit c70fcc5
Show file tree
Hide file tree
Showing 84 changed files with 4,292 additions and 4,257 deletions.
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ winit = "0.26"
# and winit doesn't know about vulkano, so import a crate that will provide a link between the two.
vulkano-win = { path = "../vulkano-win" }

bytemuck = { version = "1.7", features = ["derive", "extern_crate_std", "min_const_generics"] }
cgmath = "0.18"
png = "0.17"
time = "0.3"
serde = { version = "1.0", features = ["derive"] }
ron = "0.7"
rand = "0.8.4"
Expand Down
21 changes: 12 additions & 9 deletions examples/src/bin/basic-compute-shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
// been more or more used for general-purpose operations as well. This is called "General-Purpose
// GPU", or *GPGPU*. This is what this example demonstrates.

use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer};
use vulkano::command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage};
use vulkano::descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet};
use vulkano::device::physical::{PhysicalDevice, PhysicalDeviceType};
use vulkano::device::{Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo};
use vulkano::instance::Instance;
use vulkano::pipeline::{ComputePipeline, Pipeline, PipelineBindPoint};
use vulkano::sync;
use vulkano::sync::GpuFuture;
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer},
command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage},
descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet},
device::{
physical::{PhysicalDevice, PhysicalDeviceType},
Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
},
instance::Instance,
pipeline::{ComputePipeline, Pipeline, PipelineBindPoint},
sync::{self, GpuFuture},
};

fn main() {
// As with other examples, the first step is to create an instance.
Expand Down
59 changes: 36 additions & 23 deletions examples/src/bin/buffer-pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,48 @@
// See: https://github.com/vulkano-rs/vulkano/issues/1221
// Finally, I have not profiled CpuBufferPool against CpuAccessibleBuffer

use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use vulkano::buffer::CpuBufferPool;
use vulkano::command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage, SubpassContents};
use vulkano::device::physical::{PhysicalDevice, PhysicalDeviceType};
use vulkano::device::{Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo};
use vulkano::image::view::ImageView;
use vulkano::image::{ImageAccess, ImageUsage, SwapchainImage};
use vulkano::instance::{Instance, InstanceCreateInfo};
use vulkano::pipeline::graphics::input_assembly::InputAssemblyState;
use vulkano::pipeline::graphics::vertex_input::BuffersDefinition;
use vulkano::pipeline::graphics::viewport::{Viewport, ViewportState};
use vulkano::pipeline::GraphicsPipeline;
use vulkano::render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass};
use vulkano::swapchain::{
self, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
use bytemuck::{Pod, Zeroable};
use std::{
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
};
use vulkano::{
buffer::CpuBufferPool,
command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage, SubpassContents},
device::{
physical::{PhysicalDevice, PhysicalDeviceType},
Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
},
image::{view::ImageView, ImageAccess, ImageUsage, SwapchainImage},
impl_vertex,
instance::{Instance, InstanceCreateInfo},
pipeline::{
graphics::{
input_assembly::InputAssemblyState,
vertex_input::BuffersDefinition,
viewport::{Viewport, ViewportState},
},
GraphicsPipeline,
},
render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass},
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
},
sync::{self, FlushError, GpuFuture},
};
use vulkano::sync::{self, FlushError, GpuFuture};
use vulkano_win::VkSurfaceBuild;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::{Window, WindowBuilder};
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
};

#[repr(C)]
#[derive(Default, Debug, Clone)]
#[derive(Clone, Copy, Debug, Default, Zeroable, Pod)]
struct Vertex {
position: [f32; 2],
}
vulkano::impl_vertex!(Vertex, position);
impl_vertex!(Vertex, position);

fn main() {
let required_extensions = vulkano_win::required_extensions();
Expand Down Expand Up @@ -242,7 +255,7 @@ fn main() {
}

let (image_num, suboptimal, acquire_future) =
match swapchain::acquire_next_image(swapchain.clone(), None) {
match acquire_next_image(swapchain.clone(), None) {
Ok(r) => r,
Err(AcquireError::OutOfDate) => {
recreate_swapchain = true;
Expand Down
42 changes: 25 additions & 17 deletions examples/src/bin/clear_attachments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,32 @@
// according to those terms.

use std::sync::Arc;
use vulkano::command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage, SubpassContents};
use vulkano::device::physical::{PhysicalDevice, PhysicalDeviceType};
use vulkano::device::{Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo};
use vulkano::format::ClearValue;
use vulkano::image::attachment::{ClearAttachment, ClearRect};
use vulkano::image::{view::ImageView, ImageUsage, SwapchainImage};
use vulkano::instance::{Instance, InstanceCreateInfo};
use vulkano::pipeline::graphics::viewport::ViewportState;
use vulkano::pipeline::GraphicsPipeline;
use vulkano::render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass};
use vulkano::swapchain::{
self, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
use vulkano::{
command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage, SubpassContents},
device::{
physical::{PhysicalDevice, PhysicalDeviceType},
Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
},
format::ClearValue,
image::{
attachment::{ClearAttachment, ClearRect},
view::ImageView,
ImageUsage, SwapchainImage,
},
instance::{Instance, InstanceCreateInfo},
pipeline::{graphics::viewport::ViewportState, GraphicsPipeline},
render_pass::{Framebuffer, FramebufferCreateInfo, RenderPass, Subpass},
swapchain::{
acquire_next_image, AcquireError, Swapchain, SwapchainCreateInfo, SwapchainCreationError,
},
sync::{self, FlushError, GpuFuture},
};
use vulkano::sync::{self, FlushError, GpuFuture};
use vulkano_win::VkSurfaceBuild;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::{Window, WindowBuilder};
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
};

fn main() {
// The start of this example is exactly the same as `triangle`. You should read the
Expand Down Expand Up @@ -209,7 +217,7 @@ fn main() {
}

let (image_num, suboptimal, acquire_future) =
match swapchain::acquire_next_image(swapchain.clone(), None) {
match acquire_next_image(swapchain.clone(), None) {
Ok(r) => r,
Err(AcquireError::OutOfDate) => {
recreate_swapchain = true;
Expand Down
27 changes: 15 additions & 12 deletions examples/src/bin/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
// notice may not be copied, modified, or distributed except
// according to those terms.

use vulkano::device::physical::{PhysicalDevice, PhysicalDeviceType};
use vulkano::device::{Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo};
use vulkano::format::Format;
use vulkano::image::ImageDimensions;
use vulkano::image::ImmutableImage;
use vulkano::image::MipmapsCount;
use vulkano::instance::debug::{DebugCallback, MessageSeverity, MessageType};
use vulkano::instance::{self, InstanceCreateInfo};
use vulkano::instance::{Instance, InstanceExtensions};
use vulkano::{
device::{
physical::{PhysicalDevice, PhysicalDeviceType},
Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
},
format::Format,
image::{ImageDimensions, ImmutableImage, MipmapsCount},
instance::{
debug::{DebugCallback, MessageSeverity, MessageType},
layers_list, Instance, InstanceCreateInfo, InstanceExtensions,
},
};

fn main() {
// Vulkano Debugging Example Code
Expand Down Expand Up @@ -43,7 +46,7 @@ fn main() {
// and you should verify that list for safety - Vulkano will return an error if you specify
// any layers that are not installed on this system. That code to do could look like this:
println!("List of Vulkan debugging layers available to use:");
let mut layers = instance::layers_list().unwrap();
let mut layers = layers_list().unwrap();
while let Some(l) = layers.next() {
println!("\t{}", l.name());
}
Expand Down Expand Up @@ -158,9 +161,9 @@ fn main() {
height: 4096,
array_layers: 1,
};
const DATA: [[u8; 4]; 4096 * 4096] = [[0; 4]; 4096 * 4096];
static DATA: [[u8; 4]; 4096 * 4096] = [[0; 4]; 4096 * 4096];
let _ = ImmutableImage::from_iter(
DATA.iter().cloned(),
DATA.iter().copied(),
dimensions,
MipmapsCount::One,
pixel_format,
Expand Down
67 changes: 35 additions & 32 deletions examples/src/bin/deferred/frame/ambient_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@
// notice may not be copied, modified, or distributed except
// according to those terms.

use bytemuck::{Pod, Zeroable};
use std::sync::Arc;
use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess};
use vulkano::command_buffer::{
AutoCommandBufferBuilder, CommandBufferUsage, SecondaryAutoCommandBuffer,
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage, SecondaryAutoCommandBuffer},
descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet},
device::Queue,
image::ImageViewAbstract,
impl_vertex,
pipeline::{
graphics::{
color_blend::{AttachmentBlend, BlendFactor, BlendOp, ColorBlendState},
input_assembly::InputAssemblyState,
vertex_input::BuffersDefinition,
viewport::{Viewport, ViewportState},
},
GraphicsPipeline, Pipeline, PipelineBindPoint,
},
render_pass::Subpass,
};
use vulkano::descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet};
use vulkano::device::Queue;
use vulkano::image::ImageViewAbstract;
use vulkano::pipeline::graphics::color_blend::{
AttachmentBlend, BlendFactor, BlendOp, ColorBlendState,
};
use vulkano::pipeline::graphics::input_assembly::InputAssemblyState;
use vulkano::pipeline::graphics::vertex_input::BuffersDefinition;
use vulkano::pipeline::graphics::viewport::{Viewport, ViewportState};
use vulkano::pipeline::{GraphicsPipeline, Pipeline, PipelineBindPoint};
use vulkano::render_pass::Subpass;

/// Allows applying an ambient lighting to a scene.
pub struct AmbientLightingSystem {
Expand All @@ -36,24 +40,23 @@ impl AmbientLightingSystem {
pub fn new(gfx_queue: Arc<Queue>, subpass: Subpass) -> AmbientLightingSystem {
// TODO: vulkano doesn't allow us to draw without a vertex buffer, otherwise we could
// hard-code these values in the shader
let vertices = [
Vertex {
position: [-1.0, -1.0],
},
Vertex {
position: [-1.0, 3.0],
},
Vertex {
position: [3.0, -1.0],
},
];
let vertex_buffer = {
CpuAccessibleBuffer::from_iter(
gfx_queue.device().clone(),
BufferUsage::all(),
false,
[
Vertex {
position: [-1.0, -1.0],
},
Vertex {
position: [-1.0, 3.0],
},
Vertex {
position: [3.0, -1.0],
},
]
.iter()
.cloned(),
vertices,
)
.expect("failed to create buffer")
};
Expand Down Expand Up @@ -84,9 +87,9 @@ impl AmbientLightingSystem {
};

AmbientLightingSystem {
gfx_queue: gfx_queue,
vertex_buffer: vertex_buffer,
pipeline: pipeline,
gfx_queue,
vertex_buffer,
pipeline,
}
}

Expand Down Expand Up @@ -150,11 +153,11 @@ impl AmbientLightingSystem {
}

#[repr(C)]
#[derive(Default, Debug, Clone)]
#[derive(Clone, Copy, Debug, Default, Zeroable, Pod)]
struct Vertex {
position: [f32; 2],
}
vulkano::impl_vertex!(Vertex, position);
impl_vertex!(Vertex, position);

mod vs {
vulkano_shaders::shader! {
Expand Down
Loading

0 comments on commit c70fcc5

Please # to comment.