From 2a8c1b282322f0ab5eabef643e7c1a6c4a8c679b Mon Sep 17 00:00:00 2001 From: Francisco Casas Date: Sat, 28 Sep 2024 03:07:51 -0300 Subject: [PATCH] vkd3d-shader/hlsl: Sort signature elements by register id. --- libs/vkd3d-shader/tpf.c | 23 +++++++++++++++++++++-- tests/hlsl_d3d12.c | 26 +++++++++++++------------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/libs/vkd3d-shader/tpf.c b/libs/vkd3d-shader/tpf.c index 3a9f4744..835a3846 100644 --- a/libs/vkd3d-shader/tpf.c +++ b/libs/vkd3d-shader/tpf.c @@ -3232,19 +3232,37 @@ static void add_section(struct hlsl_ctx *ctx, struct dxbc_writer *dxbc, ctx->result = buffer->status; } +static int signature_element_pointer_compare(const void *x, const void *y) +{ + const struct signature_element *e = *(const struct signature_element **)x; + const struct signature_element *f = *(const struct signature_element **)y; + int ret; + + if ((ret = vkd3d_u32_compare(e->register_index, f->register_index))) + return ret; + return vkd3d_u32_compare(e->mask, f->mask); +} + static void tpf_write_signature(struct tpf_compiler *tpf, const struct shader_signature *signature, uint32_t tag) { bool output = tag == TAG_OSGN || (tag == TAG_PCSG && tpf->program->shader_version.type == VKD3D_SHADER_TYPE_HULL); + const struct signature_element **sorted_elements; struct vkd3d_bytecode_buffer buffer = {0}; unsigned int i; put_u32(&buffer, signature->element_count); put_u32(&buffer, 8); /* unknown */ + if (!(sorted_elements = vkd3d_calloc(signature->element_count, sizeof(*sorted_elements)))) + return; + for (i = 0; i < signature->element_count; ++i) + sorted_elements[i] = &signature->elements[i]; + qsort(sorted_elements, signature->element_count, sizeof(*sorted_elements), signature_element_pointer_compare); + for (i = 0; i < signature->element_count; ++i) { - const struct signature_element *element = &signature->elements[i]; + const struct signature_element *element = sorted_elements[i]; enum vkd3d_shader_sysval_semantic sysval; uint32_t used_mask = element->used_mask; @@ -3265,7 +3283,7 @@ static void tpf_write_signature(struct tpf_compiler *tpf, const struct shader_si for (i = 0; i < signature->element_count; ++i) { - const struct signature_element *element = &signature->elements[i]; + const struct signature_element *element = sorted_elements[i]; size_t string_offset; string_offset = put_string(&buffer, element->semantic_name); @@ -3273,6 +3291,7 @@ static void tpf_write_signature(struct tpf_compiler *tpf, const struct shader_si } add_section(tpf->ctx, &tpf->dxbc, tag, &buffer); + vkd3d_free(sorted_elements); } static D3D_SHADER_VARIABLE_CLASS sm4_class(const struct hlsl_type *type) diff --git a/tests/hlsl_d3d12.c b/tests/hlsl_d3d12.c index 356f0193..00b0dfe2 100644 --- a/tests/hlsl_d3d12.c +++ b/tests/hlsl_d3d12.c @@ -2688,19 +2688,19 @@ static void test_signature_reflection(void) {ps1_source, "ps_4_1", false, ps1_inputs, ARRAY_SIZE(ps1_inputs), ps1_outputs, ARRAY_SIZE(ps1_outputs)}, {ps2_source, "ps_4_0", true, ps2_inputs, ARRAY_SIZE(ps2_inputs), ps2_outputs, ARRAY_SIZE(ps2_outputs)}, {cs1_source, "cs_5_0"}, - {ps3_source, "ps_4_0", false, ps3_inputs, ARRAY_SIZE(ps3_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple), NULL, 0, true}, - {ps4_source, "ps_4_0", false, ps4_inputs, ARRAY_SIZE(ps4_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple), NULL, 0, true}, - {ps5_source, "ps_4_0", false, ps5_inputs, ARRAY_SIZE(ps5_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple), NULL, 0, true}, - {ps6_source, "ps_4_0", false, ps6_inputs, ARRAY_SIZE(ps6_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple), NULL, 0, true}, - {ps7_source, "ps_4_0", false, ps7_inputs, ARRAY_SIZE(ps7_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple), NULL, 0, true}, - {ps8_source, "ps_4_0", false, ps8_inputs, ARRAY_SIZE(ps8_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple), NULL, 0, true}, - {ps9_source, "ps_4_0", false, ps9_inputs, ARRAY_SIZE(ps9_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple), NULL, 0, true}, - {vs3_source, "vs_4_0", false, vs3_inputs, ARRAY_SIZE(vs3_inputs), vs3_outputs, ARRAY_SIZE(vs3_outputs), NULL, 0, true}, - {ps10_source, "ps_4_0", false, ps10_inputs, ARRAY_SIZE(ps10_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple), NULL, 0, true}, - {ps11_source, "ps_4_0", false, ps11_inputs, ARRAY_SIZE(ps11_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple), NULL, 0, true}, - {ps12_source, "ps_4_0", false, ps12_inputs, ARRAY_SIZE(ps12_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple), NULL, 0, true}, - {vs4_source, "vs_4_0", false, vs4_inputs, ARRAY_SIZE(vs4_inputs), vs4_outputs, ARRAY_SIZE(vs4_outputs), NULL, 0, true}, - {vs5_source, "vs_4_0", false, vs5_inputs, ARRAY_SIZE(vs5_inputs), vs5_outputs, ARRAY_SIZE(vs5_outputs), NULL, 0, true}, + {ps3_source, "ps_4_0", false, ps3_inputs, ARRAY_SIZE(ps3_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple)}, + {ps4_source, "ps_4_0", false, ps4_inputs, ARRAY_SIZE(ps4_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple)}, + {ps5_source, "ps_4_0", false, ps5_inputs, ARRAY_SIZE(ps5_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple)}, + {ps6_source, "ps_4_0", false, ps6_inputs, ARRAY_SIZE(ps6_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple)}, + {ps7_source, "ps_4_0", false, ps7_inputs, ARRAY_SIZE(ps7_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple)}, + {ps8_source, "ps_4_0", false, ps8_inputs, ARRAY_SIZE(ps8_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple)}, + {ps9_source, "ps_4_0", false, ps9_inputs, ARRAY_SIZE(ps9_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple)}, + {vs3_source, "vs_4_0", false, vs3_inputs, ARRAY_SIZE(vs3_inputs), vs3_outputs, ARRAY_SIZE(vs3_outputs)}, + {ps10_source, "ps_4_0", false, ps10_inputs, ARRAY_SIZE(ps10_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple)}, + {ps11_source, "ps_4_0", false, ps11_inputs, ARRAY_SIZE(ps11_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple)}, + {ps12_source, "ps_4_0", false, ps12_inputs, ARRAY_SIZE(ps12_inputs), ps_outputs_simple, ARRAY_SIZE(ps_outputs_simple)}, + {vs4_source, "vs_4_0", false, vs4_inputs, ARRAY_SIZE(vs4_inputs), vs4_outputs, ARRAY_SIZE(vs4_outputs)}, + {vs5_source, "vs_4_0", false, vs5_inputs, ARRAY_SIZE(vs5_inputs), vs5_outputs, ARRAY_SIZE(vs5_outputs)}, {hs1_source, "hs_5_0", false, NULL, 0, hs1_outputs, ARRAY_SIZE(hs1_outputs), hs1_patch_constants, ARRAY_SIZE(hs1_patch_constants)}, {hs2_source, "hs_5_0", false, NULL, 0, hs2_outputs, ARRAY_SIZE(hs2_outputs), hs2_patch_constants, ARRAY_SIZE(hs2_patch_constants)}, {hs3_source, "hs_5_0", false, NULL, 0, hs3_outputs, ARRAY_SIZE(hs3_outputs), hs3_patch_constants, ARRAY_SIZE(hs3_patch_constants)},