From fe13e35f72af8fa43082a0e69a38042802dd1ede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zef=20Kucia?= Date: Fri, 16 Jun 2017 22:38:21 +0200 Subject: [PATCH] programs/vkd3d-compiler: Add program. Mostly for testing shader translation. --- Makefile.am | 4 + programs/vkd3d-compiler/main.c | 168 +++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 programs/vkd3d-compiler/main.c diff --git a/Makefile.am b/Makefile.am index a7f12021..4a1af56d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -96,6 +96,10 @@ nodist_pkgconfig_DATA = libvkd3d.pc CLEANFILES += libvkd3d.pc EXTRA_DIST = libs/vkd3d/libvkd3d.pc.in +bin_PROGRAMS = vkd3d-compiler +vkd3d_compiler_SOURCES = programs/vkd3d-compiler/main.c +vkd3d_compiler_LDADD = libvkd3d-shader.la + LDADD = libvkd3d-utils.la check_PROGRAMS = $(vkd3d_tests) AM_DEFAULT_SOURCE_EXT = .c diff --git a/programs/vkd3d-compiler/main.c b/programs/vkd3d-compiler/main.c new file mode 100644 index 00000000..984a378f --- /dev/null +++ b/programs/vkd3d-compiler/main.c @@ -0,0 +1,168 @@ +/* + * Copyright 2017 Józef Kucia 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 + */ + +#include +#include +#include +#include + +#include "vkd3d_common.h" +#include "vkd3d_shader.h" + +static bool read_shader(struct vkd3d_shader_code *shader, const char *filename) +{ + struct stat st; + void *code; + FILE *fd; + + memset(shader, 0, sizeof(*shader)); + + if (stat(filename, &st) == -1) + { + fprintf(stderr, "Could not stat file: '%s'.\n", filename); + return false; + } + shader->size = st.st_size; + + if (!(fd = fopen(filename, "rb"))) + { + fprintf(stderr, "Cannot open file for reading: '%s'.\n", filename); + return false; + } + + if (!(code = malloc(sizeof(uint32_t) * shader->size))) + { + fprintf(stderr, "Out of memory.\n"); + fclose(fd); + return false; + } + shader->code = code; + + if (fread(code, 1, shader->size, fd) != shader->size) + { + fprintf(stderr, "Could not read shader bytecode from file: '%s'.\n", filename); + free(code); + fclose(fd); + return false; + } + + fclose(fd); + return true; +} + +static bool write_shader(const struct vkd3d_shader_code *shader, const char *filename) +{ + FILE *fd; + + if (!(fd = fopen(filename, "wb"))) + { + fprintf(stderr, "Cannot open file for writing: '%s'.\n", filename); + return false; + } + + if (fwrite(shader->code, 1, shader->size, fd) != shader->size) + fprintf(stderr, "Could not write shader bytecode to file: '%s'.\n", filename); + + fclose(fd); + return true; +} + +static const struct +{ + const char *name; + enum vkd3d_shader_compiler_option compiler_option; +} +compiler_options[] = +{ + {"--flip-y", VKD3D_SHADER_FLIP_Y}, +}; + +struct options +{ + const char *filename; + const char *output_filename; + uint32_t compiler_options; +}; + +static bool parse_command_line(int argc, char **argv, struct options *options) +{ + unsigned int i, j; + + if (argc < 2) + return false; + + memset(options, 0, sizeof(*options)); + + for (i = 1; i < argc - 1; ++i) + { + if (!strcmp(argv[i], "-o")) + { + if (i + 1 >= argc - 1) + return false; + options->output_filename = argv[++i]; + continue; + } + + for (j = 0; j < ARRAY_SIZE(compiler_options); ++j) + { + if (!strcmp(argv[i], compiler_options[j].name)) + { + options->compiler_options |= compiler_options[j].compiler_option; + break; + } + } + if (j == ARRAY_SIZE(compiler_options)) + return false; + } + + options->filename = argv[argc - 1]; + return true; +} + +int main(int argc, char **argv) +{ + struct vkd3d_shader_code dxbc, spirv; + struct options options; + HRESULT hr; + + if (!parse_command_line(argc, argv, &options)) + { + fprintf(stderr, "usage: %s [--flip-y] [-o ] \n", argv[0]); + return 1; + } + + if (!read_shader(&dxbc, options.filename)) + { + fprintf(stderr, "Failed to read DXBC shader.\n"); + return 1; + } + + hr = vkd3d_shader_compile_dxbc(&dxbc, &spirv, options.compiler_options); + vkd3d_shader_free_shader_code(&dxbc); + if (FAILED(hr)) + { + fprintf(stderr, "Failed to compile DXBC shader, hr %#x.\n", hr); + return 1; + } + + if (options.output_filename) + write_shader(&spirv, options.output_filename); + + vkd3d_shader_free_shader_code(&spirv); + return 0; +}