diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index 2a219ae..15711e4 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -28,7 +28,7 @@ VKAPI_ATTR VkBool32 VKAPI_CALL debugMessageCallback(VkDebugReportFlagsEXT flags, #if defined(__ANDROID__) LOGD("%s", debugMessage.str().c_str()); #else - std::cout << debugMessage.str() << "\n"; + std::cout << debugMessage.str() << "\n \n"; #endif fflush(stdout); return VK_FALSE; @@ -135,7 +135,7 @@ void VulkanExampleBase::prepare() std::array attachments = {}; // Multisampled attachment that we render to - attachments[0].format = swapChain.colorFormat; + attachments[0].format = colorFormat; attachments[0].samples = settings.sampleCount; attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE; @@ -146,14 +146,14 @@ void VulkanExampleBase::prepare() // This is the frame buffer attachment to where the multisampled image // will be resolved to and which will be presented to the swapchain - attachments[1].format = swapChain.colorFormat; + attachments[1].format = colorFormat; attachments[1].samples = VK_SAMPLE_COUNT_1_BIT; attachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE; attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attachments[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - attachments[1].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; + attachments[1].finalLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; // Multisampled depth attachment we render to attachments[2].format = depthFormat; @@ -227,14 +227,14 @@ void VulkanExampleBase::prepare() else { std::array attachments = {}; // Color attachment - attachments[0].format = swapChain.colorFormat; + attachments[0].format = colorFormat; attachments[0].samples = VK_SAMPLE_COUNT_1_BIT; attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE; attachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attachments[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - attachments[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; + attachments[0].finalLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; // Depth attachment attachments[1].format = depthFormat; attachments[1].samples = VK_SAMPLE_COUNT_1_BIT; @@ -1662,7 +1662,7 @@ void VulkanExampleBase::setupFrameBuffer() /* MSAA */ - VkFormat colorFormat = VK_FORMAT_R8G8B8A8_SRGB; + VkFormat colorFormat = VK_FORMAT_B8G8R8A8_SRGB; if (settings.multiSampling) { // Check if device supports requested sample count for color and depth frame buffer //assert((deviceProperties.limits.framebufferColorSampleCounts >= sampleCount) && (deviceProperties.limits.framebufferDepthSampleCounts >= sampleCount)); @@ -1749,22 +1749,59 @@ void VulkanExampleBase::setupFrameBuffer() imageViewCI.subresourceRange.layerCount = 1; VK_CHECK_RESULT(vkCreateImageView(device, &imageViewCI, nullptr, &multisampleTarget.depth.view)); } + else + { + // Color attachment + VkImageCreateInfo image = vks::initializers::imageCreateInfo(); + image.imageType = VK_IMAGE_TYPE_2D; + image.format = colorFormat; + image.extent.width = width; + image.extent.height = height; + image.extent.depth = 1; + image.mipLevels = 1; + image.arrayLayers = 1; + image.samples = VK_SAMPLE_COUNT_1_BIT; + image.tiling = VK_IMAGE_TILING_OPTIMAL; + image.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT; + VkMemoryAllocateInfo memAlloc = vks::initializers::memoryAllocateInfo(); + VkMemoryRequirements memReqs; + VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &colorAttachment.image)); + vkGetImageMemoryRequirements(device, colorAttachment.image, &memReqs); + memAlloc.allocationSize = memReqs.size; + memAlloc.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); + VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &colorAttachment.memory)); + VK_CHECK_RESULT(vkBindImageMemory(device, colorAttachment.image, colorAttachment.memory, 0)); + + VkImageViewCreateInfo colorImageView = vks::initializers::imageViewCreateInfo(); + colorImageView.viewType = VK_IMAGE_VIEW_TYPE_2D; + colorImageView.format = colorFormat; + colorImageView.subresourceRange = {}; + colorImageView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + colorImageView.subresourceRange.baseMipLevel = 0; + colorImageView.subresourceRange.levelCount = 1; + colorImageView.subresourceRange.baseArrayLayer = 0; + colorImageView.subresourceRange.layerCount = 1; + colorImageView.image = colorAttachment.image; + VK_CHECK_RESULT(vkCreateImageView(device, &colorImageView, nullptr, &colorAttachment.view)); + } + + // Depth/Stencil attachment is the same for all frame buffers - VkImageCreateInfo image = {}; - image.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; - image.pNext = NULL; - image.imageType = VK_IMAGE_TYPE_2D; - image.format = depthFormat; - image.extent = { width, height, 1 }; - image.mipLevels = 1; - image.arrayLayers = 1; - image.samples = VK_SAMPLE_COUNT_1_BIT; - image.tiling = VK_IMAGE_TILING_OPTIMAL; - image.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT; - image.flags = 0; + VkImageCreateInfo depthImageCI = {}; + depthImageCI.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; + depthImageCI.pNext = NULL; + depthImageCI.imageType = VK_IMAGE_TYPE_2D; + depthImageCI.format = depthFormat; + depthImageCI.extent = { width, height, 1 }; + depthImageCI.mipLevels = 1; + depthImageCI.arrayLayers = 1; + depthImageCI.samples = VK_SAMPLE_COUNT_1_BIT; + depthImageCI.tiling = VK_IMAGE_TILING_OPTIMAL; + depthImageCI.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT; + depthImageCI.flags = 0; VkMemoryAllocateInfo mem_alloc = {}; mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; @@ -1785,11 +1822,11 @@ void VulkanExampleBase::setupFrameBuffer() depthStencilView.subresourceRange.baseArrayLayer = 0; depthStencilView.subresourceRange.layerCount = 1; - VkMemoryRequirements memReqs; - VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &depthStencil.image)); - vkGetImageMemoryRequirements(device, depthStencil.image, &memReqs); - mem_alloc.allocationSize = memReqs.size; - mem_alloc.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); + VkMemoryRequirements depthMemReqs; + VK_CHECK_RESULT(vkCreateImage(device, &depthImageCI, nullptr, &depthStencil.image)); + vkGetImageMemoryRequirements(device, depthStencil.image, &depthMemReqs); + mem_alloc.allocationSize = depthMemReqs.size; + mem_alloc.memoryTypeIndex = vulkanDevice->getMemoryType(depthMemReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); VK_CHECK_RESULT(vkAllocateMemory(device, &mem_alloc, nullptr, &depthStencil.mem)); VK_CHECK_RESULT(vkBindImageMemory(device, depthStencil.image, depthStencil.mem, 0)); @@ -1798,7 +1835,7 @@ void VulkanExampleBase::setupFrameBuffer() // - VkImageView attachments[4]; + VkImageView attachments[settings.multiSampling ? 4 : 2]; if (settings.multiSampling) { @@ -1822,13 +1859,13 @@ void VulkanExampleBase::setupFrameBuffer() frameBufferCI.layers = 1; // Create frame buffers for every swap chain image - frameBuffers.resize(swapChain.imageCount); + frameBuffers.resize(settings.outputFrameCount - settings.startFrameCount + 1); for (uint32_t i = 0; i < frameBuffers.size(); i++) { if (settings.multiSampling) { - attachments[1] = swapChain.buffers[i].view; + attachments[1] = colorAttachment.view; } else { - attachments[0] = swapChain.buffers[i].view; + attachments[0] = colorAttachment.view; } VK_CHECK_RESULT(vkCreateFramebuffer(device, &frameBufferCI, nullptr, &frameBuffers[i])); } diff --git a/base/vulkanexamplebase.h b/base/vulkanexamplebase.h index 0aab9d0..38a1d17 100644 --- a/base/vulkanexamplebase.h +++ b/base/vulkanexamplebase.h @@ -112,6 +112,8 @@ public: bool paused = false; uint32_t lastFPS = 0; + VkFormat colorFormat = VK_FORMAT_B8G8R8A8_SRGB; + struct Signal { bool imageSequenceOutputComplete = false; @@ -123,7 +125,7 @@ public: bool validation = true; // 校验层开关 bool fullscreen = false; // 全屏开关 bool vsync = false; // 垂直同步开关 - bool multiSampling = true; // 多重采样 + bool multiSampling = false; // 多重采样 bool rotateModel = true; // 模型自旋转(暂时失效) bool headless = false; // 无头开关 bool outputPNGimage = false; @@ -143,6 +145,12 @@ public: VkImageView view; } depthStencil; + struct ColorAttachment { + VkImage image; + VkDeviceMemory memory; + VkImageView view; + } colorAttachment; + struct GamePadState { glm::vec2 axisLeft = glm::vec2(0.0f); glm::vec2 axisRight = glm::vec2(0.0f); diff --git a/src/render/render.cpp b/src/render/render.cpp index 90c7cf8..bfbc0d1 100644 --- a/src/render/render.cpp +++ b/src/render/render.cpp @@ -559,6 +559,10 @@ PlumageRender::PlumageRender() if (settings.multiSampling) { multisampleStateCI.rasterizationSamples = settings.sampleCount; } + else + { + multisampleStateCI.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; + } std::vector dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, @@ -623,6 +627,10 @@ PlumageRender::PlumageRender() if (settings.multiSampling) { multisampleStateCI.rasterizationSamples = settings.sampleCount; } + else + { + multisampleStateCI.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; + } // Skybox pipeline (background cube) shaderStages = { @@ -1583,7 +1591,14 @@ PlumageRender::PlumageRender() setupDescriptors(); preparePipelines(); - gui = new UI(vulkanDevice, renderPass, queue, pipelineCache, settings.sampleCount); + + VkSampleCountFlagBits sampleCount = VK_SAMPLE_COUNT_1_BIT; + if (settings.multiSampling) + { + sampleCount = settings.sampleCount; + } + + gui = new UI(vulkanDevice, renderPass, queue, pipelineCache, sampleCount); updateUIOverlay(); buildCommandBuffers(); @@ -2009,6 +2024,7 @@ PlumageRender::PlumageRender() VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, waitFences[frameIndex])); //显示队列 + /* VkResult present = swapChain.queuePresent(queue, currentBuffer, renderCompleteSemaphores[frameIndex]); if (!((present == VK_SUCCESS) || (present == VK_SUBOPTIMAL_KHR))) { if (present == VK_ERROR_OUT_OF_DATE_KHR) { @@ -2018,7 +2034,7 @@ PlumageRender::PlumageRender() else { VK_CHECK_RESULT(present); } - } + }*/ frameIndex += 1; frameIndex %= renderAhead;