Added a profiler, which outputs time taken and calls to various functions.
[ipdf/code.git] / src / view.cpp
1 #include "view.h"
2 #include "bufferbuilder.h"
3 #include "screen.h"
4 #include "profiler.h"
5 #include "gl_core44.h"
6
7 #ifndef CONTROLPANEL_DISABLED
8         #include "controlpanel.h"
9 #endif //CONTROLPANEL_DISABLED
10
11
12 #ifdef TRANSFORM_BEZIERS_TO_PATH 
13         #ifndef TRANSFORM_OBJECTS_NOT_VIEW
14         //#error Cannot TRANSFORM_BEZIERS_TO_PATH _without_ TRANSFORM_OBJECTS_NOT_VIEW
15         #endif
16 #endif
17
18 using namespace IPDF;
19 using namespace std;
20
21 /**
22  * Constructs a view
23  * Allocates memory for ObjectRenderers
24  * @param document - The document to associate the View with
25  * @param bounds - Initial bounds of the View
26  * @param colour - Colour to use for rendering this view. TODO: Make sure this actually works, or just remove it
27  */
28 View::View(Document & document, Screen & screen, const VRect & bounds, const Colour & colour)
29         : m_use_gpu_transform(false), m_use_gpu_rendering(USE_GPU_RENDERING), m_bounds_dirty(true), m_buffer_dirty(true), 
30                 m_render_dirty(true), m_document(document), m_screen(screen), m_cached_display(), m_bounds(bounds), m_colour(colour), m_bounds_ubo(), 
31                 m_objbounds_vbo(), m_object_renderers(NUMBER_OF_OBJECT_TYPES), m_cpu_rendering_pixels(NULL),
32                 m_perform_shading(USE_SHADING), m_show_bezier_bounds(false), m_show_bezier_type(false),
33                 m_show_fill_points(false), m_show_fill_bounds(false), m_lazy_rendering(true),
34                 m_query_gpu_bounds_on_next_frame(NULL)
35 {
36         Debug("View Created - Bounds => {%s}", m_bounds.Str().c_str());
37
38         screen.SetView(this); // oh dear...
39
40         
41
42         // Create ObjectRenderers - new's match delete's in View::~View
43         //TODO: Don't forget to put new renderers here or things will be segfaultastic
44         if (screen.Valid())
45         {
46                 m_object_renderers[RECT_FILLED] = new RectFilledRenderer();
47                 m_object_renderers[RECT_OUTLINE] = new RectOutlineRenderer();
48                 m_object_renderers[CIRCLE_FILLED] = new CircleFilledRenderer();
49                 m_object_renderers[BEZIER] = new BezierRenderer();
50                 m_object_renderers[PATH] = new PathRenderer();
51         }
52         else
53         {
54                 for (int i = RECT_FILLED; i <= PATH; ++i)
55                         m_object_renderers[i] = new FakeRenderer();
56         }
57
58         // To add rendering for a new type of object;
59         // 1. Add enum to ObjectType in ipdf.h
60         // 2. Implement class inheriting from ObjectRenderer using that type in objectrenderer.h and objectrenderer.cpp
61         // 3. Add it here
62         // 4. Profit
63
64
65 #ifndef QUADTREE_DISABLED
66         m_quadtree_max_depth = 2;
67         m_current_quadtree_node = document.GetQuadTree().root_id;
68 #endif
69 }
70
71 /**
72  * Destroy a view
73  * Frees memory used by ObjectRenderers
74  */
75 View::~View()
76 {
77         for (unsigned i = 0; i < m_object_renderers.size(); ++i)
78         {
79                 delete m_object_renderers[i]; // delete's match new's in constructor
80         }
81         m_object_renderers.clear();
82         delete [] m_cpu_rendering_pixels;
83 }
84
85 /**
86  * Translate the view
87  * @param x, y - Amount to translate
88  */
89 void View::Translate(Real x, Real y)
90 {
91         PROFILE_SCOPE("View::Translate");       
92         if (!m_use_gpu_transform)
93                 m_buffer_dirty = true;
94         m_bounds_dirty = true;
95         #ifdef TRANSFORM_OBJECTS_NOT_VIEW
96         ObjectType type = NUMBER_OF_OBJECT_TYPES;
97                 #ifdef TRANSFORM_BEZIERS_TO_PATH
98                         type = PATH;
99                 #endif
100         m_document.TranslateObjects(-x, -y, type);
101         #endif
102         m_bounds.x += m_bounds.w*VReal(x);
103         m_bounds.y += m_bounds.h*VReal(y);
104         //Debug("View Bounds => %s", m_bounds.Str().c_str());
105
106         
107 }
108
109 /**
110  * Set View bounds
111  * @param bounds - New bounds
112  */
113 void View::SetBounds(const Rect & bounds)
114 {
115         #ifdef TRANSFORM_OBJECTS_NOT_VIEW
116         ObjectType type = NUMBER_OF_OBJECT_TYPES;
117         #ifdef TRANSFORM_BEZIERS_TO_PATH
118                 type = PATH;
119         #endif
120         SVGMatrix transform = {Real(m_bounds.w)/bounds.w, 0, Real(m_bounds.x) - bounds.x, 0,Real(m_bounds.h)/bounds.h, Real(m_bounds.y) - bounds.y};
121         m_document.TransformObjectBounds(transform, type);
122         #endif
123         m_bounds.x = bounds.x;
124         m_bounds.y = bounds.y;
125         m_bounds.w = bounds.w;
126         m_bounds.h = bounds.h;
127         if (!m_use_gpu_transform)
128                 m_buffer_dirty = true;
129         m_bounds_dirty = true;
130 }
131
132 /**
133  * Scale the View at a point
134  * @param x, y - Coordinates to scale at (eg: Mouse cursor position)
135  * @param scale_amount - Amount to scale by
136  */
137 void View::ScaleAroundPoint(Real x, Real y, Real scale_amount)
138 {
139         PROFILE_SCOPE("View::ScaleAroundPoint");        
140         // (x0, y0, w, h) -> (x*w - (x*w - x0)*s, y*h - (y*h - y0)*s, w*s, h*s)
141         // x and y are coordinates in the window
142         // Convert to local coords.
143         if (!m_use_gpu_transform)
144                 m_buffer_dirty = true;
145         m_bounds_dirty = true;
146         
147         
148         #ifdef TRANSFORM_OBJECTS_NOT_VIEW
149         ObjectType type = NUMBER_OF_OBJECT_TYPES;
150         #ifdef TRANSFORM_BEZIERS_TO_PATH
151                 type = PATH;
152         #endif
153         m_document.ScaleObjectsAboutPoint(x, y, scale_amount, type);
154         #endif
155         VReal vx = m_bounds.w * VReal(x);
156         VReal vy = m_bounds.h * VReal(y);
157         vx += m_bounds.x;
158         vy += m_bounds.y;
159         
160         VReal top = vy - m_bounds.y;
161         VReal left = vx - m_bounds.x;
162         
163         top *= scale_amount;
164         left *= scale_amount;
165         
166         m_bounds.x = vx - left;
167         m_bounds.y = vy - top;
168         m_bounds.w *= scale_amount;
169         m_bounds.h *= scale_amount;
170         //Debug("Scale at {%s, %s} by %s View Bounds => %s", x.Str().c_str(), y.Str().c_str(), scale_amount.Str().c_str(), m_bounds.Str().c_str());
171         
172         
173 }
174
175 /**
176  * Transform a point in the document to a point relative to the top left corner of the view
177  * This is the CPU coordinate transform code; used only if the CPU is doing coordinate transforms
178  * @param inp - Input Rect {x,y,w,h} in the document
179  * @returns output Rect {x,y,w,h} in the View
180  */
181 Rect View::TransformToViewCoords(const Rect& inp) const
182 {
183         #ifdef TRANSFORM_OBJECTS_NOT_VIEW
184                 return inp;
185         #endif
186         return TransformRectCoordinates(m_bounds.Convert<Real>(), inp);
187 }
188
189 /**
190  * Render the view
191  * Updates FrameBuffer if the document, object bounds, or view bounds have changed, then Blits it
192  * Otherwise just Blits the cached FrameBuffer
193  * @param width - Width of View to render
194  * @param height - Height of View to render
195  */
196 void View::Render(int width, int height)
197 {
198         PROFILE_SCOPE("View::Render()");
199         if (!m_screen.Valid()) return;
200         glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION,42,-1, "Beginning View::Render()");
201         // View dimensions have changed (ie: Window was resized)
202         int prev_width = m_cached_display.GetWidth();
203         int prev_height = m_cached_display.GetHeight();
204         if (width != prev_width || height != prev_height)
205         {
206                 m_cached_display.Create(width, height);
207                 m_bounds_dirty = true;
208         }
209
210         // View bounds have not changed; blit the FrameBuffer as it is
211         if (!m_bounds_dirty && m_lazy_rendering)
212         {
213                 m_cached_display.UnBind();
214                 m_cached_display.Blit();
215                 glPopDebugGroup();
216                 return;
217         }
218         m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
219         m_cached_display.Clear();
220
221 #ifndef QUADTREE_DISABLED
222         // I'm going to write this out in comments, so hopefully then I'll understand it. :/
223         //
224         // This code looks at the current bounds and tries to work out how they need to change
225         // to keep the view looking at the correct quadtree node.
226         //
227         // The idea is that the width/height of the view bounds are always 0.5<=wh<=1.0. We then always
228         // try to keep the bottom-right corner of the node on-screen, changing nodes to suit. Why bottom-right,
229         // you may ask. It's an excellent question, with a dubious, hand-wavey answer: because we're manipulating
230         // the bounds, it was easier to do it that way. (The top-left corner of the bounds are within the main
231         // quadtree node).
232         if (m_bounds_dirty || !m_lazy_rendering)
233         {
234                 g_profiler.BeginZone("View::Render -- Quadtree view bounds management");
235                 // If we're too far zoomed out, become the parent of the current node.
236                 if ( m_bounds.w > 1.0 || m_bounds.h > 1.0)
237                 {
238                         // If a parent node exists, we'll become it.
239                         //TODO: Generate a new parent node if none exists, and work out when to change child_type
240                         // away from QTC_UNKNOWN
241                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
242                         {
243                                 m_bounds = TransformFromQuadChild(m_bounds, m_document.GetQuadTree().nodes[m_current_quadtree_node].child_type);
244                                 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].parent;
245                         }
246                 }
247
248                 // If we have a parent... (This prevents some crashes, but should disappear.)
249                 if (m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
250                 {
251                         // If the current node is off the left-hand side of the screen...
252                         while (m_bounds.x > 1)
253                         {
254                                 //... the current node becomes the node to its right.
255                                 m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
256                                 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 1, 0, &m_document);
257                         }
258                         while (m_bounds.y > 1)
259                         {
260                                 m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
261                                 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, 1, &m_document);
262                         }
263                         while (m_bounds.x < 0)
264                         {
265                                 m_bounds = Rect(m_bounds.x + 1, m_bounds.y, m_bounds.w, m_bounds.h);
266                                 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, -1, 0, &m_document);
267                         }
268                         while (m_bounds.y < 0)
269                         {
270                                 m_bounds = Rect(m_bounds.x, m_bounds.y + 1, m_bounds.w, m_bounds.h);
271                                 m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, -1, &m_document);
272                         }
273                 }
274
275                 // Recurse into a node if we are completely within it. (If we're okay with having an invalid frame or two, we can remove this.)
276                 if (ContainedInQuadChild(m_bounds, QTC_TOP_LEFT))
277                 {
278                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left == QUADTREE_EMPTY)
279                         {
280                                 // We want to reparent into a child node, but none exist. Get the document to create one.
281                                 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_LEFT);
282                                 m_render_dirty = true;
283                         }
284                         m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_LEFT);
285                         m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left;
286                 }
287                 if (ContainedInQuadChild(m_bounds, QTC_TOP_RIGHT))
288                 {
289                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right == QUADTREE_EMPTY)
290                         {
291                                 // We want to reparent into a child node, but none exist. Get the document to create one.
292                                 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_RIGHT);
293                                 m_render_dirty = true;
294                         }
295                         m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_RIGHT);
296                         m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right;
297                 }
298                 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_LEFT))
299                 {
300                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left == QUADTREE_EMPTY)
301                         {
302                                 // We want to reparent into a child node, but none exist. Get the document to create one.
303                                 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_LEFT);
304                                 m_render_dirty = true;
305                         }
306                         m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_LEFT);
307                         m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left;
308                 }
309                 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_RIGHT))
310                 {
311                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right == QUADTREE_EMPTY)
312                         {
313                                 // We want to reparent into a child node, but none exist. Get the document to create one.
314                                 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_RIGHT);
315                                 m_render_dirty = true;
316                         }
317                         m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_RIGHT);
318                         m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right;
319                 }
320
321                 // Otherwise, we'll arbitrarily select the bottom-right.
322                 // TODO: Perhaps select based on greatest area?
323                 if (m_bounds.w < 0.5 || m_bounds.h < 0.5)
324                 {
325                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right == QUADTREE_EMPTY)
326                         {
327                                 // We want to reparent into a child node, but none exist. Get the document to create one.
328                                 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_RIGHT);
329                                 m_render_dirty = true;
330                         }
331                         m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_RIGHT);
332                         m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right;
333                 }
334                 g_profiler.EndZone();
335         }
336
337         m_screen.DebugFontPrintF("Current View QuadTree");
338         QuadTreeIndex overlay = m_current_quadtree_node;
339         while (overlay != -1)
340         {
341                 m_screen.DebugFontPrintF(" Node: %d (objs: %d -> %d)", overlay, m_document.GetQuadTree().nodes[overlay].object_begin,
342                                         m_document.GetQuadTree().nodes[overlay].object_end);
343                 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
344         }
345         m_screen.DebugFontPrintF("\n");
346         m_screen.DebugFontPrintF("Left: %d, Right: %d, Up: %d, Down: %d\n",
347                         m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, -1, 0, 0),
348                         m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 1, 0, 0),
349                         m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, -1, 0),
350                         m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, 1, 0));
351
352
353         Rect view_top_bounds = m_bounds;
354         QuadTreeIndex tmp = m_current_quadtree_node;
355         while (tmp != -1)
356         {
357                 view_top_bounds = TransformFromQuadChild(view_top_bounds, m_document.GetQuadTree().nodes[tmp].child_type);
358                 tmp = m_document.GetQuadTree().nodes[tmp].parent;
359         }
360         m_screen.DebugFontPrintF("Equivalent View Bounds: %s\n", view_top_bounds.Str().c_str());
361 #endif
362
363         if (!m_use_gpu_rendering)
364         {
365                 // Dynamically resize CPU rendering target pixels if needed
366                 if (m_cpu_rendering_pixels == NULL || width*height > prev_width*prev_height)
367                 {
368                         delete [] m_cpu_rendering_pixels;
369                         m_cpu_rendering_pixels = new uint8_t[width*height*4];
370                         if (m_cpu_rendering_pixels == NULL)
371                                 Fatal("Could not allocate %d*%d*4 = %d bytes for cpu rendered pixels", width, height, width*height*4);
372                 }
373                 // Clear CPU rendering pixels
374                 for (int i = 0; i < width*height*4; ++i)
375                         m_cpu_rendering_pixels[i] = 255;
376         }
377 #ifdef QUADTREE_DISABLED
378         RenderRange(width, height, 0, m_document.ObjectCount());
379 #else
380         RenderQuadtreeNode(width, height, m_current_quadtree_node, m_quadtree_max_depth);
381 #endif
382         if (!m_use_gpu_rendering)
383         {
384                 m_screen.RenderPixels(0,0,width, height, m_cpu_rendering_pixels); //TODO: Make this work :(
385                 // Debug for great victory (do something similar for GPU and compare?)
386                 //ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, width, height}, "cpu_rendering_last_frame.bmp");
387         }
388         m_cached_display.UnBind(); // resets render target to the screen
389         m_cached_display.Blit(); // blit FrameBuffer to screen
390         m_buffer_dirty = false;
391         glPopDebugGroup();
392         
393 #ifndef CONTROLPANEL_DISABLED
394         // The powers that be suggest that this may be causing of the segfaults.
395         //ControlPanel::Update();
396 #endif //CONTROLPANEL_DISABLED
397         //Debug("Completed Render");
398         
399 }
400
401 #ifndef QUADTREE_DISABLED
402 void View::RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth)
403 {
404         Rect old_bounds = m_bounds;
405         if (node == QUADTREE_EMPTY) return;
406         if (!remaining_depth) return;
407         //Debug("Rendering QT node %d, (objs: %d -- %d)\n", node, m_document.GetQuadTree().nodes[node].object_begin, m_document.GetQuadTree().nodes[node].object_end);
408         m_bounds_dirty = true;
409         m_render_dirty = m_buffer_dirty = true;
410         QuadTreeIndex overlay = node;
411         while(overlay != -1)
412         {
413                 RenderRange(width, height, m_document.GetQuadTree().nodes[overlay].object_begin, m_document.GetQuadTree().nodes[overlay].object_end);
414                 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
415         }
416
417         if (m_bounds.Intersects(Rect(1,1,1,1)))
418         {
419                 m_bounds = Rect(m_bounds.x - 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
420                 m_bounds_dirty = true;
421                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 1, &m_document), remaining_depth - 1);
422         }
423         m_bounds = old_bounds;
424         if (m_bounds.Intersects(Rect(1,0,1,1)))
425         {
426                 m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
427                 m_bounds_dirty = true;
428                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 0, &m_document), remaining_depth - 1);
429         }
430         m_bounds = old_bounds;
431         if (m_bounds.Intersects(Rect(0,1,1,1)))
432         {
433                 m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
434                 m_bounds_dirty = true;
435                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, 1, &m_document), remaining_depth - 1);
436         }
437         m_bounds = old_bounds;
438         m_bounds_dirty = true;
439
440 #if 0
441         m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_LEFT);
442         m_bounds_dirty = true;
443         RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_left, remaining_depth-1);
444         m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_RIGHT);
445         m_bounds_dirty = true;
446         RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_right, remaining_depth-1);
447         m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_LEFT);
448         m_bounds_dirty = true;
449         RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_left, remaining_depth-1);
450         m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_RIGHT);
451         m_bounds_dirty = true;
452         RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_right, remaining_depth-1);
453         m_bounds = old_bounds;
454         m_bounds_dirty = true;
455 #endif
456 }
457 #endif
458
459 void View::RenderRange(int width, int height, unsigned first_obj, unsigned last_obj)
460 {
461         PROFILE_SCOPE("View::RenderRange");
462         glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 43, -1, "View::RenderRange()");
463         if (m_render_dirty) // document has changed
464                 PrepareRender();
465
466         if (m_buffer_dirty || m_bounds_dirty || !m_lazy_rendering) // object bounds have changed
467         {
468                 if (m_use_gpu_rendering)
469                         UpdateObjBoundsVBO(first_obj, last_obj);
470         }
471
472         if (m_use_gpu_transform)
473         {
474                 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
475                         //Debug("Transform objects, not view");
476                                 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
477                                         0.0f, 0.0f, float(width), float(height)};
478                 #else
479                 GLfloat glbounds[] = {static_cast<GLfloat>(Float(m_bounds.x)), static_cast<GLfloat>(Float(m_bounds.y)), static_cast<GLfloat>(Float(m_bounds.w)), static_cast<GLfloat>(Float(m_bounds.h)),
480                                         0.0, 0.0, static_cast<GLfloat>(width), static_cast<GLfloat>(height)};
481                 #endif
482                 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
483         }
484         else
485         {
486                 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
487                                         0.0f, 0.0f, float(width), float(height)};
488                 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
489         }
490         m_bounds_dirty = false;
491
492
493         // Render using GPU
494         if (m_use_gpu_rendering) 
495         {
496                 if (m_colour.a < 1.0f)
497                 {
498                         glEnable(GL_BLEND);
499                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
500                 }
501                 m_objbounds_vbo.Bind();
502                 m_bounds_ubo.Bind();
503                 glEnableVertexAttribArray(0);
504                 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
505         
506                 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
507                 {
508                         m_object_renderers[i]->RenderUsingGPU(first_obj, last_obj);
509                 }
510                 
511                 glDisableVertexAttribArray(0);
512                 if (m_colour.a < 1.0f)
513                 {
514                         glDisable(GL_BLEND);
515                 }
516         }
517         else // Rasterise on CPU then blit texture to GPU
518         {
519
520                 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
521                 {
522                         m_object_renderers[i]->RenderUsingCPU(m_document.m_objects, *this, {m_cpu_rendering_pixels, width, height}, first_obj, last_obj);
523                 }
524         }
525         glPopDebugGroup();
526 }
527
528 void View::UpdateObjBoundsVBO(unsigned first_obj, unsigned last_obj)
529 {
530         PROFILE_SCOPE("View::UpdateObjBoundsVBO");
531         if (m_query_gpu_bounds_on_next_frame != NULL)
532         {
533                 fprintf(m_query_gpu_bounds_on_next_frame,"# View: %s\t%s\t%s\t%s", Str(m_bounds.x).c_str(), Str(m_bounds.y).c_str(), Str(m_bounds.w).c_str(), Str(m_bounds.h).c_str());
534         }       
535         
536         //m_objbounds_vbo.Invalidate();
537         m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
538         m_objbounds_vbo.SetName("Object Bounds VBO");
539         
540         #ifndef TRANSFORM_OBJECTS_NOT_VIEW
541         if (m_use_gpu_transform)
542         {
543                 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
544         }
545         else
546         #endif //TRANSFORM_OBJECTS_NOT_VIEW
547         {
548                 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicCopy);
549         }
550         m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
551
552         BufferBuilder<GPUObjBounds> obj_bounds_builder(m_objbounds_vbo.MapRange(first_obj*sizeof(GPUObjBounds), (last_obj-first_obj)*sizeof(GPUObjBounds), false, true, true), m_objbounds_vbo.GetSize());
553
554         #ifndef TRANSFORM_BEZIERS_TO_PATH
555         for (unsigned id = first_obj; id < last_obj; ++id)
556         {
557                 Rect obj_bounds;
558                 if (m_use_gpu_transform)
559                 {
560                         obj_bounds = m_document.m_objects.bounds[id];
561                 }
562                 else
563                 {
564                         obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
565                 }
566                 GPUObjBounds gpu_bounds = {
567                         Float(obj_bounds.x),
568                         Float(obj_bounds.y),
569                         Float(obj_bounds.x + obj_bounds.w),
570                         Float(obj_bounds.y + obj_bounds.h)
571                 };
572
573                 if (m_query_gpu_bounds_on_next_frame != NULL)
574                 {       
575                         fprintf(m_query_gpu_bounds_on_next_frame,"%d\t%f\t%f\t%f\t%f\n", id, Float(obj_bounds.x), Float(obj_bounds.y), Float(obj_bounds.w), Float(obj_bounds.h));
576                 }
577                 
578                 obj_bounds_builder.Add(gpu_bounds);
579         }
580         #else
581         for (unsigned i = 0; i < m_document.m_objects.paths.size(); ++i)
582         {
583                 Path & path = m_document.m_objects.paths[i];
584                 Rect & pbounds = path.GetBounds(m_document.m_objects); // Not very efficient...
585                 //TODO: Add clipping here
586                 //if (!pbounds.Intersects(Rect(0,0,1,1)) || pbounds.w < Real(1)/Real(800))
587                 //      continue;
588
589                 for (unsigned id = path.m_start; id <= path.m_end; ++id)
590                 {
591                         if (id < first_obj || id >= last_obj)
592                                 continue;
593                                 
594                         Rect obj_bounds = m_document.m_objects.bounds[id];
595
596                         obj_bounds.x *= pbounds.w;
597                         obj_bounds.x += pbounds.x;
598                         obj_bounds.y *= pbounds.h;
599                         obj_bounds.y += pbounds.y;
600                         obj_bounds.w *= pbounds.w;
601                         obj_bounds.h *= pbounds.h;
602                         
603                         if (!m_use_gpu_transform)
604                                 obj_bounds = TransformToViewCoords(obj_bounds);
605                         GPUObjBounds gpu_bounds = {
606                                 ClampFloat(obj_bounds.x),
607                                 ClampFloat(obj_bounds.y),
608                                 ClampFloat(obj_bounds.x + obj_bounds.w),
609                                 ClampFloat(obj_bounds.y + obj_bounds.h)
610                         };
611                         obj_bounds_builder.Add(gpu_bounds);
612                         //Debug("Path %d %s -> %s via %s", id, m_document.m_objects.bounds[id].Str().c_str(), obj_bounds.Str().c_str(), pbounds.Str().c_str()); 
613                         
614                         if (m_query_gpu_bounds_on_next_frame != NULL)
615                         {
616                                 fprintf(m_query_gpu_bounds_on_next_frame,"%d\t%f\t%f\t%f\t%f\n", id, ClampFloat(obj_bounds.x), ClampFloat(obj_bounds.y), ClampFloat(obj_bounds.w), ClampFloat(obj_bounds.h));
617                         }
618                 }
619                 GPUObjBounds p_gpu_bounds = {
620                                 ClampFloat(pbounds.x),
621                                 ClampFloat(pbounds.y),
622                                 ClampFloat(pbounds.x + pbounds.w),
623                                 ClampFloat(pbounds.y + pbounds.h)
624                 };              
625                 obj_bounds_builder.Add(p_gpu_bounds);
626         }
627         #endif
628         if (m_query_gpu_bounds_on_next_frame != NULL)
629         {
630                 if (m_query_gpu_bounds_on_next_frame != stdout && m_query_gpu_bounds_on_next_frame != stderr)
631                         fclose(m_query_gpu_bounds_on_next_frame);
632                 m_query_gpu_bounds_on_next_frame = NULL;
633         }
634         m_objbounds_vbo.UnMap();
635 }
636 /**
637  * Prepare the document for rendering
638  * Will be called on View::Render if m_render_dirty is set
639  * (Called at least once, on the first Render)
640  */
641 void View::PrepareRender()
642 {
643         PROFILE_SCOPE("View::PrepareRender()");
644         Debug("Recreate buffers with %u objects", m_document.ObjectCount());
645         // Prepare bounds vbo
646         if (UsingGPURendering())
647         {
648                 m_bounds_ubo.Invalidate();
649                 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
650                 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
651                 m_bounds_ubo.SetName("m_bounds_ubo: Screen bounds.");
652         }
653         
654         // Instead of having each ObjectRenderer go through the whole document
655         //  we initialise them, go through the document once adding to the appropriate Renderers
656         //  and then finalise them
657         // This will totally be efficient if we have like, a lot of distinct ObjectTypes. Which could totally happen. You never know.
658
659         // Prepare the buffers
660         for (unsigned i = 0; i < m_object_renderers.size(); ++i)
661         {
662                 m_object_renderers[i]->PrepareBuffers(m_document.ObjectCount());
663         }
664
665         // Add objects from Document to buffers
666         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
667         {
668                 ObjectType type = m_document.m_objects.types[id];
669                 m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling?
670                 // (Also, Wow I just actually used std::vector::at())
671                 // (Also, I just managed to make it throw an exception because I'm a moron)
672                 //Debug("Object of type %d", type);
673         }
674
675
676         // Finish the buffers
677         for (unsigned i = 0; i < m_object_renderers.size(); ++i)
678         {
679                 m_object_renderers[i]->FinaliseBuffers();
680         }
681         if (UsingGPURendering())
682         {
683                 dynamic_cast<BezierRenderer*>(m_object_renderers[BEZIER])->PrepareBezierGPUBuffer(m_document.m_objects);
684         }
685         m_render_dirty = false;
686 }
687
688 void View::SaveCPUBMP(const char * filename)
689 {
690         bool prev = UsingGPURendering();
691         SetGPURendering(false);
692         Render(800, 600);
693         ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, 800, 600}, filename);
694         SetGPURendering(prev);
695 }
696
697 void View::SaveGPUBMP(const char * filename)
698 {
699         bool prev = UsingGPURendering();
700         SetGPURendering(true);
701         Render(800,600);
702         m_screen.ScreenShot(filename);
703         SetGPURendering(prev);  
704 }
705
706 void View::QueryGPUBounds(const char * filename, const char * mode)
707 {
708         m_query_gpu_bounds_on_next_frame = fopen(filename, mode); 
709         Debug("File: %s", filename);
710         if (m_query_gpu_bounds_on_next_frame == NULL)
711                 Error("Couldn't open file \"%s\" : %s", filename, strerror(errno));
712         ForceBoundsDirty(); 
713         ForceBufferDirty(); 
714         ForceRenderDirty();
715 }

UCC git Repository :: git.ucc.asn.au