Refactoring, use Vulkan result check macro

This commit is contained in:
saschawillems 2016-06-06 11:35:04 +02:00
parent 92c04aad10
commit 04a31f9db4

View file

@ -83,6 +83,7 @@ public:
{ {
zoom = -35; zoom = -35;
rotation = glm::vec3(-35.0, 0.0, 0); rotation = glm::vec3(-35.0, 0.0, 0);
enableTextOverlay = true;
title = "Vulkan Example - Tessellation shader displacement mapping"; title = "Vulkan Example - Tessellation shader displacement mapping";
// Support for tessellation shaders is optional, so check first // Support for tessellation shaders is optional, so check first
if (!deviceFeatures.tessellationShader) if (!deviceFeatures.tessellationShader)
@ -154,32 +155,19 @@ 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);
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport = vkTools::initializers::viewport( VkViewport viewport = vkTools::initializers::viewport(splitScreen ? (float)width / 2.0f : (float)width, (float)height, 0.0f, 1.0f);
splitScreen ? (float)width / 2.0f : (float)width,
(float)height,
0.0f,
1.0f
);
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
VkRect2D scissor = vkTools::initializers::rect2D( VkRect2D scissor = vkTools::initializers::rect2D(width, height, 0, 0);
width,
height,
0,
0
);
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
vkCmdSetLineWidth(drawCmdBuffers[i], 1.0f); vkCmdSetLineWidth(drawCmdBuffers[i], 1.0f);
@ -204,38 +192,10 @@ public:
vkCmdEndRenderPass(drawCmdBuffers[i]); vkCmdEndRenderPass(drawCmdBuffers[i]);
err = vkEndCommandBuffer(drawCmdBuffers[i]); VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
assert(!err);
} }
} }
void draw()
{
VkResult err;
// Get next image in the swap chain (back/front buffer)
err = swapChain.acquireNextImage(semaphores.presentComplete, &currentBuffer);
assert(!err);
submitPostPresentBarrier(swapChain.buffers[currentBuffer].image);
// Command buffer to be sumitted to the queue
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
// Submit to queue
err = vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE);
assert(!err);
submitPrePresentBarrier(swapChain.buffers[currentBuffer].image);
err = swapChain.queuePresent(queue, currentBuffer, semaphores.renderComplete);
assert(!err);
err = vkQueueWaitIdle(queue);
assert(!err);
}
void loadMeshes() void loadMeshes()
{ {
loadMesh(getAssetPath() + "models/torus.obj", &meshes.object, vertexLayout, 0.25f); loadMesh(getAssetPath() + "models/torus.obj", &meshes.object, vertexLayout, 0.25f);
@ -301,8 +261,7 @@ public:
poolSizes.data(), poolSizes.data(),
2); 2);
VkResult vkRes = vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool); VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
assert(!vkRes);
} }
void setupDescriptorSetLayout() void setupDescriptorSetLayout()
@ -336,16 +295,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()
@ -356,8 +313,7 @@ public:
&descriptorSetLayout, &descriptorSetLayout,
1); 1);
VkResult vkRes = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
assert(!vkRes);
// Displacement map image descriptor // Displacement map image descriptor
VkDescriptorImageInfo texDescriptorDisplacementMap = VkDescriptorImageInfo texDescriptorDisplacementMap =
@ -406,8 +362,6 @@ public:
void preparePipelines() void preparePipelines()
{ {
VkResult err;
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
vkTools::initializers::pipelineInputAssemblyStateCreateInfo( vkTools::initializers::pipelineInputAssemblyStateCreateInfo(
VK_PRIMITIVE_TOPOLOGY_PATCH_LIST, VK_PRIMITIVE_TOPOLOGY_PATCH_LIST,
@ -487,12 +441,12 @@ public:
pipelineCreateInfo.renderPass = renderPass; pipelineCreateInfo.renderPass = renderPass;
// Solid pipeline // Solid pipeline
err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solid); rasterizationState.cullMode = VK_CULL_MODE_BACK_BIT;
assert(!err); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solid));
// Wireframe pipeline // Wireframe pipeline
rasterizationState.polygonMode = VK_POLYGON_MODE_LINE; rasterizationState.polygonMode = VK_POLYGON_MODE_LINE;
err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.wire); rasterizationState.cullMode = VK_CULL_MODE_NONE;
assert(!err); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.wire));
// Pass through pipelines // Pass through pipelines
// Load pass through tessellation shaders (Vert and frag are reused) // Load pass through tessellation shaders (Vert and frag are reused)
@ -500,12 +454,12 @@ public:
shaderStages[3] = loadShader(getAssetPath() + "shaders/displacement/passthrough.tese.spv", VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT); shaderStages[3] = loadShader(getAssetPath() + "shaders/displacement/passthrough.tese.spv", VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT);
// Solid // Solid
rasterizationState.polygonMode = VK_POLYGON_MODE_FILL; rasterizationState.polygonMode = VK_POLYGON_MODE_FILL;
err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solidPassThrough); rasterizationState.cullMode = VK_CULL_MODE_BACK_BIT;
assert(!err); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solidPassThrough));
// Wireframe // Wireframe
rasterizationState.polygonMode = VK_POLYGON_MODE_LINE; rasterizationState.polygonMode = VK_POLYGON_MODE_LINE;
err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.wirePassThrough); rasterizationState.cullMode = VK_CULL_MODE_NONE;
assert(!err); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.wirePassThrough));
} }
// Prepare and initialize uniform buffer containing shader uniforms // Prepare and initialize uniform buffer containing shader uniforms
@ -514,8 +468,9 @@ public:
// Tessellation evaluation shader uniform buffer // Tessellation evaluation shader uniform buffer
createBuffer( createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
sizeof(uboTE), sizeof(uboTE),
&uboTE, nullptr,
&uniformDataTE.buffer, &uniformDataTE.buffer,
&uniformDataTE.memory, &uniformDataTE.memory,
&uniformDataTE.descriptor); &uniformDataTE.descriptor);
@ -523,8 +478,9 @@ public:
// Tessellation control shader uniform buffer // Tessellation control shader uniform buffer
createBuffer( createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
sizeof(uboTC), sizeof(uboTC),
&uboTC, nullptr,
&uniformDataTC.buffer, &uniformDataTC.buffer,
&uniformDataTC.memory, &uniformDataTC.memory,
&uniformDataTC.descriptor); &uniformDataTC.descriptor);
@ -548,18 +504,30 @@ public:
uboTE.model = glm::rotate(uboTE.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); uboTE.model = glm::rotate(uboTE.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
uint8_t *pData; uint8_t *pData;
VkResult err = vkMapMemory(device, uniformDataTE.memory, 0, sizeof(uboTE), 0, (void **)&pData); VK_CHECK_RESULT(vkMapMemory(device, uniformDataTE.memory, 0, sizeof(uboTE), 0, (void **)&pData));
assert(!err);
memcpy(pData, &uboTE, sizeof(uboTE)); memcpy(pData, &uboTE, sizeof(uboTE));
vkUnmapMemory(device, uniformDataTE.memory); vkUnmapMemory(device, uniformDataTE.memory);
// Tessellation control // Tessellation control
err = vkMapMemory(device, uniformDataTC.memory, 0, sizeof(uboTC), 0, (void **)&pData); VK_CHECK_RESULT(vkMapMemory(device, uniformDataTC.memory, 0, sizeof(uboTC), 0, (void **)&pData));
assert(!err);
memcpy(pData, &uboTC, sizeof(uboTC)); memcpy(pData, &uboTC, sizeof(uboTC));
vkUnmapMemory(device, uniformDataTC.memory); vkUnmapMemory(device, uniformDataTC.memory);
} }
void draw()
{
VulkanExampleBase::prepareFrame();
// Command buffer to be sumitted to the queue
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
// Submit to queue
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
VulkanExampleBase::submitFrame();
}
void prepare() void prepare()
{ {
VulkanExampleBase::prepare(); VulkanExampleBase::prepare();
@ -595,6 +563,7 @@ public:
// Clamp // Clamp
uboTC.tessLevel = fmax(1.0, fmin(uboTC.tessLevel, 32.0)); uboTC.tessLevel = fmax(1.0, fmin(uboTC.tessLevel, 32.0));
updateUniformBuffers(); updateUniformBuffers();
updateTextOverlay();
} }
void togglePipelines() void togglePipelines()
@ -619,6 +588,43 @@ public:
updateUniformBuffers(); updateUniformBuffers();
} }
virtual void keyPressed(uint32_t keyCode)
{
switch (keyCode)
{
case 0x6B:
case GAMEPAD_BUTTON_R1:
changeTessellationLevel(0.25);
break;
case 0x6D:
case GAMEPAD_BUTTON_L1:
changeTessellationLevel(-0.25);
break;
case 0x57:
case GAMEPAD_BUTTON_A:
togglePipelines();
break;
case 0x53:
case GAMEPAD_BUTTON_X:
toggleSplitScreen();
break;
}
}
virtual void getOverlayText(VulkanTextOverlay *textOverlay)
{
std::stringstream ss;
ss << std::setprecision(2) << std::fixed << uboTC.tessLevel;
#if defined(__ANDROID__)
textOverlay->addText("Tessellation level: " + ss.str() + " (Buttons L1/R1 to change)", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
textOverlay->addText("Press \"Button A\" to toggle wireframe", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
textOverlay->addText("Press \"Button X\" to toggle splitscreen", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
#else
textOverlay->addText("Tessellation level: " + ss.str() + " (NUMPAD +/- to change)", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
textOverlay->addText("Press \"w\" to toggle wireframe", 5.0f, 100.0f, VulkanTextOverlay::alignLeft);
textOverlay->addText("Press \"s\" to toggle splitscreen", 5.0f, 115.0f, VulkanTextOverlay::alignLeft);
#endif
}
}; };
VulkanExample *vulkanExample; VulkanExample *vulkanExample;
@ -629,24 +635,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
if (vulkanExample != NULL) if (vulkanExample != NULL)
{ {
vulkanExample->handleMessages(hWnd, uMsg, wParam, lParam); vulkanExample->handleMessages(hWnd, uMsg, wParam, lParam);
if (uMsg == WM_KEYDOWN)
{
switch (wParam)
{
case VK_ADD:
vulkanExample->changeTessellationLevel(0.25);
break;
case VK_SUBTRACT:
vulkanExample->changeTessellationLevel(-0.25);
break;
case 0x57:
vulkanExample->togglePipelines();
break;
case 0x53:
vulkanExample->toggleSplitScreen();
break;
}
}
} }
return (DefWindowProc(hWnd, uMsg, wParam, lParam)); return (DefWindowProc(hWnd, uMsg, wParam, lParam));
} }