|
|
|
@ -11,8 +11,13 @@ Simulation::Simulation() { |
|
|
|
|
Mesh sphere("models/sphere_high.ply"); |
|
|
|
|
Mesh bunny("models/bunny_high.ply"); |
|
|
|
|
|
|
|
|
|
softBodies.push_back(std::make_unique<SoftBody>(&sphere, 0.3f)); |
|
|
|
|
// softBodies.push_back(std::make_unique<SoftBody>(&bunny, 0.3f));
|
|
|
|
|
auto body = std::make_unique<SoftBody>(&sphere, 0.3f); |
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 500; i++){ |
|
|
|
|
auto copy = std::make_unique<SoftBody>(*body.get()); |
|
|
|
|
copy->applyOffset({i / 2.f, 0, 0}); |
|
|
|
|
softBodies.push_back(std::move(copy)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
vector<Vertex> vertices; |
|
|
|
|
vector<Edge> edges; |
|
|
|
@ -29,6 +34,12 @@ Simulation::Simulation() { |
|
|
|
|
triangles.insert(triangles.end(), softBody->triangles.begin(), softBody->triangles.end()); |
|
|
|
|
faces.insert(faces.end(), softBody->faces.begin(), softBody->faces.end()); |
|
|
|
|
tetrahedra.insert(tetrahedra.end(), softBody->tetrahedra.begin(), softBody->tetrahedra.end()); |
|
|
|
|
|
|
|
|
|
for (auto iter = faces.begin() + softBody->firstIndex / 3; iter != faces.end(); iter++){ |
|
|
|
|
iter->a += softBody->vertexOffset; |
|
|
|
|
iter->b += softBody->vertexOffset; |
|
|
|
|
iter->c += softBody->vertexOffset; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class SimulationBuffer : public Buffer { |
|
|
|
@ -60,16 +71,12 @@ void Simulation::recordDrawCommands() { |
|
|
|
|
vkCmdBindIndexBuffer(cmdBuffer, faceBuffer->handle, 0, VK_INDEX_TYPE_UINT32); |
|
|
|
|
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline->layout, 0, 1, &graphicsPipeline->descriptorSet, 0, nullptr); |
|
|
|
|
|
|
|
|
|
for (const auto& softBody : softBodies){ |
|
|
|
|
vkCmdDrawIndexed(cmdBuffer, softBody->faces.size() * 3, 1, softBody->firstIndex, softBody->vertexOffset, 0); |
|
|
|
|
} |
|
|
|
|
vkCmdDrawIndexed(cmdBuffer, faceBuffer->size / sizeof(Face) * 3, 1, 0, 0, 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Simulation::recordComputeCommands(VkCommandBuffer cmdBuffer) { |
|
|
|
|
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pbdPipeline->handle); |
|
|
|
|
|
|
|
|
|
SoftBody& body = *softBodies[0]; |
|
|
|
|
|
|
|
|
|
VkMemoryBarrier barrier {}; |
|
|
|
|
barrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER; |
|
|
|
|
barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; |
|
|
|
@ -97,8 +104,14 @@ void Simulation::recordComputeCommands(VkCommandBuffer cmdBuffer) { |
|
|
|
|
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, normalPipeline->handle); |
|
|
|
|
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, normalPipeline->layout, 0, 1, &normalPipeline->descriptorSet, 0, nullptr); |
|
|
|
|
|
|
|
|
|
uint32_t vertexGroupCount = vertexBuffer->size / sizeof(Vertex); |
|
|
|
|
uint32_t faceGroupCount = faceBuffer->size / sizeof(Face); |
|
|
|
|
#define BlOCK_SIZE 256 |
|
|
|
|
|
|
|
|
|
auto getGroupCount = [](uint32_t threads, uint32_t blockSize){ |
|
|
|
|
return (threads - 1) / blockSize + 1; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
uint32_t vertexGroupCount = getGroupCount(vertexBuffer->size / sizeof(Vertex), BlOCK_SIZE); |
|
|
|
|
uint32_t faceGroupCount = getGroupCount(faceBuffer->size / sizeof(Face), BlOCK_SIZE); |
|
|
|
|
|
|
|
|
|
uint32_t state = 0; |
|
|
|
|
vkCmdPushConstants(cmdBuffer, normalPipeline->layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(uint32_t), &state); |
|
|
|
|