Skip to content

Commit

Permalink
Find referenced descriptor sets
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Mar 6, 2025
1 parent e5ae3e0 commit ee618b2
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 4 deletions.
5 changes: 5 additions & 0 deletions sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,11 @@ void hlsl_export(char *directory, api_kind d3d) {
for (function_id i = 0; get_function(i) != NULL; ++i) {
function *f = get_function(i);
if (has_attribute(&f->attributes, add_name("compute"))) {
descriptor_set *all_sets[256];
size_t all_sets_size = 0;

find_referenced_sets(f, all_sets, &all_sets_size);

global_id all_globals[256];
size_t all_globals_size = 0;

Expand Down
67 changes: 67 additions & 0 deletions sources/backends/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,73 @@ void find_referenced_globals(function *f, global_id *globals, size_t *globals_si
}
}

static bool has_set(descriptor_set **sets, size_t *sets_size, descriptor_set *set) {
for (size_t set_index = 0; set_index < *sets_size; ++set_index) {
if (sets[set_index] == set) {
return true;
}
}

return false;
}

static void add_set(descriptor_set **sets, size_t *sets_size, descriptor_set *set) {
if (has_set(sets, sets_size, set)) {
return;
}

sets[*sets_size] = set;
*sets_size += 1;
}

void find_referenced_sets(function *f, descriptor_set **sets, size_t *sets_size) {
if (f->block == NULL) {
// built-in
return;
}

global_id globals[256];
size_t globals_size = 0;
find_referenced_globals(f, globals, &globals_size);

for (size_t global_index = 0; global_index < globals_size; ++global_index) {
global *g = get_global(globals[global_index]);

if (g->sets_count == 0) {
continue;
}

if (g->sets_count == 1) {
add_set(sets, sets_size, g->sets[0]);
continue;
}
}

for (size_t global_index = 0; global_index < globals_size; ++global_index) {
global *g = get_global(globals[global_index]);

if (g->sets_count < 2) {
continue;
}

bool found = false;

for (size_t set_index = 0; set_index < g->sets_count; ++set_index) {
descriptor_set *set = g->sets[set_index];

if (has_set(sets, sets_size, set)) {
found = true;
break;
}
}

if (!found) {
debug_context context = {0};
error(context, "Global %s could be used from multiple descriptor sets.", get_name(g->name));
}
}
}

void find_referenced_functions(function *f, function **functions, size_t *functions_size) {
if (f->block == NULL) {
// built-in
Expand Down
2 changes: 2 additions & 0 deletions sources/backends/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

#include "../functions.h"
#include "../globals.h"
#include "../sets.h"

#include <stdint.h>

void find_referenced_functions(function *f, function **functions, size_t *functions_size);
void find_referenced_types(function *f, type_id *types, size_t *types_size);
void find_referenced_globals(function *f, global_id *globals, size_t *globals_size);
void find_referenced_sets(function *f, descriptor_set **sets, size_t *sets_size);

void indent(char *code, size_t *offset, int indentation);

Expand Down
4 changes: 2 additions & 2 deletions sources/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ static definition parse_definition(state_t *state) {
current_attribute.name = current(state).identifier;

if (current_attribute.name == add_name("root_constants")) {
current_sets[current_sets_count] = add_set(current_attribute.name);
current_sets[current_sets_count] = create_set(current_attribute.name);
current_attribute.parameters[current_attribute.paramters_count] = current_sets[current_sets_count]->index;
current_sets_count += 1;
}
Expand All @@ -199,7 +199,7 @@ static definition parse_definition(state_t *state) {
debug_context context = {0};
error(context, "Descriptor set can not be called root_constants");
}
current_sets[current_sets_count] = add_set(current(state).identifier);
current_sets[current_sets_count] = create_set(current(state).identifier);
current_attribute.parameters[current_attribute.paramters_count] = current_sets[current_sets_count]->index;
current_sets_count += 1;
}
Expand Down
2 changes: 1 addition & 1 deletion sources/sets.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
static descriptor_set sets[MAX_SETS];
static size_t sets_count = 0;

descriptor_set *add_set(name_id name) {
descriptor_set *create_set(name_id name) {
for (size_t set_index = 0; set_index < sets_count; ++set_index) {
if (sets[set_index].name == name) {
return &sets[set_index];
Expand Down
2 changes: 1 addition & 1 deletion sources/sets.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ typedef struct descriptor_set {

#define MAX_SETS 256

descriptor_set *add_set(name_id name);
descriptor_set *create_set(name_id name);

void add_definition_to_set(descriptor_set *set, definition def);

Expand Down

0 comments on commit ee618b2

Please sign in to comment.