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
RevLine 
[7d2b0b9]1#ifndef _GRAPHICS_PIPELINE_VULKAN_H
2#define _GRAPHICS_PIPELINE_VULKAN_H
3
4#include "graphics-pipeline.hpp"
5
[cd487fb]6#include <stdexcept>
[7d2b0b9]7#include <vector>
8
[771b33a]9#include <vulkan/vulkan.h>
10
[e3bef3a]11#include "vulkan-utils.hpp"
12
13using namespace std;
14
[b794178]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
[7d2b0b9]25class GraphicsPipeline_Vulkan : public GraphicsPipeline {
26 public:
[87c8f1a]27 GraphicsPipeline_Vulkan(VkPhysicalDevice physicalDevice, VkDevice device, VkRenderPass renderPass,
28 Viewport viewport, int vertexSize);
[7d2b0b9]29 ~GraphicsPipeline_Vulkan();
30
[0ae182f]31 void updateRenderPass(VkRenderPass renderPass);
32
[e3bef3a]33 template<class VertexType>
34 void bindData(const vector<VertexType>& vertices, const vector<uint16_t>& indices,
[87c8f1a]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
[b794178]42 // Maybe I should rename these to addVertexAttribute (addVaryingAttribute) and addUniformAttribute
43
[771b33a]44 void addAttribute(VkFormat format, size_t offset);
[b794178]45
46 void addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData);
47 void addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData);
48
[7d2b0b9]49 void createPipeline(string vertShaderFile, string fragShaderFile);
[b794178]50 void createDescriptorSetLayout();
51 void createDescriptorPool(vector<VkImage>& swapChainImages);
52 void createDescriptorSets(vector<VkImage>& swapChainImages);
53
[603b5bc]54 void createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage);
55
[e3bef3a]56 template<class VertexType>
57 bool addObject(const vector<VertexType>& vertices, vector<uint16_t>& indices, VkCommandPool commandPool,
58 VkQueue graphicsQueue);
59
[b794178]60 void cleanup();
61 void cleanupBuffers();
[7d2b0b9]62
63 private:
[b794178]64 VkShaderModule createShaderModule(const vector<char>& code);
65 vector<char> readFile(const string& filename);
66
[87c8f1a]67 VkPhysicalDevice physicalDevice;
[7d2b0b9]68 VkDevice device;
[b794178]69 VkRenderPass renderPass;
70
71 VkPipeline pipeline;
72 VkPipelineLayout pipelineLayout;
73
[771b33a]74 VkVertexInputBindingDescription bindingDescription;
[b794178]75
[771b33a]76 vector<VkVertexInputAttributeDescription> attributeDescriptions;
[b794178]77 vector<DescriptorInfo> descriptorInfoList;
[7d2b0b9]78
[b794178]79 VkDescriptorSetLayout descriptorSetLayout;
80 VkDescriptorPool descriptorPool;
81 vector<VkDescriptorSet> descriptorSets;
[87c8f1a]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;
[7d2b0b9]92};
93
[e3bef3a]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,
[87c8f1a]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;
[e3bef3a]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) {
[cd487fb]115 throw runtime_error("ERROR: Need to resize vertex buffers");
[e3bef3a]116 } else if (numIndices + indices.size() > indexCapacity) {
[cd487fb]117 throw runtime_error("ERROR: Need to resize index buffers");
[e3bef3a]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;
[87c8f1a]135}
136
[7d2b0b9]137#endif // _GRAPHICS_PIPELINE_VULKAN_H
Note: See TracBrowser for help on using the repository browser.