This commit is contained in:
tylermurphy534 2022-09-26 00:06:51 -04:00
parent ee3a4d6073
commit eb89831d14
9 changed files with 133 additions and 111 deletions

View file

@ -131,7 +131,10 @@ void Device::pickPhysicalDevice() {
} }
vkGetPhysicalDeviceProperties(physicalDevice, &properties); vkGetPhysicalDeviceProperties(physicalDevice, &properties);
samplerAnisotropy = properties.limits.maxSamplerAnisotropy;
std::cout << "Physical device: " << properties.deviceName << std::endl; std::cout << "Physical device: " << properties.deviceName << std::endl;
std::cout << "Multisample Count: " << msaaSamples << std::endl;
std::cout << "Anisotropic Level: " << samplerAnisotropy << std::endl;
} }
VkSampleCountFlagBits Device::getMaxUsableSampleCount() { VkSampleCountFlagBits Device::getMaxUsableSampleCount() {

View file

@ -42,7 +42,8 @@ class Device {
VkSurfaceKHR surface() { return surface_; } VkSurfaceKHR surface() { return surface_; }
VkQueue graphicsQueue() { return graphicsQueue_; } VkQueue graphicsQueue() { return graphicsQueue_; }
VkQueue presentQueue() { return presentQueue_; } VkQueue presentQueue() { return presentQueue_; }
VkPhysicalDeviceProperties getProperties() { return properties; } VkSampleCountFlagBits getSamples() { return msaaSamples; }
float getAnisotropy() { return samplerAnisotropy; }
SwapChainSupportDetails getSwapChainSupport() { return querySwapChainSupport(physicalDevice); } SwapChainSupportDetails getSwapChainSupport() { return querySwapChainSupport(physicalDevice); }
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties); uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
@ -101,6 +102,7 @@ class Device {
VkQueue presentQueue_; VkQueue presentQueue_;
VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT; VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT;
float samplerAnisotropy = 1;
const std::vector<const char *> validationLayers = {"VK_LAYER_KHRONOS_validation"}; const std::vector<const char *> validationLayers = {"VK_LAYER_KHRONOS_validation"};
const std::vector<const char *> deviceExtensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_MAINTENANCE1_EXTENSION_NAME}; const std::vector<const char *> deviceExtensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_MAINTENANCE1_EXTENSION_NAME};

View file

@ -51,11 +51,10 @@ void Image::createTextureImage(const std::string &filename) {
stbi_image_free(pixels); stbi_image_free(pixels);
createImage(texWidth, texHeight, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, textureImage, textureImageMemory); createImage(xeDevice, texWidth, texHeight, mipLevels, VK_SAMPLE_COUNT_1_BIT, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, textureImage, textureImageMemory);
transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
copyBufferToImage(stagingBuffer, textureImage, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight)); copyBufferToImage(stagingBuffer, textureImage, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight));
// transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
vkDestroyBuffer(xeDevice.device(), stagingBuffer, nullptr); vkDestroyBuffer(xeDevice.device(), stagingBuffer, nullptr);
vkFreeMemory(xeDevice.device(), stagingBufferMemory, nullptr); vkFreeMemory(xeDevice.device(), stagingBufferMemory, nullptr);
@ -64,42 +63,6 @@ void Image::createTextureImage(const std::string &filename) {
} }
void Image::createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) {
VkImageCreateInfo imageInfo{};
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.extent.width = width;
imageInfo.extent.height = height;
imageInfo.extent.depth = 1;
imageInfo.mipLevels = mipLevels;
imageInfo.arrayLayers = 1;
imageInfo.format = format;
imageInfo.tiling = tiling;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageInfo.usage = usage;
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (vkCreateImage(xeDevice.device(), &imageInfo, nullptr, &image) != VK_SUCCESS) {
throw std::runtime_error("failed to create image!");
}
VkMemoryRequirements memRequirements;
vkGetImageMemoryRequirements(xeDevice.device(), image, &memRequirements);
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = xeDevice.findMemoryType(memRequirements.memoryTypeBits, properties);
if (vkAllocateMemory(xeDevice.device(), &allocInfo, nullptr, &imageMemory) != VK_SUCCESS) {
throw std::runtime_error("failed to allocate image memory!");
}
vkBindImageMemory(xeDevice.device(), image, imageMemory, 0);
}
void Image::transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout) { void Image::transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout) {
VkCommandBuffer commandBuffer = xeDevice.beginSingleTimeCommands(); VkCommandBuffer commandBuffer = xeDevice.beginSingleTimeCommands();
@ -251,20 +214,7 @@ void Image::copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, ui
} }
void Image::createTextureImageView() { void Image::createTextureImageView() {
VkImageViewCreateInfo viewInfo{}; textureImageView = createImageView(xeDevice, textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT, mipLevels);
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = textureImage;
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewInfo.format = VK_FORMAT_R8G8B8A8_SRGB;
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = mipLevels;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 1;
if (vkCreateImageView(xeDevice.device(), &viewInfo, nullptr, &textureImageView) != VK_SUCCESS) {
throw std::runtime_error("failed to create texture image view!");
}
} }
void Image::createTextureSampler() { void Image::createTextureSampler() {
@ -277,7 +227,7 @@ void Image::createTextureSampler() {
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT; samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT; samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.anisotropyEnable = VK_TRUE; samplerInfo.anisotropyEnable = VK_TRUE;
samplerInfo.maxAnisotropy = xeDevice.getProperties().limits.maxSamplerAnisotropy; samplerInfo.maxAnisotropy = xeDevice.getAnisotropy();
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK; samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
samplerInfo.unnormalizedCoordinates = VK_FALSE; samplerInfo.unnormalizedCoordinates = VK_FALSE;
samplerInfo.compareEnable = VK_FALSE; samplerInfo.compareEnable = VK_FALSE;
@ -292,4 +242,60 @@ void Image::createTextureSampler() {
} }
} }
void Image::createImage(Device& device, uint32_t width, uint32_t height, uint32_t mipLevels, VkSampleCountFlagBits numSamples, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) {
VkImageCreateInfo imageInfo{};
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.extent.width = width;
imageInfo.extent.height = height;
imageInfo.extent.depth = 1;
imageInfo.mipLevels = mipLevels;
imageInfo.arrayLayers = 1;
imageInfo.format = format;
imageInfo.tiling = tiling;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageInfo.usage = usage;
imageInfo.samples = numSamples;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (vkCreateImage(device.device(), &imageInfo, nullptr, &image) != VK_SUCCESS) {
throw std::runtime_error("failed to create image!");
}
VkMemoryRequirements memRequirements;
vkGetImageMemoryRequirements(device.device(), image, &memRequirements);
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = device.findMemoryType(memRequirements.memoryTypeBits, properties);
if (vkAllocateMemory(device.device(), &allocInfo, nullptr, &imageMemory) != VK_SUCCESS) {
throw std::runtime_error("failed to allocate image memory!");
}
vkBindImageMemory(device.device(), image, imageMemory, 0);
}
VkImageView Image::createImageView(Device& device, VkImage image, VkFormat format, VkImageAspectFlags aspectFlags, uint32_t mipLevels) {
VkImageViewCreateInfo viewInfo{};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = image;
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewInfo.format = format;
viewInfo.subresourceRange.aspectMask = aspectFlags;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = mipLevels;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 1;
VkImageView imageView;
if (vkCreateImageView(device.device(), &viewInfo, nullptr, &imageView) != VK_SUCCESS) {
throw std::runtime_error("failed to create texture image view!");
}
return imageView;
}
} }

View file

@ -19,22 +19,25 @@ class Image {
private: private:
void createTextureImage(const std::string &filename); void createTextureImage(const std::string &filename);
void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory);
void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout); void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout);
void generateMipmaps(VkImage image, VkFormat imageFormat, int32_t texWidth, int32_t texHeight, uint32_t mipLevels); void generateMipmaps(VkImage image, VkFormat imageFormat, int32_t texWidth, int32_t texHeight, uint32_t mipLevels);
void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height); void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height);
void createTextureImageView(); void createTextureImageView();
void createTextureSampler(); void createTextureSampler();
static void createImage(Device& device, uint32_t width, uint32_t height, uint32_t mipLevels, VkSampleCountFlagBits numSamples, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory);
static VkImageView createImageView(Device& device, VkImage image, VkFormat format, VkImageAspectFlags aspectFlags, uint32_t mipLevels);
Device &xeDevice; Device &xeDevice;
uint32_t mipLevels; uint32_t mipLevels;
VkImage textureImage;
VkSampler textureSampler; VkSampler textureSampler;
VkImage textureImage;
VkImageView textureImageView; VkImageView textureImageView;
VkDeviceMemory textureImageMemory; VkDeviceMemory textureImageMemory;
friend class RenderSystem; friend class RenderSystem;
friend class SwapChain;
}; };

View file

@ -136,7 +136,7 @@ namespace xe {
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline); vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
} }
void Pipeline::defaultPipelineConfigInfo(PipelineConfigInfo& configInfo) { void Pipeline::defaultPipelineConfigInfo(PipelineConfigInfo& configInfo, Device& device) {
configInfo.inputAssemblyInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; configInfo.inputAssemblyInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
configInfo.inputAssemblyInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; configInfo.inputAssemblyInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
@ -162,7 +162,7 @@ namespace xe {
configInfo.multisampleInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; configInfo.multisampleInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
configInfo.multisampleInfo.sampleShadingEnable = VK_FALSE; configInfo.multisampleInfo.sampleShadingEnable = VK_FALSE;
configInfo.multisampleInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; configInfo.multisampleInfo.rasterizationSamples = device.getSamples();
configInfo.multisampleInfo.minSampleShading = 1.0f; configInfo.multisampleInfo.minSampleShading = 1.0f;
configInfo.multisampleInfo.pSampleMask = nullptr; configInfo.multisampleInfo.pSampleMask = nullptr;
configInfo.multisampleInfo.alphaToCoverageEnable = VK_FALSE; configInfo.multisampleInfo.alphaToCoverageEnable = VK_FALSE;

View file

@ -44,7 +44,7 @@ class Pipeline {
Pipeline operator=(const Pipeline&) = delete; Pipeline operator=(const Pipeline&) = delete;
void bind(VkCommandBuffer commandBuffer); void bind(VkCommandBuffer commandBuffer);
static void defaultPipelineConfigInfo(PipelineConfigInfo& configInfo); static void defaultPipelineConfigInfo(PipelineConfigInfo& configInfo, Device& device);
private: private:
static std::vector<char> readFile(const std::string& filePath); static std::vector<char> readFile(const std::string& filePath);

View file

@ -130,7 +130,7 @@ void RenderSystem::createPipeline(VkRenderPass renderPass, std::string vert, std
assert(pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout"); assert(pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout");
PipelineConfigInfo pipelineConfig{}; PipelineConfigInfo pipelineConfig{};
Pipeline::defaultPipelineConfigInfo(pipelineConfig); Pipeline::defaultPipelineConfigInfo(pipelineConfig, xeDevice);
if (cullingEnabled) { if (cullingEnabled) {
pipelineConfig.rasterizationInfo.cullMode = VK_CULL_MODE_BACK_BIT; pipelineConfig.rasterizationInfo.cullMode = VK_CULL_MODE_BACK_BIT;
} }

View file

@ -20,6 +20,7 @@ void SwapChain::init() {
createSwapChain(); createSwapChain();
createImageViews(); createImageViews();
createRenderPass(); createRenderPass();
createColorResources();
createDepthResources(); createDepthResources();
createFramebuffers(); createFramebuffers();
createSyncObjects(); createSyncObjects();
@ -37,6 +38,12 @@ SwapChain::~SwapChain() {
swapChain = nullptr; swapChain = nullptr;
} }
for (int i = 0; i < colorImages.size(); i++) {
vkDestroyImageView(device.device(), colorImageViews[i], nullptr);
vkDestroyImage(device.device(), colorImages[i], nullptr);
vkFreeMemory(device.device(), colorImageMemorys[i], nullptr);
}
for (int i = 0; i < depthImages.size(); i++) { for (int i = 0; i < depthImages.size(); i++) {
vkDestroyImageView(device.device(), depthImageViews[i], nullptr); vkDestroyImageView(device.device(), depthImageViews[i], nullptr);
vkDestroyImage(device.device(), depthImages[i], nullptr); vkDestroyImage(device.device(), depthImages[i], nullptr);
@ -202,9 +209,24 @@ void SwapChain::createImageViews() {
} }
void SwapChain::createRenderPass() { void SwapChain::createRenderPass() {
VkAttachmentDescription colorAttachment = {};
colorAttachment.format = getSwapChainImageFormat();
colorAttachment.samples = device.getSamples();
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkAttachmentReference colorAttachmentRef = {};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkAttachmentDescription depthAttachment{}; VkAttachmentDescription depthAttachment{};
depthAttachment.format = findDepthFormat(); depthAttachment.format = findDepthFormat();
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT; depthAttachment.samples = device.getSamples();
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
@ -216,25 +238,26 @@ void SwapChain::createRenderPass() {
depthAttachmentRef.attachment = 1; depthAttachmentRef.attachment = 1;
depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
VkAttachmentDescription colorAttachment = {}; VkAttachmentDescription colorAttachmentResolve{};
colorAttachment.format = getSwapChainImageFormat(); colorAttachmentResolve.format = getSwapChainImageFormat();
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT; colorAttachmentResolve.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; colorAttachmentResolve.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; colorAttachmentResolve.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; colorAttachmentResolve.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; colorAttachmentResolve.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; colorAttachmentResolve.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; colorAttachmentResolve.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
VkAttachmentReference colorAttachmentRef = {}; VkAttachmentReference colorAttachmentResolveRef{};
colorAttachmentRef.attachment = 0; colorAttachmentResolveRef.attachment = 2;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; colorAttachmentResolveRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkSubpassDescription subpass = {}; VkSubpassDescription subpass = {};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1; subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &colorAttachmentRef; subpass.pColorAttachments = &colorAttachmentRef;
subpass.pDepthStencilAttachment = &depthAttachmentRef; subpass.pDepthStencilAttachment = &depthAttachmentRef;
subpass.pResolveAttachments = &colorAttachmentResolveRef;
VkSubpassDependency dependency = {}; VkSubpassDependency dependency = {};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL; dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
@ -247,7 +270,7 @@ void SwapChain::createRenderPass() {
dependency.dstAccessMask = dependency.dstAccessMask =
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
std::array<VkAttachmentDescription, 2> attachments = {colorAttachment, depthAttachment}; std::array<VkAttachmentDescription, 3> attachments = {colorAttachment, depthAttachment, colorAttachmentResolve};
VkRenderPassCreateInfo renderPassInfo = {}; VkRenderPassCreateInfo renderPassInfo = {};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size()); renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
@ -265,7 +288,7 @@ void SwapChain::createRenderPass() {
void SwapChain::createFramebuffers() { void SwapChain::createFramebuffers() {
swapChainFramebuffers.resize(imageCount()); swapChainFramebuffers.resize(imageCount());
for (size_t i = 0; i < imageCount(); i++) { for (size_t i = 0; i < imageCount(); i++) {
std::array<VkImageView, 2> attachments = {swapChainImageViews[i], depthImageViews[i]}; std::array<VkImageView, 3> attachments = {colorImageViews[i], depthImageViews[i], swapChainImageViews[i]};
VkExtent2D swapChainExtent = getSwapChainExtent(); VkExtent2D swapChainExtent = getSwapChainExtent();
VkFramebufferCreateInfo framebufferInfo = {}; VkFramebufferCreateInfo framebufferInfo = {};
@ -287,6 +310,20 @@ void SwapChain::createFramebuffers() {
} }
} }
void SwapChain::createColorResources() {
VkFormat colorFormat = getSwapChainImageFormat();
VkExtent2D swapChainExtent = getSwapChainExtent();
colorImages.resize(imageCount());
colorImageMemorys.resize(imageCount());
colorImageViews.resize(imageCount());
for (int i = 0; i < colorImages.size(); i++) {
Image::createImage(device, swapChainExtent.width, swapChainExtent.height, 1, device.getSamples(), colorFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, colorImages[i], colorImageMemorys[i]);
colorImageViews[i] = Image::createImageView(device, colorImages[i], colorFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1);
}
}
void SwapChain::createDepthResources() { void SwapChain::createDepthResources() {
VkFormat depthFormat = findDepthFormat(); VkFormat depthFormat = findDepthFormat();
swapChainDepthFormat = depthFormat; swapChainDepthFormat = depthFormat;
@ -297,42 +334,8 @@ void SwapChain::createDepthResources() {
depthImageViews.resize(imageCount()); depthImageViews.resize(imageCount());
for (int i = 0; i < depthImages.size(); i++) { for (int i = 0; i < depthImages.size(); i++) {
VkImageCreateInfo imageInfo{}; Image::createImage(device, swapChainExtent.width, swapChainExtent.height, 1, device.getSamples(), depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImages[i], depthImageMemorys[i]);
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; depthImageViews[i] = Image::createImageView(device, depthImages[i], depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.extent.width = swapChainExtent.width;
imageInfo.extent.height = swapChainExtent.height;
imageInfo.extent.depth = 1;
imageInfo.mipLevels = 1;
imageInfo.arrayLayers = 1;
imageInfo.format = depthFormat;
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageInfo.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageInfo.flags = 0;
device.createImageWithInfo(
imageInfo,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
depthImages[i],
depthImageMemorys[i]);
VkImageViewCreateInfo viewInfo{};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = depthImages[i];
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewInfo.format = depthFormat;
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = 1;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 1;
if (vkCreateImageView(device.device(), &viewInfo, nullptr, &depthImageViews[i]) != VK_SUCCESS) {
throw std::runtime_error("failed to create texture image view!");
}
} }
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "xe_device.hpp" #include "xe_device.hpp"
#include "xe_image.hpp"
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
@ -52,6 +53,7 @@ class SwapChain {
void init(); void init();
void createSwapChain(); void createSwapChain();
void createImageViews(); void createImageViews();
void createColorResources();
void createDepthResources(); void createDepthResources();
void createRenderPass(); void createRenderPass();
void createFramebuffers(); void createFramebuffers();
@ -73,6 +75,9 @@ class SwapChain {
std::vector<VkImage> depthImages; std::vector<VkImage> depthImages;
std::vector<VkDeviceMemory> depthImageMemorys; std::vector<VkDeviceMemory> depthImageMemorys;
std::vector<VkImageView> depthImageViews; std::vector<VkImageView> depthImageViews;
std::vector<VkImage> colorImages;
std::vector<VkDeviceMemory> colorImageMemorys;
std::vector<VkImageView> colorImageViews;
std::vector<VkImage> swapChainImages; std::vector<VkImage> swapChainImages;
std::vector<VkImageView> swapChainImageViews; std::vector<VkImageView> swapChainImageViews;