From 30192efa101b8a50aa63be04323c43764f6e3a01 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Mon, 14 Oct 2024 17:12:48 +0200 Subject: [PATCH] Start supporting array types --- Sources/backends/hlsl.c | 7 ++++++- Sources/parser.c | 30 ++++++++++++++++++++++++++++-- Sources/types.c | 1 + Sources/types.h | 10 +++++++++- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/Sources/backends/hlsl.c b/Sources/backends/hlsl.c index 8bdd8a2..5362139 100644 --- a/Sources/backends/hlsl.c +++ b/Sources/backends/hlsl.c @@ -246,7 +246,12 @@ static void write_globals(char *hlsl, size_t *offset, function *main, function * *offset += sprintf(&hlsl[*offset], "RWTexture2D _%" PRIu64 " : register(u%i);\n\n", g->var_index, register_index); } else { - *offset += sprintf(&hlsl[*offset], "Texture2D _%" PRIu64 " : register(t%i);\n\n", g->var_index, register_index); + if (get_type(g->type)->kind == TYPE_ARRAY && get_type(g->type)->array.array_size == -1) { + *offset += sprintf(&hlsl[*offset], "Texture2D _%" PRIu64 "[] : register(t%i, space1);\n\n", g->var_index, register_index); + } + else { + *offset += sprintf(&hlsl[*offset], "Texture2D _%" PRIu64 " : register(t%i);\n\n", g->var_index, register_index); + } } } else if (g->type == tex2darray_type_id) { diff --git a/Sources/parser.c b/Sources/parser.c index 2defd07..b0b1ced 100644 --- a/Sources/parser.c +++ b/Sources/parser.c @@ -1141,6 +1141,22 @@ static definition parse_const(state_t *state, attribute_list attributes) { advance_state(state); } + bool array = false; + int array_size = -1; + + if (current(state).kind == TOKEN_LEFT_SQUARE) { + array = true; + advance_state(state); + + if (current(state).kind == TOKEN_INT) { + array_size = (int)current(state).number; + advance_state(state); + } + + match_token(state, TOKEN_RIGHT_SQUARE, "Expected a right square bracket"); + advance_state(state); + } + expression *value = NULL; if (current(state).kind == TOKEN_OPERATOR && current(state).op == OPERATOR_ASSIGN) { advance_state(state); @@ -1160,7 +1176,17 @@ static definition parse_const(state_t *state, attribute_list attributes) { } else if (type_name == add_name("tex2d")) { d.kind = DEFINITION_TEX2D; - d.global = add_global(tex2d_type_id, attributes, name.identifier); + + type_id t_id = tex2d_type_id; + if (array) { + type_id array_type_id = add_type(get_type(t_id)->name); + get_type(array_type_id)->kind = TYPE_ARRAY; + get_type(array_type_id)->array.base = t_id; + get_type(array_type_id)->array.array_size = array_size; + t_id = array_type_id; + } + + d.global = add_global(t_id, attributes, name.identifier); } else if (type_name == add_name("tex2darray")) { d.kind = DEFINITION_TEX2DARRAY; @@ -1181,7 +1207,7 @@ static definition parse_const(state_t *state, attribute_list attributes) { else if (type_name == add_name("float")) { debug_context context = {0}; check(value != NULL, context, "const float requires an initialization value"); - check(value->kind == EXPRESSION_FLOAT || value->kind == EXPRESSION_INT, context, "const float3 requires a number"); + check(value->kind == EXPRESSION_FLOAT || value->kind == EXPRESSION_INT, context, "const float requires a number"); global_value float_value; float_value.kind = GLOBAL_VALUE_FLOAT; diff --git a/Sources/types.c b/Sources/types.c index 8978a45..4c66b22 100644 --- a/Sources/types.c +++ b/Sources/types.c @@ -721,6 +721,7 @@ type_id add_type(name_id name) { type_id s = next_type_index; ++next_type_index; + types[s].kind = TYPE_OBJECT; types[s].name = name; types[s].attributes.attributes_count = 0; types[s].members.size = 0; diff --git a/Sources/types.h b/Sources/types.h index afb45a3..7b3bc39 100644 --- a/Sources/types.h +++ b/Sources/types.h @@ -63,8 +63,16 @@ static attribute *find_attribute(attribute_list *attributes, name_id name) { typedef struct type { attribute_list attributes; name_id name; - members members; bool built_in; + + enum { TYPE_OBJECT, TYPE_ARRAY } kind; + union { + members members; + struct { + type_id base; + int array_size; + } array; + }; } type; void types_init(void);