Skip to content

Commit

Permalink
Notify JS about bgfx restart (#1370)
Browse files Browse the repository at this point in the history
Added an API no notify JS that the graphics context (bgfx) has been
re-created. This API call will allow JS to re-create all GPU resources
accordingly.
  • Loading branch information
SergioRZMasson authored Mar 22, 2024
1 parent 8d61e15 commit 9bf88d9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ namespace Babylon::Graphics
Update GetUpdate(const char* updateName);

void RequestScreenShot(std::function<void(std::vector<uint8_t>)> callback);
void SetRenderResetCallback(std::function<void()> callback);

arcana::task<void, std::exception_ptr> ReadTextureAsync(bgfx::TextureHandle handle, gsl::span<uint8_t> data, uint8_t mipLevel = 0);

Expand Down
5 changes: 5 additions & 0 deletions Core/Graphics/Source/DeviceContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ namespace Babylon::Graphics
return m_graphicsImpl.RequestScreenShot(std::move(callback));
}

void DeviceContext::SetRenderResetCallback(std::function<void()> callback)
{
return m_graphicsImpl.SetRenderResetCallback(std::move(callback));
}

arcana::task<void, std::exception_ptr> DeviceContext::ReadTextureAsync(bgfx::TextureHandle handle, gsl::span<uint8_t> data, uint8_t mipLevel)
{
return m_graphicsImpl.ReadTextureAsync(handle, data, mipLevel);
Expand Down
13 changes: 13 additions & 0 deletions Core/Graphics/Source/DeviceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ namespace Babylon::Graphics
m_state.Resolution.DevicePixelRatio = value;
}

void DeviceImpl::SetRenderResetCallback(std::function<void()> callback)
{
m_renderResetCallback = std::move(callback);
}

void DeviceImpl::AddToJavaScript(Napi::Env env)
{
JsRuntime::NativeObject::GetFromJavaScript(env)
Expand Down Expand Up @@ -164,6 +169,14 @@ namespace Babylon::Graphics
m_state.Bgfx.Dirty = false;

m_cancellationSource.emplace();

if (m_bgfxId != 0)
{
if (m_renderResetCallback)
{
m_renderResetCallback();
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions Core/Graphics/Source/DeviceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace Babylon::Graphics
void UpdateMSAA(uint8_t value);
void UpdateAlphaPremultiplied(bool enabled);
void UpdateDevicePixelRatio(float value);
void SetRenderResetCallback(std::function<void()> callback);

void AddToJavaScript(Napi::Env);
static DeviceImpl& GetFromJavaScript(Napi::Env);
Expand Down Expand Up @@ -163,5 +164,6 @@ namespace Babylon::Graphics

DeviceContext m_context;
uintptr_t m_bgfxId = 0;
std::function<void()> m_renderResetCallback;
};
}
13 changes: 13 additions & 0 deletions Plugins/NativeEngine/Source/NativeEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ namespace Babylon

// REVIEW: Should this be here if only used by ValidationTest?
InstanceMethod("getFrameBufferData", &NativeEngine::GetFrameBufferData),
InstanceMethod("setDeviceLostCallback", &NativeEngine::SetRenderResetCallback),
});

JsRuntime::NativeObject::GetFromJavaScript(env).Set(JS_CONSTRUCTOR_NAME, func);
Expand Down Expand Up @@ -2070,6 +2071,18 @@ namespace Babylon
return Napi::Value::From(env, outputData);
}

void NativeEngine::SetRenderResetCallback(const Napi::CallbackInfo& info)
{
const auto callback{info[0].As<Napi::Function>()};
auto callbackPtr{std::make_shared<Napi::FunctionReference>(Napi::Persistent(callback))};

m_deviceContext.SetRenderResetCallback([this, renderResetCallback = std::move(callbackPtr)]() {
m_runtime.Dispatch([renderResetCallback = std::move(renderResetCallback)](auto) {
renderResetCallback->Call({});
});
});
}

void NativeEngine::GetFrameBufferData(const Napi::CallbackInfo& info)
{
const auto callback{info[0].As<Napi::Function>()};
Expand Down
1 change: 1 addition & 0 deletions Plugins/NativeEngine/Source/NativeEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ namespace Babylon
Napi::Value CreateImageBitmap(const Napi::CallbackInfo& info);
Napi::Value ResizeImageBitmap(const Napi::CallbackInfo& info);
void GetFrameBufferData(const Napi::CallbackInfo& info);
void SetRenderResetCallback(const Napi::CallbackInfo& info);
void SetStencil(NativeDataStream::Reader& data);
void SetViewPort(NativeDataStream::Reader& data);
void SetScissor(NativeDataStream::Reader& data);
Expand Down

0 comments on commit 9bf88d9

Please sign in to comment.