1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
/* Copyright (c) 2001, Stanford University
* All rights reserved
*
* See the file LICENSE.txt for information on redistributing this software.
*/
#include "server_dispatch.h"
#include "server.h"
#include "cr_error.h"
#include "state/cr_statetypes.h"
#include "cr_mem.h"
#include "cr_string.h"
/*
* This file provides implementations of the basic OpenGL matrix functions.
* We often need to twiddle with their operation in order to make tilesorting
* and non-planar projections work.
*/
/*
* Determine which view and projection matrices to use when in stereo mode.
* Return 0 = left eye, 1 = right eye.
*/
int crServerGetCurrentEye(void)
{
if (cr_server.currentEye != -1) {
/* current eye was specified by tilesort SPU */
return cr_server.currentEye;
}
else {
/* we have a quad-buffered window and we're watching glDrawBuffer */
GLenum drawBuffer = cr_server.curClient->currentCtxInfo->pContext->buffer.drawBuffer;
int eye = drawBuffer == GL_BACK_RIGHT || drawBuffer == GL_FRONT_RIGHT
|| drawBuffer == GL_RIGHT;
return eye;
}
}
void SERVER_DISPATCH_APIENTRY crServerDispatchLoadMatrixf( const GLfloat *m )
{
const GLenum matMode = cr_server.curClient->currentCtxInfo->pContext->transform.matrixMode;
const CRMuralInfo *mural = cr_server.curClient->currentMural;
crStateLoadMatrixf( m );
if (matMode == GL_MODELVIEW && cr_server.viewOverride) {
int eye = crServerGetCurrentEye();
crServerApplyViewMatrix(&cr_server.viewMatrix[eye]);
}
else {
cr_server.head_spu->dispatch_table.LoadMatrixf( m );
}
}
void SERVER_DISPATCH_APIENTRY crServerDispatchLoadMatrixd( const GLdouble *m )
{
const GLenum matMode = cr_server.curClient->currentCtxInfo->pContext->transform.matrixMode;
const CRMuralInfo *mural = cr_server.curClient->currentMural;
crStateLoadMatrixd( m );
if (matMode == GL_MODELVIEW && cr_server.viewOverride) {
int eye = crServerGetCurrentEye();
crServerApplyViewMatrix(&cr_server.viewMatrix[eye]);
}
else {
cr_server.head_spu->dispatch_table.LoadMatrixd( m );
}
}
void SERVER_DISPATCH_APIENTRY crServerDispatchMultMatrixf( const GLfloat *m )
{
const GLenum matMode = cr_server.curClient->currentCtxInfo->pContext->transform.matrixMode;
if (matMode == GL_PROJECTION && cr_server.projectionOverride) {
/* load the overriding projection matrix */
int eye = crServerGetCurrentEye();
crStateLoadMatrix( &cr_server.projectionMatrix[eye] );
}
else {
/* the usual case */
crStateMultMatrixf( m );
cr_server.head_spu->dispatch_table.MultMatrixf( m );
}
}
void SERVER_DISPATCH_APIENTRY crServerDispatchMultMatrixd( const GLdouble *m )
{
const GLenum matMode = cr_server.curClient->currentCtxInfo->pContext->transform.matrixMode;
if (matMode == GL_PROJECTION && cr_server.projectionOverride) {
/* load the overriding projection matrix */
int eye = crServerGetCurrentEye();
crStateLoadMatrix( &cr_server.projectionMatrix[eye] );
}
else {
/* the usual case */
crStateMultMatrixd( m );
cr_server.head_spu->dispatch_table.MultMatrixd( m );
}
}
void SERVER_DISPATCH_APIENTRY crServerDispatchLoadIdentity( void )
{
const GLenum matMode = cr_server.curClient->currentCtxInfo->pContext->transform.matrixMode;
const CRMuralInfo *mural = cr_server.curClient->currentMural;
crStateLoadIdentity();
if (matMode == GL_MODELVIEW && cr_server.viewOverride) {
int eye = crServerGetCurrentEye();
crServerApplyViewMatrix(&cr_server.viewMatrix[eye]);
}
else {
cr_server.head_spu->dispatch_table.LoadIdentity( );
}
}
/*
* The following code is used to deal with vertex programs.
* Basically, vertex programs might not directly use the usual
* OpenGL projection matrix to project vertices from eye coords to
* clip coords.
*
* If you're using Cg then the vertex programs it generates will have
* some comments that we can scan to figure out which program parameters
* contain the projection matrix.
* In this case, look at the Cg program code for a string like
* "ModelViewProj". Then set the crserver's 'vertprog_projection_param'
* config option to this name.
*
* If you're not using Cg, you may have to tell Chromium which program
* parameters contain the projection matrix.
* In this case, look at the OpenGL application's vertex program code to
* determine which program parameters contain the projection matrix.
* Then set the crserver's 'vertprog_projection_param' config option to
* the number of the parameter which holds the first row of the matrix.
*
* Yup, this is complicated.
*
*/
static void matmul(GLfloat r[16], const GLfloat p[16], const GLfloat q[16])
{
GLfloat tmp[16];
int i, j, k;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
GLfloat dot = 0.0;
for (k = 0; k < 4; k++) {
dot += p[i+4*k] * q[k+4*j];
}
tmp[i+4*j] = dot;
}
}
for (i = 0; i < 16; i++)
r[i] = tmp[i];
}
static CRServerProgram *
LookupProgram(GLuint id)
{
CRServerProgram *prog = crHashtableSearch(cr_server.programTable, id);
if (!prog) {
prog = (CRServerProgram *) crAlloc(sizeof(CRServerProgram));
if (!prog)
return NULL;
prog->id = id;
prog->projParamStart = cr_server.vpProjectionMatrixParameter;
crHashtableAdd(cr_server.programTable, id, prog);
}
return prog;
}
void SERVER_DISPATCH_APIENTRY
crServerDispatchProgramLocalParameter4fARB(GLenum target, GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
{
#if 0
if (target == GL_VERTEX_PROGRAM_ARB) {
CRServerProgram *prog = LookupProgram(cr_server.currentProgram);
if (prog && prog->projParamStart != -1) {
if (index >= (GLuint) prog->projParamStart && index <= (GLuint) prog->projParamStart + 3) {
/* save the parameters as rows in the matrix */
const int i = index - prog->projParamStart;
prog->projMat[4*0+i] = x;
prog->projMat[4*1+i] = y;
prog->projMat[4*2+i] = z;
prog->projMat[4*3+i] = w;
}
/* When we get the 4th row (row==3) of the projection matrix we can
* then pre-multiply it by the base matrix and update the program
* parameters with the new matrix.
*/
if (index == (GLuint) (prog->projParamStart + 3)) {
const CRMuralInfo *mural = cr_server.curClient->currentMural;
const GLfloat *baseMat = (const GLfloat *) &(mural->extents[mural->curExtent].baseProjection);
int i;
GLfloat mat[16];
/* pre-mult the projection matrix by the base projection */
matmul(mat, baseMat, prog->projMat);
/* update the program parameters with the new matrix */
for (i = 0; i < 4; i++) {
cr_server.head_spu->dispatch_table.ProgramLocalParameter4fARB(target, index + i - 3, mat[4*0+i], mat[4*1+i], mat[4*2+i], mat[4*3+i]);
}
return; /* done */
}
}
}
#endif
/* if we get here, pass the call through unchanged */
cr_server.head_spu->dispatch_table.ProgramLocalParameter4fARB(target, index, x, y, z, w);
}
void SERVER_DISPATCH_APIENTRY
crServerDispatchProgramLocalParameter4dARB(GLenum target, GLuint index, GLdouble x, GLdouble y, GLdouble z, GLdouble w)
{
crServerDispatchProgramLocalParameter4fARB(target, index, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
}
void SERVER_DISPATCH_APIENTRY
crServerDispatchProgramParameter4fNV(GLenum target, GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
{
#if 0
if (target == GL_VERTEX_PROGRAM_NV) {
CRServerProgram *prog = LookupProgram(cr_server.currentProgram);
if (prog && prog->projParamStart != -1) {
if (index >= (GLuint) prog->projParamStart && index <= (GLuint) prog->projParamStart + 3) {
/* save the parameters as rows in the matrix */
const int i = index - prog->projParamStart;
prog->projMat[4*0+i] = x;
prog->projMat[4*1+i] = y;
prog->projMat[4*2+i] = z;
prog->projMat[4*3+i] = w;
}
/* When we get the 4th row (row==3) of the projection matrix we can
* then pre-multiply it by the base matrix and update the program
* parameters with the new matrix.
*/
if (index == (GLuint) (prog->projParamStart + 3)) {
const CRMuralInfo *mural = cr_server.curClient->currentMural;
const GLfloat *baseMat = (const GLfloat *) &(mural->extents[mural->curExtent].baseProjection);
int i;
GLfloat mat[16];
/* pre-mult the projection matrix by the base projection */
matmul(mat, baseMat, prog->projMat);
/* update the program parameters with the new matrix */
for (i = 0; i < 4; i++) {
cr_server.head_spu->dispatch_table.ProgramParameter4fNV(target, index + i - 3, mat[4*0+i], mat[4*1+i], mat[4*2+i], mat[4*3+i]);
}
return; /* done */
}
}
}
#endif
/* if we get here, pass the call through unchanged */
cr_server.head_spu->dispatch_table.ProgramParameter4fNV(target, index, x, y, z, w);
}
void SERVER_DISPATCH_APIENTRY
crServerDispatchProgramParameter4dNV(GLenum target, GLuint index, GLdouble x, GLdouble y, GLdouble z, GLdouble w)
{
crServerDispatchProgramParameter4fNV(target, index, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
}
void SERVER_DISPATCH_APIENTRY
crServerDispatchProgramStringARB(GLenum target, GLenum format, GLsizei len, const GLvoid *string)
{
if (target == GL_VERTEX_PROGRAM_ARB &&
cr_server.vpProjectionMatrixVariable != NULL) {
/* scan the program string looking for 'vertprog_projection'
* If the program was generated by Cg, the info we want will look
* something like this:
* #var float4x4 ModelViewProj : : c[0], 4 : 1 : 1
*/
CRServerProgram *prog = LookupProgram(cr_server.currentProgram);
CRASSERT(prog);
if (prog) {
const char *varPos, *paramPos;
varPos = crStrstr((const char *) string, cr_server.vpProjectionMatrixVariable);
if (varPos) {
paramPos = crStrstr(varPos, "c[");
if (paramPos) {
char number[10];
int i = 0;
paramPos += 2; /* skip "c[" */
while (crIsDigit(paramPos[i])) {
number[i] = paramPos[i];
i++;
}
number[i] = 0;
prog->projParamStart = crStrToInt(number);
}
}
else {
crWarning("Didn't find %s parameter in vertex program string",
cr_server.vpProjectionMatrixVariable);
}
}
}
/* pass through */
crStateProgramStringARB(target, format, len, string);
cr_server.head_spu->dispatch_table.ProgramStringARB(target, format, len, string);
}
void SERVER_DISPATCH_APIENTRY
crServerDispatchLoadProgramNV(GLenum target, GLuint id, GLsizei len, const GLubyte *string)
{
if (target == GL_VERTEX_PROGRAM_NV &&
cr_server.vpProjectionMatrixVariable != NULL) {
/* scan the program string looking for 'vertprog_projection'
* If the program was generated by Cg, the info we want will look
* something like this:
* #var float4x4 ModelViewProj : : c[0], 4 : 1 : 1
*/
CRServerProgram *prog = LookupProgram(id);
CRASSERT(prog);
if (prog) {
const char *varPos, *paramPos;
varPos = crStrstr((const char *) string, cr_server.vpProjectionMatrixVariable);
if (varPos) {
paramPos = crStrstr(varPos, "c[");
if (paramPos) {
char number[10];
int i = 0;
paramPos += 2; /* skip "c[" */
while (crIsDigit(paramPos[i])) {
number[i] = paramPos[i];
i++;
}
number[i] = 0;
prog->projParamStart = crStrToInt(number);
}
}
else {
crWarning("Didn't find %s parameter in vertex program string",
cr_server.vpProjectionMatrixVariable);
}
}
}
/* pass through */
crStateLoadProgramNV(target, id, len, string);
cr_server.head_spu->dispatch_table.LoadProgramNV(target, id, len, string);
}
void SERVER_DISPATCH_APIENTRY
crServerDispatchBindProgramARB(GLenum target, GLuint id)
{
id = crServerTranslateProgramID(id);
if (target == GL_VERTEX_PROGRAM_ARB) {
CRServerProgram *prog = LookupProgram(id);
(void) prog;
cr_server.currentProgram = id;
}
/* pass through */
crStateBindProgramARB(target, id);
cr_server.head_spu->dispatch_table.BindProgramARB(target, id);
}
void SERVER_DISPATCH_APIENTRY
crServerDispatchBindProgramNV(GLenum target, GLuint id)
{
if (target == GL_VERTEX_PROGRAM_NV) {
CRServerProgram *prog = LookupProgram(id);
(void) prog;
cr_server.currentProgram = id;
}
/* pass through */
crStateBindProgramNV(target, id);
cr_server.head_spu->dispatch_table.BindProgramNV(target, id);
}
|