Skip to content

Commit f66b623

Browse files
committed
Initial changes for external render target textures
1 parent 36e45ad commit f66b623

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

Plugins/ExternalTexture/Source/ExternalTexture_D3D11.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ namespace Babylon::Plugins
149149
throw std::runtime_error{"Unsupported texture mip levels"};
150150
}
151151

152+
if ((m_desc.BindFlags & D3D11_BIND_RENDER_TARGET) != 0)
153+
{
154+
m_flags |= BGFX_TEXTURE_RT;
155+
}
156+
152157
for (int i = 0; i < BX_COUNTOF(s_textureFormat); ++i)
153158
{
154159
const auto& info = s_textureFormat[i];

Plugins/ExternalTexture/Source/ExternalTexture_D3D12.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ namespace Babylon::Plugins
147147
throw std::runtime_error{"Unsupported texture mip levels"};
148148
}
149149

150+
if ((m_desc.BindFlags & D3D12_BIND_RENDER_TARGET) != 0)
151+
{
152+
m_flags |= BGFX_TEXTURE_RT;
153+
}
154+
150155
for (int i = 0; i < BX_COUNTOF(s_textureFormat); ++i)
151156
{
152157
const auto& info = s_textureFormat[i];

Plugins/ExternalTexture/Source/ExternalTexture_Metal.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@
135135
m_height = static_cast<uint16_t>(m_ptr.height);
136136
m_hasMips = m_ptr.mipmapLevelCount > 1;
137137

138+
// TODO: how to determine if texture is render target
139+
//{
140+
// m_flags |= BGFX_TEXTURE_RT;
141+
//}
142+
138143
const auto pixelFormat = m_ptr.pixelFormat;
139144
for (size_t i = 0; i < BX_COUNTOF(s_textureFormat); ++i)
140145
{

Plugins/NativeEngine/Source/NativeEngine.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,39 +1324,37 @@ namespace Babylon
13241324
bool generateDepth = info[5].As<Napi::Boolean>();
13251325
bool generateMips = info[6].As<Napi::Boolean>();
13261326

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)
13291339
{
1330-
auto depthStencilFormat = bgfx::TextureFormat::D32;
1331-
if (generateStencilBuffer)
1340+
if (generateStencilBuffer != generateDepth)
13321341
{
1333-
depthStencilFormat = bgfx::TextureFormat::D24S8;
1342+
JsConsoleLogger::LogWarn(info.Env(), "The flags generateDepth/generateStencilBuffer are mismatched. Depth and stencil are combined in one texture");
13341343
}
13351344

1336-
assert(bgfx::isTextureValid(0, false, 1, format, BGFX_TEXTURE_RT));
1345+
const auto depthStencilFormat{generateStencilBuffer ? bgfx::TextureFormat::D24S8 : bgfx::TextureFormat::D32};
13371346
assert(bgfx::isTextureValid(0, false, 1, depthStencilFormat, BGFX_TEXTURE_RT));
13381347

13391348
// bgfx doesn't add flag D3D11_RESOURCE_MISC_GENERATE_MIPS for depth textures (missing that flag will crash D3D with resolving)
13401349
// And not sure it makes sense to generate mipmaps from a depth buffer with exponential values.
13411350
// only allows mipmaps resolve step when mipmapping is asked and for the color texture, not the depth.
13421351
// 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));
13571353
}
13581354

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+
13601358
m_graphicsContext.AddTexture(texture->Handle(), width, height, generateMips, 1, format);
13611359

13621360
Graphics::FrameBuffer* frameBuffer = new Graphics::FrameBuffer(m_graphicsContext, frameBufferHandle, width, height, false, generateDepth, generateStencilBuffer);

0 commit comments

Comments
 (0)