Diligent Engine Documentation

repository·master·Indexed 26 days ago

https://github.com/diligentgraphics/diligentengine

A high-performance, cross-platform 3D graphics API abstraction layer and rendering framework. It provides a consistent C/C++/C# front-end API for modern graphics backends (D3D12, Vulkan, Metal, WebGPU) and legacy APIs (D3D11, OpenGL, OpenGLES, WebGL). Key features include automatic shader resource binding, multithreaded command buffer generation, and support for advanced capabilities like ray-tracing, mesh shaders, and async compute.

Tokens
13.6K
Snippets
24
Records
69
Agent score
81%

What's inside Diligent Engine

  1. Overview of Diligent Engine

    master

    Diligent Engine is a lightweight, high-performance, cross-platform 3D graphics API abstraction layer and rendering framework. It provides a consistent front-end API for modern graphics APIs (Direct3D12, Vulkan, Metal, WebGPU) and legacy APIs (Direct3D11, OpenGL, OpenGLES, WebGL).

    Key characteristics:

    • Cross-platform: Use the exact same client code and HLSL shaders across all supported platforms and backends without platform-specific #if defined macros.
    • Modular: Components are logically and physically separated, allowing you to use only what your project requires.
    • API Design: Object-based, stateless API available in C, C++, and C#.
    • Shader Support: Uses HLSL as a universal shading language, with support for platform-specific formats like GLSL, MSL, DirectX bytecode, and SPIR-V.
  2. Interoperate with native graphics APIs (v2.1+)

    master

    Diligent Engine provides several ways to interoperate with native APIs (D3D11, D3D12, OpenGL):

    • Access internal objects and handles.
    • Create Diligent buffers/textures from native resources.
    • Attach to an existing D3D11/D3D12 device or GL context.
    • Perform resource state and command queue synchronization for D3D12.
  3. Understand the Diligent Engine repository structure

    master

    The master repository is composed of four main submodules:

    • Core: Implements low-level graphics back-ends: Direct3D11, Direct3D12, OpenGL/GLES, and Vulkan. It is self-contained.
    • Tools: Contains utility libraries including texture loading, asset loading, dear imgui implementation, native application implementation, and render state notation tools.
    • DiligentFX: A high-level rendering framework that depends on Core and Tools.
    • Samples: Contains tutorials and sample applications to demonstrate the API. It depends on Core, Tools, and DiligentFX.
  4. Use High-Level Rendering Components from DiligentFX

    master

    High-level rendering functionality is provided by the DiligentFX module. Available components include:

    Core Rendering

    • GLTF2.0 Loader & PBR Renderer: For loading GLTF assets and rendering them with physically-based rendering and image-based lighting.
    • Hydrogent: An implementation of the Hydra rendering API.
    • Shadows: A component for rendering high-quality shadows.

    Post-processing Effects

    • Screen-Space Reflections (SSR)
    • Screen-Space Ambient Occlusion (SSAO)
    • Depth of Field (DoF)
    • Bloom
    • Epipolar Light Scattering
    • Temporal Anti-Aliasing (TAA)
    • Tone Mapping: Includes shader utilities for tone mapping.
  5. Build Diligent Engine on Win32

    master

    To build on Windows, you need the Windows SDK (10.0.19041.0+), C++ build tools, and Visual C++ ATL Support. .NET SDK 6.0+ is required for .NET support.

    Important: The full path to your CMake build folder must not contain white spaces.

    To enable Vulkan validation layers, install the Vulkan SDK and set the VK_LAYER_PATH environment variable to the Bin directory of the VulkanSDK installation.

    Use CMake to generate Visual Studio solution files. For example, to generate a 64-bit solution in build/Win64:

    cmake -S . -B ./build/Win64 -G "Visual Studio 17 2022" -A x64

    To target the Win8.1 SDK:

    cmake -D CMAKE_SYSTEM_VERSION=8.1 -S . -B ./build/Win64_8.1 -G "Visual Studio 17 2022" -A x64

    Running Applications: Open the generated .sln file in Visual Studio. You can force a specific rendering backend using command line arguments:

    • --mode d3d11
    • --mode d3d12
    • --mode gl
    • --mode vk

    If running outside of Visual Studio, ensure the application's assets folder is set as the working directory.

  6. Configure Vulkan on macOS

    master

    By default, macOS has no Vulkan implementation. Diligent Engine loads Vulkan dynamically and can use implementations like MoltenVK or gfx-portability.

    Environment Configuration

    To use MoltenVK, install the Vulkan SDK and set the following environment variables (example path: /Users/MyName/VulkanSDK/1.4.335.1):

    export VULKAN_SDK=/Users/MyName/VulkanSDK/1.4.335.1/macOS
    export PATH=$VULKAN_SDK/bin:$PATH
    export DYLD_LIBRARY_PATH=$VULKAN_SDK/lib:$DYLD_LIBRARY_PATH
    export VK_ADD_LAYER_PATH=$VULKAN_SDK/share/vulkan/explicit_layer.d
    export VK_ICD_FILENAMES=$VULKAN_SDK/share/vulkan/icd.d/MoltenVK_icd.json
    export VK_DRIVER_FILES=$VULKAN_SDK/share/vulkan/icd.d/MoltenVK_icd.json

    Important Notes

    • Launching Applications: Environment variables set in the shell are not visible to applications launched via Launchpad. You must start applications from the command line. To ensure Xcode sees these variables, open the project from the shell using the open command.
    • Xcode Environment: For Xcode 7+, you may need to disable the sanitized build system environment:
      defaults write com.apple.dt.Xcode UseSanitizedBuildSystemEnvironment -bool NO
    • Library Loading: DYLD_LIBRARY_PATH is often ignored on macOS due to System Integrity Protection. Diligent Engine automatically configures the rpath for applications if the VULKAN_SDK environment variable is set correctly.
    export VULKAN_SDK=/Users/MyName/VulkanSDK/1.4.335.1/macOS
    export PATH=$VULKAN_SDK/bin:$PATH
    export DYLD_LIBRARY_PATH=$VULKAN_SDK/lib:$DYLD_LIBRARY_PATH
    export VK_ADD_LAYER_PATH=$VULKAN_SDK/share/vulkan/explicit_layer.d
    export VK_ICD_FILENAMES=$VULKAN_SDK/share/vulkan/icd.d/MoltenVK_icd.json
    export VK_DRIVER_FILES=$VULKAN_SDK/share/vulkan/icd.d/MoltenVK_icd.json
  7. Configure Diligent Engine build options via CMake

    master

    You can customize the Diligent Engine build by passing CMake options. This allows you to disable specific graphics backends (e.g., DILIGENT_NO_VULKAN), enable/disable engine modules like DILIGENT_BUILD_SAMPLES, or provide paths to third-party dependencies like DILIGENT_NVAPI_PATH or DILIGENT_DEAR_IMGUI_PATH.

    By default, all available backends for your platform are built. To disable a backend, set its corresponding DILIGENT_NO_* option to TRUE.

    Note: WebGPU is enabled by default for Web builds, but to enable it on other platforms, ensure DILIGENT_NO_WEBGPU is set to OFF.

    cmake -D DILIGENT_NO_DIRECT3D11=TRUE -S . -B ./build/Win64 -G "Visual Studio 17 2022" -A x64
  8. Install Diligent Engine manually (Non-CMake projects)

    master

    If your project does not use CMake, it is recommended to build the Diligent libraries with CMake first and then integrate the artifacts.

    1. Configure and Build: Use -D CMAKE_INSTALL_PREFIX to specify a local installation directory.
    2. Install: Run the install target from your build folder.

    Installation Directory Structure:

    • include/: Header files (add this to your include search paths).
    • lib/: Static libraries.
    • bin/: Dynamic libraries.

    Platform Requirements: You must define one of the following platform macros as 1 in your project: PLATFORM_WIN32, PLATFORM_UNIVERSAL_WINDOWS, PLATFORM_ANDROID, PLATFORM_LINUX, PLATFORM_MACOS, or PLATFORM_IOS.

  9. Explore Diligent Engine Tutorials

    master

    Diligent Engine provides a comprehensive set of tutorials covering various graphics programming topics, from basic triangle rendering to advanced techniques like Ray Tracing and Mesh Shaders. These tutorials are available in the DiligentSamples repository and can be implemented using the standard API, the C API, or the .NET API.

    Core Graphics Topics

    • Basics: Hello Triangle, Cube rendering, Texturing, and Instancing.
    • Advanced Rendering: Geometry Shaders, Tessellation, Compute Shaders, and Render Passes (Deferred Shading).
    • Modern GPU Features: Bindless Resources, Mesh Shaders, Ray Tracing, and Variable Rate Shading (VRS).
    • Optimization & Performance: Multithreading (parallel command list generation), Data Streaming (MAP_FLAG_DISCARD, MAP_FLAG_DO_NOT_SYNCHRONIZE), and Render State Caching.
    • Specialized Techniques: Order-Independent Transparency (OIT), Shadow Mapping, MSAA, and Post-Processing via the DiligentFX module.
    • Platform Specifics: OpenXR (VR), visionOS (Apple), and Multiple Windows support.
  10. Build Diligent Engine for Android

    master

    Requires Android Studio and NDK r24 or later. Ensure NDK and CMake are properly configured.

    To build and run tutorials/samples, open the DiligentSamples/Android folder with Android Studio.

    By default, applications run in Vulkan mode. To run in OpenGL ES mode, add the following launch flag in Android Studio (Run -> Edit Configurations):

    --es mode gles

  11. Migrate Shader and Pipeline State Creation (v2.4.b)

    master

    In version 2.4.b, the API for shader initialization and pipeline state (PSO) creation changed. Shader variable types and static sampler definitions were moved from the shader creation stage to the PSO creation stage.

    Key changes:

    • Use PipelineResourceLayoutDesc and the ResourceLayout member of PipelineStateDesc to define variables and samplers.
    • Use IPipelineState::GetStaticShaderVariable (or GetStaticVariableByName/ByIndex) to set constants on the PSO rather than on the shader object.
    • IShaderVariable was renamed to IShaderResourceVariable.
    • ShaderVariableDesc was renamed to ShaderResourceVariableDesc.
    // New API Pattern (v2.4.b+)
    RefCntAutoPtr<IShader> pVS;
    {
        ShaderCI.Desc.ShaderType = SHADER_TYPE_VERTEX;
        ShaderCI.EntryPoint      = "main";
        ShaderCI.Desc.Name       = "Cube VS";
        ShaderCI.FilePath        = "cube.vsh";
        pDevice->CreateShader(ShaderCI, &pVS);
    }
    // ...
    ShaderResourceVariableDesc Vars[] =
    {
        {SHADER_TYPE_PIXEL, "g_Texture", SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE}
    };
    PSODesc.ResourceLayout.Variables    = Vars;
    PSODesc.ResourceLayout.NumVariables = _countof(Vars);
    
    // Define static sampler for g_Texture
    SamplerDesc SamLinearClampDesc( FILTER_TYPE_LINEAR, FILTER_TYPE_LINEAR, FILTER_TYPE_LINEAR, 
                                    TEXTURE_ADDRESS_CLAMP, TEXTURE_ADDRESS_CLAMP, TEXTURE_ADDRESS_CLAMP);
    StaticSamplerDesc StaticSamplers[] =
    {
        {SHADER_TYPE_PIXEL, "g_Texture", SamLinearClampDesc}
    };
    PSODesc.ResourceLayout.StaticSamplers    = StaticSamplers;
    PSODesc.ResourceLayout.NumStaticSamplers = _countof(StaticSamplers);
    
    pDevice->CreatePipelineState(PSODesc, &m_pPSO);
    m_pPSO->GetStaticShaderVariable(SHADER_TYPE_VERTEX, "Constants")->Set(m_VSConstants);
  12. Clone the Diligent Engine repository

    master

    The Diligent Engine repository uses Git submodules. To ensure you have all necessary components (Core, Tools, DiligentFX, and Samples), you must clone the repository recursively.

    To update an existing installation, pull the latest changes and update the submodules.

    # Initial clone with all submodules
    git clone --recursive https://github.com/DiligentGraphics/DiligentEngine.git
    
    # Updating an existing repository
    git pull
    git submodule update --recursive