# HLSL

**HLSL**（高级着色语言，High-Level Shading Language）是微软开发的一种着色语言，用于在 DirectX 图形 API 中编写 GPU 程序（即着色器）。它作为 DirectX 9.0 的关键组件推出，DirectX 9.0 于 2003 年 1 月 22 日公布，基于 C 编程语言。<sup>[1](https://news.microsoft.com/source/2003/01/22/directx-9-0-features-revolutionary-high-level-shader-language/)</sup> HLSL 代码在执行前被编译为 GPU 使用的着色器字节码，此后该语言从顶点和像素着色扩展到计算、光线追踪和机器学习工作负载。<sup>[2](https://github.com/microsoft/DirectXShaderCompiler/wiki/Shader-Model)</sup>

| 关键事实 | 详情 |
|---|---|
| 开发者与首发 | 由 Direct3D 的创造者微软开发；随 DirectX 9.0 发布，2003 年 1 月 22 日公布<sup>[1](https://news.microsoft.com/source/2003/01/22/directx-9-0-features-revolutionary-high-level-shader-language/)</sup> |
| 语言基础 | 基于 C；从 C 和 C++ 继承了相当一部分语言语义，但既不是两者的子集也不是超集<sup>[1](https://news.microsoft.com/source/2003/01/22/directx-9-0-features-revolutionary-high-level-shader-language/)</sup><sup> • </sup><sup>[3](https://microsoft.github.io/hlsl-specs/specs/3-Basic.html)</sup> |
| 版本管理 | 着色器模型是一种版本管理方式，标明语言新增了哪些特性；着色器模型 1 为第一代可编程管线引入了顶点和像素着色器<sup>[2](https://github.com/microsoft/DirectXShaderCompiler/wiki/Shader-Model)</sup> |
| 参考编译器 | DirectX 着色器编译器（DXC），LLVM/Clang 3.7 的一个分支，以及较旧的效应编译器（FXC）；两者并不完全一致<sup>[4](https://gitee.com/gamebaby_admin/DirectXShaderCompiler/wikis/pages/export?doc_id=1376284&type=pdf)</sup><sup> • </sup><sup>[5](https://releases.llvm.org/21.1.2/tools/clang/docs/HLSL/ExpectedDifferences.html)</sup> |
| Vulkan 支持 | 自 Vulkan 1.2 起，HLSL 被视为 Vulkan 的一等着色语言，通过 DXC 编译为 SPIR-V<sup>[6](https://www.khronos.org/blog/hlsl-first-class-vulkan-shading-language)</sup> |
| 当前着色器模型 | 着色器模型 6.9 新增了最多 1024 个元素的长向量类型，并在 HLSL 中暴露了 DXR 1.2 的特性，如着色器执行重排序<sup>[7](https://devblogs.microsoft.com/directx/shader-model-6-9-retail-and-more/)</sup> |

## 语言设计

HLSL 从 C 和 C++ 继承了相当一部分语言语义。其中一部分来自语言发展早期有意采用该语法，另一部分则是基于 Clang 的 DXC 实现的副作用。HLSL 规范草案指出，该语言既不是 C 或 C++ 的子集也不是超集，不能简单地用它们来描述。<sup>[3](https://microsoft.github.io/hlsl-specs/specs/3-Basic.html)</sup>

与 C 的若干差异是 GPU 执行环境的刻意结果。HLSL 不支持通过地址引用值：所有变量都是值类型，指针解引用运算符（`*`、`->`）和取地址运算符（`&`）均被禁止。<sup>[8](https://clang.llvm.org/docs/HLSL/HLSLSupport.html)</sup> 位移行为也偏离 C：移位计数按类型大小进行掩码，DXC 通过修改 [LLVM IR](https://www.edgepedia.org/zh/llvm-ir) 的语义来适应这一语义。<sup>[8](https://clang.llvm.org/docs/HLSL/HLSLSupport.html)</sup>

OpenGL 的着色语言 GLSL 使用内置输入输出变量，而 HLSL 改用语义（semantics）——附加在输入或输出上的字符串，描述变量的预期用途，以 `SV_` 为前缀（例如 `SV_Position` 表示裁剪空间位置）。着色器输入是入口点的显式参数，着色器显式返回其输出。<sup>[9](https://docs.vulkan.org/guide/latest/high_level_shader_language_comparison.html)</sup> 数据类型也与 GLSL 不同：HLSL 使用带数字后缀的基本类型，因此 GLSL 的 `vec4` 对应 HLSL 的 `float4`，`mat4` 对应 `float4x4`，且 HLSL 矩阵默认按列主序排列（除非另行指定），而 GLSL 矩阵按列主序排列。<sup>[9](https://docs.vulkan.org/guide/latest/high_level_shader_language_comparison.html)</sup><sup> • </sup><sup>[18](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-appendix-pre-pragma-pack-matrix)</sup>

该语言包含一个内置函数库，这些函数经过预先实现和测试，开发者可以放心使用，知道它们已经过调试且性能良好，此外还可以用 HLSL 自身语法编写用户自定义函数。<sup>[10](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-reference)</sup>

## 着色器模型

HLSL 着色器模型是一种版本管理方式，标明语言新增了哪些特性。每个级别让应用程序可以针对一组明确的功能进行开发，也让硬件和驱动开发者针对相同的描述提供支持。着色器模型 1 是首个为 DirectX 创建的模型，为第一代可编程管线实现引入了顶点和像素着色器；着色器模型 2 新增了内置函数并提高了寄存器和指令的限制。后续版本逐步引入新的处理阶段、放宽约束并添加功能的超集。<sup>[2](https://github.com/microsoft/DirectXShaderCompiler/wiki/Shader-Model)</sup> 编译器根据编译时提供的着色器模型验证每个顶点着色器、几何着色器（使用 Direct3D 10 时）和像素着色器中的代码。<sup>[10](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-reference)</sup>

**着色器模型 6.9。** 与 DirectX Agility SDK 1.619 和 DXC 1.9.2602.16 同步商业发布，着色器模型 6.9 引入了长向量（Long Vector），允许 HLSL 向量包含超过 4 个、最多 1024 个元素，并能逐元素加载、存储和操作；同时将此前可选的 16 位和 64 位着色器操作及波操作转为强制特性。它还将 HLSL 对两项 [DirectX 光线追踪](https://www.edgepedia.org/zh/directx-raytracing)（DXR）1.2 特性的支持从预览转为正式：不透明度微图（Opacity Micromaps），让硬件比 AnyHit 着色器调用更高效地处理 alpha 测试几何体；以及着色器执行重排序（SER），让应用程序着色器代码告知硬件如何发现光线间的一致性，以便排序实现更好的并行执行。<sup>[7](https://devblogs.microsoft.com/directx/shader-model-6-9-retail-and-more/)</sup> 微软报告称，在早期演示中启用 SER 后帧率提升高达 90%。<sup>[11](https://www.windowscentral.com/microsoft/windows/directx-update-shader-execution-reordering-ser-ray-tracing)</sup>

着色器模型 6.9 还引入了协作向量（Cooperative Vector），首次让开发者可以直接从 HLSL 访问硬件加速的向量-矩阵运算，从而支持神经渲染技术，如神经纹理压缩和神经辐射缓存，这些技术与传统着色内联执行。此后微软弃用了协作向量，转而采用统一矩阵-矩阵和向量-矩阵运算的设计，将在着色器模型 6.10 中作为 DirectX 线性代数的一部分推出。<sup>[12](https://devblogs.microsoft.com/directx/evolving-directx-for-the-ml-era-on-windows/)</sup><sup> • </sup><sup>[7](https://devblogs.microsoft.com/directx/shader-model-6-9-retail-and-more/)</sup>

## 编译器

HLSL 有两个参考编译器：DirectX 着色器编译器（DXC）和较旧的效应编译器（FXC）。两个参考编译器并不完全一致，已知存在许多分歧，超出 DXC GitHub 上跟踪的范围。<sup>[5](https://releases.llvm.org/21.1.2/tools/clang/docs/HLSL/ExpectedDifferences.html)</sup> DXC 是 LLVM/Clang 3.7 的分支，HLSL 社区已提议将 HLSL、DXIL 和 SPIR-V 支持添加到 LLVM 的上游仓库。<sup>[4](https://gitee.com/gamebaby_admin/DirectXShaderCompiler/wikis/pages/export?doc_id=1376284&type=pdf)</sup>

**Clang 作为未来编译器。** Clang HLSL 项目的长期目标是使 Clang 能够在所有支持的用例中替代 DXC，以高度源代码兼容性处理大多数现有 HLSL 程序。Clang 的实现按照语言规范草案编写，而非参照任一参考编译器，其目标是语言兼容 DXC 实现的 HLSL 2021。其代码生成设计还旨在与其他 GPU 图形目标（如 SPIR-V）共享实现，并可能扩展到其他 GPU 甚至 CPU 目标。<sup>[8](https://clang.llvm.org/docs/HLSL/HLSLSupport.html)</sup><sup> • </sup><sup>[5](https://releases.llvm.org/21.1.2/tools/clang/docs/HLSL/ExpectedDifferences.html)</sup>

**规范。** 微软维护着一个 HLSL 特性提案和语言规范的仓库，包括可通过 HTML 和 PDF 获取的 HLSL 规范草案，通过公开的特性提案流程开发。<sup>[13](https://github.com/microsoft/hlsl-specs)</sup>

## DirectX 之外的 HLSL

Vulkan 不接受人类可读的文本形式的着色器；它使用 SPIR-V，一种二进制中间表示，允许 GLSL 以外的着色语言，只要它们能生成适用于 Vulkan SPIR-V 环境的代码。HLSL 就是这样一种语言。Vulkan 1.2 的新增功能使 HLSL 成为该 API 的一等着色语言，使用起来与 GLSL 同样简便；除了少数例外，GLSL 提供的每个 Vulkan 特性和着色器阶段同样可以通过 HLSL 使用，包括硬件加速光线追踪。<sup>[14](https://github.khronos.org/Vulkan-Site/guide/latest/hlsl.html)</sup>

通往这一地位的路径经过编译器工作。在微软将其 HLSL 编译器开源后，Vulkan 社区与微软、谷歌、LunarG、NVIDIA 和 Valve 合作，为 Vulkan 的 HLSL 着色器编译器添加了 SPIR-V 合法化。由于 HLSL 是一种相当宽松的语言，将 HLSL 直译为 SPIR-V 有时会生成非法的 SPIR-V；合法化过程应用纠正性转换（委托给 SPIRV-Tools），使 SPIR-V 对 Vulkan 有效。Khronos 将 SPIR-V 合法化描述为实现了 HLSL 到 Vulkan SPIR-V 可靠编译的基石。<sup>[6](https://www.khronos.org/blog/hlsl-first-class-vulkan-shading-language)</sup><sup> • </sup><sup>[15](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst?plain=1)</sup>

采用随之而来。谷歌的 Stadia 上线时即有使用 Vulkan 上 HLSL 的 AAA 大作，包括《命运 2》、《荒野大镖客：救赎 2》、《刺客信条：奥德赛》和《[古墓丽影](https://www.edgepedia.org/zh/tomb-raider)》系列。<sup>[6](https://www.khronos.org/blog/hlsl-first-class-vulkan-shading-language)</sup> 游戏引擎也将 HLSL 桥接到其他 API：[虚幻引擎](https://www.edgepedia.org/zh/unreal-engine)的 HLSL 交叉编译器自动将 HLSL 转换为 GLSL 以用于 OpenGL 平台，使着色器可以一次编写、全平台使用。<sup>[16](https://dev.epicgames.com/documentation/unreal-engine/shader-development-in-unreal-engine)</sup>

## 近期发展

机器学习在 HLSL 为 DirectX 发挥的作用中占比日益增长。在 2026 年[游戏开发者大会](https://www.edgepedia.org/zh/game-developers-conference)上，微软将机器学习描述为日益嵌入整个图形管线，并介绍了 DirectX 线性代数，它添加了一等矩阵-矩阵运算，同时保留直接在 HLSL 中编写机器学习的能力，此外还有 DirectX 计算图编译器，用于将机器学习工作负载表达为完整的计算图。<sup>[12](https://devblogs.microsoft.com/directx/evolving-directx-for-the-ml-era-on-windows/)</sup> 微软还围绕该语言添加了开发者工具，包括 HLSL 中的 `DebugBreak()` 以及 DirectX Agility SDK 中的 API 级支持，用于在构建过程中确定性地收集和打包着色器。<sup>[17](https://www.windowscentral.com/microsoft/windows-11/windows-11-xbox-mode-announcement-gdc-2026-project-helix-pc-game-dev)</sup>

## References

1. [DirectX 9.0 Features Revolutionary High-Level Shader Language, Microsoft Source (January 22, 2003)](https://news.microsoft.com/source/2003/01/22/directx-9-0-features-revolutionary-high-level-shader-language/)
2. [Shader Model, DirectXShaderCompiler Wiki, Microsoft](https://github.com/microsoft/DirectXShaderCompiler/wiki/Shader-Model)
3. [HLSL Specification, Chapter 3: Basic Concepts, microsoft.github.io/hlsl-specs](https://microsoft.github.io/hlsl-specs/specs/3-Basic.html)
4. [The existing HLSL compiler DXC is a fork of LLVM/Clang 3.7, DirectXShaderCompiler wiki export](https://gitee.com/gamebaby_admin/DirectXShaderCompiler/wikis/pages/export?doc_id=1376284&type=pdf)
5. [Expected Differences vs DXC and FXC, Clang 21.1.2 documentation](https://releases.llvm.org/21.1.2/tools/clang/docs/HLSL/ExpectedDifferences.html)
6. [HLSL as a First Class Vulkan Shading Language, Khronos Blog (January 15, 2020)](https://www.khronos.org/blog/hlsl-first-class-vulkan-shading-language)
7. [Announcing Shader Model 6.9 Retail and New D3D12 Improvements, DirectX Developer Blog](https://devblogs.microsoft.com/directx/shader-model-6-9-retail-and-more/)
8. [HLSL Support, Clang documentation](https://clang.llvm.org/docs/HLSL/HLSLSupport.html)
9. [Vulkan High Level Shader Language Comparison, Vulkan Documentation Project](https://docs.vulkan.org/guide/latest/high_level_shader_language_comparison.html)
10. [Reference for HLSL, Win32 apps, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-reference)
11. [PC gamers are about to get a free performance upgrade, Windows Central](https://www.windowscentral.com/microsoft/windows/directx-update-shader-execution-reordering-ser-ray-tracing)
12. [Evolving DirectX for the ML Era on Windows, DirectX Developer Blog](https://devblogs.microsoft.com/directx/evolving-directx-for-the-ml-era-on-windows/)
13. [HLSL Specifications, microsoft/hlsl-specs on GitHub](https://github.com/microsoft/hlsl-specs)
14. [HLSL in Vulkan, Vulkan Guide](https://github.khronos.org/Vulkan-Site/guide/latest/hlsl.html)
15. [DirectXShaderCompiler SPIR-V documentation, microsoft/DirectXShaderCompiler on GitHub](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst?plain=1)
16. [Shader Development in Unreal Engine, Epic Developer Community](https://dev.epicgames.com/documentation/unreal-engine/shader-development-in-unreal-engine)
17. [Microsoft brings new Xbox mode to Windows 11 PCs, Windows Central](https://www.windowscentral.com/microsoft/windows-11/windows-11-xbox-mode-announcement-gdc-2026-project-helix-pc-game-dev)
18. [pack_matrix pragma Directive - Win32 apps](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-appendix-pre-pragma-pack-matrix)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Individual programming languages › C-family and imperative languages

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/hlsl

English original: https://www.edgepedia.org/hlsl
