D3D12: dump disassembled DXBC when dump_shaders is enabled

Useful for debugging FXC bugs.

Bug: dawn:1162
Change-Id: If4fc7d0f16370d15235a6c872309fe1536dd9edc
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/66900
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Austin Eng <enga@chromium.org>
diff --git a/src/dawn_native/d3d12/PlatformFunctions.cpp b/src/dawn_native/d3d12/PlatformFunctions.cpp
index 8e0654e..27e6564 100644
--- a/src/dawn_native/d3d12/PlatformFunctions.cpp
+++ b/src/dawn_native/d3d12/PlatformFunctions.cpp
@@ -233,10 +233,12 @@
     MaybeError PlatformFunctions::LoadFXCompiler() {
 #if DAWN_PLATFORM_WINUWP
         d3dCompile = &D3DCompile;
+        d3dDisassemble = &D3DDisassemble;
 #else
         std::string error;
         if (!mFXCompilerLib.Open("d3dcompiler_47.dll", &error) ||
-            !mFXCompilerLib.GetProc(&d3dCompile, "D3DCompile", &error)) {
+            !mFXCompilerLib.GetProc(&d3dCompile, "D3DCompile", &error) ||
+            !mFXCompilerLib.GetProc(&d3dDisassemble, "D3DDisassemble", &error)) {
             return DAWN_INTERNAL_ERROR(error.c_str());
         }
 #endif
diff --git a/src/dawn_native/d3d12/PlatformFunctions.h b/src/dawn_native/d3d12/PlatformFunctions.h
index a6146ae..cd88217 100644
--- a/src/dawn_native/d3d12/PlatformFunctions.h
+++ b/src/dawn_native/d3d12/PlatformFunctions.h
@@ -65,6 +65,7 @@
 
         // Functions from d3d3compiler.dll
         pD3DCompile d3dCompile = nullptr;
+        pD3DDisassemble d3dDisassemble = nullptr;
 
         // Functions from WinPixEventRuntime.dll
         using PFN_PIX_END_EVENT_ON_COMMAND_LIST =
diff --git a/src/dawn_native/d3d12/ShaderModuleD3D12.cpp b/src/dawn_native/d3d12/ShaderModuleD3D12.cpp
index e7395cf..2dafc96 100644
--- a/src/dawn_native/d3d12/ShaderModuleD3D12.cpp
+++ b/src/dawn_native/d3d12/ShaderModuleD3D12.cpp
@@ -461,6 +461,22 @@
                     break;
             }
 
+            if (dumpShaders && request.compiler == ShaderCompilationRequest::Compiler::FXC) {
+                std::ostringstream dumpedMsg;
+                dumpedMsg << "/* Dumped disassembled DXBC */" << std::endl;
+
+                ComPtr<ID3DBlob> disassembly;
+                if (FAILED(functions->d3dDisassemble(
+                        compiledShader->compiledFXCShader->GetBufferPointer(),
+                        compiledShader->compiledFXCShader->GetBufferSize(), 0, nullptr,
+                        &disassembly))) {
+                    dumpedMsg << "D3D disassemble failed" << std::endl;
+                } else {
+                    dumpedMsg << reinterpret_cast<const char*>(disassembly->GetBufferPointer());
+                }
+                DumpShadersEmitLog(WGPULoggingType_Info, dumpedMsg.str().c_str());
+            }
+
             return {};
         }