Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Dec 21, 2024
1 parent 27041b6 commit 0e14d83
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 174 deletions.
1 change: 1 addition & 0 deletions extensions/pl_ecs_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ pl_ecs_add_component(plComponentLibrary* ptLibrary, plComponentType tType, plEnt
.tDirection = {0.0f, -1.0f, 0.0f},
.fIntensity = 1.0f,
.fRange = 10.0f,
.fRadius = 0.025f,
.tType = PL_LIGHT_TYPE_DIRECTIONAL,
.uCascadeCount = 0,
.tFlags = 0,
Expand Down
1 change: 1 addition & 0 deletions extensions/pl_ecs_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ typedef struct _plLightComponent
plVec3 tColor;
float fIntensity;
float fRange;
float fRadius;
plVec3 tPosition;
plVec3 tDirection;
uint32_t uShadowResolution; // 0 -> automatic
Expand Down
1 change: 1 addition & 0 deletions extensions/pl_renderer_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -3337,6 +3337,7 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions
.tDirection = ptLight->tDirection,
.tColor = ptLight->tColor,
.iShadowIndex = ptLight->tFlags & PL_LIGHT_FLAG_CAST_SHADOW ? iShadowIndex++ : 0,
.iCastShadow = ptLight->tFlags & PL_LIGHT_FLAG_CAST_SHADOW,
.iCascadeCount = (int)ptLight->uCascadeCount
};
ptScene->sbtLightData[i] = tLight;
Expand Down
457 changes: 304 additions & 153 deletions extensions/pl_renderer_internal.c

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions extensions/pl_renderer_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,14 @@ typedef struct _plGPULight

int iShadowIndex;
int iCascadeCount;
int _unused[2];
int iCastShadow;
int _unused[1];
} plGPULight;

typedef struct _plGPULightShadowData
{
plVec4 cascadeSplits;
plMat4 cascadeViewProjMat[4];
plMat4 cascadeViewProjMat[6];
int iShadowMapTexIdx;
float fFactor;
float fXOffset;
Expand Down
20 changes: 12 additions & 8 deletions sandbox/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plEditorData* ptEditorData)
ptLight->uShadowResolution = 2048;
ptLight->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW;

gptEcs->create_point_light(ptMainComponentLibrary, "light", (plVec3){6.0f, 4.0f, -3.0f}, NULL);

plLightComponent* ptPointLight = NULL;
gptEcs->create_point_light(ptMainComponentLibrary, "pointlight", (plVec3){0.0f, 1.0f, 3.0f}, &ptPointLight);
ptPointLight->uShadowResolution = 512;
ptPointLight->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW;

// load models

Expand Down Expand Up @@ -423,13 +427,13 @@ pl_app_update(plEditorData* ptEditorData)
{

const float pfWidths[] = {200.0f};
gptUi->layout_row(PL_UI_LAYOUT_ROW_TYPE_DYNAMIC, 0.0f, 1, pfRatios);
plLightComponent* ptLight = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_LIGHT, ptEditorData->tSunlight);
int iCascadeCount = (int)ptLight->uCascadeCount;
if(gptUi->slider_int("Sunlight Cascades", &iCascadeCount, 1, 4, 0))
{
ptLight->uCascadeCount = (uint32_t)iCascadeCount;
}
// gptUi->layout_row(PL_UI_LAYOUT_ROW_TYPE_DYNAMIC, 0.0f, 1, pfRatios);
// plLightComponent* ptLight = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_LIGHT, ptEditorData->tSunlight);
// int iCascadeCount = (int)ptLight->uCascadeCount;
// if(gptUi->slider_int("Sunlight Cascades", &iCascadeCount, 1, 4, 0))
// {
// ptLight->uCascadeCount = (uint32_t)iCascadeCount;
// }

gptUi->layout_row(PL_UI_LAYOUT_ROW_TYPE_STATIC, 0.0f, 1, pfWidths);
if(gptUi->button("Reload Shaders"))
Expand Down
22 changes: 13 additions & 9 deletions shaders/lighting.frag
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,17 @@ const mat4 biasMat = mat4(
0.5, 0.5, 0.0, 1.0
);

float textureProj(vec4 shadowCoord, vec2 offset, int textureIndex)
float textureProj(vec4 shadowCoord, vec2 offset, int textureIndex, int iType)
{
float shadow = 1.0;
vec2 comp2 = shadowCoord.st + offset;

if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 )
{
float dist = 1.0 - texture(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), comp2).r;
float dist = (1.0 - iType) - (1 - 2 * iType) * texture(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), comp2).r;
// float dist = 1.0 - texture(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), comp2).r;
// float dist = texture(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), comp2).r;
// dist += 1.0;
if (shadowCoord.w > 0 && dist < shadowCoord.z)
{
shadow = 0.0; // ambient
Expand All @@ -244,7 +247,7 @@ float textureProj(vec4 shadowCoord, vec2 offset, int textureIndex)
return shadow;
}

float filterPCF(vec4 sc, vec2 offset, int textureIndex)
float filterPCF(vec4 sc, vec2 offset, int textureIndex, int iType)
{
ivec2 texDim = textureSize(sampler2D(at2DTextures[nonuniformEXT(textureIndex)], tShadowSampler), 0).xy;
float scale = 1.0;
Expand All @@ -257,7 +260,7 @@ float filterPCF(vec4 sc, vec2 offset, int textureIndex)

for (int x = -range; x <= range; x++) {
for (int y = -range; y <= range; y++) {
shadowFactor += textureProj(sc, vec2(dx*x, dy*y) + offset, textureIndex);
shadowFactor += textureProj(sc, vec2(dx*x, dy*y) + offset, textureIndex, iType);
count++;
}
}
Expand Down Expand Up @@ -340,14 +343,14 @@ void main()
vec3 pointToLight;
float shadow = 1.0;

if(tLightData.iCascadeCount > 0)
if(tLightData.iCastShadow > 0)
{
plLightShadowData tShadowData = tShadowData.atData[tLightData.iShadowIndex];

// Get cascade index for the current fragment's view position


for(uint j = 0; j < tLightData.iCascadeCount - 1; ++j)
for(int j = 0; j < tLightData.iCascadeCount - 1; ++j)
{
if(tViewPosition.z > tShadowData.cascadeSplits[j])
{
Expand All @@ -362,10 +365,11 @@ void main()
abiasMat[3][0] *= tShadowData.fFactor;
abiasMat[3][1] *= tShadowData.fFactor;
vec4 shadowCoord = (abiasMat * tShadowData.cascadeViewProjMat[cascadeIndex]) * vec4(tWorldPosition.xyz, 1.0);
// shadowCoord.z = -shadowCoord.z;
shadow = 0;
// shadow = textureProj(shadowCoord / shadowCoord.w, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + vec2(cascadeIndex * tShadowData.fFactor, 0), tShadowData.iShadowMapTexIdx);
shadow = filterPCF(shadowCoord / shadowCoord.w, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + vec2(cascadeIndex * tShadowData.fFactor, 0), tShadowData.iShadowMapTexIdx);
// shadow = textureProj(shadowCoord / shadowCoord.w, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + vec2(cascadeIndex * tShadowData.fFactor, 0), tShadowData.iShadowMapTexIdx, tLightData.iType);
shadow = textureProj(shadowCoord / shadowCoord.w, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + vec2(cascadeIndex * tShadowData.fFactor, 0), tShadowData.iShadowMapTexIdx, tLightData.iType);
// shadow = textureProj(vec4(0.5, 0.5, 0.5, 1.0), vec2(0, 0), tShadowData.iShadowMapTexIdx, tLightData.iType);
// shadow = filterPCF(shadowCoord / shadowCoord.w, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + vec2(cascadeIndex * tShadowData.fFactor, 0), tShadowData.iShadowMapTexIdx, tLightData.iType);
}

if(tLightData.iType != PL_LIGHT_TYPE_DIRECTIONAL)
Expand Down
3 changes: 2 additions & 1 deletion shaders/lights.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ struct plLightData

int iShadowIndex;
int iCascadeCount;
int iCastShadow;
};

struct plLightShadowData
{
vec4 cascadeSplits;
mat4 cascadeViewProjMat[4];
mat4 cascadeViewProjMat[6];
int iShadowMapTexIdx;
float fFactor;
float fXOffset;
Expand Down
7 changes: 6 additions & 1 deletion shaders/shadow.vert
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ void main()
#else
gl_Position = tCameraInfo.atCameraProjs[tObjectInfo.iIndex] * pos;
#endif
gl_Position.z = 1.0 - gl_Position.z;

// if(tObjectInfo.tModel[2][3] == 0.0) // orthographic
// gl_Position.z = 1.0 - gl_Position.z;

// branchless way
gl_Position.z = (1 - tObjectInfo.tModel[2][3]) - (1 - 2 * tObjectInfo.tModel[2][3]) * gl_Position.z;
tShaderIn.tUV[0] = inTexCoord0;
tShaderIn.tUV[1] = inTexCoord1;
tShaderIn.tUV[2] = inTexCoord2;
Expand Down

0 comments on commit 0e14d83

Please sign in to comment.