PeriDyno 1.0.0
Loading...
Searching...
No Matches
TriangleSetRenderer.cpp
Go to the documentation of this file.
2#include "VkTransfer.h"
3#include "Node.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 vkDestroyPipelineLayout(ctx->deviceHandle(), pipelineLayout, nullptr);
24 vkDestroyDescriptorSetLayout(ctx->deviceHandle(), descriptorSetLayout, nullptr);
25 vkDestroyDescriptorPool(ctx->deviceHandle(), descriptorPool, nullptr);
26 };
27
29 {
30 std::vector<VkDescriptorPoolSize> poolSizes = {
31 vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 3),
32 vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 4),
33 vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2)
34 };
35
36 VkDescriptorPoolCreateInfo descriptorPoolInfo =
38
39 VK_CHECK_RESULT(vkCreateDescriptorPool(ctx->deviceHandle(), &descriptorPoolInfo, nullptr, &descriptorPool));
40 }
41
43 {
44 // Set layout
45 std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
46 vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0)
47 };
48 VkDescriptorSetLayoutCreateInfo descriptorLayout =
50 VK_CHECK_RESULT(vkCreateDescriptorSetLayout(ctx->deviceHandle(), &descriptorLayout, nullptr, &descriptorSetLayout));
51
52 VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo =
54 VK_CHECK_RESULT(vkCreatePipelineLayout(ctx->deviceHandle(), &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));
55
56 // Set
57 VkDescriptorSetAllocateInfo allocInfo =
59 VK_CHECK_RESULT(vkAllocateDescriptorSets(ctx->deviceHandle(), &allocInfo, &descriptorSet));
60
61 std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
62 vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &mUniform.getDescriptor())
63 };
64 vkUpdateDescriptorSets(ctx->deviceHandle(), static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
65 }
66
67 void TriangleSetRenderer::preparePipelines(VkRenderPass renderPass)
68 {
69 VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
70 vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
71
72 VkPipelineRasterizationStateCreateInfo rasterizationState =
73 vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
74
75 VkPipelineColorBlendAttachmentState blendAttachmentState =
77
78 VkPipelineColorBlendStateCreateInfo colorBlendState =
80
81 VkPipelineDepthStencilStateCreateInfo depthStencilState =
82 vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);
83
84 VkPipelineViewportStateCreateInfo viewportState =
86
87 VkPipelineMultisampleStateCreateInfo multisampleState =
89
90 std::vector<VkDynamicState> dynamicStateEnables = {
91 VK_DYNAMIC_STATE_VIEWPORT,
92 VK_DYNAMIC_STATE_SCISSOR
93 };
94 VkPipelineDynamicStateCreateInfo dynamicState =
96
97 // Rendering pipeline
98 std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
99
100 shaderStages[0] = loadShader(getShadersPath() + "graphics/cloth.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
101 shaderStages[1] = loadShader(getShadersPath() + "graphics/cloth.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
102
103 VkGraphicsPipelineCreateInfo pipelineCreateInfo = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass);
104
105 // Input attributes
106
107 // Binding description
108 std::vector<VkVertexInputBindingDescription> inputBindings = {
109 vks::initializers::vertexInputBindingDescription(0, sizeof(Vertex), VK_VERTEX_INPUT_RATE_VERTEX)
110 };
111
112 // Attribute descriptions
113 std::vector<VkVertexInputAttributeDescription> inputAttributes = {
114 vks::initializers::vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, pos)),
115 vks::initializers::vertexInputAttributeDescription(0, 1, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex, uv)),
116 vks::initializers::vertexInputAttributeDescription(0, 2, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, normal))
117 };
118
119 // Assign to vertex buffer
120 VkPipelineVertexInputStateCreateInfo inputState = vks::initializers::pipelineVertexInputStateCreateInfo();
121 inputState.vertexBindingDescriptionCount = static_cast<uint32_t>(inputBindings.size());
122 inputState.pVertexBindingDescriptions = inputBindings.data();
123 inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(inputAttributes.size());
124 inputState.pVertexAttributeDescriptions = inputAttributes.data();
125
126 pipelineCreateInfo.pVertexInputState = &inputState;
127 pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
128 pipelineCreateInfo.pRasterizationState = &rasterizationState;
129 pipelineCreateInfo.pColorBlendState = &colorBlendState;
130 pipelineCreateInfo.pMultisampleState = &multisampleState;
131 pipelineCreateInfo.pViewportState = &viewportState;
132 pipelineCreateInfo.pDepthStencilState = &depthStencilState;
133 pipelineCreateInfo.pDynamicState = &dynamicState;
134 pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
135 pipelineCreateInfo.pStages = shaderStages.data();
136 pipelineCreateInfo.renderPass = renderPass;
137 VK_CHECK_RESULT(vkCreateGraphicsPipelines(ctx->deviceHandle(), ctx->pipelineCacheHandle(), 1, &pipelineCreateInfo, nullptr, &pipeline));
138 }
139
140 void TriangleSetRenderer::addGraphicsToComputeBarriers(VkCommandBuffer commandBuffer)
141 {
143 VkBufferMemoryBarrier bufferBarrier = vks::initializers::bufferMemoryBarrier();
144 bufferBarrier.srcAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
145 bufferBarrier.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
146 bufferBarrier.srcQueueFamilyIndex = ctx->queueFamilyIndices.graphics;
147 bufferBarrier.dstQueueFamilyIndex = ctx->queueFamilyIndices.compute;
148 bufferBarrier.size = VK_WHOLE_SIZE;
149
150 std::vector<VkBufferMemoryBarrier> bufferBarriers;
151 bufferBarrier.buffer = mVertex.bufferHandle();
152 bufferBarriers.push_back(bufferBarrier);
153 vkCmdPipelineBarrier(commandBuffer,
154 VK_PIPELINE_STAGE_VERTEX_INPUT_BIT,
155 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
157 0, nullptr,
158 static_cast<uint32_t>(bufferBarriers.size()), bufferBarriers.data(),
159 0, nullptr);
160 }
161 }
162
163 void TriangleSetRenderer::addComputeToGraphicsBarriers(VkCommandBuffer commandBuffer)
164 {
166 VkBufferMemoryBarrier bufferBarrier = vks::initializers::bufferMemoryBarrier();
167 bufferBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
168 bufferBarrier.dstAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT;
169 bufferBarrier.srcQueueFamilyIndex = ctx->queueFamilyIndices.compute;
170 bufferBarrier.dstQueueFamilyIndex = ctx->queueFamilyIndices.graphics;
171 bufferBarrier.size = VK_WHOLE_SIZE;
172 std::vector<VkBufferMemoryBarrier> bufferBarriers;
173 bufferBarrier.buffer = mVertex.bufferHandle();
174 bufferBarriers.push_back(bufferBarrier);
175 vkCmdPipelineBarrier(
176 commandBuffer,
177 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
178 VK_PIPELINE_STAGE_VERTEX_INPUT_BIT,
180 0, nullptr,
181 static_cast<uint32_t>(bufferBarriers.size()), bufferBarriers.data(),
182 0, nullptr);
183 }
184 }
185
186 void TriangleSetRenderer::buildCommandBuffers(VkCommandBuffer drawCmdBuffer)
187 {
188 // Acquire storage buffers from compute queue
189 addComputeToGraphicsBarriers(drawCmdBuffer);
190
191 VkDeviceSize offsets[1] = { 0 };
192
193 // Render cloth
194 if (mVertex.size() > 0)
195 {
196 VkBuffer dataBuffer = mVertex.bufferHandle();
197
198 vkCmdBindPipeline(drawCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
199 vkCmdBindDescriptorSets(drawCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
200 vkCmdBindIndexBuffer(drawCmdBuffer, mIndex.bufferHandle(), 0, VK_INDEX_TYPE_UINT32);
201 vkCmdBindVertexBuffers(drawCmdBuffer, 0, 1, &dataBuffer, offsets);
202 vkCmdDrawIndexed(drawCmdBuffer, mIndex.size(), 1, 0, 0, 0);
203 }
204
205 // release the storage buffers to the compute queue
206 addGraphicsToComputeBarriers(drawCmdBuffer);
207 }
208
210 {
211 return getAssetPath() + "shaders/" + shaderDir + "/";
212 }
213
214 VkPipelineShaderStageCreateInfo TriangleSetRenderer::loadShader(std::string fileName, VkShaderStageFlagBits stage)
215 {
216 VkPipelineShaderStageCreateInfo shaderStage = {};
217 shaderStage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
218 shaderStage.stage = stage;
219 shaderStage.module = vks::tools::loadShaderModule(fileName, ctx->deviceHandle());
220 shaderStage.pName = "main";
221 assert(shaderStage.module != VK_NULL_HANDLE);
222 return shaderStage;
223 }
224
226 {
227 auto triSet = std::dynamic_pointer_cast<TriangleSet>(this->inTopology()->getDataPtr());
228 if (triSet == nullptr)
229 return false;
230
231 this->mIndex.resize(triSet->mIndex.size());
232 vkTransfer(this->mIndex, *triSet->mIndex.handle());
233
234 this->mVertex.resize(3 * triSet->mTriangleIndex.size(), VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
235 this->mIndex.resize(3 * triSet->mTriangleIndex.size());
236
237 program = std::make_shared<VkProgram>(BUFFER(Vertex), BUFFER(uint32_t), BUFFER(Vec3f), BUFFER(uint32_t), CONSTANT(uint32_t));
238 program->load(getAssetPath() + "shaders/glsl/graphics/SetupVertexFromPoints.comp.spv");
239
240 particleNumber.setValue(triSet->mTriangleIndex.size());
241 dim3 groupSize = vkDispatchSize(triSet->mTriangleIndex.size(), 32);
242 program->begin();
243 program->enqueue(groupSize, &this->mVertex, &this->mIndex, triSet->mPoints.handle(), triSet->mIndex.handle(), &this->particleNumber);
244 program->end();
245
246 program->update();
247
248 return true;
249 }
250
252 {
253 auto triSet = std::dynamic_pointer_cast<TriangleSet>(this->inTopology()->getDataPtr());
254 assert(triSet != nullptr);
255
256 program->update();
257 }
258}
#define BUFFER(T)
Definition VkProgram.h:96
#define CONSTANT(T)
Definition VkProgram.h:100
assert(queueCount >=1)
#define VK_CHECK_RESULT(f)
Definition VulkanTools.h:55
#define VK_FLAGS_NONE
Definition VulkanTools.h:39
void addComputeToGraphicsBarriers(VkCommandBuffer commandBuffer)
VkConstant< uint32_t > particleNumber
VkPipelineShaderStageCreateInfo loadShader(std::string fileName, VkShaderStageFlagBits stage)
VkUniform< GraphicsUBO > mUniform
void buildCommandBuffers(VkCommandBuffer drawCmdBuffer) override
void preparePipelines(VkRenderPass renderPass)
VkDeviceArray< Vertex > mVertex
VkDescriptorSetLayout descriptorSetLayout
std::vector< VkShaderModule > shaderModules
std::shared_ptr< VkProgram > program
void addGraphicsToComputeBarriers(VkCommandBuffer commandBuffer)
VkDeviceArray< uint32_t > mIndex
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
static dim3 vkDispatchSize(uint totalSize, uint blockSize)
Definition VkProgram.h:34
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
bool vkTransfer(VkHostArray< T > &dst, const VkDeviceArray< T > &src)
Definition VkTransfer.inl:7
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()
VkBufferMemoryBarrier bufferMemoryBarrier()
Initialize a buffer memory barrier with no image transfer ownership.
VkShaderModule loadShaderModule(const std::string fileName, VkDevice device)