Renderer crashes with OpenGL error when deleted and created a second time
- Dominant language
- C++
- Stars
- 14k
- Forks
- 2.6k
- Avg merge
- 5d 18h
- Merged PRs (30d)
- 6
Description
### Checklist
- [X] I have searched for [similar issues](https://github.com/isl-org/Open3D/issues).
- [X] For Python issues, I have tested with the [latest development wheel](https://www.open3d.org/docs/latest/getting_started.html#development-version-pip).
- [X] I have checked the [release documentation](https://www.open3d.org/docs/release/) and the [latest documentation](https://www.open3d.org/docs/latest/) (for `main` branch).
### Describe the issue
The code below is basically from the Open3D offscreen rendering example. It just creates a renderer, renders a torus, and deletes the renderer. Now if this code is run in the debugger, in a **debug build**, and only then, then the first "iteration" of creating the renderer & scene, rendering the image, and deleting the scene and renderer, works. But when it comes to creating the renderer again (either the second iteration of the loop, or as in the code below, just deleting the renderer & scene and creating a new renderer & scene in a separate variable, then that second call to `app.RenderToImage(...)` crashes with the OpenGL error posted below.
I suspect this problem hasn't been discovered before because first most people don't delete and re-create a renderer, but second of all, the bug is hard to reproduce in relwithdebinfo and/or release mode. In release and relwithdebinfo mode, the code appears to run fine, in fact it runs fine for a few thousand iterations - and then at some point there's an `Exception thrown at 0x00007FFBE09349C3 (Open3D.dll) in main.exe: 0xC0000005: Access violation writing location 0x0000022800000000` - but there isn't an OpenGL error visible in the terminal. I suspect this is because the GL/glew release libraries don't output stuff in release mode, while the debug ones do. It is also interesting that in release/relwithdebinfo modes, the below example always crashes after the same number of iterations (2040).
It feels to me what's going on is that Open3D or Filament don't leave the OpenGL pipeline in a clean state and/or don't properly clean up after themselves - something like that.
It is possible that this bug is related to #7129, but I'm not sure.
### Steps to reproduce the bug
```python
#include "open3d/Open3D.h"
#include "open3d/visualization/rendering/Camera.h"
#include "open3d/visualization/rendering/filament/FilamentEngine.h"
#include "open3d/visualization/rendering/filament/FilamentRenderer.h"
using namespace open3d;
using namespace open3d::visualization::gui;
using namespace open3d::visualization::rendering;
static const bool kUseHeadless = false;
int main(int argc, const char* argv[])
{
{
const int width = 1280;
const int height = 800;
auto& app = Application::GetInstance();
app.Initialize(
R"(C:\Users\User\Dependencies\open3d-prebuilt\open3d-devel-windows-amd64-0.19.0\open3d-devel-windows-amd64-0.19.0\bin\resources)");
if (kUseHeadless)
{
EngineInstance::EnableHeadless();
}
int counter = 0;
// for (int i = 0; i < 10; ++i)
while (true)
{
auto* renderer = new FilamentRenderer(EngineInstance::GetInstance(), width, height,
EngineInstance::GetResourceManager());
auto* scene = new Open3DScene(*renderer);
MaterialRecord mat;
mat.shader = "defaultLit";
auto torus = open3d::geometry::TriangleMesh::CreateTorus();
torus->ComputeVertexNormals();
torus->PaintUniformColor({1.0f, 1.0f, 0.0f});
scene->AddGeometry("torus", torus.get(), mat);
scene->ShowAxes(true);
scene->GetCamera()->SetProjection(60.0f, float(width) / float(height), 0.1f, 10.0f,
Camera::FovType::Vertical);
scene->GetCamera()->LookAt({0.0f, 0.0f, 0.0f}, {3.0f, 3.0f, 3.0f}, {0.0f, 1.0f, 0.0f});
auto img = app.RenderToImage(*renderer, scene->GetView(), scene->GetScene(), width, height);
delete scene;
delete renderer;
if (counter >= 18700 && counter <= 18799)
{
io::WriteImage("./out/" + std::to_string(counter) + ".png", *img);
}
if (counter % 100 == 0)
{
io::WriteImage("./out/" + std::to_string(counter) + ".png", *img);
}
std::cout << counter << std::endl;
counter++;
auto* renderer2 = new FilamentRenderer(EngineInstance::GetInstance(), width, height,
EngineInstance::GetResourceManager());
auto* scene2 = new Open3DScene(*renderer2);
MaterialRecord mat2;
mat2.shader = "defaultLit";
auto torus2 = open3d::geometry::TriangleMesh::CreateTorus();
torus2->ComputeVertexNormals();
torus2->PaintUniformColor({1.0f, 1.0f, 0.0f});
scene2->AddGeometry("torus", torus2.get(), mat2);
scene2->ShowAxes(true);
scene2->GetCamera()->SetProjection(60.0f, float(width) / float(height), 0.1f, 10.0f,
Camera::FovType::Vertical);
scene2->GetCamera()->LookAt({0.0f, 0.0f, 0.0f}, {3.0f, 3.0f, 3.0f}, {0.0f, 1.0f, 0.0f});
auto img2 = app.RenderToImage(*renderer2, scene2->GetView(), scene2->GetScene(), width, height);
delete scene2;
delete renderer2;
}
app.OnTerminate();
}
}
```
### Error message
FEngine (64 bits) created at 0000021032E3B080 (threading is enabled)
FEngine resolved backend: OpenGL
Intel, Intel(R) Iris(R) Xe Graphics, 4.1.0 - Build 32.0.101.6078, 4.10 - Build 32.0.101.6078
OS version: 0
Renderer: Commands High watermark 7 KiB (0%), 224 commands, 32 bytes/command
Renderer: Commands High watermark 0 KiB (0%), 0 commands, 32 bytes/command
OpenGL error 0x502 (GL_INVALID_OPERATION) in "bool __cdecl filament::TimerQueryNative::queryResultAvailable(struct filament::OpenGLDriver::GLTimerQuery *)" at line 58
### Expected behavior
The example should work - no OpenGL error or exception.
### Open3D, Python and System information
```markdown
- Operating system: Windows 11, Visual Studio 2022, MSVC 14.42
- Open3D version: 0.19 (from yesterday)
- System architecture: x64
- Is this a remote workstation?: no
- How did you install Open3D?: Prebuilt release zip for Windows from this GitHub releases section
- Compiler version (if built from source): MSVC 14.42
```
### Additional information
_No response_
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.