Skip to content

Commit

Permalink
refac: job ext interface improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Nov 20, 2024
1 parent 5a736cc commit 7f73917
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
4 changes: 2 additions & 2 deletions extensions/pl_ecs_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -1020,11 +1020,11 @@ pl_run_skin_update_system(plComponentLibrary* ptLibrary)
}

static void
pl__object_update_job(uint32_t uJobIndex, void* pData)
pl__object_update_job(plInvocationData tInvoData, void* pData)
{
plComponentLibrary* ptLibrary = pData;
plObjectComponent* sbtComponents = ptLibrary->tObjectComponentManager.pComponents;
plObjectComponent* ptObject = &sbtComponents[uJobIndex];
plObjectComponent* ptObject = &sbtComponents[tInvoData.uGlobalIndex];
plTransformComponent* ptTransform = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptObject->tTransform);
plMeshComponent* ptMesh = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_MESH, ptObject->tMesh);
plSkinComponent* ptSkinComponent = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_SKIN, ptMesh->tSkinComponent);
Expand Down
41 changes: 25 additions & 16 deletions extensions/pl_job_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ typedef struct _plAtomicCounterNode

typedef struct _plSubmittedBatch
{
void (*task)(uint32_t, void*);
void (*task)(plInvocationData, void*);
void* pData;
plAtomicCounter* ptCounter;
uint32_t uJobIndex;
uint32_t uGroupSize;
plInvocationData tInvocationData;
} plSubmittedBatch;

typedef struct _plJobContext
Expand Down Expand Up @@ -177,8 +176,9 @@ pl__dispatch_jobs(uint32_t uJobCount, plJobDesc* ptJobs, plAtomicCounter** pptCo
gptJobCtx->atBatches[gptJobCtx->uBackIndex].task = ptJobs[i].task;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].pData = ptJobs[i].pData;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].ptCounter = ptCounter;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].uJobIndex = i;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].uGroupSize = 1;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].tInvocationData.uBatchIndex = 0;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].tInvocationData.uGlobalIndex = i;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].tInvocationData.uBatchSize = 1;
gptJobCtx->uBackIndex--;
if(gptJobCtx->uBackIndex == UINT32_MAX) // wrap around
gptJobCtx->uBackIndex = PL_MAX_BATCHES - 1;
Expand Down Expand Up @@ -210,8 +210,9 @@ pl__pop_batch_off_queue(plSubmittedBatch* ptBatchOut)
gptJobCtx->atBatches[gptJobCtx->uFrontIndex].pData = NULL;
gptJobCtx->atBatches[gptJobCtx->uFrontIndex].task = NULL;
gptJobCtx->atBatches[gptJobCtx->uFrontIndex].ptCounter = NULL;
gptJobCtx->atBatches[gptJobCtx->uFrontIndex].uGroupSize = UINT32_MAX;
gptJobCtx->atBatches[gptJobCtx->uFrontIndex].uJobIndex = UINT32_MAX;
gptJobCtx->atBatches[gptJobCtx->uFrontIndex].tInvocationData.uBatchSize = UINT32_MAX;
gptJobCtx->atBatches[gptJobCtx->uFrontIndex].tInvocationData.uGlobalIndex = UINT32_MAX;
gptJobCtx->atBatches[gptJobCtx->uFrontIndex].tInvocationData.uBatchIndex = UINT32_MAX;
gptJobCtx->uFrontIndex--;
if(gptJobCtx->uFrontIndex == UINT32_MAX) // wrap
gptJobCtx->uFrontIndex = PL_MAX_BATCHES - 1;
Expand Down Expand Up @@ -279,8 +280,9 @@ pl__dispatch_batch(uint32_t uJobCount, uint32_t uGroupSize, plJobDesc tJobDesc,
gptJobCtx->atBatches[gptJobCtx->uBackIndex].task = tJobDesc.task;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].pData = tJobDesc.pData;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].ptCounter = ptCounter;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].uJobIndex = i * uGroupSize;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].uGroupSize = uGroupSize;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].tInvocationData.uGlobalIndex = i * uGroupSize;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].tInvocationData.uBatchSize = uGroupSize;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].tInvocationData.uBatchIndex = i;
gptJobCtx->uBackIndex--;
if(gptJobCtx->uBackIndex == UINT32_MAX) // wrap around
gptJobCtx->uBackIndex = PL_MAX_BATCHES - 1;
Expand All @@ -293,8 +295,9 @@ pl__dispatch_batch(uint32_t uJobCount, uint32_t uGroupSize, plJobDesc tJobDesc,
gptJobCtx->atBatches[gptJobCtx->uBackIndex].task = tJobDesc.task;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].pData = tJobDesc.pData;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].ptCounter = ptCounter;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].uJobIndex = uBatches * uGroupSize;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].uGroupSize = uLeftOverJobs;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].tInvocationData.uGlobalIndex = uBatches * uGroupSize;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].tInvocationData.uBatchIndex = uBatches;
gptJobCtx->atBatches[gptJobCtx->uBackIndex].tInvocationData.uBatchSize = uLeftOverJobs;
gptJobCtx->uBackIndex--;
if(gptJobCtx->uBackIndex == UINT32_MAX) // wrap around
gptJobCtx->uBackIndex = PL_MAX_BATCHES - 1;
Expand Down Expand Up @@ -364,8 +367,13 @@ pl__thread_procedure(void* pData)
if(pl__pop_batch_off_queue(&tBatch))
{
// run tasks
for(uint32_t i = 0; i < tBatch.uGroupSize; i++)
tBatch.task(tBatch.uJobIndex + i, tBatch.pData);
plInvocationData tInvocationData = tBatch.tInvocationData;
for(uint32_t i = 0; i < tBatch.tInvocationData.uBatchSize; i++)
{
tInvocationData.uLocalIndex = i;
tInvocationData.uGlobalIndex = tBatch.tInvocationData.uGlobalIndex + i;
tBatch.task(tInvocationData, tBatch.pData);
}

// decrement atomic counter
if(tBatch.ptCounter)
Expand Down Expand Up @@ -407,8 +415,9 @@ pl__initialize(uint32_t uThreadCount)
{
gptJobCtx->atBatches[i].task = NULL;
gptJobCtx->atBatches[i].pData = NULL;
gptJobCtx->atBatches[i].uJobIndex = UINT32_MAX;
gptJobCtx->atBatches[i].uGroupSize = UINT32_MAX;
gptJobCtx->atBatches[i].tInvocationData.uBatchIndex = UINT32_MAX;
gptJobCtx->atBatches[i].tInvocationData.uGlobalIndex = UINT32_MAX;
gptJobCtx->atBatches[i].tInvocationData.uBatchSize = UINT32_MAX;
gptAtomics->create_atomic_counter(0, &gptJobCtx->atNodes[i].ptCounter);
gptJobCtx->atNodes[i].uNodeIndex = i;
gptJobCtx->atNodes[i].uNextNode = i + 1;
Expand All @@ -435,7 +444,7 @@ pl__cleanup(void)
{
gptJobCtx->atBatches[i].task = NULL;
gptJobCtx->atBatches[i].pData = NULL;
gptJobCtx->atBatches[i].uJobIndex = UINT32_MAX;
gptJobCtx->atBatches[i].tInvocationData.uGlobalIndex = UINT32_MAX;
gptAtomics->destroy_atomic_counter(&gptJobCtx->atNodes[i].ptCounter);
}
}
Expand Down
19 changes: 14 additions & 5 deletions extensions/pl_job_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ Index of this file:
//-----------------------------------------------------------------------------

// basic types
typedef struct _plJobDesc plJobDesc;
typedef struct _plJobDesc plJobDesc;
typedef struct _plInvocationData plInvocationData;

// external
typedef struct _plAtomicCounter plAtomicCounter; // pl_os.h
Expand All @@ -61,22 +62,30 @@ typedef struct _plJobI
// batch usage
// Follows more of a compute shader design. All jobs use the same data which can be indexed
// using the job index. If the jobs are small, consider increasing the group size.
// - uJobCount : how many jobs to generate
// - uGroupSize : how many jobs to execute per thread serially (set 0 for optimal group size)
// - jobCount : how many jobs to generate
// - groupSize : how many jobs to execute per thread serially (set 0 for optimal group size)
// - pass NULL for the atomic counter pointer if you don't need to wait (fire & forget)
void (*dispatch_batch)(uint32_t jobCount, uint32_t groupSize, plJobDesc, plAtomicCounter**);

// waits for counter to reach 0 and returns the counter for reuse but subsequent dispatches
// waits for counter to reach 0 and returns the counter for reuse by subsequent dispatches
void (*wait_for_counter)(plAtomicCounter*);
} plJobI;

//-----------------------------------------------------------------------------
// [SECTION] structs
//-----------------------------------------------------------------------------

typedef struct _plInvocationData
{
uint32_t uBatchIndex;
uint32_t uLocalIndex;
uint32_t uGlobalIndex;
uint32_t uBatchSize;
} plInvocationData;

typedef struct _plJobDesc
{
void (*task)(uint32_t uJobIndex, void* pData);
void (*task)(plInvocationData, void* data);
void* pData;
} plJobDesc;

Expand Down
16 changes: 8 additions & 8 deletions extensions/pl_renderer_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,8 @@ static size_t pl__get_data_type_size2(plDataType tType);
static plBlendState pl__get_blend_state(plBlendMode tBlendMode);

// job system tasks
static void pl__refr_job (uint32_t uJobIndex, void* pData);
static void pl__refr_cull_job (uint32_t uJobIndex, void* pData);
static void pl__refr_job (plInvocationData tInvoData, void* pData);
static void pl__refr_cull_job(plInvocationData tInvoData, void* pData);

// resource creation helpers
static plTextureHandle pl__refr_create_texture (const plTextureDesc* ptDesc, const char* pcName, uint32_t uIdentifier, plTextureUsage tInitialUsage);
Expand Down Expand Up @@ -3437,16 +3437,16 @@ typedef struct _plCullData
} plCullData;

static void
pl__refr_cull_job(uint32_t uJobIndex, void* pData)
pl__refr_cull_job(plInvocationData tInvoData, void* pData)
{
plCullData* ptCullData = pData;
plRefScene* ptScene = ptCullData->ptScene;
plDrawable tDrawable = ptCullData->atDrawables[uJobIndex];
plDrawable tDrawable = ptCullData->atDrawables[tInvoData.uGlobalIndex];
plMeshComponent* ptMesh = gptECS->get_component(&ptScene->tComponentLibrary, PL_COMPONENT_TYPE_MESH, tDrawable.tEntity);
ptCullData->atDrawables[uJobIndex].bCulled = true;
ptCullData->atDrawables[tInvoData.uGlobalIndex].bCulled = true;
if(pl__sat_visibility_test(ptCullData->ptCullCamera, &ptMesh->tAABBFinal))
{
ptCullData->atDrawables[uJobIndex].bCulled = false;
ptCullData->atDrawables[tInvoData.uGlobalIndex].bCulled = false;
}
}

Expand Down Expand Up @@ -5830,11 +5830,11 @@ pl__get_blend_state(plBlendMode tBlendMode)
}

static void
pl__refr_job(uint32_t uJobIndex, void* pData)
pl__refr_job(plInvocationData tInvoData, void* pData)
{
plMaterialComponent* sbtMaterials = pData;

plMaterialComponent* ptMaterial = &sbtMaterials[uJobIndex];
plMaterialComponent* ptMaterial = &sbtMaterials[tInvoData.uGlobalIndex];
int texWidth, texHeight, texNumChannels;
int texForceNumChannels = 4;

Expand Down

0 comments on commit 7f73917

Please sign in to comment.