From fdfb74b20b08144e144299bc0b7b20b95421c6c8 Mon Sep 17 00:00:00 2001 From: Henri Verbeet Date: Sun, 23 Nov 2025 01:12:00 +0100 Subject: [PATCH] vkd3d-shader/ir: Lower VSIR_OP_NRM instructions. --- libs/vkd3d-shader/ir.c | 60 ++++++++++++++++++++++++ libs/vkd3d-shader/vkd3d_shader_private.h | 18 +++++++ tests/d3dbc/nrm.shader_test | 24 +++++----- 3 files changed, 90 insertions(+), 12 deletions(-) diff --git a/libs/vkd3d-shader/ir.c b/libs/vkd3d-shader/ir.c index 1a0c9d833..3548e748c 100644 --- a/libs/vkd3d-shader/ir.c +++ b/libs/vkd3d-shader/ir.c @@ -1323,6 +1323,62 @@ static enum vkd3d_result vsir_program_lower_ifc(struct vsir_program *program, return VKD3D_OK; } +static enum vkd3d_result vsir_program_lower_nrm(struct vsir_program *program, struct vsir_program_iterator *nrm) +{ + struct vkd3d_shader_instruction *ins = vsir_program_iterator_current(nrm); + const struct vkd3d_shader_location location = ins->location; + const struct vkd3d_shader_src_param *src = ins->src; + const struct vkd3d_shader_dst_param *dst = ins->dst; + unsigned int dot_id, rsq_id, mul_id; + struct vsir_program_iterator it; + + /* nrm DST, SRC + * -> + * dp3 srDOT, SRC, SRC + * rsq srRSQ, srDOT + * mul srMUL, srRSQ, SRC + * movc DST, srDOT, srMUL, srDOT */ + + if (!(ins = vsir_program_iterator_insert_before(nrm, &it, 3))) + return VKD3D_ERROR_OUT_OF_MEMORY; + if (!vsir_instruction_init_with_params(program, ins, &location, VSIR_OP_DP3, 1, 2)) + goto fail; + dot_id = program->ssa_count++; + dst_param_init_ssa(&ins->dst[0], dot_id, src[0].reg.data_type, VSIR_DIMENSION_SCALAR); + ins->src[0] = src[0]; + ins->src[1] = src[0]; + + ins = vsir_program_iterator_next(&it); + if (!vsir_instruction_init_with_params(program, ins, &location, VSIR_OP_RSQ, 1, 1)) + goto fail; + rsq_id = program->ssa_count++; + dst_param_init_ssa(&ins->dst[0], rsq_id, src[0].reg.data_type, VSIR_DIMENSION_SCALAR); + src_param_init_ssa(&ins->src[0], dot_id, src[0].reg.data_type, VSIR_DIMENSION_SCALAR); + + ins = vsir_program_iterator_next(&it); + if (!vsir_instruction_init_with_params(program, ins, &location, VSIR_OP_MUL, 1, 2)) + goto fail; + mul_id = program->ssa_count++; + dst_param_init_ssa(&ins->dst[0], mul_id, src[0].reg.data_type, dst[0].reg.dimension); + src_param_init_ssa(&ins->src[0], rsq_id, src[0].reg.data_type, VSIR_DIMENSION_SCALAR); + ins->src[1] = src[0]; + + ins = vsir_program_iterator_next(&it); + if (!vsir_instruction_init_with_params(program, ins, &location, VSIR_OP_MOVC, 1, 3)) + goto fail; + ins->dst[0] = dst[0]; + src_param_init_ssa(&ins->src[0], dot_id, VSIR_DATA_U32, VSIR_DIMENSION_SCALAR); + src_param_init_ssa(&ins->src[1], mul_id, src[0].reg.data_type, dst[0].reg.dimension); + src_param_init_ssa(&ins->src[2], dot_id, src[0].reg.data_type, VSIR_DIMENSION_SCALAR); + + return VKD3D_OK; + +fail: + vsir_program_iterator_nop_range(&it, nrm, &location); + + return VKD3D_ERROR_OUT_OF_MEMORY; +} + static enum vkd3d_result vsir_program_lower_texkill(struct vsir_program *program, struct vsir_program_iterator *it, unsigned int *tmp_idx) { @@ -2364,6 +2420,10 @@ static enum vkd3d_result vsir_program_lower_d3dbc_instructions(struct vsir_progr ret = vsir_program_lower_ifc(program, &it, &tmp_idx, message_context); break; + case VSIR_OP_NRM: + ret = vsir_program_lower_nrm(program, &it); + break; + case VSIR_OP_SINCOS: ret = vsir_program_lower_sm1_sincos(program, &it); break; diff --git a/libs/vkd3d-shader/vkd3d_shader_private.h b/libs/vkd3d-shader/vkd3d_shader_private.h index 2ca0e2c5c..935a819be 100644 --- a/libs/vkd3d-shader/vkd3d_shader_private.h +++ b/libs/vkd3d-shader/vkd3d_shader_private.h @@ -1572,6 +1572,24 @@ static inline struct vkd3d_shader_instruction *vsir_program_iterator_insert_befo return vsir_program_iterator_current(it); } +static inline void vsir_program_iterator_nop_range(const struct vsir_program_iterator *first, + const struct vsir_program_iterator *last, const struct vkd3d_shader_location *location) +{ + const struct vkd3d_shader_instruction_array *array = first->array; + size_t first_idx = first->idx; + size_t last_idx = last->idx; + size_t idx; + + VKD3D_ASSERT(last->array == array); + VKD3D_ASSERT(last_idx < array->count); + VKD3D_ASSERT(first_idx <= last_idx); + + for (idx = first_idx; idx <= last_idx; ++idx) + { + vsir_instruction_init(&array->elements[idx], location, VSIR_OP_NOP); + } +} + enum vkd3d_shader_config_flags { VKD3D_SHADER_CONFIG_FLAG_FORCE_VALIDATION = 0x00000001, diff --git a/tests/d3dbc/nrm.shader_test b/tests/d3dbc/nrm.shader_test index c0305a1f3..7bfc0dd04 100644 --- a/tests/d3dbc/nrm.shader_test +++ b/tests/d3dbc/nrm.shader_test @@ -24,7 +24,7 @@ ffff0300 % ps_3_0 0000ffff % end [test] -todo draw quad +draw quad probe (320, 240) f32(.707106769, .707106769, 0, .707106769) [pixel shader d3dbc-hex] @@ -38,7 +38,7 @@ ffff0300 % ps_3_0 [test] uniform 0 float4 .1 .1 .1 .1 -todo draw quad +draw quad probe (320, 240) f32(.707106769, .707106769, .1, .1) [pixel shader d3dbc-hex] @@ -49,7 +49,7 @@ ffff0300 % ps_3_0 0000ffff % end [test] -todo draw quad +draw quad probe (320, 240) f32(0, .957826257, .287347883, .478913128) [pixel shader d3dbc-hex] @@ -60,7 +60,7 @@ ffff0300 % ps_3_0 0000ffff % end [test] -todo draw quad +draw quad probe (320, 240) f32(0, .957826257, .287347883, .478913128) [pixel shader d3dbc-hex] @@ -73,7 +73,7 @@ ffff0300 % ps_3_0 0000ffff % end [test] -todo draw quad +draw quad probe (320, 240) f32(0, .957826257, .1, .1) [pixel shader d3dbc-hex] @@ -85,7 +85,7 @@ ffff0300 % ps_3_0 0000ffff % end [test] -todo draw quad +draw quad probe (320, 240) f32(0, .707106769, .707106769, .707106769) [pixel shader d3dbc-hex] @@ -97,7 +97,7 @@ ffff0300 % ps_3_0 0000ffff % end [test] -todo draw quad +draw quad probe (320, 240) f32(.816496611, .408248276, .408248276, .1) 1 [pixel shader d3dbc-hex] @@ -109,7 +109,7 @@ ffff0300 % ps_3_0 0000ffff % end [test] -todo draw quad +draw quad probe (320, 240) f32(0, .707106769, .707106769, .707106769) [pixel shader d3dbc-hex] @@ -121,13 +121,13 @@ ffff0300 % ps_3_0 [test] uniform 0 float4 0 0 0 0 -todo draw quad +draw quad probe (320, 240) f32(0, 0, 0, 0) uniform 0 float4 2.0 0.0 0.0 0.5 -todo draw quad +draw quad probe (320, 240) f32(1.0, 0.0, .25, .25) uniform 0 float4 1.0 1.0 1.0 1.0 -todo draw quad +draw quad probe (320, 240) f32(.577350259, .577350259, .577350259, .577350259) [pixel shader d3dbc-hex] @@ -138,5 +138,5 @@ ffff0300 % ps_3_0 [test] uniform 0 float4 1.0 1.0 0.0 2.0 -todo draw quad +draw quad probe (320, 240) f32(.447213590, 0, .894427180, .447213590)