Repository navigation
Expand file tree
/
Copy pathVulkanApplication.cpp
More file actions
255 lines (209 loc) · 9.4 KB
/
Copy pathVulkanApplication.cpp
File metadata and controls
255 lines (209 loc) · 9.4 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
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
/**
* Copyright (c) 2025 Mustafa Yemural - www.mustafayemural.com
* Released under the MIT License
* https://opensource.org/licenses/MIT
*/
#include "VulkanApplication.h"
#include <algorithm>
#include <array>
#include "AppCommonConfig.h"
#include "AppConfig.h"
#include "ApplicationData.h"
#include "ShaderLoader.h"
#include "VulkanShaderModule.h"
namespace examples::advanced_shader_programming::mesh_and_task_shaders::triangles_with_mesh_shader
{
using namespace constants;
using namespace common::asset_manager;
using namespace common::utility;
using namespace common::vulkan_wrapper;
using namespace common::vulkan_framework;
using namespace common::window_wrapper;
VulkanApplication::VulkanApplication(ParameterServer&& params) : ApplicationMeshAndTaskShaders(std::move(params)) {}
bool VulkanApplication::Init()
{
if (!ApplicationMeshAndTaskShaders::Init()) {
std::cerr << "Application pre-init failed!" << '\n';
return false;
}
try {
InitAssetManager();
CreateInitialResources();
CreateRenderPass();
CreatePipelines();
CreateFramebuffers();
CreateCommandBuffers();
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
return false;
}
return true;
}
void VulkanApplication::DrawFrame()
{
inFlightFences_[currentFrameIndex_]->WaitForFence(true, UINT64_MAX);
uint32_t imageIndex = swapChain_->AcquireNextImage(imageAvailableSemaphores_[currentFrameIndex_], nullptr);
if (swapImagesFences_[imageIndex] != nullptr) {
swapImagesFences_[imageIndex]->WaitForFence(true, UINT64_MAX);
}
inFlightFences_[currentFrameIndex_]->ResetFence();
swapImagesFences_[imageIndex] = inFlightFences_[currentFrameIndex_];
RecordPresentCommandBuffers(imageIndex);
queue_->Submit({cmdBuffersPresent_[imageIndex]}, {imageAvailableSemaphores_[currentFrameIndex_]},
{renderFinishedSemaphores_[imageIndex]}, inFlightFences_[currentFrameIndex_],
{VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT});
queue_->Present({swapChain_}, {imageIndex}, {renderFinishedSemaphores_[imageIndex]});
currentFrameIndex_ = (currentFrameIndex_ + 1) % MAX_FRAMES_IN_FLIGHT;
}
void VulkanApplication::PreUpdate()
{
// Poll events
ApplicationMeshAndTaskShaders::PreUpdate();
// Process continuous inputs
ProcessInput();
}
void VulkanApplication::InitAssetManager()
{
assetManager_ = std::make_unique<AssetManager>();
assetManager_->RegisterLoader<ShaderAsset>(std::make_unique<ShaderLoader>(SHADERS_DIR, SHADER_TYPE));
}
void VulkanApplication::CreateInitialResources() const
{
ResourceDescriptor resourceCreateInfo;
// Fill shader module create infos
const auto mainMeshShaderAsset = assetManager_->Load<ShaderAsset>(kMainMeshShaderFile);
const auto mainFragmentShaderAsset = assetManager_->Load<ShaderAsset>(kMainFragmentShaderFile);
resourceCreateInfo.shaders = {
.modules = {
{.name = kMainMeshShaderKey, .asset = assetManager_->Get(mainMeshShaderAsset)},
{.name = kMainFragmentShaderKey, .asset = assetManager_->Get(mainFragmentShaderAsset)}}};
CreateVulkanResources(resourceCreateInfo);
}
void VulkanApplication::CreateRenderPass()
{
VkAttachmentReference colorAttachmentRef{0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
renderPass_ = device_->CreateRenderPass([&](auto& builder) {
builder.AddAttachment([](auto& attachmentCreateInfo) {
attachmentCreateInfo.format = VK_FORMAT_B8G8R8A8_SRGB;
attachmentCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
attachmentCreateInfo.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachmentCreateInfo.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachmentCreateInfo.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachmentCreateInfo.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachmentCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachmentCreateInfo.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
})
.AddSubpass([&](auto& subpassCreateInfo) {
subpassCreateInfo.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpassCreateInfo.colorAttachmentCount = 1;
subpassCreateInfo.pColorAttachments = &colorAttachmentRef;
subpassCreateInfo.pDepthStencilAttachment = nullptr;
});
});
if (!renderPass_) {
throw std::runtime_error("Failed to create render pass!");
}
}
void VulkanApplication::CreatePipelines()
{
VkPushConstantRange pushConstant;
pushConstant.offset = 0;
pushConstant.size = sizeof(MeshPushConstants);
pushConstant.stageFlags = VK_SHADER_STAGE_MESH_BIT_EXT;
pipelineLayout_ = device_->CreatePipelineLayout({}, {pushConstant});
if (!pipelineLayout_) {
throw std::runtime_error("Failed to create pipeline layout!");
}
VkViewport viewport{0, 0, static_cast<float>(currentWindowWidth_), static_cast<float>(currentWindowHeight_),
0.0f, 1.0f};
VkRect2D scissor{0, 0, currentWindowWidth_, currentWindowHeight_};
VkPipelineColorBlendAttachmentState colorBlendAttachment{};
colorBlendAttachment.blendEnable = VK_FALSE;
colorBlendAttachment.colorWriteMask =
VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
scenePipeline_ = device_->CreateGraphicsPipeline(pipelineLayout_, renderPass_, [&](auto& builder) {
builder.AddShaderStage([&](auto& shaderStageCreateInfo) {
shaderStageCreateInfo.stage = VK_SHADER_STAGE_MESH_BIT_EXT;
shaderStageCreateInfo.module = resources_->GetShaderModule(kMainMeshShaderKey)->GetHandle();
});
builder.AddShaderStage([&](auto& shaderStageCreateInfo) {
shaderStageCreateInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
shaderStageCreateInfo.module = resources_->GetShaderModule(kMainFragmentShaderKey)->GetHandle();
});
builder.SetViewportState([&](auto& viewportStateCreateInfo) {
viewportStateCreateInfo.viewportCount = 1;
viewportStateCreateInfo.pViewports = &viewport;
viewportStateCreateInfo.scissorCount = 1;
viewportStateCreateInfo.pScissors = &scissor;
});
builder.SetColorBlendState([&](auto& blendStateCreateInfo) {
blendStateCreateInfo.attachmentCount = 1;
blendStateCreateInfo.pAttachments = &colorBlendAttachment;
});
});
if (!scenePipeline_) {
throw std::runtime_error("Failed to create graphics pipeline (for scene objects)!");
}
}
void VulkanApplication::CreateFramebuffers()
{
for (const auto& swapImage: swapChainImageViews_) {
auto framebuffer = device_->CreateFramebuffer(renderPass_, {swapImage}, [&](auto& builder) {
builder.SetDimensions(currentWindowWidth_, currentWindowHeight_);
});
if (!framebuffer) {
throw std::runtime_error("Failed to create framebuffer!");
}
framebuffers_.push_back(framebuffer);
}
}
void VulkanApplication::CreateCommandBuffers()
{
cmdBuffersPresent_ = cmdPool_->CreateCommandBuffers(framebuffers_.size(), VK_COMMAND_BUFFER_LEVEL_PRIMARY);
if (cmdBuffersPresent_.empty()) {
throw std::runtime_error("Failed to create command buffers!");
}
}
void VulkanApplication::RecordPresentCommandBuffers(const std::uint32_t currentImageIndex)
{
std::array<VkClearValue, 1> clearValues{};
clearValues[0].color = params_.Get<VkClearColorValue>(AppSettings::ClearColor);
const auto& currentCmdBuffer = cmdBuffersPresent_[currentImageIndex];
if (!currentCmdBuffer->BeginCommandBuffer(nullptr)) {
throw std::runtime_error("Failed to begin recording command buffer!");
}
currentCmdBuffer->BeginRenderPass(
[&](auto& beginInfo) {
beginInfo.renderPass = renderPass_->GetHandle();
beginInfo.framebuffer = framebuffers_[currentImageIndex]->GetHandle();
beginInfo.renderArea.offset = {0, 0};
beginInfo.renderArea.extent = VkExtent2D(currentWindowWidth_, currentWindowHeight_);
beginInfo.clearValueCount = clearValues.size();
beginInfo.pClearValues = clearValues.data();
},
VK_SUBPASS_CONTENTS_INLINE);
currentCmdBuffer->BindPipeline(scenePipeline_, VK_PIPELINE_BIND_POINT_GRAPHICS);
MeshPushConstants meshPushConstants{};
meshPushConstants.triangleCount = currentTriangleCount_;
currentCmdBuffer->PushConstants(pipelineLayout_, VK_SHADER_STAGE_MESH_BIT_EXT, 0,
sizeof(meshPushConstants), &meshPushConstants);
currentCmdBuffer->DrawMeshTasksEXT(1, 1, 1);
currentCmdBuffer->EndRenderPass();
if (!currentCmdBuffer->EndCommandBuffer()) {
throw std::runtime_error("Failed to end recording command buffer!");
}
}
void VulkanApplication::ProcessInput()
{
// Set triangle count
if (window_->IsKeyPressed(GLFW_KEY_1)) {
currentTriangleCount_ = 1;
}
if (window_->IsKeyPressed(GLFW_KEY_2)) {
currentTriangleCount_ = 2;
}
if (window_->IsKeyPressed(GLFW_KEY_3)) {
currentTriangleCount_ = 3;
}
}
} // namespace examples::advanced_shader_programming::mesh_and_task_shaders::triangles_with_mesh_shader