HLSL in Engines

Unity shaders are .shader files: ShaderLab (properties, subshaders, passes, render state) wrapping HLSL programs (HLSLPROGRAM ...

41.1 Unity: ShaderLab + HLSL (URP)

Unity shaders are .shader files: ShaderLab (properties, subshaders, passes, render state) wrapping HLSL programs (HLSLPROGRAM ... ENDHLSL; the older CGPROGRAM blocks are legacy built-in-pipeline style). Unity cross-compiles to D3D11/12, Vulkan, Metal, OpenGL ES, and consoles.

Unity essentials: multi_compile (all variants always built) vs shader_feature (only variants used by materials); keywords and #pragma target 4.5; Shader Graph's Custom Function node accepts HLSL files/strings; compute shaders are .compute files with #pragma kernel CSMain, dispatched via ComputeShader.Dispatch; HDRP has its own ShaderLibrary; URP lighting helpers live in Lighting.hlsl (GetMainLight(), UniversalFragmentPBR).

Shader "Course/URPUnlitTextured"
{
    Properties
    {
        _BaseMap   ("Base Map", 2D) = "white" {}
        _BaseColor ("Base Color", Color) = (1, 1, 1, 1)
    }
    SubShader
    {
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
        Pass
        {
            Name "Forward"
            Tags { "LightMode" = "UniversalForward" }
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog
            #pragma shader_feature_local _ALPHATEST_ON
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            TEXTURE2D(_BaseMap);  SAMPLER(sampler_BaseMap);    // platform-abstracting macros

            CBUFFER_START(UnityPerMaterial)                    // required for SRP Batcher compatibility
                float4 _BaseMap_ST;
                half4  _BaseColor;
            CBUFFER_END

            struct Attributes { float4 positionOS : POSITION; float2 uv : TEXCOORD0; };
            struct Varyings   { float4 positionHCS : SV_POSITION; float2 uv : TEXCOORD0; float fogFactor : TEXCOORD1; };

            Varyings vert(Attributes IN)
     
…

41.2 Unreal Engine: .usf/.ush and material Custom nodes

Unreal's shaders are HLSL in .usf (Unreal Shader File) and .ush (header) files, cross-compiled by Unreal's shader compiler (via DXC/ShaderConductor-style paths) to each platform.

Material Custom node: write an HLSL function body inside the material editor; inputs become parameters:

Use the Include File Paths property to include your own .ush files and keep complex code out of the node.

Global shaders (C++ + .usf) for custom compute/post passes, registered with a virtual path from a plugin:

Dispatch it through the Render Dependency Graph (FComputeShaderUtils::AddPass). Permutations use SHADER_PERMUTATION_BOOL/FPermutationDomain.

// Custom node: inputs UV (float2), Time (float); output type CMOT Float3
float2 p = UV * 8.0;
float v = sin(p.x + Time) + sin(p.y + Time * 0.7) + sin(length(p - 4.0) - Time);
return 0.5 + 0.5 * cos(3.14159 * v + float3(0, 2, 4));
// C++ (plugin module startup)
FString ShaderDir = FPaths::Combine(IPluginManager::Get().FindPlugin(TEXT("MyPlugin"))->GetBaseDir(), TEXT("Shaders"));
AddShaderSourceDirectoryMapping(TEXT("/Plugin/MyPlugin"), ShaderDir);

class FMyCS : public FGlobalShader
{
    DECLARE_GLOBAL_SHADER(FMyCS);
    SHADER_USE_PARAMETER_STRUCT(FMyCS, FGlobalShader);
    BEGIN_SHADER_PARAMETER_STRUCT(FParameters, )
        SHADER_PARAMETER(float, Time)
        SHADER_PARAMETER_RDG_TEXTURE_UAV(RWTexture2D<float4>, Output)
    END_SHADER_PARAMETER_STRUCT()
};
IMPLEMENT_GLOBAL_SHADER(FMyCS, "/Plugin/MyPlugin/Private/MyCS.usf", "MainCS", SF_Compute);
// MyCS.usf
#include "/Engine/Public/Platform.ush"
float Time;
RWTexture2D<float4> Output;

[numthreads(8, 8, 1)]
void MainCS(uint3 id : SV_DispatchThreadID)
{
    Output[id.xy] = float4(frac(id.x / 64.0 + Time), frac(id.y / 64.0), 0.5, 1);
}

41.3 Other ecosystems

Godot 4: its own GLSL-like shading language; HLSL concepts transfer directly.

O3DE: AZSL (HLSL with extensions) compiled via DXC.

The Forge, bgfx, Diligent Engine, Filament: cross-platform shader pipelines (some author HLSL, some GLSL or custom dialects).

MonoGame / FNA: .fx effect files (MojoShader / MGFX) — Effects syntax lives on here.

Slang in production (e.g. NVIDIA RTX Remix, Falcor): HLSL-compatible code with modules and generics.