Events
May 19, 6 PM - May 23, 12 AM
Calling all developers, creators, and AI innovators to join us in Seattle @Microsoft Build May 19-22.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
This topic covers the FXC.EXE
compiler used for Shader Models 2 through 5.1. For Shader Model 6, you use DXC.EXE
instead, which is documented in Using dxc.exe and dxcompiler.dll. Visual Studio will use DXC.EXE
automatically when Shader Model 6 is selected for the HLSL Property Page configuration.
Microsoft Visual Studio can compile shader code from *.hlsl and *.fx files that you include in your C++ project.
As part of the build process, Visual Studio uses the fxc.exe or dxc.exe HLSL code compiler to compile the HLSL shader source files into binary shader object files or into byte arrays that are defined in header files. How the HLSL code compiler compiles each shader source file in your project depends on how you specify the Ouput Files property for that file. For more info about HLSL property pages, see HLSL Property Pages.
The compile method that you use typically depends on the size of your HLSL shader source file. If you include a large amount of byte code in a header, you increase the size and the initial load time of your app. You also force all byte code to reside in memory even after the shader is created, which wastes resources. But when you include byte code in a header, you can reduce code complexity and simplify shader creation.
Let's now look at various ways to compile your shader code and conventions for file extensions for shader code.
To conform to Microsoft convention, use these file extensions for your shader code:
If you compile your .hlsl files into binary shader object files, your app needs to read the data from those object files (.cso is the default extension for these object files), assign the data to byte arrays, and create shader objects from those byte arrays. For example, to create a vertex shader (ID3D11VertexShader**), call the ID3D11Device::CreateVertexShader method with a byte array that contains compiled vertex shader byte code. In this example code, the Ouput Files property for the SimpleVertexShader.hlsl file specifies to compile into the SimpleVertexShader.cso object file.
auto vertexShaderBytecode = ReadData("SimpleVertexShader.cso");
ComPtr<ID3D11VertexShader> vertexShader;
DX::ThrowIfFailed(
m_d3dDevice->CreateVertexShader(
vertexShaderBytecode->Data,
vertexShaderBytecode->Length,
nullptr,
&vertexShader
)
The ReadData helper here will look in the current working directory as well as the same directory as the current process' EXE file as the .cso files are typically found alongside other VS build products. See ReadData.h for an example implementation.
If you compile your .hlsl files into byte arrays that are defined in header files, you need to include those header files in your code. In this example code, the Ouput Files property for the PixelShader.hlsl file specifies to compile into the g_psshader byte array that is defined in the PixelShader.h header file.
namespace
{
include "PixelShader.h"
}
...
ComPtr<ID3D11PixelShader> m_pPixelShader;
hr = pDevice->CreatePixelShader(g_psshader, sizeof(g_psshader), nullptr, &m_pPixelShader);
You can also use the D3DCompileFromFile function at run time to compile shader code for Direct3D 11. For more info about how to do this, see How To: Compile a Shader.
Note
Windows Store apps support using D3DCompileFromFile for development but not for deployment.
Events
May 19, 6 PM - May 23, 12 AM
Calling all developers, creators, and AI innovators to join us in Seattle @Microsoft Build May 19-22.
Register todayTraining
Learning path
Run high-performance computing (HPC) applications on Azure - Training
Azure HPC is a purpose-built cloud capability for HPC & AI workload, using leading-edge processors and HPC-class InfiniBand interconnect, to deliver the best application performance, scalability, and value. Azure HPC enables users to unlock innovation, productivity, and business agility, through a highly available range of HPC & AI technologies that can be dynamically allocated as your business and technical needs change. This learning path is a series of modules that help you get started on Azure HPC - you