Skip to content
Merged
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
56 changes: 56 additions & 0 deletions Apps/UnitTests/Source/Tests.ExternalTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,59 @@ TEST(ExternalTexture, AddToContextAsyncAndUpdate)
device.FinishRenderingCurrentFrame();
#endif
}

TEST(ExternalTexture, AddToContextAsyncWithLayerIndex)
{
#ifdef SKIP_EXTERNAL_TEXTURE_TESTS
GTEST_SKIP();
#else
Babylon::Graphics::Device device{g_deviceConfig};

device.StartRenderingCurrentFrame();

// Array texture (3 layers) so a non-zero layer index is valid.
auto nativeTexture = Helpers::CreateTexture(device.GetPlatformInfo().Device, 256, 256, 3);
Babylon::Plugins::ExternalTexture externalTexture{nativeTexture};
Helpers::DestroyTexture(nativeTexture);

std::promise<void> addToContext{};
std::promise<void> promiseResolved{};

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

Babylon::Polyfills::Console::Initialize(env, [](const char* message, auto) {
std::cout << message << std::endl;
});

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

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

// Backwards-compat: the deprecated AddToContextAsync must still accept a layer
// index and forward it to CreateForJavaScript (views only that array slice).
auto jsPromise = externalTexture.AddToContextAsync(env, 1);
addToContext.set_value();

auto jsOnFulfilled = Napi::Function::New(env, [&promiseResolved](const Napi::CallbackInfo& info) {
promiseResolved.set_value();
});

auto jsOnRejected = Napi::Function::New(env, [&promiseResolved](const Napi::CallbackInfo& info) {
promiseResolved.set_exception(std::make_exception_ptr(info[0].As<Napi::Error>()));
});

jsPromise.Get("then").As<Napi::Function>().Call(jsPromise, {jsOnFulfilled, jsOnRejected});
});

// Wait for AddToContextAsync to be called.
addToContext.get_future().wait();

// Close the frame in which the deprecated shim's synchronous CreateForJavaScript ran.
device.FinishRenderingCurrentFrame();

// get() (not wait()) so a rejected promise rethrows and fails the test.
EXPECT_NO_THROW(promiseResolved.get_future().get());
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ namespace Babylon::Plugins

// Deprecated: use CreateForJavaScript instead. Retained as a shim for existing consumers.
// Returns a Promise that is already resolved with the value from CreateForJavaScript.
// If layerIndex is set, the JavaScript texture views only that array layer (single-slice).
[[deprecated("Use CreateForJavaScript instead.")]]
Napi::Promise AddToContextAsync(Napi::Env) const;
Napi::Promise AddToContextAsync(Napi::Env, std::optional<uint16_t> layerIndex = {}) const;

// Updates to a new texture. If layerIndex is set, views only that array layer (single-slice).
void Update(Graphics::TextureT, std::optional<Graphics::TextureFormatT> = {}, std::optional<uint16_t> layerIndex = {});
Expand Down
4 changes: 2 additions & 2 deletions Plugins/ExternalTexture/Source/ExternalTexture_Shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ namespace Babylon::Plugins
m_impl->Update(ptr, overrideFormat, layerIndex);
}

Napi::Promise ExternalTexture::AddToContextAsync(Napi::Env env) const
Napi::Promise ExternalTexture::AddToContextAsync(Napi::Env env, std::optional<uint16_t> layerIndex) const
{
auto deferred = Napi::Promise::Deferred::New(env);
deferred.Resolve(CreateForJavaScript(env));
deferred.Resolve(CreateForJavaScript(env, layerIndex));
return deferred.Promise();
}
}