forked from KhronosGroup/Vulkan-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhpp_pipeline_cache.cpp
More file actions
163 lines (131 loc) · 4.78 KB
/
hpp_pipeline_cache.cpp
File metadata and controls
163 lines (131 loc) · 4.78 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* Copyright (c) 2022-2026, NVIDIA CORPORATION. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hpp_pipeline_cache.h"
#include "common/hpp_utils.h"
HPPPipelineCache::HPPPipelineCache()
{
auto &config = get_configuration();
config.insert<vkb::BoolSetting>(0, enable_pipeline_cache, true);
config.insert<vkb::BoolSetting>(1, enable_pipeline_cache, false);
}
HPPPipelineCache::~HPPPipelineCache()
{
if (pipeline_cache)
{
/* Get data of pipeline cache */
std::vector<uint8_t> data = get_device().get_handle().getPipelineCacheData(pipeline_cache);
/* Write pipeline cache data to a file in binary format */
vkb::fs::write_temp(data, "pipeline_cache.data");
/* Destroy Vulkan pipeline cache */
get_device().get_handle().destroyPipelineCache(pipeline_cache);
}
if (has_device())
{
vkb::fs::write_temp(get_device().get_resource_cache().serialize(), "hpp_cache.data");
}
}
bool HPPPipelineCache::prepare(const vkb::ApplicationOptions &options)
{
if (!vkb::VulkanSampleCpp::prepare(options))
{
return false;
}
/* Try to read pipeline cache file if exists */
std::vector<uint8_t> pipeline_data;
try
{
pipeline_data = vkb::fs::read_temp("pipeline_cache.data");
}
catch (std::runtime_error &ex)
{
LOGW("No pipeline cache found. {}", ex.what());
}
/* Add initial pipeline cache data from the cached file */
vk::PipelineCacheCreateInfo pipeline_cache_create_info{.initialDataSize = static_cast<uint32_t>(pipeline_data.size()),
.pInitialData = pipeline_data.data()};
/* Create Vulkan pipeline cache */
pipeline_cache = get_device().get_handle().createPipelineCache(pipeline_cache_create_info);
vkb::HPPResourceCache &resource_cache = get_device().get_resource_cache();
/* Use pipeline cache to store pipelines */
resource_cache.set_pipeline_cache(pipeline_cache);
std::vector<uint8_t> data_cache;
try
{
data_cache = vkb::fs::read_temp("hpp_cache.data");
}
catch (std::runtime_error &ex)
{
LOGW("No data cache found. {}", ex.what());
}
/* Build all pipelines from a previous run */
resource_cache.warmup(data_cache);
get_stats().request_stats({vkb::StatIndex::frame_times});
float dpi_factor = window->get_dpi_factor();
button_size.x = button_size.x * dpi_factor;
button_size.y = button_size.y * dpi_factor;
create_gui(*window, &get_stats());
load_scene("scenes/sponza/Sponza01.gltf");
auto &camera_node = vkb::common::add_free_camera(get_scene(), "main_camera", get_render_context().get_surface_extent());
camera = &camera_node.get_component<vkb::sg::Camera>();
vkb::core::HPPShaderSource vert_shader("base.vert.spv");
vkb::core::HPPShaderSource frag_shader("base.frag.spv");
auto scene_subpass = std::make_unique<vkb::rendering::subpasses::ForwardSubpassCpp>(
get_render_context(), std::move(vert_shader), std::move(frag_shader), get_scene(), *camera);
auto render_pipeline = std::make_unique<vkb::rendering::HPPRenderPipeline>();
render_pipeline->add_subpass(std::move(scene_subpass));
set_render_pipeline(std::move(render_pipeline));
return true;
}
void HPPPipelineCache::draw_gui()
{
get_gui().show_options_window(
/* body = */ [this]() {
if (ImGui::Checkbox("Pipeline cache", &enable_pipeline_cache))
{
get_device().get_resource_cache().set_pipeline_cache(enable_pipeline_cache ? pipeline_cache : nullptr);
}
ImGui::SameLine();
if (ImGui::Button("Destroy Pipelines", button_size))
{
get_device().get_handle().waitIdle();
get_device().get_resource_cache().clear_pipelines();
record_frame_time_next_frame = true;
}
if (rebuild_pipelines_frame_time_ms > 0.0f)
{
ImGui::Text("Pipeline rebuild frame time: %.1f ms", rebuild_pipelines_frame_time_ms);
}
else
{
ImGui::Text("Pipeline rebuild frame time: N/A");
}
},
/* lines = */ 2);
}
void HPPPipelineCache::update(float delta_time)
{
if (record_frame_time_next_frame)
{
rebuild_pipelines_frame_time_ms = delta_time * 1000.0f;
record_frame_time_next_frame = false;
}
vkb::VulkanSampleCpp::update(delta_time);
}
std::unique_ptr<vkb::VulkanSampleCpp> create_hpp_pipeline_cache()
{
return std::make_unique<HPPPipelineCache>();
}