Skip to content

Commit

Permalink
Write textures and samplers in descriptor sets
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 10, 2024
1 parent 66a3efe commit 0b638d3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
77 changes: 69 additions & 8 deletions Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
6 changes: 6 additions & 0 deletions Sources/integrations/kope.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion Sources/sets.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 0b638d3

Please sign in to comment.