# Why rendering is slow in GenericRenderWindow if we re-use it

**URL:** https://discourse.vtk.org/t/why-rendering-is-slow-in-genericrenderwindow-if-we-re-use-it/10731
**Category:** Web
**Created:** [February 20, 2023, 10:55am UTC](https://discourse.vtk.org/t/why-rendering-is-slow-in-genericrenderwindow-if-we-re-use-it/10731 "2023-02-20T10:55:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dexter](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/d/dc4da7/32.png) [@dexter](https://discourse.vtk.org/u/dexter)
#### Post date: [February 20, 2023, 10:55am UTC](https://discourse.vtk.org/t/why-rendering-is-slow-in-genericrenderwindow-if-we-re-use-it/10731/1 "2023-02-20T10:55:41Z")

</div>

In my application I did two tests to prouve what I’m saying:

**1 case:**  
In each time I want to render a new 3D surface actor, I delete the whole `vtkGenericRenderWindow` and the previous actor / mapper, and I re-create a new ones, doing this the rendering is good but I’m risking to have `Too many open WebGL contexts` warning.

**2 case:**  
In each time I want to render a new 3D surface actor, I re-use the same `vtkGenericRenderWindow` and I delete the previous `actor/ mapper` and I create new `actor/ mapper`, doing this the rendering is slow, so when I move the 3D surface with the mouse it is moving so slow.

---

<div class="post-metadata">

### Author: ![Forrest](https://discourse.vtk.org/user_avatar/discourse.vtk.org/forrest/32/300_2.png) [@Forrest](https://discourse.vtk.org/u/Forrest)
#### Post date: [February 20, 2023, 4:07pm UTC](https://discourse.vtk.org/t/why-rendering-is-slow-in-genericrenderwindow-if-we-re-use-it/10731/2 "2023-02-20T16:07:41Z")

</div>

The second case shouldn’t necessarily be slow, unless your data is very, very large. You typically never want to do #1.

---

<div class="post-metadata">

### Author: ![dexter](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/d/dc4da7/32.png) [@dexter](https://discourse.vtk.org/u/dexter)
#### Post date: [February 21, 2023, 9:06am UTC](https://discourse.vtk.org/t/why-rendering-is-slow-in-genericrenderwindow-if-we-re-use-it/10731/3 "2023-02-21T09:06:46Z")

</div>

Thank you for your answer,  
Yes I can say that my data is very large, each polydata (used by the 3D surface actors) size is 30MB and I have 33 patients,

![[video-to-gif output image]](https://discourse.vtk.org/uploads/default/original/2X/9/9cd92558474e30baee8755055fc114c0879c0c3a.gif)

This how I’m doing it in my code:

PS: `context.current[3].GLWindow` is the reference holding my GenericRenderWindow

```auto
const brainRender = async () => {
    
    if (context.current[3]) {
        const { actor, mapper, } = context.current[3];
          
        context.current[3].GLWindow.getRenderer().removeAllActors();
  
        if(actor){
          actor.delete()
        }
        if(mapper){
          mapper.delete()
        }
      }
    
    let pd, GLWindow, actor, mapper;
    mapper = vtkMapper.newInstance();
    actor = vtkActor.newInstance();
    GLWindow = context.current[3].GLWindow;

    const reader1 = vtkPolyDataReader.newInstance();
    await reader1
    .setUrl(path)
    .then(async () => {
        pd = reader1.getOutputData();
    })
    .catch((error) => {
        console.log(error);
    });

    const originBrain = origin.current;

    actor.setMapper(mapper);
    actor.setPosition(originBrain[0], originBrain[1], originBrain[2]);

    mapper.setInputData(pd);
    GLWindow.getRenderer().addActor(actor);
    GLWindow.getRenderer().resetCamera();
    GLWindow.getRenderer().getActiveCamera().zoom(1.3);
    GLWindow.getRenderer().getActiveCamera().pitch(2.3);
    GLWindow.getRenderWindow().render();

    const obj = {
      actor: actor,
      mapper: mapper,
      GLWindow: GLWindow,
      pd: pd,
    };

    context.current[3] = { ...obj };
  };

```

Doing it like this and after displaying 3 or 4 patients, manipulating the 3D surface with the mouse becomes so slow:

![[video-to-gif output image]](https://discourse.vtk.org/uploads/default/original/2X/e/e67d7142abdcb0a34b500aad847730e56eacc81a.gif)

---

<div class="post-metadata">

### Author: ![Forrest](https://discourse.vtk.org/user_avatar/discourse.vtk.org/forrest/32/300_2.png) [@Forrest](https://discourse.vtk.org/u/Forrest)
#### Post date: [February 21, 2023, 3:34pm UTC](https://discourse.vtk.org/t/why-rendering-is-slow-in-genericrenderwindow-if-we-re-use-it/10731/4 "2023-02-21T15:34:43Z")

</div>

Can you make a devtools performance profile of the slow 3D surface manipulation? I don’t suppose that you have all 33 polydatas visible in the scene, since it just looks like one. But it shouldn’t be that slow.

---

<div class="post-metadata">

### Author: ![dexter](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/d/dc4da7/32.png) [@dexter](https://discourse.vtk.org/u/dexter)
#### Post date: [February 23, 2023, 8:30am UTC](https://discourse.vtk.org/t/why-rendering-is-slow-in-genericrenderwindow-if-we-re-use-it/10731/5 "2023-02-23T08:30:01Z")

</div>

Thank you for your answer, I found the problem,

In each time I’m calling my onClick event I initialize the GLWindow variable with new `vtkGenericRenderWindow`

```auto
let GLWindow = vtkGenericRenderWindow.newInstance();
if (context.current){
GLWindow = context.current[3].GLWindow
}
//...

```

So I just removed the initialisation and it works fine!

```auto
let GLWindow;
if (context.current){
GLWindow = context.current[3].GLWindow
}
//...

```
