Skip to content

Commit 8927ca2

Browse files
committed
Fix Vulkan errors for garbage values in blend states (refs to local variables out of scope..)
1 parent bf3aa87 commit 8927ca2

3 files changed

Lines changed: 40 additions & 23 deletions

File tree

src/render_graph/shadow_map_pass.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,15 @@ impl ShadowMapPass {
178178
vertex_desc,
179179
COLOR_ATTACHMENT_COUNT,
180180
|builder| {
181+
let mut attachment_blends =
182+
Vec::<vk::PipelineColorBlendAttachmentState>::with_capacity(COLOR_ATTACHMENT_COUNT);
183+
181184
let pipeline_create_info = builder
182185
.depth_stencil_state(&ps_depth_less_stencil_always())
183186
// see https://docs.microsoft.com/en-us/windows/desktop/DxTechArts/common-techniques-to-improve-shadow-depth-maps#back-face-and-front-face
184187
.rasterization_state(&ps_raster_polygons(vk::CullModeFlags::NONE))
185188
// disable writes to color (we do not set any attachments anyway)
186-
.color_blend_state(&ps_color_blend_override(COLOR_ATTACHMENT_COUNT, vk::ColorComponentFlags::empty()))
189+
.color_blend_state(&ps_color_blend_override(&mut attachment_blends, COLOR_ATTACHMENT_COUNT, vk::ColorComponentFlags::empty()))
187190
.build();
188191
create_pipeline(device, pipeline_cache, pipeline_create_info)
189192
},

src/render_graph/tfx_render/tfx_ppll_build_pass.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,13 @@ impl TfxPpllBuildPass {
152152
.back(stencil_write_hair)
153153
.build();
154154

155+
let mut attachment_blends =
156+
Vec::<vk::PipelineColorBlendAttachmentState>::with_capacity(Self::COLOR_ATTACHMENT_COUNT);
157+
155158
let pipeline_create_info = builder
156159
.depth_stencil_state(&depth_stencil)
157160
.color_blend_state(&ps_color_blend_override(
161+
&mut attachment_blends,
158162
Self::COLOR_ATTACHMENT_COUNT,
159163
vk::ColorComponentFlags::empty(),
160164
))

src/vk_utils/pipeline.rs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,21 @@ pub fn create_pipeline_with_defaults(
6666

6767
let dynamic_state = ps_dynamic_state(&[vk::DynamicState::VIEWPORT, vk::DynamicState::SCISSOR]);
6868

69+
let mut attachment_blends =
70+
Vec::<vk::PipelineColorBlendAttachmentState>::with_capacity(color_attachment_count);
71+
6972
// create pipeline itself
7073
let stages = [stage_vs, stage_fs];
7174
let input_assembly_state = ps_ia_triangle_list();
7275
let viewport_state = ps_viewport_single_dynamic();
7376
let rasterization_state = ps_raster_polygons(vk::CullModeFlags::NONE);
7477
let multisample_state = ps_multisample_disabled();
7578
let depth_stencil_state = ps_depth_always_stencil_always();
76-
let color_blend_state =
77-
ps_color_blend_override(color_attachment_count, vk::ColorComponentFlags::RGBA);
79+
let color_blend_state = ps_color_blend_override(
80+
&mut attachment_blends,
81+
color_attachment_count,
82+
vk::ColorComponentFlags::RGBA,
83+
);
7884
let create_info_builder = vk::GraphicsPipelineCreateInfo::builder()
7985
.stages(&stages)
8086
.vertex_input_state(&vertex_desc)
@@ -267,44 +273,48 @@ pub fn ps_multisample_disabled() -> vk::PipelineMultisampleStateCreateInfo {
267273
}
268274

269275
/// Write result to all color attachments, disable blending
270-
fn ps_color_attachments_write_all(
271-
attachment_count: usize,
276+
pub fn ps_color_attachment_override(
272277
color_write_mask: vk::ColorComponentFlags,
273-
) -> Vec<vk::PipelineColorBlendAttachmentState> {
274-
// VULKAN SPEC:
275-
// > If the independent blending feature is not enabled on the device,
276-
// all VkPipelineColorBlendAttachmentState elements in the pAttachments
277-
// array must be identical.
278-
278+
) -> vk::PipelineColorBlendAttachmentState {
279279
// PS. I always hated blend state
280-
let write_all = vk::PipelineColorBlendAttachmentState::builder()
280+
vk::PipelineColorBlendAttachmentState::builder()
281281
.color_write_mask(color_write_mask)
282282
.blend_enable(false)
283283
.src_color_blend_factor(vk::BlendFactor::ONE) // shader output
284284
.dst_color_blend_factor(vk::BlendFactor::ZERO) // existing value on destination attachment
285285
.src_alpha_blend_factor(vk::BlendFactor::ONE) // shader output
286286
.dst_alpha_blend_factor(vk::BlendFactor::ZERO) // existing value on destination attachment
287-
.build();
287+
.build()
288+
}
288289

289-
let mut attachments =
290-
Vec::<vk::PipelineColorBlendAttachmentState>::with_capacity(attachment_count);
291-
for _i in 0..attachment_count {
292-
attachments.push(write_all);
290+
/// Copy blends state `color_attachment_count` times.
291+
///
292+
/// **VULKAN SPEC:**
293+
/// > If the independent blending feature is not enabled on the device,
294+
/// all VkPipelineColorBlendAttachmentState elements in the pAttachments
295+
/// array must be identical.
296+
pub fn ps_color_blend_state(
297+
attachment_blends: &mut Vec<vk::PipelineColorBlendAttachmentState>, // needed cause `PipelineColorBlendAttachmentState` has ref
298+
color_attachment_count: usize,
299+
blend_state: vk::PipelineColorBlendAttachmentState,
300+
) -> vk::PipelineColorBlendStateCreateInfo {
301+
for _i in 0..color_attachment_count {
302+
attachment_blends.push(blend_state);
293303
}
294304

295-
attachments
305+
vk::PipelineColorBlendStateCreateInfo::builder()
306+
.attachments(&attachment_blends)
307+
.build()
296308
}
297309

298310
/// Write result to all color attachments, disable blending
299311
pub fn ps_color_blend_override(
312+
attachment_blends: &mut Vec<vk::PipelineColorBlendAttachmentState>,
300313
color_attachment_count: usize,
301314
color_write_mask: vk::ColorComponentFlags,
302315
) -> vk::PipelineColorBlendStateCreateInfo {
303-
let color_attachments_write_all =
304-
ps_color_attachments_write_all(color_attachment_count, color_write_mask);
305-
vk::PipelineColorBlendStateCreateInfo::builder()
306-
.attachments(&color_attachments_write_all)
307-
.build()
316+
let blend_state = ps_color_attachment_override(color_write_mask);
317+
ps_color_blend_state(attachment_blends, color_attachment_count, blend_state)
308318
}
309319

310320
/// List of things that will be provided as separate command before draw (actuall 'runtime').

0 commit comments

Comments
 (0)