Skip to content

Commit 7c86214

Browse files
committed
feat: add shader debug views to renderer ext
1 parent 17eaa17 commit 7c86214

File tree

12 files changed

+283
-41
lines changed

12 files changed

+283
-41
lines changed

editor/app.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
442442
plModelLoaderData tLoaderData0 = {0};
443443
gptModelLoader->load_gltf(ptAppData->ptComponentLibrary, "/models/gltf/humanoid/model.gltf", NULL, &tLoaderData0);
444444
gptModelLoader->load_gltf(ptAppData->ptComponentLibrary, "/gltf/Sponza/glTF/Sponza.gltf", NULL, &tLoaderData0);
445+
// gptModelLoader->load_gltf(ptAppData->ptComponentLibrary, "/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf", NULL, &tLoaderData0);
445446
gptRenderer->add_drawable_objects_to_scene(ptAppData->ptScene, tLoaderData0.uObjectCount, tLoaderData0.atObjects);
446447
gptModelLoader->free_data(&tLoaderData0);
447448
gptRenderer->finalize_scene(ptAppData->ptScene);
@@ -801,16 +802,18 @@ pl__show_editor_window(plAppData* ptAppData)
801802
gptStarter->deactivate_vsync();
802803
}
803804

804-
bool abTonemap[3] = {0};
805+
805806
static const char* apcTonemapText[] = {
806807
"None",
808+
"Simple",
807809
"ACES",
808810
"Reinhard",
809811
};
812+
bool abTonemap[PL_ARRAYSIZE(apcTonemapText)] = {0};
810813
abTonemap[ptRuntimeOptions->tTonemapMode] = true;
811814
if(gptUI->begin_combo("Tonemapping", apcTonemapText[ptRuntimeOptions->tTonemapMode], PL_UI_COMBO_FLAGS_HEIGHT_REGULAR))
812815
{
813-
for(uint32_t i = 0; i < 3; i++)
816+
for(uint32_t i = 0; i < PL_ARRAYSIZE(apcTonemapText); i++)
814817
{
815818
if(gptUI->selectable(apcTonemapText[i], &abTonemap[i], 0))
816819
{
@@ -820,14 +823,53 @@ pl__show_editor_window(plAppData* ptAppData)
820823
}
821824
gptUI->end_combo();
822825
}
826+
827+
bool bReloadShaders = false;
828+
829+
830+
static const char* apcShaderDebugModeText[] = {
831+
"None",
832+
"Base Color",
833+
"Metallic",
834+
"Roughness",
835+
"Alpha",
836+
"Emissive",
837+
"Occlusion",
838+
"Shading Normal",
839+
"Texture Normal",
840+
"Geometry Normal",
841+
"Geometry Tangent",
842+
"Geometry Bitangent",
843+
"UV 0"
844+
};
845+
bool abShaderDebugMode[PL_ARRAYSIZE(apcShaderDebugModeText)] = {0};
846+
abShaderDebugMode[ptRuntimeOptions->tShaderDebugMode] = true;
847+
if(gptUI->begin_combo("Shader Debug Mode", apcShaderDebugModeText[ptRuntimeOptions->tShaderDebugMode], PL_UI_COMBO_FLAGS_HEIGHT_REGULAR))
848+
{
849+
for(uint32_t i = 0; i < PL_ARRAYSIZE(apcShaderDebugModeText); i++)
850+
{
851+
if(gptUI->selectable(apcShaderDebugModeText[i], &abShaderDebugMode[i], 0))
852+
{
853+
bReloadShaders = true;
854+
if(i == 0)
855+
ptRuntimeOptions->tTonemapMode = PL_TONEMAP_MODE_SIMPLE;
856+
else
857+
ptRuntimeOptions->tTonemapMode = PL_TONEMAP_MODE_NONE;
858+
ptRuntimeOptions->tShaderDebugMode = i;
859+
gptUI->close_current_popup();
860+
}
861+
}
862+
gptUI->end_combo();
863+
}
864+
823865
gptUI->slider_float("Exposure", &ptRuntimeOptions->fExposure, 0.0f, 3.0f, 0);
824866
gptUI->slider_float("Brightness", &ptRuntimeOptions->fBrightness, -1.0f, 1.0f, 0);
825867
gptUI->slider_float("Contrast", &ptRuntimeOptions->fContrast, 0.0f, 2.0f, 0);
826868
gptUI->slider_float("Saturation", &ptRuntimeOptions->fSaturation, 0.0f, 2.0f, 0);
827869

828870
gptUI->checkbox("Show Origin", &ptRuntimeOptions->bShowOrigin);
829871
gptUI->checkbox("Show BVH", &ptAppData->bShowBVH);
830-
bool bReloadShaders = false;
872+
831873
if(gptUI->checkbox("Wireframe", &ptRuntimeOptions->bWireframe)) bReloadShaders = true;
832874
if(gptUI->checkbox("MultiViewport Shadows", &ptRuntimeOptions->bMultiViewportShadows)) bReloadShaders = true;
833875
if(gptUI->checkbox("Image Based Lighting", &ptRuntimeOptions->bImageBasedLighting)) bReloadShaders = true;

editor/editor.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,18 +1033,36 @@ pl__show_editor_window(plAppData* ptAppData)
10331033
}
10341034
static const char* apcTonemapText[] = {
10351035
"None",
1036+
"Simple",
10361037
"ACES",
10371038
"Reinhard",
10381039
};
1039-
ImGui::Combo("Tonemapping", &ptRuntimeOptions->tTonemapMode, apcTonemapText, 3);
1040+
ImGui::Combo("Tonemapping", &ptRuntimeOptions->tTonemapMode, apcTonemapText, PL_ARRAYSIZE(apcTonemapText));
1041+
1042+
static const char* apcShaderDebugModeText[] = {
1043+
"None",
1044+
"Base Color",
1045+
"Metallic",
1046+
"Roughness",
1047+
"Alpha",
1048+
"Emissive",
1049+
"Occlusion",
1050+
"Shading Normal",
1051+
"Texture Normal",
1052+
"Geometry Normal",
1053+
"Geometry Tangent",
1054+
"Geometry Bitangent",
1055+
"UV 0"
1056+
};
1057+
bool bReloadShaders = false;
1058+
if(ImGui::Combo("Shader Debug Mode", &ptRuntimeOptions->tShaderDebugMode, apcShaderDebugModeText, PL_ARRAYSIZE(apcShaderDebugModeText))) bReloadShaders = true;
10401059
ImGui::SliderFloat("Exposure", &ptRuntimeOptions->fExposure, 0.0f, 3.0f);
10411060
ImGui::SliderFloat("Brightness", &ptRuntimeOptions->fBrightness, -1.0f, 1.0f);
10421061
ImGui::SliderFloat("Contrast", &ptRuntimeOptions->fContrast, 0.0f, 2.0f);
10431062
ImGui::SliderFloat("Saturation", &ptRuntimeOptions->fSaturation, 0.0f, 2.0f);
10441063
ImGui::Checkbox("Show Origin", &ptRuntimeOptions->bShowOrigin);
10451064
ImGui::Checkbox("Show BVH", &ptAppData->bShowBVH);
10461065
ImGui::Checkbox("Show Skybox", &ptAppData->bShowSkybox);
1047-
bool bReloadShaders = false;
10481066
if(ImGui::Checkbox("Wireframe", &ptRuntimeOptions->bWireframe)) bReloadShaders = true;
10491067
if(ImGui::Checkbox("MultiViewport Shadows", &ptRuntimeOptions->bMultiViewportShadows)) bReloadShaders = true;
10501068
if(ImGui::Checkbox("Image Based Lighting", &ptRuntimeOptions->bImageBasedLighting)) bReloadShaders = true;

extensions/pl_renderer_ext.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ pl_renderer_initialize(plRendererSettings tSettings)
261261
gptData->tRuntimeOptions.fBrightness = 0.0f;
262262
gptData->tRuntimeOptions.fContrast = 1.0f;
263263
gptData->tRuntimeOptions.fSaturation = 1.0f;
264-
gptData->tRuntimeOptions.tTonemapMode = PL_TONEMAP_MODE_NONE;
264+
gptData->tRuntimeOptions.tTonemapMode = PL_TONEMAP_MODE_SIMPLE;
265265

266266
gptResource->initialize((plResourceManagerInit){.ptDevice = gptData->ptDevice, .uMaxTextureResolution = tSettings.uMaxTextureResolution});
267267

@@ -1771,12 +1771,14 @@ pl_renderer_outline_entities(plScene* ptScene, uint32_t uCount, plEntity* atEnti
17711771
iObjectRenderingFlags,
17721772
pl_sb_capacity(ptScene->sbtLightData),
17731773
pl_sb_capacity(ptScene->sbtProbeData),
1774+
gptData->tRuntimeOptions.tShaderDebugMode
17741775
};
17751776

17761777
int aiGBufferFragmentConstantData0[] = {
17771778
(int)ptMesh->ulVertexStreamMask,
17781779
iTextureMappingFlags,
1779-
PL_INFO_MATERIAL_METALLICROUGHNESS
1780+
PL_INFO_MATERIAL_METALLICROUGHNESS,
1781+
gptData->tRuntimeOptions.tShaderDebugMode
17801782
};
17811783

17821784
int aiVertexConstantData0[] = {
@@ -1881,7 +1883,7 @@ pl_renderer_reload_scene_shaders(plScene* ptScene)
18811883

18821884
plLightComponent* ptLights = NULL;
18831885
const uint32_t uLightCount = gptECS->get_components(ptScene->ptComponentLibrary, gptData->tLightComponentType, (void**)&ptLights, NULL);
1884-
int aiLightingConstantData[] = {iSceneWideRenderingFlags, pl_sb_capacity(ptScene->sbtLightData), pl_sb_size(ptScene->sbtProbeData)};
1886+
int aiLightingConstantData[] = {iSceneWideRenderingFlags, pl_sb_capacity(ptScene->sbtLightData), pl_sb_size(ptScene->sbtProbeData), gptData->tRuntimeOptions.tShaderDebugMode};
18851887
ptScene->tLightingShader = gptShaderVariant->get_shader("deferred_lighting", NULL, NULL, aiLightingConstantData, &gptData->tRenderPassLayout);
18861888
aiLightingConstantData[0] = gptData->tRuntimeOptions.bPunctualLighting ? (PL_RENDERING_FLAG_USE_PUNCTUAL | PL_RENDERING_FLAG_SHADOWS) : 0;
18871889
ptScene->tEnvLightingShader = gptShaderVariant->get_shader("deferred_lighting", NULL, NULL, aiLightingConstantData, &gptData->tRenderPassLayout);
@@ -1967,7 +1969,7 @@ pl_renderer_finalize_scene(plScene* ptScene)
19671969
iSceneWideRenderingFlags |= PL_RENDERING_FLAG_USE_IBL;
19681970

19691971
// create lighting shader
1970-
int aiLightingConstantData[] = {iSceneWideRenderingFlags, pl_sb_capacity(ptScene->sbtLightData), pl_sb_size(ptScene->sbtProbeData)};
1972+
int aiLightingConstantData[] = {iSceneWideRenderingFlags, pl_sb_capacity(ptScene->sbtLightData), pl_sb_size(ptScene->sbtProbeData), gptData->tRuntimeOptions.tShaderDebugMode};
19711973
ptScene->tLightingShader = gptShaderVariant->get_shader("deferred_lighting", NULL, NULL, aiLightingConstantData, &gptData->tRenderPassLayout);
19721974
aiLightingConstantData[0] = gptData->tRuntimeOptions.bPunctualLighting ? (PL_RENDERING_FLAG_USE_PUNCTUAL | PL_RENDERING_FLAG_SHADOWS) : 0;
19731975
ptScene->tEnvLightingShader = gptShaderVariant->get_shader("deferred_lighting", NULL, NULL, aiLightingConstantData, &gptData->tRenderPassLayout);

extensions/pl_renderer_ext.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,23 +229,25 @@ typedef struct _plRendererSettings
229229

230230
typedef struct _plRendererRuntimeOptions
231231
{
232-
bool bShowProbes;
233-
bool bWireframe;
234-
bool bShowOrigin;
235-
bool bShowSelectedBoundingBox;
236-
bool bMultiViewportShadows;
237-
bool bImageBasedLighting;
238-
bool bPunctualLighting;
239-
float fShadowConstantDepthBias;
240-
float fShadowSlopeDepthBias;
241-
uint32_t uOutlineWidth;
232+
bool bShowProbes;
233+
bool bWireframe;
234+
bool bShowOrigin;
235+
bool bShowSelectedBoundingBox;
236+
bool bMultiViewportShadows;
237+
bool bImageBasedLighting;
238+
bool bPunctualLighting;
239+
float fShadowConstantDepthBias;
240+
float fShadowSlopeDepthBias;
241+
uint32_t uOutlineWidth;
242+
plShaderDebugMode tShaderDebugMode;
242243

243244
// tonemapping
244245
plTonemapMode tTonemapMode;
245246
float fExposure;
246247
float fBrightness;
247248
float fContrast;
248249
float fSaturation;
250+
249251
} plRendererRuntimeOptions;
250252

251253
typedef struct _plTextureMap

extensions/pl_renderer_internal.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3532,12 +3532,14 @@ pl__renderer_set_drawable_shaders(plScene* ptScene)
35323532
iObjectRenderingFlags,
35333533
pl_sb_capacity(ptScene->sbtLightData),
35343534
pl_sb_size(ptScene->sbtProbeData),
3535+
gptData->tRuntimeOptions.tShaderDebugMode
35353536
};
35363537

35373538
int aiGBufferFragmentConstantData0[] = {
35383539
(int)ptMesh->ulVertexStreamMask,
35393540
iTextureMappingFlags,
3540-
PL_INFO_MATERIAL_METALLICROUGHNESS
3541+
PL_INFO_MATERIAL_METALLICROUGHNESS,
3542+
gptData->tRuntimeOptions.tShaderDebugMode
35413543
};
35423544

35433545
int aiVertexConstantData0[] = {

shaders/deferred_lighting.frag

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#extension GL_ARB_separate_shader_objects : enable
33
#extension GL_EXT_nonuniform_qualifier : enable
44

5+
#include "pl_shader_interop_renderer.h"
56
#include "bg_scene.inc"
67
#include "bg_view.inc"
78
#include "math.glsl"
@@ -14,6 +15,7 @@
1415
layout(constant_id = 0) const int iRenderingFlags = 0;
1516
layout(constant_id = 1) const int iLightCount = 0;
1617
layout(constant_id = 2) const int iProbeCount = 0;
18+
layout(constant_id = 3) const int tShaderDebugMode = 0;
1719

1820
//-----------------------------------------------------------------------------
1921
// [SECTION] bind group 2
@@ -353,15 +355,49 @@ void main()
353355

354356
// Layer blending
355357

356-
outColor = vec4(color.rgb, tBaseColor.a);
358+
outColor.a = tBaseColor.a;
359+
360+
if(tShaderDebugMode == PL_SHADER_DEBUG_MODE_NONE)
361+
{
362+
outColor.rgb = color.rgb;
363+
}
357364

358-
// outColor = vec4(ndcSpace.rgb, tBaseColor.a);
359-
// outColor = vec4(gl_FragCoord.x, 0, 0, tBaseColor.a);
360-
// outColor = vec4(v.r, v.g, v.b, tBaseColor.a);
361-
// outColor = vec4(v.r, v.g, v.b, tBaseColor.a);
362-
// outColor = vec4(tWorldPosition.rgb, tBaseColor.a);
363-
// outColor = vec4(tViewPosition.rgb, tBaseColor.a);
364-
// outColor = vec4(n, tBaseColor.a);
365+
if(tShaderDebugMode == PL_SHADER_DEBUG_BASE_COLOR)
366+
{
367+
outColor = tBaseColor;
368+
}
369+
370+
if(tShaderDebugMode == PL_SHADER_DEBUG_SHADING_NORMAL)
371+
{
372+
outColor = vec4((n + 1.0) / 2.0, tBaseColor.a);
373+
}
374+
375+
if(tShaderDebugMode == PL_SHADER_DEBUG_METALLIC)
376+
{
377+
outColor.rgb = vec3(materialInfo.metallic);
378+
}
379+
380+
if(tShaderDebugMode == PL_SHADER_DEBUG_ROUGHNESS)
381+
{
382+
outColor.rgb = vec3(materialInfo.perceptualRoughness);
383+
}
384+
385+
if(tShaderDebugMode == PL_SHADER_DEBUG_ALPHA)
386+
{
387+
outColor.rgb = vec3(tBaseColor.a);
388+
outColor.a = 1.0;
389+
}
390+
391+
if(tShaderDebugMode == PL_SHADER_DEBUG_OCCLUSION)
392+
{
393+
outColor.rgb = vec3(ao);
394+
}
395+
396+
if(tShaderDebugMode > PL_SHADER_DEBUG_SHADING_NORMAL)
397+
{
398+
outColor.rgb = vec3(tBaseColor.rgb);
399+
outColor.a = 1.0;
400+
}
365401

366402
// if(gl_FragCoord.x < 1400.0)
367403
// {

0 commit comments

Comments
 (0)