epezent / implot

Immediate Mode Plotting
MIT License
4.71k stars 521 forks source link

ImPlot Demo :: { Heatmaps, Histograms } are broken #228

Closed inobelar closed 3 years ago

inobelar commented 3 years ago

Clicking over "Heatmaps" and "Histograms" collapsible headers in "ImPlot demo" causes the next assertion:

Assertion failed: draw_list->_VtxCurrentIdx < (1 << 16) && "Too many vertices in ImDrawList using 16-bit indices. Read comment above", at: ../third_party/imgui/imgui.cpp,4269,AddDrawListToDrawData

Note 1:

Note 2: Tested in native and wasm imgui, even in latest docking branch. So possibly it is not imgui-relevant, but implot-relevant.

Note 3: the same assertion in v0.9and v0.8 ImPlot versions

Note 4: Assertion even in case of single ImPlot::ShowDemoWindow();, without any other ImGui widgets.

Note 5: During debugging "Heatmaps", found that everything works until called 2) and 3) ImPlot::PlotHeatmap() functions. After commenting them, everything works ok.


    if (ImGui::CollapsingHeader("Heatmaps")) {
        // ...  
        const int size = 200; /* 1) <-- size constant, which determines row & col count*/
        static double values2[size*size];
        srand((unsigned int)(DEMO_TIME*1000000));
        for (int i = 0; i < size*size; ++i)
            values2[i] = RandomRange(0.0,1.0);

        ImPlot::SetNextPlotLimits(-1,1,-1,1);
        if (ImPlot::BeginPlot("##Heatmap2",NULL,NULL,ImVec2(225,225),0,ImPlotAxisFlags_NoDecorations,ImPlotAxisFlags_NoDecorations)) {
            /* 2) --> */ ImPlot::PlotHeatmap("heat1",values2,size,size,0,1,NULL);
            /* 3) --> */ ImPlot::PlotHeatmap("heat2",values2,size,size,0,1,NULL, ImPlotPoint(-1,-1), ImPlotPoint(0,0));
            ImPlot::EndPlot();
        }
        // ...
    }

After un-commenting them, but with lowering size constant (1)) from 200 down to 75 makes that two (2) and 3)) heatmaps works too.

Since "##Heatmap1" always displayed correctly (but it's size 7x7, which is kinda small), it seems like ImPlot::PlotHeatmap() produces too many vertices into ImDrawList, when passed rows and cols values too high.


ImGui version:

v1.82
io.BackendPlatformName: imgui_impl_sdl
io.BackendRendererName: imgui_impl_opengl3

ImPlot version (currently latest):

v0.10 (WIP)
branch: master
commit: 555ff688a8134bc0c602123149abe9c17d577475
hinxx commented 3 years ago

Maybe using larger ImDrawIdx data type would help. See imconfig.h in imgui sources.

inobelar commented 3 years ago

@hinxx Thanks! After un-commenting that line (for making 32-bit type, instead of 16-bit) everything works (both in native and wasm).

To be honest, I was very surprised that the indexes are 16-bit by default (I always thought they were 32-bit). Looking at the git-history of this file, I find that this has been introduced since version 1.50!

After careful reading of ImPlot::README.md, I found the next note (in Special Notes) section, which describes that case (shame on me, I must RTFM first :smile: ):

If you experience data truncation and/or visual glitches, it is HIGHLY recommended that you EITHER:
    - Handle the ImGuiBackendFlags_RendererHasVtxOffset flag in your renderer when using 16-bit indices (the official OpenGL3 renderer supports this) and use an ImGui version with patch imgui@f6120f8, OR...
    - Enable 32-bit indices by uncommenting #define ImDrawIdx unsigned int in your ImGui imconfig.h file.

Proposal

@epezent Well ... after all, (as a complete newbie who took the library for the first time in a rush), I still want to suggest the following additions in the README (styled by "huge bold font" to not miss it):

  1. "Read this README fully & carefully before first usage" :smile:
  2. "Dear ImGui uses 16-bit indexing by default, some ImPlot widgets, like ImPlot::PlotHeatmap() may produce too many vertices into ImDrawList (depending on passed args), which causes assertion failure. For safety, please make it 32-bit, by editing ImGui::imconfig.h"
    • As a newbie, until now I even don't know about indexes type-tweaking, since everything works out-the-box without it.
    • Any visual glitches not being seen, even with all "ImPlot Demo" section opened (except "Heatmaps" & "Histograms"), even with additional extra heavy 'ImGui Demo' window & some extra widgets. That 2 sections simply explode.
    • I think, that this is not only my case, so currently is required to use 32-bit indices, not just for running 'ImPlot Demo'
epezent commented 3 years ago

Yes we can make this more emphasized in the RRADME. Good idea.

epezent commented 3 years ago

The README has been updated to include a Extremely Important Note header devoted solely to this issue.

epezent commented 3 years ago

image

I've added a runtime warning to the demo. Hopefully future users will catch this along with the README updates.

inobelar commented 3 years ago

@epezent wow, great idea! Thanks