@@ -1324,39 +1324,37 @@ namespace Babylon
1324
1324
bool generateDepth = info[5 ].As <Napi::Boolean>();
1325
1325
bool generateMips = info[6 ].As <Napi::Boolean>();
1326
1326
1327
- bgfx::FrameBufferHandle frameBufferHandle{};
1328
- if (generateDepth)
1327
+ assert (bgfx::isTextureValid (0 , false , 1 , format, BGFX_TEXTURE_RT));
1328
+
1329
+ std::array<bgfx::Attachment, 2 > attachments{};
1330
+ uint8_t numAttachments = 0 ;
1331
+
1332
+ if (!texture->IsValid ())
1333
+ {
1334
+ texture->Attach (bgfx::createTexture2D (width, height, generateMips, 1 , format, BGFX_TEXTURE_RT), false , width, height);
1335
+ }
1336
+ attachments[numAttachments++].init (texture->Handle ());
1337
+
1338
+ if (generateStencilBuffer || generateDepth)
1329
1339
{
1330
- auto depthStencilFormat = bgfx::TextureFormat::D32;
1331
- if (generateStencilBuffer)
1340
+ if (generateStencilBuffer != generateDepth)
1332
1341
{
1333
- depthStencilFormat = bgfx::TextureFormat::D24S8 ;
1342
+ JsConsoleLogger::LogWarn (info. Env (), " The flags generateDepth/generateStencilBuffer are mismatched. Depth and stencil are combined in one texture " ) ;
1334
1343
}
1335
1344
1336
- assert ( bgfx::isTextureValid ( 0 , false , 1 , format, BGFX_TEXTURE_RT)) ;
1345
+ const auto depthStencilFormat{generateStencilBuffer ? bgfx::TextureFormat::D24S8 : bgfx::TextureFormat::D32} ;
1337
1346
assert (bgfx::isTextureValid (0 , false , 1 , depthStencilFormat, BGFX_TEXTURE_RT));
1338
1347
1339
1348
// bgfx doesn't add flag D3D11_RESOURCE_MISC_GENERATE_MIPS for depth textures (missing that flag will crash D3D with resolving)
1340
1349
// And not sure it makes sense to generate mipmaps from a depth buffer with exponential values.
1341
1350
// only allows mipmaps resolve step when mipmapping is asked and for the color texture, not the depth.
1342
1351
// https://github.com/bkaradzic/bgfx/blob/2c21f68998595fa388e25cb6527e82254d0e9bff/src/renderer_d3d11.cpp#L4525
1343
- std::array<bgfx::TextureHandle, 2 > textures{
1344
- bgfx::createTexture2D (width, height, generateMips, 1 , format, BGFX_TEXTURE_RT),
1345
- bgfx::createTexture2D (width, height, false , 1 , depthStencilFormat, BGFX_TEXTURE_RT)};
1346
- std::array<bgfx::Attachment, textures.size ()> attachments{};
1347
- for (size_t idx = 0 ; idx < attachments.size (); ++idx)
1348
- {
1349
- attachments[idx].init (textures[idx]);
1350
- }
1351
- frameBufferHandle = bgfx::createFrameBuffer (static_cast <uint8_t >(attachments.size ()), attachments.data (), true );
1352
- }
1353
- else
1354
- {
1355
- assert (!generateStencilBuffer);
1356
- frameBufferHandle = bgfx::createFrameBuffer (width, height, format, BGFX_TEXTURE_RT);
1352
+ attachments[numAttachments++].init (bgfx::createTexture2D (width, height, false , 1 , depthStencilFormat, BGFX_TEXTURE_RT));
1357
1353
}
1358
1354
1359
- texture->Attach (bgfx::getTexture (frameBufferHandle), false , width, height);
1355
+ bgfx::FrameBufferHandle frameBufferHandle = bgfx::createFrameBuffer (numAttachments, attachments.data (), true );
1356
+ assert (bgfx::isValid (frameBufferHandle));
1357
+
1360
1358
m_graphicsContext.AddTexture (texture->Handle (), width, height, generateMips, 1 , format);
1361
1359
1362
1360
Graphics::FrameBuffer* frameBuffer = new Graphics::FrameBuffer (m_graphicsContext, frameBufferHandle, width, height, false , generateDepth, generateStencilBuffer);
0 commit comments