diff --git a/CMakeLists.txt b/CMakeLists.txt index 8467bf38..c62cdd5d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -317,6 +317,11 @@ if(BUILD_TESTING) set_tests_properties(timing_observer_regression timing_feedback_regression PROPERTIES TIMEOUT 10) add_executable(grSim_rendering_test tests/rendering_regression.cpp) target_link_libraries(grSim_rendering_test grSim_core) + if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + target_compile_definitions(grSim_rendering_test PRIVATE GRSIM_CHECK_PRIMITIVE_MODES) + set_property(TARGET grSim_rendering_test APPEND_STRING PROPERTY + LINK_FLAGS " -Wl,--wrap=glBegin") + endif() add_test(NAME rendering_regression COMMAND grSim_rendering_test) set_tests_properties(rendering_regression PROPERTIES TIMEOUT 30 LABELS "opengl") if(UNIX AND NOT APPLE) diff --git a/INSTALL.md b/INSTALL.md index 9e75b854..408bacab 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -224,6 +224,21 @@ A compatible system ODE is used without this bundled patch. When running grSim, check the FPS in the status bar. If it is running at **60 FPS** or higher, everything is ok. Otherwise check the graphics card's driver installation and OpenGL settings. +### WSL graphics device + +The status bar reports the renderer from the active OpenGL context. If WSL uses +llvmpipe/software rendering and you want to select an NVIDIA adapter, launch +from the repository root with: + +```bash +env -u LIBGL_ALWAYS_SOFTWARE GALLIUM_DRIVER=d3d12 \ + MESA_D3D12_DEFAULT_ADAPTER_NAME=NVIDIA ./bin/grSim +``` + +Check that the Graphics indicator names your NVIDIA GPU. This selects the +graphics device for this launch; physics still runs on its separate CPU worker. + + ## Apple Silicon application (macOS 15 or later) The macOS CI jobs build native arm64 applications on macOS 15 and 26. Both jobs diff --git a/include/glwidget.h b/include/glwidget.h index 78000841..2432b3cc 100644 --- a/include/glwidget.h +++ b/include/glwidget.h @@ -62,6 +62,10 @@ class GLWidget : public QGLWidget { const RobotSnapshot* robotSnapshot(int index) const; QImage robotImage(int index) const; void sendAction(SimulationAction action); + QString graphicsRendererName() const { return graphicsRenderer; } + QString graphicsRendererVendor() const { return graphicsVendor; } + QString graphicsRendererVersion() const { return graphicsVersion; } + bool usesSoftwareRendering() const { return softwareRendering; } bool graphicsEnabled = true; int selected = -1; std::array cursor{{0, 0, 0}}; @@ -128,6 +132,8 @@ public slots: int moving_robot_id,clicked_robot; SceneSnapshotPtr snapshot; SceneRenderer renderer; + QString graphicsRenderer, graphicsVendor, graphicsVersion; + bool softwareRendering = false; QWidget* lagWarning; bool showCursor = false; double cursorRadius = 0; diff --git a/include/mainwindow.h b/include/mainwindow.h index 71f98655..e7934cdc 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -75,6 +75,7 @@ public slots: int robotIndex(int robot,int team); private: int getRenderInterval(); + void updateGraphicsDeviceStatus(); SimulationController* simulation; QTimer *renderTimer; QTimer *uiTimer; @@ -95,6 +96,7 @@ public slots: QAction *showsimulator, *showconfig, *showrobot; QAction* fullScreenAct; QLabel *fpslabel,*cursorlabel,*selectinglabel,*vanishlabel,*noiselabel; + QLabel* graphicsDeviceLabel; QString current_dir; std::array previousVelocity{{0, 0, 0}}; diff --git a/src/glwidget.cpp b/src/glwidget.cpp index 1e60b796..a099d60b 100644 --- a/src/glwidget.cpp +++ b/src/glwidget.cpp @@ -386,7 +386,23 @@ void GLWidget::mouseMoveEvent(QMouseEvent* event) } void GLWidget::mouseReleaseEvent(QMouseEvent*) { emit clicked(); } -void GLWidget::initializeGL() { if (snapshot) renderer.initialize(*snapshot); } +void GLWidget::initializeGL() +{ + // Query this widget's current context once, rather than probing installed GPUs. + auto contextString = [](GLenum name) { + const auto* value = glGetString(name); + return value ? QString::fromLatin1(reinterpret_cast(value)) : QString(); + }; + graphicsRenderer = contextString(GL_RENDERER); + graphicsVendor = contextString(GL_VENDOR); + graphicsVersion = contextString(GL_VERSION); + const QString name = graphicsRenderer.toLower(); + softwareRendering = name.contains("llvmpipe") || name.contains("softpipe") || + name.contains("swrast") || name.contains("swiftshader") || + name.contains("software") || name.contains("gdi generic") || + name.contains("microsoft basic render"); + if (snapshot) renderer.initialize(*snapshot); +} void GLWidget::resizeGL(int width, int height) { glViewport(0, 0, width, height); } bool GLWidget::hasFramebufferObjects() const { return QGLFramebufferObject::hasOpenGLFramebufferObjects(); } bool GLWidget::hasFramebufferBlit() const { return QGLFramebufferObject::hasOpenGLFramebufferBlit(); } diff --git a/src/graphics.cpp b/src/graphics.cpp index 23c0d41a..29f7d2b1 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -40,6 +40,50 @@ const dReal sky_scale = 1.0f; // sky texture scale (1/size) const dReal sky_height = 1.0f; // sky height above viewpoint +namespace { +// Emit the same fan triangles explicitly. D3D12 has no native triangle-fan +// topology; converting cached indexed fans can force a GPU-to-CPU index readback. +// These cap vertices share one normal/material; texture coordinates vary per vertex. +class TriangleFan { +public: + explicit TriangleFan(bool textured = false) : textured(textured) { glBegin(GL_TRIANGLES); } + ~TriangleFan() { glEnd(); } + TriangleFan(const TriangleFan&) = delete; + TriangleFan& operator=(const TriangleFan&) = delete; + + void texCoord(GLdouble s, GLdouble t) + { + textureS = s; textureT = t; + glTexCoord2d(s, t); + } + + void vertexFloat(GLfloat x, GLfloat y, GLfloat z) { vertex(x, y, z); } + + void vertex(GLdouble x, GLdouble y, GLdouble z) + { + const Vertex value{x, y, z, textureS, textureT}; + if (count == 0) first = value; + else if (count > 1) { + emitVertex(first); emitVertex(previous); emitVertex(value); + } + previous = value; + ++count; + } + +private: + struct Vertex { GLdouble x, y, z, s, t; }; + void emitVertex(const Vertex& value) + { + if (textured) glTexCoord2d(value.s, value.t); + glVertex3d(value.x, value.y, value.z); + } + bool textured; + unsigned count = 0; + GLdouble textureS = 0, textureT = 0; + Vertex first{}, previous{}; +}; +} + CGraphics::CGraphics(QGLWidget* _owner) { owner = _owner; @@ -784,15 +828,17 @@ void CGraphics::drawSSLGround(dReal SSL_FIELD_RAD,dReal SSL_FIELD_LENGTH,dReal S glEnd(); //Penalty spots - glBegin(GL_POLYGON); - for(angle=0.0f; angle <= 2.0 * M_PI; angle+=anglestep) - glVertex3f(-fw + penx + cos(angle) * lw, 0.0f + sin(angle) * lw, epsilon); - glEnd(); + { + TriangleFan fan; + for(angle=0.0f; angle <= 2.0 * M_PI; angle+=anglestep) + fan.vertexFloat(-fw + penx + cos(angle) * lw, 0.0f + sin(angle) * lw, epsilon); + } - glBegin(GL_POLYGON); - for(angle=0.0f; angle <= 2.0 * M_PI; angle+=anglestep) - glVertex3f(fw - penx + cos(angle) * lw, 0.0f + sin(angle) * lw, epsilon); - glEnd(); + { + TriangleFan fan; + for(angle=0.0f; angle <= 2.0 * M_PI; angle+=anglestep) + fan.vertexFloat(fw - penx + cos(angle) * lw, 0.0f + sin(angle) * lw, epsilon); + } glEndList(); } @@ -834,22 +880,24 @@ void CGraphics::_drawBox () glEnd(); // top face - glBegin (GL_TRIANGLE_FAN); - glNormal3f (0,0,1); - glVertex3f (-lx,-ly,lz); - glVertex3f (lx,-ly,lz); - glVertex3f (lx,ly,lz); - glVertex3f (-lx,ly,lz); - glEnd(); + { + TriangleFan fan; + glNormal3f (0,0,1); + fan.vertexFloat(-lx,-ly,lz); + fan.vertexFloat(lx,-ly,lz); + fan.vertexFloat(lx,ly,lz); + fan.vertexFloat(-lx,ly,lz); + } // bottom face - glBegin (GL_TRIANGLE_FAN); - glNormal3f (0,0,-1); - glVertex3f (-lx,-ly,-lz); - glVertex3f (-lx,ly,-lz); - glVertex3f (lx,ly,-lz); - glVertex3f (lx,-ly,-lz); - glEnd(); + { + TriangleFan fan; + glNormal3f (0,0,-1); + fan.vertexFloat(-lx,-ly,-lz); + fan.vertexFloat(-lx,ly,-lz); + fan.vertexFloat(lx,ly,-lz); + fan.vertexFloat(lx,-ly,-lz); + } glEndList(); } @@ -963,17 +1011,18 @@ void CGraphics::drawCircle(dReal x0,dReal y0,dReal z0,dReal r) // draw top cap glShadeModel (GL_FLAT); ny=1; nz=0; // normal vector = (0,ny,nz) - glBegin (GL_TRIANGLE_FAN); - glNormal3d (0,0,1); - for (i=0; i<=n; i++) { + { + TriangleFan fan; glNormal3d (0,0,1); - glVertex3d (ny*r+x0,nz*r+y0,z0); - // rotate ny,nz - tmp = ca*ny - sa*nz; - nz = sa*ny + ca*nz; - ny = tmp; + for (i=0; i<=n; i++) { + glNormal3d (0,0,1); + fan.vertex(ny*r+x0,nz*r+y0,z0); + // rotate ny,nz + tmp = ca*ny - sa*nz; + nz = sa*ny + ca*nz; + ny = tmp; + } } - glEnd(); } // draw a capped cylinder of length l and radius r, aligned along the x axis @@ -1092,31 +1141,33 @@ static void compileCylinderGeometry(int n) // draw top cap glShadeModel (GL_FLAT); ny=1; nz=0; - glBegin (GL_TRIANGLE_FAN); - glNormal3d (0,0,1); - glVertex3d (0,0,l); - for (i=0; i<=n; i++) { + { + TriangleFan fan; glNormal3d (0,0,1); - glVertex3d (ny*r,nz*r,l); - tmp = ca*ny - sa*nz; - nz = sa*ny + ca*nz; - ny = tmp; + fan.vertex(0,0,l); + for (i=0; i<=n; i++) { + glNormal3d (0,0,1); + fan.vertex(ny*r,nz*r,l); + tmp = ca*ny - sa*nz; + nz = sa*ny + ca*nz; + ny = tmp; + } } - glEnd(); // draw bottom cap ny=1; nz=0; - glBegin (GL_TRIANGLE_FAN); - glNormal3d (0,0,-1); - glVertex3d (0,0,-l); - for (i=0; i<=n; i++) { + { + TriangleFan fan; glNormal3d (0,0,-1); - glVertex3d (ny*r,nz*r,-l); - tmp = ca*ny + sa*nz; - nz = -sa*ny + ca*nz; - ny = tmp; + fan.vertex(0,0,-l); + for (i=0; i<=n; i++) { + glNormal3d (0,0,-1); + fan.vertex(ny*r,nz*r,-l); + tmp = ca*ny + sa*nz; + nz = -sa*ny + ca*nz; + ny = tmp; + } } - glEnd(); } void CGraphics::_drawCylinder (bool lowLod) @@ -1164,21 +1215,19 @@ static void compileTexturedCylinder(int sides, bool robot, int part) glEnd(); } } else if (part == 1) { - glBegin(GL_TRIANGLE_FAN); + TriangleFan fan(true); glNormal3d(0, 0, 1); for (int i = first; i < (robot ? last + 1 : sides); ++i) { const double x = std::cos(i * angle), y = std::sin(i * angle); - glTexCoord2d(-0.5 * y + 0.5, 0.5 * x + 0.5); - glVertex3d(x, y, 0.5); + fan.texCoord(-0.5 * y + 0.5, 0.5 * x + 0.5); + fan.vertex(x, y, 0.5); } - glEnd(); } else { - glBegin(GL_TRIANGLE_FAN); + TriangleFan fan; glNormal3d(0, 0, -1); // Reverse the same boundary to retain the chassis cutout on both caps. for (int i = last; i >= first; --i) - glVertex3d(std::cos(i * angle), std::sin(i * angle), -0.5); - glEnd(); + fan.vertex(std::cos(i * angle), std::sin(i * angle), -0.5); } } diff --git a/src/main.cpp b/src/main.cpp index b51aa54c..ebbf9d2b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -53,7 +53,7 @@ int main(int argc, char *argv[]) w.setIsGlEnabled(false); } else { // Run normally - w.show(); + w.showMaximized(); } return a.exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 03216225..8438eac9 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -100,6 +100,17 @@ MainWindow::MainWindow(QWidget *parent, ConfigWidget *config) statusBar()->addWidget(selectinglabel); statusBar()->addWidget(vanishlabel); statusBar()->addWidget(noiselabel); + graphicsDeviceLabel = new QLabel(tr("Graphics: initializing..."), this); + graphicsDeviceLabel->setObjectName("graphicsDeviceStatus"); + graphicsDeviceLabel->setTextFormat(Qt::PlainText); + graphicsDeviceLabel->setMinimumWidth(150); + graphicsDeviceLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); + statusBar()->addPermanentWidget(graphicsDeviceLabel, 1); + auto* physicsDeviceLabel = new QLabel(tr("Physics: CPU"), this); + physicsDeviceLabel->setObjectName("physicsProcessorStatus"); + physicsDeviceLabel->setToolTip(tr("Physics runs on the CPU, separately from rendering.")); + statusBar()->addPermanentWidget(physicsDeviceLabel); + /* Menus */ QMenu *fileMenu = new QMenu("&File"); @@ -208,7 +219,6 @@ MainWindow::MainWindow(QWidget *parent, ConfigWidget *config) uiTimer->start(); - this->showMaximized(); this->setWindowTitle("grSim"); robotwidget->teamCombo->setCurrentIndex(0); @@ -311,8 +321,28 @@ void MainWindow::recordRenderedFrame() ++m_renderFramesThisSecond; } +void MainWindow::updateGraphicsDeviceStatus() +{ + const QString renderer = glwidget->graphicsRendererName(); + QString label; + if (!glwidget->graphicsEnabled) label = tr("Graphics: disabled"); + else if (renderer.isEmpty()) label = tr("Graphics: initializing..."); + else if (glwidget->usesSoftwareRendering()) label = tr("Graphics: Software (CPU)"); + else label = tr("Graphics: %1").arg(renderer); + graphicsDeviceLabel->setText(graphicsDeviceLabel->fontMetrics().elidedText( + label, Qt::ElideMiddle, graphicsDeviceLabel->width())); + graphicsDeviceLabel->setToolTip( + tr("Renderer: %1\nVendor: %2\nOpenGL: %3\n%4") + .arg(renderer.isEmpty() ? tr("Not initialized") : renderer, + glwidget->graphicsRendererVendor(), glwidget->graphicsRendererVersion(), + glwidget->usesSoftwareRendering() + ? tr("Software rendering and physics run separately on the CPU.") + : tr("Physics runs on the CPU, separately from rendering."))); +} + void MainWindow::updateUI() { + updateGraphicsDeviceStatus(); const auto snapshot = glwidget->refreshSnapshot(); if (!snapshot) return; const int index = robotIndex(glwidget->Current_robot, glwidget->Current_team); diff --git a/tests/application_smoke.cpp b/tests/application_smoke.cpp index 3470573b..df9d7837 100644 --- a/tests/application_smoke.cpp +++ b/tests/application_smoke.cpp @@ -1,10 +1,13 @@ #include #include #include +#include +#include #include #include #include #include +#include #include #include #include "mainwindow.h" @@ -38,11 +41,16 @@ int runApplicationSmokeTest(QApplication& app) config->v_YellowControlListenPort->setInt(0); config->v_sendDelay->setInt(0); MainWindow window(nullptr, config); + const bool initiallyHidden = !window.isVisible(); window.resize(800, 600); window.show(); auto* view = window.findChild(); if (!view) return 2; + std::atomic renderOnGuiThread{true}; + QObject::connect(view, &GLWidget::frameRendered, [&]() { + if (QThread::currentThread() != app.thread()) renderOnGuiThread.store(false); + }); int frames = 0, responses = 0; double firstTime = -1, lastTime = -1; bool monotonic = true, havePosition = false; @@ -105,11 +113,19 @@ int runApplicationSmokeTest(QApplication& app) return !image.isNull() && colors.size() > 16; }; const bool textures = !QImage(":/y0.png").isNull() && !QImage(":/b0.png").isNull(); - const bool rendering = view->isValid() && nonblank(normal) && nonblank(low); + const auto* graphicsStatus = window.findChild("graphicsDeviceStatus"); + const auto* physicsStatus = window.findChild("physicsProcessorStatus"); + const QString device = view->graphicsRendererName(); + const bool deviceStatus = graphicsStatus && physicsStatus && !device.isEmpty() && + graphicsStatus->toolTip().contains(device) && physicsStatus->text().contains("CPU"); + const bool rendering = initiallyHidden && view->isValid() && renderOnGuiThread.load() && + nonblank(normal) && nonblank(low); const bool network = frames > 30 && responses > 5 && monotonic && lastTime-firstTime > 0.5; - result = resources && textures && rendering && network && distance > 10 ? 0 : 1; + result = resources && textures && rendering && deviceStatus && network && distance > 10 ? 0 : 1; std::printf("%s application smoke: config=%d textures=%d OpenGL=%d vision=%d feedback=%d moved=%.1fmm\n", result == 0 ? "PASS" : "FAIL", resources, textures, rendering, frames, responses, distance); + std::printf("Renderer: %s; status=%d; rendering_on_gui_thread=%d\n", + device.toUtf8().constData(), deviceStatus, renderOnGuiThread.load()); std::fflush(stdout); app.exit(result); }); diff --git a/tests/rendering_regression.cpp b/tests/rendering_regression.cpp index 8d9efbc4..dab03a5c 100644 --- a/tests/rendering_regression.cpp +++ b/tests/rendering_regression.cpp @@ -12,6 +12,16 @@ #include #include "mainwindow.h" +#ifdef GRSIM_CHECK_PRIMITIVE_MODES +static unsigned legacyPrimitiveCalls = 0; +extern "C" void __real_glBegin(GLenum mode); +extern "C" void __wrap_glBegin(GLenum mode) +{ + if (mode == GL_TRIANGLE_FAN || mode == GL_POLYGON) ++legacyPrimitiveCalls; + __real_glBegin(mode); +} +#endif + class TestView : public GLWidget { public: TestView(ConfigWidget* config, SimulationController* simulation) @@ -268,6 +278,12 @@ int main(int argc, char** argv) passed = checkNonblankImage(target.toImage(), true, QSize(377, 211)) && passed; target.release(); } +#ifdef GRSIM_CHECK_PRIMITIVE_MODES + const bool nativeTopology = legacyPrimitiveCalls == 0; + std::printf("%s cached geometry avoids legacy primitive conversion (%u fan/polygon calls)\n", + nativeTopology ? "PASS" : "FAIL", legacyPrimitiveCalls); + passed = nativeTopology && passed; +#endif delete window; return passed ? 0 : 1; }