1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2025-04-22 08:18:50 -04:00

Cleanup & maintenance

- Removed unnecessary attributes from shader
- Renamed functions & structs
- Reduced memory footprint of OpenGL backend context
This commit is contained in:
marcizhu 2021-06-23 13:37:55 +02:00
parent 541521fe69
commit 27769a846f
No known key found for this signature in database
GPG Key ID: 2D8FA5B173E88095
2 changed files with 92 additions and 93 deletions

View File

@ -35,6 +35,10 @@
namespace ImPlot { namespace ImPlot {
namespace Backend { namespace Backend {
//-----------------------------------------------------------------------------
// [SECTION] Misc backend functions
//-----------------------------------------------------------------------------
/** /**
* @brief Struct to hold backend-related context data * @brief Struct to hold backend-related context data
* *
@ -62,6 +66,16 @@ IMPLOT_API void* CreateContext();
*/ */
IMPLOT_API void DestroyContext(); IMPLOT_API void DestroyContext();
/** @brief Bust plot cache. Called from @ref ImPlot::BustPlotCache() */
IMPLOT_API void BustPlotCache();
/** @brief Bust item cache. Called from @ref ImPlot::BustItemCache() */
IMPLOT_API void BustItemCache();
//-----------------------------------------------------------------------------
// [SECTION] Colormap functions
//-----------------------------------------------------------------------------
/** /**
* @brief Add a colormap * @brief Add a colormap
* *
@ -74,6 +88,10 @@ IMPLOT_API void DestroyContext();
*/ */
IMPLOT_API void AddColormap(const ImU32* keys, int count, bool qual); IMPLOT_API void AddColormap(const ImU32* keys, int count, bool qual);
//-----------------------------------------------------------------------------
// [SECTION] Heatmap functions
//-----------------------------------------------------------------------------
/** /**
* @brief Set heatmap data * @brief Set heatmap data
* *
@ -136,12 +154,6 @@ IMPLOT_API void RenderHeatmap(
int plotID, ImDrawList& DrawList, const ImVec2& bounds_min, const ImVec2& bounds_max, int plotID, 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);
/** @brief Bust plot cache. Called from @ref ImPlot::BustPlotCache() */
IMPLOT_API void BustPlotCache();
/** @brief Bust item cache. Called from @ref ImPlot::BustItemCache() */
IMPLOT_API void BustItemCache();
} }
} }

View File

@ -35,40 +35,39 @@ using namespace gl;
namespace ImPlot { namespace ImPlot {
namespace Backend { namespace Backend {
struct Shader struct HeatmapShader
{ {
GLuint ID = 0; ///< Shader ID for the heatmap shader GLuint ID = 0; ///< Shader ID for the heatmap shader
GLuint g_AttribLocationHeatmapSampler = 0; ///< Attribute location for the heatmap texture GLuint AttribLocationProjection = 0; ///< Attribute location for the projection matrix uniform
GLuint g_AttribLocationColormapSampler = 0; ///< Attribute location for the colormap texture GLuint AttribLocationMinValue = 0; ///< Attribute location for the minimum value uniform
GLuint g_AttribLocationProjection = 0; ///< Attribute location for the projection matrix uniform GLuint AttribLocationMaxValue = 0; ///< Attribute location for the maximum value uniform
GLuint g_AttribLocationMinValue = 0; ///< Attribute location for the minimum value uniform
GLuint g_AttribLocationMaxValue = 0; ///< Attribute location for the maximum value uniform
}; };
struct HeatmapData struct HeatmapData
{ {
Shader* ShaderProgram; HeatmapShader* ShaderProgram; ///< Shader to be used by this heatmap (either ShaderInt or ShaderFloat)
GLuint HeatmapTexID; GLuint HeatmapTexID; ///< Texture ID of the heatmap 2D texture
GLuint ColormapTexID; GLuint ColormapTexID; ///< Texture ID of the colormap 1D texture
float MinValue; float MinValue; ///< Minimum value of the colormap
float MaxValue; float MaxValue; ///< Maximum value of the colormap
}; };
struct ContextData struct ContextData
{ {
Shader ShaderInt; ///< Shader for integer heatmaps HeatmapShader ShaderInt; ///< Shader for integer heatmaps
Shader ShaderFloat; ///< Shader for floating-point heatmaps HeatmapShader ShaderFloat; ///< Shader for floating-point heatmaps
GLuint g_AttribLocationImGuiProjection = 0; ///< Attribute location for the projection matrix uniform (ImGui default shader) GLuint AttribLocationImGuiProjection = 0; ///< Attribute location for the projection matrix uniform (ImGui default shader)
GLuint ImGuiShader = 0; ///< Shader ID of ImGui's default shader
ImVector<HeatmapData> HeatmapDataList; ///< Array of heatmap data ImVector<HeatmapData> HeatmapDataList; ///< Array of heatmap data
ImVector<GLuint> ColormapIDs; ///< Texture IDs of the colormap textures ImVector<GLuint> ColormapIDs; ///< Texture IDs of the colormap textures
ImGuiStorage PlotIDs; ///< PlotID <-> Heatmap array index table ImGuiStorage PlotIDs; ///< PlotID <-> Heatmap array index table
ImVector<float> temp1; ImVector<float> temp1; ///< Temporary data
ImVector<ImS32> temp2; ImVector<ImS32> temp2; ///< Temporary data
ImVector<ImU32> temp3; ImVector<ImU32> temp3; ///< Temporary data
}; };
void* CreateContext() void* CreateContext()
@ -93,7 +92,7 @@ void DestroyContext()
Context.PlotIDs.Clear(); Context.PlotIDs.Clear();
} }
#define VERTEX_SHADER_CODE \ #define HEATMAP_VERTEX_SHADER_CODE \
"#version 330 core\n" \ "#version 330 core\n" \
"precision mediump float;\n" \ "precision mediump float;\n" \
"layout (location = %d) in vec2 Position;\n" \ "layout (location = %d) in vec2 Position;\n" \
@ -108,7 +107,7 @@ void DestroyContext()
" gl_Position = ProjMtx * vec4(Position.xy, 0.0f, 1.0f);\n" \ " gl_Position = ProjMtx * vec4(Position.xy, 0.0f, 1.0f);\n" \
"}\n" "}\n"
#define FRAGMENT_SHADER_CODE \ #define HEATMAP_FRAGMENT_SHADER_CODE \
"#version 330 core\n" \ "#version 330 core\n" \
"precision mediump float;\n" \ "precision mediump float;\n" \
"\n" \ "\n" \
@ -127,59 +126,55 @@ void DestroyContext()
" Out_Color = texture(colormap, clamp(offset, 0.0f, 1.0f));\n" \ " Out_Color = texture(colormap, clamp(offset, 0.0f, 1.0f));\n" \
"}\n" "}\n"
static void CompileShader(Shader& ShaderProgram, GLchar* VertexShaderCode, GLchar* FragmentShaderCode) static void CompileShader(HeatmapShader& ShaderProgram, GLchar* VertexShaderCode, GLchar* FragmentShaderCode)
{ {
GLuint g_VertexShader = glCreateShader(GL_VERTEX_SHADER); GLuint VertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(g_VertexShader, 1, &VertexShaderCode, nullptr); glShaderSource(VertexShader, 1, &VertexShaderCode, nullptr);
glCompileShader(g_VertexShader); glCompileShader(VertexShader);
GLuint g_FragmentShader = glCreateShader(GL_FRAGMENT_SHADER); GLuint FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(g_FragmentShader, 1, &FragmentShaderCode, nullptr); glShaderSource(FragmentShader, 1, &FragmentShaderCode, nullptr);
glCompileShader(g_FragmentShader); glCompileShader(FragmentShader);
ShaderProgram.ID = glCreateProgram(); ShaderProgram.ID = glCreateProgram();
glAttachShader(ShaderProgram.ID, g_VertexShader); glAttachShader(ShaderProgram.ID, VertexShader);
glAttachShader(ShaderProgram.ID, g_FragmentShader); glAttachShader(ShaderProgram.ID, FragmentShader);
glLinkProgram(ShaderProgram.ID); glLinkProgram(ShaderProgram.ID);
glDetachShader(ShaderProgram.ID, g_VertexShader); glDetachShader(ShaderProgram.ID, VertexShader);
glDetachShader(ShaderProgram.ID, g_FragmentShader); glDetachShader(ShaderProgram.ID, FragmentShader);
glDeleteShader(g_VertexShader); glDeleteShader(VertexShader);
glDeleteShader(g_FragmentShader); glDeleteShader(FragmentShader);
ShaderProgram.g_AttribLocationHeatmapSampler = glGetUniformLocation(ShaderProgram.ID, "heatmap"); ShaderProgram.AttribLocationProjection = glGetUniformLocation(ShaderProgram.ID, "ProjMtx");
ShaderProgram.g_AttribLocationColormapSampler = glGetUniformLocation(ShaderProgram.ID, "colormap"); ShaderProgram.AttribLocationMinValue = glGetUniformLocation(ShaderProgram.ID, "min_val");
ShaderProgram.g_AttribLocationProjection = glGetUniformLocation(ShaderProgram.ID, "ProjMtx"); ShaderProgram.AttribLocationMaxValue = glGetUniformLocation(ShaderProgram.ID, "max_val");
ShaderProgram.g_AttribLocationMinValue = glGetUniformLocation(ShaderProgram.ID, "min_val");
ShaderProgram.g_AttribLocationMaxValue = glGetUniformLocation(ShaderProgram.ID, "max_val");
glUseProgram(ShaderProgram.ID); glUseProgram(ShaderProgram.ID);
glUniform1i(ShaderProgram.g_AttribLocationHeatmapSampler, 0); // Set texture slot of heatmap texture glUniform1i(glGetUniformLocation(ShaderProgram.ID, "heatmap"), 0); // Set texture slot of heatmap texture
glUniform1i(ShaderProgram.g_AttribLocationColormapSampler, 1); // Set texture slot of colormap texture glUniform1i(glGetUniformLocation(ShaderProgram.ID, "colormap"), 1); // Set texture slot of colormap texture
} }
static void CreateShader(const ImDrawList*, const ImDrawCmd*) static void CreateHeatmapShader(const ImDrawList*, const ImDrawCmd*)
{ {
ContextData& Context = *((ContextData*)GImPlot->backendCtx); ContextData& Context = *((ContextData*)GImPlot->backendCtx);
GLuint CurrentShader; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&Context.ImGuiShader);
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&CurrentShader);
Context.g_AttribLocationImGuiProjection = glGetUniformLocation(CurrentShader, "ProjMtx"); Context.AttribLocationImGuiProjection = glGetUniformLocation(Context.ImGuiShader, "ProjMtx");
GLuint AttribLocationVtxPos = (GLuint)glGetAttribLocation(Context.ImGuiShader, "Position");
GLuint g_AttribLocationVtxPos = (GLuint)glGetAttribLocation(CurrentShader, "Position"); GLuint AttribLocationVtxUV = (GLuint)glGetAttribLocation(Context.ImGuiShader, "UV");
GLuint g_AttribLocationVtxUV = (GLuint)glGetAttribLocation(CurrentShader, "UV");
GLchar* VertexShaderCode = new GLchar[512]; GLchar* VertexShaderCode = new GLchar[512];
GLchar* FragmentShaderCode = new GLchar[512]; GLchar* FragmentShaderCode = new GLchar[512];
snprintf(VertexShaderCode, 512, VERTEX_SHADER_CODE, g_AttribLocationVtxPos, g_AttribLocationVtxUV); snprintf(VertexShaderCode, 512, HEATMAP_VERTEX_SHADER_CODE, AttribLocationVtxPos, AttribLocationVtxUV);
snprintf(FragmentShaderCode, 512, FRAGMENT_SHADER_CODE, ' '); snprintf(FragmentShaderCode, 512, HEATMAP_FRAGMENT_SHADER_CODE, ' ');
CompileShader(Context.ShaderFloat, VertexShaderCode, FragmentShaderCode); CompileShader(Context.ShaderFloat, VertexShaderCode, FragmentShaderCode);
snprintf(VertexShaderCode, 512, VERTEX_SHADER_CODE, g_AttribLocationVtxPos, g_AttribLocationVtxUV); snprintf(VertexShaderCode, 512, HEATMAP_VERTEX_SHADER_CODE, AttribLocationVtxPos, AttribLocationVtxUV);
snprintf(FragmentShaderCode, 512, FRAGMENT_SHADER_CODE, 'i'); snprintf(FragmentShaderCode, 512, HEATMAP_FRAGMENT_SHADER_CODE, 'i');
CompileShader(Context.ShaderInt, VertexShaderCode, FragmentShaderCode); CompileShader(Context.ShaderInt, VertexShaderCode, FragmentShaderCode);
glUseProgram(0); glUseProgram(0);
@ -196,12 +191,9 @@ static void RenderCallback(const ImDrawList*, const ImDrawCmd* cmd)
int plotIdx = Context.PlotIDs.GetInt(plotID, -1); int plotIdx = Context.PlotIDs.GetInt(plotID, -1);
HeatmapData& data = Context.HeatmapDataList[plotIdx]; HeatmapData& data = Context.HeatmapDataList[plotIdx];
GLuint CurrentShader;
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&CurrentShader);
// Get projection matrix of current shader // Get projection matrix of current shader
float OrthoProjection[4][4]; float OrthoProjection[4][4];
glGetUniformfv(CurrentShader, Context.g_AttribLocationImGuiProjection, &OrthoProjection[0][0]); glGetUniformfv(Context.ImGuiShader, Context.AttribLocationImGuiProjection, &OrthoProjection[0][0]);
// Enable our shader // Enable our shader
glUseProgram(data.ShaderProgram->ID); glUseProgram(data.ShaderProgram->ID);
@ -209,15 +201,18 @@ static void RenderCallback(const ImDrawList*, const ImDrawCmd* cmd)
glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, data.HeatmapTexID); // Set texture ID of data glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, data.HeatmapTexID); // Set texture ID of data
glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_1D, data.ColormapTexID); // Set texture ID of colormap glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_1D, data.ColormapTexID); // Set texture ID of colormap
glUniformMatrix4fv(data.ShaderProgram->g_AttribLocationProjection, 1, GL_FALSE, &OrthoProjection[0][0]); glUniformMatrix4fv(data.ShaderProgram->AttribLocationProjection, 1, GL_FALSE, &OrthoProjection[0][0]);
glUniform1f(data.ShaderProgram->g_AttribLocationMinValue, data.MinValue); // Set minimum range glUniform1f(data.ShaderProgram->AttribLocationMinValue, data.MinValue); // Set minimum range
glUniform1f(data.ShaderProgram->g_AttribLocationMaxValue, data.MaxValue); // Set maximum range glUniform1f(data.ShaderProgram->AttribLocationMaxValue, data.MaxValue); // Set maximum range
} }
static void UnbindTexture(const ImDrawList*, const ImDrawCmd*) static void ResetState(const ImDrawList*, const ImDrawCmd*)
{ {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
ContextData& Context = *((ContextData*)GImPlot->backendCtx);
glUseProgram(Context.ImGuiShader);
} }
static void SetTextureData(int plotID, const void* data, GLsizei rows, GLsizei cols, GLint internalFormat, GLenum format, GLenum type) static void SetTextureData(int plotID, const void* data, GLsizei rows, GLsizei cols, GLint internalFormat, GLenum format, GLenum type)
@ -235,9 +230,9 @@ static void SetTextureData(int plotID, const void* data, GLsizei rows, GLsizei c
void AddColormap(const ImU32* keys, int count, bool qual) void AddColormap(const ImU32* keys, int count, bool qual)
{ {
GLuint texID; GLuint textureID;
glGenTextures(1, &texID); glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_1D, texID); 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_RGBA, 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_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
@ -245,8 +240,8 @@ void AddColormap(const ImU32* keys, int count, bool qual)
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, qual ? GL_NEAREST : GL_LINEAR); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, qual ? GL_NEAREST : GL_LINEAR);
glBindTexture(GL_TEXTURE_1D, 0); glBindTexture(GL_TEXTURE_1D, 0);
ContextData& Context = *((ContextData*)GImPlot->backendCtx); // PETA AQUI (GImPlot es NULL) ContextData& Context = *((ContextData*)GImPlot->backendCtx);
Context.ColormapIDs.push_back(texID); Context.ColormapIDs.push_back(textureID);
} }
static GLuint CreateHeatmapTexture() static GLuint CreateHeatmapTexture()
@ -311,36 +306,28 @@ void SetHeatmapData(int plotID, const ImU64* values, int rows, int cols)
void RenderHeatmap(int plotID, ImDrawList& DrawList, const ImVec2& bounds_min, const ImVec2& bounds_max, float scale_min, float scale_max, ImPlotColormap colormap) void RenderHeatmap(int plotID, ImDrawList& DrawList, const ImVec2& bounds_min, const ImVec2& bounds_max, float scale_min, float scale_max, ImPlotColormap colormap)
{ {
ContextData& Context = *((ContextData*)GImPlot->backendCtx); ContextData& Context = *((ContextData*)GImPlot->backendCtx);
int idx = Context.PlotIDs.GetInt(plotID, -1); int idx = Context.PlotIDs.GetInt(plotID, Context.HeatmapDataList.Size);
if(idx < 0) if(idx == Context.HeatmapDataList.Size)
{ {
// New entry // New entry
HeatmapData data;
data.HeatmapTexID = CreateHeatmapTexture();
data.ColormapTexID = Context.ColormapIDs[colormap];
data.MinValue = scale_min;
data.MaxValue = scale_max;
Context.PlotIDs.SetInt(plotID, Context.HeatmapDataList.Size); Context.PlotIDs.SetInt(plotID, Context.HeatmapDataList.Size);
Context.HeatmapDataList.push_back(data); Context.HeatmapDataList.push_back(HeatmapData());
Context.HeatmapDataList[idx].HeatmapTexID = CreateHeatmapTexture();
} }
else
{
HeatmapData& data = Context.HeatmapDataList[idx]; HeatmapData& data = Context.HeatmapDataList[idx];
data.ColormapTexID = Context.ColormapIDs[colormap]; data.ColormapTexID = Context.ColormapIDs[colormap];
data.MinValue = scale_min; data.MinValue = scale_min;
data.MaxValue = scale_max; data.MaxValue = scale_max;
}
if(Context.ShaderInt.ID == 0) if(Context.ShaderInt.ID == 0 || Context.ShaderFloat.ID == 0)
DrawList.AddCallback(CreateShader, nullptr); DrawList.AddCallback(CreateHeatmapShader, nullptr);
DrawList.AddCallback(RenderCallback, (void*)(intptr_t)plotID); DrawList.AddCallback(RenderCallback, (void*)(intptr_t)plotID);
DrawList.PrimReserve(6, 4); 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, 1.0f), ImVec2(1.0f, 0.0f), 0);
DrawList.AddCallback(UnbindTexture, nullptr); DrawList.AddCallback(ResetState, nullptr);
DrawList.AddCallback(ImDrawCallback_ResetRenderState, nullptr);
} }
void BustPlotCache() void BustPlotCache()