Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 550b68f

Browse files
committed
Change VDeleter to no longer cleanup in & overload
1 parent c276f4d commit 550b68f

28 files changed

Lines changed: 735 additions & 343 deletions

code/base_code.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ class VDeleter {
2929
cleanup();
3030
}
3131

32-
T* operator &() {
32+
const T* operator &() const {
33+
return &object;
34+
}
35+
36+
T* replace() {
3337
cleanup();
3438
return &object;
3539
}
@@ -38,6 +42,16 @@ class VDeleter {
3842
return object;
3943
}
4044

45+
void operator=(T rhs) {
46+
cleanup();
47+
object = rhs;
48+
}
49+
50+
template<typename V>
51+
bool operator==(V rhs) {
52+
return object == T(rhs);
53+
}
54+
4155
private:
4256
T object{VK_NULL_HANDLE};
4357
std::function<void(T)> deleter;

code/command_buffers.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ class VDeleter {
6464
cleanup();
6565
}
6666

67-
T* operator &() {
67+
const T* operator &() const {
68+
return &object;
69+
}
70+
71+
T* replace() {
6872
cleanup();
6973
return &object;
7074
}
@@ -73,6 +77,16 @@ class VDeleter {
7377
return object;
7478
}
7579

80+
void operator=(T rhs) {
81+
cleanup();
82+
object = rhs;
83+
}
84+
85+
template<typename V>
86+
bool operator==(V rhs) {
87+
return object == T(rhs);
88+
}
89+
7690
private:
7791
T object{VK_NULL_HANDLE};
7892
std::function<void(T)> deleter;
@@ -193,7 +207,7 @@ class HelloTriangleApplication {
193207
createInfo.enabledLayerCount = 0;
194208
}
195209

196-
if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
210+
if (vkCreateInstance(&createInfo, nullptr, instance.replace()) != VK_SUCCESS) {
197211
throw std::runtime_error("failed to create instance!");
198212
}
199213
}
@@ -206,13 +220,13 @@ class HelloTriangleApplication {
206220
createInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
207221
createInfo.pfnCallback = debugCallback;
208222

209-
if (CreateDebugReportCallbackEXT(instance, &createInfo, nullptr, &callback) != VK_SUCCESS) {
223+
if (CreateDebugReportCallbackEXT(instance, &createInfo, nullptr, callback.replace()) != VK_SUCCESS) {
210224
throw std::runtime_error("failed to set up debug callback!");
211225
}
212226
}
213227

214228
void createSurface() {
215-
if (glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS) {
229+
if (glfwCreateWindowSurface(instance, window, nullptr, surface.replace()) != VK_SUCCESS) {
216230
throw std::runtime_error("failed to create window surface!");
217231
}
218232
}
@@ -276,7 +290,7 @@ class HelloTriangleApplication {
276290
createInfo.enabledLayerCount = 0;
277291
}
278292

279-
if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
293+
if (vkCreateDevice(physicalDevice, &createInfo, nullptr, device.replace()) != VK_SUCCESS) {
280294
throw std::runtime_error("failed to create logical device!");
281295
}
282296

@@ -325,7 +339,7 @@ class HelloTriangleApplication {
325339

326340
createInfo.oldSwapchain = VK_NULL_HANDLE;
327341

328-
if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
342+
if (vkCreateSwapchainKHR(device, &createInfo, nullptr, swapChain.replace()) != VK_SUCCESS) {
329343
throw std::runtime_error("failed to create swap chain!");
330344
}
331345

@@ -356,7 +370,7 @@ class HelloTriangleApplication {
356370
createInfo.subresourceRange.baseArrayLayer = 0;
357371
createInfo.subresourceRange.layerCount = 1;
358372

359-
if (vkCreateImageView(device, &createInfo, nullptr, &swapChainImageViews[i]) != VK_SUCCESS) {
373+
if (vkCreateImageView(device, &createInfo, nullptr, swapChainImageViews[i].replace()) != VK_SUCCESS) {
360374
throw std::runtime_error("failed to create image views!");
361375
}
362376
}
@@ -389,7 +403,7 @@ class HelloTriangleApplication {
389403
renderPassInfo.subpassCount = 1;
390404
renderPassInfo.pSubpasses = &subPass;
391405

392-
if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
406+
if (vkCreateRenderPass(device, &renderPassInfo, nullptr, renderPass.replace()) != VK_SUCCESS) {
393407
throw std::runtime_error("failed to create render pass!");
394408
}
395409
}
@@ -481,7 +495,7 @@ class HelloTriangleApplication {
481495
pipelineLayoutInfo.setLayoutCount = 0;
482496
pipelineLayoutInfo.pushConstantRangeCount = 0;
483497

484-
if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
498+
if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, pipelineLayout.replace()) != VK_SUCCESS) {
485499
throw std::runtime_error("failed to create pipeline layout!");
486500
}
487501

@@ -500,7 +514,7 @@ class HelloTriangleApplication {
500514
pipelineInfo.subpass = 0;
501515
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
502516

503-
if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
517+
if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, graphicsPipeline.replace()) != VK_SUCCESS) {
504518
throw std::runtime_error("failed to create graphics pipeline!");
505519
}
506520
}
@@ -522,7 +536,7 @@ class HelloTriangleApplication {
522536
framebufferInfo.height = swapChainExtent.height;
523537
framebufferInfo.layers = 1;
524538

525-
if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
539+
if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, swapChainFramebuffers[i].replace()) != VK_SUCCESS) {
526540
throw std::runtime_error("failed to create framebuffer!");
527541
}
528542
}
@@ -535,7 +549,7 @@ class HelloTriangleApplication {
535549
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
536550
poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily;
537551

538-
if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
552+
if (vkCreateCommandPool(device, &poolInfo, nullptr, commandPool.replace()) != VK_SUCCESS) {
539553
throw std::runtime_error("failed to create command pool!");
540554
}
541555
}
@@ -591,7 +605,7 @@ class HelloTriangleApplication {
591605
createInfo.codeSize = code.size();
592606
createInfo.pCode = (uint32_t*) code.data();
593607

594-
if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
608+
if (vkCreateShaderModule(device, &createInfo, nullptr, shaderModule.replace()) != VK_SUCCESS) {
595609
throw std::runtime_error("failed to create shader module!");
596610
}
597611
}

code/depth_buffering.cpp

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ class VDeleter {
7474
cleanup();
7575
}
7676

77-
T* operator &() {
77+
const T* operator &() const {
78+
return &object;
79+
}
80+
81+
T* replace() {
7882
cleanup();
7983
return &object;
8084
}
@@ -83,6 +87,16 @@ class VDeleter {
8387
return object;
8488
}
8589

90+
void operator=(T rhs) {
91+
cleanup();
92+
object = rhs;
93+
}
94+
95+
template<typename V>
96+
bool operator==(V rhs) {
97+
return object == T(rhs);
98+
}
99+
86100
private:
87101
T object{VK_NULL_HANDLE};
88102
std::function<void(T)> deleter;
@@ -326,7 +340,7 @@ class HelloTriangleApplication {
326340
createInfo.enabledLayerCount = 0;
327341
}
328342

329-
if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
343+
if (vkCreateInstance(&createInfo, nullptr, instance.replace()) != VK_SUCCESS) {
330344
throw std::runtime_error("failed to create instance!");
331345
}
332346
}
@@ -339,13 +353,13 @@ class HelloTriangleApplication {
339353
createInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
340354
createInfo.pfnCallback = debugCallback;
341355

342-
if (CreateDebugReportCallbackEXT(instance, &createInfo, nullptr, &callback) != VK_SUCCESS) {
356+
if (CreateDebugReportCallbackEXT(instance, &createInfo, nullptr, callback.replace()) != VK_SUCCESS) {
343357
throw std::runtime_error("failed to set up debug callback!");
344358
}
345359
}
346360

347361
void createSurface() {
348-
if (glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS) {
362+
if (glfwCreateWindowSurface(instance, window, nullptr, surface.replace()) != VK_SUCCESS) {
349363
throw std::runtime_error("failed to create window surface!");
350364
}
351365
}
@@ -409,7 +423,7 @@ class HelloTriangleApplication {
409423
createInfo.enabledLayerCount = 0;
410424
}
411425

412-
if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
426+
if (vkCreateDevice(physicalDevice, &createInfo, nullptr, device.replace()) != VK_SUCCESS) {
413427
throw std::runtime_error("failed to create logical device!");
414428
}
415429

@@ -464,7 +478,7 @@ class HelloTriangleApplication {
464478
throw std::runtime_error("failed to create swap chain!");
465479
}
466480

467-
*&swapChain = newSwapChain;
481+
swapChain = newSwapChain;
468482

469483
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
470484
swapChainImages.resize(imageCount);
@@ -535,7 +549,7 @@ class HelloTriangleApplication {
535549
renderPassInfo.dependencyCount = 1;
536550
renderPassInfo.pDependencies = &dependency;
537551

538-
if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
552+
if (vkCreateRenderPass(device, &renderPassInfo, nullptr, renderPass.replace()) != VK_SUCCESS) {
539553
throw std::runtime_error("failed to create render pass!");
540554
}
541555
}
@@ -561,7 +575,7 @@ class HelloTriangleApplication {
561575
layoutInfo.bindingCount = bindings.size();
562576
layoutInfo.pBindings = bindings.data();
563577

564-
if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS) {
578+
if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, descriptorSetLayout.replace()) != VK_SUCCESS) {
565579
throw std::runtime_error("failed to create descriptor set layout!");
566580
}
567581
}
@@ -668,7 +682,7 @@ class HelloTriangleApplication {
668682
pipelineLayoutInfo.setLayoutCount = 1;
669683
pipelineLayoutInfo.pSetLayouts = setLayouts;
670684

671-
if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
685+
if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, pipelineLayout.replace()) != VK_SUCCESS) {
672686
throw std::runtime_error("failed to create pipeline layout!");
673687
}
674688

@@ -688,7 +702,7 @@ class HelloTriangleApplication {
688702
pipelineInfo.subpass = 0;
689703
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
690704

691-
if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
705+
if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, graphicsPipeline.replace()) != VK_SUCCESS) {
692706
throw std::runtime_error("failed to create graphics pipeline!");
693707
}
694708
}
@@ -711,7 +725,7 @@ class HelloTriangleApplication {
711725
framebufferInfo.height = swapChainExtent.height;
712726
framebufferInfo.layers = 1;
713727

714-
if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
728+
if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, swapChainFramebuffers[i].replace()) != VK_SUCCESS) {
715729
throw std::runtime_error("failed to create framebuffer!");
716730
}
717731
}
@@ -724,7 +738,7 @@ class HelloTriangleApplication {
724738
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
725739
poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily;
726740

727-
if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
741+
if (vkCreateCommandPool(device, &poolInfo, nullptr, commandPool.replace()) != VK_SUCCESS) {
728742
throw std::runtime_error("failed to create graphics command pool!");
729743
}
730744
}
@@ -810,7 +824,7 @@ class HelloTriangleApplication {
810824
samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
811825
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
812826

813-
if (vkCreateSampler(device, &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS) {
827+
if (vkCreateSampler(device, &samplerInfo, nullptr, textureSampler.replace()) != VK_SUCCESS) {
814828
throw std::runtime_error("failed to create texture sampler!");
815829
}
816830
}
@@ -827,7 +841,7 @@ class HelloTriangleApplication {
827841
viewInfo.subresourceRange.baseArrayLayer = 0;
828842
viewInfo.subresourceRange.layerCount = 1;
829843

830-
if (vkCreateImageView(device, &viewInfo, nullptr, &imageView) != VK_SUCCESS) {
844+
if (vkCreateImageView(device, &viewInfo, nullptr, imageView.replace()) != VK_SUCCESS) {
831845
throw std::runtime_error("failed to create texture image view!");
832846
}
833847
}
@@ -848,7 +862,7 @@ class HelloTriangleApplication {
848862
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
849863
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
850864

851-
if (vkCreateImage(device, &imageInfo, nullptr, &image) != VK_SUCCESS) {
865+
if (vkCreateImage(device, &imageInfo, nullptr, image.replace()) != VK_SUCCESS) {
852866
throw std::runtime_error("failed to create image!");
853867
}
854868

@@ -860,7 +874,7 @@ class HelloTriangleApplication {
860874
allocInfo.allocationSize = memRequirements.size;
861875
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
862876

863-
if (vkAllocateMemory(device, &allocInfo, nullptr, &imageMemory) != VK_SUCCESS) {
877+
if (vkAllocateMemory(device, &allocInfo, nullptr, imageMemory.replace()) != VK_SUCCESS) {
864878
throw std::runtime_error("failed to allocate image memory!");
865879
}
866880

@@ -999,7 +1013,7 @@ class HelloTriangleApplication {
9991013
poolInfo.pPoolSizes = poolSizes.data();
10001014
poolInfo.maxSets = 1;
10011015

1002-
if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &descriptorPool) != VK_SUCCESS) {
1016+
if (vkCreateDescriptorPool(device, &poolInfo, nullptr, descriptorPool.replace()) != VK_SUCCESS) {
10031017
throw std::runtime_error("failed to create descriptor pool!");
10041018
}
10051019
}
@@ -1054,7 +1068,7 @@ class HelloTriangleApplication {
10541068
bufferInfo.usage = usage;
10551069
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
10561070

1057-
if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
1071+
if (vkCreateBuffer(device, &bufferInfo, nullptr, buffer.replace()) != VK_SUCCESS) {
10581072
throw std::runtime_error("failed to create buffer!");
10591073
}
10601074

@@ -1066,7 +1080,7 @@ class HelloTriangleApplication {
10661080
allocInfo.allocationSize = memRequirements.size;
10671081
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
10681082

1069-
if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
1083+
if (vkAllocateMemory(device, &allocInfo, nullptr, bufferMemory.replace()) != VK_SUCCESS) {
10701084
throw std::runtime_error("failed to allocate buffer memory!");
10711085
}
10721086

@@ -1193,8 +1207,8 @@ class HelloTriangleApplication {
11931207
VkSemaphoreCreateInfo semaphoreInfo = {};
11941208
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
11951209

1196-
if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphore) != VK_SUCCESS ||
1197-
vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphore) != VK_SUCCESS) {
1210+
if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, imageAvailableSemaphore.replace()) != VK_SUCCESS ||
1211+
vkCreateSemaphore(device, &semaphoreInfo, nullptr, renderFinishedSemaphore.replace()) != VK_SUCCESS) {
11981212

11991213
throw std::runtime_error("failed to create semaphores!");
12001214
}
@@ -1278,7 +1292,7 @@ class HelloTriangleApplication {
12781292
createInfo.codeSize = code.size();
12791293
createInfo.pCode = (uint32_t*) code.data();
12801294

1281-
if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
1295+
if (vkCreateShaderModule(device, &createInfo, nullptr, shaderModule.replace()) != VK_SUCCESS) {
12821296
throw std::runtime_error("failed to create shader module!");
12831297
}
12841298
}

0 commit comments

Comments
 (0)