From: Giovanni Mascellani gmascellani@codeweavers.com
--- programs/vkd3d-dxbc/main.c | 80 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 4 deletions(-)
diff --git a/programs/vkd3d-dxbc/main.c b/programs/vkd3d-dxbc/main.c index b6ac70873..d5291f9c0 100644 --- a/programs/vkd3d-dxbc/main.c +++ b/programs/vkd3d-dxbc/main.c @@ -42,11 +42,13 @@ enum OPTION_NO_COLOUR, OPTION_VERSION, OPTION_IGNORE_CHECKSUM, + OPTION_OUTPUT, };
struct options { const char *input_filename; + const char *output_filename; bool print_help; bool list; bool list_data; @@ -95,6 +97,7 @@ static bool parse_command_line(int argc, char **argv, struct options *options) {"no-colour", no_argument, NULL, OPTION_NO_COLOUR}, {"version", no_argument, NULL, OPTION_VERSION}, {"ignore-checksum", no_argument, NULL, OPTION_IGNORE_CHECKSUM}, + {"output", required_argument, NULL, OPTION_OUTPUT}, {NULL, 0, NULL, 0}, };
@@ -122,7 +125,7 @@ static bool parse_command_line(int argc, char **argv, struct options *options)
for (;;) { - if ((option = getopt_long(argc, argv, "htV", long_options, NULL)) == -1) + if ((option = getopt_long(argc, argv, "htVo:", long_options, NULL)) == -1) break;
switch (option) @@ -158,6 +161,11 @@ static bool parse_command_line(int argc, char **argv, struct options *options) options->ignore_checksum = true; break;
+ case OPTION_OUTPUT: + case 'o': + options->output_filename = optarg; + break; + default: return false; } @@ -181,6 +189,9 @@ static void print_usage(const char *program_name) " --no-colour Disable colour, even when supported by the output.\n" " -V, --version Display version information and exit.\n" " --ignore-checksum Do not validate the checksum when parsing the DXBC blob.\n" + " -o, --output=<file> Re-serialize the DXBC shader to <file>. This is useful\n" + " in combination with --ignore-checksum to fix bad or\n" + " missing checksums.\n" " -- Stop option processing. Any subsequent argument is\n" " interpreted as a filename.\n" "\n" @@ -264,6 +275,40 @@ static bool read_input(FILE *f, struct vkd3d_shader_code *dxbc) return true; }
+static FILE *open_output(const char *filename, bool *close) +{ + FILE *f; + + *close = false; + + if (!strcmp(filename, "-")) + return stdout; + + if (!(f = fopen(filename, "wb"))) + { + fprintf(stderr, "Unable to open '%s' for writing.\n", filename); + return NULL; + } + + *close = true; + return f; +} + +static bool write_output(FILE *f, const struct vkd3d_shader_code *dxbc) +{ + const char *code = dxbc->code; + size_t pos = 0, ret; + + while (pos != dxbc->size) + { + if (!(ret = fwrite(&code[pos], 1, dxbc->size - pos, f))) + return false; + pos += ret; + } + + return true; +} + static const char *dump_tag(char *out, uint32_t tag) { unsigned int i; @@ -379,14 +424,14 @@ static void dump_dxbc(const struct vkd3d_shader_code *dxbc, const struct vkd3d_s
int main(int argc, char **argv) { + struct vkd3d_shader_code dxbc, serialized; struct vkd3d_shader_dxbc_desc dxbc_desc; - struct vkd3d_shader_code dxbc; + bool close_input, close_output = false; struct options options; - bool close_input; + FILE *input, *output; char *messages; uint32_t flags; int fail = 1; - FILE *input; int ret;
if (!parse_command_line(argc, argv, &options)) @@ -432,11 +477,38 @@ int main(int argc, char **argv) if (options.list || options.list_data) dump_dxbc(&dxbc, &dxbc_desc, &options);
+ if (options.output_filename) + { + ret = vkd3d_shader_serialize_dxbc(dxbc_desc.section_count, dxbc_desc.sections, &serialized, &messages); + if (messages) + fputs(messages, stderr); + vkd3d_shader_free_messages(messages); + if (ret < 0) + goto done; + + if (!(output = open_output(options.output_filename, &close_output))) + { + vkd3d_shader_free_shader_code(&serialized); + goto done; + } + + if (!write_output(output, &serialized)) + { + fprintf(stderr, "Failed to write output blob.\n"); + vkd3d_shader_free_shader_code(&serialized); + goto done; + } + + vkd3d_shader_free_shader_code(&serialized); + } + vkd3d_shader_free_dxbc(&dxbc_desc); vkd3d_shader_free_shader_code(&dxbc); fail = 0; done: if (close_input) fclose(input); + if (close_output) + fclose(output); return fail; }