So that messages from scanning are not lost.
Signed-off-by: Zebediah Figura zfigura@codeweavers.com --- v3: rebase on top of master...
libs/vkd3d-shader/vkd3d_shader_main.c | 98 +++++++++++++++------------ 1 file changed, 55 insertions(+), 43 deletions(-)
diff --git a/libs/vkd3d-shader/vkd3d_shader_main.c b/libs/vkd3d-shader/vkd3d_shader_main.c index 0839caed..1ca04b79 100644 --- a/libs/vkd3d-shader/vkd3d_shader_main.c +++ b/libs/vkd3d-shader/vkd3d_shader_main.c @@ -23,6 +23,9 @@
VKD3D_DEBUG_ENV_NAME("VKD3D_SHADER_DEBUG");
+static int scan_dxbc(const struct vkd3d_shader_compile_info *compile_info, + struct vkd3d_shader_message_context *message_context); + static void vkd3d_string_buffer_clear(struct vkd3d_string_buffer *buffer) { buffer->buffer[0] = '\0'; @@ -318,21 +321,23 @@ int vkd3d_shader_compile(const struct vkd3d_shader_compile_info *compile_info, if ((ret = vkd3d_shader_validate_compile_info(compile_info)) < 0) return ret;
+ if (!vkd3d_shader_message_context_init(&message_context, compile_info->log_level, compile_info->source_name)) + return VKD3D_ERROR; + scan_info = *compile_info; scan_descriptor_info.type = VKD3D_SHADER_STRUCTURE_TYPE_SCAN_DESCRIPTOR_INFO; scan_descriptor_info.next = scan_info.next; scan_info.next = &scan_descriptor_info;
- if ((ret = vkd3d_shader_scan(&scan_info, messages)) < 0) - return ret; - if (messages) + if ((ret = scan_dxbc(&scan_info, &message_context)) < 0) { - vkd3d_shader_free_messages(*messages); - *messages = NULL; + vkd3d_shader_message_context_trace_messages(&message_context); + if (messages && !(*messages = vkd3d_shader_message_context_copy_messages(&message_context))) + ret = VKD3D_ERROR_OUT_OF_MEMORY; + vkd3d_shader_message_context_cleanup(&message_context); + return ret; }
- if (!vkd3d_shader_message_context_init(&message_context, compile_info->log_level, compile_info->source_name)) - return VKD3D_ERROR; if ((ret = vkd3d_shader_parser_init(&parser, &compile_info->source, &message_context)) < 0) goto done;
@@ -391,7 +396,7 @@ struct vkd3d_shader_scan_context struct vkd3d_shader_scan_descriptor_info *scan_descriptor_info; size_t descriptors_size;
- struct vkd3d_shader_message_context message_context; + struct vkd3d_shader_message_context *message_context;
struct vkd3d_shader_cf_info { @@ -416,20 +421,19 @@ struct vkd3d_shader_scan_context size_t uav_range_count; };
-static bool vkd3d_shader_scan_context_init(struct vkd3d_shader_scan_context *context, +static void vkd3d_shader_scan_context_init(struct vkd3d_shader_scan_context *context, struct vkd3d_shader_scan_descriptor_info *scan_descriptor_info, - enum vkd3d_shader_log_level log_level, const char *source_name) + struct vkd3d_shader_message_context *message_context) { memset(context, 0, sizeof(*context)); context->scan_descriptor_info = scan_descriptor_info; - return vkd3d_shader_message_context_init(&context->message_context, log_level, source_name); + context->message_context = message_context; }
static void vkd3d_shader_scan_context_cleanup(struct vkd3d_shader_scan_context *context) { vkd3d_free(context->uav_ranges); vkd3d_free(context->cf_info); - vkd3d_shader_message_context_cleanup(&context->message_context); }
static struct vkd3d_shader_cf_info *vkd3d_shader_scan_get_current_cf_info(struct vkd3d_shader_scan_context *context) @@ -713,7 +717,7 @@ static int vkd3d_shader_scan_instruction(struct vkd3d_shader_scan_context *conte case VKD3DSIH_ELSE: if (!(cf_info = vkd3d_shader_scan_get_current_cf_info(context)) || cf_info->type != VKD3D_SHADER_BLOCK_IF) { - vkd3d_shader_error(&context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, + vkd3d_shader_error(context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, "Encountered ‘else’ instruction without corresponding ‘if’ block."); return VKD3D_ERROR_INVALID_SHADER; } @@ -722,7 +726,7 @@ static int vkd3d_shader_scan_instruction(struct vkd3d_shader_scan_context *conte case VKD3DSIH_ENDIF: if (!(cf_info = vkd3d_shader_scan_get_current_cf_info(context)) || cf_info->type != VKD3D_SHADER_BLOCK_IF) { - vkd3d_shader_error(&context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, + vkd3d_shader_error(context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, "Encountered ‘endif’ instruction without corresponding ‘if’ block."); return VKD3D_ERROR_INVALID_SHADER; } @@ -735,7 +739,7 @@ static int vkd3d_shader_scan_instruction(struct vkd3d_shader_scan_context *conte case VKD3DSIH_ENDLOOP: if (!(cf_info = vkd3d_shader_scan_get_current_cf_info(context)) || cf_info->type != VKD3D_SHADER_BLOCK_LOOP) { - vkd3d_shader_error(&context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, + vkd3d_shader_error(context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, "Encountered ‘endloop’ instruction without corresponding ‘loop’ block."); return VKD3D_ERROR_INVALID_SHADER; } @@ -749,7 +753,7 @@ static int vkd3d_shader_scan_instruction(struct vkd3d_shader_scan_context *conte if (!(cf_info = vkd3d_shader_scan_get_current_cf_info(context)) || cf_info->type != VKD3D_SHADER_BLOCK_SWITCH || cf_info->inside_block) { - vkd3d_shader_error(&context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, + vkd3d_shader_error(context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, "Encountered ‘endswitch’ instruction without corresponding ‘switch’ block."); return VKD3D_ERROR_INVALID_SHADER; } @@ -759,7 +763,7 @@ static int vkd3d_shader_scan_instruction(struct vkd3d_shader_scan_context *conte if (!(cf_info = vkd3d_shader_scan_get_current_cf_info(context)) || cf_info->type != VKD3D_SHADER_BLOCK_SWITCH) { - vkd3d_shader_error(&context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, + vkd3d_shader_error(context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, "Encountered ‘case’ instruction outside switch block."); return VKD3D_ERROR_INVALID_SHADER; } @@ -769,13 +773,13 @@ static int vkd3d_shader_scan_instruction(struct vkd3d_shader_scan_context *conte if (!(cf_info = vkd3d_shader_scan_get_current_cf_info(context)) || cf_info->type != VKD3D_SHADER_BLOCK_SWITCH) { - vkd3d_shader_error(&context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, + vkd3d_shader_error(context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, "Encountered ‘default’ instruction outside switch block."); return VKD3D_ERROR_INVALID_SHADER; } if (cf_info->has_default) { - vkd3d_shader_error(&context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, + vkd3d_shader_error(context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, "Encountered duplicate ‘default’ instruction inside the current switch block."); return VKD3D_ERROR_INVALID_SHADER; } @@ -785,7 +789,7 @@ static int vkd3d_shader_scan_instruction(struct vkd3d_shader_scan_context *conte case VKD3DSIH_BREAK: if (!(cf_info = vkd3d_shader_scan_find_innermost_breakable_cf_info(context))) { - vkd3d_shader_error(&context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, + vkd3d_shader_error(context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, "Encountered ‘break’ instruction outside breakable block."); return VKD3D_ERROR_INVALID_SHADER; } @@ -794,7 +798,7 @@ static int vkd3d_shader_scan_instruction(struct vkd3d_shader_scan_context *conte case VKD3DSIH_BREAKP: if (!(cf_info = vkd3d_shader_scan_find_innermost_loop_cf_info(context))) { - vkd3d_shader_error(&context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, + vkd3d_shader_error(context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, "Encountered ‘breakp’ instruction outside loop."); return VKD3D_ERROR_INVALID_SHADER; } @@ -802,7 +806,7 @@ static int vkd3d_shader_scan_instruction(struct vkd3d_shader_scan_context *conte case VKD3DSIH_CONTINUE: if (!(cf_info = vkd3d_shader_scan_find_innermost_loop_cf_info(context))) { - vkd3d_shader_error(&context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, + vkd3d_shader_error(context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, "Encountered ‘continue’ instruction outside loop."); return VKD3D_ERROR_INVALID_SHADER; } @@ -811,7 +815,7 @@ static int vkd3d_shader_scan_instruction(struct vkd3d_shader_scan_context *conte case VKD3DSIH_CONTINUEP: if (!(cf_info = vkd3d_shader_scan_find_innermost_loop_cf_info(context))) { - vkd3d_shader_error(&context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, + vkd3d_shader_error(context->message_context, VKD3D_SHADER_ERROR_TPF_MISMATCHED_CF, "Encountered ‘continue’ instruction outside loop."); return VKD3D_ERROR_INVALID_SHADER; } @@ -844,39 +848,25 @@ static int vkd3d_shader_scan_instruction(struct vkd3d_shader_scan_context *conte return VKD3D_OK; }
-int vkd3d_shader_scan(const struct vkd3d_shader_compile_info *compile_info, char **messages) +static int scan_dxbc(const struct vkd3d_shader_compile_info *compile_info, + struct vkd3d_shader_message_context *message_context) { struct vkd3d_shader_scan_descriptor_info *scan_descriptor_info; - struct vkd3d_shader_message_context *message_context; struct vkd3d_shader_instruction instruction; struct vkd3d_shader_scan_context context; struct vkd3d_shader_parser parser; int ret;
- TRACE("compile_info %p, messages %p.\n", compile_info, messages); - - if (messages) - *messages = NULL; - - if ((ret = vkd3d_shader_validate_compile_info(compile_info)) < 0) - return ret; - if ((scan_descriptor_info = vkd3d_find_struct(compile_info->next, SCAN_DESCRIPTOR_INFO))) { scan_descriptor_info->descriptors = NULL; scan_descriptor_info->descriptor_count = 0; }
- if (!vkd3d_shader_scan_context_init(&context, scan_descriptor_info, - compile_info->log_level, compile_info->source_name)) - return VKD3D_ERROR; - message_context = &context.message_context; + vkd3d_shader_scan_context_init(&context, scan_descriptor_info, message_context);
if ((ret = vkd3d_shader_parser_init(&parser, &compile_info->source, message_context)) < 0) { - vkd3d_shader_message_context_trace_messages(message_context); - if (messages && !(*messages = vkd3d_shader_message_context_copy_messages(message_context))) - ret = VKD3D_ERROR_OUT_OF_MEMORY; vkd3d_shader_scan_context_cleanup(&context); return ret; } @@ -911,14 +901,36 @@ int vkd3d_shader_scan(const struct vkd3d_shader_compile_info *compile_info, char ret = VKD3D_OK;
done: - vkd3d_shader_message_context_trace_messages(message_context); - if (messages && !(*messages = vkd3d_shader_message_context_copy_messages(message_context))) - ret = VKD3D_ERROR_OUT_OF_MEMORY; vkd3d_shader_scan_context_cleanup(&context); vkd3d_shader_parser_destroy(&parser); return ret; }
+int vkd3d_shader_scan(const struct vkd3d_shader_compile_info *compile_info, char **messages) +{ + struct vkd3d_shader_message_context message_context; + int ret; + + TRACE("compile_info %p, messages %p.\n", compile_info, messages); + + if (messages) + *messages = NULL; + + if ((ret = vkd3d_shader_validate_compile_info(compile_info)) < 0) + return ret; + + if (!vkd3d_shader_message_context_init(&message_context, compile_info->log_level, compile_info->source_name)) + return VKD3D_ERROR_OUT_OF_MEMORY; + + ret = scan_dxbc(compile_info, &message_context); + + vkd3d_shader_message_context_trace_messages(&message_context); + if (messages && !(*messages = vkd3d_shader_message_context_copy_messages(&message_context))) + ret = VKD3D_ERROR_OUT_OF_MEMORY; + vkd3d_shader_message_context_cleanup(&message_context); + return ret; +} + void vkd3d_shader_free_scan_descriptor_info(struct vkd3d_shader_scan_descriptor_info *scan_descriptor_info) { TRACE("scan_descriptor_info %p.\n", scan_descriptor_info);
Signed-off-by: Zebediah Figura zfigura@codeweavers.com --- libs/vkd3d-shader/vkd3d_shader_main.c | 181 +++++++++++++------------- 1 file changed, 89 insertions(+), 92 deletions(-)
diff --git a/libs/vkd3d-shader/vkd3d_shader_main.c b/libs/vkd3d-shader/vkd3d_shader_main.c index 1ca04b79..46d98542 100644 --- a/libs/vkd3d-shader/vkd3d_shader_main.c +++ b/libs/vkd3d-shader/vkd3d_shader_main.c @@ -23,9 +23,6 @@
VKD3D_DEBUG_ENV_NAME("VKD3D_SHADER_DEBUG");
-static int scan_dxbc(const struct vkd3d_shader_compile_info *compile_info, - struct vkd3d_shader_message_context *message_context); - static void vkd3d_string_buffer_clear(struct vkd3d_string_buffer *buffer) { buffer->buffer[0] = '\0'; @@ -302,95 +299,6 @@ void vkd3d_shader_free_messages(char *messages) vkd3d_free(messages); }
-int vkd3d_shader_compile(const struct vkd3d_shader_compile_info *compile_info, - struct vkd3d_shader_code *out, char **messages) -{ - struct vkd3d_shader_scan_descriptor_info scan_descriptor_info; - struct vkd3d_shader_message_context message_context; - struct vkd3d_shader_instruction instruction; - struct vkd3d_shader_compile_info scan_info; - struct vkd3d_dxbc_compiler *spirv_compiler; - struct vkd3d_shader_parser parser; - int ret; - - TRACE("compile_info %p, out %p, messages %p.\n", compile_info, out, messages); - - if (messages) - *messages = NULL; - - if ((ret = vkd3d_shader_validate_compile_info(compile_info)) < 0) - return ret; - - if (!vkd3d_shader_message_context_init(&message_context, compile_info->log_level, compile_info->source_name)) - return VKD3D_ERROR; - - scan_info = *compile_info; - scan_descriptor_info.type = VKD3D_SHADER_STRUCTURE_TYPE_SCAN_DESCRIPTOR_INFO; - scan_descriptor_info.next = scan_info.next; - scan_info.next = &scan_descriptor_info; - - if ((ret = scan_dxbc(&scan_info, &message_context)) < 0) - { - vkd3d_shader_message_context_trace_messages(&message_context); - if (messages && !(*messages = vkd3d_shader_message_context_copy_messages(&message_context))) - ret = VKD3D_ERROR_OUT_OF_MEMORY; - vkd3d_shader_message_context_cleanup(&message_context); - return ret; - } - - if ((ret = vkd3d_shader_parser_init(&parser, &compile_info->source, &message_context)) < 0) - goto done; - - vkd3d_shader_dump_shader(parser.shader_version.type, &compile_info->source); - - if (compile_info->target_type == VKD3D_SHADER_TARGET_D3D_ASM) - { - ret = vkd3d_dxbc_binary_to_text(parser.data, out); - vkd3d_shader_parser_destroy(&parser); - goto done; - } - - if (!(spirv_compiler = vkd3d_dxbc_compiler_create(&parser.shader_version, - &parser.shader_desc, compile_info, &scan_descriptor_info, &message_context))) - { - ERR("Failed to create DXBC compiler.\n"); - vkd3d_shader_parser_destroy(&parser); - ret = VKD3D_ERROR; - goto done; - } - - message_context.line = 2; /* Line 1 is the version token. */ - message_context.column = 1; - while (!shader_sm4_is_end(parser.data, &parser.ptr)) - { - shader_sm4_read_instruction(parser.data, &parser.ptr, &instruction); - - if (instruction.handler_idx == VKD3DSIH_INVALID) - { - WARN("Encountered unrecognized or invalid instruction.\n"); - ret = VKD3D_ERROR_INVALID_SHADER; - break; - } - - if ((ret = vkd3d_dxbc_compiler_handle_instruction(spirv_compiler, &instruction)) < 0) - break; - ++message_context.line; - } - - if (ret >= 0) - ret = vkd3d_dxbc_compiler_generate_spirv(spirv_compiler, compile_info, out); - - vkd3d_dxbc_compiler_destroy(spirv_compiler); - vkd3d_shader_parser_destroy(&parser); -done: - vkd3d_shader_message_context_trace_messages(&message_context); - if (messages && !(*messages = vkd3d_shader_message_context_copy_messages(&message_context))) - ret = VKD3D_ERROR_OUT_OF_MEMORY; - vkd3d_shader_message_context_cleanup(&message_context); - vkd3d_shader_free_scan_descriptor_info(&scan_descriptor_info); - return ret; -} - struct vkd3d_shader_scan_context { struct vkd3d_shader_scan_descriptor_info *scan_descriptor_info; @@ -931,6 +839,95 @@ int vkd3d_shader_scan(const struct vkd3d_shader_compile_info *compile_info, char return ret; }
+int vkd3d_shader_compile(const struct vkd3d_shader_compile_info *compile_info, + struct vkd3d_shader_code *out, char **messages) +{ + struct vkd3d_shader_scan_descriptor_info scan_descriptor_info; + struct vkd3d_shader_message_context message_context; + struct vkd3d_shader_instruction instruction; + struct vkd3d_shader_compile_info scan_info; + struct vkd3d_dxbc_compiler *spirv_compiler; + struct vkd3d_shader_parser parser; + int ret; + + TRACE("compile_info %p, out %p, messages %p.\n", compile_info, out, messages); + + if (messages) + *messages = NULL; + + if ((ret = vkd3d_shader_validate_compile_info(compile_info)) < 0) + return ret; + + if (!vkd3d_shader_message_context_init(&message_context, compile_info->log_level, compile_info->source_name)) + return VKD3D_ERROR; + + scan_info = *compile_info; + scan_descriptor_info.type = VKD3D_SHADER_STRUCTURE_TYPE_SCAN_DESCRIPTOR_INFO; + scan_descriptor_info.next = scan_info.next; + scan_info.next = &scan_descriptor_info; + + if ((ret = scan_dxbc(&scan_info, &message_context)) < 0) + { + vkd3d_shader_message_context_trace_messages(&message_context); + if (messages && !(*messages = vkd3d_shader_message_context_copy_messages(&message_context))) + ret = VKD3D_ERROR_OUT_OF_MEMORY; + vkd3d_shader_message_context_cleanup(&message_context); + return ret; + } + + if ((ret = vkd3d_shader_parser_init(&parser, &compile_info->source, &message_context)) < 0) + goto done; + + vkd3d_shader_dump_shader(parser.shader_version.type, &compile_info->source); + + if (compile_info->target_type == VKD3D_SHADER_TARGET_D3D_ASM) + { + ret = vkd3d_dxbc_binary_to_text(parser.data, out); + vkd3d_shader_parser_destroy(&parser); + goto done; + } + + if (!(spirv_compiler = vkd3d_dxbc_compiler_create(&parser.shader_version, + &parser.shader_desc, compile_info, &scan_descriptor_info, &message_context))) + { + ERR("Failed to create DXBC compiler.\n"); + vkd3d_shader_parser_destroy(&parser); + ret = VKD3D_ERROR; + goto done; + } + + message_context.line = 2; /* Line 1 is the version token. */ + message_context.column = 1; + while (!shader_sm4_is_end(parser.data, &parser.ptr)) + { + shader_sm4_read_instruction(parser.data, &parser.ptr, &instruction); + + if (instruction.handler_idx == VKD3DSIH_INVALID) + { + WARN("Encountered unrecognized or invalid instruction.\n"); + ret = VKD3D_ERROR_INVALID_SHADER; + break; + } + + if ((ret = vkd3d_dxbc_compiler_handle_instruction(spirv_compiler, &instruction)) < 0) + break; + ++message_context.line; + } + + if (ret >= 0) + ret = vkd3d_dxbc_compiler_generate_spirv(spirv_compiler, compile_info, out); + + vkd3d_dxbc_compiler_destroy(spirv_compiler); + vkd3d_shader_parser_destroy(&parser); +done: + vkd3d_shader_message_context_trace_messages(&message_context); + if (messages && !(*messages = vkd3d_shader_message_context_copy_messages(&message_context))) + ret = VKD3D_ERROR_OUT_OF_MEMORY; + vkd3d_shader_message_context_cleanup(&message_context); + vkd3d_shader_free_scan_descriptor_info(&scan_descriptor_info); + return ret; +} + void vkd3d_shader_free_scan_descriptor_info(struct vkd3d_shader_scan_descriptor_info *scan_descriptor_info) { TRACE("scan_descriptor_info %p.\n", scan_descriptor_info);
Signed-off-by: Henri Verbeet hverbeet@codeweavers.com
Signed-off-by: Zebediah Figura zfigura@codeweavers.com --- include/vkd3d_shader.h | 7 ++ libs/vkd3d-shader/vkd3d_shader_main.c | 97 ++++++++++++++++++--------- 2 files changed, 73 insertions(+), 31 deletions(-)
diff --git a/include/vkd3d_shader.h b/include/vkd3d_shader.h index f339ed70..d8420acc 100644 --- a/include/vkd3d_shader.h +++ b/include/vkd3d_shader.h @@ -461,6 +461,8 @@ enum vkd3d_shader_source_type * the format used for Direct3D shader model 4 and 5 shaders. */ VKD3D_SHADER_SOURCE_DXBC_TPF, + /** High-Level Shader Language source code. */ + VKD3D_SHADER_SOURCE_HLSL,
VKD3D_FORCE_32_BIT_ENUM(VKD3D_SHADER_SOURCE_TYPE), }; @@ -480,6 +482,11 @@ enum vkd3d_shader_target_type VKD3D_SHADER_TARGET_SPIRV_BINARY, VKD3D_SHADER_TARGET_SPIRV_TEXT, VKD3D_SHADER_TARGET_D3D_ASM, + /** + * A 'Tokenized Program Format' shader embedded in a DXBC container. This is + * the format used for Direct3D shader model 4 and 5 shaders. + */ + VKD3D_SHADER_TARGET_DXBC_TPF,
VKD3D_FORCE_32_BIT_ENUM(VKD3D_SHADER_TARGET_TYPE), }; diff --git a/libs/vkd3d-shader/vkd3d_shader_main.c b/libs/vkd3d-shader/vkd3d_shader_main.c index 46d98542..0c6a3c3f 100644 --- a/libs/vkd3d-shader/vkd3d_shader_main.c +++ b/libs/vkd3d-shader/vkd3d_shader_main.c @@ -839,65 +839,51 @@ int vkd3d_shader_scan(const struct vkd3d_shader_compile_info *compile_info, char return ret; }
-int vkd3d_shader_compile(const struct vkd3d_shader_compile_info *compile_info, - struct vkd3d_shader_code *out, char **messages) +static int compile_dxbc_tpf(const struct vkd3d_shader_compile_info *compile_info, + struct vkd3d_shader_code *out, struct vkd3d_shader_message_context *message_context) { struct vkd3d_shader_scan_descriptor_info scan_descriptor_info; - struct vkd3d_shader_message_context message_context; struct vkd3d_shader_instruction instruction; struct vkd3d_shader_compile_info scan_info; struct vkd3d_dxbc_compiler *spirv_compiler; struct vkd3d_shader_parser parser; int ret;
- TRACE("compile_info %p, out %p, messages %p.\n", compile_info, out, messages); - - if (messages) - *messages = NULL; - - if ((ret = vkd3d_shader_validate_compile_info(compile_info)) < 0) - return ret; - - if (!vkd3d_shader_message_context_init(&message_context, compile_info->log_level, compile_info->source_name)) - return VKD3D_ERROR; - scan_info = *compile_info; scan_descriptor_info.type = VKD3D_SHADER_STRUCTURE_TYPE_SCAN_DESCRIPTOR_INFO; scan_descriptor_info.next = scan_info.next; scan_info.next = &scan_descriptor_info;
- if ((ret = scan_dxbc(&scan_info, &message_context)) < 0) + if ((ret = scan_dxbc(&scan_info, message_context)) < 0) + return ret; + + if ((ret = vkd3d_shader_parser_init(&parser, &compile_info->source, message_context)) < 0) { - vkd3d_shader_message_context_trace_messages(&message_context); - if (messages && !(*messages = vkd3d_shader_message_context_copy_messages(&message_context))) - ret = VKD3D_ERROR_OUT_OF_MEMORY; - vkd3d_shader_message_context_cleanup(&message_context); + vkd3d_shader_free_scan_descriptor_info(&scan_descriptor_info); return ret; }
- if ((ret = vkd3d_shader_parser_init(&parser, &compile_info->source, &message_context)) < 0) - goto done; - vkd3d_shader_dump_shader(parser.shader_version.type, &compile_info->source);
if (compile_info->target_type == VKD3D_SHADER_TARGET_D3D_ASM) { + vkd3d_shader_free_scan_descriptor_info(&scan_descriptor_info); ret = vkd3d_dxbc_binary_to_text(parser.data, out); vkd3d_shader_parser_destroy(&parser); - goto done; + return ret; }
if (!(spirv_compiler = vkd3d_dxbc_compiler_create(&parser.shader_version, - &parser.shader_desc, compile_info, &scan_descriptor_info, &message_context))) + &parser.shader_desc, compile_info, &scan_descriptor_info, message_context))) { ERR("Failed to create DXBC compiler.\n"); vkd3d_shader_parser_destroy(&parser); - ret = VKD3D_ERROR; - goto done; + vkd3d_shader_free_scan_descriptor_info(&scan_descriptor_info); + return VKD3D_ERROR; }
- message_context.line = 2; /* Line 1 is the version token. */ - message_context.column = 1; + message_context->line = 2; /* Line 1 is the version token. */ + message_context->column = 1; while (!shader_sm4_is_end(parser.data, &parser.ptr)) { shader_sm4_read_instruction(parser.data, &parser.ptr, &instruction); @@ -911,7 +897,7 @@ int vkd3d_shader_compile(const struct vkd3d_shader_compile_info *compile_info,
if ((ret = vkd3d_dxbc_compiler_handle_instruction(spirv_compiler, &instruction)) < 0) break; - ++message_context.line; + ++message_context->line; }
if (ret >= 0) @@ -919,12 +905,51 @@ int vkd3d_shader_compile(const struct vkd3d_shader_compile_info *compile_info,
vkd3d_dxbc_compiler_destroy(spirv_compiler); vkd3d_shader_parser_destroy(&parser); -done: + vkd3d_shader_free_scan_descriptor_info(&scan_descriptor_info); + return ret; +} + +static int compile_hlsl(const struct vkd3d_shader_compile_info *compile_info, + struct vkd3d_shader_code *out, struct vkd3d_shader_message_context *message_context) +{ + return VKD3D_ERROR_NOT_IMPLEMENTED; +} + +int vkd3d_shader_compile(const struct vkd3d_shader_compile_info *compile_info, + struct vkd3d_shader_code *out, char **messages) +{ + struct vkd3d_shader_message_context message_context; + int ret; + + TRACE("compile_info %p, out %p, messages %p.\n", compile_info, out, messages); + + if (messages) + *messages = NULL; + + if ((ret = vkd3d_shader_validate_compile_info(compile_info)) < 0) + return ret; + + if (!vkd3d_shader_message_context_init(&message_context, compile_info->log_level, compile_info->source_name)) + return VKD3D_ERROR; + + switch (compile_info->source_type) + { + case VKD3D_SHADER_SOURCE_DXBC_TPF: + ret = compile_dxbc_tpf(compile_info, out, &message_context); + break; + + case VKD3D_SHADER_SOURCE_HLSL: + ret = compile_hlsl(compile_info, out, &message_context); + break; + + default: + assert(0); + } + vkd3d_shader_message_context_trace_messages(&message_context); if (messages && !(*messages = vkd3d_shader_message_context_copy_messages(&message_context))) ret = VKD3D_ERROR_OUT_OF_MEMORY; vkd3d_shader_message_context_cleanup(&message_context); - vkd3d_shader_free_scan_descriptor_info(&scan_descriptor_info); return ret; }
@@ -1073,6 +1098,7 @@ const enum vkd3d_shader_source_type *vkd3d_shader_get_supported_source_types(uns static const enum vkd3d_shader_source_type types[] = { VKD3D_SHADER_SOURCE_DXBC_TPF, + VKD3D_SHADER_SOURCE_HLSL, };
TRACE("count %p.\n", count); @@ -1093,6 +1119,11 @@ const enum vkd3d_shader_target_type *vkd3d_shader_get_supported_target_types( VKD3D_SHADER_TARGET_D3D_ASM, };
+ static const enum vkd3d_shader_target_type hlsl_types[] = + { + VKD3D_SHADER_TARGET_DXBC_TPF, + }; + TRACE("source_type %#x, count %p.\n", source_type, count);
switch (source_type) @@ -1101,6 +1132,10 @@ const enum vkd3d_shader_target_type *vkd3d_shader_get_supported_target_types( *count = ARRAY_SIZE(dxbc_tpf_types); return dxbc_tpf_types;
+ case VKD3D_SHADER_SOURCE_HLSL: + *count = ARRAY_SIZE(hlsl_types); + return hlsl_types; + default: *count = 0; return NULL;
Signed-off-by: Henri Verbeet hverbeet@codeweavers.com
Signed-off-by: Zebediah Figura zfigura@codeweavers.com --- include/vkd3d_shader.h | 157 ++++++++++++++++++++++++++ libs/vkd3d-shader/vkd3d_shader.map | 1 + libs/vkd3d-shader/vkd3d_shader_main.c | 8 ++ 3 files changed, 166 insertions(+)
diff --git a/include/vkd3d_shader.h b/include/vkd3d_shader.h index d8420acc..45b51a5d 100644 --- a/include/vkd3d_shader.h +++ b/include/vkd3d_shader.h @@ -54,6 +54,12 @@ enum vkd3d_shader_structure_type /** The structure is a vkd3d_shader_transform_feedback_info structure. */ VKD3D_SHADER_STRUCTURE_TYPE_TRANSFORM_FEEDBACK_INFO,
+ /** + * The structure is a vkd3d_shader_preprocess_info structure. + * \since 1.3 + */ + VKD3D_SHADER_STRUCTURE_TYPE_PREPROCESS_INFO, + VKD3D_FORCE_32_BIT_ENUM(VKD3D_SHADER_STRUCTURE_TYPE), };
@@ -662,6 +668,128 @@ struct vkd3d_shader_spirv_domain_shader_target_info enum vkd3d_shader_tessellator_partitioning partitioning; };
+/** + * A single preprocessor macro, passed as part of struct + * vkd3d_shader_preprocess_info. + */ +struct vkd3d_shader_macro +{ + /** + * A null-terminated string containing the name of a macro. This macro must + * not be a parameterized (i.e. function-like) macro. If this field is not a + * valid macro identifier, it will be ignored. + */ + const char *name; + /** A null-terminated string containing the expansion of the macro. */ + const char *value; +}; + +/** + * Type of a callback function which will be used to open preprocessor includes. + * + * This callback function is passed as part of struct + * vkd3d_shader_preprocess_info. + * + * If this function fails, vkd3d-shader will emit a compilation error, and the + * \a pfn_close_include callback will not be called. + * + * \param filename Unquoted string used as an argument to the #include + * directive. + * + * \param local Whether the #include directive is requesting a local (i.e. + * double-quoted) or system (i.e. angle-bracketed) include. + * + * \param parent_data Unprocessed source code of the file in which this + * #include directive is evaluated. This parameter may be NULL. + * + * \param context The user-defined pointer passed to struct + * vkd3d_shader_preprocess_info. + * + * \param out Output location for the full contents of the included file. The + * code need not be allocated using standard vkd3d functions, but must remain + * valid until the corresponding call to \a pfn_close_include. If this function + * fails, the contents of this parameter are ignored. + * + * \return A member of \ref vkd3d_result. + */ +typedef int (*PFN_vkd3d_shader_open_include)(const char *filename, bool local, + const char *parent_data, void *context, struct vkd3d_shader_code *out); +/** + * Type of a callback function which will be used to close preprocessor + * includes. + * + * This callback function is passed as part of struct + * vkd3d_shader_preprocess_info. + * + * \param code Contents of the included file, which were allocated by the + * \ref PFN_vkd3d_shader_open_include callback. The source code was allocated by + * the user and thus need not be freed by vkd3d_shader_free_shader_code(). + * + * \param context The user-defined pointer passed to struct + * vkd3d_shader_preprocess_info. + */ +typedef void (*PFN_vkd3d_shader_close_include)(const struct vkd3d_shader_code *code, void *context); + +/** + * A chained structure containing preprocessing parameters. + * + * This structure is optional. + * + * This structure extends vkd3d_shader_compile_info. + * + * This structure contains only input parameters. + * + * \since 1.3 + */ +struct vkd3d_shader_preprocess_info +{ + /** Must be set to VKD3D_SHADER_STRUCTURE_TYPE_PREPROCESS_INFO. */ + enum vkd3d_shader_structure_type type; + /** Optional pointer to a structure containing further parameters. */ + const void *next; + + /** + * Pointer to an array of predefined macros. Each macro in this array will + * be expanded as if a corresponding #define statement were prepended to the + * source code. + * + * If the same macro is specified multiple times, only the first value is + * used. + */ + const struct vkd3d_shader_macro *macros; + /** Size, in elements, of \ref macros. */ + unsigned int macro_count; + + /** + * Optional pointer to a callback function, which will be called in order to + * evaluate #include directives. The function receives parameters + * corresponding to the directive's arguments, and should return the + * complete text of the included file. + * + * If this field is set to NULL, or if this structure is omitted, + * vkd3d-shader will attempt to open included files using POSIX file APIs. + * + * If this field is set to NULL, the \ref pfn_close_include field must also + * be set to NULL. + */ + PFN_vkd3d_shader_open_include pfn_open_include; + /** + * Optional pointer to a callback function, which will be called whenever an + * included file is closed. This function will be called exactly once for + * each successful call to \ref pfn_open_include, and should be used to free + * any resources allocated thereby. + * + * If this field is set to NULL, the \ref pfn_open_include field must also + * be set to NULL. + */ + PFN_vkd3d_shader_close_include pfn_close_include; + /** + * User-defined pointer which will be passed unmodified to the + * \ref pfn_open_include and \ref pfn_close_include callbacks. + */ + void *include_context; +}; + /* root signature 1.0 */ enum vkd3d_shader_filter { @@ -1505,6 +1633,35 @@ struct vkd3d_shader_signature_element *vkd3d_shader_find_signature_element( */ void vkd3d_shader_free_shader_signature(struct vkd3d_shader_signature *signature);
+/* 1.3 */ + +/** + * Preprocess the given source code. + * + * This function supports the following chained structures: + * - vkd3d_shader_preprocess_info + * + * \param compile_info A chained structure containing compilation parameters. + * + * \param out A pointer to a vkd3d_shader_code structure in which the + * preprocessed code will be stored. + * \n + * The preprocessed shader is allocated by vkd3d-shader and should be freed with + * vkd3d_shader_free_shader_code() when no longer needed. + * + * \param messages Optional output location for error or informational messages + * produced by the compiler. + * \n + * This parameter behaves identically to the \a messages parameter of + * vkd3d_shader_compile(). + * + * \return A member of \ref vkd3d_result. + * + * \since 1.3 + */ +int vkd3d_shader_preprocess(const struct vkd3d_shader_compile_info *compile_info, + struct vkd3d_shader_code *out, char **messages); + #endif /* VKD3D_SHADER_NO_PROTOTYPES */
/** Type of vkd3d_shader_get_version(). */ diff --git a/libs/vkd3d-shader/vkd3d_shader.map b/libs/vkd3d-shader/vkd3d_shader.map index 1937131b..2e49fe24 100644 --- a/libs/vkd3d-shader/vkd3d_shader.map +++ b/libs/vkd3d-shader/vkd3d_shader.map @@ -14,6 +14,7 @@ global: vkd3d_shader_get_version; vkd3d_shader_parse_input_signature; vkd3d_shader_parse_root_signature; + vkd3d_shader_preprocess; vkd3d_shader_scan; vkd3d_shader_serialize_root_signature;
diff --git a/libs/vkd3d-shader/vkd3d_shader_main.c b/libs/vkd3d-shader/vkd3d_shader_main.c index 0c6a3c3f..a1bd3fc3 100644 --- a/libs/vkd3d-shader/vkd3d_shader_main.c +++ b/libs/vkd3d-shader/vkd3d_shader_main.c @@ -1141,3 +1141,11 @@ const enum vkd3d_shader_target_type *vkd3d_shader_get_supported_target_types( return NULL; } } + +int vkd3d_shader_preprocess(const struct vkd3d_shader_compile_info *compile_info, + struct vkd3d_shader_code *out, char **messages) +{ + TRACE("compile_info %p, out %p, messages %p.\n", compile_info, out, messages); + + return VKD3D_ERROR_NOT_IMPLEMENTED; +}
Signed-off-by: Henri Verbeet hverbeet@codeweavers.com
Signed-off-by: Zebediah Figura zfigura@codeweavers.com --- include/vkd3d_shader.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
diff --git a/include/vkd3d_shader.h b/include/vkd3d_shader.h index 45b51a5d..b677d365 100644 --- a/include/vkd3d_shader.h +++ b/include/vkd3d_shader.h @@ -54,6 +54,11 @@ enum vkd3d_shader_structure_type /** The structure is a vkd3d_shader_transform_feedback_info structure. */ VKD3D_SHADER_STRUCTURE_TYPE_TRANSFORM_FEEDBACK_INFO,
+ /** + * The structure is a vkd3d_shader_hlsl_source_info structure. + * \since 1.3 + */ + VKD3D_SHADER_STRUCTURE_TYPE_HLSL_SOURCE_INFO, /** * The structure is a vkd3d_shader_preprocess_info structure. * \since 1.3 @@ -790,6 +795,39 @@ struct vkd3d_shader_preprocess_info void *include_context; };
+/** + * A chained structure containing HLSL compilation parameters. + * + * This structure is optional. + * + * This structure extends vkd3d_shader_compile_info. + * + * This structure contains only input parameters. + * + * \since 1.3 + */ +struct vkd3d_shader_hlsl_source_info +{ + /** Must be set to VKD3D_SHADER_STRUCTURE_TYPE_HLSL_SOURCE_INFO. */ + enum vkd3d_shader_structure_type type; + /** Optional pointer to a structure containing further parameters. */ + const void *next; + + /** + * Optional pointer to a null-terminated string containing the shader entry + * point. + * + * If this parameter is NULL, vkd3d-shader uses the entry point "main". + */ + const char *entry_point; + struct vkd3d_shader_code secondary_code; + /** + * Pointer to a null-terminated string containing the target shader + * profile. + */ + const char *profile; +}; + /* root signature 1.0 */ enum vkd3d_shader_filter {
Signed-off-by: Henri Verbeet hverbeet@codeweavers.com