Skip to content

Support external render target textures #1126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Plugins/ExternalTexture/Source/ExternalTexture_D3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ namespace Babylon::Plugins
info.Height = static_cast<uint16_t>(desc.Height);
info.MipLevels = static_cast<uint16_t>(desc.MipLevels);

if ((desc.BindFlags & D3D11_BIND_RENDER_TARGET) != 0)
{
info.Flags |= BGFX_TEXTURE_RT;
}

for (int i = 0; i < BX_COUNTOF(s_textureFormat); ++i)
{
const auto& format = s_textureFormat[i];
Expand Down
5 changes: 5 additions & 0 deletions Plugins/ExternalTexture/Source/ExternalTexture_D3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ namespace Babylon::Plugins
info.Height = static_cast<uint16_t>(desc.Height);
info.MipLevels = static_cast<uint16_t>(desc.MipLevels);

if ((desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) != 0)
{
info.Flags |= BGFX_TEXTURE_RT;
}

for (int i = 0; i < BX_COUNTOF(s_textureFormat); ++i)
{
const auto& format = s_textureFormat[i];
Expand Down
9 changes: 7 additions & 2 deletions Plugins/ExternalTexture/Source/ExternalTexture_Metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,21 @@ void GetInfo(Graphics::TextureT ptr, Info& info)
info.Height = static_cast<uint16_t>(ptr.height);
info.MipLevels = static_cast<uint16_t>(ptr.mipmapLevelCount);

if ((ptr.usage & MTLTextureUsageRenderTarget) != 0)
{
info.Flags |= BGFX_TEXTURE_RT;
}

const auto pixelFormat = m_ptr.pixelFormat;
for (size_t i = 0; i < BX_COUNTOF(s_textureFormat); ++i)
{
const auto& format = s_textureFormat[i];
if (format.m_fmt == pixelFormat || format.m_fmtSrgb == pixelFormat)
{
info.format = static_cast<bgfx::TextureFormat::Enum>(i);
info.Format = static_cast<bgfx::TextureFormat::Enum>(i);
if (format.m_fmtSrgb == pixelFormat)
{
info.flags |= BGFX_TEXTURE_SRGB;
info.Flags |= BGFX_TEXTURE_SRGB;
}
break;
}
Expand Down
41 changes: 20 additions & 21 deletions Plugins/NativeEngine/Source/NativeEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1443,39 +1443,38 @@ namespace Babylon
bool generateDepth = info[5].As<Napi::Boolean>();
bool generateMips = info[6].As<Napi::Boolean>();

bgfx::FrameBufferHandle frameBufferHandle{};
if (generateDepth)
assert(bgfx::isTextureValid(0, false, 1, format, BGFX_TEXTURE_RT));

std::array<bgfx::Attachment, 2> attachments{};
uint8_t numAttachments = 0;

if (!texture->IsValid())
{
auto handle = bgfx::createTexture2D(width, height, generateMips, 1, format, BGFX_TEXTURE_RT);
texture->Attach(handle, false, width, height, generateMips, 1, format, BGFX_TEXTURE_RT);
}
attachments[numAttachments++].init(texture->Handle());

if (generateStencilBuffer || generateDepth)
{
auto depthStencilFormat = bgfx::TextureFormat::D32;
if (generateStencilBuffer)
if (generateStencilBuffer != generateDepth)
{
depthStencilFormat = bgfx::TextureFormat::D24S8;
JsConsoleLogger::LogWarn(info.Env(), "The flags generateDepth/generateStencilBuffer are mismatched. Depth and stencil are combined in one texture");
}

assert(bgfx::isTextureValid(0, false, 1, format, BGFX_TEXTURE_RT));
const auto depthStencilFormat{generateStencilBuffer ? bgfx::TextureFormat::D24S8 : bgfx::TextureFormat::D32};
assert(bgfx::isTextureValid(0, false, 1, depthStencilFormat, BGFX_TEXTURE_RT));

// bgfx doesn't add flag D3D11_RESOURCE_MISC_GENERATE_MIPS for depth textures (missing that flag will crash D3D with resolving)
// And not sure it makes sense to generate mipmaps from a depth buffer with exponential values.
// only allows mipmaps resolve step when mipmapping is asked and for the color texture, not the depth.
// https://github.com/bkaradzic/bgfx/blob/2c21f68998595fa388e25cb6527e82254d0e9bff/src/renderer_d3d11.cpp#L4525
std::array<bgfx::TextureHandle, 2> textures{
bgfx::createTexture2D(width, height, generateMips, 1, format, BGFX_TEXTURE_RT),
bgfx::createTexture2D(width, height, false, 1, depthStencilFormat, BGFX_TEXTURE_RT)};
std::array<bgfx::Attachment, textures.size()> attachments{};
for (size_t idx = 0; idx < attachments.size(); ++idx)
{
attachments[idx].init(textures[idx]);
}
frameBufferHandle = bgfx::createFrameBuffer(static_cast<uint8_t>(attachments.size()), attachments.data(), true);
}
else
{
assert(!generateStencilBuffer);
frameBufferHandle = bgfx::createFrameBuffer(width, height, format, BGFX_TEXTURE_RT);
attachments[numAttachments++].init(bgfx::createTexture2D(width, height, false, 1, depthStencilFormat, BGFX_TEXTURE_RT));
}

texture->Attach(bgfx::getTexture(frameBufferHandle), false, width, height, generateMips, 1, format, BGFX_TEXTURE_RT);
bgfx::FrameBufferHandle frameBufferHandle = bgfx::createFrameBuffer(numAttachments, attachments.data(), true);
assert(bgfx::isValid(frameBufferHandle));

m_graphicsContext.AddTexture(texture->Handle(), width, height, generateMips, 1, format);

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