# Rendering Only Updated Content

#### <a class="anchor" id="bkmrk--6"></a>Description

This chapter briefly describes how to achieve performance optimization by rendering only updated content.

#### **Introduction**

##### <a class="anchor" id="bkmrk--8"></a>Invalidation Introduction

Basic principles for composing graphical content:

<div class="contents" id="bkmrk-place-content-which-"><div class="contents"><div class="textblock">- Place content which is never changed in an own scene and render it to an own layer.
- Partition content which is frequently changed by widgets or animations. Use several cameras for this purpose.
- If rarely modified graphical content is part of a dynamic scenery then think about using <span style="color: rgb(230, 126, 35);">[Texture Render Targets](https://doc316en.candera.eu/books/candera/page/multiple-render-targets)</span>.

</div></div></div>##### <a class="anchor" id="bkmrk--9"></a>How is it done?

In the render loop of the application all Widgets are updated by calling [Candera::WidgetBase::Update()](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_widget_base.html#ad7891efa29f51b34e2566ff4804c59e3). The basic render loop looks like this:

```
  // 3D Render Loop

    // handle input events
    // ...

    // update animations by calling AnimationDispatcher::DispatchTime()
    if (m_animationDispatcher3D != 0) {
      m_animationDispatcher3D->SetWorldTimeMs(nowMs);
      m_animationDispatcher3D->DispatchTime();
    }

    // update all widgets
    CgiApp::GetInstance().Update();

    Renderer::RenderAllCameras();

  // end 3D Render Loop
```

The widgets must now implement a mechanism to tell the application whether associated graphical content needs to be rendered. The following pages explain two ways to achieve this:

<div class="contents" id="bkmrk-use-candera%3A%3Acamera%3A"><div class="textblock">- <span style="color: rgb(230, 126, 35);">[Use Candera::Camera::SetRenderingEnabled() / Candera::Camera2D::SetRenderingEnabled()](https://doc316en.candera.eu/link/375#bkmrk-use-setrenderingenab)</span>
- <span style="color: rgb(230, 126, 35);">[Explicitely Pick Camera to Render](https://doc316en.candera.eu/link/375#bkmrk-pick-camera-to-rende)</span>

</div></div>####   
**Use SetRenderingEnabled()**   


##### <a class="anchor" id="bkmrk--10"></a>Use Candera::Camera::SetRenderingEnabled() / Candera::Camera2D::SetRenderingEnabled()

The example below shows how in CGIApplication all 3D cameras are turned off in first step of render loop.

```
void CgiApp3dGc::Update() {
    Base::Update();
   
  // Turn off all cameras
    UInt32 cameraCount = Renderer::GetCameraCount();
    for (UInt32 i = 0; i < cameraCount; i++)
    {
        Camera* cam = Renderer::GetCamera(i);
        cam->SetRenderingEnabled(false);
    }

    // Update widgets in all visible scenes
    WidgetBase* widget = 0;
    Vector<SceneContext*>::ConstIterator it = m_sceneContexts.ConstBegin();
    for (; it != m_sceneContexts.ConstEnd(); it++) {
        if ((*it) != 0 && (*it)->GetScene() != 0 && (*it)->GetScene()->IsRenderingEnabled()) {
            widget = (*it)->GetFirstWidget();
            while (widget != 0) {
                widget->Update();
                widget = (*it)->GetNextWidget();
            }
        }
    }
}
```

The [Candera::Widget::Update()](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_widget_base.html#ad7891efa29f51b34e2566ff4804c59e3) method needs to be implemented such that the recently turned off camera is turned on again whenever rendering is required. The [Candera::Camera](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_camera.html "A Camera provides Modelview and Projection matrices. Camera is a Transformable.* The eye point is set...") can be either set as a widget property or retrieved inside the scene by masking the widget and the associated camera with the same [Candera::ScopeMask](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_scope_mask.html "The class ScopeMask allows scene graph nodes to form conceptual groups independent of the scene graph...") value.

```
void SampleBaseWidget3D::InvalidateCamera(bool value)
{
  m_camera->SetRenderingEnabled(true);
}
```

```
void MyWidget::Update()
{
  // update graphical content from cached property value(s)
  //...

  Base::InvalidateCamera(true);
}
```

#### **Pick Camera to Render**

##### Explicitly Pick Camera to Render

Another method is to maintain a list of cameras by the application.

The render loop has to be changed

<div class="contents" id="bkmrk-to-call-candera%3A%3Aren"><div class="textblock">- to call Candera::Renderer::RenderCamera( Camera \* camera )
- instead of [Candera::Renderer::RenderAllCameras()](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_renderer.html#aed822ba1d66620f2d1e794973e30f354).

</div></div><div class="contents" id="bkmrk-the-application-need"><div class="textblock"><dl class="note"><dd><p class="callout info">The application needs to take care on <span style="color: rgb(230, 126, 35);">[Render Buffer Swapping](https://doc316en.candera.eu/link/1050#bkmrk-render-buffer-swappi)</span>.</p>

</dd></dl></div></div>