Skip to content

Commit

Permalink
Checkpoint. PositionUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
edisonlee0212 committed Oct 11, 2024
1 parent 3cedf1a commit ae2e680
Show file tree
Hide file tree
Showing 12 changed files with 703 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#extension GL_EXT_shader_explicit_arithmetic_types_int8 : require
#extension GL_KHR_shader_subgroup_basic : require
#extension GL_KHR_shader_subgroup_ballot : require
#extension GL_KHR_shader_subgroup_vote : require
#extension GL_ARB_shading_language_include : enable

#define DYNAMIC_STRANDS_SET 0
#include "DynamicStrands.glsl"

#define DYNAMIC_STRANDS_PHYSICS_SET 1
#include "DynamicStrandsPhysics.glsl"

const uint WORKGROUP_SIZE = EXT_TASK_SUBGROUP_COUNT * EXT_TASK_SUBGROUP_SIZE;

const uint ITERATIONS_PER_TASK = ((EXT_INVOCATIONS_PER_TASK + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE);

uint baseID = gl_WorkGroupID.x * EXT_INVOCATIONS_PER_TASK;
uint laneID = gl_LocalInvocationID.x;

layout(local_size_x = WORKGROUP_SIZE, local_size_y = 1, local_size_z = 1) in;

#define BARRIER() \
memoryBarrierShared(); \
barrier();

struct ExternalForce{
vec4 index_force;
};

layout(std430, set = 2, binding = 0) readonly buffer EXTERNAL_FORCE_BLOCK {
ExternalForce external_forces[];
};

layout(push_constant) uniform PUSH_CONSTANTS {
uint command_size;
float delta_time;
};

void main(){
for (uint i = 0; i < ITERATIONS_PER_TASK; i++)
{
uint local_index = laneID + i * WORKGROUP_SIZE;
uint global_index = baseID + local_index;

if(global_index >= command_size) break;

//strand_segments[global_index].end_position_thickness.y = 0.0;
ExternalForce external_force = external_forces[global_index];
uint strand_particle_index = floatBitsToUint(external_force.index_force.x);
vec3 force = external_force.index_force.yzw;

StrandSegmentParticlePhysics particle_physics = strand_segment_particle_physics_list[strand_particle_index];
float inv_mass = particle_physics.last_position_inv_mass.w;
vec3 acceleration = force * inv_mass;
vec4 position_thickness = strand_segment_particles[strand_particle_index].position_thickness;

vec3 velocity = (position_thickness.xyz - particle_physics.last_position_inv_mass.xyz) + acceleration * delta_time * .5;
position_thickness.xyz = position_thickness.xyz + velocity * delta_time;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#extension GL_EXT_shader_explicit_arithmetic_types_int8 : require
#extension GL_KHR_shader_subgroup_basic : require
#extension GL_KHR_shader_subgroup_ballot : require
#extension GL_KHR_shader_subgroup_vote : require
#extension GL_ARB_shading_language_include : enable

#define DYNAMIC_STRANDS_SET 0
#include "DynamicStrands.glsl"

#define DYNAMIC_STRANDS_PHYSICS_SET 1
#include "DynamicStrandsPhysics.glsl"

const uint WORKGROUP_SIZE = EXT_TASK_SUBGROUP_COUNT * EXT_TASK_SUBGROUP_SIZE;

const uint ITERATIONS_PER_TASK = ((EXT_INVOCATIONS_PER_TASK + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE);

uint baseID = gl_WorkGroupID.x * EXT_INVOCATIONS_PER_TASK;
uint laneID = gl_LocalInvocationID.x;

layout(local_size_x = WORKGROUP_SIZE, local_size_y = 1, local_size_z = 1) in;

#define BARRIER() \
memoryBarrierShared(); \
barrier();

struct PositionUpdate{
vec4 index_new_position;
};

layout(std430, set = 2, binding = 0) readonly buffer POSITION_UPDATE_BLOCK {
PositionUpdate position_updates[];
};

layout(push_constant) uniform PUSH_CONSTANTS {
uint command_size;
};

void main(){
for (uint i = 0; i < ITERATIONS_PER_TASK; i++)
{
uint local_index = laneID + i * WORKGROUP_SIZE;
uint global_index = baseID + local_index;

if(global_index >= command_size) break;

PositionUpdate position_update = position_updates[global_index];
uint strand_particle_index = floatBitsToUint(position_update.index_new_position.x);
vec3 new_position = position_update.index_new_position.yzw;

strand_segment_particles[strand_particle_index].position_thickness.xyz = new_position;
strand_segment_particle_physics_list[strand_particle_index].last_position_inv_mass.xyz = new_position;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#define DYNAMIC_STRANDS_SET 0
#include "DynamicStrands.glsl"

#define DYNAMIC_STRANDS_PHYSICS_SET 1
#include "DynamicStrandsPhysics.glsl"

const uint WORKGROUP_SIZE = EXT_TASK_SUBGROUP_COUNT * EXT_TASK_SUBGROUP_SIZE;

const uint ITERATIONS_PER_TASK = ((EXT_INVOCATIONS_PER_TASK + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE);
Expand All @@ -21,11 +24,11 @@ memoryBarrierShared(); \
barrier();

struct PhysicsCommand{
int strand_segment_handle;
vec3 position_offset;
int strand_segment_handle;
vec4 offset_type;
};

//#define PHYSICS_COMMANDS_SET 0
//#define PHYSICS_COMMANDS_SET 2
//layout(std430, set = PHYSICS_COMMANDS_SET, binding = 0) readonly buffer PHYSICS_COMMAND_BLOCK {
// PhysicsCommand physics_commands[];
//};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,60 @@ mat4 mat4_cast(in vec4 q){
return ret_val;
}

void main(){
vec4 quat_cast(in mat3 m){
float fourXSquaredMinus1 = m[0][0] - m[1][1] - m[2][2];
float fourYSquaredMinus1 = m[1][1] - m[0][0] - m[2][2];
float fourZSquaredMinus1 = m[2][2] - m[0][0] - m[1][1];
float fourWSquaredMinus1 = m[0][0] + m[1][1] + m[2][2];

int biggestIndex = 0;
float fourBiggestSquaredMinus1 = fourWSquaredMinus1;
if(fourXSquaredMinus1 > fourBiggestSquaredMinus1)
{
fourBiggestSquaredMinus1 = fourXSquaredMinus1;
biggestIndex = 1;
}
if(fourYSquaredMinus1 > fourBiggestSquaredMinus1)
{
fourBiggestSquaredMinus1 = fourYSquaredMinus1;
biggestIndex = 2;
}
if(fourZSquaredMinus1 > fourBiggestSquaredMinus1)
{
fourBiggestSquaredMinus1 = fourZSquaredMinus1;
biggestIndex = 3;
}

float biggestVal = sqrt(fourBiggestSquaredMinus1 + 1.0) * 0.5;
float mult = 0.25 / biggestVal;

switch(biggestIndex)
{
case 0:
return vec4((m[1][2] - m[2][1]) * mult, (m[2][0] - m[0][2]) * mult, (m[0][1] - m[1][0]) * mult, biggestVal);
case 1:
return vec4(biggestVal, (m[0][1] + m[1][0]) * mult, (m[2][0] + m[0][2]) * mult, (m[1][2] - m[2][1]) * mult);
case 2:
return vec4((m[0][1] + m[1][0]) * mult, biggestVal, (m[1][2] + m[2][1]) * mult, (m[2][0] - m[0][2]) * mult);
case 3:
return vec4((m[2][0] + m[0][2]) * mult, (m[1][2] + m[2][1]) * mult, biggestVal, (m[0][1] - m[1][0]) * mult);
default:
return vec4(0, 0, 0, 1);
}
}

vec4 quatLookAt(vec3 direction, vec3 up){
mat3 Result;

Result[2] = -direction;
vec3 Right = cross(up, Result[2]);
Result[0] = Right * inversesqrt(max(0.00001, dot(Right, Right)));
Result[1] = cross(Result[2], Result[0]);

return quat_cast(Result);
}

void main(){
SetMeshOutputsEXT(SEGMENT_VERTICES_SIZE, SEGMENT_TRIANGLE_SIZE);

StrandSegment strand_segment = strand_segments[strand_segment_id];
Expand All @@ -134,8 +186,11 @@ void main(){

vec3 center_position = (start_position + end_position) * 0.5;
float center_thickness = (start_thickness + end_thickness) * 0.5;
vec4 rotation = strand_segment.rotation;
mat4 instance_matrix = translate(end_position) * mat4_cast(rotation) * scale(vec3(center_thickness, center_thickness, distance(start_position, end_position)));

vec3 direction = normalize(end_position - start_position);
vec4 rotation = quatLookAt(direction, vec3(direction.y, direction.z, direction.x));

mat4 instance_matrix = translate(center_position) * mat4_cast(rotation) * scale(vec3(center_thickness, center_thickness, distance(start_position, end_position)));

mat4 transform = EE_CAMERAS[camera_index].projection_view * instance_matrix;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

struct StrandSegmentPhysics {
vec4 last_rotation;
vec4 angular_velocity_radius;
vec4 inv_inertia_length;
};

struct StrandSegmentParticlePhysics {
vec4 last_position_inv_mass;
vec4 delta_position_n_updates;
};

layout(std430, set = DYNAMIC_STRANDS_PHYSICS_SET, binding = 0) buffer STRAND_SEGMENTS_PHYSICS_BLOCK {
StrandSegmentPhysics strand_segment_physics_list[];
};

layout(std430, set = DYNAMIC_STRANDS_PHYSICS_SET, binding = 1) buffer STRANDS_PHYSICS_BLOCK {
StrandSegmentParticlePhysics strand_segment_particle_physics_list[];
};
13 changes: 7 additions & 6 deletions EvoEngine_Plugins/EcoSysLab/include/StrandModel/BrokenBranch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ class BrokenBranch : public IPrivateComponent {

bool enable_simulation = true;

PrivateComponentRef tree_ref;
StrandModelStrandGroup strand_group;
StrandModelStrandGroup subdivided_strand_group;
PrivateComponentRef tree_ref{};
StrandModelStrandGroup strand_group{};
StrandModelStrandGroup subdivided_strand_group{};

DynamicStrands dynamic_strands;
void UploadStrands();
DynamicStrands::InitializeParameters initialize_parameters{};
DynamicStrands::StepParameters step_parameters{};
DynamicStrands dynamic_strands{};
void UpdateDynamicStrands();
void Serialize(YAML::Emitter& out) const override;
void Deserialize(const YAML::Node& in) override;
bool OnInspect(const std::shared_ptr<EditorLayer>& editor_layer) override;
void Update() override;
void LateUpdate() override;
void OnCreate() override;
void OnDestroy() override;
Expand Down
Loading

0 comments on commit ae2e680

Please sign in to comment.