From ba8132345cbae5b749b4a01deda732ad6f8251a0 Mon Sep 17 00:00:00 2001 From: Gangzheng Tong Date: Mon, 22 Sep 2025 15:46:42 -0700 Subject: Add RHI Device Caching and Test Prefix Exclusion (#8448) # Add RHI Device Caching and Test Prefix Exclusion ## Summary This PR introduces two key improvements to the Slang test infrastructure: 1. **RHI Device Caching**: Implements device caching to significantly speed up test execution by reusing graphics devices across tests, **RHI Device Caching reduces slang-test execution time from ~15 minutes to ~5 minutes in Windows release builds** 2. **Test Prefix Exclusion**: Adds `-exclude-prefix` option to skip tests matching specified path prefixes ## Changes ### RHI Device Caching - **New `DeviceCache` class** (`slang-test-device-cache.h/cpp`): Thread-safe device cache with LRU eviction (max 10 devices) - **Cache control option**: `-cache-rhi-device` flag in both `slang-test` and `render-test` - Default: **enabled** in slang-test, **disabled** in render-test when run standalone - Automatically skips caching for CUDA devices (due to driver issues) - **Performance benefit**: Eliminates expensive device creation/destruction cycles, especially beneficial for Vulkan on Tegra platforms ### Test Prefix Exclusion - **New `-exclude-prefix ` option** in slang-test - Allows excluding entire test directories or patterns from execution - Complements existing `-category` and individual test filtering options ### Usage Examples ```bash # Enable device caching (default) slang-test # Disable device caching slang-test -cache-rhi-device false # Exclude tests from specific directories slang-test -exclude-prefix tests/problematic/ slang-test -exclude-prefix tests/slow/ -exclude-prefix tests/experimental/ ``` This change should significantly improve test execution performance, particularly in CI environments with frequent device operations. This is needed for running the GPU test in aarch64, where repeated device creation/destroy is causing driver issues. Needed by: https://github.com/shader-slang/slang/issues/8346 --------- Co-authored-by: slangbot Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- tools/render-test/render-test-main.cpp | 45 ++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 10 deletions(-) (limited to 'tools/render-test/render-test-main.cpp') diff --git a/tools/render-test/render-test-main.cpp b/tools/render-test/render-test-main.cpp index 991d60683..5b3974fc9 100644 --- a/tools/render-test/render-test-main.cpp +++ b/tools/render-test/render-test-main.cpp @@ -12,6 +12,7 @@ #include "shader-input-layout.h" #include "shader-renderer-util.h" #include "slang-support.h" +#include "slang-test-device-cache.h" #include "window.h" #if defined(_WIN32) @@ -1440,7 +1441,7 @@ static SlangResult _innerMain( } } - renderer_test::CoreToRHIDebugBridge debugCallback; + static renderer_test::CoreToRHIDebugBridge debugCallback; debugCallback.setCoreCallback(stdWriters->getDebugCallback()); // Use the profile name set on options if set @@ -1495,7 +1496,7 @@ static SlangResult _innerMain( return SLANG_E_NOT_AVAILABLE; } - Slang::ComPtr device; + CachedDeviceWrapper deviceWrapper; { DeviceDesc desc = {}; desc.deviceType = options.deviceType; @@ -1558,8 +1559,27 @@ static SlangResult _innerMain( { getRHI()->enableDebugLayers(); } - SlangResult res = getRHI()->createDevice(desc, device.writeRef()); - if (SLANG_FAILED(res)) + Slang::ComPtr rhiDevice; + SlangResult res; + if (options.cacheRhiDevice) + { + res = DeviceCache::acquireDevice(desc, rhiDevice.writeRef()); + if (SLANG_FAILED(res)) + { + rhiDevice = nullptr; + } + } + else + { + res = rhi::getRHI()->createDevice(desc, rhiDevice.writeRef()); + if (SLANG_FAILED(res)) + { + rhiDevice = nullptr; + } + } + + // Check result for both cached and non-cached paths + if (SLANG_FAILED(res) || !rhiDevice) { // We need to be careful here about SLANG_E_NOT_AVAILABLE. This return value means // that the renderer couldn't be created because it required *features* that were @@ -1575,21 +1595,20 @@ static SlangResult _innerMain( { return res; } - if (!options.onlyStartup) { fprintf(stderr, "Unable to create renderer %s\n", rendererName.getBuffer()); } - return res; } - SLANG_ASSERT(device); + SLANG_ASSERT(rhiDevice); + deviceWrapper = CachedDeviceWrapper(rhiDevice); } for (const auto& feature : requiredFeatureList) { // If doesn't have required feature... we have to give up - if (!device->hasFeature(feature)) + if (!deviceWrapper->hasFeature(feature)) { return SLANG_E_NOT_AVAILABLE; } @@ -1599,7 +1618,7 @@ static SlangResult _innerMain( // Print adapter info after device creation but before any other operations if (options.showAdapterInfo) { - auto info = device->getInfo(); + auto info = deviceWrapper->getInfo(); auto out = stdWriters->getOut(); out.print("Using graphics adapter: %s\n", info.adapterName); } @@ -1613,14 +1632,20 @@ static SlangResult _innerMain( { RenderTestApp app; renderDocBeginFrame(); - SLANG_RETURN_ON_FAIL(app.initialize(session, device, options, input)); + SLANG_RETURN_ON_FAIL(app.initialize(session, deviceWrapper.get(), options, input)); app.update(); renderDocEndFrame(); app.finalize(); } + return SLANG_OK; } +SLANG_TEST_TOOL_API void cleanDeviceCache() +{ + DeviceCache::cleanCache(); +} + SLANG_TEST_TOOL_API SlangResult innerMain( Slang::StdWriters* stdWriters, SlangSession* sharedSession, -- cgit v1.2.3