source: opengl-game/graphics-pipeline_vulkan.hpp@ e3bef3a

feature/imgui-sdl points-test
Last change on this file since e3bef3a was e3bef3a, checked in by Dmitry Portnoy <dmp1488@…>, 5 years ago

Finish the rewrite of the original vulkangame project

  • Property mode set to 100644
File size: 4.9 KB
Line 
1#ifndef _GRAPHICS_PIPELINE_VULKAN_H
2#define _GRAPHICS_PIPELINE_VULKAN_H
3
4#include "graphics-pipeline.hpp"
5
6#include <iostream>
7#include <vector>
8
9#include <vulkan/vulkan.h>
10
11#include "vulkan-utils.hpp"
12
13using namespace std;
14
15// TODO: Remove any instances of cout and instead throw exceptions
16
17// TODO: Maybe change the name of this struct so I can call the list something other than descriptorInfoList
18struct DescriptorInfo {
19 VkDescriptorType type;
20 VkShaderStageFlags stageFlags;
21
22 // Only one of the below properties should be set
23 vector<VkDescriptorBufferInfo>* bufferDataList;
24 VkDescriptorImageInfo* imageData;
25};
26
27class GraphicsPipeline_Vulkan : public GraphicsPipeline {
28 public:
29 GraphicsPipeline_Vulkan(VkPhysicalDevice physicalDevice, VkDevice device, VkRenderPass renderPass,
30 Viewport viewport, int vertexSize);
31 ~GraphicsPipeline_Vulkan();
32
33 void updateRenderPass(VkRenderPass renderPass);
34
35 template<class VertexType>
36 void bindData(const vector<VertexType>& vertices, const vector<uint16_t>& indices,
37 VkCommandPool commandPool, VkQueue graphicsQueue);
38
39 void createVertexBuffer(const void* bufferData, int vertexSize, VkCommandPool commandPool,
40 VkQueue graphicsQueue);
41 void createIndexBuffer(const void* bufferData, int indexSize, VkCommandPool commandPool,
42 VkQueue graphicsQueue);
43
44 // Maybe I should rename these to addVertexAttribute (addVaryingAttribute) and addUniformAttribute
45
46 void addAttribute(VkFormat format, size_t offset);
47
48 void addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData);
49 void addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData);
50
51 void createPipeline(string vertShaderFile, string fragShaderFile);
52 void createDescriptorSetLayout();
53 void createDescriptorPool(vector<VkImage>& swapChainImages);
54 void createDescriptorSets(vector<VkImage>& swapChainImages);
55
56 void createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage);
57
58 template<class VertexType>
59 bool addObject(const vector<VertexType>& vertices, vector<uint16_t>& indices, VkCommandPool commandPool,
60 VkQueue graphicsQueue);
61
62 void cleanup();
63 void cleanupBuffers();
64
65 private:
66 VkShaderModule createShaderModule(const vector<char>& code);
67 vector<char> readFile(const string& filename);
68
69 VkPhysicalDevice physicalDevice;
70 VkDevice device;
71 VkRenderPass renderPass;
72
73 VkPipeline pipeline;
74 VkPipelineLayout pipelineLayout;
75
76 VkVertexInputBindingDescription bindingDescription;
77
78 vector<VkVertexInputAttributeDescription> attributeDescriptions;
79 vector<DescriptorInfo> descriptorInfoList;
80
81 VkDescriptorSetLayout descriptorSetLayout;
82 VkDescriptorPool descriptorPool;
83 vector<VkDescriptorSet> descriptorSets;
84
85 size_t numVertices;
86 size_t vertexCapacity;
87 VkBuffer vertexBuffer;
88 VkDeviceMemory vertexBufferMemory;
89
90 size_t numIndices;
91 size_t indexCapacity;
92 VkBuffer indexBuffer;
93 VkDeviceMemory indexBufferMemory;
94};
95
96// TODO: Probably better to template the whole class
97// TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
98
99// TODO: combine this function and the constructor since I call this right after the constructor anyway
100template<class VertexType>
101void GraphicsPipeline_Vulkan::bindData(const vector<VertexType>& vertices, const vector<uint16_t>& indices,
102 VkCommandPool commandPool, VkQueue graphicsQueue) {
103 numVertices = vertices.size();
104 vertexCapacity = numVertices * 2;
105 createVertexBuffer(vertices.data(), sizeof(VertexType), commandPool, graphicsQueue);
106
107 numIndices = indices.size();
108 indexCapacity = numIndices * 2;
109 createIndexBuffer(indices.data(), sizeof(uint16_t), commandPool, graphicsQueue);
110}
111
112template<class VertexType>
113bool GraphicsPipeline_Vulkan::addObject(const vector<VertexType>& vertices, vector<uint16_t>& indices,
114 VkCommandPool commandPool, VkQueue graphicsQueue) {
115 cout << "Adding object to pipeline..." << endl;
116
117 if (numVertices + vertices.size() > vertexCapacity) {
118 cout << "ERROR: Need to resize vertex buffers" << endl;
119 } else if (numIndices + indices.size() > indexCapacity) {
120 cout << "ERROR: Need to resize index buffers" << endl;
121 } else {
122 cout << "Added object to scene" << endl;
123
124 for (uint16_t& idx : indices) {
125 idx += numVertices;
126 }
127
128 VulkanUtils::copyDataToBuffer(device, physicalDevice, commandPool, vertices, vertexBuffer, numVertices,
129 graphicsQueue);
130 numVertices += vertices.size();
131
132 VulkanUtils::copyDataToBuffer(device, physicalDevice, commandPool, indices, indexBuffer, numIndices,
133 graphicsQueue);
134 numIndices += indices.size();
135
136 return true;
137 }
138
139 return false;
140}
141
142#endif // _GRAPHICS_PIPELINE_VULKAN_H
Note: See TracBrowser for help on using the repository browser.