mirror of
https://gitlab.winehq.org/wine/vkd3d.git
synced 2025-01-28 13:05:02 -08:00
vkd3d-shader: Implement macro body expansion.
Signed-off-by: Zebediah Figura <zfigura@codeweavers.com> Signed-off-by: Henri Verbeet <hverbeet@codeweavers.com> Signed-off-by: Matteo Bruni <mbruni@codeweavers.com> Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
parent
73a937edd0
commit
668820f069
@ -243,7 +243,6 @@ XFAIL_TESTS = \
|
|||||||
tests/hlsl-vector-indexing.shader_test \
|
tests/hlsl-vector-indexing.shader_test \
|
||||||
tests/hlsl-vector-indexing-uniform.shader_test \
|
tests/hlsl-vector-indexing-uniform.shader_test \
|
||||||
tests/math.shader_test \
|
tests/math.shader_test \
|
||||||
tests/preproc-if.shader_test \
|
|
||||||
tests/preproc-ifdef.shader_test \
|
tests/preproc-ifdef.shader_test \
|
||||||
tests/preproc-if-expr.shader_test \
|
tests/preproc-if-expr.shader_test \
|
||||||
tests/preproc-invalid.shader_test \
|
tests/preproc-invalid.shader_test \
|
||||||
|
@ -50,10 +50,24 @@ struct preproc_file
|
|||||||
size_t if_count, if_stack_size;
|
size_t if_count, if_stack_size;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct preproc_text
|
||||||
|
{
|
||||||
|
struct vkd3d_string_buffer text;
|
||||||
|
struct vkd3d_shader_location location;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct preproc_expansion
|
||||||
|
{
|
||||||
|
struct preproc_buffer buffer;
|
||||||
|
const struct preproc_text *text;
|
||||||
|
};
|
||||||
|
|
||||||
struct preproc_macro
|
struct preproc_macro
|
||||||
{
|
{
|
||||||
struct rb_entry entry;
|
struct rb_entry entry;
|
||||||
char *name;
|
char *name;
|
||||||
|
|
||||||
|
struct preproc_text body;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct preproc_ctx
|
struct preproc_ctx
|
||||||
@ -67,6 +81,9 @@ struct preproc_ctx
|
|||||||
struct preproc_file *file_stack;
|
struct preproc_file *file_stack;
|
||||||
size_t file_count, file_stack_size;
|
size_t file_count, file_stack_size;
|
||||||
|
|
||||||
|
struct preproc_expansion *expansion_stack;
|
||||||
|
size_t expansion_count, expansion_stack_size;
|
||||||
|
|
||||||
struct rb_tree macros;
|
struct rb_tree macros;
|
||||||
|
|
||||||
int current_directive;
|
int current_directive;
|
||||||
@ -78,6 +95,7 @@ struct preproc_ctx
|
|||||||
};
|
};
|
||||||
|
|
||||||
void preproc_close_include(struct preproc_ctx *ctx, const struct vkd3d_shader_code *code) DECLSPEC_HIDDEN;
|
void preproc_close_include(struct preproc_ctx *ctx, const struct vkd3d_shader_code *code) DECLSPEC_HIDDEN;
|
||||||
|
struct preproc_macro *preproc_find_macro(struct preproc_ctx *ctx, const char *name) DECLSPEC_HIDDEN;
|
||||||
void preproc_free_macro(struct preproc_macro *macro) DECLSPEC_HIDDEN;
|
void preproc_free_macro(struct preproc_macro *macro) DECLSPEC_HIDDEN;
|
||||||
bool preproc_push_include(struct preproc_ctx *ctx, char *filename, const struct vkd3d_shader_code *code) DECLSPEC_HIDDEN;
|
bool preproc_push_include(struct preproc_ctx *ctx, char *filename, const struct vkd3d_shader_code *code) DECLSPEC_HIDDEN;
|
||||||
void preproc_warning(struct preproc_ctx *ctx, const struct vkd3d_shader_location *loc,
|
void preproc_warning(struct preproc_ctx *ctx, const struct vkd3d_shader_location *loc,
|
||||||
|
@ -169,27 +169,41 @@ static void update_location(struct preproc_ctx *ctx)
|
|||||||
|
|
||||||
static void preproc_pop_buffer(struct preproc_ctx *ctx)
|
static void preproc_pop_buffer(struct preproc_ctx *ctx)
|
||||||
{
|
{
|
||||||
struct preproc_file *file = preproc_get_top_file(ctx);
|
if (ctx->expansion_count)
|
||||||
|
|
||||||
if (ctx->file_count > 1)
|
|
||||||
preproc_close_include(ctx, &file->code);
|
|
||||||
|
|
||||||
if (file->if_count)
|
|
||||||
{
|
{
|
||||||
const struct vkd3d_shader_location loc = {.source_name = file->filename};
|
struct preproc_expansion *exp = &ctx->expansion_stack[ctx->expansion_count - 1];
|
||||||
|
|
||||||
preproc_warning(ctx, &loc, VKD3D_SHADER_WARNING_PP_UNTERMINATED_IF, "Unterminated #if block.");
|
yy_delete_buffer(exp->buffer.lexer_buffer, ctx->scanner);
|
||||||
|
|
||||||
|
--ctx->expansion_count;
|
||||||
|
TRACE("Expansion stack size is now %zu.\n", ctx->expansion_count);
|
||||||
}
|
}
|
||||||
vkd3d_free(file->if_stack);
|
else
|
||||||
|
{
|
||||||
|
struct preproc_file *file = preproc_get_top_file(ctx);
|
||||||
|
|
||||||
vkd3d_free(file->filename);
|
if (ctx->file_count > 1)
|
||||||
|
preproc_close_include(ctx, &file->code);
|
||||||
|
|
||||||
yy_delete_buffer(file->buffer.lexer_buffer, ctx->scanner);
|
if (file->if_count)
|
||||||
|
{
|
||||||
|
const struct vkd3d_shader_location loc = {.source_name = file->filename};
|
||||||
|
|
||||||
--ctx->file_count;
|
preproc_warning(ctx, &loc, VKD3D_SHADER_WARNING_PP_UNTERMINATED_IF, "Unterminated #if block.");
|
||||||
TRACE("File stack size is now %zu.\n", ctx->file_count);
|
}
|
||||||
|
vkd3d_free(file->if_stack);
|
||||||
|
|
||||||
if (ctx->file_count)
|
vkd3d_free(file->filename);
|
||||||
|
|
||||||
|
yy_delete_buffer(file->buffer.lexer_buffer, ctx->scanner);
|
||||||
|
|
||||||
|
--ctx->file_count;
|
||||||
|
TRACE("File stack size is now %zu.\n", ctx->file_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ctx->expansion_count)
|
||||||
|
yy_switch_to_buffer(ctx->expansion_stack[ctx->expansion_count - 1].buffer.lexer_buffer, ctx->scanner);
|
||||||
|
else if (ctx->file_count)
|
||||||
yy_switch_to_buffer(ctx->file_stack[ctx->file_count - 1].buffer.lexer_buffer, ctx->scanner);
|
yy_switch_to_buffer(ctx->file_stack[ctx->file_count - 1].buffer.lexer_buffer, ctx->scanner);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,6 +232,21 @@ static int return_token(int token, YYSTYPE *lval, const char *text)
|
|||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool preproc_push_expansion(struct preproc_ctx *ctx, const struct preproc_text *text)
|
||||||
|
{
|
||||||
|
struct preproc_expansion *exp;
|
||||||
|
|
||||||
|
if (!vkd3d_array_reserve((void **)&ctx->expansion_stack, &ctx->expansion_stack_size,
|
||||||
|
ctx->expansion_count + 1, sizeof(*ctx->expansion_stack)))
|
||||||
|
return false;
|
||||||
|
exp = &ctx->expansion_stack[ctx->expansion_count++];
|
||||||
|
exp->text = text;
|
||||||
|
exp->buffer.lexer_buffer = yy_scan_bytes(text->text.buffer, text->text.content_size, ctx->scanner);
|
||||||
|
exp->buffer.location = text->location;
|
||||||
|
TRACE("Expansion stack size is now %zu.\n", ctx->expansion_count);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
int yylex(YYSTYPE *lval, YYLTYPE *lloc, yyscan_t scanner)
|
int yylex(YYSTYPE *lval, YYLTYPE *lloc, yyscan_t scanner)
|
||||||
{
|
{
|
||||||
struct preproc_ctx *ctx = yyget_extra(scanner);
|
struct preproc_ctx *ctx = yyget_extra(scanner);
|
||||||
@ -239,6 +268,10 @@ int yylex(YYSTYPE *lval, YYLTYPE *lloc, yyscan_t scanner)
|
|||||||
if (!(token = preproc_lexer_lex(lval, lloc, scanner)))
|
if (!(token = preproc_lexer_lex(lval, lloc, scanner)))
|
||||||
{
|
{
|
||||||
ctx->last_was_eof = true;
|
ctx->last_was_eof = true;
|
||||||
|
|
||||||
|
/* If we have reached the end of an included file, inject a newline. */
|
||||||
|
if (ctx->expansion_count)
|
||||||
|
continue;
|
||||||
token = T_NEWLINE;
|
token = T_NEWLINE;
|
||||||
text = "\n";
|
text = "\n";
|
||||||
}
|
}
|
||||||
@ -288,6 +321,29 @@ int yylex(YYSTYPE *lval, YYLTYPE *lloc, yyscan_t scanner)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (token == T_IDENTIFIER)
|
||||||
|
{
|
||||||
|
struct preproc_macro *macro;
|
||||||
|
|
||||||
|
switch (ctx->current_directive)
|
||||||
|
{
|
||||||
|
case T_DEFINE:
|
||||||
|
case T_IFDEF:
|
||||||
|
case T_IFNDEF:
|
||||||
|
case T_UNDEF:
|
||||||
|
/* Return identifiers verbatim. */
|
||||||
|
return return_token(token, lval, text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Otherwise, expand a macro if there is one. */
|
||||||
|
|
||||||
|
if ((macro = preproc_find_macro(ctx, text)))
|
||||||
|
{
|
||||||
|
preproc_push_expansion(ctx, ¯o->body);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (ctx->current_directive)
|
if (ctx->current_directive)
|
||||||
return return_token(token, lval, text);
|
return return_token(token, lval, text);
|
||||||
|
|
||||||
@ -365,6 +421,7 @@ int preproc_lexer_parse(const struct vkd3d_shader_compile_info *compile_info,
|
|||||||
|
|
||||||
rb_destroy(&ctx.macros, preproc_macro_rb_free, NULL);
|
rb_destroy(&ctx.macros, preproc_macro_rb_free, NULL);
|
||||||
vkd3d_free(ctx.file_stack);
|
vkd3d_free(ctx.file_stack);
|
||||||
|
vkd3d_free(ctx.expansion_stack);
|
||||||
|
|
||||||
if (ctx.error)
|
if (ctx.error)
|
||||||
{
|
{
|
||||||
|
@ -68,7 +68,7 @@ static void yyerror(const YYLTYPE *loc, void *scanner, struct preproc_ctx *ctx,
|
|||||||
preproc_error(ctx, loc, VKD3D_SHADER_ERROR_PP_INVALID_SYNTAX, "%s", string);
|
preproc_error(ctx, loc, VKD3D_SHADER_ERROR_PP_INVALID_SYNTAX, "%s", string);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct preproc_macro *preproc_find_macro(struct preproc_ctx *ctx, const char *name)
|
struct preproc_macro *preproc_find_macro(struct preproc_ctx *ctx, const char *name)
|
||||||
{
|
{
|
||||||
struct rb_entry *entry;
|
struct rb_entry *entry;
|
||||||
|
|
||||||
@ -77,7 +77,8 @@ static struct preproc_macro *preproc_find_macro(struct preproc_ctx *ctx, const c
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool preproc_add_macro(struct preproc_ctx *ctx, const struct vkd3d_shader_location *loc, char *name)
|
static bool preproc_add_macro(struct preproc_ctx *ctx, const struct vkd3d_shader_location *loc, char *name,
|
||||||
|
const struct vkd3d_shader_location *body_loc, struct vkd3d_string_buffer *body)
|
||||||
{
|
{
|
||||||
struct preproc_macro *macro;
|
struct preproc_macro *macro;
|
||||||
int ret;
|
int ret;
|
||||||
@ -94,6 +95,8 @@ static bool preproc_add_macro(struct preproc_ctx *ctx, const struct vkd3d_shader
|
|||||||
if (!(macro = vkd3d_malloc(sizeof(*macro))))
|
if (!(macro = vkd3d_malloc(sizeof(*macro))))
|
||||||
return false;
|
return false;
|
||||||
macro->name = name;
|
macro->name = name;
|
||||||
|
macro->body.text = *body;
|
||||||
|
macro->body.location = *body_loc;
|
||||||
ret = rb_put(&ctx->macros, name, ¯o->entry);
|
ret = rb_put(&ctx->macros, name, ¯o->entry);
|
||||||
assert(!ret);
|
assert(!ret);
|
||||||
return true;
|
return true;
|
||||||
@ -102,6 +105,7 @@ static bool preproc_add_macro(struct preproc_ctx *ctx, const struct vkd3d_shader
|
|||||||
void preproc_free_macro(struct preproc_macro *macro)
|
void preproc_free_macro(struct preproc_macro *macro)
|
||||||
{
|
{
|
||||||
vkd3d_free(macro->name);
|
vkd3d_free(macro->name);
|
||||||
|
vkd3d_string_buffer_cleanup(¯o->body.text);
|
||||||
vkd3d_free(macro);
|
vkd3d_free(macro);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -265,6 +269,7 @@ static const void *get_parent_data(struct preproc_ctx *ctx)
|
|||||||
{
|
{
|
||||||
char *string;
|
char *string;
|
||||||
uint32_t integer;
|
uint32_t integer;
|
||||||
|
struct vkd3d_string_buffer string_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
%token <string> T_IDENTIFIER
|
%token <string> T_IDENTIFIER
|
||||||
@ -286,6 +291,7 @@ static const void *get_parent_data(struct preproc_ctx *ctx)
|
|||||||
|
|
||||||
%type <integer> expr
|
%type <integer> expr
|
||||||
%type <string> body_token
|
%type <string> body_token
|
||||||
|
%type <string_buffer> body_text
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
@ -298,8 +304,16 @@ shader_text
|
|||||||
|
|
||||||
body_text
|
body_text
|
||||||
: %empty
|
: %empty
|
||||||
|
{
|
||||||
|
vkd3d_string_buffer_init(&$$);
|
||||||
|
}
|
||||||
| body_text body_token
|
| body_text body_token
|
||||||
{
|
{
|
||||||
|
if (vkd3d_string_buffer_printf(&$$, "%s ", $2) < 0)
|
||||||
|
{
|
||||||
|
vkd3d_free($2);
|
||||||
|
YYABORT;
|
||||||
|
}
|
||||||
vkd3d_free($2);
|
vkd3d_free($2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,8 +325,12 @@ body_token
|
|||||||
directive
|
directive
|
||||||
: T_DEFINE T_IDENTIFIER body_text T_NEWLINE
|
: T_DEFINE T_IDENTIFIER body_text T_NEWLINE
|
||||||
{
|
{
|
||||||
if (!preproc_add_macro(ctx, &@$, $2))
|
if (!preproc_add_macro(ctx, &@$, $2, &@3, &$3))
|
||||||
|
{
|
||||||
|
vkd3d_free($2);
|
||||||
|
vkd3d_string_buffer_cleanup(&$3);
|
||||||
YYABORT;
|
YYABORT;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
| T_UNDEF T_IDENTIFIER T_NEWLINE
|
| T_UNDEF T_IDENTIFIER T_NEWLINE
|
||||||
{
|
{
|
||||||
|
@ -349,10 +349,10 @@ static void test_preprocess(void)
|
|||||||
|
|
||||||
for (i = 0; i < ARRAY_SIZE(tests); ++i)
|
for (i = 0; i < ARRAY_SIZE(tests); ++i)
|
||||||
{
|
{
|
||||||
if (i == 6 || i == 10 || i == 11)
|
if (i == 10 || i == 11)
|
||||||
continue;
|
continue;
|
||||||
vkd3d_test_set_context("Source \"%s\"", tests[i].source);
|
vkd3d_test_set_context("Source \"%s\"", tests[i].source);
|
||||||
todo_if (i <= 4 || (i >= 9 && i <= 14))
|
todo_if (i <= 4 || (i >= 9 && i <= 14) || i == 43)
|
||||||
check_preprocess(tests[i].source, NULL, NULL, tests[i].present, tests[i].absent);
|
check_preprocess(tests[i].source, NULL, NULL, tests[i].present, tests[i].absent);
|
||||||
}
|
}
|
||||||
vkd3d_test_set_context(NULL);
|
vkd3d_test_set_context(NULL);
|
||||||
@ -400,12 +400,11 @@ static void test_preprocess(void)
|
|||||||
macros[1].Definition = "KEY2";
|
macros[1].Definition = "KEY2";
|
||||||
todo check_preprocess("KEY", macros, NULL, "value", NULL);
|
todo check_preprocess("KEY", macros, NULL, "value", NULL);
|
||||||
|
|
||||||
if (0)
|
check_preprocess(test_include_top, NULL, &test_include, "pass", "fail");
|
||||||
todo check_preprocess(test_include_top, NULL, &test_include, "pass", "fail");
|
|
||||||
ok(!refcount_file1, "Got %d references to file1.\n", refcount_file1);
|
ok(!refcount_file1, "Got %d references to file1.\n", refcount_file1);
|
||||||
ok(!refcount_file2, "Got %d references to file1.\n", refcount_file2);
|
ok(!refcount_file2, "Got %d references to file1.\n", refcount_file2);
|
||||||
ok(!refcount_file3, "Got %d references to file1.\n", refcount_file3);
|
ok(!refcount_file3, "Got %d references to file1.\n", refcount_file3);
|
||||||
todo ok(include_count_file2 == 2, "file2 was included %u times.\n", include_count_file2);
|
ok(include_count_file2 == 2, "file2 was included %u times.\n", include_count_file2);
|
||||||
|
|
||||||
/* Macro invocation spread across multiple files. */
|
/* Macro invocation spread across multiple files. */
|
||||||
if (0)
|
if (0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user