From: Giovanni Mascellani gmascellani@codeweavers.com
--- programs/vkd3d-dxbc/main.c | 174 +++++++++++++++++++++++++++++++++++-- 1 file changed, 167 insertions(+), 7 deletions(-)
diff --git a/programs/vkd3d-dxbc/main.c b/programs/vkd3d-dxbc/main.c index 6d5d64805..dacbb5be5 100644 --- a/programs/vkd3d-dxbc/main.c +++ b/programs/vkd3d-dxbc/main.c @@ -33,26 +33,72 @@ #include <term.h> #endif
+static bool array_reserve(void **elements, size_t *capacity, size_t element_count, size_t element_size) +{ + size_t new_capacity, max_capacity; + void *new_elements; + + if (element_count <= *capacity) + return true; + + max_capacity = ~(size_t)0 / element_size; + if (max_capacity < element_count) + return false; + + new_capacity = max(*capacity, 4); + while (new_capacity < element_count && new_capacity <= max_capacity / 2) + new_capacity *= 2; + + if (new_capacity < element_count) + new_capacity = element_count; + + if (!(new_elements = realloc(*elements, new_capacity * element_size))) + return false; + + *elements = new_elements; + *capacity = new_capacity; + + return true; +} + enum { OPTION_COLOUR = CHAR_MAX + 1, + OPTION_EMIT, OPTION_HELP, OPTION_IGNORE_CHECKSUM, OPTION_LIST, OPTION_LIST_DATA, OPTION_NO_COLOUR, + OPTION_OUTPUT, OPTION_VERSION, };
+struct action +{ + enum action_type + { + ACTION_TYPE_EMIT, + } type; + union + { + const char *output_filename; + } u; +}; + struct options { const char *input_filename; + const char *output_filename; bool print_help; bool list; bool list_data; bool print_version; bool ignore_checksum;
+ struct action *actions; + size_t action_count, action_capacity; + struct colours { const char *reset; @@ -62,6 +108,23 @@ struct options } colours; };
+static struct action *action_push(struct options *options, enum action_type type) +{ + struct action *action; + + if (!array_reserve((void **)&options->actions, &options->action_capacity, + options->action_count + 1, sizeof(*options->actions))) + { + fprintf(stderr, "Out of memory.\n"); + exit(1); + } + + action = &options->actions[options->action_count++]; + action->type = type; + + return action; +} + static bool has_colour(void) { #ifdef HAVE_NCURSES @@ -84,16 +147,19 @@ static bool has_colour(void)
static bool parse_command_line(int argc, char **argv, struct options *options) { + struct action *action; int option;
static struct option long_options[] = { {"colour", no_argument, NULL, OPTION_COLOUR}, + {"emit", no_argument, NULL, OPTION_EMIT}, {"help", no_argument, NULL, OPTION_HELP}, {"ignore-checksum", no_argument, NULL, OPTION_IGNORE_CHECKSUM}, {"list", no_argument, NULL, OPTION_LIST}, {"list-data", no_argument, NULL, OPTION_LIST_DATA}, {"no-colour", no_argument, NULL, OPTION_NO_COLOUR}, + {"output", required_argument, NULL, OPTION_OUTPUT}, {"version", no_argument, NULL, OPTION_VERSION}, {NULL, 0, NULL, 0}, }; @@ -122,7 +188,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, "ehtVo:", long_options, NULL)) == -1) break;
switch (option) @@ -131,6 +197,17 @@ static bool parse_command_line(int argc, char **argv, struct options *options) options->colours = colours; break;
+ case 'e': + case OPTION_EMIT: + action = action_push(options, ACTION_TYPE_EMIT); + if (!options->output_filename) + { + fprintf(stderr, "No output filename specified.\n"); + return false; + } + action->u.output_filename = options->output_filename; + break; + case 'h': case OPTION_HELP: options->print_help = true; @@ -153,6 +230,11 @@ static bool parse_command_line(int argc, char **argv, struct options *options) options->colours = no_colours; break;
+ case OPTION_OUTPUT: + case 'o': + options->output_filename = optarg; + break; + case 'V': case OPTION_VERSION: options->print_version = true; @@ -181,6 +263,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 +349,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,26 +498,28 @@ 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 = false, close_output = false; struct options options; - bool close_input; + FILE *input, *output; char *messages; uint32_t flags; int fail = 1; - FILE *input; + size_t i; int ret;
if (!parse_command_line(argc, argv, &options)) { print_usage(argv[0]); - return 1; + goto done; }
if (options.print_help || (argc < 2 && isatty(fileno(stdin)))) { print_usage(argv[0]); - return 0; + fail = 0; + goto done; }
if (options.print_version) @@ -406,7 +527,8 @@ int main(int argc, char **argv) const char *version = vkd3d_shader_get_version(NULL, NULL);
fprintf(stdout, "vkd3d-dxbc version " PACKAGE_VERSION " using %s\n", version); - return 0; + fail = 0; + goto done; }
if (!(input = open_input(options.input_filename, &close_input))) @@ -432,11 +554,49 @@ int main(int argc, char **argv) if (options.list || options.list_data) dump_dxbc(&dxbc, &dxbc_desc, &options);
+ for (i = 0; i < options.action_count; ++i) + { + struct action *action = &options.actions[i]; + + switch (action->type) + { + case ACTION_TYPE_EMIT: + 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); + break; + + default: + vkd3d_unreachable(); + } + } + vkd3d_shader_free_dxbc(&dxbc_desc); vkd3d_shader_free_shader_code(&dxbc); fail = 0; done: + free(options.actions); if (close_input) fclose(input); + if (close_output) + fclose(output); return fail; }