vkd3d-shader: Introduce struct vkd3d_shader_source_list.

This commit is contained in:
Henri Verbeet
2025-08-05 21:17:28 +02:00
parent e4bb77ecef
commit 7c37fc6a8b
Notes: Henri Verbeet 2025-08-07 20:51:41 +02:00
Approved-by: Elizabeth Figura (@zfigura)
Approved-by: Henri Verbeet (@hverbeet)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/1671
5 changed files with 63 additions and 27 deletions

View File

@@ -4908,15 +4908,15 @@ static bool hlsl_ctx_init(struct hlsl_ctx *ctx, const struct vkd3d_shader_compil
ctx->message_context = message_context;
if (!(ctx->source_files = hlsl_alloc(ctx, sizeof(*ctx->source_files))))
return false;
if (!(ctx->source_files[0] = hlsl_strdup(ctx, compile_info->source_name ? compile_info->source_name : "<anonymous>")))
vkd3d_shader_source_list_init(&ctx->source_files);
if (!vkd3d_shader_source_list_append(&ctx->source_files,
compile_info->source_name ? compile_info->source_name : "<anonymous>"))
{
vkd3d_free(ctx->source_files);
vkd3d_shader_source_list_cleanup(&ctx->source_files);
return false;
}
ctx->source_files_count = 1;
ctx->location.source_name = ctx->source_files[0];
ctx->location.source_name = ctx->source_files.sources[0];
ctx->location.line = ctx->location.column = 1;
vkd3d_string_buffer_cache_init(&ctx->string_buffers);
@@ -4924,8 +4924,8 @@ static bool hlsl_ctx_init(struct hlsl_ctx *ctx, const struct vkd3d_shader_compil
if (!(ctx->dummy_scope = hlsl_new_scope(ctx, NULL)))
{
vkd3d_free((void *)ctx->source_files[0]);
vkd3d_free(ctx->source_files);
vkd3d_string_buffer_cache_cleanup(&ctx->string_buffers);
vkd3d_shader_source_list_cleanup(&ctx->source_files);
return false;
}
hlsl_push_scope(ctx);
@@ -5010,10 +5010,8 @@ static void hlsl_ctx_cleanup(struct hlsl_ctx *ctx)
struct hlsl_type *type, *next_type;
unsigned int i;
for (i = 0; i < ctx->source_files_count; ++i)
vkd3d_free((void *)ctx->source_files[i]);
vkd3d_free(ctx->source_files);
vkd3d_string_buffer_cache_cleanup(&ctx->string_buffers);
vkd3d_shader_source_list_cleanup(&ctx->source_files);
rb_destroy(&ctx->functions, free_function_rb, NULL);

View File

@@ -1093,8 +1093,7 @@ struct hlsl_ctx
{
const struct hlsl_profile_info *profile;
const char **source_files;
unsigned int source_files_count;
struct vkd3d_shader_source_list source_files;
/* Current location being read in the HLSL source, updated while parsing. */
struct vkd3d_shader_location location;
/* Stores the logging messages and logging configuration. */

View File

@@ -7190,23 +7190,19 @@ declaration_statement_list:
preproc_directive:
PRE_LINE STRING
{
const char **new_array = NULL;
ctx->location.line = $1;
if (strcmp($2, ctx->location.source_name))
new_array = hlsl_realloc(ctx, ctx->source_files,
sizeof(*ctx->source_files) * (ctx->source_files_count + 1));
if (new_array)
{
ctx->source_files = new_array;
ctx->source_files[ctx->source_files_count++] = $2;
ctx->location.source_name = $2;
}
else
{
vkd3d_free($2);
if (!vkd3d_shader_source_list_append(&ctx->source_files, $2))
{
ctx->result = VKD3D_ERROR_OUT_OF_MEMORY;
}
else
{
ctx->location.line = $1;
ctx->location.source_name = ctx->source_files.sources[ctx->source_files.count - 1];
}
}
vkd3d_free($2);
}
struct_declaration_without_vars:

View File

@@ -57,6 +57,39 @@ uint32_t vkd3d_parse_integer(const char *s)
return ret;
}
bool vkd3d_shader_source_list_append(struct vkd3d_shader_source_list *l, const char *source)
{
char *s;
if (!(s = vkd3d_strdup(source)))
return false;
if (!vkd3d_array_reserve((void **)&l->sources, &l->capacity, l->count + 1, sizeof(*l->sources)))
{
vkd3d_free(s);
return false;
}
l->sources[l->count++] = s;
return true;
}
void vkd3d_shader_source_list_cleanup(struct vkd3d_shader_source_list *l)
{
size_t i;
for (i = 0; i < l->count; ++i)
{
vkd3d_free((void *)l->sources[i]);
}
vkd3d_free(l->sources);
}
void vkd3d_shader_source_list_init(struct vkd3d_shader_source_list *l)
{
memset(l, 0, sizeof(*l));
}
void vkd3d_string_buffer_init(struct vkd3d_string_buffer *buffer)
{
buffer->buffer_size = 16;

View File

@@ -937,6 +937,16 @@ enum vkd3d_shader_type
struct vkd3d_shader_message_context;
struct vkd3d_shader_source_list
{
const char **sources;
size_t capacity, count;
};
bool vkd3d_shader_source_list_append(struct vkd3d_shader_source_list *l, const char *source);
void vkd3d_shader_source_list_cleanup(struct vkd3d_shader_source_list *l);
void vkd3d_shader_source_list_init(struct vkd3d_shader_source_list *l);
struct vkd3d_shader_version
{
enum vkd3d_shader_type type;