MCPcopy Create free account
hub / github.com/VictorGordan/opengl-tutorials / Merge

Method Merge

ImGUI GLFW Tutorial/imgui/imgui_draw.cpp:1705–1774  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1703}
1704
1705void ImDrawListSplitter::Merge(ImDrawList* draw_list)
1706{
1707 // Note that we never use or rely on _Channels.Size because it is merely a buffer that we never shrink back to 0 to keep all sub-buffers ready for use.
1708 if (_Count <= 1)
1709 return;
1710
1711 SetCurrentChannel(draw_list, 0);
1712 draw_list->_PopUnusedDrawCmd();
1713
1714 // Calculate our final buffer sizes. Also fix the incorrect IdxOffset values in each command.
1715 int new_cmd_buffer_count = 0;
1716 int new_idx_buffer_count = 0;
1717 ImDrawCmd* last_cmd = (_Count > 0 && draw_list->CmdBuffer.Size > 0) ? &draw_list->CmdBuffer.back() : NULL;
1718 int idx_offset = last_cmd ? last_cmd->IdxOffset + last_cmd->ElemCount : 0;
1719 for (int i = 1; i < _Count; i++)
1720 {
1721 ImDrawChannel& ch = _Channels[i];
1722
1723 // Equivalent of PopUnusedDrawCmd() for this channel's cmdbuffer and except we don't need to test for UserCallback.
1724 if (ch._CmdBuffer.Size > 0 && ch._CmdBuffer.back().ElemCount == 0)
1725 ch._CmdBuffer.pop_back();
1726
1727 if (ch._CmdBuffer.Size > 0 && last_cmd != NULL)
1728 {
1729 ImDrawCmd* next_cmd = &ch._CmdBuffer[0];
1730 if (ImDrawCmd_HeaderCompare(last_cmd, next_cmd) == 0 && last_cmd->UserCallback == NULL && next_cmd->UserCallback == NULL)
1731 {
1732 // Merge previous channel last draw command with current channel first draw command if matching.
1733 last_cmd->ElemCount += next_cmd->ElemCount;
1734 idx_offset += next_cmd->ElemCount;
1735 ch._CmdBuffer.erase(ch._CmdBuffer.Data); // FIXME-OPT: Improve for multiple merges.
1736 }
1737 }
1738 if (ch._CmdBuffer.Size > 0)
1739 last_cmd = &ch._CmdBuffer.back();
1740 new_cmd_buffer_count += ch._CmdBuffer.Size;
1741 new_idx_buffer_count += ch._IdxBuffer.Size;
1742 for (int cmd_n = 0; cmd_n < ch._CmdBuffer.Size; cmd_n++)
1743 {
1744 ch._CmdBuffer.Data[cmd_n].IdxOffset = idx_offset;
1745 idx_offset += ch._CmdBuffer.Data[cmd_n].ElemCount;
1746 }
1747 }
1748 draw_list->CmdBuffer.resize(draw_list->CmdBuffer.Size + new_cmd_buffer_count);
1749 draw_list->IdxBuffer.resize(draw_list->IdxBuffer.Size + new_idx_buffer_count);
1750
1751 // Write commands and indices in order (they are fairly small structures, we don't copy vertices only indices)
1752 ImDrawCmd* cmd_write = draw_list->CmdBuffer.Data + draw_list->CmdBuffer.Size - new_cmd_buffer_count;
1753 ImDrawIdx* idx_write = draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size - new_idx_buffer_count;
1754 for (int i = 1; i < _Count; i++)
1755 {
1756 ImDrawChannel& ch = _Channels[i];
1757 if (int sz = ch._CmdBuffer.Size) { memcpy(cmd_write, ch._CmdBuffer.Data, sz * sizeof(ImDrawCmd)); cmd_write += sz; }
1758 if (int sz = ch._IdxBuffer.Size) { memcpy(idx_write, ch._IdxBuffer.Data, sz * sizeof(ImDrawIdx)); idx_write += sz; }
1759 }
1760 draw_list->_IdxWritePtr = idx_write;
1761
1762 // Ensure there's always a non-callback draw command trailing the command-buffer

Callers 3

EndTableMethod · 0.45
EndColumnsMethod · 0.45
ChannelsMergeMethod · 0.45

Calls 4

_PopUnusedDrawCmdMethod · 0.45
pop_backMethod · 0.45
eraseMethod · 0.45
AddDrawCmdMethod · 0.45

Tested by

no test coverage detected