Skip to content

Commit

Permalink
perf: add dirty flag to transform comp (ECS)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Feb 7, 2025
1 parent e093800 commit baadac6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
25 changes: 19 additions & 6 deletions extensions/pl_ecs_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,12 @@ pl_ecs_add_component(plComponentLibrary* ptLibrary, plComponentType tType, plEnt
if(bAddSlot)
pl_sb_add(sbComponents);
ptManager->pComponents = sbComponents;
sbComponents[uComponentIndex] = (plTransformComponent){.tWorld = pl_identity_mat4(), .tScale = {1.0f, 1.0f, 1.0f}, .tRotation = {0.0f, 0.0f, 0.0f, 1.0f}};
sbComponents[uComponentIndex] = (plTransformComponent){
.tWorld = pl_identity_mat4(),
.tScale = {1.0f, 1.0f, 1.0f},
.tRotation = {0.0f, 0.0f, 0.0f, 1.0f},
.tFlags = PL_TRANSFORM_FLAGS_DIRTY
};
return &sbComponents[uComponentIndex];
}

Expand Down Expand Up @@ -917,9 +922,6 @@ pl_ecs_create_transform(plComponentLibrary* ptLibrary, const char* pcName, plTra
plEntity tNewEntity = pl_ecs_create_tag(ptLibrary, pcName);

plTransformComponent* ptTransform = pl_ecs_add_component(ptLibrary, PL_COMPONENT_TYPE_TRANSFORM, tNewEntity);
ptTransform->tScale = (plVec3){1.0f, 1.0f, 1.0f};
ptTransform->tRotation = (plVec4){0.0f, 0.0f, 0.0f, 1.0f};
ptTransform->tWorld = pl_identity_mat4();

if(pptCompOut)
*pptCompOut = ptTransform;
Expand Down Expand Up @@ -1180,7 +1182,14 @@ pl_run_transform_update_system(plComponentLibrary* ptLibrary)
for(uint32_t i = 0; i < uComponentCount; i++)
{
plTransformComponent* ptTransform = &sbtComponents[i];
ptTransform->tWorld = pl_rotation_translation_scale(ptTransform->tRotation, ptTransform->tTranslation, ptTransform->tScale);
if(ptTransform->tFlags & PL_TRANSFORM_FLAGS_DIRTY)
{
ptTransform->tWorld = pl_rotation_translation_scale(ptTransform->tRotation, ptTransform->tTranslation, ptTransform->tScale);

plHierarchyComponent* ptHierarchyComponent = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_HIERARCHY, ptLibrary->tTransformComponentManager.sbtEntities[i]);
if(ptHierarchyComponent == NULL) // let object update system have a chance to handle flag
ptTransform->tFlags &= ~PL_TRANSFORM_FLAGS_DIRTY;
}
}

pl_end_cpu_sample(gptProfile, 0);
Expand All @@ -1198,8 +1207,11 @@ pl_run_hierarchy_update_system(plComponentLibrary* ptLibrary)
plHierarchyComponent* ptHierarchyComponent = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_HIERARCHY, tChildEntity);
plTransformComponent* ptParentTransform = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptHierarchyComponent->tParent);
plTransformComponent* ptChildTransform = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_TRANSFORM, tChildEntity);
if(ptParentTransform && ptChildTransform)
if(ptParentTransform && ptChildTransform->tFlags & PL_TRANSFORM_FLAGS_DIRTY)
{
ptChildTransform->tWorld = pl_mul_mat4(&ptParentTransform->tWorld, &ptChildTransform->tWorld);
ptChildTransform->tFlags &= ~PL_TRANSFORM_FLAGS_DIRTY;
}
}

pl_end_cpu_sample(gptProfile, 0);
Expand Down Expand Up @@ -1332,6 +1344,7 @@ pl_run_animation_update_system(plComponentLibrary* ptLibrary, float fDeltaTime)
const plAnimationSampler* ptSampler = &ptAnimationComponent->sbtSamplers[ptChannel->uSamplerIndex];
const plAnimationDataComponent* ptData = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_ANIMATION_DATA, ptSampler->tData);
plTransformComponent* ptTransform = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptChannel->tTarget);
ptTransform->tFlags |= PL_TRANSFORM_FLAGS_DIRTY;

// wrap t around, so the animation loops.
// make sure that t is never earlier than the first keyframe and never later then the last keyframe.
Expand Down
16 changes: 12 additions & 4 deletions extensions/pl_ecs_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ typedef int plLightFlags;
typedef int plLightType;
typedef int plHumanoidBone;
typedef int plEnvironmentProbeFlags;
typedef int plTransformFlags;

typedef union _plEntity
{
Expand Down Expand Up @@ -392,6 +393,12 @@ enum _plEnvironmentProbeFlags
PL_ENVIRONMENT_PROBE_FLAGS_PARALLAX_CORRECTION_BOX = 1 << 3,
};

enum _plTransformFlags
{
PL_TRANSFORM_FLAGS_NONE = 0,
PL_TRANSFORM_FLAGS_DIRTY = 1 << 0,
};

//-----------------------------------------------------------------------------
// [SECTION] structs
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -532,10 +539,11 @@ typedef struct _plLayerComponent

typedef struct _plTransformComponent
{
plVec3 tScale;
plVec4 tRotation;
plVec3 tTranslation;
plMat4 tWorld;
plVec3 tScale;
plVec4 tRotation;
plVec3 tTranslation;
plMat4 tWorld;
plTransformFlags tFlags;
} plTransformComponent;

typedef struct _plMaterialComponent
Expand Down
8 changes: 4 additions & 4 deletions sandbox/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plEditorData* ptEditorData)
ptLight->uCascadeCount = 4;
ptLight->fIntensity = 1.0f;
ptLight->uShadowResolution = 1024;
ptLight->afCascadeSplits[0] = 5.0f;
ptLight->afCascadeSplits[1] = 10.0f;
ptLight->afCascadeSplits[2] = 20.0f;
ptLight->afCascadeSplits[3] = 48.0f;
ptLight->afCascadeSplits[0] = 0.10f;
ptLight->afCascadeSplits[1] = 0.25f;
ptLight->afCascadeSplits[2] = 0.50f;
ptLight->afCascadeSplits[3] = 1.00f;
ptLight->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW | PL_LIGHT_FLAG_VISUALIZER;

plEntity tPointLight = gptEcs->create_point_light(ptMainComponentLibrary, "point light", (plVec3){0.0f, 2.0f, 2.0f}, &ptLight);
Expand Down
3 changes: 3 additions & 0 deletions sandbox/pl_gizmo.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,15 @@ pl_gizmo(plGizmoData* ptGizmoData, plDrawList3D* ptGizmoDrawlist, plComponentLib
{
case PL_SELECTION_MODE_TRANSLATION:
pl__gizmo_translation(ptGizmoData, ptGizmoDrawlist, ptCamera, ptSelectedTransform, ptParentTransform);
ptSelectedTransform->tFlags |= PL_TRANSFORM_FLAGS_DIRTY;
break;
case PL_SELECTION_MODE_ROTATION:
pl__gizmo_rotation(ptGizmoData, ptGizmoDrawlist, ptCamera, ptSelectedTransform, ptParentTransform);
ptSelectedTransform->tFlags |= PL_TRANSFORM_FLAGS_DIRTY;
break;
case PL_SELECTION_MODE_SCALE:
pl__gizmo_scale(ptGizmoData, ptGizmoDrawlist, ptCamera, ptSelectedTransform, ptParentTransform);
ptSelectedTransform->tFlags |= PL_TRANSFORM_FLAGS_DIRTY;
break;
case PL_SELECTION_MODE_NONE:
default:
Expand Down

0 comments on commit baadac6

Please sign in to comment.