修改framebuffer的创建方式

main-headless
ink-soul 2024-04-16 11:38:12 +08:00
parent 9bdb87ea59
commit 3d7ff22ec3
3 changed files with 92 additions and 31 deletions

View File

@ -28,7 +28,7 @@ VKAPI_ATTR VkBool32 VKAPI_CALL debugMessageCallback(VkDebugReportFlagsEXT flags,
#if defined(__ANDROID__) #if defined(__ANDROID__)
LOGD("%s", debugMessage.str().c_str()); LOGD("%s", debugMessage.str().c_str());
#else #else
std::cout << debugMessage.str() << "\n"; std::cout << debugMessage.str() << "\n \n";
#endif #endif
fflush(stdout); fflush(stdout);
return VK_FALSE; return VK_FALSE;
@ -135,7 +135,7 @@ void VulkanExampleBase::prepare()
std::array<VkAttachmentDescription, 4> attachments = {}; std::array<VkAttachmentDescription, 4> attachments = {};
// Multisampled attachment that we render to // Multisampled attachment that we render to
attachments[0].format = swapChain.colorFormat; attachments[0].format = colorFormat;
attachments[0].samples = settings.sampleCount; attachments[0].samples = settings.sampleCount;
attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE; 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 // This is the frame buffer attachment to where the multisampled image
// will be resolved to and which will be presented to the swapchain // 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].samples = VK_SAMPLE_COUNT_1_BIT;
attachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE; attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; 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 // Multisampled depth attachment we render to
attachments[2].format = depthFormat; attachments[2].format = depthFormat;
@ -227,14 +227,14 @@ void VulkanExampleBase::prepare()
else { else {
std::array<VkAttachmentDescription, 2> attachments = {}; std::array<VkAttachmentDescription, 2> attachments = {};
// Color attachment // Color attachment
attachments[0].format = swapChain.colorFormat; attachments[0].format = colorFormat;
attachments[0].samples = VK_SAMPLE_COUNT_1_BIT; attachments[0].samples = VK_SAMPLE_COUNT_1_BIT;
attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE; attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; 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 // Depth attachment
attachments[1].format = depthFormat; attachments[1].format = depthFormat;
attachments[1].samples = VK_SAMPLE_COUNT_1_BIT; attachments[1].samples = VK_SAMPLE_COUNT_1_BIT;
@ -1662,7 +1662,7 @@ void VulkanExampleBase::setupFrameBuffer()
/* /*
MSAA MSAA
*/ */
VkFormat colorFormat = VK_FORMAT_R8G8B8A8_SRGB; VkFormat colorFormat = VK_FORMAT_B8G8R8A8_SRGB;
if (settings.multiSampling) { if (settings.multiSampling) {
// Check if device supports requested sample count for color and depth frame buffer // Check if device supports requested sample count for color and depth frame buffer
//assert((deviceProperties.limits.framebufferColorSampleCounts >= sampleCount) && (deviceProperties.limits.framebufferDepthSampleCounts >= sampleCount)); //assert((deviceProperties.limits.framebufferColorSampleCounts >= sampleCount) && (deviceProperties.limits.framebufferDepthSampleCounts >= sampleCount));
@ -1749,22 +1749,59 @@ void VulkanExampleBase::setupFrameBuffer()
imageViewCI.subresourceRange.layerCount = 1; imageViewCI.subresourceRange.layerCount = 1;
VK_CHECK_RESULT(vkCreateImageView(device, &imageViewCI, nullptr, &multisampleTarget.depth.view)); VK_CHECK_RESULT(vkCreateImageView(device, &imageViewCI, nullptr, &multisampleTarget.depth.view));
} }
else
{
// Depth/Stencil attachment is the same for all frame buffers // Color attachment
VkImageCreateInfo image = vks::initializers::imageCreateInfo();
VkImageCreateInfo image = {};
image.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
image.pNext = NULL;
image.imageType = VK_IMAGE_TYPE_2D; image.imageType = VK_IMAGE_TYPE_2D;
image.format = depthFormat; image.format = colorFormat;
image.extent = { width, height, 1 }; image.extent.width = width;
image.extent.height = height;
image.extent.depth = 1;
image.mipLevels = 1; image.mipLevels = 1;
image.arrayLayers = 1; image.arrayLayers = 1;
image.samples = VK_SAMPLE_COUNT_1_BIT; image.samples = VK_SAMPLE_COUNT_1_BIT;
image.tiling = VK_IMAGE_TILING_OPTIMAL; image.tiling = VK_IMAGE_TILING_OPTIMAL;
image.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT; image.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
image.flags = 0;
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 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 = {}; VkMemoryAllocateInfo mem_alloc = {};
mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
@ -1785,11 +1822,11 @@ void VulkanExampleBase::setupFrameBuffer()
depthStencilView.subresourceRange.baseArrayLayer = 0; depthStencilView.subresourceRange.baseArrayLayer = 0;
depthStencilView.subresourceRange.layerCount = 1; depthStencilView.subresourceRange.layerCount = 1;
VkMemoryRequirements memReqs; VkMemoryRequirements depthMemReqs;
VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &depthStencil.image)); VK_CHECK_RESULT(vkCreateImage(device, &depthImageCI, nullptr, &depthStencil.image));
vkGetImageMemoryRequirements(device, depthStencil.image, &memReqs); vkGetImageMemoryRequirements(device, depthStencil.image, &depthMemReqs);
mem_alloc.allocationSize = memReqs.size; mem_alloc.allocationSize = depthMemReqs.size;
mem_alloc.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); 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(vkAllocateMemory(device, &mem_alloc, nullptr, &depthStencil.mem));
VK_CHECK_RESULT(vkBindImageMemory(device, depthStencil.image, depthStencil.mem, 0)); 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) { if (settings.multiSampling) {
@ -1822,13 +1859,13 @@ void VulkanExampleBase::setupFrameBuffer()
frameBufferCI.layers = 1; frameBufferCI.layers = 1;
// Create frame buffers for every swap chain image // 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++) { for (uint32_t i = 0; i < frameBuffers.size(); i++) {
if (settings.multiSampling) { if (settings.multiSampling) {
attachments[1] = swapChain.buffers[i].view; attachments[1] = colorAttachment.view;
} }
else { else {
attachments[0] = swapChain.buffers[i].view; attachments[0] = colorAttachment.view;
} }
VK_CHECK_RESULT(vkCreateFramebuffer(device, &frameBufferCI, nullptr, &frameBuffers[i])); VK_CHECK_RESULT(vkCreateFramebuffer(device, &frameBufferCI, nullptr, &frameBuffers[i]));
} }

View File

@ -112,6 +112,8 @@ public:
bool paused = false; bool paused = false;
uint32_t lastFPS = 0; uint32_t lastFPS = 0;
VkFormat colorFormat = VK_FORMAT_B8G8R8A8_SRGB;
struct Signal struct Signal
{ {
bool imageSequenceOutputComplete = false; bool imageSequenceOutputComplete = false;
@ -123,7 +125,7 @@ public:
bool validation = true; // 校验层开关 bool validation = true; // 校验层开关
bool fullscreen = false; // 全屏开关 bool fullscreen = false; // 全屏开关
bool vsync = false; // 垂直同步开关 bool vsync = false; // 垂直同步开关
bool multiSampling = true; // 多重采样 bool multiSampling = false; // 多重采样
bool rotateModel = true; // 模型自旋转(暂时失效) bool rotateModel = true; // 模型自旋转(暂时失效)
bool headless = false; // 无头开关 bool headless = false; // 无头开关
bool outputPNGimage = false; bool outputPNGimage = false;
@ -143,6 +145,12 @@ public:
VkImageView view; VkImageView view;
} depthStencil; } depthStencil;
struct ColorAttachment {
VkImage image;
VkDeviceMemory memory;
VkImageView view;
} colorAttachment;
struct GamePadState { struct GamePadState {
glm::vec2 axisLeft = glm::vec2(0.0f); glm::vec2 axisLeft = glm::vec2(0.0f);
glm::vec2 axisRight = glm::vec2(0.0f); glm::vec2 axisRight = glm::vec2(0.0f);

View File

@ -559,6 +559,10 @@ PlumageRender::PlumageRender()
if (settings.multiSampling) { if (settings.multiSampling) {
multisampleStateCI.rasterizationSamples = settings.sampleCount; multisampleStateCI.rasterizationSamples = settings.sampleCount;
} }
else
{
multisampleStateCI.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
}
std::vector<VkDynamicState> dynamicStateEnables = { std::vector<VkDynamicState> dynamicStateEnables = {
VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_VIEWPORT,
@ -623,6 +627,10 @@ PlumageRender::PlumageRender()
if (settings.multiSampling) { if (settings.multiSampling) {
multisampleStateCI.rasterizationSamples = settings.sampleCount; multisampleStateCI.rasterizationSamples = settings.sampleCount;
} }
else
{
multisampleStateCI.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
}
// Skybox pipeline (background cube) // Skybox pipeline (background cube)
shaderStages = { shaderStages = {
@ -1583,7 +1591,14 @@ PlumageRender::PlumageRender()
setupDescriptors(); setupDescriptors();
preparePipelines(); 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(); updateUIOverlay();
buildCommandBuffers(); buildCommandBuffers();
@ -2009,6 +2024,7 @@ PlumageRender::PlumageRender()
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, waitFences[frameIndex])); VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, waitFences[frameIndex]));
//显示队列 //显示队列
/*
VkResult present = swapChain.queuePresent(queue, currentBuffer, renderCompleteSemaphores[frameIndex]); VkResult present = swapChain.queuePresent(queue, currentBuffer, renderCompleteSemaphores[frameIndex]);
if (!((present == VK_SUCCESS) || (present == VK_SUBOPTIMAL_KHR))) { if (!((present == VK_SUCCESS) || (present == VK_SUBOPTIMAL_KHR))) {
if (present == VK_ERROR_OUT_OF_DATE_KHR) { if (present == VK_ERROR_OUT_OF_DATE_KHR) {
@ -2018,7 +2034,7 @@ PlumageRender::PlumageRender()
else { else {
VK_CHECK_RESULT(present); VK_CHECK_RESULT(present);
} }
} }*/
frameIndex += 1; frameIndex += 1;
frameIndex %= renderAhead; frameIndex %= renderAhead;