From 6bfb94c440ea6a925c6157b83073855eac061041 Mon Sep 17 00:00:00 2001 From: Henri Verbeet Date: Thu, 3 Aug 2023 12:00:00 +0200 Subject: [PATCH] vkd3d-dxbc: Introduce a program to inspect and modify DXBC blobs. --- .gitignore | 1 + Makefile.am | 6 +- programs/vkd3d-dxbc/main.c | 120 +++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 programs/vkd3d-dxbc/main.c diff --git a/.gitignore b/.gitignore index b6d29d19..841220ee 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ Makefile Makefile.in test-suite.log /vkd3d-compiler +/vkd3d-dxbc vkd3d-*.tar.xz diff --git a/Makefile.am b/Makefile.am index ecb7c7e2..45f1dd54 100644 --- a/Makefile.am +++ b/Makefile.am @@ -353,11 +353,15 @@ EXTRA_DIST += \ libs/vkd3d-shader/libvkd3d-shader.pc.in \ libs/vkd3d-utils/libvkd3d-utils.pc.in -bin_PROGRAMS = vkd3d-compiler +bin_PROGRAMS = vkd3d-compiler vkd3d-dxbc + vkd3d_compiler_SOURCES = programs/vkd3d-compiler/main.c vkd3d_compiler_CFLAGS = $(AM_CFLAGS) @NCURSES_CFLAGS@ vkd3d_compiler_LDADD = libvkd3d-shader.la @NCURSES_LIBS@ +vkd3d_dxbc_SOURCES = programs/vkd3d-dxbc/main.c +vkd3d_dxbc_LDADD = libvkd3d-shader.la + LDADD = libvkd3d.la libvkd3d-shader.la libvkd3d-utils.la AM_DEFAULT_SOURCE_EXT = .c diff --git a/programs/vkd3d-dxbc/main.c b/programs/vkd3d-dxbc/main.c new file mode 100644 index 00000000..f75f9542 --- /dev/null +++ b/programs/vkd3d-dxbc/main.c @@ -0,0 +1,120 @@ +/* + * Copyright 2023 Henri Verbeet for CodeWeavers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include +#include +#include +#include +#include +#include + +#include "vkd3d_common.h" +#include "vkd3d_shader.h" + +enum +{ + OPTION_HELP = CHAR_MAX + 1, + OPTION_VERSION, +}; + +struct options +{ + bool print_help; + bool print_version; +}; + +static bool parse_command_line(int argc, char **argv, struct options *options) +{ + int option; + + static struct option long_options[] = + { + {"help", no_argument, NULL, OPTION_HELP}, + {"version", no_argument, NULL, OPTION_VERSION}, + {NULL, 0, NULL, 0}, + }; + + memset(options, 0, sizeof(*options)); + + for (;;) + { + if ((option = getopt_long(argc, argv, "hV", long_options, NULL)) == -1) + break; + + switch (option) + { + case 'h': + case OPTION_HELP: + options->print_help = true; + return true; + + case 'V': + case OPTION_VERSION: + options->print_version = true; + return true; + + default: + return false; + } + } + + return true; +} + +static void print_usage(const char *program_name) +{ + static const char usage[] = + "[options...]\n" + "Options:\n" + " -h, --help Display this information and exit.\n" + " -V, --version Display version information and exit.\n" + " -- Stop option processing. Any subsequent argument is\n" + " interpreted as a filename.\n"; + + fprintf(stderr, "Usage: %s %s", program_name, usage); +} + +int main(int argc, char **argv) +{ + struct options options; + + if (!parse_command_line(argc, argv, &options)) + { + print_usage(argv[0]); + return 1; + } + + if (options.print_help || (argc < 2 && isatty(fileno(stdin)))) + { + print_usage(argv[0]); + return 0; + } + + if (options.print_version) + { + const char *version = vkd3d_shader_get_version(NULL, NULL); + + fprintf(stdout, "vkd3d-dxbc version " PACKAGE_VERSION " using %s\n", version); + return 0; + } + + return 0; +}