2017-06-16 13:38:21 -07:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Józef Kucia for CodeWeavers
|
2020-08-11 04:06:37 -07:00
|
|
|
* Copyright 2020 Henri Verbeet for CodeWeavers
|
2017-06-16 13:38:21 -07:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-09-28 06:30:17 -07:00
|
|
|
#ifndef _GNU_SOURCE
|
2020-08-13 06:07:38 -07:00
|
|
|
#define _GNU_SOURCE
|
2020-09-28 06:30:17 -07:00
|
|
|
#endif
|
2017-06-16 13:38:21 -07:00
|
|
|
#include <fcntl.h>
|
|
|
|
#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-09-28 06:30:17 -07:00
|
|
|
#ifdef HAVE_NCURSES
|
|
|
|
#include <term.h>
|
|
|
|
#endif
|
2017-06-16 13:38:21 -07:00
|
|
|
|
2020-09-07 04:35:47 -07:00
|
|
|
#define MAX_COMPILE_OPTIONS 3
|
2020-06-19 04:43:35 -07:00
|
|
|
|
2020-07-02 06:43:13 -07:00
|
|
|
enum
|
|
|
|
{
|
2020-07-02 06:43:14 -07:00
|
|
|
OPTION_HELP = CHAR_MAX + 1,
|
2020-08-04 03:13:32 -07:00
|
|
|
OPTION_BUFFER_UAV,
|
2020-07-02 06:43:16 -07:00
|
|
|
OPTION_OUTPUT,
|
2020-08-11 04:06:36 -07:00
|
|
|
OPTION_PRINT_SOURCE_TYPES,
|
2020-08-11 04:06:37 -07:00
|
|
|
OPTION_PRINT_TARGET_TYPES,
|
2020-07-02 06:43:14 -07:00
|
|
|
OPTION_STRIP_DEBUG,
|
2020-07-02 06:43:15 -07:00
|
|
|
OPTION_VERSION,
|
2020-09-07 04:35:47 -07:00
|
|
|
OPTION_TEXT_FORMATTING,
|
2020-07-02 06:43:13 -07:00
|
|
|
};
|
|
|
|
|
2020-09-07 04:35:44 -07:00
|
|
|
static const struct source_type_info
|
2020-08-11 04:06:36 -07:00
|
|
|
{
|
|
|
|
enum vkd3d_shader_source_type type;
|
|
|
|
const char *name;
|
|
|
|
const char *description;
|
2020-09-28 20:41:07 -07:00
|
|
|
bool is_binary;
|
2020-08-11 04:06:36 -07:00
|
|
|
}
|
|
|
|
source_type_info[] =
|
|
|
|
{
|
|
|
|
{VKD3D_SHADER_SOURCE_DXBC_TPF,
|
2020-09-07 04:35:46 -07:00
|
|
|
"dxbc-tpf", "A 'Tokenized Program Format' shader embedded in a DXBC container.\n"
|
2020-09-28 20:41:07 -07:00
|
|
|
" This is the format used for Direct3D shader model 4 and 5 shaders.\n",
|
|
|
|
true},
|
|
|
|
{VKD3D_SHADER_SOURCE_HLSL,
|
|
|
|
"hlsl", "High Level Shader Language source code.\n",
|
|
|
|
false},
|
2020-08-11 04:06:36 -07:00
|
|
|
};
|
|
|
|
|
2020-09-07 04:35:43 -07:00
|
|
|
static const struct target_type_info
|
2020-08-11 04:06:37 -07:00
|
|
|
{
|
|
|
|
enum vkd3d_shader_target_type type;
|
|
|
|
const char *name;
|
|
|
|
const char *description;
|
2020-09-07 04:35:46 -07:00
|
|
|
bool is_binary;
|
2020-08-11 04:06:37 -07:00
|
|
|
}
|
|
|
|
target_type_info[] =
|
|
|
|
{
|
|
|
|
{VKD3D_SHADER_TARGET_SPIRV_BINARY,
|
|
|
|
"spirv-binary", "A SPIR-V shader in binary form.\n"
|
2020-09-07 04:35:46 -07:00
|
|
|
" This is the format used for Vulkan shaders.\n",
|
|
|
|
true},
|
|
|
|
{VKD3D_SHADER_TARGET_SPIRV_TEXT,
|
|
|
|
"spirv-text", "A SPIR-V shader in text form.\n",
|
|
|
|
false},
|
2020-09-24 18:26:40 -07:00
|
|
|
{VKD3D_SHADER_TARGET_D3D_ASM,
|
|
|
|
"d3d-asm", "A shader in Direct3D assembly form.\n",
|
|
|
|
false},
|
2020-09-28 20:41:08 -07:00
|
|
|
{VKD3D_SHADER_TARGET_DXBC_TPF,
|
|
|
|
"dxbc-tpf", "A 'Tokenized Program Format' shader embedded in a DXBC container.\n"
|
|
|
|
" This is the format used for Direct3D shader model 4 and 5 shaders.\n",
|
|
|
|
true},
|
2020-08-11 04:06:37 -07:00
|
|
|
};
|
|
|
|
|
2020-08-13 06:07:39 -07:00
|
|
|
static bool read_shader(struct vkd3d_shader_code *shader, FILE *f)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2020-08-13 06:07:39 -07:00
|
|
|
size_t size = 4096;
|
2017-06-16 13:38:21 -07:00
|
|
|
struct stat st;
|
2020-08-13 06:07:39 -07:00
|
|
|
size_t pos = 0;
|
|
|
|
uint8_t *data;
|
|
|
|
size_t ret;
|
2017-06-16 13:38:21 -07:00
|
|
|
|
|
|
|
memset(shader, 0, sizeof(*shader));
|
|
|
|
|
2020-08-13 06:07:39 -07:00
|
|
|
if (fstat(fileno(f), &st) == -1)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2020-08-13 06:07:39 -07:00
|
|
|
fprintf(stderr, "Could not stat input.\n");
|
2017-06-16 13:38:21 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-13 06:07:39 -07:00
|
|
|
if (S_ISREG(st.st_mode))
|
|
|
|
size = st.st_size;
|
|
|
|
|
|
|
|
if (!(data = malloc(size)))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2020-08-13 06:07:39 -07:00
|
|
|
fprintf(stderr, "Out of memory.\n");
|
2017-06-16 13:38:21 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-13 06:07:39 -07:00
|
|
|
for (;;)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2020-08-13 06:07:39 -07:00
|
|
|
if (pos >= size)
|
|
|
|
{
|
|
|
|
if (size > SIZE_MAX / 2 || !(data = realloc(data, size * 2)))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Out of memory.\n");
|
|
|
|
free(data);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(ret = fread(&data[pos], 1, size - pos, f)))
|
|
|
|
break;
|
|
|
|
pos += ret;
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
2020-08-13 06:07:39 -07:00
|
|
|
if (!feof(f))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2020-08-13 06:07:39 -07:00
|
|
|
free(data);
|
2017-06-16 13:38:21 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-13 06:07:39 -07:00
|
|
|
shader->code = data;
|
|
|
|
shader->size = pos;
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-13 06:07:38 -07:00
|
|
|
static bool write_shader(const struct vkd3d_shader_code *shader, FILE *f)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2020-08-13 06:07:38 -07:00
|
|
|
return fwrite(shader->code, 1, shader->size, f) == shader->size;
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
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[] =
|
2020-08-13 06:07:39 -07:00
|
|
|
"[options...] [file]\n"
|
2020-07-02 06:43:12 -07:00
|
|
|
"Options:\n"
|
2020-08-11 04:06:36 -07:00
|
|
|
" -h, --help Display this information and exit.\n"
|
2020-09-07 04:35:46 -07:00
|
|
|
" -b <type> Specify the target type.\n"
|
2020-08-11 04:06:36 -07:00
|
|
|
" --buffer-uav=<type> Specify the buffer type to use for buffer UAV bindings.\n"
|
|
|
|
" Valid values are 'buffer-texture' (default) and\n"
|
|
|
|
" 'storage-buffer'.\n"
|
2020-08-13 06:07:38 -07:00
|
|
|
" -o, --output=<file> Write the output to <file>. If <file> is '-' or no\n"
|
|
|
|
" output file is specified, output will be written to\n"
|
|
|
|
" standard output.\n"
|
2020-09-21 09:18:31 -07:00
|
|
|
" --formatting=<flags> Specify the formatting options for text output.\n"
|
|
|
|
" <flags> is a comma separated list of formatting flags,\n"
|
|
|
|
" optionally prefixed by '+' or '-'. Valid flags are\n"
|
|
|
|
" 'colour', 'indent', 'offsets', 'header', and 'raw-ids'.\n"
|
|
|
|
" The 'indent' and 'header' flags are enabled by default.\n"
|
2020-08-11 04:06:36 -07:00
|
|
|
" --print-source-types Display the supported source types and exit.\n"
|
2020-08-11 04:06:37 -07:00
|
|
|
" --print-target-types Display the supported target types for the specified\n"
|
|
|
|
" source type and exit.\n"
|
2020-08-11 04:06:36 -07:00
|
|
|
" --strip-debug Strip debug information from the output.\n"
|
|
|
|
" -V, --version Display version information and exit.\n"
|
|
|
|
" -x <type> Specify the type of the source. Valid values are\n"
|
|
|
|
" 'dxbc-tpf' and 'none'.\n"
|
|
|
|
" -- Stop option processing. Any subsequent argument is\n"
|
2020-08-13 06:07:39 -07:00
|
|
|
" interpreted as a filename.\n"
|
|
|
|
"\n"
|
|
|
|
"If the input file is '-' or not specified, input will be read from standard\n"
|
|
|
|
"input.\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-08-11 04:06:38 -07:00
|
|
|
enum vkd3d_shader_target_type target_type;
|
2020-07-02 06:43:15 -07:00
|
|
|
bool print_version;
|
2020-08-11 04:06:36 -07:00
|
|
|
bool print_source_types;
|
2020-08-11 04:06:37 -07:00
|
|
|
bool print_target_types;
|
2020-09-28 06:30:17 -07:00
|
|
|
uint32_t formatting;
|
|
|
|
bool explicit_colour;
|
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-08-04 03:13:32 -07:00
|
|
|
static bool parse_buffer_uav(enum vkd3d_shader_compile_option_buffer_uav *buffer_uav, const char *arg)
|
|
|
|
{
|
|
|
|
if (!strcmp(arg, "buffer-texture"))
|
|
|
|
{
|
|
|
|
*buffer_uav = VKD3D_SHADER_COMPILE_OPTION_BUFFER_UAV_STORAGE_TEXEL_BUFFER;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(arg, "storage-buffer"))
|
|
|
|
{
|
|
|
|
*buffer_uav = VKD3D_SHADER_COMPILE_OPTION_BUFFER_UAV_STORAGE_BUFFER;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-28 06:30:17 -07:00
|
|
|
static bool parse_formatting(uint32_t *formatting, bool *colour, char *arg)
|
2020-09-07 04:35:47 -07:00
|
|
|
{
|
|
|
|
static const struct formatting_option
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
enum vkd3d_shader_compile_option_formatting_flags value;
|
|
|
|
}
|
|
|
|
opts[] =
|
|
|
|
{
|
2020-09-21 09:18:30 -07:00
|
|
|
{"colour", VKD3D_SHADER_COMPILE_OPTION_FORMATTING_COLOUR},
|
|
|
|
{"indent", VKD3D_SHADER_COMPILE_OPTION_FORMATTING_INDENT},
|
|
|
|
{"offsets", VKD3D_SHADER_COMPILE_OPTION_FORMATTING_OFFSETS},
|
|
|
|
{"header", VKD3D_SHADER_COMPILE_OPTION_FORMATTING_HEADER},
|
|
|
|
{"raw-ids", VKD3D_SHADER_COMPILE_OPTION_FORMATTING_RAW_IDS},
|
2020-09-07 04:35:47 -07:00
|
|
|
};
|
|
|
|
char *tok;
|
|
|
|
|
|
|
|
for (tok = strtok(arg, ","); tok; tok = strtok(NULL, ","))
|
|
|
|
{
|
2020-09-21 09:18:31 -07:00
|
|
|
bool set = true;
|
2020-09-07 04:35:47 -07:00
|
|
|
unsigned int i;
|
2020-09-21 09:18:31 -07:00
|
|
|
|
|
|
|
if (*tok == '-')
|
|
|
|
{
|
|
|
|
set = false;
|
|
|
|
++tok;
|
|
|
|
}
|
|
|
|
else if (*tok == '+')
|
|
|
|
{
|
|
|
|
++tok;
|
|
|
|
}
|
|
|
|
|
2020-09-07 04:35:47 -07:00
|
|
|
for (i = 0; i < ARRAY_SIZE(opts); ++i)
|
|
|
|
{
|
|
|
|
if (!strcmp(tok, opts[i].name))
|
|
|
|
{
|
2020-09-21 09:18:31 -07:00
|
|
|
if (set)
|
|
|
|
*formatting |= opts[i].value;
|
|
|
|
else
|
|
|
|
*formatting &= ~opts[i].value;
|
2020-09-28 06:30:17 -07:00
|
|
|
if (opts[i].value == VKD3D_SHADER_COMPILE_OPTION_FORMATTING_COLOUR)
|
|
|
|
*colour = true;
|
2020-09-07 04:35:47 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == ARRAY_SIZE(opts))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Invalid formatting '%s' specified.\n", tok);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-10 05:13:59 -07:00
|
|
|
static enum vkd3d_shader_source_type parse_source_type(const char *source)
|
|
|
|
{
|
2020-08-11 04:06:36 -07:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!strcmp(source, "none"))
|
2020-07-10 05:13:59 -07:00
|
|
|
return VKD3D_SHADER_SOURCE_DXBC_TPF;
|
|
|
|
|
2020-08-11 04:06:36 -07:00
|
|
|
for (i = 0; i < ARRAY_SIZE(source_type_info); ++i)
|
|
|
|
{
|
|
|
|
if (!strcmp(source, source_type_info[i].name))
|
|
|
|
return source_type_info[i].type;
|
|
|
|
}
|
|
|
|
|
2020-07-10 05:13:59 -07:00
|
|
|
return VKD3D_SHADER_SOURCE_NONE;
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:06:38 -07:00
|
|
|
static enum vkd3d_shader_target_type parse_target_type(const char *target)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(target_type_info); ++i)
|
|
|
|
{
|
|
|
|
if (!strcmp(target, target_type_info[i].name))
|
|
|
|
return target_type_info[i].type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return VKD3D_SHADER_TARGET_NONE;
|
|
|
|
}
|
|
|
|
|
2020-09-07 04:35:44 -07:00
|
|
|
static const struct source_type_info *get_source_type_info(enum vkd3d_shader_source_type type)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(source_type_info); ++i)
|
|
|
|
if (type == source_type_info[i].type)
|
|
|
|
return &source_type_info[i];
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-07 04:35:43 -07:00
|
|
|
static const struct target_type_info *get_target_type_info(enum vkd3d_shader_target_type type)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(target_type_info); ++i)
|
|
|
|
if (type == target_type_info[i].type)
|
|
|
|
return &target_type_info[i];
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-07 04:35:45 -07:00
|
|
|
static bool validate_target_type(
|
|
|
|
enum vkd3d_shader_source_type source_type,
|
|
|
|
enum vkd3d_shader_target_type target_type)
|
|
|
|
{
|
|
|
|
unsigned int i, count;
|
|
|
|
const enum vkd3d_shader_target_type *supported_types =
|
|
|
|
vkd3d_shader_get_supported_target_types(source_type, &count);
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(target_type_info); ++i)
|
|
|
|
{
|
|
|
|
if (target_type == supported_types[i])
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static bool parse_command_line(int argc, char **argv, struct options *options)
|
|
|
|
{
|
2020-08-04 03:13:32 -07:00
|
|
|
enum vkd3d_shader_compile_option_buffer_uav buffer_uav;
|
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-08-11 04:06:36 -07:00
|
|
|
{"help", no_argument, NULL, OPTION_HELP},
|
|
|
|
{"buffer-uav", required_argument, NULL, OPTION_BUFFER_UAV},
|
|
|
|
{"output", required_argument, NULL, OPTION_OUTPUT},
|
2020-09-07 04:35:47 -07:00
|
|
|
{"formatting", required_argument, NULL, OPTION_TEXT_FORMATTING},
|
2020-08-11 04:06:36 -07:00
|
|
|
{"print-source-types", no_argument, NULL, OPTION_PRINT_SOURCE_TYPES},
|
2020-08-11 04:06:37 -07:00
|
|
|
{"print-target-types", no_argument, NULL, OPTION_PRINT_TARGET_TYPES},
|
2020-08-11 04:06:36 -07:00
|
|
|
{"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;
|
2020-08-11 04:06:38 -07:00
|
|
|
options->target_type = VKD3D_SHADER_TARGET_SPIRV_BINARY;
|
2020-09-28 06:30:17 -07:00
|
|
|
options->formatting = VKD3D_SHADER_COMPILE_OPTION_FORMATTING_INDENT
|
|
|
|
| VKD3D_SHADER_COMPILE_OPTION_FORMATTING_HEADER;
|
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-08-11 04:06:38 -07:00
|
|
|
if ((option = getopt_long(argc, argv, "b: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-08-11 04:06:38 -07:00
|
|
|
case 'b':
|
|
|
|
if ((options->target_type = parse_target_type(optarg)) == VKD3D_SHADER_TARGET_NONE)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Invalid target type '%s' specified.\n", optarg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-08-04 03:13:32 -07:00
|
|
|
case OPTION_BUFFER_UAV:
|
|
|
|
if (!parse_buffer_uav(&buffer_uav, optarg))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Invalid buffer UAV type '%s' specified.\n", optarg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
add_compile_option(options, VKD3D_SHADER_COMPILE_OPTION_BUFFER_UAV, buffer_uav);
|
|
|
|
break;
|
|
|
|
|
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;
|
|
|
|
|
2020-09-07 04:35:47 -07:00
|
|
|
case OPTION_TEXT_FORMATTING:
|
2020-09-28 06:30:17 -07:00
|
|
|
if (!parse_formatting(&options->formatting, &options->explicit_colour, optarg))
|
2020-09-07 04:35:47 -07:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
2020-08-11 04:06:36 -07:00
|
|
|
case OPTION_PRINT_SOURCE_TYPES:
|
|
|
|
options->print_source_types = true;
|
|
|
|
return true;
|
|
|
|
|
2020-08-11 04:06:37 -07:00
|
|
|
case OPTION_PRINT_TARGET_TYPES:
|
|
|
|
options->print_target_types = true;
|
|
|
|
break;
|
|
|
|
|
2020-07-02 06:43:13 -07:00
|
|
|
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-09-07 04:35:45 -07:00
|
|
|
if (!validate_target_type(options->source_type, options->target_type))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Target type '%s' is invalid for source type '%s'.\n",
|
|
|
|
get_target_type_info(options->target_type)->name,
|
|
|
|
get_source_type_info(options->source_type)->name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:06:37 -07:00
|
|
|
if (options->print_target_types)
|
|
|
|
return true;
|
|
|
|
|
2020-08-13 06:07:39 -07:00
|
|
|
if (optind < argc)
|
|
|
|
options->filename = argv[argc - 1];
|
2020-07-02 06:43:13 -07:00
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:06:36 -07:00
|
|
|
static void print_source_types(void)
|
|
|
|
{
|
|
|
|
const enum vkd3d_shader_source_type *source_types;
|
2020-09-07 04:35:44 -07:00
|
|
|
unsigned int count, i;
|
2020-08-11 04:06:36 -07:00
|
|
|
|
|
|
|
source_types = vkd3d_shader_get_supported_source_types(&count);
|
|
|
|
fputs("Supported source types:\n", stdout);
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
{
|
2020-09-07 04:35:44 -07:00
|
|
|
const struct source_type_info *type = get_source_type_info(source_types[i]);
|
|
|
|
if (type)
|
2020-09-07 04:35:46 -07:00
|
|
|
fprintf(stdout, " %-12s %s", type->name, type->description);
|
2020-08-11 04:06:36 -07:00
|
|
|
}
|
2020-09-07 04:35:44 -07:00
|
|
|
|
2020-08-11 04:06:36 -07:00
|
|
|
}
|
|
|
|
|
2020-08-11 04:06:37 -07:00
|
|
|
static void print_target_types(enum vkd3d_shader_source_type source_type)
|
|
|
|
{
|
|
|
|
const enum vkd3d_shader_target_type *target_types;
|
2020-09-07 04:35:44 -07:00
|
|
|
const char *source_type_name = get_source_type_info(source_type)->name;
|
2020-09-07 04:35:43 -07:00
|
|
|
unsigned int count, i;
|
2020-08-11 04:06:37 -07:00
|
|
|
|
|
|
|
target_types = vkd3d_shader_get_supported_target_types(source_type, &count);
|
|
|
|
fprintf(stdout, "Supported target types for source type '%s':\n", source_type_name);
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
{
|
2020-09-07 04:35:43 -07:00
|
|
|
const struct target_type_info *type = get_target_type_info(target_types[i]);
|
|
|
|
if (type)
|
2020-09-07 04:35:46 -07:00
|
|
|
fprintf(stdout, " %-12s %s", type->name, type->description);
|
2020-08-11 04:06:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 06:07:39 -07:00
|
|
|
static FILE *open_input(const char *filename, bool *close)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
*close = false;
|
|
|
|
|
|
|
|
if (!filename || !strcmp(filename, "-"))
|
|
|
|
return stdin;
|
|
|
|
|
|
|
|
if (!(f = fopen(filename, "rb")))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to open '%s' for reading.\n", filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*close = true;
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2020-08-13 06:07:38 -07:00
|
|
|
static FILE *open_output(const char *filename, bool *close)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
*close = false;
|
|
|
|
|
|
|
|
if (!filename || !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;
|
|
|
|
}
|
|
|
|
|
2020-09-28 06:30:17 -07:00
|
|
|
static bool has_colour(FILE *f)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_NCURSES
|
|
|
|
bool supported;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!isatty(fileno(f)))
|
|
|
|
return false;
|
|
|
|
setupterm(NULL, fileno(f), &ret);
|
|
|
|
if (ret != 1)
|
|
|
|
return false;
|
|
|
|
supported = !!tigetstr("setaf");
|
|
|
|
del_curterm(cur_term);
|
|
|
|
|
|
|
|
return supported;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2020-08-13 06:07:39 -07:00
|
|
|
bool close_input = false, close_output = false;
|
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-08-13 06:07:39 -07:00
|
|
|
FILE *input, *output;
|
2020-07-23 02:11:25 -07:00
|
|
|
char *messages;
|
2020-08-13 06:07:38 -07:00
|
|
|
int fail = 1;
|
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)
|
|
|
|
{
|
2020-08-06 01:53:46 -07:00
|
|
|
const char *version = vkd3d_shader_get_version(NULL, NULL);
|
|
|
|
|
|
|
|
fprintf(stdout, "vkd3d shader compiler version " PACKAGE_VERSION " using %s\n", version);
|
2020-07-02 06:43:15 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:06:36 -07:00
|
|
|
if (options.print_source_types)
|
|
|
|
{
|
|
|
|
print_source_types();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-11 04:06:37 -07:00
|
|
|
if (options.print_target_types)
|
|
|
|
{
|
|
|
|
print_target_types(options.source_type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-13 06:07:39 -07:00
|
|
|
if (!(input = open_input(options.filename, &close_input)))
|
|
|
|
goto done;
|
|
|
|
|
2020-09-28 20:41:07 -07:00
|
|
|
if (!options.filename && get_source_type_info(options.source_type)->is_binary && isatty(fileno(input)))
|
2020-08-13 06:07:39 -07:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Input is a tty and input format is binary, exiting.\n"
|
|
|
|
"If this is really what you intended, specify the input explicitly.\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2020-08-13 06:07:38 -07:00
|
|
|
if (!(output = open_output(options.output_filename, &close_output)))
|
|
|
|
goto done;
|
|
|
|
|
2020-09-07 04:35:46 -07:00
|
|
|
if (!options.output_filename && get_target_type_info(options.target_type)->is_binary
|
|
|
|
&& isatty(fileno(output)))
|
2020-08-13 06:07:38 -07:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Output is a tty and output format is binary, exiting.\n"
|
|
|
|
"If this is really what you intended, specify the output explicitly.\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2020-09-28 06:30:17 -07:00
|
|
|
if (!options.explicit_colour && has_colour(output))
|
|
|
|
options.formatting |= VKD3D_SHADER_COMPILE_OPTION_FORMATTING_COLOUR;
|
|
|
|
add_compile_option(&options, VKD3D_SHADER_COMPILE_OPTION_FORMATTING, options.formatting);
|
|
|
|
|
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-08-11 04:06:38 -07:00
|
|
|
info.target_type = options.target_type;
|
2020-06-19 04:43:35 -07:00
|
|
|
info.options = options.compile_options;
|
|
|
|
info.option_count = options.compile_option_count;
|
2020-07-23 02:11:25 -07:00
|
|
|
info.log_level = VKD3D_SHADER_LOG_INFO;
|
|
|
|
info.source_name = options.filename;
|
2020-06-19 04:43:32 -07:00
|
|
|
|
2020-08-13 06:07:39 -07:00
|
|
|
if (!read_shader(&info.source, input))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2020-08-13 06:07:38 -07:00
|
|
|
fprintf(stderr, "Failed to read input shader.\n");
|
|
|
|
goto done;
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
2020-07-23 02:11:25 -07:00
|
|
|
ret = vkd3d_shader_compile(&info, &spirv, &messages);
|
|
|
|
if (messages)
|
|
|
|
fputs(messages, stderr);
|
|
|
|
vkd3d_shader_free_messages(messages);
|
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-08-13 06:07:38 -07:00
|
|
|
fprintf(stderr, "Failed to compile shader, ret %d.\n", ret);
|
|
|
|
goto done;
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
2020-08-13 06:07:38 -07:00
|
|
|
if (!write_shader(&spirv, output))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to write output shader.\n");
|
|
|
|
vkd3d_shader_free_shader_code(&spirv);
|
|
|
|
goto done;
|
|
|
|
}
|
2017-06-16 13:38:21 -07:00
|
|
|
|
2020-08-13 06:07:38 -07:00
|
|
|
fail = 0;
|
2017-06-16 13:38:21 -07:00
|
|
|
vkd3d_shader_free_shader_code(&spirv);
|
2020-08-13 06:07:38 -07:00
|
|
|
done:
|
|
|
|
if (close_output)
|
|
|
|
fclose(output);
|
2020-08-13 06:07:39 -07:00
|
|
|
if (close_input)
|
|
|
|
fclose(input);
|
2020-08-13 06:07:38 -07:00
|
|
|
return fail;
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|