From 9867d48c3ae40f32d13f0b525a03a4b45668f724 Mon Sep 17 00:00:00 2001 From: Giovanni Mascellani Date: Wed, 16 Jul 2025 22:06:00 +0200 Subject: [PATCH] vkd3d-shader/ir: Use the iterator in vsir_program_lower_ifc(). --- libs/vkd3d-shader/ir.c | 16 +++++++--------- libs/vkd3d-shader/vkd3d_shader_private.h | 8 ++++++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/libs/vkd3d-shader/ir.c b/libs/vkd3d-shader/ir.c index 9ceb81f2c..02b55e2be 100644 --- a/libs/vkd3d-shader/ir.c +++ b/libs/vkd3d-shader/ir.c @@ -971,18 +971,16 @@ static enum vkd3d_result vsir_program_normalize_addr(struct vsir_program *progra } static enum vkd3d_result vsir_program_lower_ifc(struct vsir_program *program, - struct vkd3d_shader_instruction *ifc, unsigned int *tmp_idx, + struct vsir_program_iterator *it, unsigned int *tmp_idx, struct vkd3d_shader_message_context *message_context) { - struct vkd3d_shader_instruction_array *instructions = &program->instructions; - size_t pos = ifc - instructions->elements; - struct vkd3d_shader_instruction *ins; + struct vkd3d_shader_instruction *ifc, *ins; enum vkd3d_shader_opcode opcode; bool swap; - if (!shader_instruction_array_insert_at(instructions, pos + 1, 2)) + if (!vsir_program_iterator_insert_after(it, 2)) return VKD3D_ERROR_OUT_OF_MEMORY; - ifc = &instructions->elements[pos]; + ifc = vsir_program_iterator_current(it); if (*tmp_idx == ~0u) *tmp_idx = program->temp_count++; @@ -996,7 +994,7 @@ static enum vkd3d_result vsir_program_lower_ifc(struct vsir_program *program, return VKD3D_ERROR_NOT_IMPLEMENTED; } - ins = &instructions->elements[pos + 1]; + ins = vsir_program_iterator_next(it); if (!vsir_instruction_init_with_params(program, ins, &ifc->location, opcode, 1, 2)) return VKD3D_ERROR_OUT_OF_MEMORY; @@ -1009,7 +1007,7 @@ static enum vkd3d_result vsir_program_lower_ifc(struct vsir_program *program, ins->src[1] = ifc->src[!swap]; /* Create new if instruction using the previous result. */ - ins = &instructions->elements[pos + 2]; + ins = vsir_program_iterator_next(it); if (!vsir_instruction_init_with_params(program, ins, &ifc->location, VSIR_OP_IF, 0, 1)) return VKD3D_ERROR_OUT_OF_MEMORY; ins->flags = VKD3D_SHADER_CONDITIONAL_OP_NZ; @@ -1592,7 +1590,7 @@ static enum vkd3d_result vsir_program_lower_instructions(struct vsir_program *pr switch (ins->opcode) { case VSIR_OP_IFC: - if ((ret = vsir_program_lower_ifc(program, ins, &tmp_idx, message_context)) < 0) + if ((ret = vsir_program_lower_ifc(program, &it, &tmp_idx, message_context)) < 0) return ret; break; diff --git a/libs/vkd3d-shader/vkd3d_shader_private.h b/libs/vkd3d-shader/vkd3d_shader_private.h index 347fd51fe..c07d915eb 100644 --- a/libs/vkd3d-shader/vkd3d_shader_private.h +++ b/libs/vkd3d-shader/vkd3d_shader_private.h @@ -1456,6 +1456,14 @@ static inline struct vkd3d_shader_instruction *vsir_program_iterator_next( return vsir_program_iterator_current(iterator); } +/* When insertion takes place, argument `it' is updated to point to the same + * instruction as before the insertion, but all other iterators and pointers + * to the same container are invalidated and cannot be used any more. */ +static inline bool vsir_program_iterator_insert_after(struct vsir_program_iterator *it, unsigned int count) +{ + return shader_instruction_array_insert_at(it->array, it->idx + 1, count); +} + enum vkd3d_shader_config_flags { VKD3D_SHADER_CONFIG_FLAG_FORCE_VALIDATION = 0x00000001,