Signed-off-by: Zebediah Figura zfigura@codeweavers.com --- include/vkd3d_shader.h | 2 + libs/vkd3d-shader/vkd3d_shader_main.c | 273 +++++++++++++++----------- 2 files changed, 159 insertions(+), 116 deletions(-)
diff --git a/include/vkd3d_shader.h b/include/vkd3d_shader.h index 3efd355f..003da386 100644 --- a/include/vkd3d_shader.h +++ b/include/vkd3d_shader.h @@ -448,6 +448,7 @@ enum vkd3d_shader_source_type * the format used for Direct3D shader model 4 and 5 shaders. */ VKD3D_SHADER_SOURCE_DXBC_TPF, + VKD3D_SHADER_SOURCE_HLSL,
VKD3D_FORCE_32_BIT_ENUM(VKD3D_SHADER_SOURCE_TYPE), }; @@ -466,6 +467,7 @@ enum vkd3d_shader_target_type */ VKD3D_SHADER_TARGET_SPIRV_BINARY, VKD3D_SHADER_TARGET_SPIRV_TEXT, + 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 16d895ff..e44a80b7 100644 --- a/libs/vkd3d-shader/vkd3d_shader_main.c +++ b/libs/vkd3d-shader/vkd3d_shader_main.c @@ -299,92 +299,12 @@ 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; - - 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) - { - vkd3d_shader_free_messages(*messages); - *messages = NULL; - } - - 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; - - vkd3d_shader_dump_shader(parser.shader_version.type, &compile_info->source); - - 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; size_t descriptors_size;
- struct vkd3d_shader_message_context message_context; + struct vkd3d_shader_message_context *message_context;
struct vkd3d_shader_cf_info { @@ -409,20 +329,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) @@ -706,7 +625,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; } @@ -715,7 +634,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; } @@ -728,7 +647,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; } @@ -742,7 +661,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; } @@ -752,7 +671,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; } @@ -762,13 +681,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; } @@ -778,7 +697,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; } @@ -787,7 +706,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; } @@ -795,7 +714,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; } @@ -804,7 +723,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; } @@ -837,39 +756,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; } @@ -904,14 +809,39 @@ 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; + + if (compile_info->source_type == VKD3D_SHADER_SOURCE_DXBC_TPF) + ret = scan_dxbc(compile_info, &message_context); + else if (compile_info->source_type == VKD3D_SHADER_SOURCE_HLSL) + ret = VKD3D_ERROR_NOT_IMPLEMENTED; + + 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); @@ -919,6 +849,107 @@ void vkd3d_shader_free_scan_descriptor_info(struct vkd3d_shader_scan_descriptor_ vkd3d_free(scan_descriptor_info->descriptors); }
+static int compile_dxbc_to_spirv(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_instruction instruction; + struct vkd3d_shader_compile_info scan_info; + struct vkd3d_dxbc_compiler *spirv_compiler; + struct vkd3d_shader_parser parser; + int ret; + + 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) + 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 (!(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_free_scan_descriptor_info(&scan_descriptor_info); + return ret; +} + +static int compile_hlsl_to_dxbc(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_OUT_OF_MEMORY; + + if (compile_info->source_type == VKD3D_SHADER_SOURCE_DXBC_TPF + && compile_info->target_type == VKD3D_SHADER_TARGET_SPIRV_BINARY) + ret = compile_dxbc_to_spirv(compile_info, out, &message_context); + else if (compile_info->source_type == VKD3D_SHADER_SOURCE_HLSL + && compile_info->target_type == VKD3D_SHADER_TARGET_DXBC_TPF) + ret = compile_hlsl_to_dxbc(compile_info, out, &message_context); + + vkd3d_shader_message_context_trace_messages(&message_context); + if (messages) + { + vkd3d_shader_free_messages(*messages); + if (!(*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_shader_code(struct vkd3d_shader_code *shader_code) { TRACE("shader_code %p.\n", shader_code); @@ -1057,6 +1088,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); @@ -1076,6 +1108,11 @@ const enum vkd3d_shader_target_type *vkd3d_shader_get_supported_target_types( #endif };
+ 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) @@ -1084,6 +1121,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;