DirectX Utility Functions
Introduction
The DirectX Utility library provides a set of helper functions and classes that simplify common tasks when developing graphics applications with DirectX 11/12. These utilities cover resource management, shader compilation, error handling, and format conversion.
Common Utilities
DX::ThrowIfFailed
– Checks anHRESULT
and throws astd::runtime_error
on failure.DX::ReadDataAsync
– Asynchronously reads a file into astd::vector<BYTE>
.DX::CompileShader
– Wrapper aroundD3DCompileFromFile
with debug flags.DX::CreateDevice
– Simplified device and swap‑chain creation with optional debug layer.DX::ConvertToSRGB
– Utility to convert linear textures to sRGB format.
Example: Loading and Compiling a Shader
#include <dxgi.h>
#include <d3d11.h>
#include <DirectXMath.h>
#include "DXUtility.h"
using namespace DirectX;
int main()
{
// Create device and swap chain
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
DX::CreateDevice(device.GetAddressOf(), context.GetAddressOf());
// Compile vertex shader
ComPtr<ID3DBlob> vsBlob;
DX::CompileShader(L"Shaders\\VertexShader.hlsl", "VSMain", "vs_5_0", vsBlob.GetAddressOf());
// Create vertex shader
ComPtr<ID3D11VertexShader> vertexShader;
DX::ThrowIfFailed(device->CreateVertexShader(vsBlob->GetBufferPointer(),
vsBlob->GetBufferSize(),
nullptr,
&vertexShader));
// ... set up input layout, buffers, render loop ...
return 0;
}
This snippet demonstrates creating a Direct3D 11 device, compiling a HLSL vertex shader, and creating the shader object using the utility functions.