Overview
TheDirect3DDevice struct encapsulates the creation and lifetime management of a Direct3D render device used for frame processing in the virtual display driver. It manages DXGI factory, adapter, D3D device, and device context objects.
Header: Driver.h:52-66
Implementation: Driver.cpp:2992-3076
Structure Declaration
Constructors
Direct3DDevice(LUID AdapterLuid)
Signature:AdapterLuid: Locally Unique Identifier for the target GPU adapter
Driver.cpp:2992
Purpose: Initializes the device object with a specific GPU adapter LUID.
Usage:
Direct3DDevice()
Signature:Driver.cpp:2996
Purpose: Initializes the device object with zeroed LUID (default adapter).
Usage:
Initialization
Init
Signature:Driver.cpp:3000
Purpose: Initializes all Direct3D resources (factory, adapter, device, context).
Returns:
S_OKon success- DXGI or D3D error code on failure
1. Create DXGI Factory
- Factory is not cached to detect new adapters appearing on the system
- Can check
DxgiFactory->IsCurrent()if caching is desired - Re-create factory if
IsCurrent()returns false
2. Enumerate Adapter by LUID
- LUID not found (GPU removed, detached, or invalid LUID)
- GPU device disabled
- Driver not loaded
3. Create D3D11 Device
- D3D_FEATURE_LEVEL_11_1: Preferred, supports advanced features
- D3D_FEATURE_LEVEL_11_0: Fallback for older GPUs
D3D11_CREATE_DEVICE_BGRA_SUPPORT: Mandatory for IddCx drivers (WHQL requirement)
- GPU lost or removed (e.g., detachable eGPU)
- System in transient state (driver update, power transition)
- Out of memory
- Driver crash recovery
Member Variables
AdapterLuid
Type:LUID
Purpose: Locally Unique Identifier for the GPU adapter.
Structure:
DxgiFactory
Type:Microsoft::WRL::ComPtr<IDXGIFactory5>
Purpose: DXGI factory for adapter enumeration and resource creation.
Capabilities:
- Enumerate display adapters
- Query adapter properties
- Create swap chains
- Check feature support
IDXGIFactory5: Windows 10 Creators Update (1703) and later- Provides HDR and advanced color support
Adapter
Type:Microsoft::WRL::ComPtr<IDXGIAdapter1>
Purpose: Represents the physical GPU adapter.
Common Operations:
Device
Type:Microsoft::WRL::ComPtr<ID3D11Device>
Purpose: Main D3D11 device for resource creation and rendering.
Common Operations:
DeviceContext
Type:Microsoft::WRL::ComPtr<ID3D11DeviceContext>
Purpose: Immediate context for issuing rendering commands.
Common Operations:
Device Caching
The driver implements device caching to reuse D3D devices across multiple monitors on the same GPU: Cache Declaration:GetOrCreateDevice
Purpose: Retrieves cached device or creates new one if not cached. Thread Safety: Protected bys_DeviceCacheMutex.
Usage:
- Reduces device creation overhead
- Shares GPU resources across monitors
- Automatic reference counting
Error Recovery
Device Lost Handling
When GPU device is lost (removed, driver reset, power state change):Transient State Recovery
For transient failures during initialization:Integration with SwapChainProcessor
TheDirect3DDevice is used by SwapChainProcessor for frame processing:
Performance Considerations
Factory Recreation
Current Implementation:- Current: Always detects new adapters, slight overhead
- Cached: Better performance, may miss hot-plugged GPUs
BGRA Support Flag
TheD3D11_CREATE_DEVICE_BGRA_SUPPORT flag is mandatory for IddCx drivers:
Debugging
Debug Device Creation
For development builds, enable D3D debug layer:Logging
The implementation logs all major steps:References
- Source Files:
~/workspace/source/Virtual Display Driver (HDR)/MttVDD/Driver.h(lines 52-66)~/workspace/source/Virtual Display Driver (HDR)/MttVDD/Driver.cpp(lines 2992-3076)
- Related Classes: SwapChainProcessor
- Microsoft Documentation: