~unity-team/nux/nux.redirected-views

« back to all changes in this revision

Viewing changes to Nux/ClientArea.cpp

  • Committer: Tarmac
  • Author(s): Jay Taoko
  • Date: 2012-09-18 14:21:37 UTC
  • mfrom: (660.1.2 nux.dash-to-preview)
  • Revision ID: tarmac-20120918142137-uvz785kmnt2s40qy
This branch introduces "Redirected Views" in Nux. Redirected views allows a view to be rendered inside its own texture and that texture is latter composited inside the main rendering.
This branch contains the required changes for redirected views. The opportunty was also taken to fix minor issues and introduce API changes.

== Core of the Redirected View
  Nux/Area.cpp
  Nux/Area.h
  Nux/Layout.cpp
  Nux/Layout.h
  Nux/View.cpp
  Nux/View.h

== Fixed ClientArea to work with RedirectedViews
  Nux/ClientArea.cpp
  Nux/ClientArea.h

== Concrete implementation of a view to replace InputArea in some locations
  Nux/BasicView.cpp
  Nux/BasicView.h

== Nux.h contains the Feature.h file of Nux
  Nux/Nux.h

== API change to frame buffer object architecture
  NuxGraphics/IOpenGLFrameBufferObject.cpp
  NuxGraphics/IOpenGLFrameBufferObject.h

== Added InitSlTexturePremultiplyShader shader
  NuxGraphics/RenderingPipeGLSL.cpp
  NuxGraphics/GraphicsEngine.cpp
  NuxGraphics/GraphicsEngine.h
  NuxGraphics/RenderingPipeAsm.cpp

== Fixed texture inversion
  NuxGraphics/RenderingPipe.cpp. Fixes: https://bugs.launchpad.net/bugs/1049593. Approved by Neil J. Patel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
    mouse_drag.connect(sigc::mem_fun(this, &ClientArea::RecvMouseDrag));
47
47
    mouse_move.connect(sigc::mem_fun(this, &ClientArea::RecvMouseMove));
48
48
    key_down.connect(sigc::mem_fun(this, &ClientArea::RecvKeyEvent));
49
 
 
50
 
    if (GetWindowThread()->GetGraphicsDisplay().HasFrameBufferSupport())
51
 
    {
52
 
      m_FrameBufferObject = GetGraphicsDisplay()->GetGpuDevice()->CreateFrameBufferObject();
53
 
      m_MainColorRT = GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableDeviceTexture(2, 2, 1, BITFMT_R8G8B8A8, NUX_TRACKER_LOCATION);
54
 
      m_MainDepthRT = GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableDeviceTexture(2, 2, 1, BITFMT_D24S8, NUX_TRACKER_LOCATION);
55
 
    }
56
49
  }
57
50
 
58
51
  ClientArea::~ClientArea()
59
52
  {
60
53
  }
61
54
 
62
 
  void ClientArea::BeginDraw(GraphicsEngine &graphics_engine, bool force_draw)
 
55
  void ClientArea::BeginDraw(GraphicsEngine& graphics_engine, bool force_draw)
63
56
  {
64
 
    if ((IsRedrawNeeded() == false) && (force_draw == false))
65
 
      return;
 
57
    // if ((IsRedrawNeeded() == false) && (force_draw == false))
 
58
    //   return;
 
59
 
 
60
    // Save blend states
 
61
    unsigned int current_alpha_blend;
 
62
    unsigned int current_src_blend_factor;
 
63
    unsigned int current_dest_blend_factor;
 
64
    graphics_engine.GetRenderStates().GetBlend(current_alpha_blend, current_src_blend_factor, current_dest_blend_factor);
 
65
 
 
66
    ObjectPtr<IOpenGLFrameBufferObject> prev_fbo_ = GetGraphicsDisplay()->GetGpuDevice()->GetCurrentFrameBufferObject();
 
67
    Geometry prev_viewport_ = graphics_engine.GetViewportRect();
66
68
 
67
69
    if (GetWindowThread()->GetGraphicsDisplay().HasFrameBufferSupport())
68
70
    {
69
 
      int buffer_width = GetBaseWidth();
70
 
      int buffer_height = GetBaseHeight();
 
71
      int width = GetWidth();
 
72
      int height = GetHeight();
71
73
      int window_width, window_height;
72
 
      window_width = graphics_engine.GetViewportWidth();
73
 
      window_height = graphics_engine.GetViewportHeight();
 
74
      window_width = prev_viewport_.width;
 
75
      window_height = prev_viewport_.height;
74
76
 
75
 
      m_ctx.x = GetBaseX();
76
 
      m_ctx.y = GetBaseY();
77
 
      m_ctx.width  = GetBaseWidth();
78
 
      m_ctx.height = GetBaseHeight();
 
77
      m_ctx.x = GetX();
 
78
      m_ctx.y = GetY();
 
79
      m_ctx.width  = width;
 
80
      m_ctx.height = height;
79
81
 
80
82
      // A is obtained from graphics_engine. So A dimension's are in relative window coordinates.
81
83
      Rect A = graphics_engine.GetClippingRegion();
87
89
      m_ctx.width_clipregion  = C.GetWidth();
88
90
      m_ctx.height_clipregion = C.GetHeight();
89
91
 
90
 
      ObjectPtr<IOpenGLFrameBufferObject> prevFBO = GetGraphicsDisplay()->GetGpuDevice()->GetCurrentFrameBufferObject();
91
 
 
92
 
      if ((m_FrameBufferObject->GetWidth() != buffer_width) || (m_FrameBufferObject->GetHeight() != buffer_height))
93
 
      {
94
 
        m_FrameBufferObject->FormatFrameBufferObject(buffer_width, buffer_height, BITFMT_R8G8B8A8);
95
 
        m_MainColorRT = GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableDeviceTexture(buffer_width, buffer_height, 1, BITFMT_R8G8B8A8, NUX_TRACKER_LOCATION);
96
 
        m_MainDepthRT = GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableDeviceTexture(buffer_width, buffer_height, 1, BITFMT_D24S8, NUX_TRACKER_LOCATION);
97
 
      }
98
 
 
99
 
      m_FrameBufferObject->SetRenderTarget(0, m_MainColorRT->GetSurfaceLevel(0));
100
 
      m_FrameBufferObject->SetDepthSurface(m_MainDepthRT->GetSurfaceLevel(0));
 
92
      //ObjectPtr<IOpenGLFrameBufferObject> prevFBO = GetGraphicsDisplay()->GetGpuDevice()->GetCurrentFrameBufferObject();
 
93
 
 
94
      if (m_FrameBufferObject.IsNull())
 
95
      {
 
96
        // Create the fbo before using it for the first time.
 
97
        m_FrameBufferObject = GetGraphicsDisplay()->GetGpuDevice()->CreateFrameBufferObject();
 
98
      }
 
99
 
 
100
      if (!m_MainColorRT.IsValid() || (m_MainColorRT->GetWidth() != width) || (m_MainColorRT->GetHeight() != height))
 
101
      {
 
102
        // Create or resize the color and depth textures before using them.
 
103
        m_MainColorRT = GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableDeviceTexture(width, height, 1, BITFMT_R8G8B8A8, NUX_TRACKER_LOCATION);
 
104
        m_MainDepthRT = GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableDeviceTexture(width, height, 1, BITFMT_D24S8, NUX_TRACKER_LOCATION);
 
105
      }
 
106
 
 
107
      m_FrameBufferObject->FormatFrameBufferObject(width, height, BITFMT_R8G8B8A8);
 
108
      m_FrameBufferObject->EmptyClippingRegion();
 
109
      m_FrameBufferObject->SetTextureAttachment(0, m_MainColorRT, 0);
 
110
      m_FrameBufferObject->SetDepthTextureAttachment(m_MainDepthRT, 0);
101
111
      m_FrameBufferObject->Activate();
102
112
 
103
 
      graphics_engine.SetViewport(0, 0, buffer_width, buffer_height);
104
 
      m_FrameBufferObject->EmptyClippingRegion();
105
 
 
106
 
      ClientDraw(graphics_engine, m_ctx, force_draw);
107
 
 
108
 
      // Restore the main frame buffer object
109
 
      prevFBO->Activate();
110
 
 
111
 
      Area* view_window = GetTopLevelViewWindow();
112
 
      if (view_window)
113
 
      {
114
 
        graphics_engine.SetViewport(0, 0, view_window->GetBaseWidth(), view_window->GetBaseHeight());
115
 
        graphics_engine.ApplyClippingRectangle();
116
 
        graphics_engine.ApplyModelViewMatrix();
117
 
        graphics_engine.SetOrthographicProjectionMatrix(view_window->GetBaseWidth(), view_window->GetBaseHeight());
118
 
 
119
 
      }
120
 
      else
121
 
      {
122
 
        graphics_engine.SetViewport(0, 0, window_width, window_height);
123
 
        graphics_engine.ApplyClippingRectangle();
124
 
        graphics_engine.ApplyModelViewMatrix();
125
 
        graphics_engine.SetOrthographicProjectionMatrix(window_width, window_height);
126
 
      }
127
 
            
128
 
      // Copy the client frame buffer into the main frame buffer.
129
 
      {
130
 
        unsigned int w, h;
131
 
        w = m_MainColorRT->GetWidth();
132
 
        h = m_MainColorRT->GetHeight();
133
 
        int x = m_ctx.x;
134
 
        int y = m_ctx.y;
135
 
 
136
 
        TexCoordXForm texxform0;
137
 
        texxform0.uwrap = TEXWRAP_CLAMP;
138
 
        texxform0.vwrap = TEXWRAP_CLAMP;
139
 
        texxform0.FlipVCoord(true);
140
 
        GetGraphicsDisplay()->GetGraphicsEngine()->QRP_1Tex(x, y, w, h, m_MainColorRT, texxform0, Color(color::White));
141
 
      }
142
 
    }
143
 
    else
144
 
    {
145
 
      int x = graphics_engine.GetContextX();
146
 
      int y = graphics_engine.GetContextY();
147
 
 
148
 
      // The clientarea is in absolute window coordinates. It needs to be offset so that it is in relative window coordinates.
149
 
      m_ctx.x = GetBaseX() + x;
150
 
      m_ctx.y = GetBaseY() + y;
151
 
      m_ctx.width  = GetBaseWidth();
152
 
      m_ctx.height = GetBaseHeight();
153
 
 
154
 
      // A is obtained from graphics_engine. So A dimension's are in relative window coordinates.
155
 
      Rect A = graphics_engine.GetClippingRegion();
156
 
 
157
 
      Rect B = Rect(m_ctx.x, m_ctx.y, m_ctx.width, m_ctx.height);
158
 
      Rect C = A.Intersect(B);
159
 
 
160
 
      m_ctx.x_clipregion = C.x;
161
 
      m_ctx.y_clipregion = C.y;
162
 
      m_ctx.width_clipregion  = C.GetWidth();
163
 
      m_ctx.height_clipregion = C.GetHeight();
164
 
 
165
 
      int window_width, window_height;
166
 
      window_width = graphics_engine.GetViewportWidth();
167
 
      window_height = graphics_engine.GetViewportHeight();
168
 
 
169
 
      SetClientViewport(graphics_engine);
170
 
//         graphics_engine.SetViewport(
171
 
//             m_ctx.x, window_height - m_ctx.y - m_ctx.height, m_ctx.width, m_ctx.height);
172
 
//
173
 
//         graphics_engine.SetOpenGLClippingRectangle(
174
 
//             m_ctx.x_clipregion,
175
 
//             window_height - m_ctx.y_clipregion - m_ctx.height_clipregion,
176
 
//             m_ctx.width_clipregion,
177
 
//             m_ctx.height_clipregion);
178
 
 
179
 
      ClientDraw(graphics_engine, m_ctx, force_draw);
180
 
 
181
 
      // go back to 2D in case that was changed by the client.
182
 
      graphics_engine.SetViewport(0, 0, window_width, window_height);
183
 
      graphics_engine.ApplyClippingRectangle();
184
 
      graphics_engine.Push2DWindow(window_width, window_height);
185
 
    }
 
113
      graphics_engine.SetViewport(0, 0, width, height);
 
114
 
 
115
 
 
116
      ClientDraw(graphics_engine, m_ctx, force_draw);
 
117
    }
 
118
 
 
119
    if (prev_fbo_.IsValid())
 
120
    {
 
121
      // Restore the previous fbo
 
122
      prev_fbo_->Activate();
 
123
 
 
124
      prev_fbo_->ApplyClippingRegion();
 
125
    }
 
126
 
 
127
    // Restore the matrices and the view port.
 
128
    graphics_engine.ApplyModelViewMatrix();
 
129
    graphics_engine.SetOrthographicProjectionMatrix(prev_viewport_.width, prev_viewport_.height);
 
130
    graphics_engine.SetViewport(prev_viewport_.x, prev_viewport_.y, prev_viewport_.width, prev_viewport_.height);    
 
131
 
 
132
    {
 
133
      unsigned int w, h;
 
134
      w = m_MainColorRT->GetWidth();
 
135
      h = m_MainColorRT->GetHeight();
 
136
      int x = m_ctx.x;
 
137
      int y = m_ctx.y;
 
138
 
 
139
      TexCoordXForm texxform0;
 
140
      texxform0.uwrap = TEXWRAP_CLAMP;
 
141
      texxform0.vwrap = TEXWRAP_CLAMP;
 
142
      texxform0.FlipVCoord(true);
 
143
      GetGraphicsDisplay()->GetGraphicsEngine()->QRP_1Tex(x, y, w, h, m_MainColorRT, texxform0, Color(color::White));
 
144
    }
 
145
 
 
146
    // restore blend states
 
147
    graphics_engine.GetRenderStates().SetBlend(current_alpha_blend, current_src_blend_factor, current_dest_blend_factor);
186
148
  }
187
149
 
188
150
  void ClientArea::Draw(GraphicsEngine &graphics_engine, bool force_draw)
270
232
 
271
233
  }
272
234
 
273
 
  void ClientArea::QueueDraw()
274
 
  {
275
 
    //GetWindowCompositor()..AddToDrawList(this);
276
 
    WindowThread* application = GetWindowThread();
277
 
    if (application)
278
 
    {
279
 
      application->AddToDrawList(this);
280
 
      application->RequestRedraw();
281
 
      //GetWindowCompositor().AddToDrawList(this);
282
 
    }
283
 
    draw_cmd_queued_ = true;
284
 
  }
285
 
 
286
235
  bool ClientArea::AcceptKeyNavFocus()
287
236
  {
288
237
    return false;