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

Task chain for texture copy/destruction #1192

Closed
Closed
Show file tree
Hide file tree
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
84 changes: 42 additions & 42 deletions Apps/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Apps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"getNightly": "node scripts/getNightly.js"
},
"dependencies": {
"babylonjs": "^5.35.1",
"babylonjs-gui": "^5.35.1",
"babylonjs-loaders": "^5.35.1",
"babylonjs-materials": "^5.35.1",
"babylonjs": "^5.41.0",
"babylonjs-gui": "^5.41.0",
"babylonjs-loaders": "^5.41.0",
"babylonjs-materials": "^5.41.0",
"chai": "^4.3.4",
"jsc-android": "^241213.1.0",
"mocha": "^9.2.2",
Expand Down
3 changes: 3 additions & 0 deletions Core/Graphics/Include/Shared/Babylon/Graphics/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Babylon::Graphics
};

class Device;
class Texture;

class DeviceUpdate
{
Expand Down Expand Up @@ -98,6 +99,8 @@ namespace Babylon::Graphics

PlatformInfo GetPlatformInfo() const;

void TaskChainDeleteTexture(Graphics::Texture* texture);

private:
Device();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Babylon::Graphics
class Update;
class DeviceContext;
class DeviceImpl;
class Texture;

struct TextureInfo final
{
Expand Down Expand Up @@ -115,6 +116,9 @@ namespace Babylon::Graphics
void RemoveTexture(bgfx::TextureHandle handle);
TextureInfo GetTextureInfo(bgfx::TextureHandle handle);

void TaskChainDeleteTexture(Graphics::Texture* texture);
void TaskChainOpTexture(std::function<void()> op);

private:
friend UpdateToken;

Expand Down
5 changes: 5 additions & 0 deletions Core/Graphics/Source/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,9 @@ namespace Babylon::Graphics
{
return m_impl->GetPlatformInfo();
}

void Device::TaskChainDeleteTexture(Graphics::Texture* texture)
{
m_impl->TaskChainDeleteTexture(texture);
}
}
11 changes: 11 additions & 0 deletions Core/Graphics/Source/DeviceContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,15 @@ namespace Babylon::Graphics
std::scoped_lock lock{m_textureHandleToInfoMutex};
return m_textureHandleToInfo[handle.idx];
}

void DeviceContext::TaskChainDeleteTexture(Graphics::Texture* texture)
{
m_graphicsImpl.TaskChainDeleteTexture(texture);
}

void DeviceContext::TaskChainOpTexture(std::function<void()> op)
{
m_graphicsImpl.TaskChainOpTexture(op);
}

}
22 changes: 22 additions & 0 deletions Core/Graphics/Source/DeviceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <TargetConditionals.h>
#endif

#include "Texture.h"

namespace
{
constexpr auto JS_GRAPHICS_NAME = "_Graphics";
Expand All @@ -20,6 +22,8 @@ namespace Babylon::Graphics
DeviceImpl::DeviceImpl()
: m_bgfxCallback{[this](const auto& data) { CaptureCallback(data); }}
, m_context{*this}
, m_textureTask{ arcana::task_from_result<std::exception_ptr>() }
, m_update{ m_context.GetUpdate("update") }
{
std::scoped_lock lock{m_state.Mutex};
m_state.Bgfx.Initialized = false;
Expand Down Expand Up @@ -464,4 +468,22 @@ namespace Babylon::Graphics
{
return {static_cast<DeviceT>(bgfx::getInternalData()->context)};
}

void DeviceImpl::TaskChainDeleteTexture(Graphics::Texture* texture)
{
if (!texture)
{
return;
}
m_textureTask = m_textureTask.then(m_update.Scheduler(), *m_cancellationSource, [texture]() {
delete texture;
});
}

void DeviceImpl::TaskChainOpTexture(std::function<void()> op)
{
m_textureTask = m_textureTask.then(m_update.Scheduler(), *m_cancellationSource, [op]() {
op();
});
}
}
6 changes: 6 additions & 0 deletions Core/Graphics/Source/DeviceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Babylon::Graphics
struct WindowConfiguration;
struct DeviceConfiguration;
struct BackBufferUpdateInfo;
class Texture;

class DeviceImpl
{
Expand Down Expand Up @@ -91,6 +92,9 @@ namespace Babylon::Graphics
return m_context;
}

void TaskChainDeleteTexture(Graphics::Texture* texture);
void TaskChainOpTexture(std::function<void()> op);

private:
friend class UpdateToken;

Expand Down Expand Up @@ -156,5 +160,7 @@ namespace Babylon::Graphics
std::mutex m_updateSafeTimespansMutex{};

DeviceContext m_context;
arcana::task<void, std::exception_ptr> m_textureTask{};
Graphics::Update m_update;
};
}
21 changes: 17 additions & 4 deletions Plugins/NativeEngine/Source/NativeEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,10 +1029,19 @@ namespace Babylon
SetFloatN<4>(data);
}

template<typename PointerT>
auto NativeEngine::NapiPointerDeleterTexture(PointerT pointer)
{
return [pointer, &graphicsContext=m_graphicsContext]()
{
graphicsContext.TaskChainDeleteTexture(pointer);
};
}

Napi::Value NativeEngine::CreateTexture(const Napi::CallbackInfo& info)
{
Graphics::Texture* texture = new Graphics::Texture();
return Napi::Pointer<Graphics::Texture>::Create(info.Env(), texture, Napi::NapiPointerDeleter(texture));
return Napi::Pointer<Graphics::Texture>::Create(info.Env(), texture, NapiPointerDeleterTexture(texture));
}

void NativeEngine::InitializeTexture(const Napi::CallbackInfo& info)
Expand Down Expand Up @@ -1093,7 +1102,7 @@ namespace Babylon
const auto textureDestination = info[0].As<Napi::Pointer<Graphics::Texture>>().Get();
const auto textureSource = info[1].As<Napi::Pointer<Graphics::Texture>>().Get();

arcana::make_task(m_update.Scheduler(), *m_cancellationSource, [this, textureDestination, textureSource, cancellationSource = m_cancellationSource]()
m_graphicsContext.TaskChainOpTexture([this, textureDestination, textureSource, cancellationSource = m_cancellationSource]()
{
return arcana::make_task(m_runtimeScheduler, *m_cancellationSource, [this, textureDestination, textureSource, updateToken = m_update.GetUpdateToken(), cancellationSource = m_cancellationSource]()
{
Expand Down Expand Up @@ -1338,8 +1347,12 @@ namespace Babylon
void NativeEngine::DeleteTexture(const Napi::CallbackInfo& info)
{
Graphics::Texture* texture = info[0].As<Napi::Pointer<Graphics::Texture>>().Get();
m_graphicsContext.RemoveTexture(texture->Handle());
texture->Dispose();
m_graphicsContext.TaskChainOpTexture([texture, this]() {
//m_textureTask = m_textureTask.then(m_update.Scheduler(), *m_cancellationSource, [texture, this]() {
m_graphicsContext.RemoveTexture(texture->Handle());
texture->Dispose();
//delete texture;
});
}

Napi::Value NativeEngine::ReadTexture(const Napi::CallbackInfo& info)
Expand Down
2 changes: 2 additions & 0 deletions Plugins/NativeEngine/Source/NativeEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,7 @@ namespace Babylon

// TODO: This should be changed to a non-owning ref once multi-update is available.
NativeDataStream* m_commandStream{};

template<typename PointerT> auto NapiPointerDeleterTexture(PointerT pointer);
};
}
12 changes: 11 additions & 1 deletion Polyfills/Canvas/Source/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ namespace Babylon::Polyfills::Internal
return false;
}

template<typename PointerT>
auto NativeCanvas::NapiPointerDeleterTexture(PointerT pointer)
{
return [pointer, &graphicsContext = m_graphicsContext]()
{
graphicsContext.TaskChainDeleteTexture(pointer);
};
}

Napi::Value NativeCanvas::GetCanvasTexture(const Napi::CallbackInfo& info)
{
if (!m_texture)
Expand All @@ -142,7 +151,8 @@ namespace Babylon::Polyfills::Internal
}

m_texture->Attach(bgfx::getTexture(m_frameBuffer->Handle()), false, m_width, m_height, false, 1, bgfx::TextureFormat::RGBA8, BGFX_TEXTURE_RT);
return Napi::Pointer<Graphics::Texture>::Create(info.Env(), m_texture.get());
auto texturePtr{ m_texture.get() };
return Napi::Pointer<Graphics::Texture>::Create(info.Env(), texturePtr, NapiPointerDeleterTexture(texturePtr));
}

Napi::Value NativeCanvas::ParseColor(const Napi::CallbackInfo& info)
Expand Down
1 change: 1 addition & 0 deletions Polyfills/Canvas/Source/Canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@ namespace Babylon::Polyfills::Internal
bool m_dirty{};

void FlushGraphicResources() override;
template<typename PointerT> auto NapiPointerDeleterTexture(PointerT pointer);
};
}
3 changes: 0 additions & 3 deletions Polyfills/Canvas/Source/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ namespace Babylon::Polyfills::Internal
m_fillStyle = value.As<Napi::String>().Utf8Value();
const auto color = StringToColor(info.Env(), m_fillStyle);
nvgFillColor(m_nvg, color);
SetDirty();
}

Napi::Value Context::GetStrokeStyle(const Napi::CallbackInfo&)
Expand All @@ -170,7 +169,6 @@ namespace Babylon::Polyfills::Internal
m_strokeStyle = value.As<Napi::String>().Utf8Value();
auto color = StringToColor(info.Env(), m_strokeStyle);
nvgStrokeColor(m_nvg, color);
SetDirty();
}

Napi::Value Context::GetLineWidth(const Napi::CallbackInfo& )
Expand All @@ -182,7 +180,6 @@ namespace Babylon::Polyfills::Internal
{
m_lineWidth = value.As<Napi::Number>().FloatValue();
nvgStrokeWidth(m_nvg, m_lineWidth);
SetDirty();
}

void Context::Fill(const Napi::CallbackInfo&)
Expand Down