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

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

Replace some couts with runtime_exceptions and, in vulkangame, only call SDL_SetRenderTarget once, during initialization

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