@@ -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 }
0 commit comments