Skip to content
Merged
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions include/glwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<double, 3> cursor{{0, 0, 0}};
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions include/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public slots:
int robotIndex(int robot,int team);
private:
int getRenderInterval();
void updateGraphicsDeviceStatus();
SimulationController* simulation;
QTimer *renderTimer;
QTimer *uiTimer;
Expand All @@ -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<double, 3> previousVelocity{{0, 0, 0}};
Expand Down
18 changes: 17 additions & 1 deletion src/glwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*>(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(); }
Expand Down
161 changes: 105 additions & 56 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main(int argc, char *argv[])
w.setIsGlEnabled(false);
} else {
// Run normally
w.show();
w.showMaximized();
}
return a.exec();
}
32 changes: 31 additions & 1 deletion src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -208,7 +219,6 @@ MainWindow::MainWindow(QWidget *parent, ConfigWidget *config)
uiTimer->start();


this->showMaximized();
this->setWindowTitle("grSim");

robotwidget->teamCombo->setCurrentIndex(0);
Expand Down Expand Up @@ -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);
Expand Down
Loading
Loading