Monado OpenXR Runtime
Loading...
Searching...
No Matches
render_compute_pipeline_cache.hpp
Go to the documentation of this file.
1// Copyright 2026, NVIDIA CORPORATION.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Get-or-create cache for specialized compute pipelines.
6 * @ingroup aux_render
7 */
8
9#pragma once
10
12
13#include "vk/vk_helpers.h"
14
15#include <cstddef>
16#include <span>
17#include <string>
18#include <string_view>
19#include <type_traits>
20#include <unordered_map>
21
22/*
23 *
24 * Specialization key structs.
25 *
26 * Shader-generated keys (e.g. @c render_distortion_spec) come from
27 * @ref render_shader_specialization.hpp. Each struct's bytes are both the
28 * specialization data handed to Vulkan and the cache key. To keep the byte key
29 * well-defined:
30 * - use only 4-byte scalar fields (VkBool32, int32_t, uint32_t) so the struct
31 * has no padding,
32 * - always value-initialize keys,
33 * - normalize booleans to @c VK_TRUE / @c VK_FALSE so logically equal inputs
34 * produce identical bytes.
35 *
36 */
37
38/*!
39 * Get-or-create cache of specialized compute pipelines that share one shader
40 * module and pipeline layout and differ only in specialization constant values.
41 *
42 * Owns the pipelines it creates. The shader module, pipeline layout, and Vulkan
43 * pipeline cache supplied to @ref init are borrowed and are never destroyed by
44 * this object.
45 *
46 * Compute-only: creation needs just those borrowed objects plus specialization
47 * constants. Graphics pipelines need far more create-info (render pass or
48 * dynamic rendering formats, vertex input, rasterization, blend, and so on), so
49 * they cannot use this cache as-is.
50 *
51 * @tparam Key A trivially-copyable, padding-free POD (see notes above).
52 *
53 * @note Not thread-safe; the render path is serialized.
54 *
55 * @ingroup aux_render
56 */
57template <typename Key> class ComputePipelineCache
58{
59 static_assert(std::is_trivially_copyable_v<Key>, "Key must be a trivially-copyable POD");
60
61public:
62 explicit ComputePipelineCache(std::string_view name);
64
65 /*!
66 * Sets the borrowed Vulkan objects used to create pipelines.
67 *
68 * The caller retains ownership and must keep these objects valid during
69 * calls to @ref get and @ref prewarm that may create a pipeline.
70 */
71 void
72 init(VkShaderModule shader, VkPipelineLayout layout, VkPipelineCache pipelineCache);
73
74 XRT_CHECK_RESULT VkResult
75 get(struct vk_bundle *vk, const Key &key, VkPipeline &out);
76
77 XRT_CHECK_RESULT VkResult
78 prewarm(struct vk_bundle *vk, std::span<const Key> keys);
79
80 void
81 destroy(struct vk_bundle *vk);
82
83private:
84 struct Hash
85 {
86 size_t
87 operator()(const Key &key) const noexcept;
88 };
89 struct Equal
90 {
91 bool
92 operator()(const Key &a, const Key &b) const noexcept;
93 };
94
95 VkShaderModule mShader{VK_NULL_HANDLE};
96 VkPipelineLayout mLayout{VK_NULL_HANDLE};
97 VkPipelineCache mCache{VK_NULL_HANDLE};
98 std::string mName;
99
100 std::unordered_map<Key, VkPipeline, Hash, Equal> mPipelines;
101};
102
103#define RENDER_PIPELINE_CACHE_EXTERN(Key) extern template class ComputePipelineCache<Key>;
104RENDER_SHADER_SPECIALIZATION_KEYS(RENDER_PIPELINE_CACHE_EXTERN)
105#undef RENDER_PIPELINE_CACHE_EXTERN
Get-or-create cache of specialized compute pipelines that share one shader module and pipeline layout...
Definition render_compute_pipeline_cache.hpp:58
void init(VkShaderModule shader, VkPipelineLayout layout, VkPipelineCache pipelineCache)
Sets the borrowed Vulkan objects used to create pipelines.
Definition render_compute_pipeline_cache.cpp:77
C++ shader specialization map entries for render compute shaders.
A bundle of Vulkan functions and objects, used by both Compositor and Compositor client code.
Definition vk_helpers.h:81
Common Vulkan code header.