-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathresources_vk.hpp
259 lines (204 loc) · 7.98 KB
/
resources_vk.hpp
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
/*
* Copyright (c) 2014-2021, 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) 2014-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
// single set, all UBOs use dynamic offsets and are used in all stages (slowest for gpu)
#define UNIFORMS_ALLDYNAMIC 0
// single set, more accurate stage assignments, only matrix & material are dynamic (slower for gpu)
#define UNIFORMS_SPLITDYNAMIC 1
// multiple sets, only one descrset allocated, matrix & material are dynamic (fastest)
#define UNIFORMS_MULTISETSDYNAMIC 2
// multiple sets, many descrsets allocated, one for each matrix & material (slower for cpu)
#define UNIFORMS_MULTISETSSTATIC 3
// matrix & material data for rendering fits in the 256 bytes, so use that instead of descriptors
// only works on nvidia (256 bytes push data)
// slowest method, given all data is sent every frame
// !animation will show no effect!
#define UNIFORMS_PUSHCONSTANTS_RAW 4
// pass indices for matrix & material in large buffers
// GPU slower in this scene, as it has so many tiny drawcalls
// other scenes with more triangles per draw would not suffer as much
// CPU-wise faster given less data
#define UNIFORMS_PUSHCONSTANTS_INDEX 5
#define UNIFORMS_TECHNIQUE UNIFORMS_MULTISETSDYNAMIC
#define DRAW_UBOS_NUM 3
#include "cadscene_vk.hpp"
#include "resources.hpp"
#include <nvvk/context_vk.hpp>
#include <nvvk/profiler_vk.hpp>
#include <nvvk/shadermodulemanager_vk.hpp>
#include <nvvk/swapchain_vk.hpp>
#include <nvvk/buffers_vk.hpp>
#include <nvvk/commands_vk.hpp>
#include <nvvk/descriptorsets_vk.hpp>
#include <nvvk/error_vk.hpp>
#include <nvvk/memorymanagement_vk.hpp>
#include <nvvk/renderpasses_vk.hpp>
namespace csfthreaded {
class ResourcesVK : public Resources
{
public:
ResourcesVK() {}
static ResourcesVK* get()
{
static ResourcesVK res;
return &res;
}
static bool isAvailable();
struct FrameBuffer
{
int renderWidth = 0;
int renderHeight = 0;
int supersample = 0;
bool useResolved = false;
bool vsync = false;
int msaa = 0;
VkViewport viewport;
VkViewport viewportUI;
VkRect2D scissor;
VkRect2D scissorUI;
VkRenderPass passClear = VK_NULL_HANDLE;
VkRenderPass passPreserve = VK_NULL_HANDLE;
VkRenderPass passUI = VK_NULL_HANDLE;
VkFramebuffer fboScene = VK_NULL_HANDLE;
VkFramebuffer fboUI = VK_NULL_HANDLE;
VkImage imgColor = VK_NULL_HANDLE;
VkImage imgColorResolved = VK_NULL_HANDLE;
VkImage imgDepthStencil = VK_NULL_HANDLE;
VkImageView viewColor = VK_NULL_HANDLE;
VkImageView viewColorResolved = VK_NULL_HANDLE;
VkImageView viewDepthStencil = VK_NULL_HANDLE;
nvvk::DeviceMemoryAllocator memAllocator;
};
struct Common
{
nvvk::AllocationID viewAID;
VkBuffer viewBuffer;
VkDescriptorBufferInfo viewInfo;
nvvk::AllocationID animAID;
VkBuffer animBuffer;
VkDescriptorBufferInfo animInfo;
};
struct ShaderModuleIDs
{
nvvk::ShaderModuleID vertex_tris;
nvvk::ShaderModuleID vertex_line;
nvvk::ShaderModuleID fragment_tris;
nvvk::ShaderModuleID fragment_line;
nvvk::ShaderModuleID compute_animation;
};
struct Shaders
{
VkShaderModule vertex_tris;
VkShaderModule vertex_line;
VkShaderModule fragment_tris;
VkShaderModule fragment_line;
VkShaderModule compute_animation;
};
struct Pipelines
{
VkPipeline tris = VK_NULL_HANDLE;
VkPipeline line_tris = VK_NULL_HANDLE;
VkPipeline line = VK_NULL_HANDLE;
VkPipeline compute_animation = VK_NULL_HANDLE;
};
bool m_withinFrame = false;
nvvk::ShaderModuleManager m_shaderManager;
ShaderModuleIDs m_moduleids;
Shaders m_shaders;
Pipelines m_pipes;
FrameBuffer m_framebuffer;
Common m_common;
#if HAS_OPENGL
//nvvk::DeviceInstance m_ctxContent;
VkSemaphore m_semImageWritten;
VkSemaphore m_semImageRead;
nvvk::Context m_contextInstance;
#else
const nvvk::SwapChain* m_swapChain;
#endif
nvvk::Context* m_context;
VkDevice m_device = VK_NULL_HANDLE;
VkPhysicalDevice m_physical;
VkQueue m_queue;
uint32_t m_queueFamily;
nvvk::DeviceMemoryAllocator m_memAllocator;
nvvk::RingFences m_ringFences;
nvvk::RingCommandPool m_ringCmdPool;
nvvk::BatchSubmission m_submission;
bool m_submissionWaitForRead;
#if UNIFORMS_TECHNIQUE == UNIFORMS_MULTISETSDYNAMIC || UNIFORMS_TECHNIQUE == UNIFORMS_MULTISETSSTATIC
nvvk::TDescriptorSetContainer<DRAW_UBOS_NUM> m_drawing;
#else
nvvk::DescriptorSetContainer m_drawing;
#endif
nvvk::DescriptorSetContainer m_anim;
uint32_t m_numMatrices;
CadSceneVK m_scene;
nvvk::ProfilerVK m_profilerVK;
size_t m_pipeChangeID;
size_t m_fboChangeID;
#if HAS_OPENGL
bool init(nvgl::ContextWindow* window, nvh::Profiler* profiler) override;
#else
bool init(nvvk::Context* context, nvvk::SwapChain* swapChain, nvh::Profiler* profiler) override;
#endif
void deinit() override;
void initPipes();
void deinitPipes();
bool hasPipes() { return m_pipes.compute_animation != 0; }
bool initPrograms(const std::string& path, const std::string& prepend) override;
void reloadPrograms(const std::string& prepend) override;
void updatedPrograms();
void deinitPrograms();
bool initFramebuffer(int width, int height, int msaa, bool vsync) override;
void deinitFramebuffer();
bool initScene(const CadScene&) override;
void deinitScene() override;
void synchronize() override;
void beginFrame() override;
void blitFrame(const Global& global) override;
void endFrame() override;
void animation(const Global& global) override;
void animationReset() override;
glm::mat4 perspectiveProjection(float fovy, float aspect, float nearPlane, float farPlane) const override;
//////////////////////////////////////////////////////////////////////////
VkRenderPass createPass(bool clear, int msaa);
VkRenderPass createPassUI(int msaa);
VkCommandBuffer createCmdBuffer(VkCommandPool pool, bool singleshot, bool primary, bool secondaryInClear) const;
VkCommandBuffer createTempCmdBuffer(bool primary = true, bool secondaryInClear = false);
// submit for batched execution
void submissionEnqueue(VkCommandBuffer cmdbuffer) { m_submission.enqueue(cmdbuffer); }
void submissionEnqueue(uint32_t num, const VkCommandBuffer* cmdbuffers) { m_submission.enqueue(num, cmdbuffers); }
// perform queue submit
void submissionExecute(VkFence fence = NULL, bool useImageReadWait = false, bool useImageWriteSignals = false);
// synchronizes to queue
void resetTempResources();
void cmdBeginRenderPass(VkCommandBuffer cmd, bool clear, bool hasSecondary = false) const;
void cmdPipelineBarrier(VkCommandBuffer cmd, bool isOptimal = false) const;
void cmdDynamicState(VkCommandBuffer cmd) const;
void cmdImageTransition(VkCommandBuffer cmd,
VkImage img,
VkImageAspectFlags aspects,
VkAccessFlags src,
VkAccessFlags dst,
VkImageLayout oldLayout,
VkImageLayout newLayout) const;
void cmdBegin(VkCommandBuffer cmd, bool singleshot, bool primary, bool secondaryInClear) const;
};
} // namespace csfthreaded