Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
marc0246 committed Aug 20, 2022
1 parent c0f162a commit 30ec12b
Show file tree
Hide file tree
Showing 42 changed files with 529 additions and 153 deletions.
15 changes: 12 additions & 3 deletions examples/src/bin/basic-compute-shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@

use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer},
command_buffer::{AutoCommandBufferBuilder, CommandBufferUsage},
descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet},
command_buffer::{
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
},
descriptor_set::{
allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet,
},
device::{
physical::{PhysicalDevice, PhysicalDeviceType},
Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
Expand Down Expand Up @@ -134,6 +138,10 @@ fn main() {
.unwrap()
};

let mut descriptor_set_allocator = StandardDescriptorSetAllocator::new(device.clone());
let command_buffer_allocator =
StandardCommandBufferAllocator::new(device.clone(), queue.family()).unwrap();

// We start by creating the buffer that will store the data.
let data_buffer = {
// Iterator that produces the data.
Expand Down Expand Up @@ -161,14 +169,15 @@ fn main() {
// descriptor sets that each contain the buffer you want to run the shader on.
let layout = pipeline.layout().set_layouts().get(0).unwrap();
let set = PersistentDescriptorSet::new(
&mut descriptor_set_allocator,
layout.clone(),
[WriteDescriptorSet::buffer(0, data_buffer.clone())],
)
.unwrap();

// In order to execute our operation, we have to build a command buffer.
let mut builder = AutoCommandBufferBuilder::primary(
device.clone(),
&command_buffer_allocator,
queue.family(),
CommandBufferUsage::OneTimeSubmit,
)
Expand Down
8 changes: 6 additions & 2 deletions examples/src/bin/buffer-pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use std::{
use vulkano::{
buffer::CpuBufferPool,
command_buffer::{
AutoCommandBufferBuilder, CommandBufferUsage, RenderPassBeginInfo, SubpassContents,
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, CommandBufferUsage,
RenderPassBeginInfo, SubpassContents,
},
device::{
physical::{PhysicalDevice, PhysicalDeviceType},
Expand Down Expand Up @@ -224,6 +225,9 @@ fn main() {
let mut recreate_swapchain = false;
let mut previous_frame_end = Some(sync::now(device.clone()).boxed());

let command_buffer_allocator =
StandardCommandBufferAllocator::new(device.clone(), queue.family()).unwrap();

event_loop.run(move |event, _, control_flow| {
match event {
Event::WindowEvent {
Expand Down Expand Up @@ -315,7 +319,7 @@ fn main() {
// Allocate a new chunk from buffer_pool
let buffer = buffer_pool.chunk(data.to_vec()).unwrap();
let mut builder = AutoCommandBufferBuilder::primary(
device.clone(),
&command_buffer_allocator,
queue.family(),
CommandBufferUsage::OneTimeSubmit,
)
Expand Down
9 changes: 6 additions & 3 deletions examples/src/bin/clear_attachments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use std::sync::Arc;
use vulkano::{
command_buffer::{
AutoCommandBufferBuilder, ClearAttachment, ClearRect, CommandBufferUsage,
RenderPassBeginInfo, SubpassContents,
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder, ClearAttachment,
ClearRect, CommandBufferUsage, RenderPassBeginInfo, SubpassContents,
},
device::{
physical::{PhysicalDevice, PhysicalDeviceType},
Expand Down Expand Up @@ -138,6 +138,9 @@ fn main() {
)
.unwrap();

let command_buffer_allocator =
StandardCommandBufferAllocator::new(device.clone(), queue.family()).unwrap();

let mut width = swapchain.image_extent()[0];
let mut height = swapchain.image_extent()[1];
let mut framebuffers = window_size_dependent_setup(&images, render_pass.clone());
Expand Down Expand Up @@ -198,7 +201,7 @@ fn main() {
}

let mut builder = AutoCommandBufferBuilder::primary(
device.clone(),
&command_buffer_allocator,
queue.family(),
CommandBufferUsage::OneTimeSubmit,
)
Expand Down
7 changes: 6 additions & 1 deletion examples/src/bin/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use std::sync::Arc;
use vulkano::{
command_buffer::allocator::StandardCommandBufferAllocator,
device::{
physical::{PhysicalDevice, PhysicalDeviceType},
Device, DeviceCreateInfo, DeviceExtensions, QueueCreateInfo,
Expand Down Expand Up @@ -155,7 +156,7 @@ fn main() {
})
.expect("no device available");

let (_, mut queues) = Device::new(
let (device, mut queues) = Device::new(
physical_device,
DeviceCreateInfo {
enabled_extensions: device_extensions,
Expand All @@ -166,6 +167,9 @@ fn main() {
.expect("failed to create device");
let queue = queues.next().unwrap();

let command_buffer_allocator =
StandardCommandBufferAllocator::new(device.clone(), queue.family()).unwrap();

// Create an image in order to generate some additional logging:
let pixel_format = Format::R8G8B8A8_UINT;
let dimensions = ImageDimensions::Dim2d {
Expand All @@ -179,6 +183,7 @@ fn main() {
dimensions,
MipmapsCount::One,
pixel_format,
&command_buffer_allocator,
queue,
)
.unwrap();
Expand Down
26 changes: 20 additions & 6 deletions examples/src/bin/deferred/frame/ambient_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ use std::sync::Arc;
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
command_buffer::{
AutoCommandBufferBuilder, CommandBufferInheritanceInfo, CommandBufferUsage,
SecondaryAutoCommandBuffer,
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder,
CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer,
},
descriptor_set::{
allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet,
},
descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet},
device::Queue,
image::ImageViewAbstract,
impl_vertex,
Expand All @@ -37,11 +39,17 @@ pub struct AmbientLightingSystem {
vertex_buffer: Arc<CpuAccessibleBuffer<[Vertex]>>,
subpass: Subpass,
pipeline: Arc<GraphicsPipeline>,
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
descriptor_set_allocator: StandardDescriptorSetAllocator,
}

impl AmbientLightingSystem {
/// Initializes the ambient lighting system.
pub fn new(gfx_queue: Arc<Queue>, subpass: Subpass) -> AmbientLightingSystem {
pub fn new(
gfx_queue: Arc<Queue>,
subpass: Subpass,
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
) -> 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 = [
Expand Down Expand Up @@ -90,11 +98,16 @@ impl AmbientLightingSystem {
.unwrap()
};

let descriptor_set_allocator =
StandardDescriptorSetAllocator::new(gfx_queue.device().clone());

AmbientLightingSystem {
gfx_queue,
vertex_buffer,
subpass,
pipeline,
command_buffer_allocator,
descriptor_set_allocator,
}
}

Expand All @@ -111,7 +124,7 @@ impl AmbientLightingSystem {
/// - `ambient_color` is the color to apply.
///
pub fn draw(
&self,
&mut self,
viewport_dimensions: [u32; 2],
color_input: Arc<dyn ImageViewAbstract + 'static>,
ambient_color: [f32; 3],
Expand All @@ -122,6 +135,7 @@ impl AmbientLightingSystem {

let layout = self.pipeline.layout().set_layouts().get(0).unwrap();
let descriptor_set = PersistentDescriptorSet::new(
&mut self.descriptor_set_allocator,
layout.clone(),
[WriteDescriptorSet::image_view(0, color_input)],
)
Expand All @@ -134,7 +148,7 @@ impl AmbientLightingSystem {
};

let mut builder = AutoCommandBufferBuilder::secondary(
self.gfx_queue.device().clone(),
&self.command_buffer_allocator,
self.gfx_queue.family(),
CommandBufferUsage::MultipleSubmit,
CommandBufferInheritanceInfo {
Expand Down
26 changes: 20 additions & 6 deletions examples/src/bin/deferred/frame/directional_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ use std::sync::Arc;
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
command_buffer::{
AutoCommandBufferBuilder, CommandBufferInheritanceInfo, CommandBufferUsage,
SecondaryAutoCommandBuffer,
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder,
CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer,
},
descriptor_set::{
allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet,
},
descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet},
device::Queue,
image::ImageViewAbstract,
impl_vertex,
Expand All @@ -38,11 +40,17 @@ pub struct DirectionalLightingSystem {
vertex_buffer: Arc<CpuAccessibleBuffer<[Vertex]>>,
subpass: Subpass,
pipeline: Arc<GraphicsPipeline>,
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
descriptor_set_allocator: StandardDescriptorSetAllocator,
}

impl DirectionalLightingSystem {
/// Initializes the directional lighting system.
pub fn new(gfx_queue: Arc<Queue>, subpass: Subpass) -> DirectionalLightingSystem {
pub fn new(
gfx_queue: Arc<Queue>,
subpass: Subpass,
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
) -> DirectionalLightingSystem {
// TODO: vulkano doesn't allow us to draw without a vertex buffer, otherwise we could
// hard-code these values in the shader
let vertices = [
Expand Down Expand Up @@ -91,11 +99,16 @@ impl DirectionalLightingSystem {
.unwrap()
};

let descriptor_set_allocator =
StandardDescriptorSetAllocator::new(gfx_queue.device().clone());

DirectionalLightingSystem {
gfx_queue,
vertex_buffer,
subpass,
pipeline,
command_buffer_allocator,
descriptor_set_allocator,
}
}

Expand All @@ -119,7 +132,7 @@ impl DirectionalLightingSystem {
/// - `color` is the color to apply.
///
pub fn draw(
&self,
&mut self,
viewport_dimensions: [u32; 2],
color_input: Arc<dyn ImageViewAbstract + 'static>,
normals_input: Arc<dyn ImageViewAbstract + 'static>,
Expand All @@ -133,6 +146,7 @@ impl DirectionalLightingSystem {

let layout = self.pipeline.layout().set_layouts().get(0).unwrap();
let descriptor_set = PersistentDescriptorSet::new(
&mut self.descriptor_set_allocator,
layout.clone(),
[
WriteDescriptorSet::image_view(0, color_input),
Expand All @@ -148,7 +162,7 @@ impl DirectionalLightingSystem {
};

let mut builder = AutoCommandBufferBuilder::secondary(
self.gfx_queue.device().clone(),
&self.command_buffer_allocator,
self.gfx_queue.family(),
CommandBufferUsage::MultipleSubmit,
CommandBufferInheritanceInfo {
Expand Down
26 changes: 20 additions & 6 deletions examples/src/bin/deferred/frame/point_lighting_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ use std::sync::Arc;
use vulkano::{
buffer::{BufferUsage, CpuAccessibleBuffer, TypedBufferAccess},
command_buffer::{
AutoCommandBufferBuilder, CommandBufferInheritanceInfo, CommandBufferUsage,
SecondaryAutoCommandBuffer,
allocator::StandardCommandBufferAllocator, AutoCommandBufferBuilder,
CommandBufferInheritanceInfo, CommandBufferUsage, SecondaryAutoCommandBuffer,
},
descriptor_set::{
allocator::StandardDescriptorSetAllocator, PersistentDescriptorSet, WriteDescriptorSet,
},
descriptor_set::{PersistentDescriptorSet, WriteDescriptorSet},
device::Queue,
image::ImageViewAbstract,
impl_vertex,
Expand All @@ -37,11 +39,17 @@ pub struct PointLightingSystem {
vertex_buffer: Arc<CpuAccessibleBuffer<[Vertex]>>,
subpass: Subpass,
pipeline: Arc<GraphicsPipeline>,
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
descriptor_set_allocator: StandardDescriptorSetAllocator,
}

impl PointLightingSystem {
/// Initializes the point lighting system.
pub fn new(gfx_queue: Arc<Queue>, subpass: Subpass) -> PointLightingSystem {
pub fn new(
gfx_queue: Arc<Queue>,
subpass: Subpass,
command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
) -> PointLightingSystem {
// TODO: vulkano doesn't allow us to draw without a vertex buffer, otherwise we could
// hard-code these values in the shader
let vertices = [
Expand Down Expand Up @@ -90,11 +98,16 @@ impl PointLightingSystem {
.unwrap()
};

let descriptor_set_allocator =
StandardDescriptorSetAllocator::new(gfx_queue.device().clone());

PointLightingSystem {
gfx_queue,
vertex_buffer,
subpass,
pipeline,
command_buffer_allocator,
descriptor_set_allocator,
}
}

Expand Down Expand Up @@ -128,7 +141,7 @@ impl PointLightingSystem {
///
#[allow(clippy::too_many_arguments)]
pub fn draw(
&self,
&mut self,
viewport_dimensions: [u32; 2],
color_input: Arc<dyn ImageViewAbstract + 'static>,
normals_input: Arc<dyn ImageViewAbstract + 'static>,
Expand All @@ -145,6 +158,7 @@ impl PointLightingSystem {

let layout = self.pipeline.layout().set_layouts().get(0).unwrap();
let descriptor_set = PersistentDescriptorSet::new(
&mut self.descriptor_set_allocator,
layout.clone(),
[
WriteDescriptorSet::image_view(0, color_input),
Expand All @@ -161,7 +175,7 @@ impl PointLightingSystem {
};

let mut builder = AutoCommandBufferBuilder::secondary(
self.gfx_queue.device().clone(),
&self.command_buffer_allocator,
self.gfx_queue.family(),
CommandBufferUsage::MultipleSubmit,
CommandBufferInheritanceInfo {
Expand Down
Loading

0 comments on commit 30ec12b

Please sign in to comment.