Skip to content

Commit 3e5677a

Browse files
committed
fix: cascade issue in renderer ext
1 parent d69e55f commit 3e5677a

File tree

9 files changed

+134
-90
lines changed

9 files changed

+134
-90
lines changed

extensions/pl_renderer_ext.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ pl_refr_initialize(plWindow* ptWindow)
8484
// default options
8585
gptData->bVSync = true;
8686
gptData->uOutlineWidth = 4;
87-
gptData->fLambdaSplit = 0.95f;
8887
gptData->bFrustumCulling = true;
8988
gptData->fShadowConstantDepthBias = -1.25f;
9089
gptData->fShadowSlopeDepthBias = -10.75f;
@@ -3661,7 +3660,20 @@ pl_refr_render_scene(uint32_t uSceneHandle, const uint32_t* auViewHandles, const
36613660
for(uint32_t i = 0; i < pl_sb_size(ptScene->sbtPLightData); i++)
36623661
{
36633662
const plVec4 tColor = {.rgb = ptScene->sbtPLightData[i].tColor, .a = 1.0f};
3664-
gptDraw->add_3d_cross(ptView->pt3DDrawList, ptScene->sbtPLightData[i].tPosition, 0.02f, (plDrawLineOptions){.uColor = PL_COLOR_32_VEC4(tColor), .fThickness = 0.25f});
3663+
// gptDraw->add_3d_cross(ptView->pt3DDrawList, ptScene->sbtPLightData[i].tPosition, 0.02f, (plDrawLineOptions){.uColor = PL_COLOR_32_VEC4(tColor), .fThickness = 0.25f});
3664+
plDrawSphereDesc tSphere = {
3665+
.fRadius = 0.025f,
3666+
.tCenter = ptScene->sbtPLightData[i].tPosition,
3667+
.uLatBands = 6,
3668+
.uLongBands = 6
3669+
};
3670+
gptDraw->add_3d_sphere(ptView->pt3DDrawList, tSphere, (plDrawLineOptions){.uColor = PL_COLOR_32_VEC4(tColor), .fThickness = 0.005f});
3671+
tSphere.fRadius = ptScene->sbtPLightData[i].fRange;
3672+
plDrawSphereDesc tSphere2 = {
3673+
.fRadius = ptScene->sbtPLightData[i].fRange,
3674+
.tCenter = ptScene->sbtPLightData[i].tPosition
3675+
};
3676+
gptDraw->add_3d_sphere(ptView->pt3DDrawList, tSphere2, (plDrawLineOptions){.uColor = PL_COLOR_32_VEC4(tColor), .fThickness = 0.005f});
36653677
}
36663678

36673679
// debug drawing
@@ -4373,7 +4385,6 @@ pl_show_graphics_options(const char* pcTitle)
43734385
gptData->bReloadSwapchain = true;
43744386
gptUI->checkbox("Show Origin", &gptData->bShowOrigin);
43754387
gptUI->checkbox("Frustum Culling", &gptData->bFrustumCulling);
4376-
gptUI->slider_float("Lambda Split", &gptData->fLambdaSplit, 0.0f, 1.0f, 0);
43774388
gptUI->checkbox("Draw All Bounding Boxes", &gptData->bDrawAllBoundingBoxes);
43784389
gptUI->checkbox("Draw Visible Bounding Boxes", &gptData->bDrawVisibleBoundingBoxes);
43794390
gptUI->checkbox("Show Selected Bounding Box", &gptData->bShowSelectedBoundingBox);

extensions/pl_renderer_internal.c

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ pl_refr_generate_shadow_maps(plRenderEncoder* ptEncoder, plCommandBuffer* ptComm
904904
.fFieldOfView = PL_PI_2,
905905
.fAspectRatio = 1.0f
906906
};
907-
gptCamera->update(&tShadowCamera);
907+
// gptCamera->update(&tShadowCamera);
908908

909909
const plVec2 atPitchYaw[6] = {
910910
{0.0, 0.0},
@@ -1322,7 +1322,6 @@ pl_refr_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandBuffer
13221322

13231323
int iLastIndex = -1;
13241324
int iCurrentView = -1;
1325-
float fCascadeSplitLambda = gptData->fLambdaSplit;
13261325
uint32_t uDCameraBufferOffset = 0;
13271326
for(uint32_t uLightIndex = 0; uLightIndex < uLightCount; uLightIndex++)
13281327
{
@@ -1358,22 +1357,8 @@ pl_refr_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandBuffer
13581357
float afLambdaCascadeSplits[PL_MAX_SHADOW_CASCADES] = {0};
13591358
for(uint32_t uCascade = 0; uCascade < ptLight->uCascadeCount; uCascade++)
13601359
{
1361-
float fSplitDist = 0.0f;
1362-
if(fCascadeSplitLambda > 0.0f)
1363-
{
1364-
const float p = (uCascade + 1) / (float)ptLight->uCascadeCount;
1365-
const float fLog = fMinZ * powf(fRatio, p);
1366-
const float fUniform = fMinZ + fRange * p;
1367-
const float fD = fCascadeSplitLambda * (fLog - fUniform) + fUniform;
1368-
afLambdaCascadeSplits[uCascade] = (fD - fNearClip) / fClipRange;
1369-
fSplitDist = afLambdaCascadeSplits[uCascade];
1370-
ptShadowData->cascadeSplits.d[uCascade] = (fNearClip + fSplitDist * fClipRange);
1371-
}
1372-
else
1373-
{
1374-
fSplitDist = ptLight->afCascadeSplits[uCascade] / fClipRange;
1375-
ptShadowData->cascadeSplits.d[uCascade] = ptLight->afCascadeSplits[uCascade];
1376-
}
1360+
float fSplitDist = ptLight->afCascadeSplits[uCascade] / fClipRange;
1361+
ptShadowData->cascadeSplits.d[uCascade] = ptLight->afCascadeSplits[uCascade];
13771362

13781363
plVec3 atCameraCorners[] = {
13791364
{ -1.0f, 1.0f, 1.0f },
@@ -1431,9 +1416,9 @@ pl_refr_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandBuffer
14311416
tShadowCamera.fNearZ = 0.0f;
14321417
tShadowCamera.fFarZ = fRadius * 2.0f + 50.0f;
14331418
gptCamera->update(&tShadowCamera);
1434-
tShadowCamera.fAspectRatio = 1.0f;
1435-
tShadowCamera.fFieldOfView = atan2f(fRadius, (fRadius + 50.0f));
1436-
tShadowCamera.fNearZ = 0.01f;
1419+
// tShadowCamera.fAspectRatio = 1.0f;
1420+
// tShadowCamera.fFieldOfView = atan2f(fRadius, (fRadius + 50.0f));
1421+
// tShadowCamera.fNearZ = 0.01f;
14371422
fLastSplitDist = fSplitDist;
14381423

14391424
// tShadowCamera.tProjMat.col[2].z *= -1.0f;
@@ -2159,6 +2144,7 @@ pl_refr_create_global_shaders(void)
21592144
gptData->tAlphaShadowShader = gptGfx->create_shader(gptData->ptDevice, &tShadowShaderDescription);
21602145
tShadowShaderDescription.tPixelShader.puCode = NULL;
21612146
tShadowShaderDescription.tPixelShader.szCodeSize = 0;
2147+
tShadowShaderDescription.tGraphicsState.ulCullMode = PL_CULL_MODE_CULL_BACK;
21622148
gptData->tShadowShader = gptGfx->create_shader(gptData->ptDevice, &tShadowShaderDescription);
21632149

21642150
const plShaderDesc tPickShaderDescription = {

extensions/pl_renderer_internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,6 @@ typedef struct _plRefRendererData
474474
bool bReloadSwapchain;
475475
bool bReloadMSAA;
476476
bool bVSync;
477-
float fLambdaSplit;
478477
bool bShowOrigin;
479478
bool bFrustumCulling;
480479
bool bDrawAllBoundingBoxes;

sandbox/app.c

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,17 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plEditorData* ptEditorData)
198198
ptEditorData->tSunlight = gptEcs->create_directional_light(ptMainComponentLibrary, "sunlight", (plVec3){-0.375f, -1.0f, -0.085f}, &ptLight);
199199
ptLight->uCascadeCount = 4;
200200
ptLight->uShadowResolution = 2048;
201+
ptLight->afCascadeSplits[0] = 2.0f;
202+
ptLight->afCascadeSplits[1] = 8.0f;
203+
ptLight->afCascadeSplits[2] = 15.0f;
204+
ptLight->afCascadeSplits[3] = 35.0f;
201205
ptLight->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW;
202206

203-
gptEcs->create_point_light(ptMainComponentLibrary, "light", (plVec3){0.0f, 2.0f, 2.0f}, &ptLight);
207+
ptEditorData->tPointLight = gptEcs->create_point_light(ptMainComponentLibrary, "light", (plVec3){0.0f, 2.0f, 2.0f}, &ptLight);
204208
ptLight->uShadowResolution = 2048;
205209
ptLight->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW;
210+
plTransformComponent* ptLightTransform = gptEcs->add_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptEditorData->tPointLight);
211+
ptLightTransform->tTranslation = (plVec3){0.0f, 1.497f, 0.0f};
206212

207213
// load models
208214

@@ -312,6 +318,10 @@ pl_app_update(plEditorData* ptEditorData)
312318
plCameraComponent* ptCullCamera = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptEditorData->tCullCamera);
313319
gptCamera->update(ptCullCamera);
314320

321+
plTransformComponent* ptLightTransform = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptEditorData->tPointLight);
322+
plLightComponent* ptPointLight = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_LIGHT, ptEditorData->tPointLight);
323+
ptPointLight->tPosition = ptLightTransform->tTranslation;
324+
315325
if(ptEditorData->bSceneLoaded)
316326
{
317327
// run ecs system
@@ -358,6 +368,7 @@ pl_app_update(plEditorData* ptEditorData)
358368
}
359369

360370
gptUi->set_next_window_pos((plVec2){0, 0}, PL_UI_COND_ONCE);
371+
gptUi->set_next_window_size((plVec2){500.0f, 800.0f}, PL_UI_COND_ONCE);
361372

362373
if(gptUi->begin_window("Pilot Light", NULL, PL_UI_WINDOW_FLAGS_NONE))
363374
{
@@ -427,10 +438,39 @@ pl_app_update(plEditorData* ptEditorData)
427438
const float pfWidths[] = {200.0f};
428439
gptUi->layout_row(PL_UI_LAYOUT_ROW_TYPE_DYNAMIC, 0.0f, 1, pfRatios);
429440
plLightComponent* ptLight = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_LIGHT, ptEditorData->tSunlight);
430-
int iCascadeCount = (int)ptLight->uCascadeCount;
431-
if(gptUi->slider_int("Sunlight Cascades", &iCascadeCount, 1, 4, 0))
441+
// plLightComponent* ptPointLight = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_LIGHT, ptEditorData->tPointLight);
442+
443+
if(ptLight)
432444
{
433-
ptLight->uCascadeCount = (uint32_t)iCascadeCount;
445+
int iCascadeCount = (int)ptLight->uCascadeCount;
446+
if(gptUi->slider_int("Sunlight Cascades", &iCascadeCount, 1, 4, 0))
447+
{
448+
ptLight->uCascadeCount = (uint32_t)iCascadeCount;
449+
}
450+
451+
// int iResolution = (int)ptLight->uShadowResolution;
452+
gptUi->input_float4("Cascades", ptLight->afCascadeSplits, NULL, 0);
453+
}
454+
455+
if(ptPointLight)
456+
{
457+
gptUi->input_float("Point Light Radius", &ptPointLight->fRadius, NULL, 0);
458+
gptUi->input_float("Point Light Range", &ptPointLight->fRange, NULL, 0);
459+
int iResolution = (int)ptPointLight->uShadowResolution;
460+
uint32_t auResolutions[] = {
461+
128,
462+
256,
463+
512,
464+
1024,
465+
2048
466+
};
467+
static int iSelection = 4;
468+
gptUi->radio_button("Point Light Resolution: 128", &iSelection, 0);
469+
gptUi->radio_button("Point Light Resolution: 256", &iSelection, 1);
470+
gptUi->radio_button("Point Light Resolution: 512", &iSelection, 2);
471+
gptUi->radio_button("Point Light Resolution: 1024", &iSelection, 3);
472+
gptUi->radio_button("Point Light Resolution: 2048", &iSelection, 4);
473+
ptPointLight->uShadowResolution = auResolutions[iSelection];
434474
}
435475

436476
gptUi->layout_row(PL_UI_LAYOUT_ROW_TYPE_STATIC, 0.0f, 1, pfWidths);

sandbox/app.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ typedef struct _plEditorData
107107
plEntity tCullCamera;
108108
plEntity tMainCamera;
109109
plEntity tSunlight;
110+
plEntity tPointLight;
110111

111112
// views
112113
uint32_t uSceneHandle0;

sandbox/pl_ecs_tools.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ pl_show_ecs_window(plEntity* ptSelectedEntity, plComponentLibrary* ptLibrary, bo
172172
gptUi->text("Color: (%0.3f, %0.3f, %0.3f)", ptLightComp->tColor.r, ptLightComp->tColor.g, ptLightComp->tColor.b);
173173
gptUi->text("Direction: (%0.3f, %0.3f, %0.3f)", ptLightComp->tDirection.r, ptLightComp->tDirection.g, ptLightComp->tDirection.b);
174174
gptUi->text("Intensity: %0.3f", ptLightComp->fIntensity);
175+
gptUi->text("Radius: %0.3f", ptLightComp->fRadius);
176+
gptUi->text("Range: %0.3f", ptLightComp->fRange);
175177
gptUi->text("Cast Shadow: %s", ptLightComp->tFlags & PL_LIGHT_FLAG_CAST_SHADOW ? "true" : "false");
176178
}
177179

shaders/lighting.frag

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ float textureProj(vec4 shadowCoord, vec2 offset, int textureIndex)
246246

247247
if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 )
248248
{
249-
float dist = 1.0 - texture(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), comp2).r;
249+
float dist = 1.0 - texture(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), comp2).r * shadowCoord.w;
250250
if (shadowCoord.w > 0 && dist < shadowCoord.z)
251251
{
252252
shadow = 0.0; // ambient
@@ -404,8 +404,8 @@ void main()
404404
abiasMat[3][1] *= tShadowData.fFactor;
405405
vec4 shadowCoord = (abiasMat * tShadowData.viewProjMat[cascadeIndex]) * vec4(tWorldPosition.xyz, 1.0);
406406
shadow = 0;
407-
// shadow = textureProj(shadowCoord / shadowCoord.w, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + vec2(cascadeIndex * tShadowData.fFactor, 0), tShadowData.iShadowMapTexIdx);
408-
shadow = filterPCF(shadowCoord / shadowCoord.w, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + vec2(cascadeIndex * tShadowData.fFactor, 0), tShadowData.iShadowMapTexIdx);
407+
// shadow = textureProj(shadowCoord, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + vec2(cascadeIndex * tShadowData.fFactor, 0), tShadowData.iShadowMapTexIdx);
408+
shadow = filterPCF(shadowCoord, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + vec2(cascadeIndex * tShadowData.fFactor, 0), tShadowData.iShadowMapTexIdx);
409409
}
410410

411411
// BSTF
@@ -441,40 +441,44 @@ void main()
441441

442442
vec3 result = sampleCube(-normalize(pointToLight));
443443
vec4 shadowCoord = tShadowData.viewProjMat[int(result.z)] * vec4(tWorldPosition.xyz, 1.0);
444-
shadow = 1.0;
445-
const vec2 faceoffsets[6] = {
446-
vec2(0, 0),
447-
vec2(1, 0),
448-
vec2(0, 1),
449-
vec2(1, 1),
450-
vec2(0, 2),
451-
vec2(1, 2),
452-
};
453-
vec2 sampleLocation = vec2(tShadowData.fXOffset, tShadowData.fYOffset) + (result.xy * tShadowData.fFactor) + (faceoffsets[int(result.z)] * tShadowData.fFactor);
454-
float dist = texture(sampler2D(at2DTextures[nonuniformEXT(tShadowData.iShadowMapTexIdx)], tShadowSampler), sampleLocation).r;
455-
float fDist = shadowCoord.z;
456-
dist = 1 - dist * shadowCoord.w;
457-
458-
// if(int(result.z) == 5)
459-
// {
460-
461-
// if(dist > fDist - 0.025 && dist < fDist + 0.025) // green
462-
// {
463-
// inrange = true;
464-
// }
465-
// else if(dist < fDist - 0.025) // red
466-
// {
467-
// inrangeless = true;
468-
// }
469-
// else if(dist > fDist + 0.025) // blue
470-
// {
471-
// inrangemore = true;
472-
// }
473-
// }
474-
475-
if(shadowCoord.w > 0 && dist < fDist)
444+
if(shadowCoord.z > -1.0 && shadowCoord.z < 1.0)
476445
{
477-
shadow = 0.0;
446+
shadow = 1.0;
447+
const vec2 faceoffsets[6] = {
448+
vec2(0, 0),
449+
vec2(1, 0),
450+
vec2(0, 1),
451+
vec2(1, 1),
452+
vec2(0, 2),
453+
vec2(1, 2),
454+
};
455+
vec2 sampleLocation = vec2(tShadowData.fXOffset, tShadowData.fYOffset) + (result.xy * tShadowData.fFactor) + (faceoffsets[int(result.z)] * tShadowData.fFactor);
456+
float dist = texture(sampler2D(at2DTextures[nonuniformEXT(tShadowData.iShadowMapTexIdx)], tShadowSampler), sampleLocation).r;
457+
float fDist = shadowCoord.z;
458+
// dist = 1 - dist * shadowCoord.w;
459+
dist = dist * shadowCoord.w;
460+
461+
// if(int(result.z) == 5)
462+
// {
463+
464+
// if(dist > fDist - 0.025 && dist < fDist + 0.025) // green
465+
// {
466+
// inrange = true;
467+
// }
468+
// else if(dist < fDist - 0.025) // red
469+
// {
470+
// inrangeless = true;
471+
// }
472+
// else if(dist > fDist + 0.025) // blue
473+
// {
474+
// inrangemore = true;
475+
// }
476+
// }
477+
478+
if(shadowCoord.w > 0 && dist > fDist)
479+
{
480+
shadow = 0.0;
481+
}
478482
}
479483
}
480484

@@ -539,7 +543,7 @@ void main()
539543
// outColor = vec4(tViewPosition.rgb, tBaseColor.a);
540544
// outColor = vec4(n, tBaseColor.a);
541545

542-
// if(gl_FragCoord.x < 900.0)
546+
// if(gl_FragCoord.x < 1400.0)
543547
// {
544548
// switch(cascadeIndex) {
545549
// case 0 :

shaders/shadow.vert

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,9 @@ void main()
122122
gl_Position = tCameraInfo.atCameraProjs[tObjectInfo.iIndex] * pos;
123123
#endif
124124

125-
// if(tObjectInfo.tModel[2][3] == 0.0) // orthographic
126-
// gl_Position.z = 1.0 - gl_Position.z;
127-
// gl_Position.z = gl_Position.z / gl_Position.w;
125+
if(tCameraInfo.atCameraProjs[tObjectInfo.iIndex][3][3] == 1.0) // orthographic
126+
gl_Position.z = 1.0 - gl_Position.z;
128127

129-
// branchless way
130-
gl_Position.z = (1 - tObjectInfo.tModel[2][3]) - (1 - 2 * tObjectInfo.tModel[2][3]) * gl_Position.z;
131128
tShaderIn.tUV[0] = inTexCoord0;
132129
tShaderIn.tUV[1] = inTexCoord1;
133130
tShaderIn.tUV[2] = inTexCoord2;

0 commit comments

Comments
 (0)