Changeset 771b33a in opengl-game


Ignore:
Timestamp:
Oct 4, 2019, 8:39:46 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
b794178
Parents:
0b1b52d
Message:

In openglgame, port over some more of the pipeline creation code and the functionality to specify and initialize varying attributes

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • graphics-pipeline_vulkan.cpp

    r0b1b52d r771b33a  
    44#include <stdexcept>
    55
    6 GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan(VkDevice device) {
     6GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan(VkDevice device, Viewport viewport, int vertexSize) {
    77   this->device = device;
     8   this->viewport = viewport;
     9
     10   this->bindingDescription.binding = 0;
     11   this->bindingDescription.stride = vertexSize;
     12   this->bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
    813}
    914
    1015GraphicsPipeline_Vulkan::~GraphicsPipeline_Vulkan() {
     16}
     17
     18void GraphicsPipeline_Vulkan::addAttribute(VkFormat format, size_t offset) {
     19   VkVertexInputAttributeDescription attributeDesc = {};
     20
     21   attributeDesc.binding = 0;
     22   attributeDesc.location = this->attributeDescriptions.size();
     23   attributeDesc.format = format;
     24   attributeDesc.offset = offset;
     25
     26   this->attributeDescriptions.push_back(attributeDesc);
    1127}
    1228
     
    3450   VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
    3551   vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
     52
     53   vertexInputInfo.vertexBindingDescriptionCount = 1;
     54   vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(this->attributeDescriptions.size());
     55   vertexInputInfo.pVertexBindingDescriptions = &this->bindingDescription;
     56   vertexInputInfo.pVertexAttributeDescriptions = this->attributeDescriptions.data();
     57
     58   VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
     59   inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
     60   inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
     61   inputAssembly.primitiveRestartEnable = VK_FALSE;
     62
     63   VkViewport viewport = {};
     64   viewport.x = (float)this->viewport.x;
     65   viewport.y = (float)this->viewport.y;
     66   viewport.width = (float)this->viewport.width;
     67   viewport.height = (float)this->viewport.height;
     68   viewport.minDepth = 0.0f;
     69   viewport.maxDepth = 1.0f;
     70
     71   VkRect2D scissor = {};
     72   scissor.offset = { 0, 0 };
     73   scissor.extent = { (uint32_t)this->viewport.width, (uint32_t)this->viewport.height };
     74
     75   VkPipelineViewportStateCreateInfo viewportState = {};
     76   viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
     77   viewportState.viewportCount = 1;
     78   viewportState.pViewports = &viewport;
     79   viewportState.scissorCount = 1;
     80   viewportState.pScissors = &scissor;
     81
     82   VkPipelineRasterizationStateCreateInfo rasterizer = {};
     83   rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
     84   rasterizer.depthClampEnable = VK_FALSE;
     85   rasterizer.rasterizerDiscardEnable = VK_FALSE;
     86   rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
     87   rasterizer.lineWidth = 1.0f;
     88   rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
     89   rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
     90   rasterizer.depthBiasEnable = VK_FALSE;
     91
     92   VkPipelineMultisampleStateCreateInfo multisampling = {};
     93   multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
     94   multisampling.sampleShadingEnable = VK_FALSE;
     95   multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
     96
     97   VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
     98   colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
     99   colorBlendAttachment.blendEnable = VK_TRUE;
     100   colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
     101   colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
     102   colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
     103   colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
     104   colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
     105   colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
     106
     107   VkPipelineColorBlendStateCreateInfo colorBlending = {};
     108   colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
     109   colorBlending.logicOpEnable = VK_FALSE;
     110   colorBlending.logicOp = VK_LOGIC_OP_COPY;
     111   colorBlending.attachmentCount = 1;
     112   colorBlending.pAttachments = &colorBlendAttachment;
     113   colorBlending.blendConstants[0] = 0.0f;
     114   colorBlending.blendConstants[1] = 0.0f;
     115   colorBlending.blendConstants[2] = 0.0f;
     116   colorBlending.blendConstants[3] = 0.0f;
     117
     118   VkPipelineDepthStencilStateCreateInfo depthStencil = {};
     119   depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
     120   depthStencil.depthTestEnable = VK_TRUE;
     121   depthStencil.depthWriteEnable = VK_TRUE;
     122   depthStencil.depthCompareOp = VK_COMPARE_OP_LESS;
     123   depthStencil.depthBoundsTestEnable = VK_FALSE;
     124   depthStencil.minDepthBounds = 0.0f;
     125   depthStencil.maxDepthBounds = 1.0f;
     126   depthStencil.stencilTestEnable = VK_FALSE;
     127   depthStencil.front = {};
     128   depthStencil.back = {};
     129
     130   VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
     131   pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
     132   pipelineLayoutInfo.setLayoutCount = 1;
    36133
    37134   vkDestroyShaderModule(device, vertShaderModule, nullptr);
  • graphics-pipeline_vulkan.hpp

    r0b1b52d r771b33a  
    44#include "graphics-pipeline.hpp"
    55
     6#include <vector>
     7
    68#include <vulkan/vulkan.h>
    7 
    8 #include <vector>
    99
    1010class GraphicsPipeline_Vulkan : public GraphicsPipeline {
    1111   public:
    12       GraphicsPipeline_Vulkan(VkDevice device);
     12      GraphicsPipeline_Vulkan(VkDevice device, Viewport viewport, int vertexSize);
    1313      ~GraphicsPipeline_Vulkan();
    1414
     15      void addAttribute(VkFormat format, size_t offset);
    1516      void createPipeline(string vertShaderFile, string fragShaderFile);
    1617   
    1718   private:
    1819      VkDevice device;
     20      VkVertexInputBindingDescription bindingDescription;
     21      vector<VkVertexInputAttributeDescription> attributeDescriptions;
    1922
    2023      VkShaderModule createShaderModule(const vector<char>& code);
  • vulkan-game.cpp

    r0b1b52d r771b33a  
    88#include "logger.hpp"
    99
     10#include "utils.hpp"
    1011#include "vulkan-utils.hpp"
    1112
     
    106107   createCommandPool();
    107108
    108    graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device));
     109   graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device, viewport, sizeof(Vertex)));
     110
     111   graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::pos));
     112   graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::color));
     113   graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&Vertex::texCoord));
     114
    109115   graphicsPipelines.back().createPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv");
    110116
    111    graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device));
     117   graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device, viewport, sizeof(OverlayVertex)));
     118
     119   graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos));
     120   graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32_SFLOAT, offset_of(&OverlayVertex::texCoord));
     121
    112122   graphicsPipelines.back().createPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv");
    113123
     
    427437
    428438   swapChainImageFormat = surfaceFormat.format;
    429    swapChainExtent = extent;
     439   viewport = { 0, 0, (int)extent.width, (int)extent.height };
    430440}
    431441
  • vulkan-game.hpp

    r0b1b52d r771b33a  
    11#ifndef _VULKAN_GAME_H
    22#define _VULKAN_GAME_H
     3
     4#include <glm/glm.hpp>
    35
    46#include "game-gui-sdl.hpp"
     
    1113#endif
    1214
     15// TODO: Figure out if these structs should be defined in the VulkanGame class
     16
     17struct Vertex {
     18   glm::vec3 pos;
     19   glm::vec3 color;
     20   glm::vec2 texCoord;
     21};
     22
     23struct OverlayVertex {
     24   glm::vec3 pos;
     25   glm::vec2 texCoord;
     26};
     27
    1328class VulkanGame {
    1429   public:
     
    2035   private:
    2136      GameGui* gui;
     37      Viewport viewport;
    2238
    2339      vector<GraphicsPipeline_Vulkan> graphicsPipelines;
     
    3955      vector<VkImage> swapChainImages;
    4056      VkFormat swapChainImageFormat;
    41       VkExtent2D swapChainExtent;
    4257      vector<VkImageView> swapChainImageViews;
    4358
  • vulkan-ref.cpp

    r0b1b52d r771b33a  
    175175      VkFormat swapChainImageFormat;
    176176/*** END OF REFACTORED CODE ***/
    177       VkExtent2D swapChainExtent; // (This was taken out of vulkan-game for now and replaced with Viewport)
     177      // (This was taken out of vulkan-game for now and replaced with Viewport)
     178      // It will definitely be needed when creating render passes and I could use it in a few other places
     179      VkExtent2D swapChainExtent;
    178180/*** START OF REFACTORED CODE ***/
    179181      vector<VkImageView> swapChainImageViews;
Note: See TracChangeset for help on using the changeset viewer.