Selaa lähdekoodia

Add D3DPreprocess to d3dcompiler_dxc_bridge. (#1681)

* Add D3DPreprocess to d3dcompiler_dxc_bridge.

* Avoid throw in D3DCompile and D3DPreprocess.
Xiang Li 6 vuotta sitten
vanhempi
commit
7ecfd0ad2a
2 muutettua tiedostoa jossa 95 lisäystä ja 1 poistoa
  1. 94 1
      tools/clang/tools/d3dcomp/d3dcomp.cpp
  2. 1 0
      tools/clang/tools/d3dcomp/d3dcomp.def

+ 94 - 1
tools/clang/tools/d3dcomp/d3dcomp.cpp

@@ -146,7 +146,7 @@ HRESULT WINAPI BridgeD3DCompile(LPCVOID pSrcData, SIZE_T SrcDataSize,
 
   // Until we actually wrap the include handler, fail if there's a user-supplied handler.
   if (D3D_COMPILE_STANDARD_FILE_INCLUDE == pInclude) {
-    IFT(library->CreateIncludeHandler(&includeHandler));
+    IFR(library->CreateIncludeHandler(&includeHandler));
   } else if (pInclude) {
     return E_INVALIDARG;
   }
@@ -271,6 +271,99 @@ BridgeReadFileToBlob(_In_ LPCWSTR pFileName,
   return S_OK;
 }
 
+HRESULT PreprocessFromBlob(IDxcBlobEncoding *pSource, LPCWSTR pSourceName,
+                           const D3D_SHADER_MACRO *pDefines,
+                           IDxcIncludeHandler *pInclude, ID3DBlob **ppCodeText,
+                           ID3DBlob **ppErrorMsgs) {
+  CComPtr<IDxcCompiler> compiler;
+  CComPtr<IDxcOperationResult> operationResult;
+  HRESULT hr;
+
+  try {
+    std::vector<std::wstring> defineValues;
+    std::vector<DxcDefine> defines;
+    if (pDefines) {
+      CONST D3D_SHADER_MACRO *pCursor = pDefines;
+
+      // Convert to UTF-16.
+      while (pCursor->Name) {
+        defineValues.push_back(std::wstring(CA2W(pCursor->Name)));
+        if (pCursor->Definition)
+          defineValues.push_back(std::wstring(CA2W(pCursor->Definition)));
+        else
+          defineValues.push_back(std::wstring());
+        ++pCursor;
+      }
+
+      // Build up array.
+      pCursor = pDefines;
+      size_t i = 0;
+      while (pCursor->Name) {
+        defines.push_back(
+            DxcDefine{defineValues[i++].c_str(), defineValues[i++].c_str()});
+        ++pCursor;
+      }
+    }
+
+    std::vector<LPCWSTR> arguments;
+
+    IFR(CreateCompiler(&compiler));
+    IFR(compiler->Preprocess(pSource, pSourceName, arguments.data(),
+                             (UINT)arguments.size(), defines.data(),
+                             (UINT)defines.size(), pInclude, &operationResult));
+  } catch (const std::bad_alloc &) {
+    return E_OUTOFMEMORY;
+  } catch (const CAtlException &err) {
+    return err.m_hr;
+  }
+
+  operationResult->GetStatus(&hr);
+  if (SUCCEEDED(hr)) {
+    return operationResult->GetResult((IDxcBlob **)ppCodeText);
+  } else {
+    if (ppErrorMsgs)
+      operationResult->GetErrorBuffer((IDxcBlobEncoding **)ppErrorMsgs);
+    return hr;
+  }
+}
+
+HRESULT WINAPI BridgeD3DPreprocess(_In_reads_bytes_(SrcDataSize) LPCVOID pSrcData,
+                             _In_ SIZE_T SrcDataSize,
+                             _In_opt_ LPCSTR pSourceName,
+                             _In_opt_ const D3D_SHADER_MACRO *pDefines,
+                             _In_opt_ ID3DInclude *pInclude,
+                             _Out_ ID3DBlob **ppCodeText,
+                             _Out_opt_ ID3DBlob **ppErrorMsgs) {
+  CComPtr<IDxcLibrary> library;
+  CComPtr<IDxcBlobEncoding> source;
+  CComPtr<IDxcIncludeHandler> includeHandler;
+
+  *ppCodeText = nullptr;
+  if (ppErrorMsgs != nullptr)
+    *ppErrorMsgs = nullptr;
+
+  IFR(CreateLibrary(&library));
+  IFR(library->CreateBlobWithEncodingFromPinned((LPBYTE)pSrcData, SrcDataSize,
+                                                CP_ACP, &source));
+
+  // Until we actually wrap the include handler, fail if there's a user-supplied
+  // handler.
+  if (D3D_COMPILE_STANDARD_FILE_INCLUDE == pInclude) {
+    IFR(library->CreateIncludeHandler(&includeHandler));
+  } else if (pInclude) {
+    return E_INVALIDARG;
+  }
+
+  try {
+    CA2W pFileName(pSourceName);
+    return PreprocessFromBlob(source, pFileName, pDefines, includeHandler,
+                              ppCodeText, ppErrorMsgs);
+  } catch (const std::bad_alloc &) {
+    return E_OUTOFMEMORY;
+  } catch (const CAtlException &err) {
+    return err.m_hr;
+  }
+}
 
 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID) {
   BOOL result = TRUE;

+ 1 - 0
tools/clang/tools/d3dcomp/d3dcomp.def

@@ -7,3 +7,4 @@ EXPORTS
     D3DDisassemble=BridgeD3DDisassemble
     D3DReflect=BridgeD3DReflect
     D3DReadFileToBlob=BridgeReadFileToBlob
+    D3DPreprocess=BridgeD3DPreprocess