Skip to content

Commit 09390cd

Browse files
committed
feat: add omni shadow mapping to renderer ext #81
1 parent 27041b6 commit 09390cd

File tree

12 files changed

+1885
-993
lines changed

12 files changed

+1885
-993
lines changed

extensions/pl_ecs_ext.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ pl_ecs_add_component(plComponentLibrary* ptLibrary, plComponentType tType, plEnt
655655
.tDirection = {0.0f, -1.0f, 0.0f},
656656
.fIntensity = 1.0f,
657657
.fRange = 10.0f,
658+
.fRadius = 0.025f,
658659
.tType = PL_LIGHT_TYPE_DIRECTIONAL,
659660
.uCascadeCount = 0,
660661
.tFlags = 0,

extensions/pl_ecs_ext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ typedef struct _plLightComponent
460460
plVec3 tColor;
461461
float fIntensity;
462462
float fRange;
463+
float fRadius;
463464
plVec3 tPosition;
464465
plVec3 tDirection;
465466
uint32_t uShadowResolution; // 0 -> automatic

extensions/pl_renderer_ext.c

Lines changed: 937 additions & 787 deletions
Large diffs are not rendered by default.

extensions/pl_renderer_ext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ typedef struct _plRendererI
8181

8282
// per frame
8383
void (*run_ecs) (uint32_t uSceneHandle);
84-
void (*render_scene)(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions tOptions);
84+
void (*render_scene)(uint32_t uSceneHandle, const uint32_t* auViewHandles, const plViewOptions* atOptions, uint32_t uViewCount);
8585
bool (*begin_frame) (void);
8686
void (*end_frame) (void);
8787
plEntity (*get_picked_entity)(void);

extensions/pl_renderer_internal.c

Lines changed: 607 additions & 114 deletions
Large diffs are not rendered by default.

extensions/pl_renderer_internal.h

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ typedef struct _plGPUMaterial
170170
int iOcclusionTexIdx;
171171
} plGPUMaterial;
172172

173-
typedef struct _plGPULight
173+
typedef struct _plGPUDLight
174174
{
175175
plVec3 tPosition;
176176
float fIntensity;
@@ -183,18 +183,44 @@ typedef struct _plGPULight
183183

184184
int iShadowIndex;
185185
int iCascadeCount;
186-
int _unused[2];
187-
} plGPULight;
186+
int iCastShadow;
187+
int _unused[1];
188+
} plGPUDLight;
188189

189-
typedef struct _plGPULightShadowData
190+
typedef struct _plGPUDLightShadowData
190191
{
191192
plVec4 cascadeSplits;
192193
plMat4 cascadeViewProjMat[4];
193194
int iShadowMapTexIdx;
194195
float fFactor;
195196
float fXOffset;
196197
float fYOffset;
197-
} plGPULightShadowData;
198+
} plGPUDLightShadowData;
199+
200+
typedef struct _plGPUPLight
201+
{
202+
plVec3 tPosition;
203+
float fIntensity;
204+
205+
plVec3 tDirection;
206+
int iType;
207+
208+
plVec3 tColor;
209+
float fRange;
210+
211+
int iShadowIndex;
212+
int iCastShadow;
213+
int _unused[2];
214+
} plGPUPLight;
215+
216+
typedef struct _plGPUPLightShadowData
217+
{
218+
plMat4 viewProjMat[6];
219+
int iShadowMapTexIdx;
220+
float fFactor;
221+
float fXOffset;
222+
float fYOffset;
223+
} plGPUPLightShadowData;
198224

199225
typedef struct _BindGroup_0
200226
{
@@ -219,11 +245,6 @@ typedef struct _DynamicData
219245
plMat4 tModel;
220246
} DynamicData;
221247

222-
typedef struct _plShadowData
223-
{
224-
plBufferHandle atCameraBuffers[PL_MAX_FRAMES_IN_FLIGHT];
225-
} plShadowData;
226-
227248
typedef struct _plRefView
228249
{
229250
// renderpasses
@@ -269,9 +290,9 @@ typedef struct _plRefView
269290
plDrawList3D* pt3DSelectionDrawList;
270291

271292
// shadows
272-
plShadowData tShadowData;
273-
plBufferHandle atLightShadowDataBuffer[PL_MAX_FRAMES_IN_FLIGHT];
274-
plGPULightShadowData* sbtLightShadowData;
293+
plBufferHandle atDShadowCameraBuffers[PL_MAX_FRAMES_IN_FLIGHT];
294+
plBufferHandle atDLightShadowDataBuffer[PL_MAX_FRAMES_IN_FLIGHT];
295+
plGPUDLightShadowData* sbtDLightShadowData;
275296
} plRefView;
276297

277298
typedef struct _plRefScene
@@ -301,21 +322,31 @@ typedef struct _plRefScene
301322
uint32_t* sbuIndexBuffer;
302323
plGPUMaterial* sbtMaterialBuffer;
303324
plVec4* sbtSkinVertexDataBuffer;
304-
plGPULight* sbtLightData;
325+
plGPUDLight* sbtDLightData;
326+
plGPUPLight* sbtPLightData;
305327

306328
// GPU buffers
307329
plBufferHandle tVertexBuffer;
308330
plBufferHandle tIndexBuffer;
309331
plBufferHandle tStorageBuffer;
310332
plBufferHandle tMaterialDataBuffer;
311333
plBufferHandle tSkinStorageBuffer;
312-
plBufferHandle atLightBuffer[PL_MAX_VIEWS_PER_SCENE];
334+
plBufferHandle atDLightBuffer[PL_MAX_VIEWS_PER_SCENE];
335+
plBufferHandle atPLightBuffer[PL_MAX_VIEWS_PER_SCENE];
313336

314337
// views
315338
uint32_t uViewCount;
316339
plRefView atViews[PL_MAX_VIEWS_PER_SCENE];
317340
plSkinData* sbtSkinData;
318341

342+
// shadow atlas
343+
plPackRect* sbtShadowRects;
344+
plRenderPassHandle tFirstShadowRenderPass;
345+
plRenderPassHandle tShadowRenderPass;
346+
uint32_t uShadowAtlasResolution;
347+
plTextureHandle tShadowTexture[PL_MAX_FRAMES_IN_FLIGHT];
348+
uint32_t atShadowTextureBindlessIndices[PL_MAX_FRAMES_IN_FLIGHT];
349+
319350
// ECS component library
320351
plComponentLibrary tComponentLibrary;
321352

@@ -340,6 +371,11 @@ typedef struct _plRefScene
340371
plMaterialComponent* sbtMaterials;
341372
plHashMap* ptMaterialHashmap;
342373

374+
// shadows
375+
plBufferHandle atPShadowCameraBuffers[PL_MAX_FRAMES_IN_FLIGHT];
376+
plBufferHandle atPLightShadowDataBuffer[PL_MAX_FRAMES_IN_FLIGHT];
377+
plGPUPLightShadowData* sbtPLightShadowData;
378+
343379
} plRefScene;
344380

345381
typedef struct _plRefRendererData
@@ -421,6 +457,8 @@ typedef struct _plRefRendererData
421457
// sync
422458
plTimelineSemaphore* aptSemaphores[PL_MAX_FRAMES_IN_FLIGHT];
423459
uint64_t aulNextTimelineValue[PL_MAX_FRAMES_IN_FLIGHT];
460+
plTimelineSemaphore* ptClickSemaphore;
461+
uint64_t ulSemClickNextValue;
424462

425463
// command pools
426464
plCommandPool* atCmdPools[PL_MAX_FRAMES_IN_FLIGHT];
@@ -432,13 +470,6 @@ typedef struct _plRefRendererData
432470
plTextureHandle* sbtTextureHandles;
433471
plHashMap* ptTextureHashmap;
434472

435-
// shadow atlas
436-
plPackRect* sbtShadowRects;
437-
plRenderPassHandle tShadowRenderPass;
438-
uint32_t uShadowAtlasResolution;
439-
plTextureHandle tShadowTexture[PL_MAX_FRAMES_IN_FLIGHT];
440-
uint32_t atShadowTextureBindlessIndices[PL_MAX_FRAMES_IN_FLIGHT];
441-
442473
// graphics options
443474
bool bReloadSwapchain;
444475
bool bReloadMSAA;
@@ -523,7 +554,9 @@ static bool pl__sat_visibility_test(plCameraComponent*, const plAABB*);
523554
// scene render helpers
524555
static void pl_refr_update_skin_textures(plCommandBuffer*, uint32_t);
525556
static void pl_refr_perform_skinning(plCommandBuffer*, uint32_t);
526-
static void pl_refr_generate_cascaded_shadow_map(plRenderEncoder*, plCommandBuffer*, uint32_t, uint32_t, plEntity tCamera);
557+
static bool pl_refr_pack_shadow_atlas(uint32_t uSceneHandle, const uint32_t* auViewHandles, uint32_t uViewCount);
558+
static void pl_refr_generate_cascaded_shadow_map(plRenderEncoder*, plCommandBuffer*, uint32_t, uint32_t, uint32_t, plEntity tCamera);
559+
static void pl_refr_generate_shadow_maps(plRenderEncoder*, plCommandBuffer*, uint32_t);
527560
static void pl_refr_post_process_scene(plCommandBuffer*, uint32_t, uint32_t, const plMat4*);
528561

529562
// shader variant system

sandbox/app.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plEditorData* ptEditorData)
200200
ptLight->uShadowResolution = 2048;
201201
ptLight->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW;
202202

203-
gptEcs->create_point_light(ptMainComponentLibrary, "light", (plVec3){6.0f, 4.0f, -3.0f}, NULL);
203+
gptEcs->create_point_light(ptMainComponentLibrary, "light", (plVec3){0.0f, 2.0f, 2.0f}, &ptLight);
204+
ptLight->uShadowResolution = 2048;
205+
ptLight->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW;
204206

205207
// load models
206208

@@ -352,7 +354,7 @@ pl_app_update(plEditorData* ptEditorData)
352354
.ptViewCamera = &ptEditorData->tMainCamera,
353355
.ptCullCamera = ptEditorData->bFreezeCullCamera ? &ptEditorData->tCullCamera : NULL
354356
};
355-
gptRenderer->render_scene(ptEditorData->uSceneHandle0, ptEditorData->uViewHandle0, tViewOptions);
357+
gptRenderer->render_scene(ptEditorData->uSceneHandle0, &ptEditorData->uViewHandle0, &tViewOptions, 1);
356358
}
357359

358360
gptUi->set_next_window_pos((plVec2){0, 0}, PL_UI_COND_ONCE);

0 commit comments

Comments
 (0)