D3DCompile2 function

Compiles Microsoft High Level Shader Language (HLSL) code into bytecode for a given target.

Syntax

HRESULT D3DCompile2(
  LPCVOID                pSrcData,
  SIZE_T                 SrcDataSize,
  LPCSTR                 pSourceName,
  const D3D_SHADER_MACRO *pDefines,
  ID3DInclude            *pInclude,
  LPCSTR                 pEntrypoint,
  LPCSTR                 pTarget,
  UINT                   Flags1,
  UINT                   Flags2,
  UINT                   SecondaryDataFlags,
  LPCVOID                pSecondaryData,
  SIZE_T                 SecondaryDataSize,
  ID3DBlob               **ppCode,
  ID3DBlob               **ppErrorMsgs
);

Parameters

pSrcData

Type: LPCVOID

A pointer to uncompiled shader data (ASCII HLSL code).

SrcDataSize

Type: SIZE_T

The size, in bytes, of the block of memory that pSrcData points to.

pSourceName

Type: LPCSTR

An optional pointer to a constant null-terminated string containing the name that identifies the source data to use in error messages. If not used, set to NULL.

pDefines

Type: const D3D_SHADER_MACRO*

An optional array of D3D_SHADER_MACRO structures that define shader macros. Each macro definition contains a name and a NULL-terminated definition. If not used, set to NULL.

pInclude

Type: ID3DInclude*

A pointer to an ID3DInclude interface that the compiler uses to handle include files. If you set this parameter to NULL and the shader contains a #include, a compile error occurs. You can pass the D3D_COMPILE_STANDARD_FILE_INCLUDE macro, which is a pointer to a default include handler. This default include handler includes files that are relative to the current directory and files that are relative to the directory of the initial source file. When you use D3D_COMPILE_STANDARD_FILE_INCLUDE, you must specify the source file name in the pSourceName parameter; the compiler will derive the initial relative directory from pSourceName.

#define D3D_COMPILE_STANDARD_FILE_INCLUDE ((ID3DInclude*)(UINT_PTR)1)

pEntrypoint

Type: LPCSTR

A pointer to a constant null-terminated string that contains the name of the shader entry point function where shader execution begins. When you compile an effect, D3DCompile2 ignores pEntrypoint; we recommend that you set pEntrypoint to NULL because it is good programming practice to set a pointer parameter to NULL if the called function will not use it.

pTarget

Type: LPCSTR

A pointer to a constant null-terminated string that specifies the shader target or set of shader features to compile against. The shader target can be a shader model (for example, shader model 2, shader model 3, shader model 4, or shader model 5). The target can also be an effect type (for example, fx_4_1). For info about the targets that various profiles support, see Specifying Compiler Targets.

Flags1

Type: UINT

A combination of shader D3D compile constants that are combined by using a bitwise OR operation. The resulting value specifies how the compiler compiles the HLSL code.

Flags2

Type: UINT

A combination of effect D3D compile effect constants that are combined by using a bitwise OR operation. The resulting value specifies how the compiler compiles the effect. When you compile a shader and not an effect file, D3DCompile2 ignores Flags2; we recommend that you set Flags2 to zero because it is good programming practice to set a nonpointer parameter to zero if the called function will not use it.

SecondaryDataFlags

Type: UINT

A combination of the following flags that are combined by using a bitwise OR operation. The resulting value specifies how the compiler compiles the HLSL code.

Flag Description
D3DCOMPILE_SECDATA_MERGE_UAV_SLOTS (0x01) Merge unordered access view (UAV) slots in the secondary data that the pSecondaryData parameter points to.
D3DCOMPILE_SECDATA_PRESERVE_TEMPLATE_SLOTS (0x02) Preserve template slots in the secondary data that the pSecondaryData parameter points to.
D3DCOMPILE_SECDATA_REQUIRE_TEMPLATE_MATCH (0x04) Require that templates in the secondary data that the pSecondaryData parameter points to match when the compiler compiles the HLSL code.
 

If pSecondaryData is NULL, set to zero.

pSecondaryData

Type: LPCVOID

A pointer to secondary data. If you don't pass secondary data, set to NULL. Use this secondary data to align UAV slots in two shaders. Suppose shader A has UAVs and they are bound to some slots. To compile shader B such that UAVs with the same names are mapped in B to the same slots as in A, pass A’s byte code to D3DCompile2 as the secondary data.

SecondaryDataSize

Type: SIZE_T

The size, in bytes, of the block of memory that pSecondaryData points to. If pSecondaryData is NULL, set to zero.

ppCode

Type: ID3DBlob**

A pointer to a variable that receives a pointer to the ID3DBlob interface that you can use to access the compiled code.

ppErrorMsgs

Type: ID3DBlob**

A pointer to a variable that receives a pointer to the ID3DBlob interface that you can use to access compiler error messages, or NULL if there are no errors.

Return Value

Type: HRESULT

Returns one of the Direct3D 11 return codes.

Remarks

The difference between D3DCompile2 and D3DCompile is that D3DCompile2 takes some optional parameters (SecondaryDataFlags, pSecondaryData and SecondaryDataSize) that can be used to control some aspects of how bytecode is generated. Refer to the descriptions of these parameters for more details. There is no difference otherwise to the efficiency of the bytecode generated between D3DCompile2 and D3DCompile.

Compiling shaders for UWP

To compile offline shaders the recommend approach is to use the Effect-Compiler Tool. If you cannot compile all of your shaders ahead of time, consider compiling the more expensive ones and the ones on your startup and most performance-sensitive paths require, and compiling the rest at runtime. You can use a process similar to the following to compile a loaded or generated shader in a UWP application without blocking your user interface thread.
  • Using Visual Studio 2015+ to develop the UWP app, add the new item "shader.hlsl".
    • In the Solution Folder view of Visual Studio, select the shaders.hlsl item, right-click for Properties.
    • Make sure the item Content is set to Yes.
    • Make sure the Item Type is set to Text.
    • Add a button to XAML, name it appropriately ("TheButton" in this example), and add a Click handler.
  • Now add these includes to your .cpp file:
    #include <ppltasks.h>
    #include <d3dcompiler.h>
    #include <Robuffer.h>
    
  • Use the following code to call D3DCompile2. Note that there is no error checking or handling here, and also that this code demonstrates you can do both I/O and compilation in the background, which leaves your UI more responsive.
    void App1::DirectXPage::TheButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
      std::shared_ptr<Microsoft::WRL::ComPtr<ID3DBlob>> blobRef = std::make_shared<Microsoft::WRL::ComPtr<ID3DBlob>>();
    

    // Load a file and compile it. auto fileOp = Windows::ApplicationModel::Package::Current->InstalledLocation->GetFileAsync(L"shader.hlsl"); create_task(fileOp).then([this](Windows::Storage::StorageFile^ file) -> IAsyncOperation<Windows::Storage::Streams::IBuffer^>^ { // Do file I/O in background thread (use_arbitrary). return Windows::Storage::FileIO::ReadBufferAsync(file); }, task_continuation_context::use_arbitrary()) .then([this, blobRef](Windows::Storage::Streams::IBuffer^ buffer) { // Do compilation in background thread (use_arbitrary).

    // Cast to Object^, then to its underlying IInspectable interface.
    Microsoft::WRL::ComPtr&lt;IInspectable&gt; insp(reinterpret_cast&lt;IInspectable*&gt;(buffer));
    
    // Query the IBufferByteAccess interface.
    Microsoft::WRL::ComPtr&lt;Windows::Storage::Streams::IBufferByteAccess&gt; bufferByteAccess;
    insp.As(&amp;bufferByteAccess);
    
    // Retrieve the buffer data.
    byte *pBytes = nullptr;
    bufferByteAccess-&gt;Buffer(&amp;pBytes);
    
    Microsoft::WRL::ComPtr&lt;ID3DBlob&gt; blob;
    Microsoft::WRL::ComPtr&lt;ID3DBlob&gt; errMsgs;
    D3DCompile2(pBytes, buffer-&gt;Length, "shader.hlsl", nullptr, nullptr, "main", "ps_5_0", 0, 0, 0, nullptr, 0, blob.GetAddressOf(), errMsgs.GetAddressOf());
    *blobRef = blob;
    

    }, task_continuation_context::use_arbitrary()) .then(this, blobRef { // Update UI / use shader on foreground thread. wchar_t message[40]; swprintf_s(message, L"blob is %u bytes long", (unsigned)(*blobRef)->GetBufferSize()); this->TheButton->Content = ref new Platform::String(message); }, task_continuation_context::use_current()); }

Requirements

   
Target Platform Windows
Header d3dcompiler.h
Library D3DCompiler.lib
DLL D3DCompiler_47.dll

See Also

Functions

Specifying D3D12 Root Signatures in HLSL