PeriDyno 1.0.0
Loading...
Searching...
No Matches
VkGraphicsPipeline.cpp
Go to the documentation of this file.
2#include "VkTransfer.h"
4
5namespace dyno
6{
9 {
11
12 // Check whether the compute queue family is distinct from the graphics queue family
13 specializedComputeQueue = ctx->queueFamilyIndices.graphics != ctx->queueFamilyIndices.compute;
14 };
15
17 {
18 // Graphics
19 for (auto& shderModule : shaderModules) {
20 vkDestroyShaderModule(ctx->deviceHandle(), shderModule, nullptr);
21 }
22 vkDestroyPipeline(ctx->deviceHandle(), pipeline, nullptr);
23 vkDestroyPipeline(ctx->deviceHandle(), spherePipeline, nullptr);
24 vkDestroyPipeline(ctx->deviceHandle(), capsulePipeline, nullptr);
25 vkDestroyPipelineLayout(ctx->deviceHandle(), pipelineLayout, nullptr);
26 vkDestroyDescriptorSetLayout(ctx->deviceHandle(), descriptorSetLayout, nullptr);
27 vkDestroyDescriptorPool(ctx->deviceHandle(), descriptorPool, nullptr);
28 };
29
31 {
32 std::vector<VkDescriptorPoolSize> poolSizes = {
33 vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 3),
34 vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 4),
35 vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2)
36 };
37
38 VkDescriptorPoolCreateInfo descriptorPoolInfo =
40
41 VK_CHECK_RESULT(vkCreateDescriptorPool(ctx->deviceHandle(), &descriptorPoolInfo, nullptr, &descriptorPool));
42 }
43
45 {
46 // Set layout
47 std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
48 vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0)
49 };
50 VkDescriptorSetLayoutCreateInfo descriptorLayout =
52 VK_CHECK_RESULT(vkCreateDescriptorSetLayout(ctx->deviceHandle(), &descriptorLayout, nullptr, &descriptorSetLayout));
53
54 VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo =
56 VK_CHECK_RESULT(vkCreatePipelineLayout(ctx->deviceHandle(), &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));
57
58 // Set
59 VkDescriptorSetAllocateInfo allocInfo =
61 VK_CHECK_RESULT(vkAllocateDescriptorSets(ctx->deviceHandle(), &allocInfo, &descriptorSet));
62
63 std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
64 vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &mUniform.getDescriptor())
65 };
66 vkUpdateDescriptorSets(ctx->deviceHandle(), static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
67 }
68
69 void VkGraphicsPipeline::preparePipelines(VkRenderPass renderPass)
70 {
71 VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
72 vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
73
74 VkPipelineRasterizationStateCreateInfo rasterizationState =
75 vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
76
77 VkPipelineColorBlendAttachmentState blendAttachmentState =
79
80 VkPipelineColorBlendStateCreateInfo colorBlendState =
82
83 VkPipelineDepthStencilStateCreateInfo depthStencilState =
84 vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);
85
86 VkPipelineViewportStateCreateInfo viewportState =
88
89 VkPipelineMultisampleStateCreateInfo multisampleState =
91
92 std::vector<VkDynamicState> dynamicStateEnables = {
93 VK_DYNAMIC_STATE_VIEWPORT,
94 VK_DYNAMIC_STATE_SCISSOR
95 };
96 VkPipelineDynamicStateCreateInfo dynamicState =
98
99 // Rendering pipeline
100 std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
101
102 shaderStages[0] = loadShader(getShadersPath() + "graphics/render.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
103 shaderStages[1] = loadShader(getShadersPath() + "graphics/render.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
104
105 VkGraphicsPipelineCreateInfo pipelineCreateInfo = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass);
106
107 // Input attributes
108
109 // Binding description
110 std::vector<VkVertexInputBindingDescription> inputBindings = {
111 vks::initializers::vertexInputBindingDescription(0, sizeof(Vertex), VK_VERTEX_INPUT_RATE_VERTEX),
112 vks::initializers::vertexInputBindingDescription(1, sizeof(px::Box), VK_VERTEX_INPUT_RATE_INSTANCE)
113
114 };
115
116 // Attribute descriptions
117 std::vector<VkVertexInputAttributeDescription> inputAttributes = {
118 vks::initializers::vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, pos)),
119 vks::initializers::vertexInputAttributeDescription(0, 1, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, normal)),
120 vks::initializers::vertexInputAttributeDescription(0, 2, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex, uv)),
121
122 vks::initializers::vertexInputAttributeDescription(1, 3, VK_FORMAT_R32G32B32_SFLOAT, offsetof(px::Box, center)),
123 vks::initializers::vertexInputAttributeDescription(1, 4, VK_FORMAT_R32G32B32_SFLOAT, offsetof(px::Box, halfLength)),
124 vks::initializers::vertexInputAttributeDescription(1, 5, VK_FORMAT_R32G32B32A32_SFLOAT, offsetof(px::Box, rot))
125 };
126
127 // Assign to vertex buffer
128 VkPipelineVertexInputStateCreateInfo inputState = vks::initializers::pipelineVertexInputStateCreateInfo();
129 inputState.vertexBindingDescriptionCount = static_cast<uint32_t>(inputBindings.size());
130 inputState.pVertexBindingDescriptions = inputBindings.data();
131 inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(inputAttributes.size());
132 inputState.pVertexAttributeDescriptions = inputAttributes.data();
133
134 pipelineCreateInfo.pVertexInputState = &inputState;
135 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
136 pipelineCreateInfo.pRasterizationState = &rasterizationState;
137 pipelineCreateInfo.pColorBlendState = &colorBlendState;
138 pipelineCreateInfo.pMultisampleState = &multisampleState;
139 pipelineCreateInfo.pViewportState = &viewportState;
140 pipelineCreateInfo.pDepthStencilState = &depthStencilState;
141 pipelineCreateInfo.pDynamicState = &dynamicState;
142 pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
143 pipelineCreateInfo.pStages = shaderStages.data();
144 pipelineCreateInfo.renderPass = renderPass;
145 VK_CHECK_RESULT(vkCreateGraphicsPipelines(ctx->deviceHandle(), ctx->pipelineCacheHandle(), 1, &pipelineCreateInfo, nullptr, &pipeline));
146
147 // create sphere pipeline
148 shaderStages[0] = loadShader(getShadersPath() + "graphics/renderSphere.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
149
150 inputBindings = {
151 vks::initializers::vertexInputBindingDescription(0, sizeof(Vertex), VK_VERTEX_INPUT_RATE_VERTEX),
152 vks::initializers::vertexInputBindingDescription(1, sizeof(px::Sphere), VK_VERTEX_INPUT_RATE_INSTANCE)
153
154 };
155
156 inputAttributes = {
157 vks::initializers::vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, pos)),
158 vks::initializers::vertexInputAttributeDescription(0, 1, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, normal)),
159 vks::initializers::vertexInputAttributeDescription(0, 2, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex, uv)),
160
161 vks::initializers::vertexInputAttributeDescription(1, 3, VK_FORMAT_R32G32B32A32_SFLOAT, offsetof(px::Sphere, rot)),
162 vks::initializers::vertexInputAttributeDescription(1, 4, VK_FORMAT_R32G32B32_SFLOAT, offsetof(px::Sphere, center)),
163 vks::initializers::vertexInputAttributeDescription(1, 5, VK_FORMAT_R32_SFLOAT, offsetof(px::Sphere, radius))
164 };
165 inputState.pVertexBindingDescriptions = inputBindings.data();
166 inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(inputAttributes.size());
167 inputState.pVertexAttributeDescriptions = inputAttributes.data();
168 pipelineCreateInfo.pVertexInputState = &inputState;
169 pipelineCreateInfo.pStages = shaderStages.data();
170 VK_CHECK_RESULT(vkCreateGraphicsPipelines(ctx->deviceHandle(), ctx->pipelineCacheHandle(), 1, &pipelineCreateInfo, nullptr, &spherePipeline));
171
172 // create capsule pipeline
173 shaderStages[0] = loadShader(getShadersPath() + "graphics/renderCapsule.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
174
175 inputBindings = {
176 vks::initializers::vertexInputBindingDescription(0, sizeof(Vertex), VK_VERTEX_INPUT_RATE_VERTEX),
177 vks::initializers::vertexInputBindingDescription(1, sizeof(px::Capsule), VK_VERTEX_INPUT_RATE_INSTANCE)
178
179 };
180
181 inputAttributes = {
182 vks::initializers::vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, pos)),
183 vks::initializers::vertexInputAttributeDescription(0, 1, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, normal)),
184 vks::initializers::vertexInputAttributeDescription(0, 2, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex, uv)),
185
186 vks::initializers::vertexInputAttributeDescription(1, 3, VK_FORMAT_R32G32B32A32_SFLOAT, offsetof(px::Capsule, rot)),
187 vks::initializers::vertexInputAttributeDescription(1, 4, VK_FORMAT_R32G32B32_SFLOAT, offsetof(px::Capsule, center)),
188 vks::initializers::vertexInputAttributeDescription(1, 5, VK_FORMAT_R32_SFLOAT, offsetof(px::Capsule, halfLength)),
189 vks::initializers::vertexInputAttributeDescription(1, 6, VK_FORMAT_R32_SFLOAT, offsetof(px::Capsule, radius))
190 };
191 inputState.pVertexBindingDescriptions = inputBindings.data();
192 inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(inputAttributes.size());
193 inputState.pVertexAttributeDescriptions = inputAttributes.data();
194 pipelineCreateInfo.pVertexInputState = &inputState;
195 pipelineCreateInfo.pStages = shaderStages.data();
196 VK_CHECK_RESULT(vkCreateGraphicsPipelines(ctx->deviceHandle(), ctx->pipelineCacheHandle(), 1, &pipelineCreateInfo, nullptr, &capsulePipeline));
197 }
198
199// void VkGraphicsPipeline::addGraphicsToComputeBarriers(VkCommandBuffer commandBuffer)
200// {
201// if (specializedComputeQueue) {
202// VkBufferMemoryBarrier bufferBarrier = vks::initializers::bufferMemoryBarrier();
203// bufferBarrier.srcAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
204// bufferBarrier.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
205// bufferBarrier.srcQueueFamilyIndex = ctx->queueFamilyIndices.graphics;
206// bufferBarrier.dstQueueFamilyIndex = ctx->queueFamilyIndices.compute;
207// bufferBarrier.size = VK_WHOLE_SIZE;
208//
209// std::vector<VkBufferMemoryBarrier> bufferBarriers;
210// bufferBarrier.buffer = mVertex.bufferHandle();
211// bufferBarriers.push_back(bufferBarrier);
212// vkCmdPipelineBarrier(commandBuffer,
213// VK_PIPELINE_STAGE_VERTEX_INPUT_BIT,
214// VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
215// VK_FLAGS_NONE,
216// 0, nullptr,
217// static_cast<uint32_t>(bufferBarriers.size()), bufferBarriers.data(),
218// 0, nullptr);
219// }
220// }
221//
222// void VkGraphicsPipeline::addComputeToGraphicsBarriers(VkCommandBuffer commandBuffer)
223// {
224// if (specializedComputeQueue) {
225// VkBufferMemoryBarrier bufferBarrier = vks::initializers::bufferMemoryBarrier();
226// bufferBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
227// bufferBarrier.dstAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
228// bufferBarrier.srcQueueFamilyIndex = ctx->queueFamilyIndices.compute;
229// bufferBarrier.dstQueueFamilyIndex = ctx->queueFamilyIndices.graphics;
230// bufferBarrier.size = VK_WHOLE_SIZE;
231// std::vector<VkBufferMemoryBarrier> bufferBarriers;
232// bufferBarrier.buffer = mVertex.bufferHandle();
233// bufferBarriers.push_back(bufferBarrier);
234// vkCmdPipelineBarrier(
235// commandBuffer,
236// VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
237// VK_PIPELINE_STAGE_VERTEX_INPUT_BIT,
238// VK_FLAGS_NONE,
239// 0, nullptr,
240// static_cast<uint32_t>(bufferBarriers.size()), bufferBarriers.data(),
241// 0, nullptr);
242// }
243// }
244
245 void VkGraphicsPipeline::buildCommandBuffers(VkCommandBuffer drawCmdBuffer)
246 {
247 VkDeviceSize offsets[1] = { 0 };
248 VkBuffer dataBuffer = mCubeVertex.bufferHandle();
249 VkBuffer instanceBuffer = mCubeInstanceData.bufferHandle();
250 int cubeCount = mCubeInstanceData.size();
251 // draw cube
252 if (cubeCount > 0) {
253 vkCmdBindPipeline(drawCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
254 vkCmdBindDescriptorSets(drawCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
255 vkCmdBindIndexBuffer(drawCmdBuffer, mCubeIndex.bufferHandle(), 0, VK_INDEX_TYPE_UINT32);
256 vkCmdBindVertexBuffers(drawCmdBuffer, 0, 1, &dataBuffer, offsets);
257 vkCmdBindVertexBuffers(drawCmdBuffer, 1, 1, &instanceBuffer, offsets);
258 vkCmdDrawIndexed(drawCmdBuffer, mCubeIndex.size(), cubeCount, 0, 0, 0);
259 }
260
261 // draw sphere
262 int sphereCount = mSphereInstanceData.size();
263 if (sphereCount > 0) {
264 dataBuffer = mSphereVertex.bufferHandle();
265 instanceBuffer = mSphereInstanceData.bufferHandle();
266
267 vkCmdBindPipeline(drawCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, spherePipeline);
268 vkCmdBindDescriptorSets(drawCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
269 vkCmdBindIndexBuffer(drawCmdBuffer, mSphereIndex.bufferHandle(), 0, VK_INDEX_TYPE_UINT32);
270 vkCmdBindVertexBuffers(drawCmdBuffer, 0, 1, &dataBuffer, offsets);
271 vkCmdBindVertexBuffers(drawCmdBuffer, 1, 1, &instanceBuffer, offsets);
272 vkCmdDrawIndexed(drawCmdBuffer, mSphereIndex.size(), sphereCount, 0, 0, 0);
273 }
274
275 // draw Capsule
276 int capsuleCount = mCapsuleInstanceData.size();
277 if (capsuleCount > 0) {
278 dataBuffer = mCapsuleVertex.bufferHandle();
279 instanceBuffer = mCapsuleInstanceData.bufferHandle();
280
281 vkCmdBindPipeline(drawCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, capsulePipeline);
282 vkCmdBindDescriptorSets(drawCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
283 vkCmdBindIndexBuffer(drawCmdBuffer, mCapsuleIndex.bufferHandle(), 0, VK_INDEX_TYPE_UINT32);
284 vkCmdBindVertexBuffers(drawCmdBuffer, 0, 1, &dataBuffer, offsets);
285 vkCmdBindVertexBuffers(drawCmdBuffer, 1, 1, &instanceBuffer, offsets);
286 vkCmdDrawIndexed(drawCmdBuffer, mCapsuleIndex.size(), capsuleCount, 0, 0, 0);
287 }
288 }
289
291 {
292 return getAssetPath() + "shaders/" + shaderDir + "/";
293 }
294
295 VkPipelineShaderStageCreateInfo VkGraphicsPipeline::loadShader(std::string fileName, VkShaderStageFlagBits stage)
296 {
297 VkPipelineShaderStageCreateInfo shaderStage = {};
298 shaderStage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
299 shaderStage.stage = stage;
300 shaderStage.module = vks::tools::loadShaderModule(fileName, ctx->deviceHandle());
301 shaderStage.pName = "main";
302 assert(shaderStage.module != VK_NULL_HANDLE);
303 shaderModules.push_back(shaderStage.module);
304 return shaderStage;
305 }
306
307}
assert(queueCount >=1)
#define VK_CHECK_RESULT(f)
Definition VulkanTools.h:55
VkDeviceArray< Vertex > mSphereVertex
void preparePipelines(VkRenderPass renderPass)
VkDeviceArray< uint32_t > mCapsuleIndex
VkDeviceArray< uint32_t > mSphereIndex
VkDeviceArray< Vertex > mCubeVertex
std::vector< VkShaderModule > shaderModules
VkPipelineShaderStageCreateInfo loadShader(std::string fileName, VkShaderStageFlagBits stage)
void buildCommandBuffers(VkCommandBuffer drawCmdBuffer) override
VkUniform< GraphicsUBO > mUniform
VkDeviceArray< px::Sphere > mSphereInstanceData
VkDeviceArray< px::Capsule > mCapsuleInstanceData
VkDeviceArray< px::Box > mCubeInstanceData
VkDescriptorPool descriptorPool
VkPipelineLayout pipelineLayout
VkDescriptorSetLayout descriptorSetLayout
VkDeviceArray< uint32_t > mCubeIndex
std::string getShadersPath() const
VkDeviceArray< Vertex > mCapsuleVertex
VkContext * currentContext()
Definition VkSystem.h:21
static VkSystem * instance()
Definition VkSystem.cpp:10
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
VkVertexInputAttributeDescription vertexInputAttributeDescription(uint32_t binding, uint32_t location, VkFormat format, uint32_t offset)
VkDescriptorSetLayoutBinding descriptorSetLayoutBinding(VkDescriptorType type, VkShaderStageFlags stageFlags, uint32_t binding, uint32_t descriptorCount=1)
VkPipelineColorBlendStateCreateInfo pipelineColorBlendStateCreateInfo(uint32_t attachmentCount, const VkPipelineColorBlendAttachmentState *pAttachments)
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo(const VkDescriptorSetLayoutBinding *pBindings, uint32_t bindingCount)
VkPipelineViewportStateCreateInfo pipelineViewportStateCreateInfo(uint32_t viewportCount, uint32_t scissorCount, VkPipelineViewportStateCreateFlags flags=0)
VkDescriptorPoolSize descriptorPoolSize(VkDescriptorType type, uint32_t descriptorCount)
VkPipelineRasterizationStateCreateInfo pipelineRasterizationStateCreateInfo(VkPolygonMode polygonMode, VkCullModeFlags cullMode, VkFrontFace frontFace, VkPipelineRasterizationStateCreateFlags flags=0)
VkDescriptorPoolCreateInfo descriptorPoolCreateInfo(uint32_t poolSizeCount, VkDescriptorPoolSize *pPoolSizes, uint32_t maxSets)
VkPipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo(const VkDynamicState *pDynamicStates, uint32_t dynamicStateCount, VkPipelineDynamicStateCreateFlags flags=0)
VkVertexInputBindingDescription vertexInputBindingDescription(uint32_t binding, uint32_t stride, VkVertexInputRate inputRate)
VkWriteDescriptorSet writeDescriptorSet(VkDescriptorSet dstSet, VkDescriptorType type, uint32_t binding, VkDescriptorBufferInfo *bufferInfo, uint32_t descriptorCount=1)
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo(const VkDescriptorSetLayout *pSetLayouts, uint32_t setLayoutCount=1)
VkPipelineColorBlendAttachmentState pipelineColorBlendAttachmentState(VkColorComponentFlags colorWriteMask, VkBool32 blendEnable)
VkGraphicsPipelineCreateInfo pipelineCreateInfo()
VkPipelineMultisampleStateCreateInfo pipelineMultisampleStateCreateInfo(VkSampleCountFlagBits rasterizationSamples, VkPipelineMultisampleStateCreateFlags flags=0)
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo(VkDescriptorPool descriptorPool, const VkDescriptorSetLayout *pSetLayouts, uint32_t descriptorSetCount)
VkPipelineDepthStencilStateCreateInfo pipelineDepthStencilStateCreateInfo(VkBool32 depthTestEnable, VkBool32 depthWriteEnable, VkCompareOp depthCompareOp)
VkPipelineInputAssemblyStateCreateInfo pipelineInputAssemblyStateCreateInfo(VkPrimitiveTopology topology, VkPipelineInputAssemblyStateCreateFlags flags, VkBool32 primitiveRestartEnable)
VkPipelineVertexInputStateCreateInfo pipelineVertexInputStateCreateInfo()
VkShaderModule loadShaderModule(const std::string fileName, VkDevice device)