Skip to content

Commit 084ce48

Browse files
committed
feat: first pass environment probe blending #108
1 parent aedec4f commit 084ce48

File tree

5 files changed

+272
-95
lines changed

5 files changed

+272
-95
lines changed

extensions/pl_renderer_ext.c

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,8 +2213,8 @@ pl_renderer_outline_entities(plScene* ptScene, uint32_t uCount, plEntity* atEnti
22132213
iTextureMappingFlags,
22142214
ptMaterial->tFlags,
22152215
iObjectRenderingFlags,
2216-
pl_sb_capacity(ptScene->sbtLightData),
2217-
pl_sb_capacity(ptScene->sbtProbeData),
2216+
pl_sb_size(ptScene->sbtLightData),
2217+
pl_sb_size(ptScene->sbtProbeData),
22182218
gptData->tRuntimeOptions.tShaderDebugMode
22192219
};
22202220

@@ -2337,7 +2337,7 @@ pl_renderer_reload_scene_shaders(plScene* ptScene)
23372337
int aiLightingConstantData[] = {iSceneWideRenderingFlags, gptData->tRuntimeOptions.tShaderDebugMode, 0};
23382338
ptScene->tLightingShader = gptShaderVariant->get_shader("deferred_lighting", NULL, NULL, aiLightingConstantData, &gptData->tRenderPassLayout);
23392339
ptScene->tDeferredLightingVolumeShader = gptShaderVariant->get_shader("deferred_lighting_volume", NULL, NULL, aiLightingConstantData, &gptData->tRenderPassLayout);
2340-
aiLightingConstantData[2] = pl_sb_capacity(ptScene->sbtProbeData);
2340+
aiLightingConstantData[2] = pl_sb_size(ptScene->sbtProbeData);
23412341
ptScene->tProbeLightingShader = gptShaderVariant->get_shader("deferred_lighting", NULL, NULL, aiLightingConstantData, &gptData->tRenderPassLayout);
23422342
aiLightingConstantData[0] = gptData->tRuntimeOptions.bPunctualLighting ? (PL_RENDERING_FLAG_USE_PUNCTUAL | PL_RENDERING_FLAG_SHADOWS) : 0;
23432343
aiLightingConstantData[2] = 0;
@@ -2453,7 +2453,7 @@ pl_renderer_finalize_scene(plScene* ptScene)
24532453
int aiLightingConstantData[] = {iSceneWideRenderingFlags, gptData->tRuntimeOptions.tShaderDebugMode, 0};
24542454
ptScene->tLightingShader = gptShaderVariant->get_shader("deferred_lighting", NULL, NULL, aiLightingConstantData, &gptData->tRenderPassLayout);
24552455
ptScene->tDeferredLightingVolumeShader = gptShaderVariant->get_shader("deferred_lighting_volume", NULL, NULL, aiLightingConstantData, &gptData->tRenderPassLayout);
2456-
aiLightingConstantData[2] = pl_sb_capacity(ptScene->sbtProbeData);
2456+
aiLightingConstantData[2] = pl_sb_size(ptScene->sbtProbeData);
24572457
ptScene->tProbeLightingShader = gptShaderVariant->get_shader("deferred_lighting", NULL, NULL, aiLightingConstantData, &gptData->tRenderPassLayout);
24582458
aiLightingConstantData[0] = gptData->tRuntimeOptions.bPunctualLighting ? (PL_RENDERING_FLAG_USE_PUNCTUAL | PL_RENDERING_FLAG_SHADOWS) : 0;
24592459
aiLightingConstantData[2] = 0;
@@ -3013,7 +3013,13 @@ pl_renderer_prepare_view(plView* ptView, plCamera* ptCamera)
30133013
plEnvironmentProbeData* ptProbe = &ptScene->sbtProbeData[uProbeIndex];
30143014
plEnvironmentProbeComponent* ptProbeComp = gptECS->get_component(ptScene->ptComponentLibrary, gptData->tEnvironmentProbeComponentType, ptProbe->tEntity);
30153015
plObjectComponent* ptObject = gptECS->get_component(ptScene->ptComponentLibrary, gptData->tObjectComponentType, ptProbe->tEntity);
3016+
plTransformComponent* ptTransform = gptECS->get_component(ptScene->ptComponentLibrary, gptData->tObjectComponentType, ptObject->tTransform);
30163017
gptDraw->add_3d_aabb(ptView->pt3DDrawList, ptObject->tAABB.tMin, ptObject->tAABB.tMax, (plDrawLineOptions){.uColor = PL_COLOR_32_RGB(0.0f, 1.0f, 0.0f), .fThickness = 0.02f});
3018+
plSphere tSphere = {
3019+
.fRadius = ptProbeComp->fRange,
3020+
.tCenter = ptTransform->tTranslation
3021+
};
3022+
gptDraw->add_3d_sphere(ptView->pt3DDrawList, tSphere, 6, 6, (plDrawLineOptions){.uColor = PL_COLOR_32_LIGHT_GREY, .fThickness = 0.005f});
30173023
}
30183024
}
30193025

@@ -4216,33 +4222,6 @@ pl_renderer_render_view(plView* ptView, plCamera* ptCamera, plCamera* ptCullCame
42164222
pl_end_cpu_sample(gptProfile, 0);
42174223
}
42184224

4219-
{
4220-
const plBeginCommandInfo tPostBeginInfo = {
4221-
.uWaitSemaphoreCount = 1,
4222-
.atWaitSempahores = {gptStarter->get_current_timeline_semaphore()},
4223-
.auWaitSemaphoreValues = {gptStarter->get_current_timeline_value()},
4224-
};
4225-
4226-
plCommandBuffer* ptPostCmdBuffer = gptGfx->request_command_buffer(ptCmdPool, "tonemap");
4227-
gptGfx->begin_command_recording(ptPostCmdBuffer, &tPostBeginInfo);
4228-
4229-
ptSceneEncoder = gptGfx->begin_render_pass(ptPostCmdBuffer, ptView->tFinalRenderPass, NULL);
4230-
gptGfx->set_depth_bias(ptSceneEncoder, 0.0f, 0.0f, 0.0f);
4231-
gptDrawBackend->submit_3d_drawlist(ptView->pt3DDrawList, ptSceneEncoder, tDimensions.x, tDimensions.y, &tMVP, PL_DRAW_FLAG_REVERSE_Z_DEPTH | PL_DRAW_FLAG_DEPTH_TEST, 1);
4232-
gptDrawBackend->submit_3d_drawlist(ptView->pt3DSelectionDrawList, ptSceneEncoder, tDimensions.x, tDimensions.y, &tMVP, PL_DRAW_FLAG_REVERSE_Z_DEPTH | PL_DRAW_FLAG_DEPTH_TEST, 1);
4233-
gptGfx->end_render_pass(ptSceneEncoder);
4234-
4235-
gptGfx->end_command_recording(ptPostCmdBuffer);
4236-
4237-
const plSubmitInfo tPostSubmitInfo = {
4238-
.uSignalSemaphoreCount = 1,
4239-
.atSignalSempahores = {gptStarter->get_current_timeline_semaphore()},
4240-
.auSignalSemaphoreValues = {gptStarter->increment_current_timeline_value()}
4241-
};
4242-
gptGfx->submit_command_buffer(ptPostCmdBuffer, &tPostSubmitInfo);
4243-
gptGfx->return_command_buffer(ptPostCmdBuffer);
4244-
}
4245-
42464225
{
42474226
const plBindGroupDesc tTonemapBGDesc = {
42484227
.ptPool = gptData->aptTempGroupPools[gptGfx->get_current_frame_index()],
@@ -4327,6 +4306,33 @@ pl_renderer_render_view(plView* ptView, plCamera* ptCamera, plCamera* ptCullCame
43274306
gptGfx->return_command_buffer(ptPostCmdBuffer);
43284307
}
43294308

4309+
{
4310+
const plBeginCommandInfo tPostBeginInfo = {
4311+
.uWaitSemaphoreCount = 1,
4312+
.atWaitSempahores = {gptStarter->get_current_timeline_semaphore()},
4313+
.auWaitSemaphoreValues = {gptStarter->get_current_timeline_value()},
4314+
};
4315+
4316+
plCommandBuffer* ptPostCmdBuffer = gptGfx->request_command_buffer(ptCmdPool, "tonemap");
4317+
gptGfx->begin_command_recording(ptPostCmdBuffer, &tPostBeginInfo);
4318+
4319+
ptSceneEncoder = gptGfx->begin_render_pass(ptPostCmdBuffer, ptView->tFinalRenderPass, NULL);
4320+
gptGfx->set_depth_bias(ptSceneEncoder, 0.0f, 0.0f, 0.0f);
4321+
gptDrawBackend->submit_3d_drawlist(ptView->pt3DDrawList, ptSceneEncoder, tDimensions.x, tDimensions.y, &tMVP, PL_DRAW_FLAG_REVERSE_Z_DEPTH | PL_DRAW_FLAG_DEPTH_TEST, 1);
4322+
gptDrawBackend->submit_3d_drawlist(ptView->pt3DSelectionDrawList, ptSceneEncoder, tDimensions.x, tDimensions.y, &tMVP, PL_DRAW_FLAG_REVERSE_Z_DEPTH | PL_DRAW_FLAG_DEPTH_TEST, 1);
4323+
gptGfx->end_render_pass(ptSceneEncoder);
4324+
4325+
gptGfx->end_command_recording(ptPostCmdBuffer);
4326+
4327+
const plSubmitInfo tPostSubmitInfo = {
4328+
.uSignalSemaphoreCount = 1,
4329+
.atSignalSempahores = {gptStarter->get_current_timeline_semaphore()},
4330+
.auSignalSemaphoreValues = {gptStarter->increment_current_timeline_value()}
4331+
};
4332+
gptGfx->submit_command_buffer(ptPostCmdBuffer, &tPostSubmitInfo);
4333+
gptGfx->return_command_buffer(ptPostCmdBuffer);
4334+
}
4335+
43304336
// update stats
43314337
static double* pdVisibleOpaqueObjects = NULL;
43324338
static double* pdVisibleTransparentObjects = NULL;

extensions/pl_renderer_internal.c

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3084,40 +3084,45 @@ pl__renderer_update_probes(plScene* ptScene)
30843084
for(uint32_t i = 0; i < uVisibleTransparentDrawCount; i++)
30853085
{
30863086
const plDrawable* ptDrawable = &ptScene->sbtDrawables[ptProbe->sbuVisibleForwardEntities[uFace][i]];
3087-
plObjectComponent* ptObject = gptECS->get_component(ptScene->ptComponentLibrary, gptData->tObjectComponentType, ptDrawable->tEntity);
3088-
plTransformComponent* ptTransform = gptECS->get_component(ptScene->ptComponentLibrary, tTransformComponentType, ptObject->tTransform);
3089-
3090-
plDynamicBinding tDynamicBinding = pl__allocate_dynamic_data(ptDevice);
3091-
3092-
plGpuDynData* ptDynamicData = (plGpuDynData*)tDynamicBinding.pcData;
3093-
ptDynamicData->iDataOffset = ptDrawable->uDataOffset;
3094-
ptDynamicData->iVertexOffset = ptDrawable->uDynamicVertexOffset;
3095-
ptDynamicData->iMaterialIndex = ptDrawable->uMaterialIndex;
3096-
ptDynamicData->uGlobalIndex = uFace;
30973087

3098-
pl_add_to_draw_stream(ptStream, (plDrawStreamData)
3088+
if(ptDrawable->uInstanceCount != 0)
30993089
{
3100-
.tShader = ptDrawable->tEnvShader,
3101-
.auDynamicBuffers = {
3102-
tDynamicBinding.uBufferHandle
3103-
},
3104-
.atVertexBuffers = {
3105-
ptScene->tVertexBuffer,
3106-
},
3107-
.tIndexBuffer = ptDrawable->tIndexBuffer,
3108-
.uIndexOffset = ptDrawable->uIndexOffset,
3109-
.uTriangleCount = ptDrawable->uTriangleCount,
3110-
.uVertexOffset = ptDrawable->uStaticVertexOffset,
3111-
.atBindGroups = {
3112-
ptScene->atBindGroups[uFrameIdx],
3113-
tViewBG
3114-
},
3115-
.auDynamicBufferOffsets = {
3116-
tDynamicBinding.uByteOffset
3117-
},
3118-
.uInstanceOffset = ptDrawable->uTransformIndex,
3119-
.uInstanceCount = ptDrawable->uInstanceCount
3120-
});
3090+
3091+
plObjectComponent* ptObject = gptECS->get_component(ptScene->ptComponentLibrary, gptData->tObjectComponentType, ptDrawable->tEntity);
3092+
plTransformComponent* ptTransform = gptECS->get_component(ptScene->ptComponentLibrary, tTransformComponentType, ptObject->tTransform);
3093+
3094+
plDynamicBinding tDynamicBinding = pl__allocate_dynamic_data(ptDevice);
3095+
3096+
plGpuDynData* ptDynamicData = (plGpuDynData*)tDynamicBinding.pcData;
3097+
ptDynamicData->iDataOffset = ptDrawable->uDataOffset;
3098+
ptDynamicData->iVertexOffset = ptDrawable->uDynamicVertexOffset;
3099+
ptDynamicData->iMaterialIndex = ptDrawable->uMaterialIndex;
3100+
ptDynamicData->uGlobalIndex = uFace;
3101+
3102+
pl_add_to_draw_stream(ptStream, (plDrawStreamData)
3103+
{
3104+
.tShader = ptDrawable->tEnvShader,
3105+
.auDynamicBuffers = {
3106+
tDynamicBinding.uBufferHandle
3107+
},
3108+
.atVertexBuffers = {
3109+
ptScene->tVertexBuffer,
3110+
},
3111+
.tIndexBuffer = ptDrawable->tIndexBuffer,
3112+
.uIndexOffset = ptDrawable->uIndexOffset,
3113+
.uTriangleCount = ptDrawable->uTriangleCount,
3114+
.uVertexOffset = ptDrawable->uStaticVertexOffset,
3115+
.atBindGroups = {
3116+
ptScene->atBindGroups[uFrameIdx],
3117+
tViewBG
3118+
},
3119+
.auDynamicBufferOffsets = {
3120+
tDynamicBinding.uByteOffset
3121+
},
3122+
.uInstanceOffset = ptDrawable->uTransformIndex,
3123+
.uInstanceCount = ptDrawable->uInstanceCount
3124+
});
3125+
}
31213126
}
31223127

31233128
for(uint32_t i = 0; i < uVisibleTransmissionDrawCount; i++)

shaders/deferred_lighting.frag

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,37 +101,88 @@ void main()
101101
// Calculate lighting contribution from image based lighting source (IBL)
102102
if(bool(iRenderingFlags & PL_RENDERING_FLAG_USE_IBL) && iProbeCount > 0)
103103
{
104-
int iProbeIndex = 0;
105-
float fCurrentDistance = 10000.0;
106-
for(int i = iProbeCount - 1; i > -1; i--)
104+
105+
int aiActiveProbes[3];
106+
aiActiveProbes[0] = -1;
107+
aiActiveProbes[1] = -1;
108+
aiActiveProbes[2] = -1;
109+
110+
float weights[3];
111+
weights[0] = 0.0;
112+
weights[1] = 0.0;
113+
weights[2] = 0.0;
114+
115+
float distances[3];
116+
distances[0] = 10000.0;
117+
distances[1] = 10000.0;
118+
distances[2] = 10000.0;
119+
120+
int K = 0;
121+
122+
for(int i = 0; i < iProbeCount; i++)
107123
{
108124
vec3 tDist = tProbeData.atData[i].tPosition - tWorldPosition.xyz;
109125
tDist = tDist * tDist;
110126
float fDistSqr = tDist.x + tDist.y + tDist.z;
111-
if(fDistSqr <= tProbeData.atData[i].fRangeSqr && fDistSqr < fCurrentDistance)
127+
128+
if(fDistSqr <= tProbeData.atData[i].fRangeSqr)
112129
{
113-
iProbeIndex = i;
114-
fCurrentDistance = fDistSqr;
130+
131+
int iFurthest = 0;
132+
for(int j = 0; j < 3; j++)
133+
{
134+
if(distances[j] > distances[iFurthest])
135+
{
136+
iFurthest = j;
137+
}
138+
}
139+
140+
if(distances[iFurthest] > fDistSqr)
141+
{
142+
K++;
143+
aiActiveProbes[iFurthest] = i;
144+
distances[iFurthest] = fDistSqr;
145+
}
115146
}
116147
}
117-
if(iProbeIndex > -1)
148+
149+
int iClosestIndex = 0;
150+
float maxDis = distances[0];
151+
for(int j = 0; j < 3; j++)
118152
{
153+
if(distances[j] < distances[iClosestIndex])
154+
{
155+
iClosestIndex = j;
156+
}
157+
}
119158

159+
160+
K = min(K, 3);
161+
vec3 R = reflect(-v, n);
162+
float summing = computeProbeWeights(tWorldPosition.xyz, R, 2.0, K, aiActiveProbes, weights);
163+
164+
int iClosestProbeIndex = aiActiveProbes[iClosestIndex];
165+
int iMips2 = textureQueryLevels(samplerCube(atCubeTextures[nonuniformEXT(tProbeData.atData[iClosestProbeIndex].uGGXEnvSampler)], tSamplerNearestRepeat));
166+
167+
f_specular_metal = getIBLRadianceGGX(n, v, materialInfo.perceptualRoughness, iMips2, tWorldPosition.xyz, iClosestProbeIndex);
168+
f_specular_dielectric = f_specular_metal;
169+
170+
for(int i = 0; i < K; i++)
171+
{
172+
int iProbeIndex = aiActiveProbes[i];
120173
f_diffuse = getDiffuseLight(n, iProbeIndex) * tBaseColor.rgb;
121174

122175
int iMips = textureQueryLevels(samplerCube(atCubeTextures[nonuniformEXT(tProbeData.atData[iProbeIndex].uGGXEnvSampler)], tSamplerNearestRepeat));
123-
f_specular_metal = getIBLRadianceGGX(n, v, materialInfo.perceptualRoughness, iMips, tWorldPosition.xyz, iProbeIndex);
124-
f_specular_dielectric = f_specular_metal;
125176

126177
// Calculate fresnel mix for IBL
127178

128179
vec3 f_metal_fresnel_ibl = getIBLGGXFresnel(n, v, materialInfo.perceptualRoughness, tBaseColor.rgb, 1.0, iProbeIndex);
129180
f_metal_brdf_ibl = f_metal_fresnel_ibl * f_specular_metal;
130181

131-
vec3 f_dielectric_fresnel_ibl = getIBLGGXFresnel(n, v, materialInfo.perceptualRoughness, materialInfo.f0_dielectric, materialInfo.specularWeight, iProbeIndex);
182+
vec3 f_dielectric_fresnel_ibl = getIBLGGXFresnel(n, v, materialInfo.perceptualRoughness, materialInfo.f0_dielectric, materialInfo.specularWeight, i);
132183
f_dielectric_brdf_ibl = mix(f_diffuse, f_specular_dielectric, f_dielectric_fresnel_ibl);
133184

134-
color = mix(f_dielectric_brdf_ibl, f_metal_brdf_ibl, materialInfo.metallic);
185+
color += weights[i] * mix(f_dielectric_brdf_ibl, f_metal_brdf_ibl, materialInfo.metallic);
135186
}
136187
}
137188

0 commit comments

Comments
 (0)