Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async shader compilation + shutdown crash repro #1393

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 64 additions & 26 deletions Apps/UnitTests/Shared/Shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,39 +99,77 @@ TEST(JavaScript, All)
}

/*
This test does a serie of initialization and shutdowns.
It needs the shutdown PR to be merged before running properly.
TEST(NativeAPI, LifeCycle)
Repro for shutdown scenario
Engine is shutdown before shader is finished compiling
TEST(Shutdown, AsyncShaderCompilation)
{
for (int cycle = 0; cycle < 20; cycle++)
{
Babylon::Graphics::Device device{deviceConfig};
std::optional<Babylon::Polyfills::Canvas> nativeCanvas;
std::promise<int32_t> exitCodePromise;

Babylon::AppRuntime runtime{};
runtime.Dispatch([&device, &nativeCanvas](Napi::Env env) {
device.AddToJavaScript(env);
Babylon::Graphics::Device device{ deviceConfig };
auto update{ device.GetUpdate("update") };
std::optional<Babylon::Polyfills::Canvas> nativeCanvas;

Babylon::AppRuntime runtime{};
runtime.Dispatch([&device, &nativeCanvas, &exitCodePromise](Napi::Env env) {
device.AddToJavaScript(env);

Babylon::Polyfills::XMLHttpRequest::Initialize(env);
Babylon::Polyfills::Console::Initialize(env, [](const char* message, auto) {
printf("%s", message);
fflush(stdout);
Babylon::Polyfills::XMLHttpRequest::Initialize(env);
Babylon::Polyfills::Console::Initialize(env, [](const char* message, auto) {
std::cout << message << std::endl;
std::cout.flush();
});
Babylon::Polyfills::Window::Initialize(env);
nativeCanvas.emplace(Babylon::Polyfills::Canvas::Initialize(env));
Babylon::Plugins::NativeEngine::Initialize(env);
Babylon::Polyfills::Window::Initialize(env);

Babylon::Polyfills::XMLHttpRequest::Initialize(env);

nativeCanvas.emplace(Babylon::Polyfills::Canvas::Initialize(env));

Babylon::Plugins::NativeEngine::Initialize(env);

auto setExitCodeCallback = Napi::Function::New(
env, [&exitCodePromise](const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
exitCodePromise.set_value(info[0].As<Napi::Number>().Int32Value());
}, "setExitCode");
env.Global().Set("setExitCode", setExitCodeCallback);
});

Babylon::ScriptLoader loader{runtime};
loader.LoadScript("app:///Scripts/babylon.max.js");
loader.LoadScript("app:///Scripts/babylonjs.materials.js");
Babylon::ScriptLoader loader{ runtime };
loader.LoadScript("app:///Scripts/babylon.max.js");
loader.LoadScript("app:///Scripts/babylonjs.materials.js");
loader.Eval(R"(
function CreateBoxAsync(scene) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment explaining why this needs to be an async function rather than just synchronous setup code? Not clear to me.

BABYLON.Mesh.CreateBox("box1", 0.2, scene);
return Promise.resolve();
}

for (int frame = 0; frame < 10; frame++)
{
device.StartRenderingCurrentFrame();
device.FinishRenderingCurrentFrame();
}
}
var engine = new BABYLON.NativeEngine();
var scene = new BABYLON.Scene(engine);

CreateBoxAsync(scene).then(function () {
var disc = BABYLON.Mesh.CreateDisc("disc", 3, 60, scene);

scene.createDefaultCamera(true, true, true);
scene.activeCamera.alpha += Math.PI;
scene.createDefaultLight(true);

var mainMaterial = new BABYLON.PBRMaterial("main", scene);
disc.material = mainMaterial;
engine.runRenderLoop(function () {
scene.render();
});
setExitCode(1);
}, function (ex) {
console.log(ex.message, ex.stack);
});
)", "script");

auto exitCode{ exitCodePromise.get_future().get() };
device.StartRenderingCurrentFrame();
update.Start();

update.Finish();
device.FinishRenderingCurrentFrame();
}
*/

Expand Down