Skip to content

Commit

Permalink
Delete obsolete compiled uber shader file when file change detected.
Browse files Browse the repository at this point in the history
  • Loading branch information
roeas committed Apr 3, 2024
1 parent a0eef0f commit 6c81c6a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Engine/BuiltInShaders/shaders/fs_PBR.sc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ void main()
// Fragment Color
gl_FragData[0] = vec4(dirColor + envColor + emiColor, 1.0);
gl_FragData[1] = vec4(emiColor, 1.0);

gl_FragData[0] = vec4(1.0, 0.0, 0.0, 1.0);
// Post-processing will be used in the last pass.
}
2 changes: 1 addition & 1 deletion Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ void EditorApp::OnShaderHotModifiedCallback(const char* rootDir, const char* fil
// Do nothing when a non-shader file is detected.
return;
}
m_pRenderContext->OnShaderHotModified(engine::StringCrc{ engine::Path::GetFileNameWithoutExtension(filePath) });
m_pRenderContext->OnShaderHotModified(engine::Path::GetFileNameWithoutExtension(filePath));
}

void EditorApp::UpdateMaterials()
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Runtime/Rendering/PostProcessRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace engine

void PostProcessRenderer::Init()
{
AddDependentShaderResource(GetRenderContext()->RegisterShaderProgram("PostProcessProgram", "vs_fullscreen", "fs_PBR_postProcessing"));
AddDependentShaderResource(GetRenderContext()->RegisterShaderProgram("PostProcessProgram", "vs_fullscreen", "fs_postProcessing"));

GetRenderContext()->CreateUniform("s_lightingColor", bgfx::UniformType::Sampler);
GetRenderContext()->CreateUniform("u_postProcessingParams", bgfx::UniformType::Vec4);
Expand Down
21 changes: 19 additions & 2 deletions Engine/Source/Runtime/Rendering/RenderContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,27 @@ ShaderResource* RenderContext::RegisterShaderProgram(const std::string& programN
return pShaderResource;
}

void RenderContext::OnShaderHotModified(StringCrc modifiedShaderNameCrc)
void RenderContext::OnShaderHotModified(std::string modifiedShaderName)
{
// Delete all related compiled uber shader file.
// TODO : Need a file system to check is compiled file dirty.
for (const auto& entry : std::filesystem::recursive_directory_iterator(Path::GetShaderOutputDirectory()))
{
if (!entry.is_regular_file() || entry.path().extension() != ".bin")
{
continue;
}

std::string fileName = entry.path().filename().generic_string();
if (fileName.find(modifiedShaderName) != std::string::npos)
{
std::filesystem::remove(entry.path());
CD_ENGINE_TRACE("Delete obsolete file {}", fileName);
}
}

// Get all ShaderResource variants by shader name.
auto range = m_shaderResources.equal_range(modifiedShaderNameCrc);
auto range = m_shaderResources.equal_range(StringCrc{ modifiedShaderName });
for (auto it = range.first; it != range.second; ++it)
{
m_modifiedShaderResources.insert(it->second);
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Runtime/Rendering/RenderContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class RenderContext
const std::multimap<StringCrc, ShaderResource*>& GetShaderResources() const { return m_shaderResources; }

// Call back function bind to file watcher.
void OnShaderHotModified(StringCrc modifiedShaderNameCrc);
void OnShaderHotModified(std::string modifiedShaderName);
void ClearModifiedShaderResources() { m_modifiedShaderResources.clear(); }
std::set<ShaderResource*>& GetModifiedShaderResources() { return m_modifiedShaderResources; }
const std::set<ShaderResource*>& GetModifiedShaderResources() const { return m_modifiedShaderResources; }
Expand Down

0 comments on commit 6c81c6a

Please sign in to comment.