vkd3d-shader: Add predefined macros to the macro list.

Signed-off-by: Zebediah Figura <zfigura@codeweavers.com>
Signed-off-by: Matteo Bruni <mbruni@codeweavers.com>
Signed-off-by: Henri Verbeet <hverbeet@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Zebediah Figura 2021-01-25 11:23:57 -06:00 committed by Alexandre Julliard
parent 160db8306f
commit 275f949a19
4 changed files with 34 additions and 13 deletions

View File

@ -130,6 +130,8 @@ struct preproc_ctx
bool error; bool error;
}; };
bool preproc_add_macro(struct preproc_ctx *ctx, const struct vkd3d_shader_location *loc, char *name, char **arg_names,
size_t arg_count, const struct vkd3d_shader_location *body_loc, struct vkd3d_string_buffer *body) DECLSPEC_HIDDEN;
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; 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;

View File

@ -717,8 +717,9 @@ int preproc_lexer_parse(const struct vkd3d_shader_compile_info *compile_info,
{ {
static const struct vkd3d_shader_preprocess_info default_preprocess_info = {0}; static const struct vkd3d_shader_preprocess_info default_preprocess_info = {0};
struct preproc_ctx ctx = {0}; struct preproc_ctx ctx = {0};
char *source_name; char *source_name = NULL;
void *output_code; void *output_code;
unsigned int i;
vkd3d_string_buffer_init(&ctx.buffer); vkd3d_string_buffer_init(&ctx.buffer);
rb_init(&ctx.macros, preproc_macro_compare); rb_init(&ctx.macros, preproc_macro_compare);
@ -727,18 +728,30 @@ int preproc_lexer_parse(const struct vkd3d_shader_compile_info *compile_info,
ctx.message_context = message_context; ctx.message_context = message_context;
if (!(source_name = vkd3d_strdup(compile_info->source_name ? compile_info->source_name : "<anonymous>"))) if (!(source_name = vkd3d_strdup(compile_info->source_name ? compile_info->source_name : "<anonymous>")))
goto fail;
for (i = 0; i < ctx.preprocess_info->macro_count; ++i)
{ {
vkd3d_string_buffer_cleanup(&ctx.buffer); const struct vkd3d_shader_location loc = {.source_name = source_name};
return VKD3D_ERROR_OUT_OF_MEMORY; struct vkd3d_string_buffer body;
char *name;
vkd3d_string_buffer_init(&body);
vkd3d_string_buffer_printf(&body, "%s", ctx.preprocess_info->macros[i].value);
if (!(name = vkd3d_strdup(ctx.preprocess_info->macros[i].name)))
goto fail;
if (!preproc_add_macro(&ctx, &loc, name, NULL, 0, &loc, &body))
{
vkd3d_free(name);
goto fail;
}
} }
yylex_init_extra(&ctx, &ctx.scanner); yylex_init_extra(&ctx, &ctx.scanner);
if (!preproc_push_include(&ctx, source_name, &compile_info->source)) if (!preproc_push_include(&ctx, source_name, &compile_info->source))
{ {
yylex_destroy(ctx.scanner); yylex_destroy(ctx.scanner);
vkd3d_free(source_name); goto fail;
vkd3d_string_buffer_cleanup(&ctx.buffer);
return VKD3D_ERROR_OUT_OF_MEMORY;
} }
preproc_yyparse(ctx.scanner, &ctx); preproc_yyparse(ctx.scanner, &ctx);
@ -789,4 +802,10 @@ int preproc_lexer_parse(const struct vkd3d_shader_compile_info *compile_info,
vkd3d_string_buffer_trace(&ctx.buffer); vkd3d_string_buffer_trace(&ctx.buffer);
vkd3d_string_buffer_cleanup(&ctx.buffer); vkd3d_string_buffer_cleanup(&ctx.buffer);
return VKD3D_OK; return VKD3D_OK;
fail:
rb_destroy(&ctx.macros, preproc_macro_rb_free, NULL);
vkd3d_free(source_name);
vkd3d_string_buffer_cleanup(&ctx.buffer);
return VKD3D_ERROR_OUT_OF_MEMORY;
} }

View File

@ -83,8 +83,8 @@ struct preproc_macro *preproc_find_macro(struct preproc_ctx *ctx, const char *na
return NULL; return NULL;
} }
static bool preproc_add_macro(struct preproc_ctx *ctx, const struct vkd3d_shader_location *loc, char *name, bool preproc_add_macro(struct preproc_ctx *ctx, const struct vkd3d_shader_location *loc, char *name, char **arg_names,
char **arg_names, size_t arg_count, const struct vkd3d_shader_location *body_loc, struct vkd3d_string_buffer *body) size_t arg_count, const struct vkd3d_shader_location *body_loc, struct vkd3d_string_buffer *body)
{ {
struct preproc_macro *macro; struct preproc_macro *macro;
unsigned int i; unsigned int i;

View File

@ -358,7 +358,7 @@ static void test_preprocess(void)
macros[0].Definition = "value"; macros[0].Definition = "value";
macros[1].Name = NULL; macros[1].Name = NULL;
macros[1].Definition = NULL; macros[1].Definition = NULL;
todo check_preprocess("KEY", macros, NULL, "value", "KEY"); check_preprocess("KEY", macros, NULL, "value", "KEY");
check_preprocess("#undef KEY\nKEY", macros, NULL, "KEY", "value"); check_preprocess("#undef KEY\nKEY", macros, NULL, "KEY", "value");
@ -367,7 +367,7 @@ static void test_preprocess(void)
macros[0].Name = "KEY"; macros[0].Name = "KEY";
macros[0].Definition = NULL; macros[0].Definition = NULL;
todo check_preprocess("KEY", macros, NULL, NULL, "KEY"); check_preprocess("KEY", macros, NULL, NULL, "KEY");
macros[0].Name = "0"; macros[0].Name = "0";
macros[0].Definition = "value"; macros[0].Definition = "value";
@ -383,19 +383,19 @@ static void test_preprocess(void)
macros[1].Definition = "value2"; macros[1].Definition = "value2";
macros[2].Name = NULL; macros[2].Name = NULL;
macros[2].Definition = NULL; macros[2].Definition = NULL;
todo check_preprocess("KEY", macros, NULL, "value2", NULL); check_preprocess("KEY", macros, NULL, "value2", NULL);
macros[0].Name = "KEY"; macros[0].Name = "KEY";
macros[0].Definition = "KEY2"; macros[0].Definition = "KEY2";
macros[1].Name = "KEY2"; macros[1].Name = "KEY2";
macros[1].Definition = "value"; macros[1].Definition = "value";
todo check_preprocess("KEY", macros, NULL, "value", NULL); check_preprocess("KEY", macros, NULL, "value", NULL);
macros[0].Name = "KEY2"; macros[0].Name = "KEY2";
macros[0].Definition = "value"; macros[0].Definition = "value";
macros[1].Name = "KEY"; macros[1].Name = "KEY";
macros[1].Definition = "KEY2"; macros[1].Definition = "KEY2";
todo check_preprocess("KEY", macros, NULL, "value", NULL); check_preprocess("KEY", macros, NULL, "value", NULL);
check_preprocess(test_include_top, NULL, &test_include, "pass", "fail"); 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);