Files
BlueRoseNote/03-UnrealEngine/Rendering/Debug/新的DXBC&DXIL 反汇编:HLSL Decompiler 工具.md

47 lines
4.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Untitled
date: 2025-07-28 10:06:02
excerpt:
tags:
rating: ⭐
---
# 前言
- DXBC/DXIL 反汇编HLSL Decompiler:https://zhuanlan.zhihu.com/p/1927111489627599176
# 正文
## 项目背景
从 UE5 和 DirectX12 的时代开始,[Shader Model 6.0](https://zhida.zhihu.com/search?content_id=260234833&content_type=Article&match_order=1&q=Shader+Model+6.0&zhida_source=entity)+ 和它的新编译格式 DXIL已经接替老旧的 [DXBC](https://zhida.zhihu.com/search?content_id=260234833&content_type=Article&match_order=1&q=DXBC&zhida_source=entity) 成为新时代的标配。这意味着,当我们截帧分析基于 UE5 + DX12 的游戏时,看到的 Shader 代码不再是熟悉的 DXBC而是 DXIL 了。
对于热衷于深挖渲染管线一探究竟的逆向爱好者来说,这带来了一个痛点:我们急需一个能将 DXIL 反编译回可读性强的 HLSL 源码的工具。
## HLSL 反编译的历史
在 DXBC 时代Shader Model 5.1 及之前),开源项目 [3Dmigoto](https://link.zhihu.com/?target=https%3A//github.com/bo3b/3Dmigoto) 旗下的 [HLSLDecompiler](https://link.zhihu.com/?target=https%3A//github.com/bo3b/3Dmigoto/tree/master/HLSLDecompiler) 堪称神器。
它甚至被专门 Fork 出来([zxxyye/HLSLDecompiler](https://link.zhihu.com/?target=https%3A//github.com/zxxyye/HLSLDecompiler)),集成到 [RenderDoc](https://zhida.zhihu.com/search?content_id=260234833&content_type=Article&match_order=1&q=RenderDoc&zhida_source=entity) 中作为插件使用,并收获了 300+ Star 和 100+ Fork足见其受欢迎程度和逆向需求的旺盛。
## 现在与将来
然而,时代变了。[3Dmigoto](https://link.zhihu.com/?target=https%3A//github.com/bo3b/3Dmigoto) 项目本身已经陷入沉寂,其下的 [HLSLDecompiler](https://link.zhihu.com/?target=https%3A//github.com/bo3b/3Dmigoto/tree/master/HLSLDecompiler) 子模块更是多年未曾更新。
根据作者在 [Issue #358](https://link.zhihu.com/?target=https%3A//github.com/bo3b/3Dmigoto/issues/358) 中的说法,这个反编译器对 Shader Model 4.0 为止的 DXBC 代码具有良好的支持性,然而对于 Shader Model 5.0+ 的着色器代码已是捉襟见肘,并且大概率不会再有任何更新。
实际体验也印证了这点。很多 Shader Model 5.0+ DXBC 的基础指令(甚至包括 `numthreads`)它都翻译不出来,已经完全无法满足现代游戏分析的需求。
一言以蔽之,老工具已无法胜任 DXIL 和部分 DXBC 的反编译工作。为了解决这个痛点,我们需要打造一个全新的逆向分析工具:[HLSL-Decompiler](https://link.zhihu.com/?target=https%3A//github.com/YYadorigi/HLSL-Decompiler)。
它的目标很明确:高效、准确地将 DXBC / DXIL / [SPIR-V](https://zhida.zhihu.com/search?content_id=260234833&content_type=Article&match_order=1&q=SPIR-V&zhida_source=entity) 反编译回可读的 HLSL 源码,并且能无缝集成到 RenderDoc 中,真正做到「开箱即用」,接续前人未完成的使命。
## 项目简述
### 开源库
本项目基于如下开源库。它们权威,高质量,并且正活跃地持续维护。
- [DirectXShaderCompiler (DXC)](https://link.zhihu.com/?target=https%3A//github.com/microsoft/DirectXShaderCompiler/tree/main):微软官方提供的现代 HLSL 编译器。其中包括关键组件 [dxbc2dxil](https://link.zhihu.com/?target=https%3A//github.com/microsoft/DirectXShaderCompiler/tree/main/projects/dxilconv/tools/dxbc2dxil),将老式 DXBC 转换为 [LLVM IR](https://zhida.zhihu.com/search?content_id=260234833&content_type=Article&match_order=1&q=LLVM+IR&zhida_source=entity) 形式的 DXIL。
- [dxil-spirv](https://link.zhihu.com/?target=https%3A//github.com/HansKristian-Work/dxil-spirv/tree/master):将 DXIL 或其 LLVM IR 形式高效地转换为 SPIR-V。作者同时也是 [SPIRV-Cross](https://link.zhihu.com/?target=https%3A//github.com/KhronosGroup/SPIRV-Cross/tree/main) 的重要贡献者。
- [SPIRV-Cross](https://link.zhihu.com/?target=https%3A//github.com/KhronosGroup/SPIRV-Cross/tree/main)KhronosGroup 官方维护的 SPIR-V 反汇编工具,支持从 SPIR-V 到 HLSL 的转换。
### 工具链
该项目的核心就是巧妙地串联起上述库,形成一个高效的反编译流水线。原理如下图所示。
![](https://pic1.zhimg.com/v2-52e1214206c0ec9fcec5e6cf100f5040_1440w.jpg)
### 为什么你需要它?
- **分析 UE5 / DX12 游戏渲染管线**:轻松查看截帧得到的 DXIL Shader 源码。
- **逆向老旧 DXBC (SM5.1)**:老工具搞不定的,它能搞定。
- **研究 Vulkan Shader (SPIR-V)**:方便地将 SPIR-V 转回 HLSL 进行分析或移植。
- **RenderDoc 插件集成**:安装即用,分析体验丝滑流畅。
- **开源 & 活跃**:基于活跃项目构建,持续维护更新有保障。
### 项目地址
项目已开源在 GitHub[https://github.com/YYadorigi/HL](https://link.zhihu.com/?target=https%3A//github.com/YYadorigi/HLSL-Decompiler)