Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use only the selected range of buffer and image views in synchronization #1880

Merged
merged 1 commit into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions vulkano/src/buffer/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,21 @@
//! ).unwrap();
//! ```

use crate::buffer::{BufferAccess, BufferAccessObject, BufferInner};
use crate::device::Device;
use crate::device::DeviceOwned;
use crate::format::Format;
use crate::format::FormatFeatures;
use crate::DeviceSize;
use crate::Error;
use crate::OomError;
use crate::VulkanObject;
use crate::{check_errors, Version};
use std::error;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::mem::MaybeUninit;
use std::ptr;
use std::sync::Arc;
use super::{BufferAccess, BufferAccessObject, BufferInner};
use crate::{
check_errors,
device::{Device, DeviceOwned},
format::{Format, FormatFeatures},
DeviceSize, Error, OomError, Version, VulkanObject,
};
use std::{
error, fmt,
hash::{Hash, Hasher},
mem::MaybeUninit,
ops::Range,
ptr,
sync::Arc,
};

/// Represents a way for the GPU to interpret buffer data. See the documentation of the
/// `view` module.
Expand All @@ -71,6 +70,7 @@ where

format: Option<Format>,
format_features: FormatFeatures,
range: Range<DeviceSize>,
}

impl<B> BufferView<B>
Expand All @@ -86,7 +86,7 @@ where

let device = buffer.device();
let properties = device.physical_device().properties();
let range = buffer.size();
let size = buffer.size();
let BufferInner {
buffer: inner_buffer,
offset,
Expand Down Expand Up @@ -120,15 +120,15 @@ where
let texels_per_block = format.texels_per_block();

// VUID-VkBufferViewCreateInfo-range-00929
if range % block_size != 0 {
if size % block_size != 0 {
return Err(BufferViewCreationError::RangeNotAligned {
range,
range: size,
required_alignment: block_size,
});
}

// VUID-VkBufferViewCreateInfo-range-00930
if ((range / block_size) * texels_per_block as DeviceSize) as u32
if ((size / block_size) * texels_per_block as DeviceSize) as u32
> properties.max_texel_buffer_elements
{
return Err(BufferViewCreationError::MaxTexelBufferElementsExceeded);
Expand Down Expand Up @@ -200,7 +200,7 @@ where
buffer: inner_buffer.internal_object(),
format: format.into(),
offset,
range,
range: size,
..Default::default()
};

Expand All @@ -222,6 +222,7 @@ where

format: Some(format),
format_features,
range: 0..size,
}))
}

Expand Down Expand Up @@ -411,6 +412,9 @@ pub unsafe trait BufferViewAbstract:

/// Returns the features supported by the buffer view's format.
fn format_features(&self) -> &FormatFeatures;

/// Returns the byte range of the wrapped buffer that this view exposes.
fn range(&self) -> Range<DeviceSize>;
}

unsafe impl<B> BufferViewAbstract for BufferView<B>
Expand All @@ -432,6 +436,11 @@ where
fn format_features(&self) -> &FormatFeatures {
&self.format_features
}

#[inline]
fn range(&self) -> Range<DeviceSize> {
self.range.clone()
}
}

impl PartialEq for dyn BufferViewAbstract {
Expand Down
12 changes: 10 additions & 2 deletions vulkano/src/command_buffer/auto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use super::{
use crate::{
buffer::{sys::UnsafeBuffer, BufferAccess},
device::{physical::QueueFamily, Device, DeviceOwned, Queue},
image::{sys::UnsafeImage, ImageAccess, ImageLayout},
image::{sys::UnsafeImage, ImageAccess, ImageLayout, ImageSubresourceRange},
pipeline::GraphicsPipeline,
query::{QueryControlFlags, QueryPipelineStatisticFlags, QueryType},
render_pass::Subpass,
Expand Down Expand Up @@ -684,7 +684,14 @@ where
}

#[inline]
fn buffer(&self, index: usize) -> Option<(&Arc<dyn BufferAccess>, PipelineMemoryAccess)> {
fn buffer(
&self,
index: usize,
) -> Option<(
&Arc<dyn BufferAccess>,
Range<DeviceSize>,
PipelineMemoryAccess,
)> {
self.inner.buffer(index)
}

Expand All @@ -699,6 +706,7 @@ where
index: usize,
) -> Option<(
&Arc<dyn ImageAccess>,
&ImageSubresourceRange,
PipelineMemoryAccess,
ImageLayout,
ImageLayout,
Expand Down
107 changes: 60 additions & 47 deletions vulkano/src/command_buffer/commands/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::{
sync::{AccessFlags, PipelineMemoryAccess, PipelineStages},
DeviceSize, VulkanObject,
};
use std::{borrow::Cow, error, fmt, mem::size_of, sync::Arc};
use std::{borrow::Cow, error, fmt, mem::size_of, ops::Range, sync::Arc};

/// # Commands to execute a bound pipeline.
///
Expand Down Expand Up @@ -482,7 +482,7 @@ fn check_descriptor_sets_validity<'a, P: Pipeline>(
// - If the signedness of any read or sample operation does not match the signedness of
// the image’s format.
if let Some(scalar_type) = reqs.image_scalar_type {
let aspects = image_view.aspects();
let aspects = image_view.subresource_range().aspects;
let view_scalar_type = ShaderScalarType::from(
if aspects.color || aspects.plane0 || aspects.plane1 || aspects.plane2 {
image_view.format().unwrap().type_color().unwrap()
Expand Down Expand Up @@ -2025,41 +2025,40 @@ impl SyncCommandBufferBuilder {
access
});

let buffer_resource =
move |(memory, buffer): (PipelineMemoryAccess, Arc<dyn BufferAccess>)| {
let range = 0..buffer.size(); // TODO:
(
format!("Buffer bound to set {} descriptor {}", set, binding).into(),
Resource::Buffer {
buffer,
range,
memory,
},
)
};
let image_resource =
move |(memory, image): (PipelineMemoryAccess, Arc<dyn ImageAccess>)| {
let subresource_range = ImageSubresourceRange {
// TODO:
aspects: image.format().aspects(),
mip_levels: image.current_mip_levels_access(),
array_layers: image.current_array_layers_access(),
};
let layout = image
.descriptor_layouts()
.expect("descriptor_layouts must return Some when used in an image view")
.layout_for(descriptor_type);
(
format!("Image bound to set {} descriptor {}", set, binding).into(),
Resource::Image {
image,
subresource_range,
memory,
start_layout: layout,
end_layout: layout,
},
)
};
let buffer_resource = move |(buffer, range, memory): (
Arc<dyn BufferAccess>,
Range<DeviceSize>,
PipelineMemoryAccess,
)| {
(
format!("Buffer bound to set {} descriptor {}", set, binding).into(),
Resource::Buffer {
buffer,
range,
memory,
},
)
};
let image_resource = move |(image, subresource_range, memory): (
Arc<dyn ImageAccess>,
ImageSubresourceRange,
PipelineMemoryAccess,
)| {
let layout = image
.descriptor_layouts()
.expect("descriptor_layouts must return Some when used in an image view")
.layout_for(descriptor_type);
(
format!("Image bound to set {} descriptor {}", set, binding).into(),
Resource::Image {
image,
subresource_range,
memory,
start_layout: layout,
end_layout: layout,
},
)
};

match state.descriptor_sets[&set]
.resources()
Expand All @@ -2072,7 +2071,13 @@ impl SyncCommandBufferBuilder {
access
.zip(elements)
.filter_map(|(access, element)| {
element.as_ref().map(|buffer| (access, buffer.clone()))
element.as_ref().map(|buffer| {
(
buffer.clone(),
0..buffer.size(), // TODO:
access,
)
})
})
.map(buffer_resource),
);
Expand All @@ -2082,9 +2087,9 @@ impl SyncCommandBufferBuilder {
access
.zip(elements)
.filter_map(|(access, element)| {
element
.as_ref()
.map(|buffer_view| (access, buffer_view.buffer()))
element.as_ref().map(|buffer_view| {
(buffer_view.buffer(), buffer_view.range(), access)
})
})
.map(buffer_resource),
);
Expand All @@ -2094,9 +2099,13 @@ impl SyncCommandBufferBuilder {
access
.zip(elements)
.filter_map(|(access, element)| {
element
.as_ref()
.map(|image_view| (access, image_view.image()))
element.as_ref().map(|image_view| {
(
image_view.image(),
image_view.subresource_range().clone(),
access,
)
})
})
.map(image_resource),
);
Expand All @@ -2106,9 +2115,13 @@ impl SyncCommandBufferBuilder {
access
.zip(elements)
.filter_map(|(access, element)| {
element
.as_ref()
.map(|(image_view, _)| (access, image_view.image()))
element.as_ref().map(|(image_view, _)| {
(
image_view.image(),
image_view.subresource_range().clone(),
access,
)
})
})
.map(image_resource),
);
Expand Down
14 changes: 4 additions & 10 deletions vulkano/src/command_buffer/commands/render_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
format::{ClearValue, NumericType},
image::{
attachment::{ClearAttachment, ClearRect},
ImageAspects, ImageSubresourceRange,
ImageAspects,
},
pipeline::GraphicsPipeline,
render_pass::{Framebuffer, LoadOp},
Expand Down Expand Up @@ -402,19 +402,13 @@ impl SyncCommandBufferBuilder {
.iter()
.enumerate()
.map(|(num, desc)| {
let image = render_pass_begin_info.framebuffer.attachments()[num].image();
let subresource_range = ImageSubresourceRange {
// TODO:
aspects: image.format().aspects(),
mip_levels: image.current_mip_levels_access(),
array_layers: image.current_array_layers_access(),
};
let image_view = &render_pass_begin_info.framebuffer.attachments()[num];

(
format!("attachment {}", num).into(),
Resource::Image {
image,
subresource_range,
image: image_view.image(),
subresource_range: image_view.subresource_range().clone(),
memory: PipelineMemoryAccess {
stages: PipelineStages {
all_commands: true,
Expand Down
15 changes: 5 additions & 10 deletions vulkano/src/command_buffer/commands/secondary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::{
CommandBufferInheritanceRenderPassInfo, CommandBufferUsage, ExecuteCommandsError,
PrimaryAutoCommandBuffer, SecondaryCommandBuffer, SubpassContents,
},
image::ImageSubresourceRange,
query::QueryType,
SafeDeref, VulkanObject,
};
Expand Down Expand Up @@ -248,28 +247,24 @@ impl<'a> SyncCommandBufferBuilderExecuteCommands<'a> {
let mut resources = Vec::new();
for (cbuf_num, cbuf) in self.inner.iter().enumerate() {
for buf_num in 0..cbuf.num_buffers() {
let (buffer, memory) = cbuf.buffer(buf_num).unwrap();
let (buffer, range, memory) = cbuf.buffer(buf_num).unwrap();
resources.push((
format!("Buffer bound to secondary command buffer {}", cbuf_num).into(),
Resource::Buffer {
buffer: buffer.clone(),
range: 0..buffer.size(), // TODO:
range,
memory,
},
));
}
for img_num in 0..cbuf.num_images() {
let (image, memory, start_layout, end_layout) = cbuf.image(img_num).unwrap();
let (image, subresource_range, memory, start_layout, end_layout) =
cbuf.image(img_num).unwrap();
resources.push((
format!("Image bound to secondary command buffer {}", cbuf_num).into(),
Resource::Image {
image: image.clone(),
subresource_range: ImageSubresourceRange {
// TODO:
aspects: image.format().aspects(),
mip_levels: image.current_mip_levels_access(),
array_layers: image.current_array_layers_access(),
},
subresource_range: subresource_range.clone(),
memory,
start_layout,
end_layout,
Expand Down
Loading