Skip to content

Commit 42e2d2a

Browse files
authored
Support external render target textures (#1126)
1 parent 7a0574f commit 42e2d2a

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

Plugins/ExternalTexture/Source/ExternalTexture_D3D11.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ namespace Babylon::Plugins
163163
info.Height = static_cast<uint16_t>(desc.Height);
164164
info.MipLevels = static_cast<uint16_t>(desc.MipLevels);
165165

166+
if ((desc.BindFlags & D3D11_BIND_RENDER_TARGET) != 0)
167+
{
168+
info.Flags |= BGFX_TEXTURE_RT;
169+
}
170+
166171
for (int i = 0; i < BX_COUNTOF(s_textureFormat); ++i)
167172
{
168173
const auto& format = s_textureFormat[i];

Plugins/ExternalTexture/Source/ExternalTexture_D3D12.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ namespace Babylon::Plugins
157157
info.Height = static_cast<uint16_t>(desc.Height);
158158
info.MipLevels = static_cast<uint16_t>(desc.MipLevels);
159159

160+
if ((desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) != 0)
161+
{
162+
info.Flags |= BGFX_TEXTURE_RT;
163+
}
164+
160165
for (int i = 0; i < BX_COUNTOF(s_textureFormat); ++i)
161166
{
162167
const auto& format = s_textureFormat[i];

Plugins/ExternalTexture/Source/ExternalTexture_Metal.mm

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,21 @@ void GetInfo(Graphics::TextureT ptr, Info& info)
145145
info.Height = static_cast<uint16_t>(ptr.height);
146146
info.MipLevels = static_cast<uint16_t>(ptr.mipmapLevelCount);
147147

148+
if ((ptr.usage & MTLTextureUsageRenderTarget) != 0)
149+
{
150+
info.Flags |= BGFX_TEXTURE_RT;
151+
}
152+
148153
const auto pixelFormat = m_ptr.pixelFormat;
149154
for (size_t i = 0; i < BX_COUNTOF(s_textureFormat); ++i)
150155
{
151156
const auto& format = s_textureFormat[i];
152157
if (format.m_fmt == pixelFormat || format.m_fmtSrgb == pixelFormat)
153158
{
154-
info.format = static_cast<bgfx::TextureFormat::Enum>(i);
159+
info.Format = static_cast<bgfx::TextureFormat::Enum>(i);
155160
if (format.m_fmtSrgb == pixelFormat)
156161
{
157-
info.flags |= BGFX_TEXTURE_SRGB;
162+
info.Flags |= BGFX_TEXTURE_SRGB;
158163
}
159164
break;
160165
}

Plugins/NativeEngine/Source/NativeEngine.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,39 +1443,38 @@ namespace Babylon
14431443
bool generateDepth = info[5].As<Napi::Boolean>();
14441444
bool generateMips = info[6].As<Napi::Boolean>();
14451445

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)
14481459
{
1449-
auto depthStencilFormat = bgfx::TextureFormat::D32;
1450-
if (generateStencilBuffer)
1460+
if (generateStencilBuffer != generateDepth)
14511461
{
1452-
depthStencilFormat = bgfx::TextureFormat::D24S8;
1462+
JsConsoleLogger::LogWarn(info.Env(), "The flags generateDepth/generateStencilBuffer are mismatched. Depth and stencil are combined in one texture");
14531463
}
14541464

1455-
assert(bgfx::isTextureValid(0, false, 1, format, BGFX_TEXTURE_RT));
1465+
const auto depthStencilFormat{generateStencilBuffer ? bgfx::TextureFormat::D24S8 : bgfx::TextureFormat::D32};
14561466
assert(bgfx::isTextureValid(0, false, 1, depthStencilFormat, BGFX_TEXTURE_RT));
14571467

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

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

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

0 commit comments

Comments
 (0)