Fix GPU rendering a bit. Will probably break >1 ob
[ipdf/code.git] / src / main.h
1 #include "common.h"
2
3 #include "document.h"
4 #include "view.h"
5 #include "screen.h"
6 #include <unistd.h>
7
8
9 using namespace std;
10 using namespace IPDF;
11
12 inline void OverlayBMP(Document & doc, const char * input, const char * output, const Rect & bounds = Rect(0,0,1,1), const Colour & c = Colour(0.f,0.f,0.f,1.f))
13 {
14
15         Screen scr;
16         View view(doc, scr, bounds, c);
17         if (input != NULL)
18                 scr.RenderBMP(input);
19         view.Render();
20         if (output != NULL)
21                 scr.ScreenShot(output);
22         scr.Present();
23 }
24
25 inline void MainLoop(Document & doc, const Rect & bounds = Rect(0,0,1,1), const Colour & c = Colour(0.f,0.f,0.f,1.f))
26 {
27         // order is important... segfaults occur when screen (which inits GL) is not constructed first -_-
28         Screen scr;
29         View view(doc,scr, bounds, c);
30         scr.DebugFontInit("DejaVuSansMono.ttf");
31         scr.SetMouseHandler([&](int x, int y, int buttons, int wheel) // [?] wtf
32         {
33                 static bool oldButtonDown = false;
34                 static int oldx, oldy;
35                 if (buttons == 3 && !oldButtonDown)
36                 {
37                         oldButtonDown = true;
38                         view.ToggleGPUTransform();
39                         oldx = x;
40                         oldy = y;
41                         return;
42                 }
43                 if (buttons == 2 && !oldButtonDown)
44                 {
45                         oldButtonDown = true;
46                         view.ToggleGPURendering();
47                         oldx = x;
48                         oldy = y;
49                 }
50                 if (buttons && !oldButtonDown)
51                 {
52                         // We're beginning a drag.
53                         oldButtonDown = true;
54                         oldx = x;
55                         oldy = y;
56                         scr.SetMouseCursor(Screen::CursorMove);
57                 }
58                 if (buttons)
59                 {
60                         #if REAL == REAL_RATIONAL
61                                 view.Translate(Real(oldx, scr.ViewportWidth()) -Real(x,scr.ViewportWidth()), Real(oldy, scr.ViewportHeight()) - Real(y,scr.ViewportHeight()));
62                         #else                   
63                                 view.Translate(Real(oldx-x)/Real(scr.ViewportWidth()), Real(oldy-y)/Real(scr.ViewportHeight()));
64                         #endif
65                 }
66                 else
67                 {
68                         oldButtonDown = false;
69                         scr.SetMouseCursor(Screen::CursorArrow);
70                 }
71                 oldx = x;
72                 oldy = y;
73                 
74                 if (wheel)
75                 {
76                         #if REAL == REAL_RATIONAL
77                                 view.ScaleAroundPoint(Real(x,scr.ViewportWidth()), Real(y,scr.ViewportHeight()), Real(100-5*wheel, 100));
78                         #else
79                                 view.ScaleAroundPoint(Real(x)/Real(scr.ViewportWidth()),Real(y)/Real(scr.ViewportHeight()), Real(expf(-wheel/20.f)));
80                         #endif
81                 
82                 }
83         }
84         );
85
86         double total_cpu_time = 0;
87         double total_gpu_time = 0;
88         double total_real_time = 0;
89         struct timespec real_clock_start;
90         struct timespec real_clock_now;
91         struct timespec real_clock_prev;
92         clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_start);
93         real_clock_now = real_clock_start;
94         double frames = 0;
95         double data_rate = 1; // period between data output to stdout (if <= 0 there will be no output)
96         uint64_t data_points = 0;
97         setbuf(stdout, NULL);
98         while (scr.PumpEvents())
99         {
100                 real_clock_prev = real_clock_now;
101                 ++frames;
102                 scr.Clear();
103                 //view.ForceBoundsDirty();
104                 //view.ForceBufferDirty();
105                 //view.ForceRenderDirty();
106
107                 view.Render(scr.ViewportWidth(), scr.ViewportHeight());
108
109                 double cpu_frame = scr.GetLastFrameTimeCPU();
110                 double gpu_frame = scr.GetLastFrameTimeGPU();
111                 clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_now);
112                 double real_frame = (real_clock_now.tv_sec - real_clock_prev.tv_sec) + 1e-9*(real_clock_now.tv_nsec - real_clock_prev.tv_nsec);
113
114
115                 total_real_time += real_frame; total_cpu_time += cpu_frame; total_gpu_time += gpu_frame;
116                 if (data_rate > 0 && total_real_time > data_rate*(data_points+1)) 
117                 {
118                         printf("%lu\t%f\t%f\t%f\t%f\t%f\t%f\n", (uint64_t)frames, total_real_time, total_cpu_time, total_gpu_time, real_frame, cpu_frame, gpu_frame);
119                         data_points++;
120                 }
121                 scr.DebugFontPrintF("Rendered frame %lu\n", (uint64_t)frames);
122                 scr.DebugFontPrintF("[CPU] Render took %lf ms (%lf FPS) (total %lf s, avg FPS %lf)\n", cpu_frame*1e3, 1.0/cpu_frame, total_cpu_time,frames/total_cpu_time);
123                 scr.DebugFontPrintF("[GPU] Render took %lf ms (%lf FPS) (total %lf s, avg FPS %lf)\n", gpu_frame*1e3, 1.0/gpu_frame, total_gpu_time, frames/total_gpu_time);
124                 scr.DebugFontPrintF("[REALTIME] Render+Present+Cruft took %lf ms (%lf FPS) (total %lf s, avg FPS %lf)\n", real_frame*1e3, 1.0/real_frame, total_real_time,frames/total_real_time);
125                 scr.DebugFontPrintF("View bounds: %s\n", view.GetBounds().Str().c_str());
126                 scr.DebugFontPrintF("type of Real == %s\n", g_real_name[REAL]);
127
128                 if (view.UsingGPUTransform())
129                 {
130                         scr.DebugFontPrint("Doing coordinate transform on the GPU.\n");
131                 }
132                 else
133                 {
134                         scr.DebugFontPrint("Doing coordinate transform on the CPU.\n");
135                 }
136                 if (view.UsingGPURendering())
137                 {
138                         scr.DebugFontPrint("Doing rendering using GPU.\n");
139                 }
140                 else
141                 {
142                         scr.DebugFontPrint("Doing rendering using CPU.\n");
143                 }
144                 scr.Present();
145         }
146 }

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