summaryrefslogtreecommitdiffstats
path: root/tools/render-test/slang-test-device-cache.h
blob: 752d03ff1685c4b0c1c719871c709bd2cdbe3530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once

#include <mutex>
#include <slang-rhi.h>
#include <string>
#include <unordered_map>
#include <vector>

// Device Cache for preventing NVIDIA Tegra driver state corruption
// This cache reuses Vulkan instances and devices to avoid the VK_ERROR_INCOMPATIBLE_DRIVER
// issue that occurs after ~19 device creation/destruction cycles on Tegra platforms.
// Uses ComPtr for automatic device lifecycle management - devices are released when removed from
// cache.
class DeviceCache
{
public:
    struct DeviceCacheKey
    {
        rhi::DeviceType deviceType;
        bool enableValidation;
        bool enableRayTracingValidation;
        std::string profileName;
        std::vector<std::string> requiredFeatures;

        bool operator==(const DeviceCacheKey& other) const;
    };

    struct DeviceCacheKeyHash
    {
        std::size_t operator()(const DeviceCacheKey& key) const;
    };

    struct CachedDevice
    {
        Slang::ComPtr<rhi::IDevice> device;
        uint64_t creationOrder;

        CachedDevice();
    };

private:
    static constexpr int MAX_CACHED_DEVICES = 10;

    // Use function-local statics to control destruction order (Meyer's singleton pattern)
    static std::mutex& getMutex();
    static std::unordered_map<DeviceCacheKey, CachedDevice, DeviceCacheKeyHash>& getDeviceCache();
    static uint64_t& getNextCreationOrder();

    static void evictOldestDeviceIfNeeded();

public:
    static SlangResult acquireDevice(const rhi::DeviceDesc& desc, rhi::IDevice** outDevice);
    static void cleanCache();
};

// RAII wrapper for cached devices to ensure proper cleanup
class CachedDeviceWrapper
{
private:
    Slang::ComPtr<rhi::IDevice> m_device;

public:
    CachedDeviceWrapper() = default;

    CachedDeviceWrapper(Slang::ComPtr<rhi::IDevice> device)
        : m_device(device)
    {
    }

    ~CachedDeviceWrapper() {}

    // Move constructor
    CachedDeviceWrapper(CachedDeviceWrapper&& other) noexcept
        : m_device(std::move(other.m_device))
    {
    }

    // Move assignment
    CachedDeviceWrapper& operator=(CachedDeviceWrapper&& other) noexcept
    {
        if (this != &other)
        {
            m_device = std::move(other.m_device);
        }
        return *this;
    }

    // Delete copy constructor and assignment
    CachedDeviceWrapper(const CachedDeviceWrapper&) = delete;
    CachedDeviceWrapper& operator=(const CachedDeviceWrapper&) = delete;

    rhi::IDevice* get() const { return m_device.get(); }
    rhi::IDevice* operator->() const { return m_device.get(); }
    operator bool() const { return m_device != nullptr; }

    Slang::ComPtr<rhi::IDevice>& getComPtr() { return m_device; }
};