Skip to content

Commit

Permalink
Create bindless descriptor sets properly
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Oct 21, 2024
1 parent 92c753b commit c2c5178
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
44 changes: 22 additions & 22 deletions Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,28 @@ static void write_root_signature(char *hlsl, size_t *offset) {
*offset += sprintf(&hlsl[*offset], ")");
}

if (has_boundless) {
uint32_t boundless_space = 1;
for (size_t definition_index = 0; definition_index < set->definitions_count; ++definition_index) {
definition *def = &set->definitions[definition_index];

switch (def->kind) {
case DEFINITION_TEX2D:
case DEFINITION_TEX2DARRAY:
case DEFINITION_TEXCUBE: {
type *t = get_type(get_global(def->global)->type);

if (t->kind == TYPE_ARRAY && t->array.array_size == -1) {
*offset += sprintf(&hlsl[*offset], "\\\n, DescriptorTable(SRV(t0, space = %i, numDescriptors = unbounded))", boundless_space);
boundless_space += 1;
}

break;
}
}
}
}

if (has_sampler) {
*offset += sprintf(&hlsl[*offset], "\\\n, DescriptorTable(");

Expand All @@ -530,28 +552,6 @@ static void write_root_signature(char *hlsl, size_t *offset) {

*offset += sprintf(&hlsl[*offset], ")");
}

if (has_boundless) {
uint32_t boundless_space = 1;
for (size_t definition_index = 0; definition_index < set->definitions_count; ++definition_index) {
definition *def = &set->definitions[definition_index];

switch (def->kind) {
case DEFINITION_TEX2D:
case DEFINITION_TEX2DARRAY:
case DEFINITION_TEXCUBE: {
type *t = get_type(get_global(def->global)->type);

if (t->kind == TYPE_ARRAY && t->array.array_size == -1) {
*offset += sprintf(&hlsl[*offset], "\\\n, DescriptorTable(SRV(t0, space = %i, numDescriptors = unbounded))", boundless_space);
boundless_space += 1;
}

break;
}
}
}
}
}

*offset += sprintf(&hlsl[*offset], "\")]\n");
Expand Down
29 changes: 22 additions & 7 deletions Sources/integrations/kope.c
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@ void kope_export(char *directory, api_kind api) {
size_t other_count = 0;
size_t sampler_count = 0;
size_t dynamic_count = 0;
size_t bindless_count = 0;

for (size_t descriptor_index = 0; descriptor_index < set->definitions_count; ++descriptor_index) {
definition d = set->definitions[descriptor_index];
Expand All @@ -1032,17 +1033,24 @@ void kope_export(char *directory, api_kind api) {
other_count += 1;
}
break;
CASE_TEXTURE:
other_count += 1;
CASE_TEXTURE: {
type *t = get_type(get_global(d.global)->type);
if (t->kind == TYPE_ARRAY && t->array.array_size == -1) {
bindless_count += 1;
}
else {
other_count += 1;
}
break;
}
case DEFINITION_SAMPLER:
sampler_count += 1;
break;
}
}

fprintf(output, "\tkope_d3d12_device_create_descriptor_set(device, %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", &set->set);\n", other_count,
dynamic_count, sampler_count);
fprintf(output, "\tkope_d3d12_device_create_descriptor_set(device, %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", %" PRIu64 ", &set->set);\n", other_count,
dynamic_count, bindless_count, sampler_count);

size_t other_index = 0;
size_t sampler_index = 0;
Expand Down Expand Up @@ -1072,6 +1080,10 @@ void kope_export(char *directory, api_kind api) {
get_name(get_global(d.global)->name));
fprintf(output, "\tassert(set->%s != NULL);\n", get_name(get_global(d.global)->name));
fprintf(output, "\tfor (size_t index = 0; index < parameters->textures_count; ++index) {\n");
fprintf(output,
"\t\tkope_d3d12_descriptor_set_set_texture_view_srv(device, set->set.bindless_descriptor_allocation.offset + (uint32_t)index, "
"&parameters->%s[index]);\n",
get_name(get_global(d.global)->name));
fprintf(output, "\t\tset->%s[index] = parameters->%s[index];\n", get_name(get_global(d.global)->name),
get_name(get_global(d.global)->name));
fprintf(output, "\t}\n");
Expand All @@ -1086,14 +1098,17 @@ void kope_export(char *directory, api_kind api) {
get_name(get_global(d.global)->name), other_index);
}
else {
fprintf(output, "\tkope_d3d12_descriptor_set_set_texture_view_srv(device, &set->set, &parameters->%s, %" PRIu64 ");\n",
get_name(get_global(d.global)->name), other_index);
fprintf(output,
"\tkope_d3d12_descriptor_set_set_texture_view_srv(device, set->set.descriptor_allocation.offset + %" PRIu64
", &parameters->%s);\n",
other_index, get_name(get_global(d.global)->name));
}

fprintf(output, "\tset->%s = parameters->%s;\n", get_name(get_global(d.global)->name), get_name(get_global(d.global)->name));

other_index += 1;
}

other_index += 1;
break;
}
case DEFINITION_TEX2DARRAY: {
Expand Down

0 comments on commit c2c5178

Please sign in to comment.