2017-06-16 13:38:21 -07:00
|
|
|
/*
|
|
|
|
* 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 <fcntl.h>
|
2018-04-11 04:21:43 -07:00
|
|
|
#include <stdbool.h>
|
2017-06-16 13:38:21 -07:00
|
|
|
#include <stdio.h>
|
2019-01-31 02:29:33 -08:00
|
|
|
#include <stdlib.h>
|
2017-06-16 13:38:21 -07:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2020-07-02 06:43:13 -07:00
|
|
|
#include <getopt.h>
|
2017-06-16 13:38:21 -07:00
|
|
|
|
|
|
|
#include "vkd3d_common.h"
|
|
|
|
#include "vkd3d_shader.h"
|
|
|
|
|
2020-06-19 04:43:35 -07:00
|
|
|
#define MAX_COMPILE_OPTIONS 1
|
|
|
|
|
2020-07-02 06:43:13 -07:00
|
|
|
enum
|
|
|
|
{
|
2020-07-02 06:43:14 -07:00
|
|
|
OPTION_HELP = CHAR_MAX + 1,
|
2020-07-02 06:43:16 -07:00
|
|
|
OPTION_OUTPUT,
|
2020-07-02 06:43:14 -07:00
|
|
|
OPTION_STRIP_DEBUG,
|
2020-07-02 06:43:15 -07:00
|
|
|
OPTION_VERSION,
|
2020-07-02 06:43:13 -07:00
|
|
|
};
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-15 04:57:52 -07:00
|
|
|
if (!(code = malloc(shader->size)))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-18 04:31:46 -07:00
|
|
|
static void print_usage(const char *program_name)
|
|
|
|
{
|
2020-07-02 06:43:12 -07:00
|
|
|
static const char usage[] =
|
|
|
|
"[options...] file\n"
|
|
|
|
"Options:\n"
|
2020-07-02 06:43:14 -07:00
|
|
|
" -h, --help Display this information and exit.\n"
|
2020-07-02 06:43:16 -07:00
|
|
|
" -o, --output=<file> Write the output to <file>.\n"
|
2020-07-02 06:43:13 -07:00
|
|
|
" --strip-debug Strip debug information from the output.\n"
|
2020-07-02 06:43:15 -07:00
|
|
|
" -V, --version Display version information and exit.\n"
|
2020-07-10 05:13:59 -07:00
|
|
|
" -x <type> Specify the type of the source. Valid values are\n"
|
|
|
|
" 'dxbc-tpf' and 'none'.\n"
|
2020-07-02 06:43:13 -07:00
|
|
|
" -- Stop option processing. Any subsequent argument is\n"
|
|
|
|
" interpreted as a filename.\n";
|
2017-07-18 04:31:46 -07:00
|
|
|
|
2020-07-02 06:43:12 -07:00
|
|
|
fprintf(stderr, "Usage: %s %s", program_name, usage);
|
2017-07-18 04:31:46 -07:00
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
struct options
|
|
|
|
{
|
|
|
|
const char *filename;
|
|
|
|
const char *output_filename;
|
2020-07-10 05:13:59 -07:00
|
|
|
enum vkd3d_shader_source_type source_type;
|
2020-07-02 06:43:15 -07:00
|
|
|
bool print_version;
|
2020-07-02 06:43:14 -07:00
|
|
|
|
2020-06-19 04:43:35 -07:00
|
|
|
struct vkd3d_shader_compile_option compile_options[MAX_COMPILE_OPTIONS];
|
|
|
|
unsigned int compile_option_count;
|
2017-06-16 13:38:21 -07:00
|
|
|
};
|
|
|
|
|
2020-06-19 04:43:35 -07:00
|
|
|
static void add_compile_option(struct options *options,
|
|
|
|
enum vkd3d_shader_compile_option_name name, unsigned int value)
|
|
|
|
{
|
|
|
|
struct vkd3d_shader_compile_option *o;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < options->compile_option_count; ++i)
|
|
|
|
{
|
|
|
|
o = &options->compile_options[i];
|
|
|
|
|
|
|
|
if (o->name == name)
|
|
|
|
{
|
|
|
|
o->value = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options->compile_option_count >= ARRAY_SIZE(options->compile_options))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Ignoring option.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
o = &options->compile_options[options->compile_option_count++];
|
|
|
|
o->name = name;
|
|
|
|
o->value = value;
|
|
|
|
}
|
|
|
|
|
2020-07-10 05:13:59 -07:00
|
|
|
static enum vkd3d_shader_source_type parse_source_type(const char *source)
|
|
|
|
{
|
|
|
|
if (!strcmp(source, "dxbc-tpf") || !strcmp(source, "none"))
|
|
|
|
return VKD3D_SHADER_SOURCE_DXBC_TPF;
|
|
|
|
|
|
|
|
return VKD3D_SHADER_SOURCE_NONE;
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static bool parse_command_line(int argc, char **argv, struct options *options)
|
|
|
|
{
|
2020-07-02 06:43:13 -07:00
|
|
|
int option;
|
2017-06-16 13:38:21 -07:00
|
|
|
|
2020-07-02 06:43:13 -07:00
|
|
|
static struct option long_options[] =
|
|
|
|
{
|
2020-07-02 06:43:16 -07:00
|
|
|
{"help", no_argument, NULL, OPTION_HELP},
|
|
|
|
{"output", required_argument, NULL, OPTION_OUTPUT},
|
|
|
|
{"strip-debug", no_argument, NULL, OPTION_STRIP_DEBUG},
|
|
|
|
{"version", no_argument, NULL, OPTION_VERSION},
|
|
|
|
{NULL, 0, NULL, 0},
|
2020-07-02 06:43:13 -07:00
|
|
|
};
|
2017-06-16 13:38:21 -07:00
|
|
|
|
|
|
|
memset(options, 0, sizeof(*options));
|
2020-07-10 05:13:59 -07:00
|
|
|
options->source_type = VKD3D_SHADER_SOURCE_DXBC_TPF;
|
2017-06-16 13:38:21 -07:00
|
|
|
|
2020-07-02 06:43:13 -07:00
|
|
|
for (;;)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2020-07-10 05:13:59 -07:00
|
|
|
if ((option = getopt_long(argc, argv, "ho:Vx:", long_options, NULL)) == -1)
|
2020-07-02 06:43:13 -07:00
|
|
|
break;
|
2017-06-16 13:38:21 -07:00
|
|
|
|
2020-07-02 06:43:13 -07:00
|
|
|
switch (option)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2020-07-02 06:43:16 -07:00
|
|
|
case OPTION_OUTPUT:
|
2020-07-02 06:43:13 -07:00
|
|
|
case 'o':
|
|
|
|
options->output_filename = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPTION_STRIP_DEBUG:
|
|
|
|
add_compile_option(options, VKD3D_SHADER_COMPILE_OPTION_STRIP_DEBUG, 1);
|
2017-06-16 13:38:21 -07:00
|
|
|
break;
|
2020-07-02 06:43:13 -07:00
|
|
|
|
2020-07-02 06:43:15 -07:00
|
|
|
case OPTION_VERSION:
|
|
|
|
case 'V':
|
|
|
|
options->print_version = true;
|
|
|
|
return true;
|
|
|
|
|
2020-07-10 05:13:59 -07:00
|
|
|
case 'x':
|
|
|
|
if ((options->source_type = parse_source_type(optarg)) == VKD3D_SHADER_SOURCE_NONE)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Invalid source type '%s' specified.\n", optarg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-07-02 06:43:13 -07:00
|
|
|
default:
|
|
|
|
return false;
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 06:43:13 -07:00
|
|
|
if (optind >= argc)
|
|
|
|
return false;
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
options->filename = argv[argc - 1];
|
2020-07-02 06:43:13 -07:00
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2020-06-19 04:43:32 -07:00
|
|
|
struct vkd3d_shader_compile_info info;
|
|
|
|
struct vkd3d_shader_code spirv;
|
2017-06-16 13:38:21 -07:00
|
|
|
struct options options;
|
2020-06-17 05:34:16 -07:00
|
|
|
int ret;
|
2017-06-16 13:38:21 -07:00
|
|
|
|
|
|
|
if (!parse_command_line(argc, argv, &options))
|
|
|
|
{
|
2017-07-18 04:31:46 -07:00
|
|
|
print_usage(argv[0]);
|
2017-06-16 13:38:21 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-02 06:43:15 -07:00
|
|
|
if (options.print_version)
|
|
|
|
{
|
|
|
|
fprintf(stdout, "vkd3d shader compiler version " PACKAGE_VERSION "\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-19 04:43:32 -07:00
|
|
|
info.type = VKD3D_SHADER_STRUCTURE_TYPE_COMPILE_INFO;
|
|
|
|
info.next = NULL;
|
2020-07-10 05:13:59 -07:00
|
|
|
info.source_type = options.source_type;
|
2020-06-23 02:20:12 -07:00
|
|
|
info.target_type = VKD3D_SHADER_TARGET_SPIRV_BINARY;
|
2020-06-19 04:43:35 -07:00
|
|
|
info.options = options.compile_options;
|
|
|
|
info.option_count = options.compile_option_count;
|
2020-06-19 04:43:32 -07:00
|
|
|
|
|
|
|
if (!read_shader(&info.source, options.filename))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to read DXBC shader.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-06-19 04:43:36 -07:00
|
|
|
ret = vkd3d_shader_compile(&info, &spirv);
|
2020-06-19 04:43:32 -07:00
|
|
|
vkd3d_shader_free_shader_code(&info.source);
|
2020-06-17 05:34:16 -07:00
|
|
|
if (ret < 0)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2020-06-17 05:34:16 -07:00
|
|
|
fprintf(stderr, "Failed to compile DXBC shader, ret %d.\n", ret);
|
2017-06-16 13:38:21 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.output_filename)
|
|
|
|
write_shader(&spirv, options.output_filename);
|
|
|
|
|
|
|
|
vkd3d_shader_free_shader_code(&spirv);
|
|
|
|
return 0;
|
|
|
|
}
|