From dbf94d673be4b128ce510d45fcada3c27b1567e5 Mon Sep 17 00:00:00 2001 From: Seiji Emery Date: Fri, 7 Aug 2015 12:01:58 -0700 Subject: [PATCH] Cleanup --- tests/gpu-test/src/main.cpp | 335 ++++++++++-------------------------- 1 file changed, 89 insertions(+), 246 deletions(-) diff --git a/tests/gpu-test/src/main.cpp b/tests/gpu-test/src/main.cpp index 126c9dd952..7cc6adbd50 100644 --- a/tests/gpu-test/src/main.cpp +++ b/tests/gpu-test/src/main.cpp @@ -24,16 +24,9 @@ #include #include #include -//#include - #include -//#include #include -//#include -//#include -//#include -//#include #include #include #include @@ -82,18 +75,6 @@ public: } }; - -const QString& getQmlDir() { - static QString dir; - if (dir.isEmpty()) { - QDir path(__FILE__); - path.cdUp(); - dir = path.cleanPath(path.absoluteFilePath("../../../interface/resources/qml/")) + "/"; - qDebug() << "Qml Path: " << dir; - } - return dir; -} - #define MOVE_PARAM(name) decltype(name) && name struct BasicModel { @@ -262,7 +243,21 @@ void renderCube(gpu::Batch & batch, const BasicModel & cube) { batch.draw(gpu::TRIANGLES, 24); } -// Create a simple OpenGL window that renders text in various ways + +gpu::ShaderPointer makeShader(const std::string & vertexShaderSrc, const std::string & fragmentShaderSrc, const gpu::Shader::BindingSet & bindings) { + auto vs = gpu::ShaderPointer(gpu::Shader::createVertex(vertexShaderSrc)); + auto fs = gpu::ShaderPointer(gpu::Shader::createPixel(fragmentShaderSrc)); + auto shader = gpu::ShaderPointer(gpu::Shader::createProgram(vs, fs)); + if (!gpu::Shader::makeProgram(*shader, bindings)) { + printf("Could not compile shader\n"); + exit(-1); + } + return shader; +} + + +// Creates an OpenGL window that renders a simple unlit scene using the gpu library and GeometryCache +// Should eventually get refactored into something that supports multiple gpu backends. class QTestWindow : public QWindow { Q_OBJECT @@ -271,9 +266,10 @@ class QTestWindow : public QWindow { gpu::ContextPointer _context; gpu::PipelinePointer _pipeline; - BasicModelPointer _cubeModel; - //TextRenderer* _textRenderer[4]; + glm::mat4 _projectionMatrix; +// BasicModelPointer _cubeModel; RateCounter fps; + QTime _time; protected: void renderText(); @@ -307,26 +303,88 @@ public: gpu::Context::init(); _context = std::make_shared(); + auto shader = makeShader(simple_vert, simple_frag, gpu::Shader::BindingSet {}); + auto state = std::make_shared(); + state->setMultisampleEnable(true); + state->setDepthTest(gpu::State::DepthTest { true }); + _pipeline = gpu::PipelinePointer(gpu::Pipeline::create(shader, state)); + + // Clear screen gpu::Batch batch; batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLORS, { 1.0, 0.0, 0.5, 1.0 }); _context->render(batch); - _cubeModel = makeCube(); +// _cubeModel = makeCube(); DependencyManager::set(); - -// makeCurrent(); -// _context->syncCache(); setFramePosition(QPoint(-1000, 0)); resize(QSize(800, 600)); + + _time.start(); } virtual ~QTestWindow() { } - void draw(); + void draw() { + if (!isVisible()) { + return; + } + makeCurrent(); + + gpu::Batch batch; + batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLORS, { 0.0f, 0.0f, 0.0f, 1.0f }); + batch.clearDepthFramebuffer(1e4); + batch.setViewportTransform({ 0, 0, _size.width() * devicePixelRatio(), _size.height() * devicePixelRatio() }); + batch.setProjectionTransform(_projectionMatrix); + + double t = _time.elapsed() * 1e-3; + glm::vec3 unitscale { 1.0f }; + glm::vec3 up { 0.0f, 1.0f, 0.0f }; + + glm::vec3 cam_pos { 1.5f * sin(t), 0.0f, 2.0f }; +// glm::vec3 camera_focus { 5.0f * cos(t * 0.1f), 0.0f, 0.0f }; + glm::vec3 camera_focus { 0.0f, 0.0f, 0.0f }; + glm::quat cam_rotation; + // glm::quat cam_rotation = glm::quat_cast(glm::lookAt(cam_pos, camera_focus, up)); + // cam_rotation.w = -cam_rotation.w; + // printf("cam rotation: %f %f %f %f\n", cam_rotation.x, cam_rotation.y, cam_rotation.z, cam_rotation.w); + Transform cam_transform { cam_rotation, unitscale, cam_pos }; + + batch.setViewTransform(cam_transform); + batch.setPipeline(_pipeline); + + auto geometryCache = DependencyManager::get(); + + // Render grid on xz plane (not the optimal way to do things, but w/e) + // Note: GeometryCache::renderGrid will *not* work, as it is apparenly unaffected by batch rotations and renders xy only + batch.setModelTransform(Transform()); + for (int i = 0; i < 100; ++i) { + geometryCache->renderLine(batch, { -100.0f, -1.0f, -50.0f + float(i) }, { 100.0f, -1.0f, -50.0f + float(i) }, { 0.35f, 0.25f, 0.15f, 1.0f }); + } + for (int i = 0; i < 100; ++i) { + geometryCache->renderLine(batch, { -50.0f + float(i), -1.0f, -100.0f}, { -50.0f + float(i), -1.0f, 100.0f }, { 0.15f, 0.25f, 0.35f, 1.0f }); + } + + // Render unlit cube + sphere + geometryCache->renderUnitCube(batch); + geometryCache->renderWireCube(batch, 1.0f, { 0.4f, 0.4f, 0.7f, 1.0f }); + + batch.setModelTransform(Transform().setTranslation({ 1.5f, -0.5f, -0.5f })); + geometryCache->renderSphere(batch, 0.5f, 50, 50, { 0.8f, 0.25f, 0.25f }); + + _context->render(batch); + _qGlContext->swapBuffers(this); + + fps.increment(); + if (fps.elapsed() >= 0.5f) { + qDebug() << "FPS: " << fps.rate(); + fps.reset(); + } + } + void makeCurrent() { _qGlContext->makeCurrent(this); } @@ -334,234 +392,19 @@ public: protected: void resizeEvent(QResizeEvent* ev) override { resizeWindow(ev->size()); - } -}; - -#ifndef SERIF_FONT_FAMILY -#define SERIF_FONT_FAMILY "Times New Roman" -#endif - -//static const wchar_t* EXAMPLE_TEXT = L"Hello"; -//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::vec3 COLORS[4] = { - { 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 renderTestScene (gpu::Batch & batch) { - std::once_flag initFlag; - gpu::PipelinePointer pipeline { nullptr }; - - std::call_once(initFlag, [&](){ - auto vs = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(simple_vert))); - auto fs = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(simple_frag))); - auto shader = gpu::ShaderPointer(gpu::Shader::createProgram(vs, fs)); - gpu::Shader::BindingSet bindings; - if (!gpu::Shader::makeProgram(*shader, bindings)) { - printf("Could not compile shader\n"); - if (!vs) - printf("bad vertex shader\n"); - if (!fs) - printf("bad fragment shader\n"); - if (!shader) - printf("bad shader program\n"); - exit(-1); - } - auto state = std::make_shared(); - state->setMultisampleEnable(true); - state->setDepthTest({ true }); - pipeline = gpu::PipelinePointer(gpu::Pipeline::create(shader, state)); - }); - batch.setPipeline(pipeline); - auto geometryCache = DependencyManager::get(); - - float scale = 80.0f; - - batch.setModelTransform(Transform(glm::angleAxis(40.0f, glm::vec3{ 0.0f, 1.0f, 0.0f}), glm::vec3 { scale }, glm::vec3 { -50.0f, -50.0f, 0.0f })); -// geometryCache->renderGrid(batch, 800, 800, { 0.35f, 0.25f, 0.15f, 1.0f }); -// geometryCache->renderGrid(batch, 200, 200, { 0.4f, 0.4f, 0.9f, 1.0f }); - - - batch.setModelTransform(Transform()); - - for (int i = 0; i < 100; ++i) { - geometryCache->renderLine(batch, { -100.0f, -1.0f, -50.0f + float(i) }, { 100.0f, -1.0f, -50.0f + float(i) }, { 0.35f, 0.25f, 0.15f, 1.0f }); - } - for (int i = 0; i < 100; ++i) { - geometryCache->renderLine(batch, { -50.0f + float(i), -1.0f, -100.0f}, { -50.0f + float(i), -1.0f, 100.0f }, { 0.15f, 0.25f, 0.35f, 1.0f }); - } - - geometryCache->renderUnitCube(batch); - geometryCache->renderWireCube(batch, 1.0f, { 0.4f, 0.4f, 0.7f, 1.0f }); - - batch.setModelTransform(Transform().setTranslation({ 1.5f, -0.5f, -0.5f })); - geometryCache->renderSphere(batch, 0.5f, 50, 50, { 0.8f, 0.25f, 0.25f }); -// geometryCache->renderWireCube(batch, 1.0f, { 0.2f, 0.2f, 0.2f, 1.0f }); -} - -void QTestWindow::draw() { - if (!isVisible()) { - return; - } - makeCurrent(); - gpu::Batch batch; - static int frameNum = 0; - frameNum++; - float t = frameNum / 120.0f; - - float k = (frameNum % 120) / 120; - float ks = glm::sin(glm::pi() * 2.0f * k); - - batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLORS, { 0.1, 0.1, 0.1, 1.0 }); - batch.clearDepthFramebuffer(100.0f); - batch.setViewportTransform({ 0, 0, _size.width() * devicePixelRatio(), _size.height() * devicePixelRatio() }); - -// glm::vec3 camera_focus { 5.0f * cos(t * 0.1f), 0.0f, 0.0f }; - glm::vec3 camera_focus { 0.0f, 0.0f, 0.0f }; - - glm::vec3 unitscale { 1.0f }; - glm::vec3 up { 0.0f, 1.0f, 0.0f }; - glm::vec3 cam_pos { 1.5f * sin(t), 0.0f, 2.0f }; -// glm::vec3 cam_pos { 5.0f * sin(t * 0.1f), 1.0f, 10.0f }; -// glm::quat cam_rotation; - glm::quat cam_rotation = glm::quat_cast(glm::lookAt(cam_pos, camera_focus, up)); - cam_rotation.w = -cam_rotation.w; - Transform cam_transform { cam_rotation, unitscale, cam_pos }; - - - float fov_degrees = 60; -// float aspect_ratio = _size.height() / (_size.width() || 1.0f); - float aspect_ratio = (float)_size.width() / _size.height(); -// float aspect_ratio = 16.0f / 9.0f; + float fov_degrees = 60.0f; + float aspect_ratio = (float)_size.width() / _size.height(); float near_clip = 0.1f; float far_clip = 1000.0f; - auto projection = glm::perspective(glm::radians(fov_degrees), aspect_ratio, near_clip, far_clip); - - batch.setProjectionTransform(projection); - - -// batch.setViewTransform(Transform().setTranslation({ 1.5f * sin(t), 0.5f, 1.0f })); - batch.setViewTransform(cam_transform); - - renderTestScene(batch); - - _context->render(batch); - _qGlContext->swapBuffers(this); - - fps.increment(); - if (fps.elapsed() >= 2.0f) { - qDebug() << "FPS: " << fps.rate() * 2.0f; // This prints out the frames per 2 secs (ie. half of the actual fps) bug...? - fps.reset(); + _projectionMatrix = glm::perspective(glm::radians(fov_degrees), aspect_ratio, near_clip, far_clip); } -} - - - - - - -//void QTestWindow::draw() { -// if (!isVisible()) { -// return; -// } -// -// makeCurrent(); -// -// gpu::Batch batch; -// static int frameNum = 0; -// frameNum++; -// float t = frameNum / 120.0f; -// -// float k = (frameNum % 120) / 120; -// float ks = glm::sin(glm::pi() * 2.0f * k); -// -// batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLORS, { 0.1, 0.1, 0.1, 1.0 }); -//// batch.clearDepthFramebuffer(-10000.0f); -//// batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLORS, { 1.0, float(frameNum % 60)/60.0f, 0.5, 1.0 }); -// -// batch.setViewportTransform({ 0, 0, _size.width() * devicePixelRatio(), _size.height() * devicePixelRatio() }); -// -// -// -// // camera at x: 5 * sin(t) -// // y: 1, -// // z: +10 -// // obj at (0, 0, 0) -// // camera is looking at obj (using glm::lookAt) -// -// glm::vec3 up { 0.0f, 1.0f, 0.0f }; -// glm::vec3 unitscale { 1.0f }; -// -// float cube_angle = 0.0f; -//// glm::vec3 cube_pos { -//// 0.0f, -//// 0.0f, -//// 0.0f -//// }; -// glm::vec3 cube_pos { -// 20.0f * cos(t * 5.0f), -// 10.0f * sin(t * 2.5f) + 1.0f, -// -15.0f + float(int(t * int(1e3)) % int(1e4)) / 1e3 -// }; -// -// // float cube_angle = 360.0f * k * 1.25 + 120.0f * k * 0.1f; -//// glm::quat cube_rotation = glm::angleAxis(glm::radians(cube_angle), up); -// glm::quat cube_rotation; -// Transform cube_transform { cube_rotation, unitscale, cube_pos }; -// -//// glm::vec3 cam_pos { 0.0f, 0.0f, -10.0f }; -// glm::vec3 cam_pos { 5.0f * sin(t * 0.1f), 1.0f, -10.0f }; -// glm::quat cam_rotation = glm::quat_cast(glm::lookAt(cam_pos, cube_pos, up)); -// cam_rotation.w = -cam_rotation.w; -// Transform cam_transform { cam_rotation, unitscale, cam_pos }; -// -// float fov_degrees = 120.0f; -//// float aspect_ratio = _size.height() / (_size.width() || 1.0f); -// float aspect_ratio = 16.0f / 9.0f; -// float near_clip = 0.1f; -// float far_clip = 1000.0f; -// auto projection = glm::perspective(glm::radians(fov_degrees), aspect_ratio, near_clip, far_clip); -// -// batch.setProjectionTransform(projection); -// batch.setViewTransform(cam_transform); -// batch.setModelTransform(cube_transform); -// -// batch.setModelTransform(Transform().setTranslation({ 20.0f * cos(t * 5.0f), 10.0f * sin(t * 2.5f + 1.0f), -15.0f + float(int(t * 1000) % 10000) / 1e3f})); -//// batch.setPipeline(_pipeline); -//// batch.setInputBuffer(gpu::Stream::POSITION, _buffer, 0, 3); -//// batch.setInputFormat(_format); -//// batch.draw(gpu::TRIANGLES, 3); -// -// renderCube(batch, *_cubeModel); -// -// DependencyManager::get()->renderGrid(batch, 4, 4, glm::vec4 { 0.3f, 0.3f, 0.3f, 1.0f }); -// _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, ) -// -// _qGlContext->swapBuffers(this); -// // glFinish(); -// -// fps.increment(); -// if (fps.elapsed() >= 2.0f) { -// qDebug() << "FPS: " << fps.rate() * 2.0f; // This prints out the frames per 2 secs (ie. half of the actual fps) bug...? -// fps.reset(); -// } -//} +}; int main(int argc, char** argv) { QGuiApplication app(argc, argv); QTestWindow window; QTimer timer; -// timer.setInterval(1000 / 120.0f); timer.setInterval(0); app.connect(&timer, &QTimer::timeout, &app, [&] { window.draw();