summaryrefslogtreecommitdiff
path: root/retrace/daemon/bargraph
diff options
context:
space:
mode:
authorMark Janes <mark.a.janes@intel.com>2015-12-01 09:45:34 -0800
committerMark Janes <mark.a.janes@intel.com>2017-06-19 14:04:46 -0700
commitf9bcbd239674f835c6ea02e0f847e02bdf0926a6 (patch)
tree9627cdda93f2e80f6e289c4ae4a65c0075adddd3 /retrace/daemon/bargraph
parentf2c41514f05528411eb2ad5462ae75bdfa3d5c0c (diff)
change bargraph to use Qt GL dispatch
On windows, System OpenGL headers clash with Qt OpenGL headers. Any class which uses Qt GL (bargraph renderer) can't also use standard GL calls. This commit refactors the bargraph (which was pure GL) to use Qt's preferred dispatch mechanism.
Diffstat (limited to 'retrace/daemon/bargraph')
-rw-r--r--retrace/daemon/bargraph/glframe_bargraph.cpp123
-rw-r--r--retrace/daemon/bargraph/glframe_bargraph.hpp9
-rw-r--r--retrace/daemon/bargraph/test/main_test.cpp8
-rw-r--r--retrace/daemon/bargraph/test/test_bargraph.cpp2
4 files changed, 91 insertions, 51 deletions
diff --git a/retrace/daemon/bargraph/glframe_bargraph.cpp b/retrace/daemon/bargraph/glframe_bargraph.cpp
index c6d7c713..014d353c 100644
--- a/retrace/daemon/bargraph/glframe_bargraph.cpp
+++ b/retrace/daemon/bargraph/glframe_bargraph.cpp
@@ -29,18 +29,46 @@
#include <assert.h>
#include <stdint.h>
-#include <GL/gl.h>
-
#include <algorithm>
#include <vector>
#include <set>
-
-#include "glframe_glhelper.hpp"
+#include <string>
using glretrace::BarGraphRenderer;
using glretrace::BarGraphSubscriber;
using glretrace::BarMetrics;
+#define GL_CHECK() CheckError(__FILE__, __LINE__)
+
+void
+BarGraphRenderer::GetCompileError(GLint shader, std::string *message) {
+ GLint status;
+ glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
+ if (status == GL_TRUE)
+ return;
+ static const int MAXLEN = 1024;
+ std::vector<char> log(MAXLEN);
+ GLsizei len;
+ glGetShaderInfoLog(shader, MAXLEN, &len, log.data());
+ *message = log.data();
+}
+
+void
+BarGraphRenderer::CheckError(const char * file, int line) {
+ const int error = glGetError();
+ if ( error == GL_NO_ERROR)
+ return;
+ printf("ERROR: %x %s:%i\n", error, file, line);
+}
+
+void
+BarGraphRenderer::PrintCompileError(GLint shader) {
+ std::string message;
+ GetCompileError(shader, &message);
+ if (message.size())
+ printf("ERROR -- compile failed: %s\n", message.c_str());
+}
+
const char *
BarGraphRenderer::vshader =
"attribute vec2 coord; \n"
@@ -68,48 +96,49 @@ BarGraphRenderer::BarGraphRenderer(bool invert)
mouse_area(2),
invert_y(invert ? -1 : 1),
subscriber(NULL) {
+ initializeOpenGLFunctions();
// generate vbo
- GL::GenBuffers(1, &vbo);
+ glGenBuffers(1, &vbo);
GL_CHECK();
- GL::BindBuffer(GL_ARRAY_BUFFER, vbo);
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
GL_CHECK();
- GL::BindBuffer(GL_ARRAY_BUFFER, 0);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
GL_CHECK();
- const int vs = GL::CreateShader(GL_VERTEX_SHADER);
+ const int vs = glCreateShader(GL_VERTEX_SHADER);
GL_CHECK();
int len = strlen(vshader);
- GL::ShaderSource(vs, 1, &vshader, &len);
+ glShaderSource(vs, 1, &vshader, &len);
GL_CHECK();
- GL::CompileShader(vs);
+ glCompileShader(vs);
PrintCompileError(vs);
- const int fs = GL::CreateShader(GL_FRAGMENT_SHADER);
+ const int fs = glCreateShader(GL_FRAGMENT_SHADER);
GL_CHECK();
len = strlen(fshader);
- GL::ShaderSource(fs, 1, &fshader, &len);
+ glShaderSource(fs, 1, &fshader, &len);
GL_CHECK();
- GL::CompileShader(fs);
+ glCompileShader(fs);
PrintCompileError(fs);
- prog = GL::CreateProgram();
- GL::AttachShader(prog, vs);
+ prog = glCreateProgram();
+ glAttachShader(prog, vs);
GL_CHECK();
- GL::AttachShader(prog, fs);
+ glAttachShader(prog, fs);
GL_CHECK();
- GL::LinkProgram(prog);
+ glLinkProgram(prog);
GL_CHECK();
// get attribute locations
- att_coord = GL::GetAttribLocation(prog, "coord");
+ att_coord = glGetAttribLocation(prog, "coord");
GL_CHECK();
- uni_max_x = GL::GetUniformLocation(prog, "max_x");
+ uni_max_x = glGetUniformLocation(prog, "max_x");
GL_CHECK();
- uni_max_y = GL::GetUniformLocation(prog, "max_y");
+ uni_max_y = glGetUniformLocation(prog, "max_y");
GL_CHECK();
- uni_invert_y = GL::GetUniformLocation(prog, "invert_y");
+ uni_invert_y = glGetUniformLocation(prog, "invert_y");
GL_CHECK();
- uni_bar_color = GL::GetUniformLocation(prog, "bar_color");
+ uni_bar_color = glGetUniformLocation(prog, "bar_color");
GL_CHECK();
}
@@ -219,37 +248,37 @@ template class std::vector<BarGraphRenderer::Vertex>;
void
BarGraphRenderer::render() {
- GL::Enable(GL_BLEND);
+ glEnable(GL_BLEND);
GL_CHECK();
- GL::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GL_CHECK();
- GL::ClearColor(1.0, 1.0, 1.0, 1.0);
+ glClearColor(1.0, 1.0, 1.0, 1.0);
GL_CHECK();
- GL::Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GL_CHECK();
- GL::UseProgram(prog);
+ glUseProgram(prog);
GL_CHECK();
// set uniforms
- GL::Uniform1f(uni_max_y, max_y);
+ glUniform1f(uni_max_y, max_y);
GL_CHECK();
- GL::Uniform1f(uni_max_x, total_x);
+ glUniform1f(uni_max_x, total_x);
GL_CHECK();
- GL::Uniform1f(uni_invert_y, invert_y);
+ glUniform1f(uni_invert_y, invert_y);
GL_CHECK();
// bind vbo
- GL::BindBuffer(GL_ARRAY_BUFFER, vbo);
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
GL_CHECK();
// enable vbo
- GL::EnableVertexAttribArray(att_coord);
+ glEnableVertexAttribArray(att_coord);
GL_CHECK();
// describe vbo
- GL::VertexAttribPointer(att_coord, // attribute
+ glVertexAttribPointer(att_coord, // attribute
2, // number of elements
// per vertex, here
// (x,y)
@@ -269,20 +298,20 @@ BarGraphRenderer::render() {
grid_lines[i+1].y = max_y * (0.2 + i * 0.1);
}
const float grey_color[4] = { 0.2, 0.2, 0.2, 0.6 };
- GL::Uniform4f(uni_bar_color, grey_color[0], grey_color[1],
+ glUniform4f(uni_bar_color, grey_color[0], grey_color[1],
grey_color[2], grey_color[3]);
GL_CHECK();
- GL::BufferData(GL_ARRAY_BUFFER, grid_lines.size() * sizeof(Vertex),
+ glBufferData(GL_ARRAY_BUFFER, grid_lines.size() * sizeof(Vertex),
grid_lines.data(), GL_STATIC_DRAW);
GL_CHECK();
for (int i = 0; i < 4; ++i) {
- GL::DrawArrays(GL_LINE_STRIP, i*2, 2);
+ glDrawArrays(GL_LINE_STRIP, i*2, 2);
GL_CHECK();
}
- // GL::Disable(GL_BLEND);
+ // glDisable(GL_BLEND);
// buffer vertex data to vbo
- GL::BufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex),
+ glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex),
vertices.data(), GL_STATIC_DRAW);
GL_CHECK();
@@ -294,17 +323,17 @@ BarGraphRenderer::render() {
const float bar_color[4] = { 0.0, 0.0, 1.0, 1.0 };
const float selected_color[4] = { 1.0, 1.0, 0.0, 1.0 };
const float* color = selected[i/4] ? selected_color : bar_color;
- GL::Uniform4f(uni_bar_color, color[0], color[1], color[2], color[3]);
+ glUniform4f(uni_bar_color, color[0], color[1], color[2], color[3]);
GL_CHECK();
- GL::DrawArrays(GL_TRIANGLE_STRIP, i, 4);
+ glDrawArrays(GL_TRIANGLE_STRIP, i, 4);
GL_CHECK();
// draw a thin border around each bar
const float black_color[4] = { 0.0, 0.0, 0.0, 1.0 };
- GL::Uniform4f(uni_bar_color, black_color[0], black_color[1],
+ glUniform4f(uni_bar_color, black_color[0], black_color[1],
black_color[2], black_color[3]);
GL_CHECK();
- GL::DrawElements(GL_LINE_STRIP, 5, GL_UNSIGNED_SHORT,
+ glDrawElements(GL_LINE_STRIP, 5, GL_UNSIGNED_SHORT,
outline_indices.data());
GL_CHECK();
@@ -321,24 +350,24 @@ BarGraphRenderer::render() {
// mouse selection is active, draw a selection rectangle
// buffer data to vbo
- GL::BufferData(GL_ARRAY_BUFFER, mouse_vertices.size() * sizeof(Vertex),
+ glBufferData(GL_ARRAY_BUFFER, mouse_vertices.size() * sizeof(Vertex),
mouse_vertices.data(), GL_STATIC_DRAW);
GL_CHECK();
// yellow selection box
const float color[4] = { 0.5, 0.5, 0.5, 0.5 };
- GL::Uniform4f(uni_bar_color, color[0], color[1], color[2], color[3]);
+ glUniform4f(uni_bar_color, color[0], color[1], color[2], color[3]);
GL_CHECK();
- GL::DrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+ glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
GL_CHECK();
}
// disable vbo
- GL::DisableVertexAttribArray(att_coord);
+ glDisableVertexAttribArray(att_coord);
GL_CHECK();
// unbind vbo
- GL::BindBuffer(GL_ARRAY_BUFFER, 0);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
GL_CHECK();
}
diff --git a/retrace/daemon/bargraph/glframe_bargraph.hpp b/retrace/daemon/bargraph/glframe_bargraph.hpp
index 5998d468..6ca880f7 100644
--- a/retrace/daemon/bargraph/glframe_bargraph.hpp
+++ b/retrace/daemon/bargraph/glframe_bargraph.hpp
@@ -28,9 +28,10 @@
#ifndef _GLFRAME_BARGRAPH_HPP_
#define _GLFRAME_BARGRAPH_HPP_
-#include <GLES2/gl2.h>
+#include <QOpenGLFunctions>
#include <set>
+#include <string>
#include <vector>
namespace glretrace {
@@ -57,7 +58,7 @@ class BarGraphSubscriber {
// - mouse area
// - bars
// - Independent of Qt
-class BarGraphRenderer {
+class BarGraphRenderer : protected QOpenGLFunctions {
public:
explicit BarGraphRenderer(bool invert = false); // Qt draws top-to-bottom
void setBars(const std::vector<BarMetrics> &bars);
@@ -82,6 +83,10 @@ class BarGraphRenderer {
Vertex top_right;
};
+ void CheckError(const char * file, int line);
+ void GetCompileError(GLint shader, std::string *message);
+ void PrintCompileError(GLint shader);
+
std::vector<bool> selected;
std::vector<Vertex> vertices;
std::vector<Vertex> mouse_vertices;
diff --git a/retrace/daemon/bargraph/test/main_test.cpp b/retrace/daemon/bargraph/test/main_test.cpp
index a6f50b31..7da945d9 100644
--- a/retrace/daemon/bargraph/test/main_test.cpp
+++ b/retrace/daemon/bargraph/test/main_test.cpp
@@ -27,8 +27,16 @@
#include <gtest/gtest.h>
#include <google/protobuf/stubs/common.h>
+#include <QApplication>
+#include <QGLWidget>
int main(int argc, char **argv) {
+ // boilerplate for ensuring a GL context exists. Required for
+ // bargraph.
+ QApplication app(argc, argv);
+ QGLWidget tmpwidget;
+ tmpwidget.makeCurrent();
+
::testing::InitGoogleTest(&argc, argv);
const int r = RUN_ALL_TESTS();
return r;
diff --git a/retrace/daemon/bargraph/test/test_bargraph.cpp b/retrace/daemon/bargraph/test/test_bargraph.cpp
index cbe0b821..cc6ea758 100644
--- a/retrace/daemon/bargraph/test/test_bargraph.cpp
+++ b/retrace/daemon/bargraph/test/test_bargraph.cpp
@@ -49,8 +49,6 @@ using glretrace::SelectionObserver;
using glretrace::TestContext;
TEST(BarGraph, Create) {
- GlFunctions::Init();
- TestContext c;
BarGraphRenderer r;
}