vkd3d-compiler: Try to detect the source type by inspecting the file.

Signed-off-by: Zebediah Figura <zfigura@codeweavers.com>
Signed-off-by: Giovanni Mascellani <gmascellani@codeweavers.com>
Signed-off-by: Henri Verbeet <hverbeet@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Zebediah Figura 2021-12-25 22:43:14 -06:00 committed by Alexandre Julliard
parent 47ea863535
commit 9fef9fe2bc

View File

@ -20,6 +20,7 @@
#ifndef _GNU_SOURCE #ifndef _GNU_SOURCE
#define _GNU_SOURCE #define _GNU_SOURCE
#endif #endif
#include <assert.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -49,26 +50,32 @@ enum
OPTION_TEXT_FORMATTING, OPTION_TEXT_FORMATTING,
}; };
#define MAKE_TAG(ch0, ch1, ch2, ch3) \
((DWORD)(ch0) | ((DWORD)(ch1) << 8) | \
((DWORD)(ch2) << 16) | ((DWORD)(ch3) << 24 ))
#define TAG_DXBC MAKE_TAG('D', 'X', 'B', 'C')
static const struct source_type_info static const struct source_type_info
{ {
enum vkd3d_shader_source_type type; enum vkd3d_shader_source_type type;
const char *name; const char *name;
const char *description; const char *description;
bool is_binary; bool is_binary;
enum vkd3d_shader_target_type default_target_type;
} }
source_type_info[] = source_type_info[] =
{ {
{VKD3D_SHADER_SOURCE_DXBC_TPF, {VKD3D_SHADER_SOURCE_DXBC_TPF,
"dxbc-tpf", "A 'Tokenized Program Format' shader embedded in a DXBC container.\n" "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", " This is the format used for Direct3D shader model 4 and 5 shaders.\n",
true}, true, VKD3D_SHADER_TARGET_SPIRV_BINARY},
{VKD3D_SHADER_SOURCE_HLSL, {VKD3D_SHADER_SOURCE_HLSL,
"hlsl", "High Level Shader Language source code.\n", "hlsl", "High Level Shader Language source code.\n",
false}, false, VKD3D_SHADER_TARGET_DXBC_TPF},
{VKD3D_SHADER_SOURCE_D3D_BYTECODE, {VKD3D_SHADER_SOURCE_D3D_BYTECODE,
"d3dbc", "Legacy Direct3D byte-code.\n" "d3dbc", "Legacy Direct3D byte-code.\n"
" This is the format used for Direct3D shader model 1, 2, and 3 shaders.\n", " This is the format used for Direct3D shader model 1, 2, and 3 shaders.\n",
true}, true, VKD3D_SHADER_TARGET_SPIRV_BINARY},
}; };
static const struct target_type_info static const struct target_type_info
@ -166,8 +173,8 @@ static void print_usage(const char *program_name)
"Options:\n" "Options:\n"
" -h, --help Display this information and exit.\n" " -h, --help Display this information and exit.\n"
" -b <type> Specify the target type. Use --print-target-types to\n" " -b <type> Specify the target type. Use --print-target-types to\n"
" list valid target types for a given source type. The\n" " list the valid and default target types for a given\n"
" default target type is 'spirv-binary'.\n" " source type.\n"
" --buffer-uav=<type> Specify the buffer type to use for buffer UAV bindings.\n" " --buffer-uav=<type> Specify the buffer type to use for buffer UAV bindings.\n"
" Valid values are 'buffer-texture' (default) and\n" " Valid values are 'buffer-texture' (default) and\n"
" 'storage-buffer'.\n" " 'storage-buffer'.\n"
@ -189,8 +196,7 @@ static void print_usage(const char *program_name)
" -V, --version Display version information and exit.\n" " -V, --version Display version information and exit.\n"
" -x <type> Specify the type of the source. Use --print-source-types\n" " -x <type> Specify the type of the source. Use --print-source-types\n"
" to list valid source types. The default source type is\n" " to list valid source types. The default source type is\n"
" 'dxbc-tpf' for compile operations, and 'hlsl' for\n" " automatically detected from the input shader.\n"
" preprocess operations.\n"
" -- Stop option processing. Any subsequent argument is\n" " -- Stop option processing. Any subsequent argument is\n"
" interpreted as a filename.\n" " interpreted as a filename.\n"
"\n" "\n"
@ -497,11 +503,6 @@ static bool parse_command_line(int argc, char **argv, struct options *options)
} }
} }
if (options->source_type == VKD3D_SHADER_SOURCE_NONE)
options->source_type = options->preprocess_only ? VKD3D_SHADER_SOURCE_HLSL : VKD3D_SHADER_SOURCE_DXBC_TPF;
if (options->target_type == VKD3D_SHADER_TARGET_NONE && !options->preprocess_only)
options->target_type = VKD3D_SHADER_TARGET_SPIRV_BINARY;
if (optind < argc) if (optind < argc)
options->filename = argv[argc - 1]; options->filename = argv[argc - 1];
@ -526,8 +527,9 @@ static void print_source_types(void)
static void print_target_types(enum vkd3d_shader_source_type source_type) static void print_target_types(enum vkd3d_shader_source_type source_type)
{ {
const struct source_type_info *source_info = get_source_type_info(source_type);
const enum vkd3d_shader_target_type *target_types; const enum vkd3d_shader_target_type *target_types;
const char *source_type_name = get_source_type_info(source_type)->name; const char *source_type_name = source_info->name;
unsigned int count, i; unsigned int count, i;
target_types = vkd3d_shader_get_supported_target_types(source_type, &count); target_types = vkd3d_shader_get_supported_target_types(source_type, &count);
@ -538,6 +540,7 @@ static void print_target_types(enum vkd3d_shader_source_type source_type)
if (type) if (type)
fprintf(stdout, " %-12s %s", type->name, type->description); fprintf(stdout, " %-12s %s", type->name, type->description);
} }
printf("The default target type is '%s'.\n", get_target_type_info(source_info->default_target_type)->name);
} }
static FILE *open_input(const char *filename, bool *close) static FILE *open_input(const char *filename, bool *close)
@ -636,10 +639,48 @@ int main(int argc, char **argv)
if (options.print_target_types) if (options.print_target_types)
{ {
if (options.source_type == VKD3D_SHADER_SOURCE_NONE)
{
fprintf(stderr, "--print-target-types requires an explicitly specified source type.\n");
return 1;
}
print_target_types(options.source_type); print_target_types(options.source_type);
return 0; return 0;
} }
if (!(input = open_input(options.filename, &close_input)))
goto done;
if (!read_shader(&info.source, input))
{
fprintf(stderr, "Failed to read input shader.\n");
goto done;
}
if (options.source_type == VKD3D_SHADER_SOURCE_NONE)
{
uint32_t token;
if (options.preprocess_only)
{
options.source_type = VKD3D_SHADER_SOURCE_HLSL;
}
else if (info.source.size >= sizeof(token))
{
memcpy(&token, info.source.code, sizeof(token));
if (token == TAG_DXBC)
options.source_type = VKD3D_SHADER_SOURCE_DXBC_TPF;
else if ((token & 0xfffe0000) == 0xfffe0000)
options.source_type = VKD3D_SHADER_SOURCE_D3D_BYTECODE;
else
options.source_type = VKD3D_SHADER_SOURCE_HLSL;
}
}
if (options.target_type == VKD3D_SHADER_TARGET_NONE && !options.preprocess_only)
options.target_type = get_source_type_info(options.source_type)->default_target_type;
if (!options.preprocess_only && !validate_target_type(options.source_type, options.target_type)) if (!options.preprocess_only && !validate_target_type(options.source_type, options.target_type))
{ {
fprintf(stderr, "Target type '%s' is invalid for source type '%s'.\n", fprintf(stderr, "Target type '%s' is invalid for source type '%s'.\n",
@ -654,9 +695,6 @@ int main(int argc, char **argv)
return 0; return 0;
} }
if (!(input = open_input(options.filename, &close_input)))
goto done;
if (!options.filename && get_source_type_info(options.source_type)->is_binary && isatty(fileno(input))) if (!options.filename && get_source_type_info(options.source_type)->is_binary && isatty(fileno(input)))
{ {
fprintf(stderr, "Input is a tty and input format is binary, exiting.\n" fprintf(stderr, "Input is a tty and input format is binary, exiting.\n"
@ -706,12 +744,6 @@ int main(int argc, char **argv)
spirv_target_info.entry_point = options.entry_point; spirv_target_info.entry_point = options.entry_point;
spirv_target_info.environment = VKD3D_SHADER_SPIRV_ENVIRONMENT_VULKAN_1_0; spirv_target_info.environment = VKD3D_SHADER_SPIRV_ENVIRONMENT_VULKAN_1_0;
if (!read_shader(&info.source, input))
{
fprintf(stderr, "Failed to read input shader.\n");
goto done;
}
if (options.preprocess_only) if (options.preprocess_only)
ret = vkd3d_shader_preprocess(&info, &output_code, &messages); ret = vkd3d_shader_preprocess(&info, &output_code, &messages);
else else