From 0b638d3be56fed3a0f187110727b36f84ba8dfaa Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Tue, 10 Sep 2024 07:15:13 +0200 Subject: [PATCH] Write textures and samplers in descriptor sets --- Sources/backends/hlsl.c | 77 +++++++++++++++++++++++++++++++++---- Sources/integrations/kope.c | 6 +++ Sources/sets.c | 2 +- 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/Sources/backends/hlsl.c b/Sources/backends/hlsl.c index a52fd9a..0bf6c7e 100644 --- a/Sources/backends/hlsl.c +++ b/Sources/backends/hlsl.c @@ -303,29 +303,90 @@ static size_t all_descriptor_sets_count = 0; static void write_root_signature(char *hlsl, size_t *offset) { uint32_t cbv_index = 0; + uint32_t srv_index = 0; + uint32_t sampler_index = 0; *offset += sprintf(&hlsl[*offset], "[RootSignature(\"RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT)"); for (size_t set_count = 0; set_count < all_descriptor_sets_count; ++set_count) { - *offset += sprintf(&hlsl[*offset], "\\\n, DescriptorTable("); - descriptor_set *set = all_descriptor_sets[set_count]; + + bool has_sampler = false; + bool has_other = false; + for (size_t definition_index = 0; definition_index < set->definitions_count; ++definition_index) { definition *def = &set->definitions[definition_index]; switch (def->kind) { case DEFINITION_CONST_CUSTOM: - *offset += sprintf(&hlsl[*offset], "CBV(b%i)", cbv_index); - cbv_index += 1; + case DEFINITION_TEX2D: + case DEFINITION_TEXCUBE: + has_other = true; + break; + case DEFINITION_SAMPLER: + has_sampler = true; break; - default: { - debug_context context = {0}; - error(context, "Can not write definition to root signature"); } + } + + if (has_other) { + *offset += sprintf(&hlsl[*offset], "\\\n, DescriptorTable("); + + bool first = true; + for (size_t definition_index = 0; definition_index < set->definitions_count; ++definition_index) { + definition *def = &set->definitions[definition_index]; + + switch (def->kind) { + case DEFINITION_CONST_CUSTOM: + if (first) { + first = false; + } + else { + *offset += sprintf(&hlsl[*offset], ", "); + } + *offset += sprintf(&hlsl[*offset], "CBV(b%i)", cbv_index); + cbv_index += 1; + break; + case DEFINITION_TEX2D: + case DEFINITION_TEXCUBE: + if (first) { + first = false; + } + else { + *offset += sprintf(&hlsl[*offset], ", "); + } + *offset += sprintf(&hlsl[*offset], "SRV(t%i)", srv_index); + srv_index += 1; + break; + } } + + *offset += sprintf(&hlsl[*offset], ")"); } - *offset += sprintf(&hlsl[*offset], ")"); + if (has_sampler) { + *offset += sprintf(&hlsl[*offset], "\\\n, DescriptorTable("); + + bool first = true; + for (size_t definition_index = 0; definition_index < set->definitions_count; ++definition_index) { + definition *def = &set->definitions[definition_index]; + + switch (def->kind) { + case DEFINITION_SAMPLER: + if (first) { + first = false; + } + else { + *offset += sprintf(&hlsl[*offset], ", "); + } + *offset += sprintf(&hlsl[*offset], "Sampler(s%i)", sampler_index); + sampler_index += 1; + break; + } + } + + *offset += sprintf(&hlsl[*offset], ")"); + } } *offset += sprintf(&hlsl[*offset], "\")]\n"); diff --git a/Sources/integrations/kope.c b/Sources/integrations/kope.c index 81e5ba0..8a2a91f 100644 --- a/Sources/integrations/kope.c +++ b/Sources/integrations/kope.c @@ -354,6 +354,12 @@ void kope_export(char *directory, api_kind api) { case DEFINITION_CONST_CUSTOM: fprintf(output, "\tkope_g5_buffer *%s;\n", get_name(get_global(d.global)->name)); break; + case DEFINITION_TEX2D: + fprintf(output, "\tkope_g5_texture *%s;\n", get_name(get_global(d.global)->name)); + break; + case DEFINITION_SAMPLER: + fprintf(output, "\tkope_g5_texture *%s;\n", get_name(get_global(d.global)->name)); + break; default: { debug_context context = {0}; error(context, "Unexpected kind of definition"); diff --git a/Sources/sets.c b/Sources/sets.c index bc94ef5..e60a0de 100644 --- a/Sources/sets.c +++ b/Sources/sets.c @@ -32,7 +32,7 @@ void add_definition_to_set(descriptor_set *set, definition def) { assert(def.kind != DEFINITION_FUNCTION && def.kind != DEFINITION_STRUCT); for (size_t definition_index = 0; definition_index < set->definitions_count; ++definition_index) { - if (set->definitions[definition_index].global = def.global) { + if (set->definitions[definition_index].global == def.global) { return; } }