diff --git a/displacement/displacement.cpp b/displacement/displacement.cpp index 59ca540a..858d4ea6 100644 --- a/displacement/displacement.cpp +++ b/displacement/displacement.cpp @@ -83,6 +83,7 @@ public: { zoom = -35; rotation = glm::vec3(-35.0, 0.0, 0); + enableTextOverlay = true; title = "Vulkan Example - Tessellation shader displacement mapping"; // Support for tessellation shaders is optional, so check first if (!deviceFeatures.tessellationShader) @@ -154,32 +155,19 @@ public: renderPassBeginInfo.clearValueCount = 2; renderPassBeginInfo.pClearValues = clearValues; - VkResult err; - for (int32_t i = 0; i < drawCmdBuffers.size(); ++i) { // Set target frame buffer renderPassBeginInfo.framebuffer = frameBuffers[i]; - err = vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo); - assert(!err); + VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo)); vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); - VkViewport viewport = vkTools::initializers::viewport( - splitScreen ? (float)width / 2.0f : (float)width, - (float)height, - 0.0f, - 1.0f - ); + VkViewport viewport = vkTools::initializers::viewport(splitScreen ? (float)width / 2.0f : (float)width, (float)height, 0.0f, 1.0f); vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); - VkRect2D scissor = vkTools::initializers::rect2D( - width, - height, - 0, - 0 - ); + VkRect2D scissor = vkTools::initializers::rect2D(width, height, 0, 0); vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); vkCmdSetLineWidth(drawCmdBuffers[i], 1.0f); @@ -204,38 +192,10 @@ public: vkCmdEndRenderPass(drawCmdBuffers[i]); - err = vkEndCommandBuffer(drawCmdBuffers[i]); - assert(!err); + VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i])); } } - void draw() - { - VkResult err; - - // Get next image in the swap chain (back/front buffer) - err = swapChain.acquireNextImage(semaphores.presentComplete, ¤tBuffer); - 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() { loadMesh(getAssetPath() + "models/torus.obj", &meshes.object, vertexLayout, 0.25f); @@ -301,8 +261,7 @@ public: poolSizes.data(), 2); - VkResult vkRes = vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool); - assert(!vkRes); + VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool)); } void setupDescriptorSetLayout() @@ -336,16 +295,14 @@ public: setLayoutBindings.data(), setLayoutBindings.size()); - VkResult err = vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout); - assert(!err); + VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout)); VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = vkTools::initializers::pipelineLayoutCreateInfo( &descriptorSetLayout, 1); - err = vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout); - assert(!err); + VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout)); } void setupDescriptorSet() @@ -356,8 +313,7 @@ public: &descriptorSetLayout, 1); - VkResult vkRes = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet); - assert(!vkRes); + VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet)); // Displacement map image descriptor VkDescriptorImageInfo texDescriptorDisplacementMap = @@ -406,8 +362,6 @@ public: void preparePipelines() { - VkResult err; - VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vkTools::initializers::pipelineInputAssemblyStateCreateInfo( VK_PRIMITIVE_TOPOLOGY_PATCH_LIST, @@ -487,12 +441,12 @@ public: pipelineCreateInfo.renderPass = renderPass; // Solid pipeline - err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solid); - assert(!err); + rasterizationState.cullMode = VK_CULL_MODE_BACK_BIT; + VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solid)); // Wireframe pipeline rasterizationState.polygonMode = VK_POLYGON_MODE_LINE; - err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.wire); - assert(!err); + rasterizationState.cullMode = VK_CULL_MODE_NONE; + VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.wire)); // Pass through pipelines // 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); // Solid rasterizationState.polygonMode = VK_POLYGON_MODE_FILL; - err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solidPassThrough); - assert(!err); + rasterizationState.cullMode = VK_CULL_MODE_BACK_BIT; + VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solidPassThrough)); // Wireframe rasterizationState.polygonMode = VK_POLYGON_MODE_LINE; - err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.wirePassThrough); - assert(!err); + rasterizationState.cullMode = VK_CULL_MODE_NONE; + VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.wirePassThrough)); } // Prepare and initialize uniform buffer containing shader uniforms @@ -514,8 +468,9 @@ public: // Tessellation evaluation shader uniform buffer createBuffer( VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, sizeof(uboTE), - &uboTE, + nullptr, &uniformDataTE.buffer, &uniformDataTE.memory, &uniformDataTE.descriptor); @@ -523,8 +478,9 @@ public: // Tessellation control shader uniform buffer createBuffer( VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, sizeof(uboTC), - &uboTC, + nullptr, &uniformDataTC.buffer, &uniformDataTC.memory, &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)); uint8_t *pData; - VkResult err = vkMapMemory(device, uniformDataTE.memory, 0, sizeof(uboTE), 0, (void **)&pData); - assert(!err); + VK_CHECK_RESULT(vkMapMemory(device, uniformDataTE.memory, 0, sizeof(uboTE), 0, (void **)&pData)); memcpy(pData, &uboTE, sizeof(uboTE)); vkUnmapMemory(device, uniformDataTE.memory); // Tessellation control - err = vkMapMemory(device, uniformDataTC.memory, 0, sizeof(uboTC), 0, (void **)&pData); - assert(!err); + VK_CHECK_RESULT(vkMapMemory(device, uniformDataTC.memory, 0, sizeof(uboTC), 0, (void **)&pData)); memcpy(pData, &uboTC, sizeof(uboTC)); 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() { VulkanExampleBase::prepare(); @@ -595,6 +563,7 @@ public: // Clamp uboTC.tessLevel = fmax(1.0, fmin(uboTC.tessLevel, 32.0)); updateUniformBuffers(); + updateTextOverlay(); } void togglePipelines() @@ -619,6 +588,43 @@ public: 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; @@ -629,24 +635,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) if (vulkanExample != NULL) { 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)); }