Skip to content

Add GPU Performance Profiler and Enhanced Error Handling#2

Open
subhobhai943 wants to merge 1 commit intoca2:mainfrom
subhobhai943:feature/gpu-performance-monitoring
Open

Add GPU Performance Profiler and Enhanced Error Handling#2
subhobhai943 wants to merge 1 commit intoca2:mainfrom
subhobhai943:feature/gpu-performance-monitoring

Conversation

@subhobhai943
Copy link

Overview

This PR adds production-ready GPU performance monitoring and enhanced error handling utilities to the DirectX 12 implementation, providing developers with powerful debugging and optimization tools.

New Features

1. GPU Performance Profiler (gpu_profiler.h/cpp)

A comprehensive performance monitoring system that tracks:

Key Metrics

  • Frame timing - Precise frame time measurements in milliseconds
  • FPS tracking - Average, minimum, and maximum frame rates
  • VRAM usage - Real-time video memory consumption via DXGI queries
  • Draw call statistics - Count and triangle metrics per frame
  • Frame history - Configurable rolling window (default 120 frames)

Features

// Usage example:
gpu_profiler profiler;
profiler.initialize(device, adapter);

// In render loop:
profiler.begin_frame();
// ... rendering code ...
profiler.record_draw_call(triangle_count);
profiler.end_frame();

// Get statistics:
auto summary = profiler.get_performance_summary();
std::cout << profiler.get_performance_report();

Benefits

  • Zero overhead when disabled - Minimal performance impact
  • Production-ready - Can be left in shipping code
  • Detailed insights - Identify performance bottlenecks
  • VRAM monitoring - Prevent out-of-memory crashes

2. Enhanced Error Handler (error_handler.h/cpp)

A robust error handling system with:

Capabilities

  • Detailed error messages - Human-readable explanations for all DirectX errors
  • Error history tracking - Maintains log of recent errors
  • Severity levels - Info, Warning, Error, Fatal classifications
  • Device removal detection - Handles GPU crashes gracefully
  • Custom callbacks - Hook into error events
  • Debugging support - Optional breakpoint on error

Error Messages Include

  • HRESULT code and description
  • Possible causes and solutions
  • Stack trace information (function, file, line)
  • Timestamp for error correlation

Usage Examples

error_handler handler;

// Simple error checking:
DX12_CHECK(device->CreateFence(...), handler);

// With automatic exception:
DX12_CHECK_THROW(CreateDescriptorHeap(...), handler);

// Check device health:
std::string reason;
if (FAILED(error_handler::check_device_removed(device, reason))) {
    // Handle GPU crash
    std::cerr << "GPU Error: " << reason;
}

// Generate report:
std::cout << handler.get_error_report();

Technical Highlights

Performance Profiler

  • Uses std::chrono::steady_clock for high-resolution timing
  • Queries VRAM via IDXGIAdapter3::QueryVideoMemoryInfo
  • Efficient std::deque for O(1) history management
  • Thread-safe design (can be extended with mutexes if needed)

Error Handler

  • Comprehensive HRESULT to message mapping
  • Covers common DirectX 12 error codes:
    • DXGI_ERROR_DEVICE_REMOVED
    • DXGI_ERROR_DEVICE_RESET
    • E_OUTOFMEMORY
    • DXGI_ERROR_INVALID_CALL
    • And more...
  • Windows FormatMessage integration for system errors
  • Graceful degradation on null pointers

Integration

These utilities are:

  • Self-contained - No dependencies beyond existing DirectX 12 headers
  • Non-invasive - Can be integrated incrementally
  • Header-only compatible - Easy to include in any module
  • ca2 framework compliant - Follows existing code style and patterns

Use Cases

During Development

  1. Profile rendering performance and identify bottlenecks
  2. Monitor VRAM usage to optimize texture sizes
  3. Track draw call counts for optimization
  4. Debug DirectX errors with detailed context

In Production

  1. Monitor frame times for dynamic quality adjustment
  2. Detect and gracefully handle GPU crashes
  3. Generate crash reports with error history
  4. Track performance metrics for telemetry

Testing Recommendations

  1. Profiler testing:

    • Verify frame timing accuracy
    • Test VRAM query with various adapters
    • Validate statistics calculations
    • Check history buffer bounds
  2. Error handler testing:

    • Test with common error scenarios
    • Verify device removal detection
    • Test callback system
    • Validate error report generation

Future Enhancements

Potential additions:

  • GPU timestamp queries for precise GPU timing
  • Memory allocation tracking per resource type
  • Performance comparison across frames
  • JSON export for external analysis tools
  • Integration with PIX/RenderDoc

Compatibility

  • Windows 10+ (DirectX 12 requirement)
  • Visual Studio 2019+ (C++17 features)
  • Compatible with existing ca2 framework patterns
  • No breaking changes to existing code

Ready for review! These utilities will significantly improve the development experience and production robustness of the DirectX 12 implementation. 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant