Skip to content

Commit db1e1cf

Browse files
committed
Polish path tracer cache layout
1 parent 1a64e82 commit db1e1cf

File tree

2 files changed

+73
-15
lines changed

2 files changed

+73
-15
lines changed

31_HLSLPathTracer/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ if(NBL_BUILD_IMGUI)
2222
set(PATH_TRACER_BUILD_MODE "WALLTIME_OPTIMIZED" CACHE STRING
2323
"Choose the EX31 precompiled shader layout. WALLTIME_OPTIMIZED keeps polygon-method selection inside the shared triangle shader module to reduce compile wall time. SPECIALIZED bakes triangle polygon methods into distinct entrypoints, which increases backend work and rebuild time but keeps those variants as separate precompiled entrypoints."
2424
)
25+
set(PATH_TRACER_CACHE_ROOT "pipeline/cache" CACHE STRING
26+
"Relative cache root written to path_tracer.runtime.json in the common bin directory. The runtime resolves this path relative to the JSON file location. Empty disables the generated dev-mode JSON and falls back to --pipeline-cache-dir or LocalAppData."
27+
)
2528
set_property(CACHE PATH_TRACER_BUILD_MODE PROPERTY STRINGS WALLTIME_OPTIMIZED SPECIALIZED)
2629
set(_PATH_TRACER_BUILD_MODE_VALUES WALLTIME_OPTIMIZED SPECIALIZED)
2730
if(NOT PATH_TRACER_BUILD_MODE IN_LIST _PATH_TRACER_BUILD_MODE_VALUES)
2831
message(FATAL_ERROR "Unsupported PATH_TRACER_BUILD_MODE='${PATH_TRACER_BUILD_MODE}'. Expected one of: ${_PATH_TRACER_BUILD_MODE_VALUES}")
2932
endif()
33+
if(IS_ABSOLUTE "${PATH_TRACER_CACHE_ROOT}")
34+
message(FATAL_ERROR "PATH_TRACER_CACHE_ROOT must stay relative because the runtime resolves it against path_tracer.runtime.json")
35+
endif()
3036

3137
set(NBL_INCLUDE_SERACH_DIRECTORIES
3238
"${CMAKE_CURRENT_SOURCE_DIR}/include"
@@ -45,6 +51,14 @@ if(NBL_BUILD_IMGUI)
4551
else()
4652
target_compile_definitions(${EXECUTABLE_NAME} PRIVATE PATH_TRACER_BUILD_MODE_WALLTIME_OPTIMIZED=1)
4753
endif()
54+
if(NOT PATH_TRACER_CACHE_ROOT STREQUAL "")
55+
string(REPLACE "\\" "/" PATH_TRACER_CACHE_ROOT_JSON "${PATH_TRACER_CACHE_ROOT}")
56+
file(GENERATE
57+
OUTPUT "$<TARGET_FILE_DIR:${EXECUTABLE_NAME}>/path_tracer.runtime.json"
58+
CONTENT "{\n \"cache_root\": \"${PATH_TRACER_CACHE_ROOT_JSON}\"\n}\n"
59+
)
60+
unset(PATH_TRACER_CACHE_ROOT_JSON)
61+
endif()
4862

4963
set(OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/auto-gen-flat")
5064
get_filename_component(OUTPUT_DIRECTORY_ABSOLUTE "${OUTPUT_DIRECTORY}" ABSOLUTE)

31_HLSLPathTracer/main.cpp

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "nbl/builtin/hlsl/colorspace/encodeCIEXYZ.hlsl"
1212
#include "nbl/builtin/hlsl/sampling/quantized_sequence.hlsl"
1313
#include "nbl/asset/utils/ISPIRVEntryPointTrimmer.h"
14+
#include "nbl/system/ModuleLookupUtils.h"
1415
#include "app_resources/hlsl/render_common.hlsl"
1516
#include "app_resources/hlsl/render_rwmc_common.hlsl"
1617
#include "app_resources/hlsl/resolve_common.hlsl"
@@ -25,6 +26,8 @@
2526
#include <optional>
2627
#include <thread>
2728

29+
#include "nlohmann/json.hpp"
30+
2831
using namespace nbl;
2932
using namespace core;
3033
using namespace hlsl;
@@ -61,6 +64,7 @@ class HLSLComputePathtracer final : public SimpleWindowedApplication, public Bui
6164
constexpr static inline uint32_t2 WindowDimensions = { 1280, 720 };
6265
constexpr static inline uint32_t MaxFramesInFlight = 5;
6366
static constexpr std::string_view BuildConfigName = PATH_TRACER_BUILD_CONFIG_NAME;
67+
static constexpr std::string_view RuntimeConfigFilename = "path_tracer.runtime.json";
6468
static inline std::string DefaultImagePathsFile = "envmap/envmap_0.exr";
6569
static inline std::string OwenSamplerFilePath = "owen_sampler_buffer.bin";
6670

@@ -1196,7 +1200,7 @@ class HLSLComputePathtracer final : public SimpleWindowedApplication, public Bui
11961200
ImGui::TextDisabled("All pipelines ready");
11971201
ImGui::Dummy(ImVec2(0.f, 2.f));
11981202

1199-
if (ImGui::CollapsingHeader("Camera", ImGuiTreeNodeFlags_DefaultOpen))
1203+
if (ImGui::CollapsingHeader("Camera"))
12001204
{
12011205
if (beginSectionTable("##camera_controls_table"))
12021206
{
@@ -1918,26 +1922,66 @@ class HLSLComputePathtracer final : public SimpleWindowedApplication, public Bui
19181922
return localOutputCWD / "pipeline/cache";
19191923
}
19201924

1921-
path getPipelineCacheRootDir() const
1925+
path getRuntimeConfigPath() const
19221926
{
1923-
return tryGetPipelineCacheDirOverride().value_or(getDefaultPipelineCacheDir());
1927+
return system::executableDirectory() / RuntimeConfigFilename;
19241928
}
19251929

1926-
static constexpr std::string_view PipelineCacheSchemaVersion = "v4";
1927-
static constexpr std::string_view TrimmedShaderCacheSchemaVersion = "v5";
1930+
std::optional<path> tryGetPipelineCacheDirFromRuntimeConfig() const
1931+
{
1932+
const auto configPath = getRuntimeConfigPath();
1933+
if (!m_system->exists(configPath, IFile::ECF_READ))
1934+
return std::nullopt;
1935+
1936+
std::ifstream input(configPath);
1937+
if (!input.is_open())
1938+
return std::nullopt;
19281939

1929-
path getPipelineCacheBlobPath() const
1940+
nlohmann::json json;
1941+
try
1942+
{
1943+
input >> json;
1944+
}
1945+
catch (const std::exception& e)
1946+
{
1947+
m_logger->log("Failed to parse PATH_TRACER runtime config %s: %s", ILogger::ELL_WARNING, configPath.string().c_str(), e.what());
1948+
return std::nullopt;
1949+
}
1950+
1951+
const auto cacheRootIt = json.find("cache_root");
1952+
if (cacheRootIt == json.end() || !cacheRootIt->is_string())
1953+
return std::nullopt;
1954+
1955+
const auto cacheRoot = cacheRootIt->get<std::string>();
1956+
if (cacheRoot.empty())
1957+
return std::nullopt;
1958+
1959+
const path relativeRoot(cacheRoot);
1960+
if (relativeRoot.is_absolute())
1961+
{
1962+
m_logger->log("Ignoring absolute cache_root in %s", ILogger::ELL_WARNING, configPath.string().c_str());
1963+
return std::nullopt;
1964+
}
1965+
1966+
return (configPath.parent_path() / relativeRoot).lexically_normal();
1967+
}
1968+
1969+
path getPipelineCacheRootDir() const
19301970
{
1931-
const auto key = m_device->getPipelineCacheKey();
1932-
return getPipelineCacheRootDir() / PipelineCacheSchemaVersion / "pipeline" / BuildConfigName / (std::string(key.deviceAndDriverUUID) + ".bin");
1971+
if (const auto overrideDir = tryGetPipelineCacheDirOverride(); overrideDir.has_value())
1972+
return overrideDir.value();
1973+
if (const auto runtimeConfigDir = tryGetPipelineCacheDirFromRuntimeConfig(); runtimeConfigDir.has_value())
1974+
return runtimeConfigDir.value();
1975+
return getDefaultPipelineCacheDir();
19331976
}
19341977

1935-
path getTrimmedShaderCacheDir() const
1978+
path getPipelineCacheBlobPath() const
19361979
{
1937-
return getPipelineCacheRootDir() / TrimmedShaderCacheSchemaVersion / "trimmed" / BuildConfigName;
1980+
const auto key = m_device->getPipelineCacheKey();
1981+
return getPipelineCacheRootDir() / "blob" / BuildConfigName / (std::string(key.deviceAndDriverUUID) + ".bin");
19381982
}
19391983

1940-
path getValidatedSpirvCacheDir() const
1984+
path getSpirvCacheDir() const
19411985
{
19421986
return getPipelineCacheRootDir() / "spirv" / BuildConfigName;
19431987
}
@@ -1947,15 +1991,15 @@ class HLSLComputePathtracer final : public SimpleWindowedApplication, public Bui
19471991
core::blake3_hasher hasher;
19481992
hasher << std::string_view(shader ? shader->getFilepathHint() : std::string_view{});
19491993
hasher << std::string_view(entryPoint);
1950-
return getTrimmedShaderCacheDir() / (hashToHex(static_cast<core::blake3_hash_t>(hasher)) + ".spv");
1994+
return getSpirvCacheDir() / (hashToHex(static_cast<core::blake3_hash_t>(hasher)) + ".spv");
19511995
}
19521996

19531997
path getValidatedSpirvMarkerPath(const ICPUBuffer* spirvBuffer) const
19541998
{
19551999
auto contentHash = spirvBuffer->getContentHash();
19562000
if (contentHash == ICPUBuffer::INVALID_HASH)
19572001
contentHash = spirvBuffer->computeContentHash();
1958-
return getValidatedSpirvCacheDir() / (hashToHex(contentHash) + ".hash");
2002+
return getSpirvCacheDir() / (hashToHex(contentHash) + ".hash");
19592003
}
19602004

19612005
size_t getBackgroundPipelineBuildBudget() const
@@ -1993,8 +2037,8 @@ class HLSLComputePathtracer final : public SimpleWindowedApplication, public Bui
19932037
void initializePipelineCache()
19942038
{
19952039
m_pipelineCache.blobPath = getPipelineCacheBlobPath();
1996-
m_pipelineCache.trimmedShaders.rootDir = getTrimmedShaderCacheDir();
1997-
m_pipelineCache.trimmedShaders.validationDir = getValidatedSpirvCacheDir();
2040+
m_pipelineCache.trimmedShaders.rootDir = getSpirvCacheDir();
2041+
m_pipelineCache.trimmedShaders.validationDir = getSpirvCacheDir();
19982042
if (!m_pipelineCache.trimmedShaders.trimmer)
19992043
m_pipelineCache.trimmedShaders.trimmer = core::make_smart_refctd_ptr<asset::ISPIRVEntryPointTrimmer>();
20002044
const auto pipelineCacheRootDir = getPipelineCacheRootDir();

0 commit comments

Comments
 (0)