-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathspecialization.hpp
More file actions
89 lines (79 loc) · 2.78 KB
/
specialization.hpp
File metadata and controls
89 lines (79 loc) · 2.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
/*
* Copyright (c) 2022-2025, NVIDIA CORPORATION. All rights reserved.
*
* 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.
*
* SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <span>
#include <vector>
#include "vulkan/vulkan_core.h"
namespace nvvk {
/*--------------------------------------------------------------------------------------------------
# class nvvk::Specialization
> Helper to generate specialization info
Examples:
```cpp
nvvk::Specialization specialization;
specialization.add(0, 5); // Adding value 5 to constant_id=0
VkPipelineShaderStageCreateInfo info;
...
info.pSpecializationInfo = specialization.getSpecialization();
createPipeline();
```
Note: this is adding information in a vector, therefor add all values
before calling getSpecialization(). Construct the pipeline before
specialization get out of scope, or pointer getting invalidated
by adding new values or clearing the vector of data.
---------------------------------------------------------------------------------------------------*/
class Specialization
{
public:
void add(uint32_t constantID, int32_t value)
{
m_specValues.push_back(value);
VkSpecializationMapEntry entry{};
entry.constantID = constantID;
entry.size = sizeof(int32_t);
entry.offset = static_cast<uint32_t>(m_specEntries.size() * sizeof(int32_t));
m_specEntries.emplace_back(entry);
}
void add(const std::span<std::pair<uint32_t, int32_t>>& const_values)
{
for(const auto& v : const_values)
{
add(v.first, v.second);
}
}
const VkSpecializationInfo* getSpecializationInfo()
{
m_specInfo.dataSize = static_cast<size_t>(m_specValues.size() * sizeof(int32_t));
m_specInfo.pData = m_specValues.data();
m_specInfo.mapEntryCount = static_cast<uint32_t>(m_specEntries.size());
m_specInfo.pMapEntries = m_specEntries.data();
return &m_specInfo;
}
void clear()
{
m_specValues.clear();
m_specEntries.clear();
m_specInfo = {};
}
private:
std::vector<int32_t> m_specValues;
std::vector<VkSpecializationMapEntry> m_specEntries;
VkSpecializationInfo m_specInfo{};
};
} // namespace nvvk