瀏覽代碼

Fix compile shaders with relative path and include files

Fixes #991
Helena Kotas 7 年之前
父節點
當前提交
e4c5158b98

+ 3 - 0
tools/clang/test/HLSL/include-declarations.h

@@ -0,0 +1,3 @@
+struct foo {
+  float4x4 MPV;
+} CB;

+ 4 - 0
tools/clang/test/HLSL/include-main.hlsl

@@ -0,0 +1,4 @@
+#include "include-declarations.h"
+
+void main() {
+}

+ 24 - 21
tools/clang/tools/dxcompiler/dxcfilesystem.cpp

@@ -501,32 +501,35 @@ public:
   }
   __override DWORD GetFileAttributesW(_In_ LPCWSTR lpFileName) throw() {
     DXTRACE_FMT_APIFS("DxcArgsFileSystem::GetFileAttributesW %S\n", lpFileName);
-    std::wstring FileNameStore;
-    MakeAbsoluteOrCurDirRelativeW(lpFileName, FileNameStore);
-    size_t sourceNameLen = wcslen(m_pSourceName);
-    size_t fileNameLen = wcslen(lpFileName);
-
-    // Check for a match to the source.
-    if (fileNameLen == sourceNameLen) {
-      if (0 == wcsncmp(m_pSourceName, lpFileName, fileNameLen)) {
-        return FILE_ATTRIBUTE_NORMAL;
+    DWORD findError;
+    {
+      std::wstring FileNameStore; // The destructor might release and set LastError to success.
+      MakeAbsoluteOrCurDirRelativeW(lpFileName, FileNameStore);
+      size_t sourceNameLen = wcslen(m_pSourceName);
+      size_t fileNameLen = wcslen(lpFileName);
+
+      // Check for a match to the source.
+      if (fileNameLen == sourceNameLen) {
+        if (0 == wcsncmp(m_pSourceName, lpFileName, fileNameLen)) {
+          return FILE_ATTRIBUTE_NORMAL;
+        }
       }
-    }
 
-    // Check for a perfect match to the output.
-    if (m_pOutputStreamName != nullptr &&
+      // Check for a perfect match to the output.
+      if (m_pOutputStreamName != nullptr &&
         0 == wcscmp(m_pOutputStreamName, lpFileName)) {
-      return FILE_ATTRIBUTE_NORMAL;
-    }
+        return FILE_ATTRIBUTE_NORMAL;
+      }
 
-    if (TryFindDirHandle(lpFileName) != INVALID_HANDLE_VALUE) {
-      return FILE_ATTRIBUTE_DIRECTORY;
-    }
+      if (TryFindDirHandle(lpFileName) != INVALID_HANDLE_VALUE) {
+        return FILE_ATTRIBUTE_DIRECTORY;
+      }
 
-    size_t includedIndex;
-    DWORD findError = TryFindOrOpen(lpFileName, includedIndex);
-    if (findError == ERROR_SUCCESS) {
-      return FILE_ATTRIBUTE_NORMAL;
+      size_t includedIndex;
+      findError = TryFindOrOpen(lpFileName, includedIndex);
+      if (findError == ERROR_SUCCESS) {
+        return FILE_ATTRIBUTE_NORMAL;
+      }
     }
 
     SetLastError(findError);

+ 12 - 0
utils/hct/hcttestcmds.cmd

@@ -604,6 +604,18 @@ if %errorlevel% equ 0 (
   exit /b 1
 )
 
+echo Test file with relative path and include
+mkdir subfolder 2>nul
+mkdir inc       2>nul
+copy "%2"\include-main.hlsl subfolder >nul
+copy "%2"\include-declarations.h inc  >nul
+dxc.exe -Tps_6_0 -I inc subfolder\include-main.hlsl >nul
+if %errorlevel% neq 0 (
+  echo Failed to compile subfolder\include-main.hlsl
+  call :cleanup 2>nul
+  exit /b 1
+)
+
 rem SPIR-V Change Starts
 echo Smoke test for SPIR-V CodeGen ...
 set spirv_smoke_success=0