1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-11-22 18:28:53 -05:00

add UseGpuAcceleration

This commit is contained in:
Evan Pezent 2021-07-02 20:26:02 -07:00
commit 8f1c9320e2
5 changed files with 46 additions and 31 deletions

View File

@ -53,8 +53,8 @@ struct HeatmapData
HeatmapShader* ShaderProgram; ///< Shader to be used by this heatmap (either ShaderInt or ShaderFloat)
GLuint HeatmapTexID; ///< Texture ID of the heatmap 2D texture
GLuint ColormapTexID; ///< Texture ID of the colormap 1D texture
ImVec2 MinBounds; ///< Minimum bounds of the heatmap
ImVec2 MaxBounds; ///< Maximum bounds of the heatmap
ImPlotPoint MinBounds; ///< Minimum bounds of the heatmap
ImPlotPoint MaxBounds; ///< Maximum bounds of the heatmap
float MinValue; ///< Minimum value of the colormap
float MaxValue; ///< Maximum value of the colormap
bool AxisLogX; ///< Whether the X axis is logarithmic or not
@ -111,8 +111,8 @@ void DestroyContext()
"\n" \
"void main()\n" \
"{\n" \
" Frag_UV = UV;\n" \
" gl_Position = ProjMtx * vec4(Position.xy, 0.0f, 1.0f);\n" \
" Frag_UV = UV;\n" \
" gl_Position = ProjMtx * vec4(Position.xy, 0.0f, 1.0f);\n" \
"}\n"
#define HEATMAP_FRAGMENT_SHADER_CODE \
@ -141,12 +141,14 @@ void DestroyContext()
"\n" \
"void main()\n" \
"{\n" \
" float min_tex_offs = 0.5 / float(textureSize(colormap, 0));\n" \
" float uv_x = ax_log.x ? log_den(Frag_UV.x, bounds_min.x, bounds_max.x) : Frag_UV.x;\n" \
" float uv_y = ax_log.y ? log_den(Frag_UV.y, bounds_min.y, bounds_max.y) : Frag_UV.y;\n" \
"\n" \
" float value = float(texture(heatmap, vec2(uv_x, uv_y)).r);\n" \
" float offset = (value - min_val) / (max_val - min_val);\n" \
" Out_Color = texture(colormap, clamp(offset, 0.0f, 1.0f));\n" \
" float offset = (value - min_val) / (max_val - min_val);\n" \
" offset = mix(min_tex_offs, 1.0 - min_tex_offs, clamp(offset, 0.0f, 1.0f));\n" \
" Out_Color = texture(colormap, offset);\n" \
"}\n"
static void CompileShader(HeatmapShader& ShaderProgram, GLchar* VertexShaderCode, GLchar* FragmentShaderCode)
@ -231,8 +233,8 @@ static void RenderCallback(const ImDrawList*, const ImDrawCmd* cmd)
glUniform1f(data.ShaderProgram->AttribLocationMinValue, data.MinValue); // Set minimum range
glUniform1f(data.ShaderProgram->AttribLocationMaxValue, data.MaxValue); // Set maximum range
glUniform2i(data.ShaderProgram->AttribLocationAxisLog, data.AxisLogX, data.AxisLogY); // Logarithmic axis
glUniform2f(data.ShaderProgram->AttribLocationMinBounds, data.MinBounds.x, data.MinBounds.y); // Set minimum bounds
glUniform2f(data.ShaderProgram->AttribLocationMaxBounds, data.MaxBounds.x, data.MaxBounds.y); // Set maximum bounds
glUniform2f(data.ShaderProgram->AttribLocationMinBounds, (float)data.MinBounds.x, (float)data.MinBounds.y); // Set minimum bounds
glUniform2f(data.ShaderProgram->AttribLocationMaxBounds, (float)data.MaxBounds.x, (float)data.MaxBounds.y); // Set maximum bounds
}
static void ResetState(const ImDrawList*, const ImDrawCmd*)
@ -262,7 +264,7 @@ void AddColormap(const ImU32* keys, int count, bool qual)
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_1D, textureID);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, count, 0, GL_RGBA, GL_UNSIGNED_BYTE, keys);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, count, 0, GL_RGBA, GL_UNSIGNED_BYTE, keys);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, qual ? GL_NEAREST : GL_LINEAR);
@ -332,19 +334,19 @@ void SetHeatmapData(int itemID, const ImU64* values, int rows, int cols)
SetTextureData(itemID, Context.temp3.Data, rows, cols, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT);
}
void SetAxisLog(int itemID, bool x_is_log, bool y_is_log, const ImVec2& bounds_min, const ImVec2& bounds_max)
void SetAxisLog(int itemID, bool x_is_log, bool y_is_log, const ImPlotPoint& bounds_min, const ImPlotPoint& bounds_max)
{
ContextData& Context = *((ContextData*)GImPlot->backendCtx);
int idx = Context.ItemIDs.GetInt(itemID, -1);
HeatmapData& data = Context.HeatmapDataList[idx];
data.AxisLogX = x_is_log;
data.AxisLogY =y_is_log;
data.AxisLogY = y_is_log;
data.MinBounds = bounds_min;
data.MaxBounds = bounds_max;
}
void RenderHeatmap(int itemID, ImDrawList& DrawList, const ImVec2& bounds_min, const ImVec2& bounds_max, float scale_min, float scale_max, ImPlotColormap colormap)
void RenderHeatmap(int itemID, ImDrawList& DrawList, const ImVec2& bounds_min, const ImVec2& bounds_max, float scale_min, float scale_max, ImPlotColormap colormap, bool reverse_y)
{
ContextData& Context = *((ContextData*)GImPlot->backendCtx);
int idx = Context.ItemIDs.GetInt(itemID, Context.HeatmapDataList.Size);
@ -367,7 +369,7 @@ void RenderHeatmap(int itemID, ImDrawList& DrawList, const ImVec2& bounds_min, c
DrawList.AddCallback(RenderCallback, (void*)(intptr_t)itemID);
DrawList.PrimReserve(6, 4);
DrawList.PrimRectUV(bounds_min, bounds_max, ImVec2(0.0f, 1.0f), ImVec2(1.0f, 0.0f), 0);
DrawList.PrimRectUV(bounds_min, bounds_max, ImVec2(0.0f, reverse_y ? 1.0f : 0.0f), ImVec2(1.0f, reverse_y ? 0.0f : 1.0f), 0);
DrawList.AddCallback(ResetState, nullptr);
}

View File

@ -188,7 +188,7 @@ IMPLOT_API void SetHeatmapData(int itemID, const ImU64* values, int rows, int c
*/
IMPLOT_API void RenderHeatmap(
int itemID, ImDrawList& DrawList, const ImVec2& bounds_min, const ImVec2& bounds_max,
float scale_min, float scale_max, ImPlotColormap colormap);
float scale_min, float scale_max, ImPlotColormap colormap, bool reverse_y);
/**
* @brief Set logarithmic axis
@ -202,7 +202,7 @@ IMPLOT_API void RenderHeatmap(
* @param bounds_min Minimum bounds (for X & Y) of the heatmap
* @param bounds_min Maximum bounds (for X & Y) of the heatmap
*/
void SetAxisLog(int itemID, bool x_is_log, bool y_is_log, const ImVec2& bounds_min, const ImVec2& bounds_max);
void SetAxisLog(int itemID, bool x_is_log, bool y_is_log, const ImPlotPoint& bounds_min, const ImPlotPoint& bounds_max);
}
}

View File

@ -145,10 +145,16 @@ ImPlotStyle::ImPlotStyle() {
Colormap = ImPlotColormap_Deep;
AntiAliasedLines = false;
UseLocalTime = false;
Use24HourClock = false;
UseISO8601 = false;
AntiAliasedLines = false;
#ifdef IMPLOT_BACKEND_ENABLED
UseGpuAacceleration = true;
#else
UseGpuAacceleration = false;
#endif
}
ImPlotItem* ImPlotPlot::GetLegendItem(int i) {
@ -3678,6 +3684,9 @@ void ShowStyleEditor(ImPlotStyle* ref) {
float indent = ImGui::CalcItemWidth() - ImGui::GetFrameHeight();
ImGui::Indent(ImGui::CalcItemWidth() - ImGui::GetFrameHeight());
ImGui::Checkbox("AntiAliasedLines", &style.AntiAliasedLines);
#ifdef IMPLOT_BACKEND_ENABLED
ImGui::Checkbox("UseGpuAacceleration", &style.UseGpuAacceleration);
#endif
ImGui::Unindent(indent);
ImGui::Text("Plot Styling");
ImGui::SliderFloat("PlotBorderSize", &style.PlotBorderSize, 0.0f, 2.0f, "%.0f");

View File

@ -307,10 +307,11 @@ struct ImPlotStyle {
// colormap
ImPlotColormap Colormap; // The current colormap. Set this to either an ImPlotColormap_ enum or an index returned by AddColormap.
// settings/flags
bool AntiAliasedLines; // = false, enable global anti-aliasing on plot lines (overrides ImPlotFlags_AntiAliased)
bool UseLocalTime; // = false, axis labels will be formatted for your timezone when ImPlotAxisFlag_Time is enabled
bool UseISO8601; // = false, dates will be formatted according to ISO 8601 where applicable (e.g. YYYY-MM-DD, YYYY-MM, --MM-DD, etc.)
bool Use24HourClock; // = false, times will be formatted using a 24 hour clock
bool AntiAliasedLines; // = false, enable global anti-aliasing on plot lines (overrides ImPlotFlags_AntiAliased)
bool UseGpuAacceleration; // = true*, GPU acceleration will be enabled where your backend supports it (*only true if a backend is enabled, false otherwise)
IMPLOT_API ImPlotStyle();
};

View File

@ -1893,22 +1893,25 @@ void RenderHeatmap(Transformer transformer, ImDrawList& DrawList, const T* value
const double yref = reverse_y ? bounds_max.y : bounds_min.y;
const double ydir = reverse_y ? -1 : 1;
#ifdef IMPLOT_BACKEND_HAS_HEATMAP
ImVec2 bmin = transformer(bounds_min);
ImVec2 bmax = transformer(bounds_max);
if (GImPlot->Style.UseGpuAacceleration) {
ImVec2 bmin = transformer(bounds_min);
ImVec2 bmax = transformer(bounds_max);
ImPlotScale scale = GetCurrentScale();
ImPlotScale scale = GetCurrentScale();
// NOTE: Order is important!
Backend::RenderHeatmap(gp.CurrentItem->ID, DrawList, bmin, bmax, (float)scale_min, (float)scale_max, gp.Style.Colormap);
Backend::SetAxisLog(gp.CurrentItem->ID,
scale == ImPlotScale_LogLin || scale == ImPlotScale_LogLog,
scale == ImPlotScale_LinLog || scale == ImPlotScale_LogLog,
ImVec2((float)bounds_min.x, (float)bounds_min.y), ImVec2((float)bounds_max.x, (float)bounds_max.y));
Backend::SetHeatmapData(gp.CurrentItem->ID, values, rows, cols);
#else
GetterHeatmap<T> getter(values, rows, cols, scale_min, scale_max, (bounds_max.x - bounds_min.x) / cols, (bounds_max.y - bounds_min.y) / rows, bounds_min.x, yref, ydir);
RenderPrimitives(RectRenderer<GetterHeatmap<T>, Transformer>(getter, transformer), DrawList, gp.CurrentPlot->PlotRect);
// NOTE: Order is important!
Backend::RenderHeatmap(gp.CurrentItem->ID, DrawList, bmin, bmax, (float)scale_min, (float)scale_max, gp.Style.Colormap, reverse_y);
Backend::SetAxisLog(gp.CurrentItem->ID,
scale == ImPlotScale_LogLin || scale == ImPlotScale_LogLog,
scale == ImPlotScale_LinLog || scale == ImPlotScale_LogLog,
bounds_min, bounds_max);
Backend::SetHeatmapData(gp.CurrentItem->ID, values, rows, cols);
}
else
#endif
{
GetterHeatmap<T> getter(values, rows, cols, scale_min, scale_max, (bounds_max.x - bounds_min.x) / cols, (bounds_max.y - bounds_min.y) / rows, bounds_min.x, yref, ydir);
RenderPrimitives(RectRenderer<GetterHeatmap<T>, Transformer>(getter, transformer), DrawList, gp.CurrentPlot->PlotRect);
}
if (fmt != NULL) {
const double w = (bounds_max.x - bounds_min.x) / cols;
const double h = (bounds_max.y - bounds_min.y) / rows;