Use separate samplers and sampled images in the examples (#2354)

* Use separate samplers and sampled images in the examples

* Update examples/src/bin/push-descriptors/main.rs

Co-authored-by: marc0246 <40955683+marc0246@users.noreply.github.com>

---------

Co-authored-by: marc0246 <40955683+marc0246@users.noreply.github.com>
This commit is contained in:
Rua 2023-10-08 14:10:19 +02:00 committed by GitHub
parent 518369e7f1
commit 444c88dfca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 196 additions and 155 deletions

View File

@ -399,10 +399,11 @@ fn main() {
layout(location = 0) in vec2 tex_coords; layout(location = 0) in vec2 tex_coords;
layout(location = 0) out vec4 f_color; layout(location = 0) out vec4 f_color;
layout(set = 1, binding = 0) uniform sampler2D tex; layout(set = 1, binding = 0) uniform sampler s;
layout(set = 1, binding = 1) uniform texture2D tex;
void main() { void main() {
f_color = texture(tex, tex_coords); f_color = texture(sampler2D(tex, s), tex_coords);
} }
", ",
} }
@ -505,11 +506,10 @@ fn main() {
PersistentDescriptorSet::new( PersistentDescriptorSet::new(
&descriptor_set_allocator, &descriptor_set_allocator,
pipeline.layout().set_layouts()[1].clone(), pipeline.layout().set_layouts()[1].clone(),
[WriteDescriptorSet::image_view_sampler( [
0, WriteDescriptorSet::sampler(0, sampler.clone()),
ImageView::new_default(texture).unwrap(), WriteDescriptorSet::image_view(1, ImageView::new_default(texture).unwrap()),
sampler.clone(), ],
)],
[], [],
) )
.unwrap() .unwrap()

View File

@ -270,9 +270,10 @@ mod linux {
let set = PersistentDescriptorSet::new( let set = PersistentDescriptorSet::new(
&descriptor_set_allocator, &descriptor_set_allocator,
layout.clone(), layout.clone(),
[WriteDescriptorSet::image_view_sampler( [
0, image_view, sampler, WriteDescriptorSet::sampler(0, sampler),
)], WriteDescriptorSet::image_view(1, image_view),
],
[], [],
) )
.unwrap(); .unwrap();
@ -798,9 +799,12 @@ mod linux {
#version 450 #version 450
layout(location = 0) in vec2 tex_coords; layout(location = 0) in vec2 tex_coords;
layout(location = 0) out vec4 f_color; layout(location = 0) out vec4 f_color;
layout(set = 0, binding = 0) uniform sampler2D tex;
layout(set = 0, binding = 0) uniform sampler s;
layout(set = 0, binding = 1) uniform texture2D tex;
void main() { void main() {
f_color = texture(tex, tex_coords); f_color = texture(sampler2D(tex, s), tex_coords);
} }
", ",
} }

View File

@ -389,7 +389,10 @@ fn main() {
let set = PersistentDescriptorSet::new( let set = PersistentDescriptorSet::new(
&descriptor_set_allocator, &descriptor_set_allocator,
layout.clone(), layout.clone(),
[WriteDescriptorSet::image_view_sampler(0, texture, sampler)], [
WriteDescriptorSet::sampler(0, sampler),
WriteDescriptorSet::image_view(1, texture),
],
[], [],
) )
.unwrap(); .unwrap();
@ -578,10 +581,11 @@ mod fs {
layout(location = 0) in vec2 tex_coords; layout(location = 0) in vec2 tex_coords;
layout(location = 0) out vec4 f_color; layout(location = 0) out vec4 f_color;
layout(set = 0, binding = 0) uniform sampler2D tex; layout(set = 0, binding = 0) uniform sampler s;
layout(set = 0, binding = 1) uniform texture2D tex;
void main() { void main() {
f_color = texture(tex, tex_coords); f_color = texture(sampler2D(tex, s), tex_coords);
} }
", ",
} }

View File

@ -337,7 +337,10 @@ fn main() {
let set = PersistentDescriptorSet::new( let set = PersistentDescriptorSet::new(
&descriptor_set_allocator, &descriptor_set_allocator,
layout.clone(), layout.clone(),
[WriteDescriptorSet::image_view_sampler(0, texture, sampler)], [
WriteDescriptorSet::sampler(0, sampler),
WriteDescriptorSet::image_view(1, texture),
],
[], [],
) )
.unwrap(); .unwrap();
@ -526,10 +529,11 @@ mod fs {
layout(location = 0) in vec2 tex_coords; layout(location = 0) in vec2 tex_coords;
layout(location = 0) out vec4 f_color; layout(location = 0) out vec4 f_color;
layout(set = 0, binding = 0) uniform sampler2D tex; layout(set = 0, binding = 0) uniform sampler s;
layout(set = 0, binding = 1) uniform texture2D tex;
void main() { void main() {
f_color = texture(tex, tex_coords); f_color = texture(sampler2D(tex, s), tex_coords);
} }
", ",
} }

View File

@ -358,7 +358,7 @@ fn main() {
let set = PersistentDescriptorSet::new( let set = PersistentDescriptorSet::new(
&descriptor_set_allocator, &descriptor_set_allocator,
layout.clone(), layout.clone(),
[WriteDescriptorSet::image_view(0, texture)], [WriteDescriptorSet::image_view(1, texture)],
[], [],
) )
.unwrap(); .unwrap();
@ -547,10 +547,11 @@ mod fs {
layout(location = 0) in vec2 tex_coords; layout(location = 0) in vec2 tex_coords;
layout(location = 0) out vec4 f_color; layout(location = 0) out vec4 f_color;
layout(set = 0, binding = 0) uniform sampler2D tex; layout(set = 0, binding = 0) uniform sampler s;
layout(set = 0, binding = 1) uniform texture2D tex;
void main() { void main() {
f_color = texture(tex, tex_coords); f_color = texture(sampler2D(tex, s), tex_coords);
} }
", ",
} }

View File

@ -198,7 +198,10 @@ impl PixelsDrawPipeline {
PersistentDescriptorSet::new( PersistentDescriptorSet::new(
&self.descriptor_set_allocator, &self.descriptor_set_allocator,
layout.clone(), layout.clone(),
[WriteDescriptorSet::image_view_sampler(0, image, sampler)], [
WriteDescriptorSet::sampler(0, sampler),
WriteDescriptorSet::image_view(1, image),
],
[], [],
) )
.unwrap() .unwrap()
@ -279,10 +282,11 @@ mod fs {
layout(location = 0) out vec4 f_color; layout(location = 0) out vec4 f_color;
layout(set = 0, binding = 0) uniform sampler2D tex; layout(set = 0, binding = 0) uniform sampler s;
layout(set = 0, binding = 1) uniform texture2D tex;
void main() { void main() {
f_color = texture(tex, v_tex_coords); f_color = texture(sampler2D(tex, s), v_tex_coords);
} }
", ",
} }

View File

@ -194,7 +194,10 @@ impl PixelsDrawPipeline {
PersistentDescriptorSet::new( PersistentDescriptorSet::new(
&self.descriptor_set_allocator, &self.descriptor_set_allocator,
layout.clone(), layout.clone(),
[WriteDescriptorSet::image_view_sampler(0, image, sampler)], [
WriteDescriptorSet::sampler(0, sampler),
WriteDescriptorSet::image_view(1, image),
],
[], [],
) )
.unwrap() .unwrap()
@ -275,10 +278,11 @@ mod fs {
layout(location = 0) out vec4 f_color; layout(location = 0) out vec4 f_color;
layout(set = 0, binding = 0) uniform sampler2D tex; layout(set = 0, binding = 0) uniform sampler s;
layout(set = 0, binding = 1) uniform texture2D tex;
void main() { void main() {
f_color = texture(tex, v_tex_coords); f_color = texture(sampler2D(tex, s), v_tex_coords);
} }
", ",
} }

View File

@ -428,9 +428,14 @@ fn main() {
PipelineBindPoint::Graphics, PipelineBindPoint::Graphics,
pipeline.layout().clone(), pipeline.layout().clone(),
0, 0,
[WriteDescriptorSet::image_view(0, texture.clone())] [
.into_iter() // If the binding is an immutable sampler, using push descriptors
.collect(), // you must write a dummy value to the binding.
WriteDescriptorSet::none(0),
WriteDescriptorSet::image_view(1, texture.clone()),
]
.into_iter()
.collect(),
) )
.unwrap() .unwrap()
.bind_vertex_buffers(0, vertex_buffer.clone()) .bind_vertex_buffers(0, vertex_buffer.clone())
@ -522,10 +527,11 @@ mod fs {
layout(location = 0) in vec2 tex_coords; layout(location = 0) in vec2 tex_coords;
layout(location = 0) out vec4 f_color; layout(location = 0) out vec4 f_color;
layout(set = 0, binding = 0) uniform sampler2D tex; layout(set = 0, binding = 0) uniform sampler s;
layout(set = 0, binding = 1) uniform texture2D tex;
void main() { void main() {
f_color = texture(tex, tex_coords); f_color = texture(sampler2D(tex, s), tex_coords);
} }
", ",
} }

View File

@ -410,10 +410,10 @@ fn main() {
let mut layout_create_info = let mut layout_create_info =
PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages); PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages);
// Adjust the info for set 0, binding 0 to make it variable with 2 descriptors. // Adjust the info for set 0, binding 1 to make it variable with 2 descriptors.
let binding = layout_create_info.set_layouts[0] let binding = layout_create_info.set_layouts[0]
.bindings .bindings
.get_mut(&0) .get_mut(&1)
.unwrap(); .unwrap();
binding.binding_flags |= DescriptorBindingFlags::VARIABLE_DESCRIPTOR_COUNT; binding.binding_flags |= DescriptorBindingFlags::VARIABLE_DESCRIPTOR_COUNT;
binding.descriptor_count = 2; binding.descriptor_count = 2;
@ -458,14 +458,10 @@ fn main() {
&descriptor_set_allocator, &descriptor_set_allocator,
layout.clone(), layout.clone(),
2, 2,
[WriteDescriptorSet::image_view_sampler_array( [
0, WriteDescriptorSet::sampler(0, sampler),
0, WriteDescriptorSet::image_view_array(1, 0, [mascot_texture as _, vulkano_texture as _]),
[ ],
(mascot_texture as _, sampler.clone()),
(vulkano_texture as _, sampler),
],
)],
[], [],
) )
.unwrap(); .unwrap();
@ -667,10 +663,11 @@ mod fs {
layout(location = 0) out vec4 f_color; layout(location = 0) out vec4 f_color;
layout(set = 0, binding = 0) uniform sampler2D tex[]; layout(set = 0, binding = 0) uniform sampler s;
layout(set = 0, binding = 1) uniform texture2D tex[];
void main() { void main() {
f_color = texture(nonuniformEXT(tex[tex_i]), coords); f_color = texture(nonuniformEXT(sampler2D(tex[tex_i], s)), coords);
} }
", ",
} }

View File

@ -348,7 +348,10 @@ fn main() {
let set = PersistentDescriptorSet::new( let set = PersistentDescriptorSet::new(
&descriptor_set_allocator, &descriptor_set_allocator,
layout.clone(), layout.clone(),
[WriteDescriptorSet::image_view_sampler(0, texture, sampler)], [
WriteDescriptorSet::sampler(0, sampler),
WriteDescriptorSet::image_view(1, texture),
],
[], [],
) )
.unwrap(); .unwrap();
@ -543,10 +546,11 @@ mod fs {
layout(location = 1) flat in uint layer; layout(location = 1) flat in uint layer;
layout(location = 0) out vec4 f_color; layout(location = 0) out vec4 f_color;
layout(set = 0, binding = 0) uniform sampler2DArray tex; layout(set = 0, binding = 0) uniform sampler s;
layout(set = 0, binding = 1) uniform texture2DArray tex;
void main() { void main() {
f_color = texture(tex, vec3(tex_coords, layer)); f_color = texture(sampler2DArray(tex, s), vec3(tex_coords, layer));
} }
", ",
} }

View File

@ -413,17 +413,6 @@ impl WriteDescriptorSet {
let array_element_count = elements.len(); let array_element_count = elements.len();
debug_assert!(array_element_count != 0); debug_assert!(array_element_count != 0);
// VUID-VkWriteDescriptorSet-dstArrayElement-00321
if first_array_element + array_element_count > max_descriptor_count {
return Err(Box::new(ValidationError {
problem: "`first_array_element` + the number of provided elements is greater than \
the number of descriptors in the descriptor set binding"
.into(),
vuids: &["VUID-VkWriteDescriptorSet-dstArrayElement-00321"],
..Default::default()
}));
}
let validate_image_view = let validate_image_view =
|image_view: &ImageView, index: usize| -> Result<(), Box<ValidationError>> { |image_view: &ImageView, index: usize| -> Result<(), Box<ValidationError>> {
if image_view.image().image_type() == ImageType::Dim3d { if image_view.image().image_type() == ImageType::Dim3d {
@ -530,86 +519,88 @@ impl WriteDescriptorSet {
match layout_binding.descriptor_type { match layout_binding.descriptor_type {
DescriptorType::Sampler => { DescriptorType::Sampler => {
if !layout_binding.immutable_samplers.is_empty() { if layout_binding.immutable_samplers.is_empty() {
if layout let elements = if let WriteDescriptorSetElements::Sampler(elements) = elements {
.flags() elements
.intersects(DescriptorSetLayoutCreateFlags::PUSH_DESCRIPTOR) } else {
{ return Err(Box::new(ValidationError {
// For push descriptors, we must write a dummy element. context: "elements".into(),
if let WriteDescriptorSetElements::None(_) = elements { problem: format!(
// Do nothing "contains `{}` elements, but descriptor set binding {} \
} else { requires `sampler` elements",
provided_element_type(elements),
binding,
)
.into(),
// vuids?
..Default::default()
}));
};
for (index, sampler) in elements.iter().enumerate() {
assert_eq!(device, sampler.device());
if sampler.sampler_ycbcr_conversion().is_some() {
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "elements".into(), context: format!("elements[{}]", index).into(),
problem: format!( problem: "the descriptor type is not \
"contains `{}` elements, but the descriptor set \ `DescriptorType::CombinedImageSampler`, and the sampler has a \
binding requires `none` elements", sampler YCbCr conversion"
provided_element_type(elements) .into(),
)
.into(),
// vuids? // vuids?
..Default::default() ..Default::default()
})); }));
} }
if device.enabled_extensions().khr_portability_subset
&& !device.enabled_features().mutable_comparison_samplers
&& sampler.compare().is_some()
{
return Err(Box::new(ValidationError {
context: format!("elements[{}]", index).into(),
problem: "this device is a portability subset device, and \
the sampler has depth comparison enabled"
.into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[
Requires::Feature("mutable_comparison_samplers"),
])]),
vuids: &[
"VUID-VkDescriptorImageInfo-mutableComparisonSamplers-04450",
],
}));
}
}
} else if layout
.flags()
.intersects(DescriptorSetLayoutCreateFlags::PUSH_DESCRIPTOR)
{
// For push descriptors, we must write a dummy element.
if let WriteDescriptorSetElements::None(_) = elements {
// Do nothing
} else { } else {
// For regular descriptors, no element must be written.
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "binding".into(), context: "elements".into(),
problem: "no descriptors must be written to this \ problem: format!(
descriptor set binding" "contains `{}` elements, but descriptor set binding {} \
.into(), requires `none` elements",
provided_element_type(elements),
binding,
)
.into(),
// vuids? // vuids?
..Default::default() ..Default::default()
})); }));
} }
}
let elements = if let WriteDescriptorSetElements::Sampler(elements) = elements {
elements
} else { } else {
// For regular descriptors, no element must be written.
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "elements".into(), context: "binding".into(),
problem: format!( problem: "no descriptors must be written to this \
"contains `{}` elements, but the descriptor set \ descriptor set binding"
binding requires `sampler` elements", .into(),
provided_element_type(elements)
)
.into(),
// vuids? // vuids?
..Default::default() ..Default::default()
})); }));
};
for (index, sampler) in elements.iter().enumerate() {
assert_eq!(device, sampler.device());
if sampler.sampler_ycbcr_conversion().is_some() {
return Err(Box::new(ValidationError {
context: format!("elements[{}]", index).into(),
problem: "the descriptor type is not \
`DescriptorType::CombinedImageSampler`, and the sampler has a \
sampler YCbCr conversion"
.into(),
// vuids?
..Default::default()
}));
}
if device.enabled_extensions().khr_portability_subset
&& !device.enabled_features().mutable_comparison_samplers
&& sampler.compare().is_some()
{
return Err(Box::new(ValidationError {
context: format!("elements[{}]", index).into(),
problem: "this device is a portability subset device, and \
the sampler has depth comparison enabled"
.into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::Feature(
"mutable_comparison_samplers",
)])]),
vuids: &["VUID-VkDescriptorImageInfo-mutableComparisonSamplers-04450"],
}));
}
} }
} }
@ -622,9 +613,10 @@ impl WriteDescriptorSet {
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "elements".into(), context: "elements".into(),
problem: format!( problem: format!(
"contains `{}` elements, but the descriptor set \ "contains `{}` elements, but descriptor set binding {} \
binding requires `image_view_sampler` elements", requires `image_view_sampler` elements",
provided_element_type(elements) provided_element_type(elements),
binding,
) )
.into(), .into(),
// vuids? // vuids?
@ -731,9 +723,10 @@ impl WriteDescriptorSet {
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "elements".into(), context: "elements".into(),
problem: format!( problem: format!(
"contains `{}` elements, but the descriptor set \ "contains `{}` elements, but descriptor set binding {} \
binding requires `image_view` elements", requires `image_view` elements",
provided_element_type(elements) provided_element_type(elements),
binding,
) )
.into(), .into(),
..Default::default() ..Default::default()
@ -807,9 +800,10 @@ impl WriteDescriptorSet {
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "elements".into(), context: "elements".into(),
problem: format!( problem: format!(
"contains `{}` elements, but the descriptor set \ "contains `{}` elements, but descriptor set binding {} \
binding requires `image_view` elements", requires `image_view` elements",
provided_element_type(elements) provided_element_type(elements),
binding,
) )
.into(), .into(),
// vuids? // vuids?
@ -885,9 +879,10 @@ impl WriteDescriptorSet {
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "elements".into(), context: "elements".into(),
problem: format!( problem: format!(
"contains `{}` elements, but the descriptor set \ "contains `{}` elements, but descriptor set binding {} \
binding requires `image_view` elements", requires `image_view` elements",
provided_element_type(elements) provided_element_type(elements),
binding,
) )
.into(), .into(),
// vuids? // vuids?
@ -963,9 +958,10 @@ impl WriteDescriptorSet {
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "elements".into(), context: "elements".into(),
problem: format!( problem: format!(
"contains `{}` elements, but the descriptor set \ "contains `{}` elements, but descriptor set binding {} \
binding requires `buffer_view` elements", requires `buffer_view` elements",
provided_element_type(elements) provided_element_type(elements),
binding,
) )
.into(), .into(),
// vuids? // vuids?
@ -1002,9 +998,10 @@ impl WriteDescriptorSet {
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "elements".into(), context: "elements".into(),
problem: format!( problem: format!(
"contains `{}` elements, but the descriptor set \ "contains `{}` elements, but descriptor set binding {} \
binding requires `buffer_view` elements", requires `buffer_view` elements",
provided_element_type(elements) provided_element_type(elements),
binding,
) )
.into(), .into(),
// vuids? // vuids?
@ -1042,9 +1039,10 @@ impl WriteDescriptorSet {
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "elements".into(), context: "elements".into(),
problem: format!( problem: format!(
"contains `{}` elements, but the descriptor set \ "contains `{}` elements, but descriptor set binding {} \
binding requires `buffer` elements", requires `buffer` elements",
provided_element_type(elements) provided_element_type(elements),
binding,
) )
.into(), .into(),
// vuids? // vuids?
@ -1094,9 +1092,10 @@ impl WriteDescriptorSet {
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "elements".into(), context: "elements".into(),
problem: format!( problem: format!(
"contains `{}` elements, but the descriptor set \ "contains `{}` elements, but descriptor set binding {} \
binding requires `buffer` elements", requires `buffer` elements",
provided_element_type(elements) provided_element_type(elements),
binding,
) )
.into(), .into(),
// vuids? // vuids?
@ -1146,9 +1145,10 @@ impl WriteDescriptorSet {
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "elements".into(), context: "elements".into(),
problem: format!( problem: format!(
"contains `{}` elements, but the descriptor set \ "contains `{}` elements, but descriptor set binding {} \
binding requires `image_view` elements", requires `image_view` elements",
provided_element_type(elements) provided_element_type(elements),
binding,
) )
.into(), .into(),
// vuids? // vuids?
@ -1235,9 +1235,10 @@ impl WriteDescriptorSet {
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "elements".into(), context: "elements".into(),
problem: format!( problem: format!(
"contains `{}` elements, but the descriptor set \ "contains `{}` elements, but descriptor set binding {} \
binding requires `inline_uniform_block` elements", requires `inline_uniform_block` elements",
provided_element_type(elements) provided_element_type(elements),
binding,
) )
.into(), .into(),
..Default::default() ..Default::default()
@ -1286,9 +1287,10 @@ impl WriteDescriptorSet {
return Err(Box::new(ValidationError { return Err(Box::new(ValidationError {
context: "elements".into(), context: "elements".into(),
problem: format!( problem: format!(
"contains `{}` elements, but the descriptor set \ "contains `{}` elements, but descriptor set binding {} \
binding requires `acceleration_structure` elements", requires `acceleration_structure` elements",
provided_element_type(elements) provided_element_type(elements),
binding,
) )
.into(), .into(),
..Default::default() ..Default::default()
@ -1316,6 +1318,17 @@ impl WriteDescriptorSet {
} }
} }
// VUID-VkWriteDescriptorSet-dstArrayElement-00321
if first_array_element + array_element_count > max_descriptor_count {
return Err(Box::new(ValidationError {
problem: "`first_array_element` + the number of provided elements is greater than \
the number of descriptors in the descriptor set binding"
.into(),
vuids: &["VUID-VkWriteDescriptorSet-dstArrayElement-00321"],
..Default::default()
}));
}
Ok(()) Ok(())
} }