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 "packer.h"
|
---|
8 | #include "cr_opcodes.h"
|
---|
9 | #include "cr_version.h"
|
---|
10 | #include "state/cr_limits.h"
|
---|
11 | #include "cr_glstate.h"
|
---|
12 |
|
---|
13 | /*Convert from GLint to GLfloat in [-1.f,1.f]*/
|
---|
14 | #define CRP_I2F_NORM(i) ((2.f*((GLint)(i))+1.f) * (1.f/4294967294.f))
|
---|
15 | /*Convert from GLshort to GLfloat in [-1.f,1.f]*/
|
---|
16 | #define CRP_S2F_NORM(s) ((2.f*((GLshort)(s))+1.f) * (1.f/65535.f))
|
---|
17 |
|
---|
18 |
|
---|
19 | static void crPackVertexAttrib(const CRVertexArrays *array, unsigned int attr, GLint index)
|
---|
20 | {
|
---|
21 | GLint *iPtr;
|
---|
22 | GLshort *sPtr;
|
---|
23 | unsigned char *p = array->a[attr].p + index * array->a[attr].stride;
|
---|
24 |
|
---|
25 | #ifdef CR_ARB_vertex_buffer_object
|
---|
26 | if (array->a[attr].buffer && array->a[attr].buffer->data)
|
---|
27 | {
|
---|
28 | p = (unsigned char *)(array->a[attr].buffer->data) + (unsigned long)p;
|
---|
29 | }
|
---|
30 | #endif
|
---|
31 | switch (array->a[attr].type)
|
---|
32 | {
|
---|
33 | case GL_SHORT:
|
---|
34 | sPtr = (GLshort*) p;
|
---|
35 | switch (array->a[attr].size)
|
---|
36 | {
|
---|
37 | case 1:
|
---|
38 | if (array->a[attr].normalized)
|
---|
39 | crPackVertexAttrib1fARB(attr, CRP_S2F_NORM(sPtr[0]));
|
---|
40 | else
|
---|
41 | crPackVertexAttrib1svARB(attr, sPtr);
|
---|
42 | break;
|
---|
43 | case 2:
|
---|
44 | if (array->a[attr].normalized)
|
---|
45 | crPackVertexAttrib2fARB(attr, CRP_S2F_NORM(sPtr[0]), CRP_S2F_NORM(sPtr[1]));
|
---|
46 | else
|
---|
47 | crPackVertexAttrib2svARB(attr, sPtr);
|
---|
48 | break;
|
---|
49 | case 3:
|
---|
50 | if (array->a[attr].normalized)
|
---|
51 | crPackVertexAttrib3fARB(attr, CRP_S2F_NORM(sPtr[0]), CRP_S2F_NORM(sPtr[1]), CRP_S2F_NORM(sPtr[2]));
|
---|
52 | else
|
---|
53 | crPackVertexAttrib3svARB(attr, sPtr);
|
---|
54 | break;
|
---|
55 | case 4:
|
---|
56 | if (array->a[attr].normalized)
|
---|
57 | crPackVertexAttrib4NsvARB(attr, sPtr);
|
---|
58 | else
|
---|
59 | crPackVertexAttrib4svARB(attr, sPtr);
|
---|
60 | break;
|
---|
61 | }
|
---|
62 | break;
|
---|
63 | case GL_INT:
|
---|
64 | iPtr = (GLint*) p;
|
---|
65 | switch (array->a[attr].size)
|
---|
66 | {
|
---|
67 | case 1:
|
---|
68 | if (array->a[attr].normalized)
|
---|
69 | crPackVertexAttrib1fARB(attr, CRP_I2F_NORM(iPtr[0]));
|
---|
70 | else
|
---|
71 | crPackVertexAttrib1fARB(attr, iPtr[0]);
|
---|
72 | break;
|
---|
73 | case 2:
|
---|
74 | if (array->a[attr].normalized)
|
---|
75 | crPackVertexAttrib2fARB(attr, CRP_I2F_NORM(iPtr[0]), CRP_I2F_NORM(iPtr[1]));
|
---|
76 | else
|
---|
77 | crPackVertexAttrib2fARB(attr, iPtr[0], iPtr[1]);
|
---|
78 | break;
|
---|
79 | case 3:
|
---|
80 | if (array->a[attr].normalized)
|
---|
81 | crPackVertexAttrib3fARB(attr, CRP_I2F_NORM(iPtr[0]), CRP_I2F_NORM(iPtr[1]), CRP_I2F_NORM(iPtr[2]));
|
---|
82 | else
|
---|
83 | crPackVertexAttrib3fARB(attr, iPtr[0], iPtr[1], iPtr[2]);
|
---|
84 | break;
|
---|
85 | case 4:
|
---|
86 | if (array->a[attr].normalized)
|
---|
87 | crPackVertexAttrib4NivARB(attr, iPtr);
|
---|
88 | else
|
---|
89 | crPackVertexAttrib4fARB(attr, iPtr[0], iPtr[1], iPtr[2], iPtr[3]);
|
---|
90 | break;
|
---|
91 | }
|
---|
92 | break;
|
---|
93 | case GL_FLOAT:
|
---|
94 | switch (array->a[attr].size)
|
---|
95 | {
|
---|
96 | case 1: crPackVertexAttrib1fvARB(attr, (GLfloat *)p); break;
|
---|
97 | case 2: crPackVertexAttrib2fvARB(attr, (GLfloat *)p); break;
|
---|
98 | case 3: crPackVertexAttrib3fvARB(attr, (GLfloat *)p); break;
|
---|
99 | case 4: crPackVertexAttrib4fvARB(attr, (GLfloat *)p); break;
|
---|
100 | }
|
---|
101 | break;
|
---|
102 | case GL_DOUBLE:
|
---|
103 | switch (array->a[attr].size)
|
---|
104 | {
|
---|
105 | case 1: crPackVertexAttrib1dvARB(attr, (GLdouble *)p); break;
|
---|
106 | case 2: crPackVertexAttrib2dvARB(attr, (GLdouble *)p); break;
|
---|
107 | case 3: crPackVertexAttrib3dvARB(attr, (GLdouble *)p); break;
|
---|
108 | case 4: crPackVertexAttrib4dvARB(attr, (GLdouble *)p); break;
|
---|
109 | }
|
---|
110 | break;
|
---|
111 | default:
|
---|
112 | crWarning("Bad datatype for vertex attribute [%d] array: 0x%x\n",
|
---|
113 | attr, array->a[attr].type);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Expand glArrayElement into crPackVertex/Color/Normal/etc.
|
---|
119 | */
|
---|
120 | void
|
---|
121 | crPackExpandArrayElement(GLint index, CRClientState *c)
|
---|
122 | {
|
---|
123 | unsigned char *p;
|
---|
124 | unsigned int unit, attr;
|
---|
125 | const CRVertexArrays *array = &(c->array);
|
---|
126 | const GLboolean vpEnabled = crStateGetCurrent()->program.vpEnabled;
|
---|
127 |
|
---|
128 | if (array->n.enabled && !(vpEnabled && array->a[VERT_ATTRIB_NORMAL].enabled))
|
---|
129 | {
|
---|
130 | p = array->n.p + index * array->n.stride;
|
---|
131 |
|
---|
132 | #ifdef CR_ARB_vertex_buffer_object
|
---|
133 | if (array->n.buffer && array->n.buffer->data)
|
---|
134 | {
|
---|
135 | p = (unsigned char *)(array->n.buffer->data) + (unsigned long)p;
|
---|
136 | }
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | switch (array->n.type)
|
---|
140 | {
|
---|
141 | case GL_BYTE: crPackNormal3bv((GLbyte *)p); break;
|
---|
142 | case GL_SHORT: crPackNormal3sv((GLshort *)p); break;
|
---|
143 | case GL_INT: crPackNormal3iv((GLint *)p); break;
|
---|
144 | case GL_FLOAT: crPackNormal3fv((GLfloat *)p); break;
|
---|
145 | case GL_DOUBLE: crPackNormal3dv((GLdouble *)p); break;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (array->c.enabled && !(vpEnabled && array->a[VERT_ATTRIB_COLOR0].enabled))
|
---|
150 | {
|
---|
151 | p = array->c.p + index * array->c.stride;
|
---|
152 |
|
---|
153 | #ifdef CR_ARB_vertex_buffer_object
|
---|
154 | if (array->c.buffer && array->c.buffer->data)
|
---|
155 | {
|
---|
156 | p = (unsigned char *)(array->c.buffer->data) + (unsigned long)p;
|
---|
157 | }
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | switch (array->c.type)
|
---|
161 | {
|
---|
162 | case GL_BYTE:
|
---|
163 | switch (c->array.c.size)
|
---|
164 | {
|
---|
165 | case 3: crPackColor3bv((GLbyte *)p); break;
|
---|
166 | case 4: crPackColor4bv((GLbyte *)p); break;
|
---|
167 | }
|
---|
168 | break;
|
---|
169 | case GL_UNSIGNED_BYTE:
|
---|
170 | switch (c->array.c.size)
|
---|
171 | {
|
---|
172 | case 3: crPackColor3ubv((GLubyte *)p); break;
|
---|
173 | case 4: crPackColor4ubv((GLubyte *)p); break;
|
---|
174 | }
|
---|
175 | break;
|
---|
176 | case GL_SHORT:
|
---|
177 | switch (c->array.c.size)
|
---|
178 | {
|
---|
179 | case 3: crPackColor3sv((GLshort *)p); break;
|
---|
180 | case 4: crPackColor4sv((GLshort *)p); break;
|
---|
181 | }
|
---|
182 | break;
|
---|
183 | case GL_UNSIGNED_SHORT:
|
---|
184 | switch (c->array.c.size)
|
---|
185 | {
|
---|
186 | case 3: crPackColor3usv((GLushort *)p); break;
|
---|
187 | case 4: crPackColor4usv((GLushort *)p); break;
|
---|
188 | }
|
---|
189 | break;
|
---|
190 | case GL_INT:
|
---|
191 | switch (c->array.c.size)
|
---|
192 | {
|
---|
193 | case 3: crPackColor3iv((GLint *)p); break;
|
---|
194 | case 4: crPackColor4iv((GLint *)p); break;
|
---|
195 | }
|
---|
196 | break;
|
---|
197 | case GL_UNSIGNED_INT:
|
---|
198 | switch (c->array.c.size)
|
---|
199 | {
|
---|
200 | case 3: crPackColor3uiv((GLuint *)p); break;
|
---|
201 | case 4: crPackColor4uiv((GLuint *)p); break;
|
---|
202 | }
|
---|
203 | break;
|
---|
204 | case GL_FLOAT:
|
---|
205 | switch (c->array.c.size)
|
---|
206 | {
|
---|
207 | case 3: crPackColor3fv((GLfloat *)p); break;
|
---|
208 | case 4: crPackColor4fv((GLfloat *)p); break;
|
---|
209 | }
|
---|
210 | break;
|
---|
211 | case GL_DOUBLE:
|
---|
212 | switch (c->array.c.size)
|
---|
213 | {
|
---|
214 | case 3: crPackColor3dv((GLdouble *)p); break;
|
---|
215 | case 4: crPackColor4dv((GLdouble *)p); break;
|
---|
216 | }
|
---|
217 | break;
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | #ifdef CR_EXT_secondary_color
|
---|
222 | if (array->s.enabled && !(vpEnabled && array->a[VERT_ATTRIB_COLOR1].enabled))
|
---|
223 | {
|
---|
224 | p = array->s.p + index * array->s.stride;
|
---|
225 |
|
---|
226 | #ifdef CR_ARB_vertex_buffer_object
|
---|
227 | if (array->s.buffer && array->s.buffer->data)
|
---|
228 | {
|
---|
229 | p = (unsigned char *)(array->s.buffer->data) + (unsigned long)p;
|
---|
230 | }
|
---|
231 | #endif
|
---|
232 |
|
---|
233 | switch (array->s.type)
|
---|
234 | {
|
---|
235 | case GL_BYTE:
|
---|
236 | crPackSecondaryColor3bvEXT((GLbyte *)p); break;
|
---|
237 | case GL_UNSIGNED_BYTE:
|
---|
238 | crPackSecondaryColor3ubvEXT((GLubyte *)p); break;
|
---|
239 | case GL_SHORT:
|
---|
240 | crPackSecondaryColor3svEXT((GLshort *)p); break;
|
---|
241 | case GL_UNSIGNED_SHORT:
|
---|
242 | crPackSecondaryColor3usvEXT((GLushort *)p); break;
|
---|
243 | case GL_INT:
|
---|
244 | crPackSecondaryColor3ivEXT((GLint *)p); break;
|
---|
245 | case GL_UNSIGNED_INT:
|
---|
246 | crPackSecondaryColor3uivEXT((GLuint *)p); break;
|
---|
247 | case GL_FLOAT:
|
---|
248 | crPackSecondaryColor3fvEXT((GLfloat *)p); break;
|
---|
249 | case GL_DOUBLE:
|
---|
250 | crPackSecondaryColor3dvEXT((GLdouble *)p); break;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | #endif // CR_EXT_secondary_color
|
---|
254 |
|
---|
255 |
|
---|
256 | #ifdef CR_EXT_fog_coord
|
---|
257 | if (array->f.enabled && !(vpEnabled && array->a[VERT_ATTRIB_FOG].enabled))
|
---|
258 | {
|
---|
259 | p = array->f.p + index * array->f.stride;
|
---|
260 |
|
---|
261 | #ifdef CR_ARB_vertex_buffer_object
|
---|
262 | if (array->f.buffer && array->f.buffer->data)
|
---|
263 | {
|
---|
264 | p = (unsigned char *)(array->f.buffer->data) + (unsigned long)p;
|
---|
265 | }
|
---|
266 | #endif
|
---|
267 | crPackFogCoordfEXT( *((GLfloat *) p) );
|
---|
268 | }
|
---|
269 | #endif // CR_EXT_fog_coord
|
---|
270 |
|
---|
271 | for (unit = 0 ; unit < CR_MAX_TEXTURE_UNITS ; unit++)
|
---|
272 | {
|
---|
273 | if (array->t[unit].enabled && !(vpEnabled && array->a[VERT_ATTRIB_TEX0+unit].enabled))
|
---|
274 | {
|
---|
275 | p = array->t[unit].p + index * array->t[unit].stride;
|
---|
276 |
|
---|
277 | #ifdef CR_ARB_vertex_buffer_object
|
---|
278 | if (array->t[unit].buffer && array->t[unit].buffer->data)
|
---|
279 | {
|
---|
280 | p = (unsigned char *)(array->t[unit].buffer->data) + (unsigned long)p;
|
---|
281 | }
|
---|
282 | #endif
|
---|
283 |
|
---|
284 | switch (array->t[unit].type)
|
---|
285 | {
|
---|
286 | case GL_SHORT:
|
---|
287 | switch (array->t[unit].size)
|
---|
288 | {
|
---|
289 | case 1: crPackMultiTexCoord1svARB(GL_TEXTURE0_ARB + unit, (GLshort *)p); break;
|
---|
290 | case 2: crPackMultiTexCoord2svARB(GL_TEXTURE0_ARB + unit, (GLshort *)p); break;
|
---|
291 | case 3: crPackMultiTexCoord3svARB(GL_TEXTURE0_ARB + unit, (GLshort *)p); break;
|
---|
292 | case 4: crPackMultiTexCoord4svARB(GL_TEXTURE0_ARB + unit, (GLshort *)p); break;
|
---|
293 | }
|
---|
294 | break;
|
---|
295 | case GL_INT:
|
---|
296 | switch (array->t[unit].size)
|
---|
297 | {
|
---|
298 | case 1: crPackMultiTexCoord1ivARB(GL_TEXTURE0_ARB + unit, (GLint *)p); break;
|
---|
299 | case 2: crPackMultiTexCoord2ivARB(GL_TEXTURE0_ARB + unit, (GLint *)p); break;
|
---|
300 | case 3: crPackMultiTexCoord3ivARB(GL_TEXTURE0_ARB + unit, (GLint *)p); break;
|
---|
301 | case 4: crPackMultiTexCoord4ivARB(GL_TEXTURE0_ARB + unit, (GLint *)p); break;
|
---|
302 | }
|
---|
303 | break;
|
---|
304 | case GL_FLOAT:
|
---|
305 | switch (array->t[unit].size)
|
---|
306 | {
|
---|
307 | case 1: crPackMultiTexCoord1fvARB(GL_TEXTURE0_ARB + unit, (GLfloat *)p); break;
|
---|
308 | case 2: crPackMultiTexCoord2fvARB(GL_TEXTURE0_ARB + unit, (GLfloat *)p); break;
|
---|
309 | case 3: crPackMultiTexCoord3fvARB(GL_TEXTURE0_ARB + unit, (GLfloat *)p); break;
|
---|
310 | case 4: crPackMultiTexCoord4fvARB(GL_TEXTURE0_ARB + unit, (GLfloat *)p); break;
|
---|
311 | }
|
---|
312 | break;
|
---|
313 | case GL_DOUBLE:
|
---|
314 | switch (array->t[unit].size)
|
---|
315 | {
|
---|
316 | case 1: crPackMultiTexCoord1dvARB(GL_TEXTURE0_ARB + unit, (GLdouble *)p); break;
|
---|
317 | case 2: crPackMultiTexCoord2dvARB(GL_TEXTURE0_ARB + unit, (GLdouble *)p); break;
|
---|
318 | case 3: crPackMultiTexCoord3dvARB(GL_TEXTURE0_ARB + unit, (GLdouble *)p); break;
|
---|
319 | case 4: crPackMultiTexCoord4dvARB(GL_TEXTURE0_ARB + unit, (GLdouble *)p); break;
|
---|
320 | }
|
---|
321 | break;
|
---|
322 | }
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | if (array->i.enabled)
|
---|
327 | {
|
---|
328 | p = array->i.p + index * array->i.stride;
|
---|
329 |
|
---|
330 | #ifdef CR_ARB_vertex_buffer_object
|
---|
331 | if (array->i.buffer && array->i.buffer->data)
|
---|
332 | {
|
---|
333 | p = (unsigned char *)(array->i.buffer->data) + (unsigned long)p;
|
---|
334 | }
|
---|
335 | #endif
|
---|
336 |
|
---|
337 | switch (array->i.type)
|
---|
338 | {
|
---|
339 | case GL_SHORT: crPackIndexsv((GLshort *)p); break;
|
---|
340 | case GL_INT: crPackIndexiv((GLint *)p); break;
|
---|
341 | case GL_FLOAT: crPackIndexfv((GLfloat *)p); break;
|
---|
342 | case GL_DOUBLE: crPackIndexdv((GLdouble *)p); break;
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | if (array->e.enabled)
|
---|
347 | {
|
---|
348 | p = array->e.p + index * array->e.stride;
|
---|
349 |
|
---|
350 | #ifdef CR_ARB_vertex_buffer_object
|
---|
351 | if (array->e.buffer && array->e.buffer->data)
|
---|
352 | {
|
---|
353 | p = (unsigned char *)(array->e.buffer->data) + (unsigned long)p;
|
---|
354 | }
|
---|
355 | #endif
|
---|
356 |
|
---|
357 | crPackEdgeFlagv(p);
|
---|
358 | }
|
---|
359 |
|
---|
360 | for (attr = 1; attr < VERT_ATTRIB_MAX; attr++)
|
---|
361 | {
|
---|
362 | if (array->a[attr].enabled)
|
---|
363 | {
|
---|
364 | crPackVertexAttrib(array, attr, index);
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | if (array->a[VERT_ATTRIB_POS].enabled)
|
---|
369 | {
|
---|
370 | crPackVertexAttrib(array, 0, index);
|
---|
371 | }
|
---|
372 | else if (array->v.enabled)
|
---|
373 | {
|
---|
374 | p = array->v.p + index * array->v.stride;
|
---|
375 |
|
---|
376 | #ifdef CR_ARB_vertex_buffer_object
|
---|
377 | if (array->v.buffer && array->v.buffer->data)
|
---|
378 | {
|
---|
379 | p = (unsigned char *)(array->v.buffer->data) + (unsigned long)p;
|
---|
380 | }
|
---|
381 | #endif
|
---|
382 | switch (array->v.type)
|
---|
383 | {
|
---|
384 | case GL_SHORT:
|
---|
385 | switch (c->array.v.size)
|
---|
386 | {
|
---|
387 | case 2: crPackVertex2sv((GLshort *)p); break;
|
---|
388 | case 3: crPackVertex3sv((GLshort *)p); break;
|
---|
389 | case 4: crPackVertex4sv((GLshort *)p); break;
|
---|
390 | }
|
---|
391 | break;
|
---|
392 | case GL_INT:
|
---|
393 | switch (c->array.v.size)
|
---|
394 | {
|
---|
395 | case 2: crPackVertex2iv((GLint *)p); break;
|
---|
396 | case 3: crPackVertex3iv((GLint *)p); break;
|
---|
397 | case 4: crPackVertex4iv((GLint *)p); break;
|
---|
398 | }
|
---|
399 | break;
|
---|
400 | case GL_FLOAT:
|
---|
401 | switch (c->array.v.size)
|
---|
402 | {
|
---|
403 | case 2: crPackVertex2fv((GLfloat *)p); break;
|
---|
404 | case 3: crPackVertex3fv((GLfloat *)p); break;
|
---|
405 | case 4: crPackVertex4fv((GLfloat *)p); break;
|
---|
406 | }
|
---|
407 | break;
|
---|
408 | case GL_DOUBLE:
|
---|
409 | switch (c->array.v.size)
|
---|
410 | {
|
---|
411 | case 2: crPackVertex2dv((GLdouble *)p); break;
|
---|
412 | case 3: crPackVertex3dv((GLdouble *)p); break;
|
---|
413 | case 4: crPackVertex4dv((GLdouble *)p); break;
|
---|
414 | }
|
---|
415 | break;
|
---|
416 | }
|
---|
417 | }
|
---|
418 | }
|
---|
419 |
|
---|
420 |
|
---|
421 | void
|
---|
422 | crPackExpandDrawArrays(GLenum mode, GLint first, GLsizei count, CRClientState *c)
|
---|
423 | {
|
---|
424 | int i;
|
---|
425 |
|
---|
426 | if (count < 0)
|
---|
427 | {
|
---|
428 | __PackError(__LINE__, __FILE__, GL_INVALID_VALUE, "crPackDrawArrays(negative count)");
|
---|
429 | return;
|
---|
430 | }
|
---|
431 |
|
---|
432 | if (mode > GL_POLYGON)
|
---|
433 | {
|
---|
434 | __PackError(__LINE__, __FILE__, GL_INVALID_ENUM, "crPackDrawArrays(bad mode)");
|
---|
435 | return;
|
---|
436 | }
|
---|
437 |
|
---|
438 | crPackBegin(mode);
|
---|
439 | for (i=0; i<count; i++)
|
---|
440 | {
|
---|
441 | crPackExpandArrayElement(first + i, c);
|
---|
442 | }
|
---|
443 | crPackEnd();
|
---|
444 | }
|
---|
445 |
|
---|
446 | static GLsizei crPackElementsIndexSize(GLenum type)
|
---|
447 | {
|
---|
448 | switch (type)
|
---|
449 | {
|
---|
450 | case GL_UNSIGNED_BYTE:
|
---|
451 | return sizeof(GLubyte);
|
---|
452 | case GL_UNSIGNED_SHORT:
|
---|
453 | return sizeof(GLushort);
|
---|
454 | case GL_UNSIGNED_INT:
|
---|
455 | return sizeof(GLuint);
|
---|
456 | default:
|
---|
457 | crError("Unknown type 0x%x in crPackElementsIndexSize", type);
|
---|
458 | return 0;
|
---|
459 | }
|
---|
460 | }
|
---|
461 |
|
---|
462 | void PACK_APIENTRY
|
---|
463 | crPackDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
|
---|
464 | {
|
---|
465 | unsigned char *data_ptr, *start_ptr;
|
---|
466 | int packet_length = sizeof(int) + sizeof(mode) + sizeof(count) + sizeof(type) + sizeof(GLintptrARB);
|
---|
467 | GLsizei indexsize;
|
---|
468 | #ifdef CR_ARB_vertex_buffer_object
|
---|
469 | CRBufferObject *elementsBuffer = crStateGetCurrent()->bufferobject.elementsBuffer;
|
---|
470 | packet_length += sizeof(GLboolean);
|
---|
471 | if (elementsBuffer && elementsBuffer->name)
|
---|
472 | {
|
---|
473 | /*@todo not sure it's possible, and not sure what to do*/
|
---|
474 | if (!elementsBuffer->data)
|
---|
475 | {
|
---|
476 | crWarning("crPackDrawElements: trying to use bound but empty elements buffer, ignoring.");
|
---|
477 | return;
|
---|
478 | }
|
---|
479 | indexsize = 0;
|
---|
480 | }
|
---|
481 | else
|
---|
482 | #endif
|
---|
483 | {
|
---|
484 | indexsize = crPackElementsIndexSize(type);
|
---|
485 | }
|
---|
486 |
|
---|
487 | packet_length += count * indexsize;
|
---|
488 |
|
---|
489 | start_ptr = data_ptr = (unsigned char *) crPackAlloc(packet_length);
|
---|
490 | WRITE_DATA_AI(GLenum, CR_DRAWELEMENTS_EXTEND_OPCODE );
|
---|
491 | WRITE_DATA_AI(GLenum, mode );
|
---|
492 | WRITE_DATA_AI(GLsizei, count);
|
---|
493 | WRITE_DATA_AI(GLenum, type);
|
---|
494 | WRITE_DATA_AI(GLsizeiptrARB, (GLsizeiptrARB) indices );
|
---|
495 | #ifdef CR_ARB_vertex_buffer_object
|
---|
496 | WRITE_DATA_AI(GLboolean, (GLboolean) indexsize>0);
|
---|
497 | #endif
|
---|
498 | if (indexsize>0)
|
---|
499 | {
|
---|
500 | crMemcpy(data_ptr, indices, count * indexsize);
|
---|
501 | }
|
---|
502 | crHugePacket(CR_EXTEND_OPCODE, start_ptr);
|
---|
503 | crPackFree(start_ptr);
|
---|
504 | }
|
---|
505 |
|
---|
506 | void PACK_APIENTRY
|
---|
507 | crPackDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
|
---|
508 | GLenum type, const GLvoid *indices)
|
---|
509 | {
|
---|
510 | unsigned char *data_ptr, *start_ptr;
|
---|
511 | int packet_length = sizeof(int) + sizeof(mode) + sizeof(start)
|
---|
512 | + sizeof(end) + sizeof(count) + sizeof(type) + sizeof(GLintptrARB);
|
---|
513 | GLsizei indexsize;
|
---|
514 |
|
---|
515 | #ifdef CR_ARB_vertex_buffer_object
|
---|
516 | CRBufferObject *elementsBuffer = crStateGetCurrent()->bufferobject.elementsBuffer;
|
---|
517 | packet_length += sizeof(GLboolean);
|
---|
518 | if (elementsBuffer && elementsBuffer->name)
|
---|
519 | {
|
---|
520 | /*@todo not sure it's possible, and not sure what to do*/
|
---|
521 | if (!elementsBuffer->data)
|
---|
522 | {
|
---|
523 | crWarning("crPackDrawElements: trying to use bound but empty elements buffer, ignoring.");
|
---|
524 | return;
|
---|
525 | }
|
---|
526 | indexsize = 0;
|
---|
527 | }
|
---|
528 | else
|
---|
529 | #endif
|
---|
530 | {
|
---|
531 | indexsize = crPackElementsIndexSize(type);
|
---|
532 | }
|
---|
533 |
|
---|
534 | packet_length += count * indexsize;
|
---|
535 |
|
---|
536 | start_ptr = data_ptr = (unsigned char *) crPackAlloc(packet_length);
|
---|
537 | WRITE_DATA_AI(GLenum, CR_DRAWRANGEELEMENTS_EXTEND_OPCODE);
|
---|
538 | WRITE_DATA_AI(GLenum, mode);
|
---|
539 | WRITE_DATA_AI(GLuint, start);
|
---|
540 | WRITE_DATA_AI(GLuint, end);
|
---|
541 | WRITE_DATA_AI(GLsizei, count);
|
---|
542 | WRITE_DATA_AI(GLenum, type);
|
---|
543 | WRITE_DATA_AI(GLsizeiptrARB, (GLsizeiptr) indices);
|
---|
544 | #ifdef CR_ARB_vertex_buffer_object
|
---|
545 | WRITE_DATA_AI(GLboolean, (GLboolean) indexsize>0);
|
---|
546 | #endif
|
---|
547 | if (indexsize>0)
|
---|
548 | {
|
---|
549 | crMemcpy(data_ptr, indices, count * indexsize);
|
---|
550 | }
|
---|
551 | crHugePacket(CR_EXTEND_OPCODE, start_ptr);
|
---|
552 | crPackFree(start_ptr);
|
---|
553 | }
|
---|
554 |
|
---|
555 |
|
---|
556 | /**
|
---|
557 | * Expand glDrawElements into crPackBegin/Vertex/End, etc commands.
|
---|
558 | * Note: if mode==999, don't call glBegin/glEnd.
|
---|
559 | */
|
---|
560 | void
|
---|
561 | crPackExpandDrawElements(GLenum mode, GLsizei count, GLenum type,
|
---|
562 | const GLvoid *indices, CRClientState *c)
|
---|
563 | {
|
---|
564 | int i;
|
---|
565 | GLubyte *p = (GLubyte *)indices;
|
---|
566 | #ifdef CR_ARB_vertex_buffer_object
|
---|
567 | CRBufferObject *elementsBuffer = crStateGetCurrent()->bufferobject.elementsBuffer;
|
---|
568 | #endif
|
---|
569 |
|
---|
570 | if (count < 0)
|
---|
571 | {
|
---|
572 | __PackError(__LINE__, __FILE__, GL_INVALID_VALUE,
|
---|
573 | "crPackDrawElements(negative count)");
|
---|
574 | return;
|
---|
575 | }
|
---|
576 |
|
---|
577 | if (mode > GL_POLYGON && mode != 999)
|
---|
578 | {
|
---|
579 | __PackError(__LINE__, __FILE__, GL_INVALID_ENUM,
|
---|
580 | "crPackDrawElements(bad mode)");
|
---|
581 | return;
|
---|
582 | }
|
---|
583 |
|
---|
584 | if (type != GL_UNSIGNED_BYTE &&
|
---|
585 | type != GL_UNSIGNED_SHORT &&
|
---|
586 | type != GL_UNSIGNED_INT)
|
---|
587 | {
|
---|
588 | __PackError(__LINE__, __FILE__, GL_INVALID_ENUM,
|
---|
589 | "crPackDrawElements(bad type)");
|
---|
590 | return;
|
---|
591 | }
|
---|
592 |
|
---|
593 | #ifdef CR_ARB_vertex_buffer_object
|
---|
594 | if (elementsBuffer && elementsBuffer->data)
|
---|
595 | {
|
---|
596 | p = (unsigned char *)(elementsBuffer->data) + (unsigned long)p;
|
---|
597 | }
|
---|
598 | #endif
|
---|
599 |
|
---|
600 | if (mode != 999)
|
---|
601 | crPackBegin(mode);
|
---|
602 |
|
---|
603 | //crDebug("crPackExpandDrawElements mode:0x%x, count:%d, type:0x%x", mode, count, type);
|
---|
604 |
|
---|
605 | switch (type)
|
---|
606 | {
|
---|
607 | case GL_UNSIGNED_BYTE:
|
---|
608 | for (i=0; i<count; i++)
|
---|
609 | {
|
---|
610 | crPackExpandArrayElement((GLint) *p++, c);
|
---|
611 | }
|
---|
612 | break;
|
---|
613 | case GL_UNSIGNED_SHORT:
|
---|
614 | for (i=0; i<count; i++)
|
---|
615 | {
|
---|
616 | crPackExpandArrayElement((GLint) * (GLushort *) p, c);
|
---|
617 | p+=sizeof (GLushort);
|
---|
618 | }
|
---|
619 | break;
|
---|
620 | case GL_UNSIGNED_INT:
|
---|
621 | for (i=0; i<count; i++)
|
---|
622 | {
|
---|
623 | crPackExpandArrayElement((GLint) * (GLuint *) p, c);
|
---|
624 | p+=sizeof (GLuint);
|
---|
625 | }
|
---|
626 | break;
|
---|
627 | default:
|
---|
628 | crError( "this can't happen: array_spu.self.DrawElements" );
|
---|
629 | break;
|
---|
630 | }
|
---|
631 |
|
---|
632 | if (mode != 999)
|
---|
633 | crPackEnd();
|
---|
634 | }
|
---|
635 |
|
---|
636 |
|
---|
637 | /**
|
---|
638 | * Convert a glDrawElements command into a sequence of ArrayElement() calls.
|
---|
639 | * NOTE: Caller must issue the glBegin/glEnd.
|
---|
640 | */
|
---|
641 | void
|
---|
642 | crPackUnrollDrawElements(GLsizei count, GLenum type,
|
---|
643 | const GLvoid *indices)
|
---|
644 | {
|
---|
645 | int i;
|
---|
646 |
|
---|
647 | switch (type) {
|
---|
648 | case GL_UNSIGNED_BYTE:
|
---|
649 | {
|
---|
650 | const GLubyte *p = (const GLubyte *) indices;
|
---|
651 | for (i = 0; i < count; i++)
|
---|
652 | crPackArrayElement(p[i]);
|
---|
653 | }
|
---|
654 | break;
|
---|
655 | case GL_UNSIGNED_SHORT:
|
---|
656 | {
|
---|
657 | const GLushort *p = (const GLushort *) indices;
|
---|
658 | for (i = 0; i < count; i++)
|
---|
659 | crPackArrayElement(p[i]);
|
---|
660 | }
|
---|
661 | break;
|
---|
662 | case GL_UNSIGNED_INT:
|
---|
663 | {
|
---|
664 | const GLuint *p = (const GLuint *) indices;
|
---|
665 | for (i = 0; i < count; i++)
|
---|
666 | crPackArrayElement(p[i]);
|
---|
667 | }
|
---|
668 | break;
|
---|
669 | default:
|
---|
670 | __PackError(__LINE__, __FILE__, GL_INVALID_ENUM,
|
---|
671 | "crPackUnrollDrawElements(bad type)");
|
---|
672 | }
|
---|
673 | }
|
---|
674 |
|
---|
675 |
|
---|
676 |
|
---|
677 | /*
|
---|
678 | * glDrawRangeElements, expanded into crPackBegin/Vertex/End/etc.
|
---|
679 | */
|
---|
680 | void
|
---|
681 | crPackExpandDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c)
|
---|
682 | {
|
---|
683 | if (start>end)
|
---|
684 | {
|
---|
685 | crWarning("crPackExpandDrawRangeElements start>end (%d>%d)", start, end);
|
---|
686 | return;
|
---|
687 | }
|
---|
688 |
|
---|
689 | crPackExpandDrawElements(mode, count, type, indices, c);
|
---|
690 | }
|
---|
691 |
|
---|
692 |
|
---|
693 | #ifdef CR_EXT_multi_draw_arrays
|
---|
694 | /*
|
---|
695 | * Pack real DrawArrays commands.
|
---|
696 | */
|
---|
697 | void PACK_APIENTRY
|
---|
698 | crPackMultiDrawArraysEXT( GLenum mode, GLint *first, GLsizei *count,
|
---|
699 | GLsizei primcount )
|
---|
700 | {
|
---|
701 | GLint i;
|
---|
702 | for (i = 0; i < primcount; i++) {
|
---|
703 | if (count[i] > 0) {
|
---|
704 | crPackDrawArrays(mode, first[i], count[i]);
|
---|
705 | }
|
---|
706 | }
|
---|
707 | }
|
---|
708 |
|
---|
709 |
|
---|
710 | /*
|
---|
711 | * Pack with crPackBegin/Vertex/End/etc.
|
---|
712 | */
|
---|
713 | void
|
---|
714 | crPackExpandMultiDrawArraysEXT( GLenum mode, GLint *first, GLsizei *count,
|
---|
715 | GLsizei primcount, CRClientState *c )
|
---|
716 | {
|
---|
717 | GLint i;
|
---|
718 | for (i = 0; i < primcount; i++) {
|
---|
719 | if (count[i] > 0) {
|
---|
720 | crPackExpandDrawArrays(mode, first[i], count[i], c);
|
---|
721 | }
|
---|
722 | }
|
---|
723 | }
|
---|
724 |
|
---|
725 |
|
---|
726 | /*
|
---|
727 | * Pack real DrawElements commands.
|
---|
728 | */
|
---|
729 | void PACK_APIENTRY
|
---|
730 | crPackMultiDrawElementsEXT( GLenum mode, const GLsizei *count, GLenum type,
|
---|
731 | const GLvoid **indices, GLsizei primcount )
|
---|
732 | {
|
---|
733 | GLint i;
|
---|
734 | for (i = 0; i < primcount; i++) {
|
---|
735 | if (count[i] > 0) {
|
---|
736 | crPackDrawElements(mode, count[i], type, indices[i]);
|
---|
737 | }
|
---|
738 | }
|
---|
739 | }
|
---|
740 |
|
---|
741 |
|
---|
742 | /*
|
---|
743 | * Pack with crPackBegin/Vertex/End/etc.
|
---|
744 | */
|
---|
745 | void
|
---|
746 | crPackExpandMultiDrawElementsEXT( GLenum mode, const GLsizei *count,
|
---|
747 | GLenum type, const GLvoid **indices,
|
---|
748 | GLsizei primcount, CRClientState *c )
|
---|
749 | {
|
---|
750 | GLint i;
|
---|
751 | for (i = 0; i < primcount; i++) {
|
---|
752 | if (count[i] > 0) {
|
---|
753 | crPackExpandDrawElements(mode, count[i], type, indices[i], c);
|
---|
754 | }
|
---|
755 | }
|
---|
756 | }
|
---|
757 | #endif /* CR_EXT_multi_draw_arrays */
|
---|