Skip to content

Commit

Permalink
Support root constants
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 19, 2024
1 parent 44d3516 commit ed739ac
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 34 deletions.
28 changes: 28 additions & 0 deletions Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,34 @@ static void write_root_signature(char *hlsl, size_t *offset) {
for (size_t set_count = 0; set_count < all_descriptor_sets_count; ++set_count) {
descriptor_set *set = all_descriptor_sets[set_count];

if (set->name == add_name("root_constants")) {
if (set->definitions_count != 1) {
debug_context context = {0};
error(context, "More than one root constants struct found");
}

uint32_t size = 0;
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:
size += struct_size(get_global(def->global)->type);
break;
default: {
debug_context context = {0};
error(context, "Unsupported type for a root constant");
break;
}
}
}

*offset += sprintf(&hlsl[*offset], "\\\n, RootConstants(num32BitConstants=%i, b%i)", size / 4, cbv_index);
cbv_index += 1;

continue;
}

bool has_sampler = false;
bool has_other = false;

Expand Down
60 changes: 60 additions & 0 deletions Sources/backends/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,63 @@ void find_referenced_types(function *f, type_id *types, size_t *types_size) {
}
}
}

uint32_t base_type_size(type_id type) {
if (type == float_id) {
return 4;
}
if (type == float2_id) {
return 4 * 2;
}
if (type == float3_id) {
return 4 * 3;
}
if (type == float4_id) {
return 4 * 4;
}
if (type == float4x4_id) {
return 4 * 4 * 4;
}
if (type == float3x3_id) {
return 3 * 4 * 4;
}

if (type == uint_id) {
return 4;
}
if (type == uint2_id) {
return 4 * 2;
}
if (type == uint3_id) {
return 4 * 3;
}
if (type == uint4_id) {
return 4 * 4;
}

if (type == int_id) {
return 4;
}
if (type == int2_id) {
return 4 * 2;
}
if (type == int3_id) {
return 4 * 3;
}
if (type == int4_id) {
return 4 * 4;
}

debug_context context = {0};
error(context, "Unknown type %s for structure", get_name(get_type(type)->name));
return 1;
}

uint32_t struct_size(type_id id) {
uint32_t size = 0;
type *t = get_type(id);
for (size_t member_index = 0; member_index < t->members.size; ++member_index) {
size += base_type_size(t->members.m[member_index].type.type);
}
return size;
}
4 changes: 4 additions & 0 deletions Sources/backends/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ 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 indent(char *code, size_t *offset, int indentation);

uint32_t base_type_size(type_id type);

uint32_t struct_size(type_id id);
34 changes: 0 additions & 34 deletions Sources/integrations/kope.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,40 +54,6 @@ static const char *structure_type(type_id type) {
return "UNKNOWN";
}

static uint32_t base_type_size(type_id type) {
if (type == float_id) {
return 4;
}
if (type == float2_id) {
return 4 * 2;
}
if (type == float3_id) {
return 4 * 3;
}
if (type == float4_id) {
return 4 * 4;
}
if (type == float4x4_id) {
return 4 * 4 * 4;
}
if (type == float3x3_id) {
return 3 * 4 * 4;
}

debug_context context = {0};
error(context, "Unknown type %s for structure", get_name(get_type(type)->name));
return 1;
}

static uint32_t struct_size(type_id id) {
uint32_t size = 0;
type *t = get_type(id);
for (size_t member_index = 0; member_index < t->members.size; ++member_index) {
size += base_type_size(t->members.m[member_index].type.type);
}
return size;
}

static const char *convert_compare_mode(int mode) {
switch (mode) {
case 0:
Expand Down
9 changes: 9 additions & 0 deletions Sources/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ static definition parse_definition(state_t *state) {
match_token(state, TOKEN_IDENTIFIER, "Expected an identifier");
current_attribute.name = current(state).identifier;

if (current_attribute.name == add_name("root_constants")) {
current_set = add_set(current_attribute.name);
current_attribute.parameters[current_attribute.paramters_count] = current_set->index;
}

advance_state(state);

if (current(state).kind == TOKEN_LEFT_PAREN) {
Expand All @@ -188,6 +193,10 @@ static definition parse_definition(state_t *state) {
while (current(state).kind != TOKEN_RIGHT_PAREN) {
if (current(state).kind == TOKEN_IDENTIFIER) {
if (current_attribute.name == add_name("set")) {
if (current(state).identifier == add_name("root_constants")) {
debug_context context = {0};
error(context, "Descriptor set can not be called root_constants");
}
current_set = add_set(current(state).identifier);
current_attribute.parameters[current_attribute.paramters_count] = current_set->index;
}
Expand Down

0 comments on commit ed739ac

Please sign in to comment.