From e557296b08f1c43abd81472b21500c399e70c919 Mon Sep 17 00:00:00 2001
From: Zhaoming Jiang <zhaoming.jiang@intel.com>
Date: Tue, 8 Jun 2021 10:03:44 +0000
Subject: [PATCH] Dump translated HLSL shaders if the toggle enabled

Add a toggle named "dump_translated_shaders", and when this toggle
is enabled, translated HLSL shaders will be dumped and log via
Device::EmitLog in the kInfo logging level.

BUG: dawn:792
Change-Id: Ia5ccc96019c32b43aa315f080f80a26919b441e3
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/53580
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
---
 src/dawn_native/Toggles.cpp                 | 6 ++++++
 src/dawn_native/Toggles.h                   | 1 +
 src/dawn_native/d3d12/ShaderModuleD3D12.cpp | 6 ++++++
 3 files changed, 13 insertions(+)

diff --git a/src/dawn_native/Toggles.cpp b/src/dawn_native/Toggles.cpp
index efc1540b4f..81cec28982 100644
--- a/src/dawn_native/Toggles.cpp
+++ b/src/dawn_native/Toggles.cpp
@@ -189,6 +189,12 @@ namespace dawn_native {
               "This is useful to prevent a Chromium renderer process from successfully sending"
               "SPIR-V code to be compiled in the GPU process.",
               "https://crbug.com/1214923"}},
+            {Toggle::DumpTranslatedShaders,
+             {"dump_translated_shaders",
+              "Dump generated shaders for debug propose, dumped shaders will be log via "
+              "EmitLog, thus printed in Chrome console or consumed by user-defined callback "
+              "function.",
+              "https://crbug.com/dawn/792"}}
             // Dummy comment to separate the }} so it is clearer what to copy-paste to add a toggle.
         }};
     }  // anonymous namespace
diff --git a/src/dawn_native/Toggles.h b/src/dawn_native/Toggles.h
index f9dc6c0018..639ebb44e7 100644
--- a/src/dawn_native/Toggles.h
+++ b/src/dawn_native/Toggles.h
@@ -53,6 +53,7 @@ namespace dawn_native {
         UseTempBufferInSmallFormatTextureToTextureCopyFromGreaterToLessMipLevel,
         EmitHLSLDebugSymbols,
         DisallowSpirv,
+        DumpTranslatedShaders,
 
         EnumCount,
         InvalidEnum = EnumCount,
diff --git a/src/dawn_native/d3d12/ShaderModuleD3D12.cpp b/src/dawn_native/d3d12/ShaderModuleD3D12.cpp
index 2b7f3e947a..ed4727d2c3 100644
--- a/src/dawn_native/d3d12/ShaderModuleD3D12.cpp
+++ b/src/dawn_native/d3d12/ShaderModuleD3D12.cpp
@@ -393,6 +393,12 @@ namespace dawn_native { namespace d3d12 {
             entryPointName = "main";
         }
 
+        if (device->IsToggleEnabled(Toggle::DumpTranslatedShaders)) {
+            std::ostringstream dumpedMsg;
+            dumpedMsg << "/* Dumped generated HLSL */" << std::endl << hlslSource;
+            GetDevice()->EmitLog(WGPULoggingType_Info, dumpedMsg.str().c_str());
+        }
+
         // Use HLSL source as the input for the key since it does need to know about the pipeline
         // layout. The pipeline layout is only required if we key from WGSL: two different pipeline
         // layouts could be used to produce different shader blobs and the wrong shader blob could