Use vulkan result check macro, clean up resources
This commit is contained in:
parent
2f5a3705ab
commit
78736527ee
1 changed files with 18 additions and 101 deletions
|
|
@ -105,6 +105,10 @@ public:
|
||||||
vkFreeCommandBuffers(device, cmdPool, 1, &computeCmdBuffer);
|
vkFreeCommandBuffers(device, cmdPool, 1, &computeCmdBuffer);
|
||||||
|
|
||||||
textureLoader->destroyTexture(textureComputeTarget);
|
textureLoader->destroyTexture(textureComputeTarget);
|
||||||
|
|
||||||
|
// Compute
|
||||||
|
vkDestroyPipelineLayout(device, computePipelineLayout, nullptr);
|
||||||
|
vkDestroyDescriptorSetLayout(device, computeDescriptorSetLayout, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare a texture target that is used to store compute shader calculations
|
// Prepare a texture target that is used to store compute shader calculations
|
||||||
|
|
@ -208,15 +212,12 @@ public:
|
||||||
renderPassBeginInfo.clearValueCount = 2;
|
renderPassBeginInfo.clearValueCount = 2;
|
||||||
renderPassBeginInfo.pClearValues = clearValues;
|
renderPassBeginInfo.pClearValues = clearValues;
|
||||||
|
|
||||||
VkResult err;
|
|
||||||
|
|
||||||
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
|
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
|
||||||
{
|
{
|
||||||
// Set target frame buffer
|
// Set target frame buffer
|
||||||
renderPassBeginInfo.framebuffer = frameBuffers[i];
|
renderPassBeginInfo.framebuffer = frameBuffers[i];
|
||||||
|
|
||||||
err = vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo);
|
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
|
||||||
assert(!err);
|
|
||||||
|
|
||||||
// Image memory barrier to make sure that compute
|
// Image memory barrier to make sure that compute
|
||||||
// shader writes are finished before sampling
|
// shader writes are finished before sampling
|
||||||
|
|
@ -260,8 +261,7 @@ public:
|
||||||
|
|
||||||
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
||||||
|
|
||||||
err = vkEndCommandBuffer(drawCmdBuffers[i]);
|
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
||||||
assert(!err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -270,8 +270,7 @@ public:
|
||||||
{
|
{
|
||||||
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
|
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
|
||||||
|
|
||||||
VkResult err = vkBeginCommandBuffer(computeCmdBuffer, &cmdBufInfo);
|
VK_CHECK_RESULT(vkBeginCommandBuffer(computeCmdBuffer, &cmdBufInfo));
|
||||||
assert(!err);
|
|
||||||
|
|
||||||
vkCmdBindPipeline(computeCmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipelines.compute);
|
vkCmdBindPipeline(computeCmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipelines.compute);
|
||||||
vkCmdBindDescriptorSets(computeCmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipelineLayout, 0, 1, &computeDescriptorSet, 0, 0);
|
vkCmdBindDescriptorSets(computeCmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipelineLayout, 0, 1, &computeDescriptorSet, 0, 0);
|
||||||
|
|
@ -366,8 +365,7 @@ public:
|
||||||
poolSizes.data(),
|
poolSizes.data(),
|
||||||
3);
|
3);
|
||||||
|
|
||||||
VkResult vkRes = vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool);
|
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
||||||
assert(!vkRes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupDescriptorSetLayout()
|
void setupDescriptorSetLayout()
|
||||||
|
|
@ -386,16 +384,14 @@ public:
|
||||||
setLayoutBindings.data(),
|
setLayoutBindings.data(),
|
||||||
setLayoutBindings.size());
|
setLayoutBindings.size());
|
||||||
|
|
||||||
VkResult err = vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout);
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
|
||||||
assert(!err);
|
|
||||||
|
|
||||||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
||||||
vkTools::initializers::pipelineLayoutCreateInfo(
|
vkTools::initializers::pipelineLayoutCreateInfo(
|
||||||
&descriptorSetLayout,
|
&descriptorSetLayout,
|
||||||
1);
|
1);
|
||||||
|
|
||||||
err = vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout);
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
||||||
assert(!err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupDescriptorSet()
|
void setupDescriptorSet()
|
||||||
|
|
@ -406,8 +402,7 @@ public:
|
||||||
&descriptorSetLayout,
|
&descriptorSetLayout,
|
||||||
1);
|
1);
|
||||||
|
|
||||||
VkResult vkRes = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSetPostCompute);
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSetPostCompute));
|
||||||
assert(!vkRes);
|
|
||||||
|
|
||||||
// Image descriptor for the color map texture
|
// Image descriptor for the color map texture
|
||||||
VkDescriptorImageInfo texDescriptor =
|
VkDescriptorImageInfo texDescriptor =
|
||||||
|
|
@ -438,14 +433,11 @@ public:
|
||||||
VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
||||||
1);
|
1);
|
||||||
|
|
||||||
VkResult vkRes = vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &computeCmdBuffer);
|
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &computeCmdBuffer));
|
||||||
assert(!vkRes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void preparePipelines()
|
void preparePipelines()
|
||||||
{
|
{
|
||||||
VkResult err;
|
|
||||||
|
|
||||||
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
|
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
|
||||||
vkTools::initializers::pipelineInputAssemblyStateCreateInfo(
|
vkTools::initializers::pipelineInputAssemblyStateCreateInfo(
|
||||||
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
|
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
|
||||||
|
|
@ -517,8 +509,7 @@ public:
|
||||||
pipelineCreateInfo.pStages = shaderStages.data();
|
pipelineCreateInfo.pStages = shaderStages.data();
|
||||||
pipelineCreateInfo.renderPass = renderPass;
|
pipelineCreateInfo.renderPass = renderPass;
|
||||||
|
|
||||||
err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.display);
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.display));
|
||||||
assert(!err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare the compute pipeline that generates the ray traced image
|
// Prepare the compute pipeline that generates the ray traced image
|
||||||
|
|
@ -542,24 +533,14 @@ public:
|
||||||
setLayoutBindings.data(),
|
setLayoutBindings.data(),
|
||||||
setLayoutBindings.size());
|
setLayoutBindings.size());
|
||||||
|
|
||||||
VkResult err = vkCreateDescriptorSetLayout(
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &computeDescriptorSetLayout));
|
||||||
device,
|
|
||||||
&descriptorLayout,
|
|
||||||
nullptr,
|
|
||||||
&computeDescriptorSetLayout);
|
|
||||||
assert(!err);
|
|
||||||
|
|
||||||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
||||||
vkTools::initializers::pipelineLayoutCreateInfo(
|
vkTools::initializers::pipelineLayoutCreateInfo(
|
||||||
&computeDescriptorSetLayout,
|
&computeDescriptorSetLayout,
|
||||||
1);
|
1);
|
||||||
|
|
||||||
err = vkCreatePipelineLayout(
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &computePipelineLayout));
|
||||||
device,
|
|
||||||
&pPipelineLayoutCreateInfo,
|
|
||||||
nullptr,
|
|
||||||
&computePipelineLayout);
|
|
||||||
assert(!err);
|
|
||||||
|
|
||||||
VkDescriptorSetAllocateInfo allocInfo =
|
VkDescriptorSetAllocateInfo allocInfo =
|
||||||
vkTools::initializers::descriptorSetAllocateInfo(
|
vkTools::initializers::descriptorSetAllocateInfo(
|
||||||
|
|
@ -567,8 +548,7 @@ public:
|
||||||
&computeDescriptorSetLayout,
|
&computeDescriptorSetLayout,
|
||||||
1);
|
1);
|
||||||
|
|
||||||
err = vkAllocateDescriptorSets(device, &allocInfo, &computeDescriptorSet);
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &computeDescriptorSet));
|
||||||
assert(!err);
|
|
||||||
|
|
||||||
std::vector<VkDescriptorImageInfo> computeTexDescriptors =
|
std::vector<VkDescriptorImageInfo> computeTexDescriptors =
|
||||||
{
|
{
|
||||||
|
|
@ -689,11 +669,7 @@ public:
|
||||||
createComputeCommandBuffer();
|
createComputeCommandBuffer();
|
||||||
setupVertexDescriptions();
|
setupVertexDescriptions();
|
||||||
prepareUniformBuffers();
|
prepareUniformBuffers();
|
||||||
prepareTextureTarget(
|
prepareTextureTarget(&textureComputeTarget, TEX_DIM, TEX_DIM, VK_FORMAT_R8G8B8A8_UNORM);
|
||||||
&textureComputeTarget,
|
|
||||||
TEX_DIM,
|
|
||||||
TEX_DIM,
|
|
||||||
VK_FORMAT_R8G8B8A8_UNORM);
|
|
||||||
setupDescriptorSetLayout();
|
setupDescriptorSetLayout();
|
||||||
preparePipelines();
|
preparePipelines();
|
||||||
setupDescriptorPool();
|
setupDescriptorPool();
|
||||||
|
|
@ -721,63 +697,4 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
VulkanExample *vulkanExample;
|
VULKAN_EXAMPLE_MAIN()
|
||||||
|
|
||||||
#if defined(_WIN32)
|
|
||||||
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
||||||
{
|
|
||||||
if (vulkanExample != NULL)
|
|
||||||
{
|
|
||||||
vulkanExample->handleMessages(hWnd, uMsg, wParam, lParam);
|
|
||||||
}
|
|
||||||
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
|
|
||||||
}
|
|
||||||
#elif defined(__linux__) && !defined(__ANDROID__)
|
|
||||||
static void handleEvent(const xcb_generic_event_t *event)
|
|
||||||
{
|
|
||||||
if (vulkanExample != NULL)
|
|
||||||
{
|
|
||||||
vulkanExample->handleEvent(event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Main entry point
|
|
||||||
#if defined(_WIN32)
|
|
||||||
// Windows entry point
|
|
||||||
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow)
|
|
||||||
#elif defined(__ANDROID__)
|
|
||||||
// Android entry point
|
|
||||||
void android_main(android_app* state)
|
|
||||||
#elif defined(__linux__)
|
|
||||||
// Linux entry point
|
|
||||||
int main(const int argc, const char *argv[])
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
#if defined(__ANDROID__)
|
|
||||||
// Removing this may cause the compiler to omit the main entry point
|
|
||||||
// which would make the application crash at start
|
|
||||||
app_dummy();
|
|
||||||
#endif
|
|
||||||
vulkanExample = new VulkanExample();
|
|
||||||
#if defined(_WIN32)
|
|
||||||
vulkanExample->setupWindow(hInstance, WndProc);
|
|
||||||
#elif defined(__ANDROID__)
|
|
||||||
// Attach vulkan example to global android application state
|
|
||||||
state->userData = vulkanExample;
|
|
||||||
state->onAppCmd = VulkanExample::handleAppCommand;
|
|
||||||
state->onInputEvent = VulkanExample::handleAppInput;
|
|
||||||
vulkanExample->androidApp = state;
|
|
||||||
#elif defined(__linux__)
|
|
||||||
vulkanExample->setupWindow();
|
|
||||||
#endif
|
|
||||||
#if !defined(__ANDROID__)
|
|
||||||
vulkanExample->initSwapchain();
|
|
||||||
vulkanExample->prepare();
|
|
||||||
#endif
|
|
||||||
vulkanExample->renderLoop();
|
|
||||||
delete(vulkanExample);
|
|
||||||
#if !defined(__ANDROID__)
|
|
||||||
return 0;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue