From ac1fcefdb10917e2887de617d810cdb8312ca5bf Mon Sep 17 00:00:00 2001 From: Sergiusz Bazanski Date: Fri, 22 Jun 2018 20:16:49 +0100 Subject: OpenGL 3.1, VAO/VBO --- gui/designwidget.cc | 8 ++--- gui/fpgaviewwidget.cc | 84 ++++++++++++++++++++++++++++++++++++++++++--------- gui/fpgaviewwidget.h | 29 ++++++++++++++++-- 3 files changed, 100 insertions(+), 21 deletions(-) (limited to 'gui') diff --git a/gui/designwidget.cc b/gui/designwidget.cc index 0f87f06c..95b76d81 100644 --- a/gui/designwidget.cc +++ b/gui/designwidget.cc @@ -116,8 +116,8 @@ DesignWidget::DesignWidget(Context *_ctx, QWidget *parent) QList bel_items; for (auto bel : ctx->getBels()) { auto name = ctx->getBelName(bel); - bel_items.append( - new BelTreeItem(name, ElementType::BEL, QString(name.c_str(ctx)))); + bel_items.append(new BelTreeItem(name, ElementType::BEL, + QString(name.c_str(ctx)))); } bel_root->addChildren(bel_items); @@ -140,8 +140,8 @@ DesignWidget::DesignWidget(Context *_ctx, QWidget *parent) treeWidget->insertTopLevelItem(0, pip_root); for (auto pip : ctx->getPips()) { auto name = ctx->getPipName(pip); - pip_items.append( - new PipTreeItem(name, ElementType::PIP, QString(name.c_str(ctx)))); + pip_items.append(new PipTreeItem(name, ElementType::PIP, + QString(name.c_str(ctx)))); } pip_root->addChildren(pip_items); diff --git a/gui/fpgaviewwidget.cc b/gui/fpgaviewwidget.cc index f9cfc900..71e74e19 100644 --- a/gui/fpgaviewwidget.cc +++ b/gui/fpgaviewwidget.cc @@ -173,6 +173,20 @@ bool LineShader::compile(void) program_->log().toStdString().c_str()); return false; } + + if (!vao_.create()) + log_abort(); + vao_.bind(); + + if (!buffers_.position.create()) + log_abort(); + if (!buffers_.normal.create()) + log_abort(); + if (!buffers_.miter.create()) + log_abort(); + if (!buffers_.index.create()) + log_abort(); + attributes_.position = program_->attributeLocation("position"); attributes_.normal = program_->attributeLocation("normal"); attributes_.miter = program_->attributeLocation("miter"); @@ -180,44 +194,84 @@ bool LineShader::compile(void) uniforms_.projection = program_->uniformLocation("projection"); uniforms_.color = program_->uniformLocation("color"); + vao_.release(); return true; } void LineShader::draw(const LineShaderData &line, const QMatrix4x4 &projection) { auto gl = QOpenGLContext::currentContext()->functions(); + vao_.bind(); program_->bind(); + buffers_.position.bind(); + buffers_.position.allocate(&line.vertices[0], + sizeof(Vertex2DPOD) * line.vertices.size()); + + buffers_.normal.bind(); + buffers_.normal.allocate(&line.normals[0], + sizeof(Vertex2DPOD) * line.normals.size()); + + buffers_.miter.bind(); + buffers_.miter.allocate(&line.miters[0], + sizeof(GLfloat) * line.miters.size()); + + buffers_.index.bind(); + buffers_.index.allocate(&line.indices[0], + sizeof(GLuint) * line.indices.size()); + program_->setUniformValue(uniforms_.projection, projection); program_->setUniformValue(uniforms_.thickness, line.thickness); program_->setUniformValue(uniforms_.color, line.color.r, line.color.g, line.color.b, line.color.a); + buffers_.position.bind(); + program_->enableAttributeArray("position"); gl->glVertexAttribPointer(attributes_.position, 2, GL_FLOAT, GL_FALSE, 0, - &line.vertices[0]); + (void *)0); + + buffers_.normal.bind(); + program_->enableAttributeArray("normal"); gl->glVertexAttribPointer(attributes_.normal, 2, GL_FLOAT, GL_FALSE, 0, - &line.normals[0]); - gl->glVertexAttribPointer(attributes_.miter, 1, GL_FLOAT, GL_FALSE, 0, - &line.miters[0]); + (void *)0); - gl->glEnableVertexAttribArray(0); - gl->glEnableVertexAttribArray(1); - gl->glEnableVertexAttribArray(2); + buffers_.miter.bind(); + program_->enableAttributeArray("miter"); + gl->glVertexAttribPointer(attributes_.miter, 1, GL_FLOAT, GL_FALSE, 0, + (void *)0); + buffers_.index.bind(); gl->glDrawElements(GL_TRIANGLES, line.indices.size(), GL_UNSIGNED_INT, - &line.indices[0]); + (void *)0); + + program_->disableAttributeArray("miter"); + program_->disableAttributeArray("normal"); + program_->disableAttributeArray("position"); - gl->glDisableVertexAttribArray(2); - gl->glDisableVertexAttribArray(1); - gl->glDisableVertexAttribArray(0); program_->release(); + vao_.release(); } FPGAViewWidget::FPGAViewWidget(QWidget *parent) : QOpenGLWidget(parent), moveX_(0), moveY_(0), zoom_(10.0f), lineShader_(this) { - ctx = qobject_cast(getMainWindow())->getContext(); + ctx_ = qobject_cast(getMainWindow())->getContext(); + auto fmt = format(); + fmt.setMajorVersion(3); + fmt.setMinorVersion(1); + setFormat(fmt); + + fmt = format(); + printf("FPGAViewWidget running on OpenGL %d.%d\n", fmt.majorVersion(), + fmt.minorVersion()); + if (fmt.majorVersion() < 3) { + printf("Could not get OpenGL 3.0 context. Aborting.\n"); + log_abort(); + } + if (fmt.minorVersion() < 1) { + printf("Could not get OpenGL 3.1 context - trying anyway...\n "); + } } QMainWindow *FPGAViewWidget::getMainWindow() @@ -320,15 +374,15 @@ void FPGAViewWidget::paintGL() // Draw Bels. auto bels = LineShaderData(0.02f, QColor("#b000ba")); - for (auto bel : ctx->getBels()) { - for (auto &el : ctx->getBelGraphics(bel)) + for (auto bel : ctx_->getBels()) { + for (auto &el : ctx_->getBelGraphics(bel)) drawElement(bels, el); } lineShader_.draw(bels, matrix); // Draw Frame Graphics. auto frames = LineShaderData(0.02f, QColor("#0066ba")); - for (auto &el : ctx->getFrameGraphics()) { + for (auto &el : ctx_->getFrameGraphics()) { drawElement(frames, el); } lineShader_.draw(frames, matrix); diff --git a/gui/fpgaviewwidget.h b/gui/fpgaviewwidget.h index 0cfcbb3e..9f9dc024 100644 --- a/gui/fpgaviewwidget.h +++ b/gui/fpgaviewwidget.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -162,6 +163,15 @@ class LineShader GLuint miter; } attributes_; + // GL buffers + struct + { + QOpenGLBuffer position; + QOpenGLBuffer normal; + QOpenGLBuffer miter; + QOpenGLBuffer index; + } buffers_; + // GL uniform locations. struct { @@ -173,8 +183,23 @@ class LineShader GLuint color; } uniforms_; + QOpenGLVertexArrayObject vao_; + public: - LineShader(QObject *parent) : parent_(parent), program_(nullptr) {} + LineShader(QObject *parent) : parent_(parent), program_(nullptr) + { + buffers_.position = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer); + buffers_.position.setUsagePattern(QOpenGLBuffer::StaticDraw); + + buffers_.normal = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer); + buffers_.normal.setUsagePattern(QOpenGLBuffer::StaticDraw); + + buffers_.miter = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer); + buffers_.miter.setUsagePattern(QOpenGLBuffer::StaticDraw); + + buffers_.index = QOpenGLBuffer(QOpenGLBuffer::IndexBuffer); + buffers_.index.setUsagePattern(QOpenGLBuffer::StaticDraw); + } static constexpr const char *vertexShaderSource_ = "attribute highp vec2 position;\n" @@ -238,7 +263,7 @@ class FPGAViewWidget : public QOpenGLWidget, protected QOpenGLFunctions float startDragX_; float startDragY_; - Context *ctx; + Context *ctx_; }; NEXTPNR_NAMESPACE_END -- cgit v1.2.3 From 82ec1be31f2a13bca04cf1a6b69789c9cee0e535 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Sat, 23 Jun 2018 14:32:18 +0200 Subject: Added BUILD_GUI, BUILD_PYTHON and BUILD_TESTS cmake options, --- gui/basewindow.cc | 5 +++++ gui/designwidget.cc | 1 - gui/pythontab.cc | 3 +++ gui/pythontab.h | 3 +++ 4 files changed, 11 insertions(+), 1 deletion(-) (limited to 'gui') diff --git a/gui/basewindow.cc b/gui/basewindow.cc index ac28e95f..9d99152c 100644 --- a/gui/basewindow.cc +++ b/gui/basewindow.cc @@ -27,7 +27,10 @@ #include "jsonparse.h" #include "log.h" #include "mainwindow.h" + +#ifndef NO_PYTHON #include "pythontab.h" +#endif static void initBasenameResource() { Q_INIT_RESOURCE(base); } @@ -70,7 +73,9 @@ BaseMainWindow::BaseMainWindow(Context *_ctx, QWidget *parent) SLOT(writeInfo(std::string))); tabWidget = new QTabWidget(); +#ifndef NO_PYTHON tabWidget->addTab(new PythonTab(), "Python"); +#endif info = new InfoTab(); tabWidget->addTab(info, "Info"); diff --git a/gui/designwidget.cc b/gui/designwidget.cc index 95b76d81..6752b780 100644 --- a/gui/designwidget.cc +++ b/gui/designwidget.cc @@ -24,7 +24,6 @@ #include #include #include "fpgaviewwidget.h" -#include "pybindings.h" NEXTPNR_NAMESPACE_BEGIN diff --git a/gui/pythontab.cc b/gui/pythontab.cc index a11059b5..8e8b7be4 100644 --- a/gui/pythontab.cc +++ b/gui/pythontab.cc @@ -16,6 +16,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * */ + #ifndef NO_PYTHON #include "pythontab.h" #include @@ -138,3 +139,5 @@ void PythonTab::showContextMenu(const QPoint &pt) void PythonTab::clearBuffer() { plainTextEdit->clear(); } NEXTPNR_NAMESPACE_END + +#endif \ No newline at end of file diff --git a/gui/pythontab.h b/gui/pythontab.h index f37381d7..40de0ebe 100644 --- a/gui/pythontab.h +++ b/gui/pythontab.h @@ -20,6 +20,8 @@ #ifndef PYTHONTAB_H #define PYTHONTAB_H +#ifndef NO_PYTHON + #include #include #include @@ -52,5 +54,6 @@ class PythonTab : public QWidget }; NEXTPNR_NAMESPACE_END +#endif // NO_PYTHON #endif // PYTHONTAB_H -- cgit v1.2.3