Skip to content

Commit 6b07a3f

Browse files
WIP
1 parent 36a1684 commit 6b07a3f

6 files changed

Lines changed: 129 additions & 12 deletions

File tree

extensions/pl_renderer_ext.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ Index of this file:
1616
//-----------------------------------------------------------------------------
1717

1818
#include "pl_renderer_internal.h"
19-
#include "pl_renderer_internal.c"
2019
#include "pl_renderer_terrain.c"
20+
#include "pl_renderer_internal.c"
21+
2122

2223
//-----------------------------------------------------------------------------
2324
// [SECTION] public api
@@ -1131,7 +1132,7 @@ pl_renderer_create_scene(plSceneInit tInit)
11311132
ptScene->tPointLightingShader = gptShaderVariant->get_shader("deferred_lighting_point", NULL, NULL, aiLightingConstantData, &gptData->tRenderPassLayout);
11321133
ptScene->tProbeLightingShader = gptShaderVariant->get_shader("deferred_lighting", NULL, NULL, NULL, &gptData->tRenderPassLayout);
11331134
ptScene->tTerrainShader = gptShaderVariant->get_shader("terrain", NULL, NULL, NULL, &gptData->tRenderPassLayout);
1134-
ptScene->tTerrainShadowShader = gptShaderVariant->get_shader("terrain_shadow", NULL, NULL, NULL, &gptData->tRenderPassLayout);
1135+
ptScene->tTerrainShadowShader = gptShaderVariant->get_shader("terrain_shadow", NULL, NULL, NULL, &gptData->tDepthRenderPassLayout);
11351136

11361137
for(uint32_t i = 0; i < gptGfx->get_frames_in_flight(); i++)
11371138
{
@@ -2701,7 +2702,7 @@ pl_renderer_reload_scene_shaders(plScene* ptScene)
27012702
ptScene->tPointLightingShader = gptShaderVariant->get_shader("deferred_lighting_point", NULL, NULL, aiLightingConstantData, &gptData->tRenderPassLayout);
27022703
ptScene->tProbeLightingShader = gptShaderVariant->get_shader("deferred_lighting", NULL, NULL, NULL, &gptData->tRenderPassLayout);
27032704
ptScene->tTerrainShader = gptShaderVariant->get_shader("terrain", NULL, NULL, NULL, &gptData->tRenderPassLayout);
2704-
ptScene->tTerrainShadowShader = gptShaderVariant->get_shader("terrain_shadow", NULL, NULL, NULL, &gptData->tRenderPassLayout);
2705+
ptScene->tTerrainShadowShader = gptShaderVariant->get_shader("terrain_shadow", NULL, NULL, NULL, &gptData->tDepthRenderPassLayout);
27052706

27062707
gptShader->set_options(&tOriginalOptions);
27072708

extensions/pl_renderer_internal.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,32 @@ pl__renderer_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandB
14711471
atCamViewProjs[uCascade] = pl_mul_mat4(&tShadowCamera.tProjMat, &tShadowCamera.tViewMat);
14721472
ptShadowData->viewProjMat[uCascade] = atCamViewProjs[uCascade];
14731473
fLastSplitDist = fSplitDist;
1474+
1475+
// // move to function
1476+
// {
1477+
// const plMat4 tMVP = pl_mul_mat4(&tShadowCamera.tProjMat, &tShadowCamera.tViewMat);
1478+
1479+
// gptGfx->set_depth_bias(ptEncoder, gptData->tRuntimeOptions.fShadowConstantDepthBias, 0.0f, gptData->tRuntimeOptions.fShadowSlopeDepthBias);
1480+
// gptGfx->bind_shader(ptEncoder, ptScene->tTerrainShadowShader);
1481+
// gptGfx->bind_vertex_buffer(ptEncoder, ptScene->ptTerrain->tVertexBuffer);
1482+
// plBindGroupHandle atBindGroups[] = {ptScene->atSceneBindGroups[uFrameIdx], tInfo.tBindGroup};
1483+
1484+
// plDynamicBinding tDynamicBinding = pl__allocate_dynamic_data(ptDevice);
1485+
// plGpuDynShadow* ptDynamicData = (plGpuDynShadow*)tDynamicBinding.pcData;
1486+
// ptDynamicData->iIndex = (int)uCascade + iShadowIndex;
1487+
1488+
// gptGfx->bind_graphics_bind_groups(
1489+
// ptEncoder,
1490+
// ptScene->tTerrainShadowShader,
1491+
// 0, 2,
1492+
// atBindGroups,
1493+
// 1, &tDynamicBinding
1494+
// );
1495+
1496+
1497+
// for(uint32_t i = 0; i < pl_sb_size(ptScene->ptTerrain->sbtChunkFiles); i++)
1498+
// pl__render_chunk_shadow(ptScene, ptScene->ptTerrain, &tShadowCamera, ptEncoder, &ptScene->ptTerrain->sbtChunkFiles[i].tFile.atChunks[0], &ptScene->ptTerrain->sbtChunkFiles[i].tFile, &tMVP, 0);
1499+
// }
14741500
}
14751501

14761502
// TODO: rework to not waste so much space (don't use max cascades as stride)
@@ -1785,7 +1811,7 @@ pl__renderer_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandB
17851811
}
17861812
};
17871813

1788-
gptGfx->draw_stream(ptEncoder, 1, &tArea);
1814+
gptGfx->draw_stream(ptEncoder, 1, &tArea);
17891815
}
17901816
}
17911817
}

extensions/pl_renderer_terrain.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static bool pl__terrain_load(plTerrain* ptTerrain, plTerrainProcessInfo* ptInfo)
9393
void pl__remove_from_replacement_queue(plTerrain* ptTerrain, plTerrainChunk* ptChunk);
9494

9595
static void pl__render_chunk(plScene*, plTerrain*, plCamera*, plRenderEncoder*, plTerrainChunk*, plTerrainChunkFile*, const plMat4* ptMVP, uint32_t);
96+
static void pl__render_chunk_shadow(plScene*, plTerrain*, plCamera*, plRenderEncoder*, plTerrainChunk*, plTerrainChunkFile*, const plMat4* ptMVP, uint32_t);
9697

9798
static void pl__free_chunk_until(plTerrain* P, uint64_t idx_bytes_needed, uint64_t vtx_bytes_needed);
9899

@@ -995,6 +996,83 @@ pl__render_chunk(plScene* ptScene, plTerrain* ptTerrain, plCamera* ptCamera , pl
995996
}
996997
}
997998

999+
static void
1000+
pl__render_chunk_shadow(plScene* ptScene, plTerrain* ptTerrain, plCamera* ptCamera , plRenderEncoder* ptEncoder, plTerrainChunk* ptChunk, plTerrainChunkFile* ptFile, const plMat4* ptMVP, uint32_t uGlobalIndex)
1001+
{
1002+
PL_ASSERT(ptChunk != NULL);
1003+
1004+
plAABB tAABB = {
1005+
.tMin = ptChunk->tMinBound,
1006+
.tMax = ptChunk->tMaxBound
1007+
};
1008+
1009+
if(!pl__renderer_sat_visibility_test(ptCamera, &tAABB))
1010+
return;
1011+
1012+
plVec3 tClosestPoint = gptCollision->point_closest_point_aabb(ptCamera->tPos, tAABB);
1013+
float fDistance = fabsf(pl_length_vec3(pl_sub_vec3(tClosestPoint, ptCamera->tPos)));
1014+
1015+
pl__request_residency(ptTerrain, ptChunk);
1016+
1017+
if(ptChunk->ptIndexHole == NULL)
1018+
return;
1019+
1020+
float fViewportWidth = gptIOI->get_io()->tMainViewportSize.x;
1021+
float fHorizontalFieldOfView = 2.0f * atanf(tanf(0.5f * ptCamera->fFieldOfView) * ptCamera->fAspectRatio);
1022+
1023+
float fK = fViewportWidth / (2.0f * tanf(0.5f * fHorizontalFieldOfView));
1024+
1025+
float fGeometricError = ptFile->fMaxBaseError * (float)ptChunk->uLevel;
1026+
float fRho = fGeometricError * fK / fDistance;
1027+
1028+
1029+
float tauSubdivide = ptTerrain->tRuntimeOptions.fTau;
1030+
float tauMerge = tauSubdivide * 0.5f;
1031+
1032+
bool bChildrenResident = pl__all_children_resident(ptChunk);
1033+
1034+
// Decide refinement using hysteresis
1035+
if(!bChildrenResident || fRho <= tauSubdivide)
1036+
{
1037+
const plDrawIndex tDraw = {
1038+
.uInstanceCount = 1,
1039+
.uIndexCount = ptChunk->uIndexCount,
1040+
.uVertexStart = (uint32_t)(ptChunk->ptVertexHole->uOffset / sizeof(plTerrainVertex)),
1041+
.uIndexStart = (uint32_t)(ptChunk->ptIndexHole->uOffset / sizeof(uint32_t)),
1042+
.tIndexBuffer = ptTerrain->tIndexBuffer,
1043+
.uInstanceCount = 1 // uCascadeCount
1044+
};
1045+
1046+
gptGfx->draw_indexed(ptEncoder, 1, &tDraw);
1047+
*gptData->pdDrawCalls += 1;
1048+
1049+
pl__touch_chunk(ptTerrain, ptChunk);
1050+
1051+
// --- Hysteresis refinement logic ---
1052+
if(fRho > tauSubdivide)
1053+
{
1054+
// Need children soon – schedule them
1055+
for(uint32_t i = 0; i < 4; i++)
1056+
{
1057+
if(ptChunk->aptChildren[i])
1058+
pl__request_residency(ptTerrain, ptChunk->aptChildren[i]);
1059+
}
1060+
}
1061+
else if(fRho < tauMerge)
1062+
{
1063+
// Clearly low detail – unload children
1064+
pl__unload_children(ptTerrain, ptChunk);
1065+
}
1066+
// else: middle band → keep current state, prevent thrash
1067+
}
1068+
else
1069+
{
1070+
// Descend into children
1071+
for(uint32_t i = 0; i < 4; i++)
1072+
pl__render_chunk_shadow(ptScene, ptTerrain, ptCamera, ptEncoder, ptChunk->aptChildren[i], ptFile, ptMVP, uGlobalIndex);
1073+
}
1074+
}
1075+
9981076
static bool
9991077
pl__terrain_load(plTerrain* ptTerrain, plTerrainProcessInfo* ptInfo)
10001078
{

shaders/pl_terrain.frag

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ textureProj(vec4 shadowCoord, vec2 offset, int textureIndex)
2121
float dist = texture(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tSamplerNearestClamp), comp2).r;
2222
if (shadowCoord.w > 0 && dist > shadowCoord.z)
2323
{
24-
shadow = 0.0; // ambient
24+
shadow = 0.2; // ambient
2525
}
2626
}
2727
return shadow;
@@ -67,10 +67,11 @@ void main()
6767
tUVActual = tUVActual + tDynamicData.tData.tUVInfo.zw;
6868

6969
vec3 normal = normalize(tShaderIn.tWorldNormal);
70+
vec3 n = normal;
7071
// vec4 tHazardColor = texture(sampler2D(at2DTextures[tDynamicData.tData.uTextureIndex], tSamplerLinearRepeat), tUVActual);
7172

7273
vec3 sunlightColor = vec3(1.0, 1.0, 1.0);
73-
vec4 diffuse = texture(sampler2D(at2DTextures[tDynamicData.tData.uTextureIndex], tSamplerLinearRepeat), tUVActual);
74+
vec4 diffuse = pl_srgb_to_linear(texture(sampler2D(at2DTextures[tDynamicData.tData.uTextureIndex], tSamplerLinearRepeat), tUVActual));
7475
vec3 ambient = vec3(0);
7576

7677
outColor.xyz = vec3(0);
@@ -145,9 +146,19 @@ void main()
145146
break;
146147
}
147148
}
149+
150+
151+
}
152+
153+
vec3 l = normalize(pointToLight);
154+
float NdotL = clampedDot(n, l);
155+
if (NdotL > 0.0)
156+
{
157+
vec3 intensity = getLightIntensity(tLightData, pointToLight);
158+
outColor.xyz += shadow * intensity * diffuse.rgb * (max(0.0, dot(normal, pointToLight)) * sunlightColor + ambient);
148159
}
149160

150-
outColor.xyz += shadow * diffuse.rgb * (max(0.0, dot(normal, pointToLight)) * sunlightColor + ambient);
161+
151162
// outColor.xyz += diffuse.rgb * (max(0.0, dot(normal, pointToLight)) * sunlightColor + ambient);
152163
}
153164

@@ -170,6 +181,7 @@ void main()
170181

171182
if(bool(tDynamicData.tData.tFlags & PL_TERRAIN_SHADER_FLAGS_SHOW_LEVELS))
172183
{
184+
// outColor = tShaderIn.tColor;
173185
outColor += tShaderIn.tColor;
174186
}
175187

shaders/pl_terrain_shadow.vert

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ void main()
5353

5454
vec4 inPosition = vec4(inPos, 1.0);
5555

56-
uint uTransformIndex = plInstanceInfo.atData[gl_InstanceIndex].uTransformIndex;
57-
int uViewportIndex = plInstanceInfo.atData[gl_InstanceIndex].iViewportIndex;
56+
// uint uTransformIndex = plInstanceInfo.atData[gl_InstanceIndex].uTransformIndex;
57+
// int uViewportIndex = plInstanceInfo.atData[gl_InstanceIndex].iViewportIndex;
5858

5959

60-
gl_Position = tCameraInfo.atCameraProjs[tObjectInfo.tData.iIndex + uViewportIndex] * tTransformBuffer.atTransform[uTransformIndex] * inPosition;
61-
gl_ViewportIndex = uViewportIndex;
60+
gl_Position = tCameraInfo.atCameraProjs[tObjectInfo.tData.iIndex] * inPosition;
61+
// gl_ViewportIndex = uViewportIndex;
6262
}

shaders/shaders.pls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@
823823
"tFragmentShader": { "file": "pl_terrain.frag"},
824824
"tGraphicsState": {
825825
"ulDepthWriteEnabled": true,
826-
"ulDepthMode": "PL_COMPARE_MODE_GREATER_OR_EQUAL",
826+
"ulDepthMode": "PL_COMPARE_MODE_GREATER",
827827
"ulCullMode": "PL_CULL_MODE_CULL_BACK",
828828
"ulWireframe": false,
829829
"ulDepthClampEnabled": false,

0 commit comments

Comments
 (0)