[PATCH vkd3d 3/3] vkd3d-compiler: Try to detect the source type by inspecting the file.

Zebediah Figura zfigura at codeweavers.com
Sat Dec 25 22:05:46 CST 2021


Signed-off-by: Zebediah Figura <zfigura at codeweavers.com>
---
 programs/vkd3d-compiler/main.c | 68 +++++++++++++++++++++++++++-------
 1 file changed, 54 insertions(+), 14 deletions(-)

diff --git a/programs/vkd3d-compiler/main.c b/programs/vkd3d-compiler/main.c
index 5607c29d9..55f82c114 100644
--- a/programs/vkd3d-compiler/main.c
+++ b/programs/vkd3d-compiler/main.c
@@ -20,6 +20,7 @@
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE
 #endif
+#include <assert.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -49,6 +50,11 @@ enum
     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
 {
     enum vkd3d_shader_source_type type;
@@ -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)
         options->filename = argv[argc - 1];
 
@@ -640,6 +641,54 @@ int main(int argc, char **argv)
         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, info.source.size);
+            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)
+    {
+        switch (options.source_type)
+        {
+            case VKD3D_SHADER_SOURCE_D3D_BYTECODE:
+            case VKD3D_SHADER_SOURCE_DXBC_TPF:
+                options.target_type = VKD3D_SHADER_TARGET_SPIRV_BINARY;
+                break;
+
+            case VKD3D_SHADER_SOURCE_HLSL:
+                options.target_type = VKD3D_SHADER_TARGET_DXBC_TPF;
+                break;
+
+            default:
+                assert(0);
+                break;
+        }
+    }
+
     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",
@@ -654,9 +703,6 @@ int main(int argc, char **argv)
         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)))
     {
         fprintf(stderr, "Input is a tty and input format is binary, exiting.\n"
@@ -706,12 +752,6 @@ int main(int argc, char **argv)
     spirv_target_info.entry_point = options.entry_point;
     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)
         ret = vkd3d_shader_preprocess(&info, &output_code, &messages);
     else
-- 
2.34.1




More information about the wine-devel mailing list