Skip to content

Commit

Permalink
Checkpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
edisonlee0212 committed Oct 11, 2024
1 parent 2da68e8 commit 3cedf1a
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BrokenBranch : public IPrivateComponent {
void OnDestroy() override;
void CollectAssetRef(std::vector<AssetRef>& list) override;
void ExperimentSetup();
static void Subdivide(float segment_length, const StrandModelStrandGroup& src, StrandModelStrandGroup& dst);
void Subdivide(float segment_length, const StrandModelStrandGroup& src);
void InitializeStrandParticles(const StrandModelStrandGroup& strand_group) const;
void ClearStrandParticles() const;

Expand Down
23 changes: 12 additions & 11 deletions EvoEngine_Plugins/EcoSysLab/include/StrandModel/DynamicStrands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ using namespace evo_engine;
namespace eco_sys_lab_plugin {

class DynamicStrands {
void Upload() const;
bool current_left = true;
uint32_t current_version = 0;
uint32_t device_buffer_version[2];
std::shared_ptr<Buffer> device_ref_strand_segments_buffer[2];
std::shared_ptr<Buffer> device_ref_strands_buffer[2];
std::shared_ptr<Buffer> device_ref_strand_segment_particles_buffer[2];

std::shared_ptr<Buffer> device_ref_strand_segments_buffer;
std::shared_ptr<Buffer> device_ref_strands_buffer;
std::shared_ptr<Buffer> device_ref_strand_segment_particles_buffer;

std::shared_ptr<Buffer> device_strand_segments_buffer[2];
std::shared_ptr<Buffer> device_strands_buffer[2];
std::shared_ptr<Buffer> device_strand_segment_particles_buffer[2];
std::shared_ptr<Buffer> device_strand_segments_buffer;
std::shared_ptr<Buffer> device_strands_buffer;
std::shared_ptr<Buffer> device_strand_segment_particles_buffer;

public:
struct InitializeParameters {
Expand All @@ -26,6 +23,8 @@ class DynamicStrands {
struct StepParameters {
bool physics = true;
struct PhysicsParameters {
float d_t;
uint32_t max_iteration;
} physics_parameters{};

bool render = true;
Expand Down Expand Up @@ -60,8 +59,10 @@ class DynamicStrands {
std::vector<GpuStrand> ref_strands;
inline static std::shared_ptr<DescriptorSetLayout> strands_layout{};
DynamicStrands();
void UpdateData(const InitializeParameters& initialize_parameters, const std::vector<Strand>& target_strands,
void InitializeData(const InitializeParameters& initialize_parameters, const std::vector<Strand>& target_strands,
const std::vector<StrandSegment>& target_strand_segments);
void Upload() const;
void Download();
void Step(const StepParameters& target_step_parameters);
void Step();
void Clear();
Expand Down
24 changes: 13 additions & 11 deletions EvoEngine_Plugins/EcoSysLab/src/BrokenBranch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void BrokenBranch::UploadStrands() {
const auto owner = GetOwner();
const auto scene = GetScene();
initialize_parameters.root_transform = scene->GetDataComponent<GlobalTransform>(owner);
dynamic_strands.UpdateData(initialize_parameters, subdivided_strand_group.PeekStrands(),
dynamic_strands.InitializeData(initialize_parameters, subdivided_strand_group.PeekStrands(),
subdivided_strand_group.PeekStrandSegments());
}

Expand All @@ -32,16 +32,13 @@ bool BrokenBranch::OnInspect(const std::shared_ptr<EditorLayer>& editor_layer) {
}
strand_group = tree->strand_model.strand_model_skeleton.data.strand_group;
if (auto_subdivide) {
Subdivide(segment_length, strand_group, subdivided_strand_group);
dynamic_strands.UpdateData({}, subdivided_strand_group.PeekStrands(),
subdivided_strand_group.PeekStrandSegments());
Subdivide(segment_length, strand_group);
}
tree_ref.Clear();
}
}
if (ImGui::Button("Subdivide strands")) {
Subdivide(segment_length, strand_group, subdivided_strand_group);
dynamic_strands.UpdateData({}, subdivided_strand_group.PeekStrands(), subdivided_strand_group.PeekStrandSegments());
Subdivide(segment_length, strand_group);
}
if (ImGui::Button("Experiment")) {
ExperimentSetup();
Expand Down Expand Up @@ -94,7 +91,7 @@ bool BrokenBranch::OnInspect(const std::shared_ptr<EditorLayer>& editor_layer) {
temp_strand_group.RemoveStrand(new_strand_handle);
}
}
Subdivide(segment_length, temp_strand_group, subdivided_strand_group);
Subdivide(segment_length, temp_strand_group);

}
if (ImGui::Button("Build particles for original")) {
Expand Down Expand Up @@ -153,10 +150,15 @@ void BrokenBranch::ExperimentSetup() {
strand_group.CalculateRotations();
}

void BrokenBranch::Subdivide(const float segment_length, const StrandModelStrandGroup& src,
StrandModelStrandGroup& dst) {
src.UniformlySubdivide(dst, segment_length, segment_length * .01f);
dst.RandomAssignColor();
void BrokenBranch::Subdivide(const float segment_length, const StrandModelStrandGroup& src) {
src.UniformlySubdivide(subdivided_strand_group, segment_length, segment_length * .01f);
subdivided_strand_group.RandomAssignColor();
DynamicStrands::InitializeParameters initialize_parameters{};
const auto scene = GetScene();
const auto owner = GetOwner();
initialize_parameters.root_transform = scene->GetDataComponent<GlobalTransform>(owner);
dynamic_strands.InitializeData(initialize_parameters, subdivided_strand_group.PeekStrands(),
subdivided_strand_group.PeekStrandSegments());
}

void BrokenBranch::InitializeStrandParticles(const StrandModelStrandGroup& target_strand_group) const {
Expand Down
96 changes: 49 additions & 47 deletions EvoEngine_Plugins/EcoSysLab/src/DynamicStrands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,24 @@
using namespace eco_sys_lab_plugin;

DynamicStrands::DynamicStrands() {
const auto max_frame_count = Platform::GetMaxFramesInFlight();
assert(max_frame_count == 2);
VkBufferCreateInfo buffer_create_info{};
buffer_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
buffer_create_info.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
buffer_create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
buffer_create_info.size = 1;
VmaAllocationCreateInfo buffer_vma_allocation_create_info{};
buffer_vma_allocation_create_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
current_version = 0;
for (uint32_t i = 0; i < max_frame_count; i++) {
device_ref_strand_segments_buffer[i] =
std::make_shared<Buffer>(buffer_create_info, buffer_vma_allocation_create_info);
device_ref_strands_buffer[i] = std::make_shared<Buffer>(buffer_create_info, buffer_vma_allocation_create_info);
device_ref_strand_segment_particles_buffer[i] =
std::make_shared<Buffer>(buffer_create_info, buffer_vma_allocation_create_info);

device_strand_segments_buffer[i] = std::make_shared<Buffer>(buffer_create_info, buffer_vma_allocation_create_info);
device_strands_buffer[i] = std::make_shared<Buffer>(buffer_create_info, buffer_vma_allocation_create_info);
device_strand_segment_particles_buffer[i] =
std::make_shared<Buffer>(buffer_create_info, buffer_vma_allocation_create_info);

device_buffer_version[i] = current_version;
}

device_ref_strand_segments_buffer = std::make_shared<Buffer>(buffer_create_info, buffer_vma_allocation_create_info);
device_ref_strands_buffer = std::make_shared<Buffer>(buffer_create_info, buffer_vma_allocation_create_info);
device_ref_strand_segment_particles_buffer =
std::make_shared<Buffer>(buffer_create_info, buffer_vma_allocation_create_info);

device_strand_segments_buffer = std::make_shared<Buffer>(buffer_create_info, buffer_vma_allocation_create_info);
device_strands_buffer = std::make_shared<Buffer>(buffer_create_info, buffer_vma_allocation_create_info);
device_strand_segment_particles_buffer =
std::make_shared<Buffer>(buffer_create_info, buffer_vma_allocation_create_info);

if (!strands_layout) {
strands_layout = std::make_shared<DescriptorSetLayout>();
strands_layout->PushDescriptorBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_SHADER_STAGE_ALL, 0);
Expand All @@ -45,14 +39,6 @@ void DynamicStrands::Step() {
}

void DynamicStrands::Step(const StepParameters& target_step_parameters) {
current_left = !current_left;

const auto current_frame_index = current_left ? 0 : 1;
if (device_buffer_version[current_frame_index] != current_version) {
Upload();
device_buffer_version[current_frame_index] = current_version;
}

if (target_step_parameters.physics) {
Physics(target_step_parameters.physics_parameters);
}
Expand All @@ -62,7 +48,7 @@ void DynamicStrands::Step(const StepParameters& target_step_parameters) {
}
}

void DynamicStrands::UpdateData(const InitializeParameters& initialize_parameters,
void DynamicStrands::InitializeData(const InitializeParameters& initialize_parameters,
const std::vector<Strand>& target_strands,
const std::vector<StrandSegment>& target_strand_segments) {
Clear();
Expand All @@ -88,9 +74,18 @@ void DynamicStrands::UpdateData(const InitializeParameters& initialize_parameter
particle.color = target_strand_segments[i].end_color;
particle_index++;
}
ref_strands[strand_index].first_strand_segment_handle = handles.front();
ref_strands[strand_index].last_strand_segment_handle = handles.back();
if (!handles.empty()) {
ref_strands[strand_index].first_strand_segment_handle = handles.front();
ref_strands[strand_index].last_strand_segment_handle = handles.back();
}else {
ref_strands[strand_index].first_strand_segment_handle = -1;
ref_strands[strand_index].last_strand_segment_handle = -1;
}
}
Jobs::RunParallelFor(ref_strand_segment_particles.size(), [&](const size_t i) {
auto& particle = ref_strand_segment_particles[i];
particle.position = initialize_parameters.root_transform.TransformPoint(particle.position);
});
Jobs::RunParallelFor(target_strand_segments.size(), [&](const size_t i) {
ref_strand_segments[i].prev_handle = target_strand_segments[i].GetPrevHandle();
ref_strand_segments[i].next_handle = target_strand_segments[i].GetNextHandle();
Expand All @@ -99,7 +94,7 @@ void DynamicStrands::UpdateData(const InitializeParameters& initialize_parameter
ref_strand_segments[i].rotation =
initialize_parameters.root_transform.GetRotation() * target_strand_segments[i].rotation;
});
current_version++;
Upload();
}

/*
Expand Down Expand Up @@ -139,14 +134,24 @@ void DynamicStrands::Simulate(const SimulateParameters& simulate_parameters) {
}*/

void DynamicStrands::Upload() const {
const auto current_frame_index = current_left ? 0 : 1;
device_ref_strand_segments_buffer[current_frame_index]->UploadVector(ref_strand_segments);
device_ref_strands_buffer[current_frame_index]->UploadVector(ref_strands);
device_ref_strand_segment_particles_buffer[current_frame_index]->UploadVector(ref_strand_segment_particles);

device_strand_segments_buffer[current_frame_index]->UploadVector(ref_strand_segments);
device_strands_buffer[current_frame_index]->UploadVector(ref_strands);
device_strand_segment_particles_buffer[current_frame_index]->UploadVector(ref_strand_segment_particles);
device_ref_strand_segments_buffer->UploadVector(ref_strand_segments);
device_ref_strands_buffer->UploadVector(ref_strands);
device_ref_strand_segment_particles_buffer->UploadVector(ref_strand_segment_particles);

device_strand_segments_buffer->UploadVector(ref_strand_segments);
device_strands_buffer->UploadVector(ref_strands);
device_strand_segment_particles_buffer->UploadVector(ref_strand_segment_particles);
}

void DynamicStrands::Download() {
device_ref_strand_segments_buffer->DownloadVector(ref_strand_segments, ref_strand_segments.size());
device_ref_strands_buffer->DownloadVector(ref_strands, ref_strands.size());
device_ref_strand_segment_particles_buffer->DownloadVector(ref_strand_segment_particles,
ref_strand_segment_particles.size());

device_strand_segments_buffer->DownloadVector(ref_strand_segments, ref_strand_segments.size());
device_strands_buffer->DownloadVector(ref_strands, ref_strands.size());
device_strand_segment_particles_buffer->DownloadVector(ref_strand_segment_particles, ref_strand_segment_particles.size());
}

void DynamicStrands::Clear() {
Expand All @@ -156,14 +161,11 @@ void DynamicStrands::Clear() {
}

void DynamicStrands::BindStrandsDescriptorSet(const std::shared_ptr<DescriptorSet>& target_descriptor_set) const {
const auto current_frame_index = current_left ? 0 : 1;
target_descriptor_set->UpdateBufferDescriptorBinding(0, device_ref_strand_segments_buffer[current_frame_index], 0);
target_descriptor_set->UpdateBufferDescriptorBinding(1, device_ref_strands_buffer[current_frame_index], 0);
target_descriptor_set->UpdateBufferDescriptorBinding(
2, device_ref_strand_segment_particles_buffer[current_frame_index], 0);

target_descriptor_set->UpdateBufferDescriptorBinding(3, device_strand_segments_buffer[current_frame_index], 0);
target_descriptor_set->UpdateBufferDescriptorBinding(4, device_strands_buffer[current_frame_index], 0);
target_descriptor_set->UpdateBufferDescriptorBinding(5, device_strand_segment_particles_buffer[current_frame_index],
0);
target_descriptor_set->UpdateBufferDescriptorBinding(0, device_ref_strand_segments_buffer, 0);
target_descriptor_set->UpdateBufferDescriptorBinding(1, device_ref_strands_buffer, 0);
target_descriptor_set->UpdateBufferDescriptorBinding(2, device_ref_strand_segment_particles_buffer, 0);

target_descriptor_set->UpdateBufferDescriptorBinding(3, device_strand_segments_buffer, 0);
target_descriptor_set->UpdateBufferDescriptorBinding(4, device_strands_buffer, 0);
target_descriptor_set->UpdateBufferDescriptorBinding(5, device_strand_segment_particles_buffer, 0);
}
2 changes: 1 addition & 1 deletion EvoEngine_Plugins/EcoSysLab/src/DynamicStrandsPhysics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using namespace eco_sys_lab_plugin;

void DynamicStrands::Physics(const StepParameters::PhysicsParameters& physics_parameters) const {
const auto current_frame_index = current_left ? 0 : 1;
const auto current_frame_index = Platform::GetCurrentFrameIndex();
static std::shared_ptr<ComputePipeline> precompute_pipeline{};
static std::vector<std::shared_ptr<DescriptorSet>> strands_descriptor_sets{};

Expand Down
2 changes: 1 addition & 1 deletion EvoEngine_Plugins/EcoSysLab/src/DynamicStrandsRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void DynamicStrands::Render(const StepParameters::RenderParameters& render_param
EVOENGINE_LOG("Failed to render! RenderLayer not present!")
return;
}
const auto current_frame_index = current_left ? 0 : 1;
const auto current_frame_index = Platform::GetCurrentFrameIndex();

static std::shared_ptr<GraphicsPipeline> render_pipeline{};
static std::vector<std::shared_ptr<DescriptorSet>> strands_descriptor_sets{};
Expand Down
3 changes: 0 additions & 3 deletions EvoEngine_SDK/include/Rendering/Platform/Platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ class Platform final {
void RecreateSwapChain();

void OnDestroy();
void SwapChainSwapImage();
void SubmitPresent();
void WaitForCommandsComplete() const;
void Submit();

void ResetCommandBuffers();
Expand Down Expand Up @@ -217,7 +215,6 @@ class Platform final {
inline static uint32_t max_spot_light_size = 16;
};


class Constants {
public:
inline static bool support_mesh_shader = true;
Expand Down
Loading

0 comments on commit 3cedf1a

Please sign in to comment.