From 4e175e863b53dfbeb9e95ae97fc4724f9624df96 Mon Sep 17 00:00:00 2001 From: ink-soul Date: Tue, 23 May 2023 15:52:04 +0800 Subject: [PATCH] update framebuffer setter --- homework/homework1/homework1.cpp | 76 ++++++++++++++++++++++++++++++-- homework/homework1/homework1.h | 1 + 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/homework/homework1/homework1.cpp b/homework/homework1/homework1.cpp index eb3f27d..e4c7d34 100644 --- a/homework/homework1/homework1.cpp +++ b/homework/homework1/homework1.cpp @@ -609,12 +609,12 @@ if (textures.size() > 0) { VulkanglTFModel::Texture texture = textures[materials[primitive.materialIndex].baseColorTextureIndex]; - auto normalMap = textures[materials[primitive.materialIndex].normalMapTextureIndex]; - auto roughMetalMap = textures[materials[primitive.materialIndex].matalicRoughTextureIndex]; + VulkanglTFModel::Texture normalMap = textures[materials[primitive.materialIndex].normalMapTextureIndex]; + VulkanglTFModel::Texture roughMetalMap = textures[materials[primitive.materialIndex].matalicRoughTextureIndex]; if (materials[primitive.materialIndex].emissiveTextureIndex >= 0) { - auto emissiveMap = textures[materials[primitive.materialIndex].emissiveTextureIndex]; + VulkanglTFModel::Texture emissiveMap = textures[materials[primitive.materialIndex].emissiveTextureIndex]; vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 4, 1, &images[emissiveMap.imageIndex].descriptorSet, 0, nullptr); } @@ -674,8 +674,78 @@ void VulkanExample::setupFrameBuffer() VkFormat attachDepthFormat; VkBool32 validDepthFormat = vks::tools::getSupportedDepthFormat(physicalDevice, &attachDepthFormat); assert(validDepthFormat); + VulkanExample::createAttachment(VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, &pbrFrameBuffer.color, width, height); + VulkanExample::createAttachment(attachDepthFormat, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, &pbrFrameBuffer.depth, width, height); + + std::array attachs = {}; + for (uint32_t i = 0; i < static_cast(attachs.size()); i++) + { + attachs[i].samples = VK_SAMPLE_COUNT_1_BIT; + attachs[i].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + attachs[i].storeOp = VK_ATTACHMENT_STORE_OP_STORE; + attachs[i].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + attachs[i].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + attachs[i].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; + attachs[i].finalLayout = 1 ? VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL : VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL; + } + attachs[0].format = pbrFrameBuffer.color.format; + attachs[1].format = pbrFrameBuffer.depth.format; + + VkAttachmentReference colorRefference = {}; + colorRefference.attachment = 0; + colorRefference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + + VkAttachmentReference depthRefference = {}; + colorRefference.attachment = 1; + colorRefference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + + VkSubpassDescription subpass = {}; + subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; + subpass.pColorAttachments = &colorRefference; + subpass.colorAttachmentCount = 1; + subpass.pDepthStencilAttachment = &depthRefference; + + std::array dependencies; + //To test src 0 + dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL; + dependencies[0].dstSubpass = 0; + dependencies[0].srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; + dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + dependencies[0].srcAccessMask = VK_ACCESS_SHADER_READ_BIT; + dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; + dependencies[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; + + dependencies[1].srcSubpass = 0; + dependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL; + dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + dependencies[1].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; + dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; + dependencies[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT; + dependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; + + VkRenderPassCreateInfo renderPassCI = {}; + renderPassCI.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; + renderPassCI.pAttachments = attachs.data(); + renderPassCI.attachmentCount = static_cast(attachs.size()); + renderPassCI.pSubpasses = &subpass; + renderPassCI.pDependencies = dependencies.data(); + renderPassCI.dependencyCount = 2; + VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassCI, nullptr, &pbrFrameBuffer.fbo.renderPass)); + // FBO + VkImageView attachments[2] = { pbrFrameBuffer.color.imageView,pbrFrameBuffer.depth.imageView }; + VkFramebufferCreateInfo frameBufferCreateInfo = vks::initializers::framebufferCreateInfo(); + frameBufferCreateInfo.renderPass = pbrFrameBuffer.fbo.renderPass; + frameBufferCreateInfo.pAttachments = attachments; + frameBufferCreateInfo.attachmentCount = 2; + frameBufferCreateInfo.width = pbrFrameBuffer.fbo.width; + frameBufferCreateInfo.height = pbrFrameBuffer.fbo.height; + frameBufferCreateInfo.layers = 1; + VK_CHECK_RESULT(vkCreateFramebuffer(device, &frameBufferCreateInfo, nullptr, &pbrFrameBuffer.fbo.frameBuffer)); + + VkSamplerCreateInfo samplerCI = vks::initializers::samplerCreateInfo(); + samplerCI.magFilter = VK_FILTER_NEAREST; } void VulkanExample::getEnabledFeatures() diff --git a/homework/homework1/homework1.h b/homework/homework1/homework1.h index 1c7d3f3..3fd0791 100644 --- a/homework/homework1/homework1.h +++ b/homework/homework1/homework1.h @@ -273,6 +273,7 @@ public: VkDeviceMemory deviceMemory; VkImageView imageView; VkFormat format; + void destroy(VkDevice device) {