meta4d-me / CatDogEngine

A cross-platform game engine/editor written in modern C++ (WIP)
GNU General Public License v2.0
161 stars 17 forks source link

Shader lazy load #382

Closed roeas closed 1 year ago

roeas commented 1 year ago

Compiling and loading shaders like this, instead of compiling all shader variants at once: Snipaste_2023-09-22_16-57-58

Heres an example of the Renderer frame:

void WorldRenderer::Init()
{
    constexpr StringCrc programCrc = StringCrc("WorldProgram");
    GetRenderContext()->RegisterShaderProgram(programCrc, { "vs_PBR", "fs_PBR" });
        // ...
}

void WorldRenderer::Warmup()
{
    GetRenderContext()->CreateUniform(lutSampler, bgfx::UniformType::Sampler);
    GetRenderContext()->CreateTexture(lutTexture);
        // GetRenderContext()->UploadShaderProgram("WorldProgram");
    // ...
}

void WorldRenderer::Render(float deltaTime)
{
    // ...
    // GetRenderContext()->Submit(GetViewID(), "WorldProgram");
    GetRenderContext()->Submit(GetViewID(), "WorldProgram", pMaterialComponent->GetFeaturesCombine());
}

Click here if you want to check every variant of your uber shader compiling. image

roeas commented 1 year ago

Overall, it looks that you limit Init to create CPU data and add CreateGraphicsResources interface to submit cpu data to gpu.

  1. [Optional] Submit maybe a better name to present CreateGraphicsResources?
  2. [Optional] You can wrap all renderer or renderpass logics to a new data structure such as engine::RenderPipeline and EditorApp can simply hold m_editorRenderPipeline and m_engineRenderPipeline. The pipeline concept is a series of Renderer/RenderPass in order. Multiple RenderPipelines(Rasterize, RayTrace, Compute, ...) contribute to the final rendering world.
  3. Currently, we have an user-visible entity named shader collection. It seems not very reasonable.
  1. Sounds reasonable.
  2. OK, I'll apply this concept after these shader loading and compiling stuff done.
  3. I use this entity to implement functionality similar to this document https://docs.unity3d.com/2023.2/Documentation/Manual/shader-variant-collections.html. The SVC needs to be stored in the project file right? Still under consideration by me.
roeas commented 1 year ago

Indeed it is a bit strange to think of SVC as a component.

T-rvw commented 1 year ago

In project serialization, the main part is Entity/Component data but also allow to add other data such as SVC. It seems an asset, not a component in the unity document.

T-rvw commented 1 year ago

You can add a launch option which let editor pre-compile all shaders? Sometimes it will be helpful. For example, a user doesn't edit any shader, just make a game level which doesn't need to wait in a random timepoint.