DirectXShaderCompiler
This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
Top Related Projects
Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.
A collection of tools, libraries, and tests for Vulkan shader compilation.
Tool suite for Texture and 3D Model Compression, Optimization and Analysis using CPUs, GPUs and APUs
GLSL optimizer based on Mesa's GLSL compiler. Used to be used in Unity for mobile shader optimization.
Quick Overview
The Microsoft DirectXShaderCompiler repository is a collection of tools and libraries for compiling HLSL (High-Level Shading Language) code into DirectX bytecode. It serves as the core of the DirectX Shader Compiler, which is used to compile shaders for DirectX-based applications and games.
Pros
- Comprehensive Shader Compilation: The project provides a robust and feature-rich shader compilation pipeline, supporting a wide range of HLSL language features and targeting various DirectX shader models.
- Cross-Platform Compatibility: The DirectXShaderCompiler can be built and used on multiple platforms, including Windows, Linux, and macOS, making it accessible to a broader developer audience.
- Open-Source and Customizable: As an open-source project, developers can inspect, modify, and extend the codebase to suit their specific needs or integrate it into their own projects.
- Active Development and Community: The project is actively maintained by Microsoft and has a vibrant community of contributors, ensuring ongoing improvements and bug fixes.
Cons
- Complexity: The codebase can be complex, with a large number of interdependent components, which may make it challenging for new contributors to get started.
- Windows-Centric: While the project supports cross-platform compilation, the primary focus and development efforts are centered around the Windows platform, which may limit its adoption on other operating systems.
- Limited Documentation: The project's documentation, while available, could be more comprehensive and user-friendly, especially for developers new to the DirectX ecosystem.
- Performance Overhead: Depending on the complexity of the shaders and the target hardware, the compilation process can be resource-intensive, which may impact the overall performance of the development workflow.
Code Examples
Here are a few code examples demonstrating the usage of the DirectXShaderCompiler:
- Compiling a Vertex Shader:
#include <dxc/dxcapi.h>
HRESULT CompileVertexShader(const char* shaderSource, IDxcBlob** ppCompiledBlob) {
IDxcCompiler* pCompiler;
IDxcUtils* pUtils;
IDxcIncludeHandler* pIncludeHandler;
DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&pCompiler));
DxcCreateInstance(CLSID_DxcUtils, IID_PPV_ARGS(&pUtils));
pCompiler->CreateIncludeHandler(&pIncludeHandler);
DxcBuffer source = { shaderSource, strlen(shaderSource), CP_UTF8 };
IDxcOperationResult* pResult;
pCompiler->Compile(&source, L"shader.hlsl", L"VSMain", L"vs_5_0", nullptr, 0, nullptr, 0, pIncludeHandler, &pResult);
HRESULT hr;
pResult->GetStatus(&hr);
if (SUCCEEDED(hr)) {
pResult->GetResult(ppCompiledBlob);
}
pIncludeHandler->Release();
pUtils->Release();
pCompiler->Release();
return hr;
}
- Compiling a Pixel Shader:
#include <dxc/dxcapi.h>
HRESULT CompilePixelShader(const char* shaderSource, IDxcBlob** ppCompiledBlob) {
// Similar to the vertex shader example, but with "PSMain" as the entry point and "ps_5_0" as the target
// ...
}
- Disassembling a Compiled Shader:
#include <dxc/dxcapi.h>
HRESULT DisassembleShader(IDxcBlob* pCompiledBlob, IDxcBlobEncoding** ppDisassembly) {
IDxcCompiler* pCompiler;
IDxcUtils* pUtils;
DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&pCompiler));
D
Competitor Comparisons
Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.
Pros of glslang
- Cross-platform Compatibility: glslang supports a wide range of platforms, including Windows, Linux, and macOS, making it a more versatile choice for developers working across different environments.
- Comprehensive Language Support: glslang supports a variety of shading languages, including GLSL, HLSL, and SPIR-V, allowing developers to work with the language that best suits their needs.
- Active Community: The glslang project has a larger and more active community, with more contributors and a more robust ecosystem of tools and resources.
Cons of glslang
- Complexity: glslang can be more complex to set up and configure, especially for developers who are new to the project or working with a specific shading language.
- Steeper Learning Curve: Developers may need to invest more time and effort to become proficient with glslang, as it has a more extensive feature set and a more complex API.
Code Comparison
DirectXShaderCompiler:
HRESULT CompileFromFile(
_In_z_ LPCWSTR pFileName,
_In_opt_z_ LPCWSTR pEntryPoint,
_In_z_ LPCWSTR pTargetProfile,
_In_opt_ UINT uFlags1,
_In_opt_ UINT uFlags2,
_Out_ IDxcBlob** ppCode,
_Out_opt_ IDxcBlobEncoding** ppErrorMsgs) {
// Compile shader code
}
glslang:
EShLanguage GetShLanguage(const TBuiltInResource& resources) {
if (resources.maxVertexAttribs > 0)
return EShLangVertex;
else if (resources.maxTessControlInputComponents > 0)
return EShLangTessControl;
else if (resources.maxTessEvaluationInputComponents > 0)
return EShLangTessEvaluation;
else if (resources.maxGeometryInputComponents > 0)
return EShLangGeometry;
else if (resources.maxFragmentInputComponents > 0)
return EShLangFragment;
else if (resources.maxComputeWorkGroupSize[0] > 0)
return EShLangCompute;
else
return EShLangCount;
}
A collection of tools, libraries, and tests for Vulkan shader compilation.
Pros of Shaderc
- Shaderc supports a wider range of input languages, including GLSL, HLSL, and Vulkan SPIR-V, making it more versatile for cross-platform development.
- Shaderc provides a unified API for compiling shaders, simplifying the integration process for developers.
- Shaderc is actively maintained and has a larger community of contributors, potentially leading to faster bug fixes and feature updates.
Cons of Shaderc
- DirectXShaderCompiler is tightly integrated with the Microsoft ecosystem, which may be preferred by developers working exclusively on Windows-based platforms.
- DirectXShaderCompiler has a more extensive set of features and optimizations specifically tailored for DirectX and HLSL, potentially offering better performance for DirectX-based applications.
Code Comparison
DirectXShaderCompiler (HLSL):
void main(float4 position : POSITION, out float4 outPosition : SV_POSITION)
{
outPosition = position;
}
Shaderc (GLSL):
#version 330
layout(location = 0) in vec4 position;
layout(location = 0) out vec4 outPosition;
void main()
{
outPosition = position;
}
Tool suite for Texture and 3D Model Compression, Optimization and Analysis using CPUs, GPUs and APUs
Pros of Compressonator
- Supports a wide range of image and texture formats, including DDS, KTX, and PVR.
- Provides a comprehensive set of compression algorithms, including DXT, ETC, and ASTC.
- Offers a user-friendly GUI for easy compression and decompression tasks.
Cons of Compressonator
- Limited to image and texture compression, while DirectXShaderCompiler has a broader scope for shader compilation.
- May not have the same level of performance and optimization as DirectXShaderCompiler, which is developed by Microsoft.
- Lacks the extensive documentation and community support that DirectXShaderCompiler enjoys.
Code Comparison
Here's a brief comparison of the code structure between the two projects:
DirectXShaderCompiler
int __cdecl wmain(int argc, _Wchar_t **argv) {
return dxc::main(argc, argv);
}
int dxc::main(int argc, _Wchar_t **argv) {
// ...
DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&pCompiler));
// ...
}
Compressonator
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// ...
CMP_Feedback_Proc pFeedbackProc = NULL;
CMP_CompressOptions options;
CMP_ConvertTexture(&srcTexture, &dstTexture, &options, pFeedbackProc);
// ...
}
The key differences are that DirectXShaderCompiler focuses on shader compilation, while Compressonator is primarily for image and texture compression. The code structures reflect these different use cases, with DirectXShaderCompiler using a dxc::main()
function and Compressonator using a standard WinMain()
entry point.
GLSL optimizer based on Mesa's GLSL compiler. Used to be used in Unity for mobile shader optimization.
Pros of glsl-optimizer
- Smaller Codebase: glsl-optimizer has a smaller codebase compared to DirectXShaderCompiler, making it potentially easier to understand and contribute to.
- Cross-Platform: glsl-optimizer is designed to work across multiple platforms, including Windows, macOS, and Linux, while DirectXShaderCompiler is primarily focused on the Windows platform.
- Specialized for GLSL: glsl-optimizer is specifically optimized for GLSL (OpenGL Shading Language), which may make it more suitable for certain use cases compared to the more general-purpose DirectXShaderCompiler.
Cons of glsl-optimizer
- Limited Functionality: glsl-optimizer is focused on GLSL optimization and may not provide the same level of functionality or features as DirectXShaderCompiler, which supports a wider range of shader languages and capabilities.
- Fewer Contributors: DirectXShaderCompiler has a larger community of contributors and maintainers compared to glsl-optimizer, which may result in slower development and less active support.
- Compatibility Concerns: As a cross-platform tool, glsl-optimizer may have compatibility issues or limitations on certain platforms, whereas DirectXShaderCompiler is tightly integrated with the Windows ecosystem.
Code Comparison
Here's a brief code comparison between the two projects:
DirectXShaderCompiler (C++):
HRESULT CompileShaderFromFile(
_In_z_ LPCWSTR pFileName,
_In_opt_z_ LPCSTR pEntrypoint,
_In_z_ LPCSTR pTarget,
_In_opt_ UINT uFlags1,
_In_opt_ UINT uFlags2,
_Out_ IDxcBlob** ppCode,
_Out_opt_ IDxcBlobEncoding** ppErrorMsgs) {
// Compile shader code
// ...
}
glsl-optimizer (C++):
bool optimize_glsl(
const char* input,
int input_len,
const char* entry,
const char* profile,
char** output,
int* output_len,
char** error_log) {
// Optimize GLSL code
// ...
}
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
DirectX Shader Compiler
The DirectX Shader Compiler project includes a compiler and related tools used to compile High-Level Shader Language (HLSL) programs into DirectX Intermediate Language (DXIL) representation. Applications that make use of DirectX for graphics, games, and computation can use it to generate shader programs.
For more information, see the Wiki.
Visit the DirectX Landing Page for more resources for DirectX developers.
Features and Goals
The starting point of the project is a fork of the LLVM and Clang projects, modified to accept HLSL and emit a validated container that can be consumed by GPU drivers.
At the moment, the DirectX HLSL Compiler provides the following components:
-
dxc.exe, a command-line tool that can compile HLSL programs for shader model 6.0 or higher
-
dxcompiler.dll, a DLL providing a componentized compiler, assembler, disassembler, and validator
-
dxilconv.dll, a DLL providing a converter from DXBC (older shader bytecode format)
-
various other tools based on the above components
The Microsoft Windows SDK releases include a supported version of the compiler and validator.
The goal of the project is to allow the broader community of shader developers to contribute to the language and representation of shader programs, maintaining the principles of compatibility and supportability for the platform. It's currently in active development across two axes: language evolution (with no impact to DXIL representation), and surfacing hardware capabilities (with impact to DXIL, and thus requiring coordination with GPU implementations).
Pre-built Releases
Development kits containing only the dxc.exe driver app, the dxcompiler.dll, and the dxil.dll signing binary are available here, or in the releases tab.
SPIR-V CodeGen
As an example of community contribution, this project can also target the SPIR-V intermediate representation. Please see the doc for how HLSL features are mapped to SPIR-V, and the wiki page for how to build, use, and contribute to the SPIR-V CodeGen.
Building Sources
See the full documentation for Building and testing DXC for detailed instructions.
Running Shaders
To run shaders compiled as DXIL, you will need support from the operating system as well as from the driver for your graphics adapter. Windows 10 Creators Update is the first version to support DXIL shaders. See the Wiki for information on using experimental support or the software adapter.
Hardware Support
Hardware GPU support for DXIL is provided by the following vendors:
NVIDIA
NVIDIA's r396 drivers (r397.64 and later) provide release mode support for DXIL 1.1 and Shader Model 6.1 on Win10 1709 and later, and experimental mode support for DXIL 1.2 and Shader Model 6.2 on Win10 1803 and later. These drivers also support DXR in experimental mode.
Drivers can be downloaded from geforce.com.
AMD
AMDâs driver (Radeon Software Adrenalin Edition 18.4.1 or later) provides release mode support for DXIL 1.1 and Shader Model 6.1. Drivers can be downloaded from AMD's download site.
Intel
Intel's 15.60 drivers (15.60.0.4849 and later) support release mode for DXIL 1.0 and Shader Model 6.0 as well as release mode for DXIL 1.1 and Shader Model 6.1 (View Instancing support only).
Drivers can be downloaded from the following link Intel Graphics Drivers
Direct access to 15.60 driver (latest as of this update) is provided below:
Making Changes
To make contributions, see the CONTRIBUTING.md file in this project.
Documentation
You can find documentation for this project in the docs
directory. These contain the original LLVM documentation files, as well as two new files worth nothing:
- HLSLChanges.rst: this is the starting point for how this fork diverges from the original llvm/clang sources
- DXIL.rst: this file contains the specification for the DXIL format
- tools/clang/docs/UsingDxc.rst: this file contains a user guide for dxc.exe
License
DirectX Shader Compiler is distributed under the terms of the University of Illinois Open Source License.
See LICENSE.txt and ThirdPartyNotices.txt for details.
Code of Conduct
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Top Related Projects
Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.
A collection of tools, libraries, and tests for Vulkan shader compilation.
Tool suite for Texture and 3D Model Compression, Optimization and Analysis using CPUs, GPUs and APUs
GLSL optimizer based on Mesa's GLSL compiler. Used to be used in Unity for mobile shader optimization.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot