Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Dec 17, 2024
1 parent aba7453 commit d59c99a
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 89 deletions.
63 changes: 21 additions & 42 deletions extensions/pl_renderer_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,8 @@ pl_refr_create_view(uint32_t uSceneHandle, plVec2 tDimensions)
plRefView* ptView = &ptScene->atViews[uViewHandle];

ptView->tTargetSize = tDimensions;
ptView->tShadowData.tResolution.x = 1024.0f * 2.0f;
ptView->tShadowData.tResolution.y = 1024.0f * 2.0f;
ptView->tShadowData.tResolution.x = 1024.0f * 4.0f;
ptView->tShadowData.tResolution.y = 1024.0f * 4.0f;

// create offscreen per-frame resources
const plTextureDesc tRawOutputTextureDesc = {
Expand Down Expand Up @@ -635,9 +635,9 @@ pl_refr_create_view(uint32_t uSceneHandle, plVec2 tDimensions)
const plTextureDesc tShadowDepthTextureDesc = {
.tDimensions = {ptView->tShadowData.tResolution.x, ptView->tShadowData.tResolution.y, 1},
.tFormat = PL_FORMAT_D32_FLOAT,
.uLayers = 4,
.uLayers = 1,
.uMips = 1,
.tType = PL_TEXTURE_TYPE_2D_ARRAY,
.tType = PL_TEXTURE_TYPE_2D,
.tUsage = PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT | PL_TEXTURE_USAGE_SAMPLED,
.pcDebugName = "shadow map"
};
Expand Down Expand Up @@ -694,7 +694,7 @@ pl_refr_create_view(uint32_t uSceneHandle, plVec2 tDimensions)
plRenderPassAttachments atAttachmentSets[PL_MAX_FRAMES_IN_FLIGHT] = {0};
plRenderPassAttachments atUVAttachmentSets[PL_MAX_FRAMES_IN_FLIGHT] = {0};
plRenderPassAttachments atPostProcessAttachmentSets[PL_MAX_FRAMES_IN_FLIGHT] = {0};
plRenderPassAttachments atShadowAttachmentSets[PL_MAX_SHADOW_CASCADES][PL_MAX_FRAMES_IN_FLIGHT] = {0};
plRenderPassAttachments atShadowAttachmentSets[PL_MAX_FRAMES_IN_FLIGHT] = {0};

ptView->tPickTexture = pl__refr_create_texture(&tPickTextureDesc, "pick original", 0, PL_TEXTURE_USAGE_SAMPLED);
ptView->tPickDepthTexture = pl__refr_create_texture(&tPickDepthTextureDesc, "pick depth original", 0, PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT);
Expand Down Expand Up @@ -772,22 +772,7 @@ pl_refr_create_view(uint32_t uSceneHandle, plVec2 tDimensions)
ptView->tShadowData.atCameraBuffers[i] = pl__refr_create_staging_buffer(&atCameraBuffersDesc, "shadow buffer", i);
ptView->tShadowData.tDepthTexture[i] = pl__refr_create_texture(&tShadowDepthTextureDesc, "shadow map", i, PL_TEXTURE_USAGE_SAMPLED);


for(uint32_t j = 0; j < 4; j++)
{

plTextureViewDesc tShadowDepthView = {
.tFormat = PL_FORMAT_D32_FLOAT,
.uBaseMip = 0,
.uMips = 1,
.uBaseLayer = j,
.uLayerCount = 1,
.tTexture = ptView->tShadowData.tDepthTexture[i],
.pcDebugName = "shadow view"
};
(ptView->tShadowData.atDepthTextureViews[j])[i] = gptGfx->create_texture_view(gptData->ptDevice, &tShadowDepthView);
(atShadowAttachmentSets[j])[i].atViewAttachments[0] = (ptView->tShadowData.atDepthTextureViews[j])[i];
}
atShadowAttachmentSets[i].atViewAttachments[0] = ptView->tShadowData.tDepthTexture[i];
}

const plRenderPassDesc tRenderPassDesc = {
Expand Down Expand Up @@ -934,10 +919,7 @@ pl_refr_create_view(uint32_t uSceneHandle, plVec2 tDimensions)
};
ptView->tUVRenderPass = gptGfx->create_render_pass(gptData->ptDevice, &tUVRenderPass0Desc, atUVAttachmentSets);

for(uint32_t i = 0; i < 4; i++)
{
ptView->tShadowData.atOpaqueRenderPasses[i] = gptGfx->create_render_pass(gptData->ptDevice, &tDepthRenderPassDesc, atShadowAttachmentSets[i]);
}
ptView->tShadowData.tOpaqueRenderPass = gptGfx->create_render_pass(gptData->ptDevice, &tDepthRenderPassDesc, atShadowAttachmentSets);

return uViewHandle;
}
Expand Down Expand Up @@ -2302,10 +2284,10 @@ pl_refr_reload_scene_shaders(uint32_t uSceneHandle)
{ .uSlot = 2, .tType = PL_BUFFER_BINDING_TYPE_STORAGE, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL}
},
.atTextureBindings = {
{.uSlot = 2, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL, .tType = PL_TEXTURE_BINDING_TYPE_SAMPLED, .uDescriptorCount = 4},
{.uSlot = 3, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL, .tType = PL_TEXTURE_BINDING_TYPE_SAMPLED},
},
.atSamplerBindings = {
{.uSlot = 6, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL}
{.uSlot = 4, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL}
},
}
}
Expand Down Expand Up @@ -2872,10 +2854,10 @@ pl_refr_finalize_scene(uint32_t uSceneHandle)
{ .uSlot = 2, .tType = PL_BUFFER_BINDING_TYPE_STORAGE, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL}
},
.atTextureBindings = {
{.uSlot = 3, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL, .tType = PL_TEXTURE_BINDING_TYPE_SAMPLED, .uDescriptorCount = 4},
{.uSlot = 3, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL, .tType = PL_TEXTURE_BINDING_TYPE_SAMPLED},
},
.atSamplerBindings = {
{.uSlot = 7, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL}
{.uSlot = 4, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL}
},
}
}
Expand Down Expand Up @@ -3343,10 +3325,10 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions
{ .uSlot = 2, .tType = PL_BUFFER_BINDING_TYPE_STORAGE, .tStages = PL_STAGE_PIXEL | PL_STAGE_VERTEX}
},
.atTextureBindings = {
{.uSlot = 3, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL, .tType = PL_TEXTURE_BINDING_TYPE_SAMPLED, .uDescriptorCount = 4},
{.uSlot = 3, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL, .tType = PL_TEXTURE_BINDING_TYPE_SAMPLED},
},
.atSamplerBindings = {
{.uSlot = 7, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL}
{.uSlot = 4, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL}
},
};
const plBindGroupDesc tLightBGDesc = {
Expand All @@ -3362,27 +3344,24 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions
{ .uSlot = 1, .tBuffer = ptScene->atLightBuffer[uFrameIdx], .szBufferRange = sizeof(plGPULight) * pl_sb_size(ptScene->sbtLightData)},
{ .uSlot = 2, .tBuffer = ptView->atLightShadowDataBuffer[uFrameIdx], .szBufferRange = sizeof(plGPULightShadowData) * pl_sb_size(ptView->sbtLightShadowData)}
};
plBindGroupUpdateTextureData atBGTextureData[4] = {0};
for(uint32_t i = 0; i < 4; i++)
{
atBGTextureData[i].tTexture = (ptView->tShadowData.atDepthTextureViews[i])[uFrameIdx];
atBGTextureData[i].uSlot = 3;
atBGTextureData[i].uIndex = i;
atBGTextureData[i].tType = PL_TEXTURE_BINDING_TYPE_SAMPLED;
}
plBindGroupUpdateTextureData tBGTextureData = {0};
tBGTextureData.tTexture = ptView->tShadowData.tDepthTexture[uFrameIdx];
tBGTextureData.uSlot = 3;
tBGTextureData.uIndex = 0;
tBGTextureData.tType = PL_TEXTURE_BINDING_TYPE_SAMPLED;

plBindGroupUpdateSamplerData tShadowSamplerData[] = {
{
.tSampler = gptData->tShadowSampler,
.uSlot = 7
.uSlot = 4
}
};

plBindGroupUpdateData tBGData2 = {
.uBufferCount = 3,
.atBufferBindings = atLightBufferData,
.uTextureCount = 4,
.atTextureBindings = atBGTextureData,
.uTextureCount = 1,
.atTextureBindings = &tBGTextureData,
.uSamplerCount = 1,
.atSamplerBindings = tShadowSamplerData
};
Expand Down
94 changes: 75 additions & 19 deletions extensions/pl_renderer_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -907,19 +907,70 @@ pl_refr_generate_cascaded_shadow_map(plCommandBuffer* ptCommandBuffer, uint32_t
const uint32_t uOpaqueDrawableCount = pl_sb_size(ptScene->sbtDeferredDrawables);
const uint32_t uTransparentDrawableCount = pl_sb_size(ptScene->sbtForwardDrawables);

const plVec2 tDimensions = gptGfx->get_render_pass(ptDevice, ptView->tShadowData.atOpaqueRenderPasses[0])->tDesc.tDimensions;

plDrawArea tArea = {
.ptDrawStream = ptStream,
.tScissor = {
.uWidth = (uint32_t)tDimensions.x,
.uHeight = (uint32_t)tDimensions.y,
},
.tViewport = {
.fWidth = tDimensions.x,
.fHeight = tDimensions.y,
.fMaxDepth = 1.0f
}
const plVec2 tDimensions = gptGfx->get_render_pass(ptDevice, ptView->tShadowData.tOpaqueRenderPass)->tDesc.tDimensions;

plDrawArea atAreas[] =
{
{
.ptDrawStream = ptStream,
.tScissor = {
.uWidth = (uint32_t)(tDimensions.x * 0.5f),
.uHeight = (uint32_t)(tDimensions.y * 0.5f),
},
.tViewport = {
.fWidth = tDimensions.x * 0.5f,
.fHeight = tDimensions.y * 0.5f,
.fMaxDepth = 1.0f
}
},
{
.ptDrawStream = ptStream,
.tScissor = {
.iOffsetX = 0,
.iOffsetY = (int)(tDimensions.y * 0.5f),
.uWidth = (uint32_t)(tDimensions.x * 0.5f),
.uHeight = (uint32_t)(tDimensions.y * 0.5f),
},
.tViewport = {
.fX = 0,
.fY = tDimensions.y * 0.5f,
.fWidth = tDimensions.x * 0.5f,
.fHeight = tDimensions.y * 0.5f,
.fMaxDepth = 1.0f
}
},
{
.ptDrawStream = ptStream,
.tScissor = {
.iOffsetX = (int)(tDimensions.x * 0.5f),
.iOffsetY = 0,
.uWidth = (uint32_t)(tDimensions.x * 0.5f),
.uHeight = (uint32_t)(tDimensions.y * 0.5f),
},
.tViewport = {
.fX = tDimensions.x * 0.5f,
.fY = 0.0f,
.fWidth = tDimensions.x * 0.5f,
.fHeight = tDimensions.y * 0.5f,
.fMaxDepth = 1.0f
}
},
{
.ptDrawStream = ptStream,
.tScissor = {
.iOffsetX = (int)(tDimensions.x * 0.5f),
.iOffsetY = (int)(tDimensions.y * 0.5f),
.uWidth = (uint32_t)(tDimensions.x * 0.5f),
.uHeight = (uint32_t)(tDimensions.y * 0.5f),
},
.tViewport = {
.fX = tDimensions.x * 0.5f,
.fY = tDimensions.y * 0.5f,
.fWidth = tDimensions.x * 0.5f,
.fHeight = tDimensions.y * 0.5f,
.fMaxDepth = 1.0f
}
}
};

typedef struct _plShadowDynamicData
Expand All @@ -931,6 +982,9 @@ pl_refr_generate_cascaded_shadow_map(plCommandBuffer* ptCommandBuffer, uint32_t
plMat4 tModel;
} plShadowDynamicData;

plRenderEncoder* ptEncoder = gptGfx->begin_render_pass(ptCommandBuffer, ptView->tShadowData.tOpaqueRenderPass, NULL);


for(uint32_t uCascade = 0; uCascade < ptLight->uCascadeCount; uCascade++)
{

Expand All @@ -939,7 +993,6 @@ pl_refr_generate_cascaded_shadow_map(plCommandBuffer* ptCommandBuffer, uint32_t

gptGfx->reset_draw_stream(ptStream, uVisibleOpaqueDrawCount + uVisibleTransparentDrawCount);

plRenderEncoder* ptEncoder = gptGfx->begin_render_pass(ptCommandBuffer, ptView->tShadowData.atOpaqueRenderPasses[uCascade], NULL);
gptGfx->set_depth_bias(ptEncoder, 0.005f, 0.0f, 1.0f);

for(uint32_t i = 0; i < uVisibleOpaqueDrawCount; i++)
Expand Down Expand Up @@ -1020,12 +1073,15 @@ pl_refr_generate_cascaded_shadow_map(plCommandBuffer* ptCommandBuffer, uint32_t
.uInstanceOffset = 0,
.uInstanceCount = 1
});
}

gptGfx->draw_stream(ptEncoder, 1, &tArea);
gptGfx->end_render_pass(ptEncoder);
};

gptGfx->draw_stream(ptEncoder, 1, &atAreas[uCascade]);

}

gptGfx->end_render_pass(ptEncoder);

pl_end_cpu_sample(gptProfile, 0);
}

Expand Down Expand Up @@ -1299,10 +1355,10 @@ pl_refr_create_global_shaders(void)
{ .uSlot = 2, .tType = PL_BUFFER_BINDING_TYPE_STORAGE, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL}
},
.atTextureBindings = {
{.uSlot = 3, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL, .tType = PL_TEXTURE_BINDING_TYPE_SAMPLED, .uDescriptorCount = 4},
{.uSlot = 3, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL, .tType = PL_TEXTURE_BINDING_TYPE_SAMPLED},
},
.atSamplerBindings = {
{.uSlot = 7, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL}
{.uSlot = 4, .tStages = PL_STAGE_VERTEX | PL_STAGE_PIXEL}
},
}
}
Expand Down
3 changes: 1 addition & 2 deletions extensions/pl_renderer_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,8 @@ typedef struct _plShadowData
{
plVec2 tResolution;
plTextureHandle tDepthTexture[PL_MAX_FRAMES_IN_FLIGHT];
plTextureHandle atDepthTextureViews[PL_MAX_SHADOW_CASCADES][PL_MAX_FRAMES_IN_FLIGHT];

plRenderPassHandle atOpaqueRenderPasses[PL_MAX_SHADOW_CASCADES];
plRenderPassHandle tOpaqueRenderPass;

plBufferHandle atCameraBuffers[PL_MAX_FRAMES_IN_FLIGHT];
} plShadowData;
Expand Down
31 changes: 19 additions & 12 deletions shaders/lighting.frag
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ layout(set = 2, binding = 2) readonly buffer plShadowData
plLightShadowData atData[];
} tShadowData;

layout (set = 2, binding = 3) uniform texture2D shadowmap[4];
layout(set = 2, binding = 7) uniform sampler tShadowSampler;
layout (set = 2, binding = 3) uniform texture2D shadowmap;
layout(set = 2, binding = 4) uniform sampler tShadowSampler;

//-----------------------------------------------------------------------------
// [SECTION] dynamic bind group
Expand Down Expand Up @@ -223,20 +223,27 @@ vec3 getIBLRadianceLambertian(vec3 n, vec3 v, float roughness, vec3 diffuseColor
}

const mat4 biasMat = mat4(
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.25, 0.0, 0.0, 0.0,
0.0, 0.25, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.0, 1.0
0.25, 0.25, 0.0, 1.0
);

float textureProj(vec4 shadowCoord, vec2 offset, uint cascadeIndex)
const vec2 atCascadeOffsets[4] = {
vec2(0),
vec2(0, 0.5),
vec2(0.5, 0),
vec2(0.5, 0.5),
};

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

if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 )
{
float dist = texture(sampler2D(shadowmap[cascadeIndex], tShadowSampler), comp2).r;
float dist = texture(sampler2D(shadowmap, tShadowSampler), comp2).r;
if (shadowCoord.w > 0 && dist < shadowCoord.z)
{
shadow = 0.01; // ambient
Expand All @@ -245,9 +252,9 @@ float textureProj(vec4 shadowCoord, vec2 offset, uint cascadeIndex)
return shadow;
}

float filterPCF(vec4 sc, uint cascadeIndex)
float filterPCF(vec4 sc, vec2 offset)
{
ivec2 texDim = textureSize(sampler2D(shadowmap[cascadeIndex], tShadowSampler), 0).xy;
ivec2 texDim = textureSize(sampler2D(shadowmap, tShadowSampler), 0).xy;
float scale = 0.75;
float dx = scale * 1.0 / float(texDim.x);
float dy = scale * 1.0 / float(texDim.y);
Expand All @@ -258,7 +265,7 @@ float filterPCF(vec4 sc, uint cascadeIndex)

for (int x = -range; x <= range; x++) {
for (int y = -range; y <= range; y++) {
shadowFactor += textureProj(sc, vec2(dx*x, dy*y), cascadeIndex);
shadowFactor += textureProj(sc, vec2(dx*x, dy*y) + offset);
count++;
}
}
Expand Down Expand Up @@ -359,8 +366,8 @@ void main()
// Depth compare for shadowing
vec4 shadowCoord = (biasMat * tShadowData.cascadeViewProjMat[cascadeIndex]) * vec4(tWorldPosition.xyz, 1.0);
shadow = 0;
shadow = textureProj(shadowCoord / shadowCoord.w, vec2(0.0), cascadeIndex);
// shadow = filterPCF(shadowCoord / shadowCoord.w, cascadeIndex);
// shadow = textureProj(shadowCoord / shadowCoord.w, atCascadeOffsets[cascadeIndex]);
shadow = filterPCF(shadowCoord / shadowCoord.w, atCascadeOffsets[cascadeIndex]);
}

if(tLightData.iType != PL_LIGHT_TYPE_DIRECTIONAL)
Expand Down
Loading

0 comments on commit d59c99a

Please sign in to comment.