-- v5: vkd3d-dxbc: Add an option to re-emit the shader with the correct checksum. vkd3d-dxbc: Add an option to ignore checksum. vkd3d-shader/dxbc: Add flag to ignore the DXBC checksum.
From: Giovanni Mascellani gmascellani@codeweavers.com
--- include/vkd3d_shader.h | 19 ++++++++++++++++--- libs/vkd3d-shader/dxbc.c | 9 +++++---- 2 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/include/vkd3d_shader.h b/include/vkd3d_shader.h index 9e663919c..04640e6c2 100644 --- a/include/vkd3d_shader.h +++ b/include/vkd3d_shader.h @@ -212,6 +212,20 @@ enum vkd3d_shader_compile_option_feature_flags VKD3D_FORCE_32_BIT_ENUM(VKD3D_SHADER_COMPILE_OPTION_FEATURE_FLAGS), };
+/** + * Flags for vkd3d_shader_parse_dxbc(). + * + * \since 1.12 + */ +enum vkd3d_shader_parse_dxbc_flags +{ + /** Ignore the checksum and continue parsing even if it is + * incorrect. */ + VKD3D_SHADER_PARSE_DXBC_IGNORE_CHECKSUM = 0x00000001, + + VKD3D_FORCE_32_BIT_ENUM(VKD3D_SHADER_PARSE_DXBC_FLAGS), +}; + enum vkd3d_shader_compile_option_name { /** @@ -2377,9 +2391,8 @@ VKD3D_SHADER_API void vkd3d_shader_free_dxbc(struct vkd3d_shader_dxbc_desc *dxbc * * \param dxbc A vkd3d_shader_code structure containing the DXBC blob to parse. * - * \param flags A set of flags modifying the behaviour of the function. No - * flags are defined for this version of vkd3d-shader, and this parameter - * should be set to 0. + * \param flags A combination of zero or more elements of enum + * vkd3d_shader_parse_dxbc_flags. * * \param desc A vkd3d_shader_dxbc_desc structure describing the contents of * the DXBC blob. Its vkd3d_shader_dxbc_section_desc structures will contain diff --git a/libs/vkd3d-shader/dxbc.c b/libs/vkd3d-shader/dxbc.c index 37ebc73c0..019069afb 100644 --- a/libs/vkd3d-shader/dxbc.c +++ b/libs/vkd3d-shader/dxbc.c @@ -150,7 +150,7 @@ static const char *shader_get_string(const char *data, size_t data_size, size_t }
static int parse_dxbc(const struct vkd3d_shader_code *dxbc, struct vkd3d_shader_message_context *message_context, - const char *source_name, struct vkd3d_shader_dxbc_desc *desc) + const char *source_name, uint32_t flags, struct vkd3d_shader_dxbc_desc *desc) { const struct vkd3d_shader_location location = {.source_name = source_name}; struct vkd3d_shader_dxbc_section_desc *sections, *section; @@ -187,7 +187,8 @@ static int parse_dxbc(const struct vkd3d_shader_code *dxbc, struct vkd3d_shader_ checksum[2] = read_u32(&ptr); checksum[3] = read_u32(&ptr); vkd3d_compute_dxbc_checksum(data, data_size, calculated_checksum); - if (memcmp(checksum, calculated_checksum, sizeof(checksum))) + if (memcmp(checksum, calculated_checksum, sizeof(checksum)) + && !(flags & VKD3D_SHADER_PARSE_DXBC_IGNORE_CHECKSUM)) { WARN("Checksum {0x%08x, 0x%08x, 0x%08x, 0x%08x} does not match " "calculated checksum {0x%08x, 0x%08x, 0x%08x, 0x%08x}.\n", @@ -287,7 +288,7 @@ static int for_each_dxbc_section(const struct vkd3d_shader_code *dxbc, unsigned int i; int ret;
- if ((ret = parse_dxbc(dxbc, message_context, source_name, &desc)) < 0) + if ((ret = parse_dxbc(dxbc, message_context, source_name, 0, &desc)) < 0) return ret;
for (i = 0; i < desc.section_count; ++i) @@ -313,7 +314,7 @@ int vkd3d_shader_parse_dxbc(const struct vkd3d_shader_code *dxbc, *messages = NULL; vkd3d_shader_message_context_init(&message_context, VKD3D_SHADER_LOG_INFO);
- ret = parse_dxbc(dxbc, &message_context, NULL, desc); + ret = parse_dxbc(dxbc, &message_context, NULL, flags, desc);
vkd3d_shader_message_context_trace_messages(&message_context); if (!vkd3d_shader_message_context_copy_messages(&message_context, messages) && ret >= 0)
From: Giovanni Mascellani gmascellani@codeweavers.com
--- programs/vkd3d-dxbc/main.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-)
diff --git a/programs/vkd3d-dxbc/main.c b/programs/vkd3d-dxbc/main.c index e76bcb631..6d5d64805 100644 --- a/programs/vkd3d-dxbc/main.c +++ b/programs/vkd3d-dxbc/main.c @@ -37,6 +37,7 @@ enum { OPTION_COLOUR = CHAR_MAX + 1, OPTION_HELP, + OPTION_IGNORE_CHECKSUM, OPTION_LIST, OPTION_LIST_DATA, OPTION_NO_COLOUR, @@ -50,6 +51,7 @@ struct options bool list; bool list_data; bool print_version; + bool ignore_checksum;
struct colours { @@ -86,13 +88,14 @@ static bool parse_command_line(int argc, char **argv, struct options *options)
static struct option long_options[] = { - {"colour", no_argument, NULL, OPTION_COLOUR}, - {"help", no_argument, NULL, OPTION_HELP}, - {"list", no_argument, NULL, OPTION_LIST}, - {"list-data", no_argument, NULL, OPTION_LIST_DATA}, - {"no-colour", no_argument, NULL, OPTION_NO_COLOUR}, - {"version", no_argument, NULL, OPTION_VERSION}, - {NULL, 0, NULL, 0}, + {"colour", no_argument, NULL, OPTION_COLOUR}, + {"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}, + {"version", no_argument, NULL, OPTION_VERSION}, + {NULL, 0, NULL, 0}, };
static const struct colours colours = @@ -133,6 +136,10 @@ static bool parse_command_line(int argc, char **argv, struct options *options) options->print_help = true; return true;
+ case OPTION_IGNORE_CHECKSUM: + options->ignore_checksum = true; + break; + case 't': case OPTION_LIST: options->list = true; @@ -173,6 +180,7 @@ static void print_usage(const char *program_name) " --list-data List the data contained in the DXBC sections.\n" " --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" " -- Stop option processing. Any subsequent argument is\n" " interpreted as a filename.\n" "\n" @@ -376,6 +384,7 @@ int main(int argc, char **argv) struct options options; bool close_input; char *messages; + uint32_t flags; int fail = 1; FILE *input; int ret; @@ -409,7 +418,11 @@ int main(int argc, char **argv) goto done; }
- ret = vkd3d_shader_parse_dxbc(&dxbc, 0, &dxbc_desc, &messages); + flags = 0; + if (options.ignore_checksum) + flags |= VKD3D_SHADER_PARSE_DXBC_IGNORE_CHECKSUM; + + ret = vkd3d_shader_parse_dxbc(&dxbc, flags, &dxbc_desc, &messages); if (messages) fputs(messages, stderr); vkd3d_shader_free_messages(messages);
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; }
vkd3d_compute_dxbc_checksum(data, data_size, calculated_checksum); - if (memcmp(checksum, calculated_checksum, sizeof(checksum))) + if (memcmp(checksum, calculated_checksum, sizeof(checksum)) + && !(flags & VKD3D_SHADER_PARSE_DXBC_IGNORE_CHECKSUM))
It probably doesn't matter too much, but it seems like a bit of a waste to do the memcmp() if we're going to ignore the checksum anyway. In principle that's true for the vkd3d_compute_dxbc_checksum() call as well.
+ 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; + }
Is that a problem? We could just write to stdout, right? In fact, I'd argue for just outputting to stdout in the initial commit, and then adding --output support in a separate commit.
+ " -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"
That doesn't quite match what the code does now.