Merge branch 'q3k/gl-vbo' into 'master'

OpenGL 3.1, VAO/VBO

See merge request SymbioticEDA/nextpnr!2
This commit is contained in:
Miodrag Milanović 2018-06-23 12:10:52 +00:00
commit b63fdfbeab
3 changed files with 100 additions and 21 deletions

View File

@ -116,8 +116,8 @@ DesignWidget::DesignWidget(Context *_ctx, QWidget *parent)
QList<QTreeWidgetItem *> bel_items; QList<QTreeWidgetItem *> bel_items;
for (auto bel : ctx->getBels()) { for (auto bel : ctx->getBels()) {
auto name = ctx->getBelName(bel); auto name = ctx->getBelName(bel);
bel_items.append( bel_items.append(new BelTreeItem(name, ElementType::BEL,
new BelTreeItem(name, ElementType::BEL, QString(name.c_str(ctx)))); QString(name.c_str(ctx))));
} }
bel_root->addChildren(bel_items); bel_root->addChildren(bel_items);
@ -140,8 +140,8 @@ DesignWidget::DesignWidget(Context *_ctx, QWidget *parent)
treeWidget->insertTopLevelItem(0, pip_root); treeWidget->insertTopLevelItem(0, pip_root);
for (auto pip : ctx->getPips()) { for (auto pip : ctx->getPips()) {
auto name = ctx->getPipName(pip); auto name = ctx->getPipName(pip);
pip_items.append( pip_items.append(new PipTreeItem(name, ElementType::PIP,
new PipTreeItem(name, ElementType::PIP, QString(name.c_str(ctx)))); QString(name.c_str(ctx))));
} }
pip_root->addChildren(pip_items); pip_root->addChildren(pip_items);

View File

@ -173,6 +173,20 @@ bool LineShader::compile(void)
program_->log().toStdString().c_str()); program_->log().toStdString().c_str());
return false; 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_.position = program_->attributeLocation("position");
attributes_.normal = program_->attributeLocation("normal"); attributes_.normal = program_->attributeLocation("normal");
attributes_.miter = program_->attributeLocation("miter"); attributes_.miter = program_->attributeLocation("miter");
@ -180,44 +194,84 @@ bool LineShader::compile(void)
uniforms_.projection = program_->uniformLocation("projection"); uniforms_.projection = program_->uniformLocation("projection");
uniforms_.color = program_->uniformLocation("color"); uniforms_.color = program_->uniformLocation("color");
vao_.release();
return true; return true;
} }
void LineShader::draw(const LineShaderData &line, const QMatrix4x4 &projection) void LineShader::draw(const LineShaderData &line, const QMatrix4x4 &projection)
{ {
auto gl = QOpenGLContext::currentContext()->functions(); auto gl = QOpenGLContext::currentContext()->functions();
vao_.bind();
program_->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_.projection, projection);
program_->setUniformValue(uniforms_.thickness, line.thickness); program_->setUniformValue(uniforms_.thickness, line.thickness);
program_->setUniformValue(uniforms_.color, line.color.r, line.color.g, program_->setUniformValue(uniforms_.color, line.color.r, line.color.g,
line.color.b, line.color.a); line.color.b, line.color.a);
buffers_.position.bind();
program_->enableAttributeArray("position");
gl->glVertexAttribPointer(attributes_.position, 2, GL_FLOAT, GL_FALSE, 0, 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, gl->glVertexAttribPointer(attributes_.normal, 2, GL_FLOAT, GL_FALSE, 0,
&line.normals[0]); (void *)0);
buffers_.miter.bind();
program_->enableAttributeArray("miter");
gl->glVertexAttribPointer(attributes_.miter, 1, GL_FLOAT, GL_FALSE, 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_.index.bind();
gl->glDrawElements(GL_TRIANGLES, line.indices.size(), GL_UNSIGNED_INT, 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(); program_->release();
vao_.release();
} }
FPGAViewWidget::FPGAViewWidget(QWidget *parent) FPGAViewWidget::FPGAViewWidget(QWidget *parent)
: QOpenGLWidget(parent), moveX_(0), moveY_(0), zoom_(10.0f), : QOpenGLWidget(parent), moveX_(0), moveY_(0), zoom_(10.0f),
lineShader_(this) lineShader_(this)
{ {
ctx = qobject_cast<BaseMainWindow *>(getMainWindow())->getContext(); ctx_ = qobject_cast<BaseMainWindow *>(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() QMainWindow *FPGAViewWidget::getMainWindow()
@ -320,15 +374,15 @@ void FPGAViewWidget::paintGL()
// Draw Bels. // Draw Bels.
auto bels = LineShaderData(0.02f, QColor("#b000ba")); auto bels = LineShaderData(0.02f, QColor("#b000ba"));
for (auto bel : ctx->getBels()) { for (auto bel : ctx_->getBels()) {
for (auto &el : ctx->getBelGraphics(bel)) for (auto &el : ctx_->getBelGraphics(bel))
drawElement(bels, el); drawElement(bels, el);
} }
lineShader_.draw(bels, matrix); lineShader_.draw(bels, matrix);
// Draw Frame Graphics. // Draw Frame Graphics.
auto frames = LineShaderData(0.02f, QColor("#0066ba")); auto frames = LineShaderData(0.02f, QColor("#0066ba"));
for (auto &el : ctx->getFrameGraphics()) { for (auto &el : ctx_->getFrameGraphics()) {
drawElement(frames, el); drawElement(frames, el);
} }
lineShader_.draw(frames, matrix); lineShader_.draw(frames, matrix);

View File

@ -24,6 +24,7 @@
#include <QOpenGLBuffer> #include <QOpenGLBuffer>
#include <QOpenGLFunctions> #include <QOpenGLFunctions>
#include <QOpenGLShaderProgram> #include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLWidget> #include <QOpenGLWidget>
#include <QPainter> #include <QPainter>
@ -162,6 +163,15 @@ class LineShader
GLuint miter; GLuint miter;
} attributes_; } attributes_;
// GL buffers
struct
{
QOpenGLBuffer position;
QOpenGLBuffer normal;
QOpenGLBuffer miter;
QOpenGLBuffer index;
} buffers_;
// GL uniform locations. // GL uniform locations.
struct struct
{ {
@ -173,8 +183,23 @@ class LineShader
GLuint color; GLuint color;
} uniforms_; } uniforms_;
QOpenGLVertexArrayObject vao_;
public: 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_ = static constexpr const char *vertexShaderSource_ =
"attribute highp vec2 position;\n" "attribute highp vec2 position;\n"
@ -238,7 +263,7 @@ class FPGAViewWidget : public QOpenGLWidget, protected QOpenGLFunctions
float startDragX_; float startDragX_;
float startDragY_; float startDragY_;
Context *ctx; Context *ctx_;
}; };
NEXTPNR_NAMESPACE_END NEXTPNR_NAMESPACE_END