Skip to content

Commit 3645a28

Browse files
authored
Render target texture improvements (#1173)
1 parent 603415f commit 3645a28

File tree

8 files changed

+89
-66
lines changed

8 files changed

+89
-66
lines changed

Apps/package-lock.json

Lines changed: 42 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Apps/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"getNightly": "node scripts/getNightly.js"
77
},
88
"dependencies": {
9-
"babylonjs": "^5.32.2",
10-
"babylonjs-gui": "^5.32.2",
11-
"babylonjs-loaders": "^5.32.2",
12-
"babylonjs-materials": "^5.32.2",
9+
"babylonjs": "^5.35.1",
10+
"babylonjs-gui": "^5.35.1",
11+
"babylonjs-loaders": "^5.35.1",
12+
"babylonjs-materials": "^5.35.1",
1313
"chai": "^4.3.4",
1414
"jsc-android": "^241213.1.0",
1515
"mocha": "^9.2.2",

Core/Graphics/InternalInclude/Babylon/Graphics/DeviceContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ namespace Babylon::Graphics
110110

111111
bgfx::ViewId AcquireNewViewId(bgfx::Encoder&);
112112

113+
// TODO: find a different way to get the texture info for frame capture
113114
void AddTexture(bgfx::TextureHandle handle, uint16_t width, uint16_t height, bool hasMips, uint16_t numLayers, bgfx::TextureFormat::Enum format);
114115
void RemoveTexture(bgfx::TextureHandle handle);
115116
TextureInfo GetTextureInfo(bgfx::TextureHandle handle);

Core/Graphics/InternalInclude/Babylon/Graphics/Texture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace Babylon::Graphics
2424
void UpdateCube(uint16_t layer, uint8_t side, uint8_t mip, uint16_t x, uint16_t y, uint16_t width, uint16_t height, const bgfx::Memory* mem, uint16_t pitch = UINT16_MAX);
2525

2626
void Attach(bgfx::TextureHandle handle, bool ownsHandle, uint16_t width, uint16_t height, bool hasMips, uint16_t numLayers, bgfx::TextureFormat::Enum format, uint64_t flags);
27+
void Disown();
2728

2829
bgfx::TextureHandle Handle() const;
2930
uint16_t Width() const;

Core/Graphics/Source/Texture.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ namespace Babylon::Graphics
7777
m_flags = flags;
7878
}
7979

80+
void Texture::Disown()
81+
{
82+
m_ownsHandle = false;
83+
}
84+
8085
bgfx::TextureHandle Texture::Handle() const
8186
{
8287
return m_handle;

Plugins/ExternalTexture/Source/ExternalTexture_Shared.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace Babylon::Plugins
7575
return;
7676
}
7777

78-
arcana::make_task(context.AfterRenderScheduler(), arcana::cancellation_source::none(), [&context, &runtime, deferred = std::move(deferred), handle, impl = std::move(impl)]()
78+
arcana::make_task(context.AfterRenderScheduler(), arcana::cancellation_source::none(), [&runtime, deferred = std::move(deferred), handle, impl = std::move(impl)]()
7979
{
8080
if (bgfx::overrideInternal(handle, impl->Ptr()) == 0)
8181
{
@@ -88,8 +88,6 @@ namespace Babylon::Plugins
8888
return;
8989
}
9090

91-
context.AddTexture(handle, 0, 0, impl->HasMips(), 0, impl->Format());
92-
9391
runtime.Dispatch([deferred = std::move(deferred), handle, impl = std::move(impl)](Napi::Env env)
9492
{
9593
auto* texture = new Graphics::Texture{};

Plugins/NativeEngine/Source/NativeEngine.cpp

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ namespace Babylon
303303
JS_CLASS_NAME,
304304
{
305305
// This must match the version in nativeEngine.ts
306-
StaticValue("PROTOCOL_VERSION", Napi::Number::From(env, 6)),
306+
StaticValue("PROTOCOL_VERSION", Napi::Number::From(env, 7)),
307307

308308
StaticValue("CAPS_LIMITS_MAX_TEXTURE_SIZE", Napi::Number::From(env, limits.maxTextureSize)),
309309
StaticValue("CAPS_LIMITS_MAX_TEXTURE_LAYERS", Napi::Number::From(env, limits.maxTextureLayers)),
@@ -342,8 +342,8 @@ namespace Babylon
342342

343343
StaticValue("TEXTURE_FORMAT_RGB8", Napi::Number::From(env, static_cast<uint32_t>(bgfx::TextureFormat::RGB8))),
344344
StaticValue("TEXTURE_FORMAT_RGBA8", Napi::Number::From(env, static_cast<uint32_t>(bgfx::TextureFormat::RGBA8))),
345+
StaticValue("TEXTURE_FORMAT_RGBA16F", Napi::Number::From(env, static_cast<uint32_t>(bgfx::TextureFormat::RGBA16F))),
345346
StaticValue("TEXTURE_FORMAT_RGBA32F", Napi::Number::From(env, static_cast<uint32_t>(bgfx::TextureFormat::RGBA32F))),
346-
StaticValue("TEXTURE_FORMAT_BGRA8", Napi::Number::From(env, static_cast<uint32_t>(bgfx::TextureFormat::BGRA8))),
347347

348348
StaticValue("ATTRIB_TYPE_INT8", Napi::Number::From(env, static_cast<uint32_t>(bgfx::AttribType::Int8))),
349349
StaticValue("ATTRIB_TYPE_UINT8", Napi::Number::From(env, static_cast<uint32_t>(bgfx::AttribType::Uint8))),
@@ -460,6 +460,7 @@ namespace Babylon
460460
InstanceMethod("getAttributes", &NativeEngine::GetAttributes),
461461

462462
InstanceMethod("createTexture", &NativeEngine::CreateTexture),
463+
InstanceMethod("initializeTexture", &NativeEngine::InitializeTexture),
463464
InstanceMethod("loadTexture", &NativeEngine::LoadTexture),
464465
InstanceMethod("loadRawTexture", &NativeEngine::LoadRawTexture),
465466
InstanceMethod("loadRawTexture2DArray", &NativeEngine::LoadRawTexture2DArray),
@@ -1034,6 +1035,29 @@ namespace Babylon
10341035
return Napi::Pointer<Graphics::Texture>::Create(info.Env(), texture, Napi::NapiPointerDeleter(texture));
10351036
}
10361037

1038+
void NativeEngine::InitializeTexture(const Napi::CallbackInfo& info)
1039+
{
1040+
const auto texture = info[0].As<Napi::Pointer<Graphics::Texture>>().Get();
1041+
const uint16_t width = static_cast<uint16_t>(info[1].As<Napi::Number>().Uint32Value());
1042+
const uint16_t height = static_cast<uint16_t>(info[2].As<Napi::Number>().Uint32Value());
1043+
const bool hasMips = info[3].As<Napi::Boolean>();
1044+
const bgfx::TextureFormat::Enum format = static_cast<bgfx::TextureFormat::Enum>(info[4].As<Napi::Number>().Uint32Value());
1045+
const bool renderTarget = info[5].As<Napi::Boolean>();
1046+
const bool srgb = info[6].As<Napi::Boolean>();
1047+
1048+
auto flags = BGFX_TEXTURE_NONE;
1049+
if (renderTarget)
1050+
{
1051+
flags |= BGFX_TEXTURE_RT;
1052+
}
1053+
if (srgb)
1054+
{
1055+
flags |= BGFX_TEXTURE_SRGB;
1056+
}
1057+
1058+
texture->Create2D(width, height, hasMips, 1, format, flags);
1059+
}
1060+
10371061
void NativeEngine::LoadTexture(const Napi::CallbackInfo& info)
10381062
{
10391063
const auto texture = info[0].As<Napi::Pointer<Graphics::Texture>>().Get();
@@ -1446,25 +1470,20 @@ namespace Babylon
14461470

14471471
Napi::Value NativeEngine::CreateFrameBuffer(const Napi::CallbackInfo& info)
14481472
{
1449-
Graphics::Texture* texture = info[0].As<Napi::Pointer<Graphics::Texture>>().Get();
1450-
uint16_t width = static_cast<uint16_t>(info[1].As<Napi::Number>().Uint32Value());
1451-
uint16_t height = static_cast<uint16_t>(info[2].As<Napi::Number>().Uint32Value());
1452-
bgfx::TextureFormat::Enum format = static_cast<bgfx::TextureFormat::Enum>(info[3].As<Napi::Number>().Uint32Value());
1453-
bool generateStencilBuffer = info[4].As<Napi::Boolean>();
1454-
bool generateDepth = info[5].As<Napi::Boolean>();
1455-
bool generateMips = info[6].As<Napi::Boolean>();
1456-
1457-
assert(bgfx::isTextureValid(0, false, 1, format, BGFX_TEXTURE_RT));
1473+
Graphics::Texture* texture = info[0].IsNull() ? nullptr : info[0].As<Napi::Pointer<Graphics::Texture>>().Get();
1474+
const uint16_t width = static_cast<uint16_t>(info[1].As<Napi::Number>().Uint32Value());
1475+
const uint16_t height = static_cast<uint16_t>(info[2].As<Napi::Number>().Uint32Value());
1476+
const bool generateStencilBuffer = info[3].As<Napi::Boolean>();
1477+
const bool generateDepth = info[4].As<Napi::Boolean>();
14581478

14591479
std::array<bgfx::Attachment, 2> attachments{};
14601480
uint8_t numAttachments = 0;
14611481

1462-
if (!texture->IsValid())
1482+
if (texture != nullptr)
14631483
{
1464-
auto handle = bgfx::createTexture2D(width, height, generateMips, 1, format, BGFX_TEXTURE_RT);
1465-
texture->Attach(handle, false, width, height, generateMips, 1, format, BGFX_TEXTURE_RT);
1484+
texture->Disown();
1485+
attachments[numAttachments++].init(texture->Handle());
14661486
}
1467-
attachments[numAttachments++].init(texture->Handle());
14681487

14691488
if (generateStencilBuffer || generateDepth)
14701489
{
@@ -1486,8 +1505,6 @@ namespace Babylon
14861505
bgfx::FrameBufferHandle frameBufferHandle = bgfx::createFrameBuffer(numAttachments, attachments.data(), true);
14871506
assert(bgfx::isValid(frameBufferHandle));
14881507

1489-
m_graphicsContext.AddTexture(texture->Handle(), width, height, generateMips, 1, format);
1490-
14911508
Graphics::FrameBuffer* frameBuffer = new Graphics::FrameBuffer(m_graphicsContext, frameBufferHandle, width, height, false, generateDepth, generateStencilBuffer);
14921509
return Napi::Pointer<Graphics::FrameBuffer>::Create(info.Env(), frameBuffer, Napi::NapiPointerDeleter(frameBuffer));
14931510
}

Plugins/NativeEngine/Source/NativeEngine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ namespace Babylon
150150
void SetFloat3(NativeDataStream::Reader& data);
151151
void SetFloat4(NativeDataStream::Reader& data);
152152
Napi::Value CreateTexture(const Napi::CallbackInfo& info);
153+
void InitializeTexture(const Napi::CallbackInfo& info);
153154
void LoadTexture(const Napi::CallbackInfo& info);
154155
void CopyTexture(const Napi::CallbackInfo& info);
155156
void LoadRawTexture(const Napi::CallbackInfo& info);

0 commit comments

Comments
 (0)