Skip to content

Commit

Permalink
also use depth attach size, check attach both ways
Browse files Browse the repository at this point in the history
  • Loading branch information
DGriffin91 committed Aug 25, 2024
1 parent 564883b commit 780dcea
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 39 deletions.
99 changes: 98 additions & 1 deletion examples/multipass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn main() -> Result<(), DisplayError> {
let depth_stencil_format = best_depth_stencil_format(&event_loop.device);
let mut pool = LazyPool::new(&event_loop.device);
let fill_background = create_fill_background_pipeline(&event_loop.device);
let prepass = create_prepass_pipeline(&event_loop.device);
let pbr = create_pbr_pipeline(&event_loop.device);
let funky_shape = create_funky_shape(&event_loop, &mut pool)?;

Expand Down Expand Up @@ -80,9 +81,28 @@ fn main() -> Result<(), DisplayError> {
let push_const_data = write_push_consts(obj_pos, material);

let mut write = DepthStencilMode::DEPTH_WRITE;

// Depth Prepass
frame
.render_graph
.begin_pass("Depth Prepass")
.bind_pipeline(&prepass)
.set_depth_stencil(write)
.read_descriptor(0, camera_buf)
.access_node(index_buf, AccessType::IndexBuffer)
.access_node(vertex_buf, AccessType::VertexBuffer)
.clear_depth_stencil(depth_stencil)
.store_depth_stencil(depth_stencil)
.record_subpass(move |subpass, _| {
subpass
.bind_index_buffer(index_buf, vk::IndexType::UINT16)
.bind_vertex_buffer(vertex_buf)
.draw_indexed(funky_shape.index_count, 1, 0, 0, 0);
});

write.stencil_test = true;
write.depth_test = false;
write.front.compare_op = vk::CompareOp::ALWAYS;
write.front.compare_op = vk::CompareOp::GREATER_OR_EQUAL;
write.front.compare_mask = 0xff;
write.front.write_mask = 0xff;
write.front.reference = 0x01;
Expand Down Expand Up @@ -330,6 +350,83 @@ fn create_fill_background_pipeline(device: &Arc<Device>) -> Arc<GraphicPipeline>
)
}

fn create_prepass_pipeline(device: &Arc<Device>) -> Arc<GraphicPipeline> {
let vertex_shader = Shader::new_vertex(
inline_spirv!(
r#"
#version 450
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec3 inNormal;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
mat4 view;
vec3 camPos;
} ubo;
layout (location = 0) out vec3 outWorldPos;
layout (location = 1) out vec3 outNormal;
layout(push_constant) uniform PushConsts {
vec3 objPos;
} pushConsts;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
vec3 locPos = vec3(ubo.model * vec4(inPos, 1.0));
outWorldPos = locPos + pushConsts.objPos;
outNormal = mat3(ubo.model) * inNormal;
gl_Position = ubo.projection * ubo.view * vec4(outWorldPos, 1.0);
}
"#,
vert
)
.as_slice(),
);

let fragment_shader = Shader::new_fragment(
inline_spirv!(
r#"
#version 450
layout (location = 0) in vec3 inWorldPos;
layout (location = 1) in vec3 inNormal;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
mat4 view;
vec3 camPos;
} ubo;
void main()
{
}
"#,
frag
)
.as_slice(),
);

Arc::new(
GraphicPipeline::create(
device,
GraphicPipelineInfo::default(),
[vertex_shader, fragment_shader],
)
.unwrap(),
)
}

fn create_pbr_pipeline(device: &Arc<Device>) -> Arc<GraphicPipeline> {
// See: https://github.com/SaschaWillems/Vulkan/blob/master/data/shaders/glsl/pbrbasic/pbr.vert
let vertex_shader = Shader::new_vertex(
Expand Down
99 changes: 61 additions & 38 deletions src/graph/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,49 +246,58 @@ impl Resolver {
return false;
}

// Compare individual color attachments for compatibility
for (attachment_idx, lhs_attachment) in lhs_exec
.color_attachments
.iter()
.chain(lhs_exec.color_loads.iter())
.chain(lhs_exec.color_stores.iter())
.chain(
lhs_exec
.color_clears
.iter()
.map(|(attachment_idx, (attachment, _))| (attachment_idx, attachment)),
)
.chain(
lhs_exec
.color_resolves
.iter()
.map(|(attachment_idx, (attachment, _))| (attachment_idx, attachment)),
)
{
let rhs_attachment = rhs_exec
// Check attachment compatibility both ways since one may have a different quantity than the other
for (a_exec, b_exec) in [(lhs_exec, rhs_exec), (rhs_exec, lhs_exec)] {
// Compare individual color attachments for compatibility
for (attachment_idx, a_attachment) in a_exec
.color_attachments
.get(attachment_idx)
.or_else(|| rhs_exec.color_loads.get(attachment_idx))
.or_else(|| rhs_exec.color_stores.get(attachment_idx))
.or_else(|| {
rhs_exec
.iter()
.chain(a_exec.color_loads.iter())
.chain(a_exec.color_stores.iter())
.chain(
a_exec
.color_clears
.get(attachment_idx)
.map(|(attachment, _)| attachment)
})
.or_else(|| {
rhs_exec
.iter()
.map(|(attachment_idx, (attachment, _))| (attachment_idx, attachment)),
)
.chain(
a_exec
.color_resolves
.get(attachment_idx)
.map(|(attachment, _)| attachment)
});
.iter()
.map(|(attachment_idx, (attachment, _))| (attachment_idx, attachment)),
)
{
let b_attachment = b_exec
.color_attachments
.get(attachment_idx)
.or_else(|| b_exec.color_loads.get(attachment_idx))
.or_else(|| b_exec.color_stores.get(attachment_idx))
.or_else(|| {
b_exec
.color_clears
.get(attachment_idx)
.map(|(attachment, _)| attachment)
})
.or_else(|| {
b_exec
.color_resolves
.get(attachment_idx)
.map(|(attachment, _)| attachment)
});

if !Attachment::are_compatible(Some(*lhs_attachment), rhs_attachment.copied()) {
trace!(" incompatible color attachments");
if b_attachment.is_none() {
trace!(" incompatible color attachments");

return false;
} else {
common_color_attachment = true;
return false;
}

if !Attachment::are_compatible(Some(*a_attachment), b_attachment.copied()) {
trace!(" incompatible color attachments");

return false;
} else {
common_color_attachment = true;
}
}
}

Expand Down Expand Up @@ -2451,6 +2460,20 @@ impl Resolver {
height = height.min(attachment_height);
}

if let Some((attachment_width, attachment_height)) =
first_exec.depth_stencil_clear.map(|(attachment, _)| {
let info = bindings[attachment.target].as_driver_image().unwrap().info;
(info.width, info.height)
})
{
if (width, height) != (u32::MAX, u32::MAX) {
assert_eq!(width, attachment_width);
assert_eq!(height, attachment_height);
}
width = width.min(attachment_width);
height = height.min(attachment_height);
}

Area {
height,
width,
Expand Down

0 comments on commit 780dcea

Please sign in to comment.