vkd3d-shader/ir: Lower UDIV to UDIV_SIMPLE and UREM.

This commit is contained in:
Giovanni Mascellani
2025-06-20 19:03:49 +02:00
committed by Henri Verbeet
parent 82ef9bf2e5
commit 596f02210d
Notes: Henri Verbeet 2025-06-25 17:09:38 +02:00
Approved-by: Henri Verbeet (@hverbeet)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/1603
2 changed files with 82 additions and 60 deletions

View File

@@ -1167,6 +1167,83 @@ static enum vkd3d_result vsir_program_lower_imul(struct vsir_program *program,
return VKD3D_OK;
}
static enum vkd3d_result vsir_program_lower_udiv(struct vsir_program *program,
struct vkd3d_shader_instruction *udiv, struct vsir_transformation_context *ctx)
{
struct vkd3d_shader_instruction_array *instructions = &program->instructions;
size_t pos = udiv - instructions->elements;
struct vkd3d_shader_instruction *ins, *mov;
unsigned int count = 2;
if (udiv->dst_count != 2)
{
vkd3d_shader_error(ctx->message_context, &udiv->location,
VKD3D_SHADER_ERROR_VSIR_INVALID_DEST_COUNT,
"Internal compiler error: invalid destination count %u for UDIV.",
udiv->dst_count);
return VKD3D_ERROR;
}
if (udiv->dst[0].reg.type != VKD3DSPR_NULL)
++count;
if (udiv->dst[1].reg.type != VKD3DSPR_NULL)
++count;
if (!shader_instruction_array_insert_at(instructions, pos + 1, count))
return VKD3D_ERROR_OUT_OF_MEMORY;
udiv = &instructions->elements[pos];
ins = &instructions->elements[pos + 1];
/* Save the sources in a SSA in case a destination collides with a source. */
mov = ins++;
if (!(vsir_instruction_init_with_params(program, mov, &udiv->location, VSIR_OP_MOV, 1, 1)))
return VKD3D_ERROR_OUT_OF_MEMORY;
mov->src[0] = udiv->src[0];
dst_param_init_ssa_vec4(&mov->dst[0], program->ssa_count, udiv->src[0].reg.data_type);
mov = ins++;
if (!(vsir_instruction_init_with_params(program, mov, &udiv->location, VSIR_OP_MOV, 1, 1)))
return VKD3D_ERROR_OUT_OF_MEMORY;
mov->src[0] = udiv->src[1];
dst_param_init_ssa_vec4(&mov->dst[0], program->ssa_count + 1, udiv->src[1].reg.data_type);
if (udiv->dst[0].reg.type != VKD3DSPR_NULL)
{
if (!(vsir_instruction_init_with_params(program, ins, &udiv->location, VSIR_OP_UDIV_SIMPLE, 1, 2)))
return VKD3D_ERROR_OUT_OF_MEMORY;
ins->flags = udiv->flags;
src_param_init_ssa_vec4(&ins->src[0], program->ssa_count, udiv->src[0].reg.data_type);
src_param_init_ssa_vec4(&ins->src[1], program->ssa_count + 1, udiv->src[1].reg.data_type);
ins->dst[0] = udiv->dst[0];
++ins;
}
if (udiv->dst[1].reg.type != VKD3DSPR_NULL)
{
if (!(vsir_instruction_init_with_params(program, ins, &udiv->location, VSIR_OP_UREM, 1, 2)))
return VKD3D_ERROR_OUT_OF_MEMORY;
ins->flags = udiv->flags;
src_param_init_ssa_vec4(&ins->src[0], program->ssa_count, udiv->src[0].reg.data_type);
src_param_init_ssa_vec4(&ins->src[1], program->ssa_count + 1, udiv->src[1].reg.data_type);
ins->dst[0] = udiv->dst[1];
++ins;
}
vkd3d_shader_instruction_make_nop(udiv);
program->ssa_count += 2;
return VKD3D_OK;
}
static enum vkd3d_result vsir_program_lower_sm1_sincos(struct vsir_program *program,
struct vkd3d_shader_instruction *sincos)
{
@@ -1553,6 +1630,11 @@ static enum vkd3d_result vsir_program_lower_instructions(struct vsir_program *pr
return ret;
break;
case VSIR_OP_UDIV:
if ((ret = vsir_program_lower_udiv(program, ins, ctx)) < 0)
return ret;
break;
case VSIR_OP_SINCOS:
if (ins->dst_count == 1)
{

View File

@@ -8079,63 +8079,6 @@ static void spirv_compiler_emit_imad(struct spirv_compiler *compiler,
spirv_compiler_emit_store_dst(compiler, dst, val_id);
}
static void spirv_compiler_emit_int_div(struct spirv_compiler *compiler,
const struct vkd3d_shader_instruction *instruction)
{
uint32_t type_id, val_id, src0_id, src1_id, condition_id, uint_max_id;
struct vkd3d_spirv_builder *builder = &compiler->spirv_builder;
const struct vkd3d_shader_dst_param *dst = instruction->dst;
const struct vkd3d_shader_src_param *src = instruction->src;
unsigned int component_count = 0;
if (dst[0].reg.type != VKD3DSPR_NULL)
{
component_count = vsir_write_mask_component_count(dst[0].write_mask);
type_id = spirv_compiler_get_type_id_for_dst(compiler, &dst[0]);
src0_id = spirv_compiler_emit_load_src(compiler, &src[0], dst[0].write_mask);
src1_id = spirv_compiler_emit_load_src(compiler, &src[1], dst[0].write_mask);
condition_id = spirv_compiler_emit_int_to_bool(compiler,
VKD3D_SHADER_CONDITIONAL_OP_NZ, src[1].reg.data_type, component_count, src1_id);
if (dst[0].reg.data_type == VKD3D_DATA_UINT64)
uint_max_id = spirv_compiler_get_constant_uint64_vector(compiler, UINT64_MAX, component_count);
else
uint_max_id = spirv_compiler_get_constant_uint_vector(compiler, 0xffffffff, component_count);
val_id = vkd3d_spirv_build_op_tr2(builder, &builder->function_stream, SpvOpUDiv, type_id, src0_id, src1_id);
/* The SPIR-V spec says: "The resulting value is undefined if Operand 2 is 0." */
val_id = vkd3d_spirv_build_op_select(builder, type_id, condition_id, val_id, uint_max_id);
spirv_compiler_emit_store_dst(compiler, &dst[0], val_id);
}
if (dst[1].reg.type != VKD3DSPR_NULL)
{
if (!component_count || dst[0].write_mask != dst[1].write_mask)
{
component_count = vsir_write_mask_component_count(dst[1].write_mask);
type_id = spirv_compiler_get_type_id_for_dst(compiler, &dst[1]);
src0_id = spirv_compiler_emit_load_src(compiler, &src[0], dst[1].write_mask);
src1_id = spirv_compiler_emit_load_src(compiler, &src[1], dst[1].write_mask);
condition_id = spirv_compiler_emit_int_to_bool(compiler,
VKD3D_SHADER_CONDITIONAL_OP_NZ, src[1].reg.data_type, component_count, src1_id);
if (dst[1].reg.data_type == VKD3D_DATA_UINT64)
uint_max_id = spirv_compiler_get_constant_uint64_vector(compiler, UINT64_MAX, component_count);
else
uint_max_id = spirv_compiler_get_constant_uint_vector(compiler, 0xffffffff, component_count);
}
val_id = vkd3d_spirv_build_op_tr2(builder, &builder->function_stream, SpvOpUMod, type_id, src0_id, src1_id);
/* The SPIR-V spec says: "The resulting value is undefined if Operand 2 is 0." */
val_id = vkd3d_spirv_build_op_select(builder, type_id, condition_id, val_id, uint_max_id);
spirv_compiler_emit_store_dst(compiler, &dst[1], val_id);
}
}
static void spirv_compiler_emit_ftoi(struct spirv_compiler *compiler,
const struct vkd3d_shader_instruction *instruction)
{
@@ -10754,9 +10697,6 @@ static int spirv_compiler_handle_instruction(struct spirv_compiler *compiler,
case VSIR_OP_IMAD:
spirv_compiler_emit_imad(compiler, instruction);
break;
case VSIR_OP_UDIV:
spirv_compiler_emit_int_div(compiler, instruction);
break;
case VSIR_OP_DTOI:
case VSIR_OP_FTOI:
spirv_compiler_emit_ftoi(compiler, instruction);