-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvulkan-core.h
294 lines (252 loc) · 11.2 KB
/
vulkan-core.h
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
// Copyright(c) 2019, 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.
// Modifications copyright (C) 2020 Leonardo Romor <[email protected]>
//
// This file contains the represents an intermediate interface to simplify vulkan.
#ifndef __SPACE_CORE_H_
#define __SPACE_CORE_H_
#include <vector>
#include <optional>
#include <memory>
#include <limits>
#include <vulkan/vulkan.hpp>
#include <X11/Xlib.h>
template<class T>
inline constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
return v < lo ? lo : hi < v ? hi : v;
}
template <typename TargetType, typename SourceType>
VULKAN_HPP_INLINE TargetType checked_cast(SourceType value) {
static_assert(
sizeof(TargetType) <= sizeof(SourceType),
"No need to cast from smaller to larger type!");
static_assert(
!std::numeric_limits<TargetType>::is_signed,
"Only unsigned types supported!");
static_assert(
!std::numeric_limits<SourceType>::is_signed,
"Only unsigned types supported!");
assert(value <= std::numeric_limits<TargetType>::max());
return static_cast<TargetType>(value);
}
namespace space {
namespace core {
// Hold the Vulkan configuration data
// such as application name, engine,
// and requested instance layers and extensions.
struct VkAppConfig {
const char *app_name;
const char *engine_name;
std::vector<std::string> instance_layers;
std::vector<std::string> instance_extensions;
};
// Holds the vulkan datacstructures
// used to represent the vulkan implementation,
// instantiation and configuration. It does not
// include any rendering related vullkan calls or
// data structures.
struct VkAppContext {
vk::DynamicLoader dynamic_loader;
vk::UniqueInstance instance;
vk::UniqueSurfaceKHR surface;
vk::UniqueDevice device;
vk::UniqueDebugUtilsMessengerEXT debug_utils_messenger;
vk::PhysicalDevice physical_device;
uint32_t graphics_queue_family_index;
uint32_t present_queue_family_index;
};
// Takes care of the super boring Vulkan bootstraping.
std::optional<struct VkAppContext> InitVulkan(
const VkAppConfig config, Display *display, Window window);
// Vulkan rendering helper routines
struct SwapChainData {
SwapChainData(
vk::PhysicalDevice const& physical_device,
vk::UniqueDevice const& device,
vk::SurfaceKHR const& surface, vk::Extent2D const& extent,
vk::ImageUsageFlags usage,
vk::UniqueSwapchainKHR const& old_swap_chain,
uint32_t graphics_family_index,
uint32_t present_family_index);
vk::Format color_format;
vk::Extent2D extent;
vk::UniqueSwapchainKHR swap_chain;
std::vector<vk::Image> images;
std::vector<vk::UniqueImageView> image_views;
};
class BufferData {
public:
BufferData(
vk::PhysicalDevice const& physicalDevice,
vk::UniqueDevice const& device, vk::DeviceSize size,
vk::BufferUsageFlags usage,
vk::MemoryPropertyFlags propertyFlags = vk::MemoryPropertyFlagBits::eHostVisible
| vk::MemoryPropertyFlagBits::eHostCoherent);
template <typename DataType>
void Upload(
vk::UniqueDevice const& device, DataType const& data) const;
template <typename DataType>
void Upload(
vk::UniqueDevice const& device, std::vector<DataType> const& data,
size_t stride = 0) const;
template <typename DataType>
void Upload(
vk::PhysicalDevice const& physicalDevice, vk::UniqueDevice const& device,
vk::UniqueCommandPool const& commandPool, vk::Queue queue,
std::vector<DataType> const& data,
size_t stride) const;
vk::UniqueBuffer buffer;
vk::UniqueDeviceMemory deviceMemory;
private:
// For debugging pourposes only
// We should optionally remove these checks.
// For instance, is there enough space?
vk::DeviceSize m_size;
vk::BufferUsageFlags m_usage;
vk::MemoryPropertyFlags m_propertyFlags;
};
struct ImageData {
ImageData(vk::PhysicalDevice const& physical_device, vk::UniqueDevice const& device,
vk::Format format, vk::Extent2D const& extent, vk::ImageTiling tiling,
vk::ImageUsageFlags usage, vk::ImageLayout initial_layout,
vk::MemoryPropertyFlags memory_properties, vk::ImageAspectFlags aspect_mask,
vk::SampleCountFlagBits nsamples = vk::SampleCountFlagBits::e1);
vk::Format format;
vk::UniqueImage image;
vk::UniqueDeviceMemory device_memory;
vk::UniqueImageView image_view;
};
struct DepthBufferData : public ImageData {
DepthBufferData(
vk::PhysicalDevice &physical_device, vk::UniqueDevice & device,
vk::Format format, vk::Extent2D const& extent,
vk::ImageUsageFlagBits usage,
vk::SampleCountFlagBits nsamples = vk::SampleCountFlagBits::e1)
: ImageData(
physical_device, device, format, extent, vk::ImageTiling::eOptimal,
usage | vk::ImageUsageFlagBits::eDepthStencilAttachment, vk::ImageLayout::eUndefined,
vk::MemoryPropertyFlagBits::eDeviceLocal, vk::ImageAspectFlagBits::eDepth,
nsamples) {}
};
vk::UniqueCommandPool CreateCommandPool(vk::UniqueDevice &device, uint32_t queue_family_index);
template <class T>
void CopyToDevice(
vk::UniqueDevice const& device, vk::UniqueDeviceMemory const& memory,
T const* pData, size_t count, size_t stride = sizeof(T)) {
assert(sizeof(T) <= stride);
uint8_t* deviceData = static_cast<uint8_t*>(
device->mapMemory(memory.get(), 0, count * stride));
if (stride == sizeof(T)) {
memcpy(deviceData, pData, count * sizeof(T));
} else {
for (size_t i = 0; i < count; i++) {
memcpy(deviceData, &pData[i], sizeof(T));
deviceData += stride;
}
}
device->unmapMemory(memory.get());
}
template <class T>
void CopyToDevice(
vk::UniqueDevice const& device, vk::UniqueDeviceMemory const& memory,
T const& data) { CopyToDevice<T>(device, memory, &data, 1); }
template <typename Func>
void OneTimeSubmit(
vk::UniqueCommandBuffer const& commandBuffer, vk::Queue const& queue,
Func const& func) {
commandBuffer->begin(
vk::CommandBufferBeginInfo(
vk::CommandBufferUsageFlagBits::eOneTimeSubmit));
func(commandBuffer);
commandBuffer->end();
queue.submit(vk::SubmitInfo(0, nullptr, nullptr, 1, &(*commandBuffer)), nullptr);
queue.waitIdle();
}
template <typename Func>
void OneTimeSubmit(
vk::UniqueDevice const& device, vk::UniqueCommandPool const& commandPool,
vk::Queue const& queue, Func const& func) {
vk::UniqueCommandBuffer commandBuffer =
std::move(device->allocateCommandBuffersUnique(
vk::CommandBufferAllocateInfo(
*commandPool, vk::CommandBufferLevel::ePrimary, 1)).front());
OneTimeSubmit(commandBuffer, queue, func);
}
vk::UniqueDeviceMemory AllocateMemory(
vk::UniqueDevice const& device,
vk::PhysicalDeviceMemoryProperties const& memoryProperties,
vk::MemoryRequirements const& memoryRequirements,
vk::MemoryPropertyFlags memoryPropertyFlags);
uint32_t FindMemoryType(
vk::PhysicalDeviceMemoryProperties const& memoryProperties,
uint32_t typeBits, vk::MemoryPropertyFlags requirementsMask);
vk::UniqueDescriptorSetLayout CreateDescriptorSetLayout(
vk::UniqueDevice const& device,
std::vector<std::tuple<vk::DescriptorType, uint32_t, vk::ShaderStageFlags>> const& bindingData,
vk::DescriptorSetLayoutCreateFlags flags = {});
void UpdateDescriptorSets(
vk::UniqueDevice const& device, vk::UniqueDescriptorSet const& descriptorSet,
std::vector<std::tuple<vk::DescriptorType,
vk::UniqueBuffer const&,
vk::UniqueBufferView const&>> const& bufferData, uint32_t bindingOffset = 0);
vk::UniqueDescriptorPool CreateDescriptorPool(
vk::UniqueDevice &device, std::vector<vk::DescriptorPoolSize> const& poolSizes);
std::vector<vk::UniqueFramebuffer> CreateFramebuffers(
vk::UniqueDevice &device, vk::UniqueRenderPass &renderPass,
std::vector<vk::UniqueImageView> const& imageViews,
vk::UniqueImageView const& depthImageView,
vk::UniqueImageView const& colorImageView,
vk::Extent2D const& extent);
std::optional<vk::SurfaceFormatKHR> PickSurfaceFormat(
std::vector<vk::SurfaceFormatKHR> const& formats);
vk::UniqueRenderPass CreateRenderPass(
vk::UniqueDevice &device, vk::Format colorFormat, vk::Format depthFormat,
vk::AttachmentLoadOp loadOp = vk::AttachmentLoadOp::eClear,
vk::ImageLayout colorFinalLayout = vk::ImageLayout::ePresentSrcKHR,
vk::SampleCountFlagBits nsamples = vk::SampleCountFlagBits::e1);
// Simplify the creation of the graphics pipeline.
class GraphicsPipelineBuilder {
public:
GraphicsPipelineBuilder(const vk::UniqueDevice *device,
const vk::UniquePipelineLayout *pipeline_layout,
const vk::UniqueRenderPass *render_pass,
vk::SampleCountFlagBits nsamples = vk::SampleCountFlagBits::e1);
GraphicsPipelineBuilder& SetPrimitiveTopology(vk::PrimitiveTopology topology);
GraphicsPipelineBuilder& SetPolygoneMode(vk::PolygonMode mode);
GraphicsPipelineBuilder& SetFrontFace(vk::FrontFace front_face);
// Has depth
GraphicsPipelineBuilder& DepthBuffered(const bool value = true);
// Shaders
GraphicsPipelineBuilder& AddVertexShader(
const vk::ShaderModule &shader, const vk::SpecializationInfo *specialization_info = NULL);
GraphicsPipelineBuilder& AddFragmentShader(
const vk::ShaderModule &shader, const vk::SpecializationInfo *specialization_info = NULL);
GraphicsPipelineBuilder& AddVertexInputBindingDescription(
uint32_t binding, uint32_t stride, vk::VertexInputRate input_rate);
GraphicsPipelineBuilder& AddVertexInputAttributeDescription(
uint32_t location, uint32_t binding, vk::Format format, uint32_t offset);
// Which states will be handled using command buffers.
GraphicsPipelineBuilder& EnableDynamicState(const vk::DynamicState &state);
// Consume the builder and construct the pipeline
vk::UniquePipeline Create(vk::UniquePipelineCache *pipeline_cache = nullptr);
~GraphicsPipelineBuilder();
private:
class Impl;
std::unique_ptr<Impl> impl_;
};
vk::SampleCountFlagBits GetMaxUsableSampleCount(vk::PhysicalDevice const& physical_device);
}
}
#endif // _VULKAN_CORE_H