changed gl calls to gpu::

This commit is contained in:
Seiji Emery 2015-08-03 11:58:38 -07:00
parent fe74ec3f89
commit 883aa7af8d

View file

@ -21,20 +21,27 @@
#include <QLoggingCategory> #include <QLoggingCategory>
#include <gpu/Context.h> #include <gpu/Context.h>
#include <gpu/Batch.h>
#include <gpu/Stream.h>
#include <gpu/StandardShaderLib.h>
//#include <shared/ViewFrustrum.h>
#include <gpu/GLBackend.h> #include <gpu/GLBackend.h>
#include <QOpenGLBuffer> //#include <QOpenGLBuffer>
#include <QOpenGLContext> #include <QOpenGLContext>
#include <QOpenGLDebugLogger> //#include <QOpenGLDebugLogger>
#include <QOpenGLShaderProgram> //#include <QOpenGLShaderProgram>
#include <QOpenGLTexture> //#include <QOpenGLTexture>
#include <QOpenGLVertexArrayObject> //#include <QOpenGLVertexArrayObject>
#include <QResizeEvent> #include <QResizeEvent>
#include <QTime> #include <QTime>
#include <QTimer> #include <QTimer>
#include <QWindow> #include <QWindow>
#include <cstdio>
#include <PathUtils.h> #include <PathUtils.h>
@ -74,6 +81,15 @@ public:
} }
}; };
const char * basicVS =
" varying vec3 pos; "
" void main(void) { "
" gl_Position.xyz = pos; "
" } ";
const char * basicFS =
" void main(void) { "
" gl_FragColor.xyz = vec3(0.7, 0.2, 0.5); "
" } ";
const QString& getQmlDir() { const QString& getQmlDir() {
static QString dir; static QString dir;
@ -90,8 +106,14 @@ const QString& getQmlDir() {
class QTestWindow : public QWindow { class QTestWindow : public QWindow {
Q_OBJECT Q_OBJECT
QOpenGLContext* _context{ nullptr }; QOpenGLContext* _qGlContext{ nullptr };
QSize _size; QSize _size;
gpu::ContextPointer _context;
gpu::PipelinePointer _pipeline;
gpu::BufferPointer _buffer;
gpu::Stream::FormatPointer _format;
//TextRenderer* _textRenderer[4]; //TextRenderer* _textRenderer[4];
RateCounter fps; RateCounter fps;
@ -117,27 +139,62 @@ public:
setFormat(format); setFormat(format);
_context = new QOpenGLContext; _qGlContext = new QOpenGLContext;
_context->setFormat(format); _qGlContext->setFormat(format);
_context->create(); _qGlContext->create();
show(); show();
makeCurrent(); makeCurrent();
gpu::Context::init<gpu::GLBackend>(); gpu::Context::init<gpu::GLBackend>();
_context = std::make_shared<gpu::Context>();
// Clear screen
{ gpu::Batch batch;
QOpenGLDebugLogger* logger = new QOpenGLDebugLogger(this); batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLORS, { 1.0, 0.0, 0.5, 1.0 });
logger->initialize(); // initializes in the current context, i.e. ctx _context->render(batch);
logger->enableMessages();
connect(logger, &QOpenGLDebugLogger::messageLogged, this, [&](const QOpenGLDebugMessage & debugMessage) { // Create default shaders
qDebug() << debugMessage;
}); std::string vsSource (basicVS);
// logger->startLogging(QOpenGLDebugLogger::SynchronousLogging); std::string fsSource (basicFS);
auto vs = gpu::ShaderPointer(gpu::Shader::createVertex(vsSource));
auto fs = gpu::ShaderPointer(gpu::Shader::createPixel(fsSource));
auto shader = gpu::ShaderPointer(gpu::Shader::createProgram(vs, fs));
gpu::Shader::BindingSet bindings;
if (!gpu::Shader::makeProgram(*shader, bindings)) {
printf("Could not compile shader");
exit(-1);
} }
qDebug() << (const char*)glGetString(GL_VERSION);
// auto shader = gpu::StandardShaderLib::getDrawViewportQuadTransformTexcoordVS();
auto state = std::make_shared<gpu::State>();
_pipeline = gpu::PipelinePointer(gpu::Pipeline::create(shader, state));
float z = 10.0f;
const glm::vec3 vertices[] = {
{ -1.0f, 1.0f, z },
{ 1.0f, -1.0f, z },
{ -1.0f, -1.0f, z }
};
_buffer = std::make_shared<gpu::Buffer>(sizeof(vertices), (const gpu::Byte*)vertices);
_format = std::make_shared<gpu::Stream::Format>();
_format->setAttribute(gpu::Stream::POSITION, gpu::Stream::POSITION, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ));
// {
// QOpenGLDebugLogger* logger = new QOpenGLDebugLogger(this);
// logger->initialize(); // initializes in the current context, i.e. ctx
// logger->enableMessages();
// connect(logger, &QOpenGLDebugLogger::messageLogged, this, [&](const QOpenGLDebugMessage & debugMessage) {
// qDebug() << debugMessage;
// });
// // logger->startLogging(QOpenGLDebugLogger::SynchronousLogging);
// }
// qDebug() << (const char*)glGetString(GL_VERSION);
//_textRenderer[0] = TextRenderer::getInstance(SANS_FONT_FAMILY, 12, false); //_textRenderer[0] = TextRenderer::getInstance(SANS_FONT_FAMILY, 12, false);
//_textRenderer[1] = TextRenderer::getInstance(SERIF_FONT_FAMILY, 12, false, //_textRenderer[1] = TextRenderer::getInstance(SERIF_FONT_FAMILY, 12, false,
@ -146,12 +203,14 @@ public:
// false, TextRenderer::OUTLINE_EFFECT); // false, TextRenderer::OUTLINE_EFFECT);
//_textRenderer[3] = TextRenderer::getInstance(INCONSOLATA_FONT_FAMILY, 24); //_textRenderer[3] = TextRenderer::getInstance(INCONSOLATA_FONT_FAMILY, 24);
glEnable(GL_BLEND); // glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.2f, 0.2f, 0.2f, 1); // glClearColor(0.2f, 0.2f, 0.2f, 1);
glDisable(GL_DEPTH_TEST); // glDisable(GL_DEPTH_TEST);
makeCurrent(); makeCurrent();
_context->syncCache();
setFramePosition(QPoint(-1000, 0)); setFramePosition(QPoint(-1000, 0));
resize(QSize(800, 600)); resize(QSize(800, 600));
@ -162,7 +221,7 @@ public:
void draw(); void draw();
void makeCurrent() { void makeCurrent() {
_context->makeCurrent(this); _qGlContext->makeCurrent(this);
} }
protected: protected:
@ -180,8 +239,12 @@ protected:
//static const wchar_t* EXAMPLE_TEXT = L"\xC1y Hello 1.0\ny\xC1 line 2\n\xC1y"; //static const wchar_t* EXAMPLE_TEXT = L"\xC1y Hello 1.0\ny\xC1 line 2\n\xC1y";
static const glm::uvec2 QUAD_OFFSET(10, 10); static const glm::uvec2 QUAD_OFFSET(10, 10);
static const glm::vec3 COLORS[4] = { { 1.0, 1.0, 1.0 }, { 0.5, 1.0, 0.5 }, { static const glm::vec3 COLORS[4] = {
1.0, 0.5, 0.5 }, { 0.5, 0.5, 1.0 } }; { 1.0, 1.0, 1.0 },
{ 0.5, 1.0, 0.5 },
{ 1.0, 0.5, 0.5 },
{ 0.5, 0.5, 1.0 }
};
void QTestWindow::draw() { void QTestWindow::draw() {
@ -190,15 +253,45 @@ void QTestWindow::draw() {
} }
makeCurrent(); makeCurrent();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, _size.width() * devicePixelRatio(), _size.height() * devicePixelRatio()); gpu::Batch batch;
static int frameNum = 0;
_context->swapBuffers(this); frameNum++;
glFinish(); batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLORS, { 1.0, float(frameNum % 60)/60.0f, 0.5, 1.0 });
// _context->render(batch);
//// batch.clear();
// batch.setViewportTransform({ 0, 0, _size.width() * devicePixelRatio(), _size.height() * devicePixelRatio() });
//
glm::quat cubeOrientation;
//
batch.setViewTransform(Transform());
batch.setProjectionTransform(glm::mat4());
// batch.setProjectionTransform(_viewFrustrum->getProjection());
batch.setModelTransform(Transform().setRotation(cubeOrientation));
batch.setPipeline(_pipeline);
batch.setInputBuffer(gpu::Stream::POSITION, _buffer, 0, 3);
batch.setInputFormat(_format);
batch.draw(gpu::TRIANGLES, 3);
_context->render(batch);
//
//// gpu::Stream::Format format;
//// format.setAttribute(gpu::Stream::POSITION, gpu::Stream::POSITION, gpu::Element(gpu::Vec3, gpu::FLOAT, gpu::XYZ));
//// batch.setInputBuffer(gpu::Stream::POSITION, _trianglePosBuffer, )
//
//
//// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//// glViewport(0, 0, _size.width() * devicePixelRatio(), _size.height() * devicePixelRatio());
//
//// _context->swapBuffers(this);
//// glFinish();
//
_qGlContext->swapBuffers(this);
fps.increment(); fps.increment();
if (fps.elapsed() >= 2.0f) { if (fps.elapsed() >= 2.0f) {
qDebug() << "FPS: " << fps.rate(); qDebug() << "FPS: " << fps.rate(); // This prints out the frames per 2 secs (ie. half of the actual fps) bug...?
fps.reset(); fps.reset();
} }
} }
@ -207,7 +300,7 @@ int main(int argc, char** argv) {
QGuiApplication app(argc, argv); QGuiApplication app(argc, argv);
QTestWindow window; QTestWindow window;
QTimer timer; QTimer timer;
timer.setInterval(1); timer.setInterval(1000 / 120.0f);
app.connect(&timer, &QTimer::timeout, &app, [&] { app.connect(&timer, &QTimer::timeout, &app, [&] {
window.draw(); window.draw();
}); });