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);