From d1d61c9e0f78bd727bfbd3eccccf7657782209f8 Mon Sep 17 00:00:00 2001 From: Jonathan Hoffstadt Date: Sun, 22 Dec 2024 09:41:39 -0600 Subject: [PATCH] WIP: p --- extensions/pl_renderer_ext.c | 6 +- sandbox/app.c | 20 ++++--- shaders/lighting.frag | 111 +++++++++++++++++++++++++++++++---- shaders/shadow.vert | 3 +- shaders/transparent.frag | 4 +- 5 files changed, 122 insertions(+), 22 deletions(-) diff --git a/extensions/pl_renderer_ext.c b/extensions/pl_renderer_ext.c index 694d43c..0baafd5 100644 --- a/extensions/pl_renderer_ext.c +++ b/extensions/pl_renderer_ext.c @@ -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 = { { @@ -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, @@ -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]; diff --git a/sandbox/app.c b/sandbox/app.c index 383e954..1433e94 100644 --- a/sandbox/app.c +++ b/sandbox/app.c @@ -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 @@ -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); } } diff --git a/shaders/lighting.frag b/shaders/lighting.frag index de1678d..11401e5 100644 --- a/shaders/lighting.frag +++ b/shaders/lighting.frag @@ -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 @@ -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() { @@ -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)) @@ -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) @@ -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 @@ -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); diff --git a/shaders/shadow.vert b/shaders/shadow.vert index 20749a3..d410b7a 100644 --- a/shaders/shadow.vert +++ b/shaders/shadow.vert @@ -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; diff --git a/shaders/transparent.frag b/shaders/transparent.frag index b84594b..0f5d906 100644 --- a/shaders/transparent.frag +++ b/shaders/transparent.frag @@ -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