Changeset adcd252 in opengl-game


Ignore:
Timestamp:
Aug 2, 2019, 8:00:34 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
bba12e7
Parents:
4f63fa8
Message:

Add a depth buffer for depth testing

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • shaders/shader.vert

    r4f63fa8 radcd252  
    88} ubo;
    99
    10 layout(location = 0) in vec2 inPosition;
     10layout(location = 0) in vec3 inPosition;
    1111layout(location = 1) in vec3 inColor;
    1212layout(location = 2) in vec2 inTexCoord;
     
    1616
    1717void main() {
    18    gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0);
     18   gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
    1919   fragColor = inColor;
    2020   fragTexCoord = inTexCoord;
  • vulkan-game.cpp

    r4f63fa8 radcd252  
    6767
    6868struct Vertex {
    69    glm::vec2 pos;
     69   glm::vec3 pos;
    7070   glm::vec3 color;
    7171   glm::vec2 texCoord;
     
    8686      attributeDescriptions[0].binding = 0;
    8787      attributeDescriptions[0].location = 0;
    88       attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT;
     88      attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
    8989      attributeDescriptions[0].offset = offsetof(Vertex, pos);
    9090
     
    110110
    111111const vector<Vertex> vertices = {
    112    {{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
    113    {{ 0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
    114    {{ 0.5f,  0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
    115    {{-0.5f,  0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
     112   {{-0.5f, -0.5f,  0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
     113   {{ 0.5f, -0.5f,  0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
     114   {{ 0.5f,  0.5f,  0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
     115   {{-0.5f,  0.5f,  0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
     116
     117   {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
     118   {{ 0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
     119   {{ 0.5f,  0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
     120   {{-0.5f,  0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
    116121};
    117122
    118123const vector<uint16_t> indices = {
    119    0, 1, 2, 2, 3, 0
     124   0, 1, 2, 2, 3, 0,
     125   4, 5, 6, 6, 7, 4
    120126};
    121127
     
    184190
    185191      VkCommandPool commandPool;
     192
     193      VkImage depthImage;
     194      VkDeviceMemory depthImageMemory;
     195      VkImageView depthImageView;
    186196
    187197      VkImage textureImage;
     
    236246         createDescriptorSetLayout();
    237247         createGraphicsPipeline();
     248         createCommandPool();
     249         createDepthResources();
    238250         createFramebuffers();
    239          createCommandPool();
    240251         createTextureImage();
    241252         createTextureImageView();
     
    596607
    597608         for (size_t i = 0; i < swapChainImages.size(); i++) {
    598             swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat);
     609            swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT);
    599610         }
    600611      }
     
    615626         colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
    616627
     628         VkAttachmentDescription depthAttachment = {};
     629         depthAttachment.format = findDepthFormat();
     630         depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
     631         depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
     632         depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
     633         depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
     634         depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
     635         depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
     636         depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
     637
     638         VkAttachmentReference depthAttachmentRef = {};
     639         depthAttachmentRef.attachment = 1;
     640         depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
     641
    617642         VkSubpassDescription subpass = {};
    618643         subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
    619644         subpass.colorAttachmentCount = 1;
    620645         subpass.pColorAttachments = &colorAttachmentRef;
     646         subpass.pDepthStencilAttachment = &depthAttachmentRef;
    621647
    622648         VkSubpassDependency dependency = {};
     
    628654         dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
    629655
     656         array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };
    630657         VkRenderPassCreateInfo renderPassInfo = {};
    631658         renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
    632          renderPassInfo.attachmentCount = 1;
    633          renderPassInfo.pAttachments = &colorAttachment;
     659         renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
     660         renderPassInfo.pAttachments = attachments.data();
    634661         renderPassInfo.subpassCount = 1;
    635662         renderPassInfo.pSubpasses = &subpass;
     
    754781         colorBlending.blendConstants[3] = 0.0f;
    755782
     783         VkPipelineDepthStencilStateCreateInfo depthStencil = {};
     784         depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
     785         depthStencil.depthTestEnable = VK_TRUE;
     786         depthStencil.depthWriteEnable = VK_TRUE;
     787         depthStencil.depthCompareOp = VK_COMPARE_OP_LESS;
     788         depthStencil.depthBoundsTestEnable = VK_FALSE;
     789         depthStencil.minDepthBounds = 0.0f;
     790         depthStencil.maxDepthBounds = 1.0f;
     791         depthStencil.stencilTestEnable = VK_FALSE;
     792         depthStencil.front = {};
     793         depthStencil.back = {};
     794
    756795         VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
    757796         pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
     
    773812         pipelineInfo.pRasterizationState = &rasterizer;
    774813         pipelineInfo.pMultisampleState = &multisampling;
    775          pipelineInfo.pDepthStencilState = nullptr;
     814         pipelineInfo.pDepthStencilState = &depthStencil;
    776815         pipelineInfo.pColorBlendState = &colorBlending;
    777816         pipelineInfo.pDynamicState = nullptr;
     
    808847
    809848         for (size_t i = 0; i < swapChainImageViews.size(); i++) {
    810             VkImageView attachments[] = {
    811                swapChainImageViews[i]
     849            array <VkImageView, 2> attachments = {
     850               swapChainImageViews[i],
     851               depthImageView
    812852            };
    813853
     
    815855            framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    816856            framebufferInfo.renderPass = renderPass;
    817             framebufferInfo.attachmentCount = 1;
    818             framebufferInfo.pAttachments = attachments;
     857            framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
     858            framebufferInfo.pAttachments = attachments.data();
    819859            framebufferInfo.width = swapChainExtent.width;
    820860            framebufferInfo.height = swapChainExtent.height;
     
    870910
    871911         return indices;
     912      }
     913
     914      void createDepthResources() {
     915         VkFormat depthFormat = findDepthFormat();
     916
     917         createImage(swapChainExtent.width, swapChainExtent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL,
     918            VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImage, depthImageMemory);
     919         depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
     920
     921         transitionImageLayout(depthImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
     922      }
     923
     924      VkFormat findDepthFormat() {
     925         return findSupportedFormat(
     926            { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT },
     927            VK_IMAGE_TILING_OPTIMAL,
     928            VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
     929         );
     930      }
     931
     932      VkFormat findSupportedFormat(const vector<VkFormat>& candidates, VkImageTiling tiling,
     933            VkFormatFeatureFlags features) {
     934         for (VkFormat format : candidates) {
     935            VkFormatProperties props;
     936            vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &props);
     937
     938            if (tiling == VK_IMAGE_TILING_LINEAR &&
     939                  (props.linearTilingFeatures & features) == features) {
     940               return format;
     941            } else if (tiling == VK_IMAGE_TILING_OPTIMAL &&
     942                  (props.optimalTilingFeatures & features) == features) {
     943               return format;
     944            }
     945         }
     946
     947         throw runtime_error("failed to find supported format!");
     948      }
     949
     950      bool hasStencilComponent(VkFormat format) {
     951         return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
    872952      }
    873953
     
    9551035         barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
    9561036         barrier.image = image;
    957          barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
     1037
     1038         if (newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
     1039            barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
     1040
     1041            if (hasStencilComponent(format)) {
     1042               barrier.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
     1043            }
     1044         } else {
     1045            barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
     1046         }
     1047
    9581048         barrier.subresourceRange.baseMipLevel = 0;
    9591049         barrier.subresourceRange.levelCount = 1;
     
    9761066            sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
    9771067            destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
     1068         } else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
     1069            barrier.srcAccessMask = 0;
     1070            barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
     1071
     1072            sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
     1073            destinationStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
    9781074         } else {
    9791075            throw invalid_argument("unsupported layout transition!");
     
    10191115
    10201116      void createTextureImageView() {
    1021          textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_UNORM);
    1022       }
    1023 
    1024       VkImageView createImageView(VkImage image, VkFormat format) {
     1117         textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
     1118      }
     1119
     1120      VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) {
    10251121         VkImageViewCreateInfo viewInfo = {};
    10261122         viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
     
    10341130         viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
    10351131
    1036          viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
     1132         viewInfo.subresourceRange.aspectMask = aspectFlags;
    10371133         viewInfo.subresourceRange.baseMipLevel = 0;
    10381134         viewInfo.subresourceRange.levelCount = 1;
     
    13141410            renderPassInfo.renderArea.extent = swapChainExtent;
    13151411
    1316             VkClearValue clearColor = { 0.0f, 0.0f, 0.0f, 1.0f };
    1317             renderPassInfo.clearValueCount = 1;
    1318             renderPassInfo.pClearValues = &clearColor;
     1412            array<VkClearValue, 2> clearValues = {};
     1413            clearValues[0].color = { 0.0f, 0.0f, 0.0f, 1.0f };
     1414            clearValues[1].depthStencil = { 1.0f, 0 };
     1415
     1416            renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
     1417            renderPassInfo.pClearValues = clearValues.data();
    13191418
    13201419            vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
     
    14951594         createRenderPass();
    14961595         createGraphicsPipeline();
     1596         createDepthResources();
    14971597         createFramebuffers();
    14981598         createUniformBuffers();
     
    15411641
    15421642      void cleanupSwapChain() {
     1643         vkDestroyImageView(device, depthImageView, nullptr);
     1644         vkDestroyImage(device, depthImage, nullptr);
     1645         vkFreeMemory(device, depthImageMemory, nullptr);
     1646
    15431647         for (auto framebuffer : swapChainFramebuffers) {
    15441648            vkDestroyFramebuffer(device, framebuffer, nullptr);
Note: See TracChangeset for help on using the changeset viewer.