Skip to content

Commit

Permalink
WIP: p
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Dec 22, 2024
1 parent c2e60cb commit d1d61c9
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 22 deletions.
6 changes: 4 additions & 2 deletions extensions/pl_renderer_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ pl_refr_initialize(plWindow* ptWindow)
// create depth render pass layout
const plRenderPassLayoutDesc tDepthRenderPassLayoutDesc = {
.atRenderTargets = {
{ .tFormat = PL_FORMAT_D16_UNORM, .bDepth = true }, // depth buffer
{ .tFormat = PL_FORMAT_D32_FLOAT, .bDepth = true }, // depth buffer
},
.atSubpasses = {
{
Expand Down Expand Up @@ -2938,7 +2938,7 @@ pl_refr_finalize_scene(uint32_t uSceneHandle)

const plTextureDesc tShadowDepthTextureDesc = {
.tDimensions = {(float)gptData->uShadowAtlasResolution, (float)gptData->uShadowAtlasResolution, 1},
.tFormat = PL_FORMAT_D16_UNORM,
.tFormat = PL_FORMAT_D32_FLOAT,
.uLayers = 1,
.uMips = 1,
.tType = PL_TEXTURE_TYPE_2D,
Expand Down Expand Up @@ -3343,6 +3343,8 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions

const plLightComponent* sbtLights = ptScene->tComponentLibrary.tLightComponentManager.pComponents;
int iShadowIndex = 0;
pl_sb_reset(ptScene->sbtPLightData);
pl_sb_reset(ptScene->sbtDLightData);
for(uint32_t i = 0; i < pl_sb_size(sbtLights); i++)
{
const plLightComponent* ptLight = &sbtLights[i];
Expand Down
20 changes: 13 additions & 7 deletions sandbox/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,17 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plEditorData* ptEditorData)


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

plLightComponent* ptLight2 = NULL;
gptEcs->create_directional_light(ptMainComponentLibrary, "sunlight2", (plVec3){0.0f, -1.0f, -1.0f}, &ptLight2);
ptLight2->uCascadeCount = 4;
ptLight2->uShadowResolution = 1024;
ptLight2->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW;
// plLightComponent* ptLight2 = NULL;
// gptEcs->create_directional_light(ptMainComponentLibrary, "sunlight2", (plVec3){0.0f, -1.0f, -1.0f}, &ptLight2);
// ptLight2->uCascadeCount = 4;
// ptLight2->uShadowResolution = 1024;
// ptLight2->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW;


// load models
Expand Down Expand Up @@ -533,6 +535,10 @@ pl_app_update(plEditorData* ptEditorData)
{
if(abModels[i])
{
// plMat4 tRot = pl_mat4_rotate_xyz(PL_PI_2, 1.0f, 0.0f, 0.0f);
// plMat4 tTranslate = pl_mat4_translate_xyz(0.0f, 0.0f, 9.8f);
// plMat4 tTransform = pl_mul_mat4(&tTranslate, &tRot);
gptModelLoader->load_gltf(ptMainComponentLibrary, apcModelPaths[i], NULL, &tLoaderData0);
gptModelLoader->load_gltf(ptMainComponentLibrary, apcModelPaths[i], NULL, &tLoaderData0);
}
}
Expand Down
111 changes: 101 additions & 10 deletions shaders/lighting.frag
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ layout(set = 2, binding = 0) uniform _plGlobalInfo

layout(set = 2, binding = 1) uniform _plDLightInfo
{
plLightData atData[500];
plLightData atData[1000];
} tDirectionLightInfo;

layout(set = 2, binding = 2) uniform _plPLightInfo
{
plLightData atData[500];
plLightData atData[1000];
} tPointLightInfo;

layout(set = 2, binding = 3) readonly buffer plShadowData
Expand Down Expand Up @@ -285,6 +285,33 @@ vec3 Decode( vec2 f )
return normalize( n );
}

vec3 sampleCube(vec3 v)
{
vec3 vAbs = abs(v);
float ma;
vec2 uv;
float faceIndex = 0.0;
if(vAbs.z >= vAbs.x && vAbs.z >= vAbs.y)
{
faceIndex = v.z < 0.0 ? 1.0 : 0.0;
ma = 0.5 / vAbs.z;
uv = vec2(v.z < 0.0 ? v.x : -v.x, -v.y);
}
else if(vAbs.y >= vAbs.x)
{
faceIndex = v.y < 0.0 ? 5.0 : 4.0;
ma = 0.5 / vAbs.y;
uv = vec2(v.x, v.y < 0.0 ? -v.z : v.z);
}
else
{
faceIndex = v.x < 0.0 ? 3.0 : 2.0;
ma = 0.5 / vAbs.x;
uv = vec2(v.x < 0.0 ? v.z : -v.z, -v.y);
}
vec2 result = uv * ma + vec2(0.5, 0.5);
return vec3(result, faceIndex);
}

void main()
{
Expand Down Expand Up @@ -336,6 +363,9 @@ void main()
vec3 f_specular_ibl = f_specular;
f_diffuse = vec3(0.0);
f_specular = vec3(0.0);
bool inrange = false;
bool inrangeless = false;
bool inrangemore = false;

uint cascadeIndex = 0;
if(bool(iRenderingFlags & PL_RENDERING_FLAG_USE_PUNCTUAL))
Expand Down Expand Up @@ -395,11 +425,15 @@ void main()

}



for(int i = 0; i < iPointLightCount; i++)
{
plLightData tLightData = tPointLightInfo.atData[i];

vec3 pointToLight = tLightData.tPosition - tWorldPosition.xyz;
float fDist = length(pointToLight);

float shadow = 1.0;

if(tLightData.iCastShadow > 0)
Expand All @@ -409,14 +443,58 @@ void main()
// Get cascade index for the current fragment's view position

// Depth compare for shadowing
mat4 abiasMat = biasMat;
abiasMat[0][0] *= tShadowData.fFactor;
abiasMat[1][1] *= tShadowData.fFactor;
abiasMat[3][0] *= tShadowData.fFactor;
abiasMat[3][1] *= tShadowData.fFactor;
vec4 shadowCoord = (abiasMat * tShadowData.cascadeViewProjMat[0]) * vec4(tWorldPosition.xyz, 1.0);
shadow = 0;
shadow = textureProj(shadowCoord / shadowCoord.w, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + vec2(cascadeIndex * tShadowData.fFactor, 0), tShadowData.iShadowMapTexIdx, tLightData.iType);

// mat4 abiasMat = biasMat;
// abiasMat[0][0] *= tShadowData.fFactor;
// abiasMat[1][1] *= tShadowData.fFactor;
// abiasMat[3][0] *= tShadowData.fFactor;
// abiasMat[3][1] *= tShadowData.fFactor;
vec3 result = sampleCube(-normalize(pointToLight));
vec4 shadowCoord = tShadowData.cascadeViewProjMat[int(result.z)] * vec4(tWorldPosition.xyz, 1.0);
// vec4 shadowCoord = abiasMat * tShadowData.cascadeViewProjMat[int(result.z)] * vec4(tWorldPosition.xyz, 1.0);
// shadowCoord.xyz /= shadowCoord.w;
shadow = 1.0;
const vec2 faceoffsets[6] = {
vec2(0, 0),
vec2(1, 0),
vec2(0, 1),
vec2(1, 1),
vec2(0, 2),
vec2(1, 2),
};
vec2 sampleLocation = vec2(tShadowData.fXOffset, tShadowData.fYOffset) + (result.xy * tShadowData.fFactor) + (faceoffsets[int(result.z)] * tShadowData.fFactor);
float dist = texture(sampler2D(at2DTextures[nonuniformEXT(tShadowData.iShadowMapTexIdx)], tShadowSampler), sampleLocation).r;
fDist = shadowCoord.z;
dist = 1 - dist * shadowCoord.w;
// fDist = shadowCoord.w;
// dist = (1 - dist) * tLightData.fRange;
// dist = (1 - dist);
// dist = dist * tLightData.fRange;

// if(int(result.z) == 0)
// {

// if(dist > fDist - 0.025 && dist < fDist + 0.025) // green
// {
// inrange = true;
// }
// else if(dist < fDist - 0.025) // red
// {
// inrangeless = true;
// }
// else if(dist > fDist + 0.025) // blue
// {
// inrangemore = true;
// }
// }

if(shadowCoord.w > 0 && dist < fDist)
{
shadow = 0.0;
}
// shadow = textureProj(shadowCoord / shadowCoord.w, vec2(tShadowData.fXOffset, tShadowData.fYOffset) + faceoffsets[int(result.z)] * tShadowData.fFactor, tShadowData.iShadowMapTexIdx, tLightData.iType);


}

// BSTF
Expand Down Expand Up @@ -459,6 +537,19 @@ void main()
vec3 color = diffuse + specular;

outColor = vec4(color.rgb, tBaseColor.a);
if(inrange)
{
outColor.rgb *= vec3(0.25f, 1.0f, 0.25f);
}
else if(inrangeless)
{
outColor.rgb *= vec3(1.0f, 0.25f, 0.25f);
}
else if(inrangemore)
{
outColor.rgb *= vec3(0.25f, 0.25f, 1.0f);
}

// outColor = vec4(ndcSpace.rgb, tBaseColor.a);
// outColor = vec4(gl_FragCoord.x, 0, 0, tBaseColor.a);
// outColor = vec4(v.r, v.g, v.b, tBaseColor.a);
Expand Down
3 changes: 2 additions & 1 deletion shaders/shadow.vert
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ void main()
#endif

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

// branchless way
gl_Position.z = (1 - tObjectInfo.tModel[2][3]) - (1 - 2 * tObjectInfo.tModel[2][3]) * gl_Position.z;
Expand Down
4 changes: 2 additions & 2 deletions shaders/transparent.frag
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ layout(set = 1, binding = 0) uniform _plGlobalInfo

layout(set = 1, binding = 1) uniform _plDLightInfo
{
plLightData atData[500];
plLightData atData[1000];
} tDirectionLightInfo;

layout(set = 1, binding = 2) uniform _plPLightInfo
{
plLightData atData[500];
plLightData atData[1000];
} tPointLightInfo;

layout(set = 1, binding = 3) readonly buffer plShadowData
Expand Down

0 comments on commit d1d61c9

Please sign in to comment.