Skip to main content

Buffer

Struct Buffer 

Source
pub struct Buffer { /* private fields */ }

Implementations§

Source§

impl Buffer

Source

pub fn new( device: &Device, allocator: &mut Allocator, location: MemoryLocation, size: DeviceSize, buffer_usage_flags: BufferUsageFlags, ) -> Buffer

Examples found in repository?
examples/buffer/main.rs (lines 19-25)
17    fn new(ctx: &mut InitContext) -> Self {
18        // Image
19        let buffer = Buffer::new(
20            &ctx.device,
21            &mut ctx.allocator,
22            MemoryLocation::CpuToGpu,
23            2000 * 2000 * 4,
24            BufferUsageFlags::TRANSFER_SRC
25        );
26
27        {
28            let mut mem = buffer.mapped().unwrap();
29            for i in mem.as_mut_slice() {
30                *i = 255u8;
31            }
32        }
33
34        Self {
35            buffer
36        }
37    }
38}
39
40impl RenderComponent for ComputeRender {
41
42    fn render(&mut self, ctx: &mut RenderContext) {
43
44        if self.buffer.size() != (ctx.swapchain_image.width() * ctx.swapchain_image.height() * 4) as u64 {
45            // Recreate image
46            self.buffer = Buffer::new(
47                &ctx.device,
48                &mut ctx.allocator,
49                MemoryLocation::CpuToGpu,
50                (ctx.swapchain_image.width() * ctx.swapchain_image.height() * 4) as DeviceSize,
51                BufferUsageFlags::TRANSFER_SRC
52            );
53
54            {
55                let mut mem = self.buffer.mapped().unwrap();
56                for i in mem.as_mut_slice() {
57                    *i = 255u8;
58                }
59            }
60        }
61
62        // Transition the swapchain image
63        ctx.command_buffer.image_barrier(
64            ctx.swapchain_image,
65            vk::ImageLayout::PRESENT_SRC_KHR,
66            vk::ImageLayout::TRANSFER_DST_OPTIMAL,
67            vk::PipelineStageFlags::TOP_OF_PIPE,
68            vk::PipelineStageFlags::TRANSFER,
69            vk::AccessFlags::NONE,
70            vk::AccessFlags::TRANSFER_WRITE
71        );
72
73        // Copy to the swapchain
74        ctx.command_buffer.clear_color_image(
75            ctx.swapchain_image,
76            ImageLayout::TRANSFER_DST_OPTIMAL,
77            [1.0, 0.0, 0.0, 1.0]
78        );
79
80        ctx.command_buffer.copy_buffer_to_image(
81            &self.buffer,
82            ctx.swapchain_image,
83            ImageLayout::TRANSFER_DST_OPTIMAL,
84            &[
85                BufferImageCopy::default()
86                    .buffer_image_height(0)
87                    .buffer_row_length(0)
88                    .buffer_offset(0)
89                    .image_extent(
90                        Extent3D::default()
91                            .width(ctx.swapchain_image.width())
92                            .height(ctx.swapchain_image.height())
93                            .depth(1)
94                    )
95                    .image_subresource(
96                        ImageSubresourceLayers::default()
97                            .aspect_mask(vk::ImageAspectFlags::COLOR)
98                            .base_array_layer(0)
99                            .layer_count(1)
100                            .mip_level(0)
101                    )
102            ]
103        );
104
105        // Transfer back to default states
106        ctx.command_buffer.image_barrier(
107            ctx.swapchain_image,
108            vk::ImageLayout::TRANSFER_DST_OPTIMAL,
109            vk::ImageLayout::PRESENT_SRC_KHR,
110            vk::PipelineStageFlags::TRANSFER,
111            vk::PipelineStageFlags::BOTTOM_OF_PIPE,
112            vk::AccessFlags::TRANSFER_WRITE,
113            vk::AccessFlags::NONE
114        );
115
116    }
Source

pub fn mapped(&self) -> Result<MappedBufferGuard<'_>, BufferError>

Examples found in repository?
examples/buffer/main.rs (line 28)
17    fn new(ctx: &mut InitContext) -> Self {
18        // Image
19        let buffer = Buffer::new(
20            &ctx.device,
21            &mut ctx.allocator,
22            MemoryLocation::CpuToGpu,
23            2000 * 2000 * 4,
24            BufferUsageFlags::TRANSFER_SRC
25        );
26
27        {
28            let mut mem = buffer.mapped().unwrap();
29            for i in mem.as_mut_slice() {
30                *i = 255u8;
31            }
32        }
33
34        Self {
35            buffer
36        }
37    }
38}
39
40impl RenderComponent for ComputeRender {
41
42    fn render(&mut self, ctx: &mut RenderContext) {
43
44        if self.buffer.size() != (ctx.swapchain_image.width() * ctx.swapchain_image.height() * 4) as u64 {
45            // Recreate image
46            self.buffer = Buffer::new(
47                &ctx.device,
48                &mut ctx.allocator,
49                MemoryLocation::CpuToGpu,
50                (ctx.swapchain_image.width() * ctx.swapchain_image.height() * 4) as DeviceSize,
51                BufferUsageFlags::TRANSFER_SRC
52            );
53
54            {
55                let mut mem = self.buffer.mapped().unwrap();
56                for i in mem.as_mut_slice() {
57                    *i = 255u8;
58                }
59            }
60        }
61
62        // Transition the swapchain image
63        ctx.command_buffer.image_barrier(
64            ctx.swapchain_image,
65            vk::ImageLayout::PRESENT_SRC_KHR,
66            vk::ImageLayout::TRANSFER_DST_OPTIMAL,
67            vk::PipelineStageFlags::TOP_OF_PIPE,
68            vk::PipelineStageFlags::TRANSFER,
69            vk::AccessFlags::NONE,
70            vk::AccessFlags::TRANSFER_WRITE
71        );
72
73        // Copy to the swapchain
74        ctx.command_buffer.clear_color_image(
75            ctx.swapchain_image,
76            ImageLayout::TRANSFER_DST_OPTIMAL,
77            [1.0, 0.0, 0.0, 1.0]
78        );
79
80        ctx.command_buffer.copy_buffer_to_image(
81            &self.buffer,
82            ctx.swapchain_image,
83            ImageLayout::TRANSFER_DST_OPTIMAL,
84            &[
85                BufferImageCopy::default()
86                    .buffer_image_height(0)
87                    .buffer_row_length(0)
88                    .buffer_offset(0)
89                    .image_extent(
90                        Extent3D::default()
91                            .width(ctx.swapchain_image.width())
92                            .height(ctx.swapchain_image.height())
93                            .depth(1)
94                    )
95                    .image_subresource(
96                        ImageSubresourceLayers::default()
97                            .aspect_mask(vk::ImageAspectFlags::COLOR)
98                            .base_array_layer(0)
99                            .layer_count(1)
100                            .mip_level(0)
101                    )
102            ]
103        );
104
105        // Transfer back to default states
106        ctx.command_buffer.image_barrier(
107            ctx.swapchain_image,
108            vk::ImageLayout::TRANSFER_DST_OPTIMAL,
109            vk::ImageLayout::PRESENT_SRC_KHR,
110            vk::PipelineStageFlags::TRANSFER,
111            vk::PipelineStageFlags::BOTTOM_OF_PIPE,
112            vk::AccessFlags::TRANSFER_WRITE,
113            vk::AccessFlags::NONE
114        );
115
116    }
Source

pub fn binding(&self) -> DescriptorBufferInfo

Source

pub fn handle(&self) -> &Buffer

Source

pub fn size(&self) -> DeviceSize

Examples found in repository?
examples/buffer/main.rs (line 44)
42    fn render(&mut self, ctx: &mut RenderContext) {
43
44        if self.buffer.size() != (ctx.swapchain_image.width() * ctx.swapchain_image.height() * 4) as u64 {
45            // Recreate image
46            self.buffer = Buffer::new(
47                &ctx.device,
48                &mut ctx.allocator,
49                MemoryLocation::CpuToGpu,
50                (ctx.swapchain_image.width() * ctx.swapchain_image.height() * 4) as DeviceSize,
51                BufferUsageFlags::TRANSFER_SRC
52            );
53
54            {
55                let mut mem = self.buffer.mapped().unwrap();
56                for i in mem.as_mut_slice() {
57                    *i = 255u8;
58                }
59            }
60        }
61
62        // Transition the swapchain image
63        ctx.command_buffer.image_barrier(
64            ctx.swapchain_image,
65            vk::ImageLayout::PRESENT_SRC_KHR,
66            vk::ImageLayout::TRANSFER_DST_OPTIMAL,
67            vk::PipelineStageFlags::TOP_OF_PIPE,
68            vk::PipelineStageFlags::TRANSFER,
69            vk::AccessFlags::NONE,
70            vk::AccessFlags::TRANSFER_WRITE
71        );
72
73        // Copy to the swapchain
74        ctx.command_buffer.clear_color_image(
75            ctx.swapchain_image,
76            ImageLayout::TRANSFER_DST_OPTIMAL,
77            [1.0, 0.0, 0.0, 1.0]
78        );
79
80        ctx.command_buffer.copy_buffer_to_image(
81            &self.buffer,
82            ctx.swapchain_image,
83            ImageLayout::TRANSFER_DST_OPTIMAL,
84            &[
85                BufferImageCopy::default()
86                    .buffer_image_height(0)
87                    .buffer_row_length(0)
88                    .buffer_offset(0)
89                    .image_extent(
90                        Extent3D::default()
91                            .width(ctx.swapchain_image.width())
92                            .height(ctx.swapchain_image.height())
93                            .depth(1)
94                    )
95                    .image_subresource(
96                        ImageSubresourceLayers::default()
97                            .aspect_mask(vk::ImageAspectFlags::COLOR)
98                            .base_array_layer(0)
99                            .layer_count(1)
100                            .mip_level(0)
101                    )
102            ]
103        );
104
105        // Transfer back to default states
106        ctx.command_buffer.image_barrier(
107            ctx.swapchain_image,
108            vk::ImageLayout::TRANSFER_DST_OPTIMAL,
109            vk::ImageLayout::PRESENT_SRC_KHR,
110            vk::PipelineStageFlags::TRANSFER,
111            vk::PipelineStageFlags::BOTTOM_OF_PIPE,
112            vk::AccessFlags::TRANSFER_WRITE,
113            vk::AccessFlags::NONE
114        );
115
116    }

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more