Skip to content

Commit c93057d

Browse files
authored
Update imgui_impl_bgfx.cpp
1 parent ecac754 commit c93057d

1 file changed

Lines changed: 90 additions & 17 deletions

File tree

backends/imgui_impl_bgfx.cpp

Lines changed: 90 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,12 +1075,78 @@ void ImGui_ImplBgfx_NewFrame()
10751075
void ImGui_ImplBgfx_RenderDrawData(ImDrawData* draw_data)
10761076
{
10771077
ImGui_ImplBgfx_Data* bd = ImGui_ImplBgfx_GetBackendData();
1078+
if (NULL != draw_data->Textures)
1079+
{
1080+
for (ImTextureData* texData : *draw_data->Textures)
1081+
{
1082+
switch (texData->Status)
1083+
{
1084+
case ImTextureStatus_WantCreate:
1085+
{
1086+
ImGui::TextureBgfx tex =
1087+
{
1088+
.handle = bgfx::createTexture2D(
1089+
(uint16_t)texData->Width
1090+
, (uint16_t)texData->Height
1091+
, false
1092+
, 1
1093+
, bgfx::TextureFormat::BGRA8
1094+
, 0
1095+
),
1096+
.flags = IMGUI_FLAGS_ALPHA_BLEND,
1097+
.mip = 0,
1098+
.unused = 0,
1099+
};
1100+
1101+
bgfx::setName(tex.handle, "ImGui Font Atlas");
1102+
bgfx::updateTexture2D(tex.handle, 0, 0, 0, 0
1103+
, bx::narrowCast<uint16_t>(texData->Width)
1104+
, bx::narrowCast<uint16_t>(texData->Height)
1105+
, bgfx::copy(texData->GetPixels(), texData->GetSizeInBytes())
1106+
);
1107+
1108+
texData->SetTexID(bx::bitCast<ImTextureID>(tex));
1109+
texData->SetStatus(ImTextureStatus_OK);
1110+
}
1111+
break;
1112+
1113+
case ImTextureStatus_WantDestroy:
1114+
{
1115+
ImGui::TextureBgfx tex = bx::bitCast<ImGui::TextureBgfx>(texData->GetTexID());
1116+
bgfx::destroy(tex.handle);
1117+
texData->SetTexID(ImTextureID_Invalid);
1118+
texData->SetStatus(ImTextureStatus_Destroyed);
1119+
}
1120+
break;
1121+
1122+
case ImTextureStatus_WantUpdates:
1123+
{
1124+
ImGui::TextureBgfx tex = bx::bitCast<ImGui::TextureBgfx>(texData->GetTexID());
1125+
1126+
for (ImTextureRect& rect : texData->Updates)
1127+
{
1128+
const uint32_t bpp = texData->BytesPerPixel;
1129+
const bgfx::Memory* pix = bgfx::alloc(rect.h * rect.w * bpp);
1130+
bx::gather(pix->data, texData->GetPixelsAt(rect.x, rect.y), texData->GetPitch(), rect.w * bpp, rect.h);
1131+
bgfx::updateTexture2D(tex.handle, 0, 0, rect.x, rect.y, rect.w, rect.h, pix);
1132+
}
1133+
}
1134+
break;
1135+
1136+
default:
1137+
break;
1138+
}
1139+
}
1140+
}
10781141

10791142
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
1080-
int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
1081-
int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
1082-
if (fb_width <= 0 || fb_height <= 0)
1143+
int32_t dispWidth = int32_t(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
1144+
int32_t dispHeight = int32_t(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
1145+
if (dispWidth <= 0
1146+
|| dispHeight <= 0)
1147+
{
10831148
return;
1149+
}
10841150

10851151
bgfx::setViewName(bd->ViewId, "ImGui");
10861152
bgfx::setViewMode(bd->ViewId, bgfx::ViewMode::Sequential);
@@ -1142,21 +1208,24 @@ void ImGui_ImplBgfx_RenderDrawData(ImDrawData* draw_data)
11421208
| BGFX_STATE_MSAA
11431209
;
11441210

1145-
bgfx::TextureHandle th = bd->FontTexture;
1211+
bgfx::TextureHandle th = BGFX_INVALID_HANDLE;
11461212
bgfx::ProgramHandle program = bd->Program;
11471213

1148-
if (NULL != cmd->GetTexID())
1214+
const ImTextureID texId = cmd->GetTexID();
1215+
1216+
if (ImTextureID_Invalid != texId)
11491217
{
1150-
union { ImTextureID ptr; struct { bgfx::TextureHandle handle; uint8_t flags; uint8_t mip; } s; } texture = { cmd->GetTexID() };
1218+
ImGui::TextureBgfx tex = bx::bitCast<ImGui::TextureBgfx>(texId);
11511219

1152-
state |= 0 != (IMGUI_FLAGS_ALPHA_BLEND & texture.s.flags)
1220+
state |= 0 != (IMGUI_FLAGS_ALPHA_BLEND & tex.flags)
11531221
? BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
11541222
: BGFX_STATE_NONE
11551223
;
1156-
th = texture.s.handle;
1157-
if (0 != texture.s.mip)
1224+
th = tex.handle;
1225+
1226+
if (0 != tex.mip)
11581227
{
1159-
const float lodEnabled[4] = { float(texture.s.mip), 1.0f, 0.0f, 0.0f };
1228+
const float lodEnabled[4] = { float(tex.mip), 1.0f, 0.0f, 0.0f };
11601229
bgfx::setUniform(bd->ImageLodEnabled, lodEnabled);
11611230
program = bd->ImageProgram;
11621231
}
@@ -1173,8 +1242,8 @@ void ImGui_ImplBgfx_RenderDrawData(ImDrawData* draw_data)
11731242
clipRect.z = (cmd->ClipRect.z - clipPos.x) * clipScale.x;
11741243
clipRect.w = (cmd->ClipRect.w - clipPos.y) * clipScale.y;
11751244

1176-
if (clipRect.x < fb_width
1177-
&& clipRect.y < fb_height
1245+
if (clipRect.x < dispWidth
1246+
&& clipRect.y < dispHeight
11781247
&& clipRect.z >= 0.0f
11791248
&& clipRect.w >= 0.0f)
11801249
{
@@ -1281,11 +1350,15 @@ namespace ImGui
12811350
{
12821351
inline ImTextureID toId(bgfx::TextureHandle _handle, uint8_t _flags, uint8_t _mip)
12831352
{
1284-
union { struct { bgfx::TextureHandle handle; uint8_t flags; uint8_t mip; } s; ImTextureID id; } tex;
1285-
tex.s.handle = _handle;
1286-
tex.s.flags = _flags;
1287-
tex.s.mip = _mip;
1288-
return tex.id;
1353+
TextureBgfx tex
1354+
{
1355+
.handle = _handle,
1356+
.flags = _flags,
1357+
.mip = _mip,
1358+
.unused = 0,
1359+
};
1360+
1361+
return bx::bitCast<ImTextureID>(tex);
12891362
}
12901363

12911364
void Image(bgfx::TextureHandle handle, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tintCol, const ImVec4& borderCol)

0 commit comments

Comments
 (0)