Skip to content

Commit

Permalink
Chaos... But at least it's moving strand segments now, incorrectly co…
Browse files Browse the repository at this point in the history
…rrected though.
  • Loading branch information
edisonlee0212 committed Oct 15, 2024
1 parent 2084e47 commit ffc91fc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ barrier();

layout(push_constant) uniform PUSH_CONSTANTS {
uint constraint_size;
float time_size;
float inverse_time_size;
float time_step;
float inv_time_step;
};

#define DYNAMIC_STRANDS_PHYSICS_SET 1
Expand Down Expand Up @@ -54,7 +54,7 @@ void init_constraint(in uint rod_constraint_handle){

init_stretch_bending_twisting_constraint(
rod_constraint.stiffness_coefficient_k_segment0_index.xyz,
inverse_time_size,
inv_time_step,
rod_constraint.stretch_compliance_average_segment_length.w,
stretch_compliance,
bending_and_torsion_compliance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class DynamicStrands {
#pragma endregion
#pragma region Step
struct PhysicsParameters {
float d_t;
float time_step = 0.01f;
uint32_t sub_step = 1;
uint32_t max_iteration;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class DynamicStrandsPreStep {
};

inline static std::shared_ptr<ComputePipeline> pre_step_pipeline;
void Execute(const DynamicStrands::PhysicsParameters& physics_parameters, const DynamicStrands& target_dynamic_strands);
void Execute(const DynamicStrands::PhysicsParameters& physics_parameters,
const DynamicStrands& target_dynamic_strands);
};

class DynamicStrandsPostStep {
Expand All @@ -26,7 +27,8 @@ class DynamicStrandsPostStep {
};

inline static std::shared_ptr<ComputePipeline> post_step_pipeline;
void Execute(const DynamicStrands::PhysicsParameters& physics_parameters, const DynamicStrands& target_dynamic_strands);
void Execute(const DynamicStrands::PhysicsParameters& physics_parameters,
const DynamicStrands& target_dynamic_strands);
};
class IDynamicStrandsOperator {
public:
Expand All @@ -36,13 +38,18 @@ class IDynamicStrandsOperator {
}
virtual void Execute(const DynamicStrands::PhysicsParameters& physics_parameters,
const DynamicStrands& target_dynamic_strands) = 0;
virtual void DownloadData() {
}
};
class IDynamicStrandsConstraint {
public:
virtual void InitializeData(const DynamicStrands::InitializeParameters& initialize_parameters,
const DynamicStrands& target_dynamic_strands){};
virtual void Project(const DynamicStrands::PhysicsParameters& physics_parameters,
const DynamicStrands& target_dynamic_strands) = 0;

virtual void DownloadData() {
}
};

#pragma region Operators
Expand Down Expand Up @@ -148,27 +155,20 @@ class DsStiffRod final : public IDynamicStrandsConstraint {
int padding1 = -1;
};

struct PerSegmentData {
glm::vec3 lambda_ss;
float padding0;
};

RodProperties rod_properties;

std::vector<PerStrandData> per_strand_data_list;
std::vector<GpuRodConstraint> rod_constraints;
std::vector<PerSegmentData> per_segment_data_list;

std::vector<std::shared_ptr<Buffer>> rod_properties_buffer;

std::shared_ptr<Buffer> per_strand_data_list_buffer;
std::shared_ptr<Buffer> rod_constraints_buffer;
std::shared_ptr<Buffer> per_segment_data_list_buffer;

struct StiffRodInitConstraintConstant {
uint32_t constraint_size = 0;
float time_size;
float inverse_time_size;
float time_step;
float inv_time_step;
};

struct StiffRodUpdateConstraintConstant {
Expand All @@ -190,7 +190,7 @@ class DsStiffRod final : public IDynamicStrandsConstraint {

void Project(const DynamicStrands::PhysicsParameters& physics_parameters,
const DynamicStrands& target_dynamic_strands) override;

void DownloadData() override;
static glm::vec3 ComputeDarbouxVector(const glm::quat& q0, const glm::quat& q1, float average_segment_length);
};
#pragma endregion
Expand Down
6 changes: 6 additions & 0 deletions EvoEngine_Plugins/EcoSysLab/src/DynamicStrands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ void DynamicStrands::Upload() const {
void DynamicStrands::Download() {
device_strands_buffer->DownloadVector(strands, strands.size());
device_strand_segments_buffer->DownloadVector(strand_segments, strand_segments.size());
for (const auto& op : operators) {
op->DownloadData();
}
for (const auto& c : constraints) {
c->DownloadData();
}
}

void DynamicStrands::Clear() {
Expand Down
33 changes: 19 additions & 14 deletions EvoEngine_Plugins/EcoSysLab/src/DynamicStrandsPhysics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ void DynamicStrands::Physics(const PhysicsParameters& physics_parameters,
}
if (target_pre_step)
target_pre_step->Execute(physics_parameters, *this);
for (const auto& constraint : target_constraints) {
constraint->Project(physics_parameters, *this);
for (const auto& c : target_constraints) {
c->Project(physics_parameters, *this);
}
if (target_post_step)
target_post_step->Execute(physics_parameters, *this);
Expand Down Expand Up @@ -46,6 +46,8 @@ void DynamicStrandsPreStep::Execute(const DynamicStrands::PhysicsParameters& phy

PreStepPushConstant push_constant;
push_constant.segment_size = target_dynamic_strands.strand_segments.size();
push_constant.time_step = physics_parameters.time_step;
push_constant.inv_time_step = 1.f / push_constant.time_step;
const uint32_t task_work_group_invocations =
Platform::GetSelectedPhysicalDevice()->mesh_shader_properties_ext.maxPreferredTaskWorkGroupInvocations;

Expand Down Expand Up @@ -263,7 +265,6 @@ DsStiffRod::DsStiffRod() {
layout->PushDescriptorBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT, 0);
layout->PushDescriptorBinding(1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT, 0);
layout->PushDescriptorBinding(2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT, 0);
layout->PushDescriptorBinding(3, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_COMPUTE_BIT, 0);

layout->Initialize();
}
Expand Down Expand Up @@ -291,9 +292,7 @@ DsStiffRod::DsStiffRod() {

per_strand_data_list_buffer = std::make_shared<Buffer>(storage_buffer_create_info, buffer_vma_allocation_create_info);
rod_constraints_buffer = std::make_shared<Buffer>(storage_buffer_create_info, buffer_vma_allocation_create_info);
per_segment_data_list_buffer =
std::make_shared<Buffer>(storage_buffer_create_info, buffer_vma_allocation_create_info);


if (!init_constraint_pipeline) {
static std::shared_ptr<Shader> init_constraint_shader{};
init_constraint_shader = std::make_shared<Shader>();
Expand Down Expand Up @@ -397,7 +396,7 @@ void DsStiffRod::InitializeData(const DynamicStrands::InitializeParameters& init
const auto& l1 = segment1.length;
const float segment_length = (l0 + l1) * .5f;
rod_constraint.rest_darboux_vector = ComputeDarbouxVector(q0, q1, segment_length);

rod_constraint.average_segment_length = segment_length;
const auto rotation0_transpose = glm::transpose(glm::mat3_cast(q0));
const auto rotation1_transpose = glm::transpose(glm::mat3_cast(q1));
const auto& p0 = segment0.x0;
Expand Down Expand Up @@ -426,11 +425,9 @@ void DsStiffRod::InitializeData(const DynamicStrands::InitializeParameters& init
rod_constraints.back().next_handle = -1;
stiff_rod_per_strand_data.end_rod_constraint_handle = static_cast<int>(rod_constraints.size()) - 1;
}
per_segment_data_list.resize(target_dynamic_strands.strand_segments.size());

per_strand_data_list_buffer->UploadVector(per_strand_data_list);
rod_constraints_buffer->UploadVector(rod_constraints);
per_segment_data_list_buffer->UploadVector(per_segment_data_list);
}

void DsStiffRod::Project(const DynamicStrands::PhysicsParameters& physics_parameters,
Expand All @@ -442,19 +439,22 @@ void DsStiffRod::Project(const DynamicStrands::PhysicsParameters& physics_parame
0, rod_properties_buffer[current_frame_index]);
strands_physics_descriptor_sets[current_frame_index]->UpdateBufferDescriptorBinding(1, per_strand_data_list_buffer);
strands_physics_descriptor_sets[current_frame_index]->UpdateBufferDescriptorBinding(2, rod_constraints_buffer);
strands_physics_descriptor_sets[current_frame_index]->UpdateBufferDescriptorBinding(3, per_segment_data_list_buffer);

int num_sub_step = 1;
int num_iteration = 1;

StiffRodInitConstraintConstant init_push_constant;
init_push_constant.constraint_size = rod_constraints.size();
init_push_constant.time_step = physics_parameters.time_step / num_sub_step;
init_push_constant.inv_time_step = 1.f / init_push_constant.time_step;

StiffRodUpdateConstraintConstant update_constraint_constant;
update_constraint_constant.constraint_size = rod_constraints.size();

StiffRodProjectConstraintConstant project_constraint_constant;
project_constraint_constant.strand_size = per_strand_data_list.size();

int num_sub_step = 5;
int num_iteration = 5;


const uint32_t task_work_group_invocations =
Platform::GetSelectedPhysicalDevice()->mesh_shader_properties_ext.maxPreferredTaskWorkGroupInvocations;
Expand Down Expand Up @@ -485,7 +485,7 @@ void DsStiffRod::Project(const DynamicStrands::PhysicsParameters& physics_parame
vkCmdDispatch(vk_command_buffer,
Platform::DivUp(update_constraint_constant.constraint_size, task_work_group_invocations), 1, 1);
Platform::EverythingBarrier(vk_command_buffer);

project_constraint_pipeline->Bind(vk_command_buffer);
project_constraint_pipeline->BindDescriptorSet(
vk_command_buffer, 0,
Expand All @@ -502,6 +502,11 @@ void DsStiffRod::Project(const DynamicStrands::PhysicsParameters& physics_parame
});
}

void DsStiffRod::DownloadData() {
rod_constraints_buffer->DownloadVector(rod_constraints, rod_constraints.size());
per_strand_data_list_buffer->DownloadVector(per_strand_data_list, per_strand_data_list.size());
}

glm::vec3 DsStiffRod::ComputeDarbouxVector(const glm::quat& q0, const glm::quat& q1,
const float average_segment_length) {
const auto relative_rotation = glm::conjugate(q0) * q1;
Expand Down

0 comments on commit ffc91fc

Please sign in to comment.