1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "cr_spu.h"
|
---|
8 | #include "chromium.h"
|
---|
9 | #include "cr_mem.h"
|
---|
10 | #include "cr_net.h"
|
---|
11 | #include "server_dispatch.h"
|
---|
12 | #include "server.h"
|
---|
13 |
|
---|
14 | #ifdef VBOXCR_LOGFPS
|
---|
15 | #include <iprt/timer.h>
|
---|
16 | #include <iprt/ctype.h>
|
---|
17 | typedef struct VBOXCRFPS
|
---|
18 | {
|
---|
19 | uint64_t mPeriodSum;
|
---|
20 | uint64_t *mpaPeriods;
|
---|
21 | uint64_t mPrevTime;
|
---|
22 | uint64_t mcFrames;
|
---|
23 | uint32_t mcPeriods;
|
---|
24 | uint32_t miPeriod;
|
---|
25 | } VBOXCRFPS, *PVBOXCRFPS;
|
---|
26 |
|
---|
27 | void vboxCrFpsInit(PVBOXCRFPS pFps, uint32_t cPeriods)
|
---|
28 | {
|
---|
29 | memset(pFps, 0, sizeof (*pFps));
|
---|
30 | pFps->mcPeriods = cPeriods;
|
---|
31 | pFps->mpaPeriods = malloc(sizeof (pFps->mpaPeriods[0]) * cPeriods);
|
---|
32 | memset(pFps->mpaPeriods, 0, cPeriods * sizeof(pFps->mpaPeriods[0]));
|
---|
33 | }
|
---|
34 |
|
---|
35 | void vboxCrFpsTerm(PVBOXCRFPS pFps)
|
---|
36 | {
|
---|
37 | free(pFps->mpaPeriods);
|
---|
38 | }
|
---|
39 |
|
---|
40 | void vboxCrFpsReportFrame(PVBOXCRFPS pFps)
|
---|
41 | {
|
---|
42 | uint64_t cur = RTTimeNanoTS();
|
---|
43 | if(pFps->mPrevTime)
|
---|
44 | {
|
---|
45 | uint64_t curPeriod = cur - pFps->mPrevTime;
|
---|
46 | pFps->mPeriodSum += curPeriod - pFps->mpaPeriods[pFps->miPeriod];
|
---|
47 | pFps->mpaPeriods[pFps->miPeriod] = curPeriod;
|
---|
48 | ++pFps->miPeriod;
|
---|
49 | pFps->miPeriod %= pFps->mcPeriods;
|
---|
50 | }
|
---|
51 | pFps->mPrevTime = cur;
|
---|
52 | ++pFps->mcFrames;
|
---|
53 | }
|
---|
54 |
|
---|
55 | uint64_t vboxCrFpsGetEveragePeriod(PVBOXCRFPS pFps)
|
---|
56 | {
|
---|
57 | return pFps->mPeriodSum / pFps->mcPeriods;
|
---|
58 | }
|
---|
59 |
|
---|
60 | double vboxCrFpsGetFps(PVBOXCRFPS pFps)
|
---|
61 | {
|
---|
62 | return ((double)1000000000.0) / vboxCrFpsGetEveragePeriod(pFps);
|
---|
63 | }
|
---|
64 |
|
---|
65 | uint64_t vboxCrFpsGetNumFrames(PVBOXCRFPS pFps)
|
---|
66 | {
|
---|
67 | return pFps->mcFrames;
|
---|
68 | }
|
---|
69 |
|
---|
70 | #endif
|
---|
71 |
|
---|
72 |
|
---|
73 | void SERVER_DISPATCH_APIENTRY crServerDispatchClear( GLenum mask )
|
---|
74 | {
|
---|
75 | CRMuralInfo *mural = cr_server.curClient->currentMural;
|
---|
76 | const RunQueue *q = cr_server.run_queue;
|
---|
77 |
|
---|
78 | if (cr_server.only_swap_once)
|
---|
79 | {
|
---|
80 | /* NOTE: we only do the clear for the _last_ client in the list.
|
---|
81 | * This is because in multi-threaded apps the zeroeth client may
|
---|
82 | * be idle and never call glClear at all. See threadtest.c
|
---|
83 | * It's pretty likely that the last client will be active.
|
---|
84 | */
|
---|
85 | if ((mask & GL_COLOR_BUFFER_BIT) &&
|
---|
86 | (cr_server.curClient != cr_server.clients[cr_server.numClients - 1]))
|
---|
87 | return;
|
---|
88 | }
|
---|
89 |
|
---|
90 | cr_server.head_spu->dispatch_table.Clear( mask );
|
---|
91 | }
|
---|
92 |
|
---|
93 | static void __draw_poly(CRPoly *p)
|
---|
94 | {
|
---|
95 | int b;
|
---|
96 |
|
---|
97 | cr_server.head_spu->dispatch_table.Begin(GL_POLYGON);
|
---|
98 | for (b=0; b<p->npoints; b++)
|
---|
99 | cr_server.head_spu->dispatch_table.Vertex2dv(p->points+2*b);
|
---|
100 | cr_server.head_spu->dispatch_table.End();
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | void SERVER_DISPATCH_APIENTRY
|
---|
105 | crServerDispatchSwapBuffers( GLint window, GLint flags )
|
---|
106 | {
|
---|
107 | CRMuralInfo *mural;
|
---|
108 | #ifdef VBOXCR_LOGFPS
|
---|
109 | static VBOXCRFPS Fps;
|
---|
110 | static bool bFpsInited = false;
|
---|
111 |
|
---|
112 | if (!bFpsInited)
|
---|
113 | {
|
---|
114 | vboxCrFpsInit(&Fps, 64 /* cPeriods */);
|
---|
115 | bFpsInited = true;
|
---|
116 | }
|
---|
117 | vboxCrFpsReportFrame(&Fps);
|
---|
118 | if(!(vboxCrFpsGetNumFrames(&Fps) % 31))
|
---|
119 | {
|
---|
120 | double fps = vboxCrFpsGetFps(&Fps);
|
---|
121 | crDebug("fps: %f\n", fps);
|
---|
122 | }
|
---|
123 | #endif
|
---|
124 | mural = (CRMuralInfo *) crHashtableSearch(cr_server.muralTable, window);
|
---|
125 | if (!mural) {
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | if (cr_server.only_swap_once)
|
---|
131 | {
|
---|
132 | /* NOTE: we only do the clear for the _last_ client in the list.
|
---|
133 | * This is because in multi-threaded apps the zeroeth client may
|
---|
134 | * be idle and never call glClear at all. See threadtest.c
|
---|
135 | * It's pretty likely that the last client will be active.
|
---|
136 | */
|
---|
137 | if (cr_server.curClient != cr_server.clients[cr_server.numClients - 1])
|
---|
138 | {
|
---|
139 | return;
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | #if 0
|
---|
144 | if (cr_server.overlapBlending)
|
---|
145 | {
|
---|
146 | int a;
|
---|
147 | CRPoly *p;
|
---|
148 | GLboolean lighting, fog, blend, cull, tex[3];
|
---|
149 | GLenum mm, blendSrc, blendDst;
|
---|
150 | GLcolorf col;
|
---|
151 | CRContext *ctx = crStateGetCurrent();
|
---|
152 | const CRmatrix *baseProj;
|
---|
153 |
|
---|
154 | /*
|
---|
155 | * I've probably missed some state here, or it
|
---|
156 | * might be easier just to push/pop it....
|
---|
157 | */
|
---|
158 | lighting = ctx->lighting.lighting;
|
---|
159 | fog = ctx->fog.enable;
|
---|
160 | tex[0] = 0;
|
---|
161 | for (a=0; a<CR_MAX_TEXTURE_UNITS; a++)
|
---|
162 | {
|
---|
163 | if (!ctx->texture.unit[a].enabled1D) continue;
|
---|
164 |
|
---|
165 | tex[0] = 1;
|
---|
166 | break;
|
---|
167 | }
|
---|
168 | tex[1] = 0;
|
---|
169 | for (a=0; a<CR_MAX_TEXTURE_UNITS; a++)
|
---|
170 | {
|
---|
171 | if (!ctx->texture.unit[a].enabled2D) continue;
|
---|
172 |
|
---|
173 | tex[1] = 1;
|
---|
174 | break;
|
---|
175 | }
|
---|
176 | tex[2] = 0;
|
---|
177 | for (a=0; a<CR_MAX_TEXTURE_UNITS; a++)
|
---|
178 | {
|
---|
179 | if (!ctx->texture.unit[a].enabled3D) continue;
|
---|
180 |
|
---|
181 | tex[2] = 1;
|
---|
182 | break;
|
---|
183 | }
|
---|
184 |
|
---|
185 | cull = ctx->polygon.cullFace;
|
---|
186 | blend = ctx->buffer.blend;
|
---|
187 | blendSrc = ctx->buffer.blendSrcRGB;
|
---|
188 | blendDst = ctx->buffer.blendDstRGB;
|
---|
189 | mm = ctx->transform.matrixMode;
|
---|
190 | col.r = ctx->current.vertexAttrib[VERT_ATTRIB_COLOR0][0];
|
---|
191 | col.g = ctx->current.vertexAttrib[VERT_ATTRIB_COLOR0][1];
|
---|
192 | col.b = ctx->current.vertexAttrib[VERT_ATTRIB_COLOR0][2];
|
---|
193 | col.a = ctx->current.vertexAttrib[VERT_ATTRIB_COLOR0][3];
|
---|
194 |
|
---|
195 | baseProj = &(cr_server.curClient->currentMural->extents[0].baseProjection);
|
---|
196 |
|
---|
197 | switch(mm)
|
---|
198 | {
|
---|
199 | case GL_PROJECTION:
|
---|
200 | cr_server.head_spu->dispatch_table.PushMatrix();
|
---|
201 | cr_server.head_spu->dispatch_table.LoadMatrixf((GLfloat *) baseProj);
|
---|
202 | cr_server.head_spu->dispatch_table.MultMatrixf(cr_server.unnormalized_alignment_matrix);
|
---|
203 | cr_server.head_spu->dispatch_table.MatrixMode(GL_MODELVIEW);
|
---|
204 | cr_server.head_spu->dispatch_table.PushMatrix();
|
---|
205 | cr_server.head_spu->dispatch_table.LoadIdentity();
|
---|
206 | break;
|
---|
207 |
|
---|
208 | default:
|
---|
209 | cr_server.head_spu->dispatch_table.MatrixMode(GL_MODELVIEW);
|
---|
210 | /* fall through */
|
---|
211 |
|
---|
212 | case GL_MODELVIEW:
|
---|
213 | cr_server.head_spu->dispatch_table.PushMatrix();
|
---|
214 | cr_server.head_spu->dispatch_table.LoadIdentity();
|
---|
215 | cr_server.head_spu->dispatch_table.MatrixMode(GL_PROJECTION);
|
---|
216 | cr_server.head_spu->dispatch_table.PushMatrix();
|
---|
217 | cr_server.head_spu->dispatch_table.LoadMatrixf((GLfloat *) baseProj);
|
---|
218 | cr_server.head_spu->dispatch_table.MultMatrixf(cr_server.unnormalized_alignment_matrix);
|
---|
219 | break;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /* fix state */
|
---|
223 | if (lighting)
|
---|
224 | cr_server.head_spu->dispatch_table.Disable(GL_LIGHTING);
|
---|
225 | if (fog)
|
---|
226 | cr_server.head_spu->dispatch_table.Disable(GL_FOG);
|
---|
227 | if (tex[0])
|
---|
228 | cr_server.head_spu->dispatch_table.Disable(GL_TEXTURE_1D);
|
---|
229 | if (tex[1])
|
---|
230 | cr_server.head_spu->dispatch_table.Disable(GL_TEXTURE_2D);
|
---|
231 | if (tex[2])
|
---|
232 | cr_server.head_spu->dispatch_table.Disable(GL_TEXTURE_3D);
|
---|
233 | if (cull)
|
---|
234 | cr_server.head_spu->dispatch_table.Disable(GL_CULL_FACE);
|
---|
235 |
|
---|
236 | /* Regular Blending */
|
---|
237 | if (cr_server.overlapBlending == 1)
|
---|
238 | {
|
---|
239 | if (!blend)
|
---|
240 | cr_server.head_spu->dispatch_table.Enable(GL_BLEND);
|
---|
241 | if ((blendSrc != GL_ZERO) && (blendDst != GL_SRC_ALPHA))
|
---|
242 | cr_server.head_spu->dispatch_table.BlendFunc(GL_ZERO, GL_SRC_ALPHA);
|
---|
243 |
|
---|
244 | /* draw the blends */
|
---|
245 | for (a=1; a<cr_server.num_overlap_levels; a++)
|
---|
246 | {
|
---|
247 | if (a-1 < cr_server.num_overlap_intens)
|
---|
248 | {
|
---|
249 | cr_server.head_spu->dispatch_table.Color4f(0, 0, 0,
|
---|
250 | cr_server.overlap_intens[a-1]);
|
---|
251 | }
|
---|
252 | else
|
---|
253 | {
|
---|
254 | cr_server.head_spu->dispatch_table.Color4f(0, 0, 0, 1);
|
---|
255 | }
|
---|
256 |
|
---|
257 | p = cr_server.overlap_geom[a];
|
---|
258 | while (p)
|
---|
259 | {
|
---|
260 | /* hopefully this isnt concave... */
|
---|
261 | __draw_poly(p);
|
---|
262 | p = p->next;
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | if (!blend)
|
---|
267 | cr_server.head_spu->dispatch_table.Disable(GL_BLEND);
|
---|
268 | if ((blendSrc != GL_ZERO) && (blendDst != GL_SRC_ALPHA))
|
---|
269 | cr_server.head_spu->dispatch_table.BlendFunc(blendSrc, blendDst);
|
---|
270 | }
|
---|
271 | else
|
---|
272 | /* Knockout Blending */
|
---|
273 | {
|
---|
274 | cr_server.head_spu->dispatch_table.Color4f(0, 0, 0, 1);
|
---|
275 |
|
---|
276 | if (blend)
|
---|
277 | cr_server.head_spu->dispatch_table.Disable(GL_BLEND);
|
---|
278 | p = cr_server.overlap_knockout;
|
---|
279 | while (p)
|
---|
280 | {
|
---|
281 | __draw_poly(p);
|
---|
282 | p = p->next;
|
---|
283 | }
|
---|
284 | if (blend)
|
---|
285 | cr_server.head_spu->dispatch_table.Enable(GL_BLEND);
|
---|
286 | }
|
---|
287 |
|
---|
288 |
|
---|
289 | /* return things to normal */
|
---|
290 | switch (mm)
|
---|
291 | {
|
---|
292 | case GL_PROJECTION:
|
---|
293 | cr_server.head_spu->dispatch_table.PopMatrix();
|
---|
294 | cr_server.head_spu->dispatch_table.MatrixMode(GL_PROJECTION);
|
---|
295 | cr_server.head_spu->dispatch_table.PopMatrix();
|
---|
296 | break;
|
---|
297 | case GL_MODELVIEW:
|
---|
298 | cr_server.head_spu->dispatch_table.PopMatrix();
|
---|
299 | cr_server.head_spu->dispatch_table.MatrixMode(GL_MODELVIEW);
|
---|
300 | cr_server.head_spu->dispatch_table.PopMatrix();
|
---|
301 | break;
|
---|
302 | default:
|
---|
303 | cr_server.head_spu->dispatch_table.PopMatrix();
|
---|
304 | cr_server.head_spu->dispatch_table.MatrixMode(GL_MODELVIEW);
|
---|
305 | cr_server.head_spu->dispatch_table.PopMatrix();
|
---|
306 | cr_server.head_spu->dispatch_table.MatrixMode(mm);
|
---|
307 | break;
|
---|
308 | }
|
---|
309 |
|
---|
310 | if (lighting)
|
---|
311 | cr_server.head_spu->dispatch_table.Enable(GL_LIGHTING);
|
---|
312 | if (fog)
|
---|
313 | cr_server.head_spu->dispatch_table.Enable(GL_FOG);
|
---|
314 | if (tex[0])
|
---|
315 | cr_server.head_spu->dispatch_table.Enable(GL_TEXTURE_1D);
|
---|
316 | if (tex[1])
|
---|
317 | cr_server.head_spu->dispatch_table.Enable(GL_TEXTURE_2D);
|
---|
318 | if (tex[2])
|
---|
319 | cr_server.head_spu->dispatch_table.Enable(GL_TEXTURE_3D);
|
---|
320 | if (cull)
|
---|
321 | cr_server.head_spu->dispatch_table.Enable(GL_CULL_FACE);
|
---|
322 |
|
---|
323 | cr_server.head_spu->dispatch_table.Color4f(col.r, col.g, col.b, col.a);
|
---|
324 | }
|
---|
325 | #endif
|
---|
326 |
|
---|
327 | /* Check if using a file network */
|
---|
328 | if (!cr_server.clients[0]->conn->actual_network && window == MAGIC_OFFSET)
|
---|
329 | window = 0;
|
---|
330 |
|
---|
331 | if (crServerIsRedirectedToFBO())
|
---|
332 | {
|
---|
333 | crServerPresentFBO(mural);
|
---|
334 | }
|
---|
335 | else
|
---|
336 | {
|
---|
337 | cr_server.head_spu->dispatch_table.SwapBuffers( mural->spuWindow, flags );
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 | void SERVER_DISPATCH_APIENTRY
|
---|
342 | crServerDispatchFlush(void)
|
---|
343 | {
|
---|
344 | cr_server.head_spu->dispatch_table.Flush();
|
---|
345 |
|
---|
346 | if (crServerIsRedirectedToFBO())
|
---|
347 | {
|
---|
348 | crServerPresentFBO(cr_server.curClient->currentMural);
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | void SERVER_DISPATCH_APIENTRY
|
---|
353 | crServerDispatchFinish(void)
|
---|
354 | {
|
---|
355 | cr_server.head_spu->dispatch_table.Finish();
|
---|
356 |
|
---|
357 | if (crServerIsRedirectedToFBO())
|
---|
358 | {
|
---|
359 | crServerPresentFBO(cr_server.curClient->currentMural);
|
---|
360 | }
|
---|
361 | }
|
---|