@@ -1443,39 +1443,38 @@ namespace Babylon
1443
1443
bool generateDepth = info[5 ].As <Napi::Boolean>();
1444
1444
bool generateMips = info[6 ].As <Napi::Boolean>();
1445
1445
1446
- bgfx::FrameBufferHandle frameBufferHandle{};
1447
- if (generateDepth)
1446
+ assert (bgfx::isTextureValid (0 , false , 1 , format, BGFX_TEXTURE_RT));
1447
+
1448
+ std::array<bgfx::Attachment, 2 > attachments{};
1449
+ uint8_t numAttachments = 0 ;
1450
+
1451
+ if (!texture->IsValid ())
1452
+ {
1453
+ auto handle = bgfx::createTexture2D (width, height, generateMips, 1 , format, BGFX_TEXTURE_RT);
1454
+ texture->Attach (handle, false , width, height, generateMips, 1 , format, BGFX_TEXTURE_RT);
1455
+ }
1456
+ attachments[numAttachments++].init (texture->Handle ());
1457
+
1458
+ if (generateStencilBuffer || generateDepth)
1448
1459
{
1449
- auto depthStencilFormat = bgfx::TextureFormat::D32;
1450
- if (generateStencilBuffer)
1460
+ if (generateStencilBuffer != generateDepth)
1451
1461
{
1452
- depthStencilFormat = bgfx::TextureFormat::D24S8 ;
1462
+ JsConsoleLogger::LogWarn (info. Env (), " The flags generateDepth/generateStencilBuffer are mismatched. Depth and stencil are combined in one texture " ) ;
1453
1463
}
1454
1464
1455
- assert ( bgfx::isTextureValid ( 0 , false , 1 , format, BGFX_TEXTURE_RT)) ;
1465
+ const auto depthStencilFormat{generateStencilBuffer ? bgfx::TextureFormat::D24S8 : bgfx::TextureFormat::D32} ;
1456
1466
assert (bgfx::isTextureValid (0 , false , 1 , depthStencilFormat, BGFX_TEXTURE_RT));
1457
1467
1458
1468
// bgfx doesn't add flag D3D11_RESOURCE_MISC_GENERATE_MIPS for depth textures (missing that flag will crash D3D with resolving)
1459
1469
// And not sure it makes sense to generate mipmaps from a depth buffer with exponential values.
1460
1470
// only allows mipmaps resolve step when mipmapping is asked and for the color texture, not the depth.
1461
1471
// https://github.com/bkaradzic/bgfx/blob/2c21f68998595fa388e25cb6527e82254d0e9bff/src/renderer_d3d11.cpp#L4525
1462
- std::array<bgfx::TextureHandle, 2 > textures{
1463
- bgfx::createTexture2D (width, height, generateMips, 1 , format, BGFX_TEXTURE_RT),
1464
- bgfx::createTexture2D (width, height, false , 1 , depthStencilFormat, BGFX_TEXTURE_RT)};
1465
- std::array<bgfx::Attachment, textures.size ()> attachments{};
1466
- for (size_t idx = 0 ; idx < attachments.size (); ++idx)
1467
- {
1468
- attachments[idx].init (textures[idx]);
1469
- }
1470
- frameBufferHandle = bgfx::createFrameBuffer (static_cast <uint8_t >(attachments.size ()), attachments.data (), true );
1471
- }
1472
- else
1473
- {
1474
- assert (!generateStencilBuffer);
1475
- frameBufferHandle = bgfx::createFrameBuffer (width, height, format, BGFX_TEXTURE_RT);
1472
+ attachments[numAttachments++].init (bgfx::createTexture2D (width, height, false , 1 , depthStencilFormat, BGFX_TEXTURE_RT));
1476
1473
}
1477
1474
1478
- texture->Attach (bgfx::getTexture (frameBufferHandle), false , width, height, generateMips, 1 , format, BGFX_TEXTURE_RT);
1475
+ bgfx::FrameBufferHandle frameBufferHandle = bgfx::createFrameBuffer (numAttachments, attachments.data (), true );
1476
+ assert (bgfx::isValid (frameBufferHandle));
1477
+
1479
1478
m_graphicsContext.AddTexture (texture->Handle (), width, height, generateMips, 1 , format);
1480
1479
1481
1480
Graphics::FrameBuffer* frameBuffer = new Graphics::FrameBuffer (m_graphicsContext, frameBufferHandle, width, height, false , generateDepth, generateStencilBuffer);
0 commit comments