Skip to content

Commit 3fb6727

Browse files
committed
Refactor2
1 parent 0ae859b commit 3fb6727

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2855
-2656
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Vulkan SDK本身由C编写,这带来更好的跨语言能力,可以其他语
9393

9494

9595

96-
### 项目参考资料
96+
### 参考资料
9797

9898
Vulkan-hpp文档 [vulkan-hpp](https://github.com/KhronosGroup/Vulkan-Hpp)
9999

docs/codes/0122_fixfunction/main.cpp

Lines changed: 72 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <optional>
1414
#include <stdexcept>
1515

16-
1716
class HelloTriangleApplication {
1817
public:
1918
void run() {
@@ -26,20 +25,20 @@ class HelloTriangleApplication {
2625
private:
2726
/////////////////////////////////////////////////////////////////
2827
/// static values
29-
static const uint32_t WIDTH = 800;
30-
static const uint32_t HEIGHT = 600;
28+
static constexpr uint32_t WIDTH = 800;
29+
static constexpr uint32_t HEIGHT = 600;
3130

32-
static constexpr std::array<const char*,1> validationLayers {
31+
inline static const std::vector<const char*> validationLayers {
3332
"VK_LAYER_KHRONOS_validation"
3433
};
35-
static constexpr std::array<const char*,1> deviceExtensions {
34+
inline static const std::vector<const char*> deviceExtensions {
3635
VK_KHR_SWAPCHAIN_EXTENSION_NAME
3736
};
3837

3938
#ifdef NDEBUG
40-
static const bool enableValidationLayers = false;
39+
static constexpr bool enableValidationLayers = false;
4140
#else
42-
static const bool enableValidationLayers = true;
41+
static constexpr bool enableValidationLayers = true;
4342
#endif
4443
/////////////////////////////////////////////////////////////////
4544

@@ -61,19 +60,15 @@ class HelloTriangleApplication {
6160
std::vector<vk::raii::ImageView> m_swapChainImageViews;
6261
vk::raii::PipelineLayout m_pipelineLayout{ nullptr };
6362
/////////////////////////////////////////////////////////////////
64-
63+
6564
/////////////////////////////////////////////////////////////////
6665
/// run()
6766
void initWindow() {
68-
// initialize glfw lib
6967
glfwInit();
70-
71-
// Configure GLFW to not use OpenGL
68+
7269
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
73-
// Temporarily disable window resizing to simplify operations
7470
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
75-
76-
// Create window
71+
7772
m_window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
7873
}
7974

@@ -101,7 +96,7 @@ class HelloTriangleApplication {
10196
/////////////////////////////////////////////////////////////////
10297

10398
/////////////////////////////////////////////////////////////////
104-
/// create instance
99+
/// instance creation
105100
std::vector<const char*> getRequiredExtensions() {
106101
uint32_t glfwExtensionCount = 0;
107102
const char** glfwExtensions;
@@ -116,7 +111,6 @@ class HelloTriangleApplication {
116111

117112
return extensions;
118113
}
119-
120114
void createInstance(){
121115
if (enableValidationLayers && !checkValidationLayerSupport()) {
122116
throw std::runtime_error("validation layers requested, but not available!");
@@ -129,20 +123,12 @@ class HelloTriangleApplication {
129123
1, // engineVersion
130124
VK_API_VERSION_1_1 // apiVersion
131125
);
132-
126+
133127
vk::InstanceCreateInfo createInfo(
134128
{}, // vk::InstanceCreateFlags
135129
&applicationInfo // vk::ApplicationInfo*
136130
);
137131

138-
// std::vector<vk::ExtensionProperties>
139-
auto extensions = m_context.enumerateInstanceExtensionProperties();
140-
std::cout << "available extensions:\n";
141-
142-
for (const auto& extension : extensions) {
143-
std::cout << '\t' << extension.extensionName << std::endl;
144-
}
145-
146132
std::vector<const char*> requiredExtensions = getRequiredExtensions();
147133
// special setter
148134
createInfo.setPEnabledExtensionNames( requiredExtensions );
@@ -155,6 +141,13 @@ class HelloTriangleApplication {
155141
createInfo.pNext = &debugMessengerCreateInfo;
156142
}
157143

144+
auto extensions = m_context.enumerateInstanceExtensionProperties();
145+
std::cout << "available extensions:\n";
146+
147+
for (const auto& extension : extensions) {
148+
std::cout << '\t' << extension.extensionName << std::endl;
149+
}
150+
158151
m_instance = m_context.createInstance( createInfo );
159152
}
160153
/////////////////////////////////////////////////////////////////
@@ -202,6 +195,14 @@ class HelloTriangleApplication {
202195

203196
/////////////////////////////////////////////////////////////////
204197
/// physical device
198+
struct QueueFamilyIndices {
199+
std::optional<uint32_t> graphicsFamily;
200+
std::optional<uint32_t> presentFamily;
201+
202+
bool isComplete() {
203+
return graphicsFamily.has_value() && presentFamily.has_value();
204+
}
205+
};
205206
bool checkDeviceExtensionSupport(const vk::raii::PhysicalDevice& physicalDevice) {
206207
// std::vector<vk::ExtensionProperties>
207208
auto availableExtensions = physicalDevice.enumerateDeviceExtensionProperties();
@@ -241,20 +242,11 @@ class HelloTriangleApplication {
241242
throw std::runtime_error("failed to find a suitable GPU!");
242243
}
243244
}
244-
struct QueueFamilyIndices {
245-
std::optional<uint32_t> graphicsFamily;
246-
std::optional<uint32_t> presentFamily;
247-
248-
bool isComplete() {
249-
return graphicsFamily.has_value() && presentFamily.has_value();
250-
}
251-
};
252245
QueueFamilyIndices findQueueFamilies(const vk::raii::PhysicalDevice& physicalDevice) {
253246
QueueFamilyIndices indices;
254247

255248
// std::vector<vk::QueueFamilyProperties>
256249
auto queueFamilies = physicalDevice.getQueueFamilyProperties();
257-
258250
for (int i = 0; const auto& queueFamily : queueFamilies) {
259251
if (queueFamily.queueFlags & vk::QueueFlagBits::eGraphics) {
260252
indices.graphicsFamily = i;
@@ -272,7 +264,7 @@ class HelloTriangleApplication {
272264
return indices;
273265
}
274266
/////////////////////////////////////////////////////////////////
275-
267+
276268
/////////////////////////////////////////////////////////////////
277269
/// logical device
278270
void createLogicalDevice() {
@@ -284,12 +276,10 @@ class HelloTriangleApplication {
284276

285277
float queuePriority = 1.0f;
286278
for (uint32_t queueFamily : uniqueQueueFamilies) {
287-
queueCreateInfos.emplace_back( vk::DeviceQueueCreateInfo(
288-
{}, // flags
289-
indices.graphicsFamily.value(), // queueFamilyIndex
290-
1, // queueCount
291-
&queuePriority
292-
));
279+
vk::DeviceQueueCreateInfo queueCreateInfo;
280+
queueCreateInfo.queueFamilyIndex = queueFamily;
281+
queueCreateInfo.setQueuePriorities( queuePriority );
282+
queueCreateInfos.emplace_back( queueCreateInfo );
293283
}
294284

295285
vk::PhysicalDeviceFeatures deviceFeatures;
@@ -298,12 +288,12 @@ class HelloTriangleApplication {
298288
createInfo.setQueueCreateInfos( queueCreateInfos );
299289
createInfo.pEnabledFeatures = &deviceFeatures;
300290

291+
createInfo.setPEnabledExtensionNames( deviceExtensions );
292+
301293
if (enableValidationLayers) {
302294
createInfo.setPEnabledLayerNames( validationLayers );
303295
}
304296

305-
createInfo.setPEnabledExtensionNames( deviceExtensions );
306-
307297
m_device = m_physicalDevice.createDevice( createInfo );
308298
m_graphicsQueue = m_device.getQueue( indices.graphicsFamily.value(), 0 );
309299
m_presentQueue = m_device.getQueue( indices.presentFamily.value(), 0 );
@@ -381,22 +371,19 @@ class HelloTriangleApplication {
381371
vk::Extent2D extent = chooseSwapExtent( swapChainSupport.capabilities );
382372

383373
uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
384-
385374
if (swapChainSupport.capabilities.maxImageCount > 0 &&
386375
imageCount > swapChainSupport.capabilities.maxImageCount) {
387376
imageCount = swapChainSupport.capabilities.maxImageCount;
388377
}
389378

390-
vk::SwapchainCreateInfoKHR createInfo(
391-
{}, // flags
392-
m_surface, // vk::Surface
393-
imageCount, // minImageCount
394-
surfaceFormat.format, // Format
395-
surfaceFormat.colorSpace, // ColorSpaceKHR
396-
extent, // Extent2D
397-
1, // imageArrayLayers
398-
vk::ImageUsageFlagBits::eColorAttachment // imageUsage
399-
);
379+
vk::SwapchainCreateInfoKHR createInfo;
380+
createInfo.surface = m_surface;
381+
createInfo.minImageCount = imageCount;
382+
createInfo.imageFormat = surfaceFormat.format;
383+
createInfo.imageColorSpace = surfaceFormat.colorSpace;
384+
createInfo.imageExtent = extent;
385+
createInfo.imageArrayLayers = 1;
386+
createInfo.imageUsage = vk::ImageUsageFlagBits::eColorAttachment;
400387

401388
QueueFamilyIndices indices = findQueueFamilies( m_physicalDevice );
402389
std::vector<uint32_t> queueFamilyIndices { indices.graphicsFamily.value(), indices.presentFamily.value() };
@@ -426,12 +413,10 @@ class HelloTriangleApplication {
426413
void createImageViews() {
427414
m_swapChainImageViews.reserve( m_swapChainImages.size() );
428415
for (size_t i = 0; i < m_swapChainImages.size(); i++) {
429-
vk::ImageViewCreateInfo createInfo(
430-
{}, // flags
431-
m_swapChainImages[i], // vk::Image
432-
vk::ImageViewType::e2D, // ImageViewType
433-
m_swapChainImageFormat // format
434-
);
416+
vk::ImageViewCreateInfo createInfo;
417+
createInfo.image = m_swapChainImages[i];
418+
createInfo.viewType = vk::ImageViewType::e2D;
419+
createInfo.format = m_swapChainImageFormat;
435420
createInfo.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
436421
createInfo.subresourceRange.baseMipLevel = 0;
437422
createInfo.subresourceRange.levelCount = 1;
@@ -451,23 +436,20 @@ class HelloTriangleApplication {
451436
if (!file.is_open()) {
452437
throw std::runtime_error("failed to open file!");
453438
}
454-
455439
size_t fileSize = (size_t) file.tellg();
456440
std::vector<char> buffer(fileSize);
441+
457442
file.seekg(0);
458443
file.read(buffer.data(), fileSize);
459444
file.close();
460445

461446
return buffer;
462447
}
463448
vk::raii::ShaderModule createShaderModule(const std::vector<char>& code) {
464-
vk::ShaderModuleCreateInfo createInfo(
465-
{}, // flags
466-
code.size(), // codeSize
467-
reinterpret_cast<const uint32_t*>(code.data()) // pCode
468-
);
469-
470-
return m_device.createShaderModule( createInfo );
449+
vk::ShaderModuleCreateInfo createInfo;
450+
createInfo.codeSize = code.size();
451+
createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
452+
return m_device.createShaderModule(createInfo);
471453
}
472454
void createGraphicsPipeline() {
473455
auto vertShaderCode = readFile("shaders/vert.spv");
@@ -476,22 +458,18 @@ class HelloTriangleApplication {
476458
vk::raii::ShaderModule vertShaderModule = createShaderModule(vertShaderCode);
477459
vk::raii::ShaderModule fragShaderModule = createShaderModule(fragShaderCode);
478460

479-
vk::PipelineShaderStageCreateInfo vertShaderStageInfo(
480-
{}, // flags
481-
vk::ShaderStageFlagBits::eVertex, // stage
482-
vertShaderModule, // ShaderModule
483-
"main" // pName
484-
);
461+
vk::PipelineShaderStageCreateInfo vertShaderStageInfo;
462+
vertShaderStageInfo.stage = vk::ShaderStageFlagBits::eVertex;
463+
vertShaderStageInfo.module = vertShaderModule;
464+
vertShaderStageInfo.pName = "main";
485465

486-
vk::PipelineShaderStageCreateInfo fragShaderStageInfo(
487-
{}, // flags
488-
vk::ShaderStageFlagBits::eFragment, // stage
489-
fragShaderModule, // ShaderModule
490-
"main" // pName
491-
);
466+
vk::PipelineShaderStageCreateInfo fragShaderStageInfo;
467+
fragShaderStageInfo.stage = vk::ShaderStageFlagBits::eFragment;
468+
fragShaderStageInfo.module = fragShaderModule;
469+
fragShaderStageInfo.pName = "main";
492470

493471
std::vector<vk::PipelineShaderStageCreateInfo> shaderStages{ vertShaderStageInfo, fragShaderStageInfo };
494-
472+
495473
std::vector<vk::DynamicState> dynamicStates = {
496474
vk::DynamicState::eViewport,
497475
vk::DynamicState::eScissor
@@ -502,27 +480,13 @@ class HelloTriangleApplication {
502480

503481
vk::PipelineVertexInputStateCreateInfo vertexInputInfo;
504482

505-
vk::PipelineInputAssemblyStateCreateInfo inputAssembly(
506-
{}, // flags
507-
vk::PrimitiveTopology::eTriangleList, // topology
508-
false // primitiveRestartEnable - default false
509-
);
510-
511-
vk::Viewport viewport(
512-
0.0f, 0.0f, // x y
513-
static_cast<float>(m_swapChainExtent.width), // width
514-
static_cast<float>(m_swapChainExtent.height), // height
515-
0.0f, 1.0f // minDepth maxDepth
516-
);
517-
518-
vk::Rect2D scissor(
519-
{0, 0}, // offset
520-
m_swapChainExtent // Extent2D
521-
);
483+
vk::PipelineInputAssemblyStateCreateInfo inputAssembly;
484+
inputAssembly.topology = vk::PrimitiveTopology::eTriangleList;
485+
inputAssembly.primitiveRestartEnable = false; // default
522486

523487
vk::PipelineViewportStateCreateInfo viewportState;
524-
viewportState.setViewports( viewport );
525-
viewportState.setScissors( scissor );
488+
viewportState.viewportCount = 1;
489+
viewportState.scissorCount = 1;
526490

527491
vk::PipelineRasterizationStateCreateInfo rasterizer;
528492
rasterizer.depthClampEnable = false;
@@ -533,23 +497,16 @@ class HelloTriangleApplication {
533497
rasterizer.frontFace = vk::FrontFace::eClockwise;
534498
rasterizer.depthBiasEnable = false;
535499

536-
vk::PipelineMultisampleStateCreateInfo multisampling(
537-
{}, // flags
538-
vk::SampleCountFlagBits::e1, // rasterizationSamples
539-
false // sampleShadingEnable
540-
);
500+
vk::PipelineMultisampleStateCreateInfo multisampling;
501+
multisampling.rasterizationSamples = vk::SampleCountFlagBits::e1;
502+
multisampling.sampleShadingEnable = false; // default
541503

542-
vk::PipelineColorBlendAttachmentState colorBlendAttachment(
543-
false, // blendEnable
544-
vk::BlendFactor::eOne, // srcColorBlendFactor - optional
545-
vk::BlendFactor::eZero, // dstColorBlendFactor - optional
546-
vk::BlendOp::eAdd, // colorBlendOp - optional
547-
vk::BlendFactor::eOne, // srcAlphaBlendFactor - optional
548-
vk::BlendFactor::eZero, // dstAlphaBlendFactor - optional
549-
vk::BlendOp::eAdd, // alphaBlendOp - optional
550-
vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
504+
vk::PipelineColorBlendAttachmentState colorBlendAttachment;
505+
colorBlendAttachment.blendEnable = false; // default
506+
// colorBlendAttachment.colorWriteMask = vk::FlagTraits<vk::ColorComponentFlagBits>::allFlags;
507+
colorBlendAttachment.colorWriteMask = (
508+
vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
551509
vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA
552-
// colorWriteMask - default is RGBA
553510
);
554511

555512
vk::PipelineColorBlendStateCreateInfo colorBlending;

0 commit comments

Comments
 (0)