Jump to content

Asset Reloading Multiple Times


SpiderPig
 Share

Go to solution Solved by Josh,

Recommended Posts

DynamicLine.zip

I've only noticed these things since the OpenGL build.  It could be because now there are compile times involved.

If you edit the attached frag file, it loads it twice and the changes are not applied.  This is the console output after a single edit of DynamicLine.frag.  If you change the color in the attached frag shader you'll see this in the console but the box will not change color.

Loading shader module "Materials/DynamicLine/DynamicLine.frag"
Loading shader module "Materials/DynamicLine/DynamicLine.frag"

If you edit the material, all changes are applied but it still loads it twice.

Deleting shader family "Materials/DynamicLine/DynamicLine.fam"
Loading shader family "Materials/DynamicLine/DynamicLine.fam"
Deleting shader family "Materials/DynamicLine/DynamicLine.fam"
Loading shader family "Materials/DynamicLine/DynamicLine.fam"

With some shaders I've seen it do it 4 times.  It's annoying because as the files get bigger it takes longer to compile.

#include "Engine.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto framebuffer = CreateFramebuffer(window);
    auto world = CreateWorld();
    world->SetGravity(0.0f, -2.0f, 0.0f);

    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetPosition(0, 0, -2);

    auto light = CreateDirectionalLight(world);
    light->SetRotation(35, 35, 0);

    auto box = CreateBox(world);
    box->SetColor(0, 1, 0);

    auto mat = LoadMaterial("Materials\\DynamicLine\\DynamicLine.mat");
    box->SetMaterial(mat);

    auto watcher = CreateFileSystemWatcher("Materials");
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        while (PeekEvent()) {
            auto ev = WaitEvent();

            if (ev.id == EVENT_FILECHANGE or ev.id == EVENT_FILECREATE)
            {
                //Look for a loaded asset with this file path
                auto asset = FindCachedAsset(ev.text);
                if (asset)
                {
                    asset->Reload();
                }
            }
        }

        box->Turn(0.0f, 0.1f, 0.0f);
        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

Link to comment
Share on other sites

Yes. Some programs do saving a little differently. They'll do things like write the file somewhere else, and then copy it over once writing is finished, to prevent a file from getting corrupted. Some of these actions can trigger the file system watcher multiple times, and I do my best to prevent it.

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I added a weak pointer in the shader module (the asset) to all shaders they get attached to, and it will reattach them and update the shader automatically when the module is reloaded.

Tested shader editing in VSCode and Notepad and with both those programs the asset only gets reloaded once...

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Just installed Notepad++ and had no problems with multiple reloads when saving a post-processing fragment shader file.

Let's see what happens when the next build of the editor goes up.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Editing a .fam file in VS Code, I do see two FILE_CHANGED events being emitted by the file system watcher.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

From MS:
https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher.created?view=net-8.0

Quote

 

Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.

 

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

  • Solution

It may be possible to improve this in the future but I am wary of trying to mask this behavior, since it might lead to other problems. For now I am going to consider this "not a bug".

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...