Repository: shader-slang/slang
File: tools/render-test/slang-test-device-cache.cpp (C++)

Code before:
        if (SLANG_SUCCEEDED(result))
            return device;
        return nullptr;
    }

    std::lock_guard<std::mutex> lock(getMutex());
    auto& deviceCache = getDeviceCache();
    auto& nextCreationOrder = getNextCreationOrder();

    // Create cache key
    DeviceCacheKey key;
    key.deviceType = desc.deviceType;
    key.enableValidation = desc.enableValidation;
    key.enableRayTracingValidation = desc.enableRayTracingValidation;
    key.profileName = desc.slang.targetProfile ? desc.slang.targetProfile : "";

    // Add required features to key
    for (int i = 0; i < desc.requiredFeatureCount; ++i)
    {
        key.requiredFeatures.push_back(desc.requiredFeatures[i]);
    }
    std::sort(key.requiredFeatures.begin(), key.requiredFeatures.end());

    // Evict oldest device if we've reached the limit
    evictOldestDeviceIfNeeded();

    // Check if we have a cached device
    auto it = deviceCache.find(key);
    if (it != deviceCache.end())
    {
        // Return the cached device - COM reference counting handles the references
        return it->second.device;
    }

    // Create new device
    Slang::ComPtr<rhi::IDevice> device;
    auto result = rhi::getRHI()->createDevice(desc, device.writeRef());
    if (SLANG_FAILED(result))
    {
        return nullptr;
    }

    // Cache the device
    CachedDevice& cached = deviceCache[key];
    cached.device = device;
    cached.creationOrder = nextCreationOrder++;

    return device;
}



Review comment (bug):
I think we may want to print some message when the verbose mode is enabled for a debugging purpose.
It will be useful if we can track when certain devices were created and when certain devices were evicted.

Code after:
    // Skip caching for CUDA devices due to crashes
    if (desc.deviceType == rhi::DeviceType::CUDA)
    {
        return rhi::getRHI()->createDevice(desc, outDevice);
    }

    std::lock_guard<std::mutex> lock(getMutex());
    auto& deviceCache = getDeviceCache();
    auto& nextCreationOrder = getNextCreationOrder();

    // Create cache key
    DeviceCacheKey key;
    key.deviceType = desc.deviceType;
    key.enableValidation = desc.enableValidation;
    key.enableRayTracingValidation = desc.enableRayTracingValidation;
    key.profileName = desc.slang.targetProfile ? desc.slang.targetProfile : "Unknown";

    // Add required features to key
    for (int i = 0; i < desc.requiredFeatureCount; ++i)
    {
        key.requiredFeatures.push_back(desc.requiredFeatures[i]);
    }
    std::sort(key.requiredFeatures.begin(), key.requiredFeatures.end());

    // Evict oldest device if we've reached the limit
    evictOldestDeviceIfNeeded();

    // Check if we have a cached device
    auto it = deviceCache.find(key);
    if (it != deviceCache.end())
    {
        // Return the cached device - COM reference counting handles the references
        *outDevice = it->second.device.get();
        if (*outDevice)
        {
            (*outDevice)->addRef();
            return SLANG_OK;
        }
    }

    // Create new device
    Slang::ComPtr<rhi::IDevice> device;
    auto result = rhi::getRHI()->createDevice(desc, device.writeRef());
    if (SLANG_FAILED(result))
    {
        return result;
    }

    // Cache the device
    CachedDevice& cached = deviceCache[key];
    cached.device = device;