Skip to content

Commit

Permalink
chore: add default shader directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan hoffstadt committed Feb 18, 2025
1 parent 957d181 commit 2127b52
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 49 deletions.
23 changes: 18 additions & 5 deletions extensions/pl_renderer_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -2233,6 +2233,7 @@ pl_refr_reload_scene_shaders(uint32_t uSceneHandle)

pl__refr_set_drawable_shaders(uSceneHandle);
pl__refr_sort_drawables(uSceneHandle);
pl_end_cpu_sample(gptProfile, 0);
}

static void
Expand Down Expand Up @@ -4160,8 +4161,24 @@ pl_show_graphics_options(const char* pcTitle)

if(bReloadShaders)
{
plShaderOptions tOriginalOptions = *gptShader->get_options();

plShaderOptions tNewDefaultShaderOptions = {
.apcIncludeDirectories = {
"../shaders/"
},
.apcDirectories = {
"../shaders/"
},
.tFlags = PL_SHADER_FLAGS_AUTO_OUTPUT | PL_SHADER_FLAGS_INCLUDE_DEBUG | PL_SHADER_FLAGS_ALWAYS_COMPILE

};
gptShader->set_options(&tNewDefaultShaderOptions);

for(uint32_t i = 0; i < pl_sb_size(gptData->sbtScenes); i++)
pl_refr_reload_scene_shaders(i);

gptShader->set_options(&tOriginalOptions);
}
gptUI->checkbox("Frustum Culling", &gptData->bFrustumCulling);
gptUI->checkbox("Draw All Bounding Boxes", &gptData->bDrawAllBoundingBoxes);
Expand All @@ -4173,8 +4190,6 @@ pl_show_graphics_options(const char* pcTitle)

for(uint32_t i = 0; i < pl_sb_size(gptData->sbtScenes); i++)
{
if(bReloadShaders)
pl_refr_reload_scene_shaders(i);

if(gptUI->tree_node("Scene", 0))
{
Expand Down Expand Up @@ -4255,9 +4270,7 @@ pl_load_renderer_ext(plApiRegistryI* ptApiRegistry, bool bReload)
gptGfx = pl_get_api_latest(ptApiRegistry, plGraphicsI);
gptUI = pl_get_api_latest(ptApiRegistry, plUiI);
gptResource = pl_get_api_latest(ptApiRegistry, plResourceI);
#ifdef PL_CORE_EXTENSION_INCLUDE_SHADER
gptShader = pl_get_api_latest(ptApiRegistry, plShaderI);
#endif
gptShader = pl_get_api_latest(ptApiRegistry, plShaderI);

if(bReload)
{
Expand Down
41 changes: 21 additions & 20 deletions extensions/pl_renderer_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1823,7 +1823,7 @@ pl_refr_generate_cascaded_shadow_map(plRenderEncoder* ptEncoder, plCommandBuffer
tDynamicBinding.uByteOffset
},
.uInstanceOffset = 0,
.uInstanceCount = uCascadeCount
.uInstanceCount = 1
});
}

Expand Down Expand Up @@ -2213,27 +2213,28 @@ pl_refr_create_global_shaders(void)
gptData->tForwardShader = gptGfx->create_shader(gptData->ptDevice, &tForwardShaderDescription);


static const plShaderMacroDefinition tDefinition = {
.pcName = "PL_MULTIPLE_VIEWPORTS",
.szNameLength = 21
};
static plShaderOptions tShadowShaderOptions = {
.uMacroDefinitionCount = 1,
.ptMacroDefinitions = &tDefinition,
.apcIncludeDirectories = {
"../shaders/"
},
.apcDirectories = {
"../shaders/"
},
.tFlags = PL_SHADER_FLAGS_AUTO_OUTPUT
};
#ifndef PL_OFFLINE_SHADERS_ONLY
tShadowShaderOptions.tFlags |= PL_SHADER_FLAGS_ALWAYS_COMPILE | PL_SHADER_FLAGS_INCLUDE_DEBUG;
#endif
// static const plShaderMacroDefinition tDefinition = {
// .pcName = "PL_MULTIPLE_VIEWPORTS",
// .szNameLength = 21
// };
// static plShaderOptions tShadowShaderOptions = {
// .uMacroDefinitionCount = 1,
// .ptMacroDefinitions = &tDefinition,
// .apcIncludeDirectories = {
// "../shaders/"
// },
// .apcDirectories = {
// "./",
// "../shaders/"
// },
// .tFlags = PL_SHADER_FLAGS_AUTO_OUTPUT
// };
// #ifndef PL_OFFLINE_SHADERS_ONLY
// tShadowShaderOptions.tFlags |= PL_SHADER_FLAGS_ALWAYS_COMPILE | PL_SHADER_FLAGS_INCLUDE_DEBUG;
// #endif

plShaderModule tShadowPixelShader = gptShader->load_glsl("shadow.frag", "main", NULL, NULL);
plShaderModule tVertexShader = gptShader->load_glsl("shadow.vert", "main", NULL, gptData->bMultiViewportShadows ? &tShadowShaderOptions : NULL);
plShaderModule tVertexShader = gptShader->load_glsl("shadow.vert", "main", NULL, NULL);

plShaderDesc tShadowShaderDescription = {
.tPixelShader = tShadowPixelShader,
Expand Down
60 changes: 58 additions & 2 deletions extensions/pl_shader_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ pl_initialize_shader_ext(const plShaderOptions* ptShaderOptions)
gptShaderCtx->tDefaultShaderOptions.apcIncludeDirectories[0] = "./";
gptShaderCtx->tDefaultShaderOptions._uIncludeDirectoriesCount = 1;

gptShaderCtx->tDefaultShaderOptions._uDirectoriesCount = 0;
gptShaderCtx->tDefaultShaderOptions.apcDirectories[0] = "./";
gptShaderCtx->tDefaultShaderOptions._uDirectoriesCount = 1;

if(ptShaderOptions)
{
Expand All @@ -168,7 +169,7 @@ pl_initialize_shader_ext(const plShaderOptions* ptShaderOptions)
for(uint32_t i = 0; i < PL_MAX_SHADER_DIRECTORIES; i++)
{
if(ptShaderOptions->apcDirectories[i])
gptShaderCtx->tDefaultShaderOptions.apcDirectories[i] = ptShaderOptions->apcDirectories[i];
gptShaderCtx->tDefaultShaderOptions.apcDirectories[i + 1] = ptShaderOptions->apcDirectories[i];
else
break;
gptShaderCtx->tDefaultShaderOptions._uDirectoriesCount++;
Expand All @@ -184,6 +185,59 @@ pl_initialize_shader_ext(const plShaderOptions* ptShaderOptions)
return true;
}

const plShaderOptions*
pl_shader_get_options(void)
{
if(!gptShaderCtx->bInitialized)
return NULL;
return &gptShaderCtx->tDefaultShaderOptions;
}

static void
pl_shader_set_options(const plShaderOptions* ptShaderOptions)
{

if(!gptShaderCtx->bInitialized)
return;

gptShaderCtx->tDefaultShaderOptions.tFlags = ptShaderOptions->tFlags;
if(ptShaderOptions->tFlags & PL_SHADER_FLAGS_AUTO_OUTPUT)
{
#ifdef PL_METAL_BACKEND
gptShaderCtx->tDefaultShaderOptions.tFlags |= PL_SHADER_FLAGS_METAL_OUTPUT;
#endif
#ifdef PL_VULKAN_BACKEND
gptShaderCtx->tDefaultShaderOptions.tFlags |= PL_SHADER_FLAGS_SPIRV_OUTPUT;
#endif
}
gptShaderCtx->tDefaultShaderOptions.apcIncludeDirectories[0] = "./";
gptShaderCtx->tDefaultShaderOptions._uIncludeDirectoriesCount = 1;

gptShaderCtx->tDefaultShaderOptions.apcDirectories[0] = "./";
gptShaderCtx->tDefaultShaderOptions._uDirectoriesCount = 1;

if(ptShaderOptions)
{
for(uint32_t i = 0; i < PL_MAX_SHADER_INCLUDE_DIRECTORIES; i++)
{
if(ptShaderOptions->apcIncludeDirectories[i])
gptShaderCtx->tDefaultShaderOptions.apcIncludeDirectories[i + 1] = ptShaderOptions->apcIncludeDirectories[i];
else
break;
gptShaderCtx->tDefaultShaderOptions._uIncludeDirectoriesCount++;
}

for(uint32_t i = 0; i < PL_MAX_SHADER_DIRECTORIES; i++)
{
if(ptShaderOptions->apcDirectories[i])
gptShaderCtx->tDefaultShaderOptions.apcDirectories[i + 1] = ptShaderOptions->apcDirectories[i];
else
break;
gptShaderCtx->tDefaultShaderOptions._uDirectoriesCount++;
}
}
}

static void
pl_write_to_disk(const char* pcShader, const plShaderModule* ptModule)
{
Expand Down Expand Up @@ -563,6 +617,8 @@ pl_load_shader_ext(plApiRegistryI* ptApiRegistry, bool bReload)
{
const plShaderI tApi = {
.initialize = pl_initialize_shader_ext,
.set_options = pl_shader_set_options,
.get_options = pl_shader_get_options,
.load_glsl = pl_load_glsl,
.compile_glsl = pl_compile_glsl,
.write_to_disk = pl_write_to_disk,
Expand Down
6 changes: 5 additions & 1 deletion extensions/pl_shader_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ typedef struct _plShaderModule plShaderModule; // pl_graphics_ext.h
typedef struct _plShaderI
{
// setup
bool (*initialize)(const plShaderOptions*);
bool (*initialize) (const plShaderOptions*);

// settings
void (*set_options)(const plShaderOptions*);
const plShaderOptions* (*get_options)(void);

// load shader (compile if not already compiled)
plShaderModule (*load_glsl)(const char* shader, const char* entryFunc, const char* file, plShaderOptions*);
Expand Down
16 changes: 4 additions & 12 deletions extensions/pl_unity_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ Index of this file:

#include "pl.h"

#ifdef PL_CORE_EXTENSION_INCLUDE_SHADER
#include "pl_shader_ext.c"
#endif

#include "pl_shader_ext.c"
#include "pl_image_ext.c"
#include "pl_rect_pack_ext.c"
#include "pl_stats_ext.c"
Expand Down Expand Up @@ -88,11 +85,8 @@ pl_load_ext(plApiRegistryI* ptApiRegistry, bool bReload)
pl_load_draw_ext(ptApiRegistry, bReload);
pl_load_draw_backend_ext(ptApiRegistry, bReload);
pl_load_ui_ext(ptApiRegistry, bReload);

#ifdef PL_CORE_EXTENSION_INCLUDE_SHADER
pl_load_shader_ext(ptApiRegistry, bReload);
gptShader = pl_get_api_latest(ptApiRegistry, plShaderI);
#endif
pl_load_shader_ext(ptApiRegistry, bReload);
gptShader = pl_get_api_latest(ptApiRegistry, plShaderI);

pl_load_ecs_ext(ptApiRegistry, bReload);
pl_load_resource_ext(ptApiRegistry, bReload);
Expand All @@ -107,9 +101,7 @@ pl_load_ext(plApiRegistryI* ptApiRegistry, bool bReload)
PL_EXPORT void
pl_unload_ext(plApiRegistryI* ptApiRegistry, bool bReload)
{
#ifdef PL_CORE_EXTENSION_INCLUDE_SHADER
pl_unload_shader_ext(ptApiRegistry, bReload);
#endif
pl_unload_shader_ext(ptApiRegistry, bReload);
pl_unload_job_ext(ptApiRegistry, bReload);
pl_unload_image_ext(ptApiRegistry, bReload);
pl_unload_rect_pack_ext(ptApiRegistry, bReload);
Expand Down
4 changes: 2 additions & 2 deletions sandbox/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
.apcDirectories = {
"../shaders/"
},
.tFlags = PL_SHADER_FLAGS_AUTO_OUTPUT
.tFlags = PL_SHADER_FLAGS_AUTO_OUTPUT | PL_SHADER_FLAGS_INCLUDE_DEBUG

};
#ifndef PL_OFFLINE_SHADERS_ONLY
tDefaultShaderOptions.tFlags |= PL_SHADER_FLAGS_ALWAYS_COMPILE | PL_SHADER_FLAGS_INCLUDE_DEBUG;
tDefaultShaderOptions.tFlags |= PL_SHADER_FLAGS_ALWAYS_COMPILE;
#endif
gptShader->initialize(&tDefaultShaderOptions);

Expand Down
8 changes: 4 additions & 4 deletions shaders/shadow.vert
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ void main()
}

vec4 pos = tObjectInfo.tModel * inPosition;
#ifdef PL_MULTIPLE_VIEWPORTS
// #ifdef PL_MULTIPLE_VIEWPORTS
gl_Position = tCameraInfo.atCameraProjs[tObjectInfo.iIndex + gl_InstanceIndex] * pos;
gl_ViewportIndex = gl_InstanceIndex;
#else
gl_Position = tCameraInfo.atCameraProjs[tObjectInfo.iIndex] * pos;
#endif
// #else
// gl_Position = tCameraInfo.atCameraProjs[tObjectInfo.iIndex] * pos;
// #endif

if(tCameraInfo.atCameraProjs[tObjectInfo.iIndex][3][3] == 1.0) // orthographic
gl_Position.z = 1.0 - gl_Position.z;
Expand Down
3 changes: 0 additions & 3 deletions src/pl_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
// #define PL_OFFLINE_SHADERS_ONLY
// #define PL_INCLUDE_SPIRV_CROSS

// core extension options
#define PL_CORE_EXTENSION_INCLUDE_SHADER

// experimental (don't use yet)
// #define PL_EXPERIMENTAL_RENDER_WHILE_RESIZE

Expand Down

0 comments on commit 2127b52

Please sign in to comment.