VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_bufferobject.c@ 23123

Last change on this file since 23123 was 23123, checked in by vboxsync, 15 years ago

crOpenGL: snapshots support for VBOs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 28.2 KB
Line 
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 "state.h"
8#include "state/cr_statetypes.h"
9#include "state/cr_statefuncs.h"
10#include "state_internals.h"
11#include "cr_mem.h"
12#include "cr_string.h"
13
14
15static CRBufferObject *AllocBufferObject(GLuint name)
16{
17 CRBufferObject *b = crCalloc(sizeof(CRBufferObject));
18 if (b) {
19 b->refCount = 1;
20 b->name = name;
21 b->usage = GL_STATIC_DRAW_ARB;
22 b->access = GL_READ_WRITE_ARB;
23 }
24 return b;
25}
26
27
28void crStateBufferObjectInit (CRContext *ctx)
29{
30 CRStateBits *sb = GetCurrentBits();
31 CRBufferObjectBits *bb = &sb->bufferobject;
32 CRBufferObjectState *b = &ctx->bufferobject;
33
34 RESET(bb->dirty, ctx->bitid);
35 RESET(bb->arrayBinding, ctx->bitid);
36 RESET(bb->elementsBinding, ctx->bitid);
37
38#ifdef IN_GUEST
39 b->retainBufferData = GL_TRUE;
40#else
41 b->retainBufferData = GL_FALSE;
42#endif
43
44 b->nullBuffer = AllocBufferObject(0);
45 b->arrayBuffer = b->nullBuffer;
46 b->elementsBuffer = b->nullBuffer;
47 b->nullBuffer->refCount = 3;
48
49 b->buffers = crAllocHashtable();
50
51 b->bResyncNeeded = GL_FALSE;
52}
53
54
55static void crStateFreeBufferObject(void *data)
56{
57 CRBufferObject *pObj = (CRBufferObject *)data;
58 if (pObj->data) crFree(pObj->data);
59
60#ifndef IN_GUEST
61 if (diff_api.DeleteBuffersARB)
62 {
63 diff_api.DeleteBuffersARB(1, &pObj->name);
64 }
65#endif
66
67 crFree(pObj);
68}
69
70void crStateBufferObjectDestroy (CRContext *ctx)
71{
72 CRBufferObjectState *b = &ctx->bufferobject;
73 crFreeHashtable(b->buffers, crStateFreeBufferObject);
74 crFree(b->nullBuffer);
75}
76
77void STATE_APIENTRY
78crStateBindBufferARB (GLenum target, GLuint buffer)
79{
80 CRContext *g = GetCurrentContext();
81 CRBufferObjectState *b = &(g->bufferobject);
82 CRStateBits *sb = GetCurrentBits();
83 CRBufferObjectBits *bb = &(sb->bufferobject);
84 CRBufferObject *oldObj, *newObj;
85
86 if (g->current.inBeginEnd) {
87 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
88 "glBindBufferARB called in begin/end");
89 return;
90 }
91
92 FLUSH();
93
94 if (target == GL_ARRAY_BUFFER_ARB) {
95 oldObj = b->arrayBuffer;
96 }
97 else if (target == GL_ELEMENT_ARRAY_BUFFER_ARB) {
98 oldObj = b->elementsBuffer;
99 }
100 else {
101 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
102 "glBindBufferARB(target)");
103 return;
104 }
105
106 if (buffer == 0) {
107 newObj = b->nullBuffer;
108 }
109 else {
110 newObj = (CRBufferObject *) crHashtableSearch(b->buffers, buffer);
111 if (!newObj) {
112 newObj = AllocBufferObject(buffer);
113 if (!newObj) {
114 crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY, "glBindBuffer");
115 return;
116 }
117 crHashtableAdd( b->buffers, buffer, newObj );
118 }
119 }
120
121 newObj->refCount++;
122 oldObj->refCount--;
123
124 if (target == GL_ARRAY_BUFFER_ARB) {
125 b->arrayBuffer = newObj;
126 DIRTY(bb->dirty, g->neg_bitid);
127 DIRTY(bb->arrayBinding, g->neg_bitid);
128 }
129 else {
130 CRASSERT(target == GL_ELEMENT_ARRAY_BUFFER_ARB);
131 b->elementsBuffer = newObj;
132 DIRTY(bb->dirty, g->neg_bitid);
133 DIRTY(bb->elementsBinding, g->neg_bitid);
134 }
135
136 if (oldObj->refCount <= 0) {
137 /*we shouldn't reach this point*/
138 CRASSERT(false);
139 crHashtableDelete(b->buffers, (unsigned long) oldObj->name, crStateFreeBufferObject);
140 }
141}
142
143void STATE_APIENTRY
144crStateDeleteBuffersARB(GLsizei n, const GLuint *buffers)
145{
146 CRContext *g = GetCurrentContext();
147 CRBufferObjectState *b = &(g->bufferobject);
148 CRStateBits *sb = GetCurrentBits();
149 CRBufferObjectBits *bb = &(sb->bufferobject);
150 int i;
151
152 FLUSH();
153
154 if (g->current.inBeginEnd) {
155 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
156 "glDeleteBuffersARB called in Begin/End");
157 return;
158 }
159
160 if (n < 0) {
161 crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
162 "glDeleteBuffersARB(n < 0)");
163 return;
164 }
165
166 for (i = 0; i < n; i++) {
167 if (buffers[i]) {
168 CRBufferObject *obj = (CRBufferObject *)
169 crHashtableSearch(b->buffers, buffers[i]);
170 if (obj) {
171 if (obj == b->arrayBuffer) {
172 b->arrayBuffer = b->nullBuffer;
173 b->arrayBuffer->refCount++;
174 DIRTY(bb->dirty, g->neg_bitid);
175 DIRTY(bb->arrayBinding, g->neg_bitid);
176 }
177 if (obj == b->elementsBuffer) {
178 b->elementsBuffer = b->nullBuffer;
179 b->elementsBuffer->refCount++;
180 DIRTY(bb->dirty, g->neg_bitid);
181 DIRTY(bb->elementsBinding, g->neg_bitid);
182 }
183 /* @todo check bindings with the vertex arrays */
184
185 crHashtableDelete(b->buffers, buffers[i], crStateFreeBufferObject);
186 }
187 }
188 }
189}
190
191
192void STATE_APIENTRY
193crStateGenBuffersARB(GLsizei n, GLuint * buffers)
194{
195 CRContext *g = GetCurrentContext();
196 CRBufferObjectState *b = &(g->bufferobject);
197 GLint start;
198
199 FLUSH();
200
201 if (g->current.inBeginEnd) {
202 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
203 "glGenBuffersARB called in Begin/End");
204 return;
205 }
206
207 if (n < 0) {
208 crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
209 "glGenBuffersARB(n < 0)");
210 return;
211 }
212
213 start = crHashtableAllocKeys(b->buffers, n);
214 if (start) {
215 GLint i;
216 for (i = 0; i < n; i++)
217 buffers[i] = (GLuint) (start + i);
218 }
219 else {
220 crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY, "glGenBuffersARB");
221 }
222}
223
224
225GLboolean STATE_APIENTRY
226crStateIsBufferARB(GLuint buffer)
227{
228 CRContext *g = GetCurrentContext();
229 CRBufferObjectState *b = &g->bufferobject;
230
231 FLUSH();
232
233 if (g->current.inBeginEnd) {
234 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
235 "glIsBufferARB called in begin/end");
236 return GL_FALSE;
237 }
238
239 if (buffer && crHashtableSearch(b->buffers, buffer))
240 return GL_TRUE;
241 else
242 return GL_FALSE;
243}
244
245
246void STATE_APIENTRY
247crStateBufferDataARB(GLenum target, GLsizeiptrARB size, const GLvoid * data, GLenum usage)
248{
249 CRContext *g = GetCurrentContext();
250 CRBufferObjectState *b = &g->bufferobject;
251 CRBufferObject *obj;
252 CRStateBits *sb = GetCurrentBits();
253 CRBufferObjectBits *bb = &sb->bufferobject;
254
255 FLUSH();
256
257 if (g->current.inBeginEnd) {
258 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
259 "glBufferDataARB called in begin/end");
260 return;
261 }
262
263 if (size < 0) {
264 crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
265 "glBufferDataARB(size < 0)");
266 return;
267 }
268
269 switch (usage) {
270 case GL_STREAM_DRAW_ARB:
271 case GL_STREAM_READ_ARB:
272 case GL_STREAM_COPY_ARB:
273 case GL_STATIC_DRAW_ARB:
274 case GL_STATIC_READ_ARB:
275 case GL_STATIC_COPY_ARB:
276 case GL_DYNAMIC_DRAW_ARB:
277 case GL_DYNAMIC_READ_ARB:
278 case GL_DYNAMIC_COPY_ARB:
279 /* OK */
280 break;
281 default:
282 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
283 "glBufferDataARB(usage)");
284 return;
285 }
286
287 if (target == GL_ARRAY_BUFFER_ARB) {
288 obj = b->arrayBuffer;
289 }
290 else if (target == GL_ELEMENT_ARRAY_BUFFER_ARB) {
291 obj = b->elementsBuffer;
292 }
293 else {
294 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
295 "glBufferDataARB(target)");
296 return;
297 }
298
299 if (obj->name == 0) {
300 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glBufferDataARB");
301 return;
302 }
303
304 if (obj->pointer) {
305 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
306 "glBufferDataARB(buffer is mapped)");
307 return;
308 }
309
310 obj->usage = usage;
311 obj->size = size;
312
313 /* The user of the state tracker should set the retainBufferData field
314 * during context initialization, if needed.
315 */
316 if (b->retainBufferData) {
317 if (obj->data) {
318 crFree(obj->data);
319 }
320
321 obj->data = crAlloc(size);
322 if (!obj->data) {
323 crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY, "glBufferDataARB");
324 return;
325 }
326 if (data)
327 crMemcpy(obj->data, data, size);
328 }
329
330 DIRTY(bb->dirty, g->neg_bitid);
331 DIRTY(obj->dirty, g->neg_bitid);
332 obj->dirtyStart = 0;
333 obj->dirtyLength = size;
334}
335
336
337void STATE_APIENTRY
338crStateBufferSubDataARB(GLenum target, GLintptrARB offset, GLsizeiptrARB size, const GLvoid * data)
339{
340 CRContext *g = GetCurrentContext();
341 CRBufferObjectState *b = &g->bufferobject;
342 CRBufferObject *obj;
343 CRStateBits *sb = GetCurrentBits();
344 CRBufferObjectBits *bb = &sb->bufferobject;
345
346 FLUSH();
347
348 if (g->current.inBeginEnd) {
349 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
350 "glBufferSubDataARB called in begin/end");
351 return;
352 }
353
354 if (target == GL_ARRAY_BUFFER_ARB) {
355 obj = b->arrayBuffer;
356 }
357 else if (target == GL_ELEMENT_ARRAY_BUFFER_ARB) {
358 obj = b->elementsBuffer;
359 }
360 else {
361 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
362 "glBufferSubDataARB(target)");
363 return;
364 }
365
366 if (obj->name == 0) {
367 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
368 "glBufferSubDataARB");
369 return;
370 }
371
372 if (obj->pointer) {
373 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
374 "glBufferSubDataARB(buffer is mapped)");
375 return;
376 }
377
378 if (size < 0 || offset < 0 || (unsigned int)offset + size > obj->size) {
379 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
380 "glBufferSubDataARB(bad offset and/or size)");
381 return;
382 }
383
384 if (b->retainBufferData && obj->data) {
385 crMemcpy((char *) obj->data + offset, data, size);
386 }
387
388 DIRTY(bb->dirty, g->neg_bitid);
389 DIRTY(obj->dirty, g->neg_bitid);
390 /* grow dirty region */
391 if (offset + size > obj->dirtyStart + obj->dirtyLength)
392 obj->dirtyLength = offset + size;
393 if (offset < obj->dirtyStart)
394 obj->dirtyStart = offset;
395}
396
397
398void STATE_APIENTRY
399crStateGetBufferSubDataARB(GLenum target, GLintptrARB offset, GLsizeiptrARB size, void * data)
400{
401 CRContext *g = GetCurrentContext();
402 CRBufferObjectState *b = &g->bufferobject;
403 CRBufferObject *obj;
404
405 FLUSH();
406
407 if (g->current.inBeginEnd) {
408 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
409 "glGetBufferSubDataARB called in begin/end");
410 return;
411 }
412
413 if (target == GL_ARRAY_BUFFER_ARB) {
414 obj = b->arrayBuffer;
415 }
416 else if (target == GL_ELEMENT_ARRAY_BUFFER_ARB) {
417 obj = b->elementsBuffer;
418 }
419 else {
420 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
421 "glGetBufferSubDataARB(target)");
422 return;
423 }
424
425 if (obj->name == 0) {
426 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
427 "glGetBufferSubDataARB");
428 return;
429 }
430
431 if (obj->pointer) {
432 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
433 "glGetBufferSubDataARB(buffer is mapped)");
434 return;
435 }
436
437 if (size < 0 || offset < 0 || (unsigned int)offset + size > obj->size) {
438 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
439 "glGetBufferSubDataARB(bad offset and/or size)");
440 return;
441 }
442
443 if (b->retainBufferData && obj->data) {
444 crMemcpy(data, (char *) obj->data + offset, size);
445 }
446}
447
448
449void * STATE_APIENTRY
450crStateMapBufferARB(GLenum target, GLenum access)
451{
452 CRContext *g = GetCurrentContext();
453 CRBufferObjectState *b = &g->bufferobject;
454 CRBufferObject *obj;
455
456 FLUSH();
457
458 if (g->current.inBeginEnd) {
459 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
460 "glMapBufferARB called in begin/end");
461 return NULL;
462 }
463
464 if (target == GL_ARRAY_BUFFER_ARB) {
465 obj = b->arrayBuffer;
466 }
467 else if (target == GL_ELEMENT_ARRAY_BUFFER_ARB) {
468 obj = b->elementsBuffer;
469 }
470 else {
471 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
472 "glMapBufferARB(target)");
473 return NULL;
474 }
475
476 if (obj->name == 0) {
477 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glMapBufferARB");
478 return GL_FALSE;
479 }
480
481 switch (access) {
482 case GL_READ_ONLY_ARB:
483 case GL_WRITE_ONLY_ARB:
484 case GL_READ_WRITE_ARB:
485 obj->access = access;
486 break;
487 default:
488 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
489 "glMapBufferARB(access)");
490 return NULL;
491 }
492
493 if (b->retainBufferData && obj->data)
494 obj->pointer = obj->data;
495
496 return obj->pointer;
497}
498
499
500GLboolean STATE_APIENTRY
501crStateUnmapBufferARB(GLenum target)
502{
503 CRContext *g = GetCurrentContext();
504 CRBufferObjectState *b = &g->bufferobject;
505 CRBufferObject *obj;
506 CRStateBits *sb = GetCurrentBits();
507 CRBufferObjectBits *bb = &sb->bufferobject;
508
509 FLUSH();
510
511 if (g->current.inBeginEnd) {
512 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
513 "glUnmapBufferARB called in begin/end");
514 return GL_FALSE;
515 }
516
517 if (target == GL_ARRAY_BUFFER_ARB) {
518 obj = b->arrayBuffer;
519 }
520 else if (target == GL_ELEMENT_ARRAY_BUFFER_ARB) {
521 obj = b->elementsBuffer;
522 }
523 else {
524 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
525 "glUnmapBufferARB(target)");
526 return GL_FALSE;
527 }
528
529 if (obj->name == 0) {
530 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glUnmapBufferARB");
531 return GL_FALSE;
532 }
533
534 if (!obj->pointer) {
535 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glUnmapBufferARB");
536 return GL_FALSE;
537 }
538
539 obj->pointer = NULL;
540
541 if (obj->access != GL_READ_ONLY_ARB) {
542 /* the data was most likely modified */
543 DIRTY(bb->dirty, g->neg_bitid);
544 DIRTY(obj->dirty, g->neg_bitid);
545 obj->dirtyStart = 0;
546 obj->dirtyLength = obj->size;
547 }
548
549 return GL_TRUE;
550}
551
552
553void STATE_APIENTRY
554crStateGetBufferParameterivARB(GLenum target, GLenum pname, GLint *params)
555{
556 CRContext *g = GetCurrentContext();
557 CRBufferObjectState *b = &g->bufferobject;
558 CRBufferObject *obj;
559
560 FLUSH();
561
562 if (g->current.inBeginEnd) {
563 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
564 "glGetBufferParameterivARB called in begin/end");
565 return;
566 }
567
568 if (target == GL_ARRAY_BUFFER_ARB) {
569 obj = b->arrayBuffer;
570 }
571 else if (target == GL_ELEMENT_ARRAY_BUFFER_ARB) {
572 obj = b->elementsBuffer;
573 }
574 else {
575 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
576 "glGetBufferParameterivARB(target)");
577 return;
578 }
579
580 switch (pname) {
581 case GL_BUFFER_SIZE_ARB:
582 *params = obj->size;
583 break;
584 case GL_BUFFER_USAGE_ARB:
585 *params = obj->usage;
586 break;
587 case GL_BUFFER_ACCESS_ARB:
588 *params = obj->access;
589 break;
590 case GL_BUFFER_MAPPED_ARB:
591 *params = (obj->pointer != NULL);
592 break;
593 default:
594 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
595 "glGetBufferParameterivARB(pname)");
596 return;
597 }
598}
599
600
601void STATE_APIENTRY
602crStateGetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params)
603{
604 CRContext *g = GetCurrentContext();
605 CRBufferObjectState *b = &g->bufferobject;
606 CRBufferObject *obj;
607
608 FLUSH();
609
610 if (g->current.inBeginEnd) {
611 crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
612 "glGetBufferPointervARB called in begin/end");
613 return;
614 }
615
616 if (target == GL_ARRAY_BUFFER_ARB) {
617 obj = b->arrayBuffer;
618 }
619 else if (target == GL_ELEMENT_ARRAY_BUFFER_ARB) {
620 obj = b->elementsBuffer;
621 }
622 else {
623 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
624 "glGetBufferPointervARB(target)");
625 return;
626 }
627
628 if (pname != GL_BUFFER_MAP_POINTER_ARB) {
629 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
630 "glGetBufferPointervARB(pname)");
631 return;
632 }
633
634 *params = obj->pointer;
635}
636
637
638/**
639 * We need to check if the GL_EXT_vertex/pixel_buffer_object extensions
640 * are supported before calling any of the diff_api functions.
641 * This flag indidcates if the extensions is available (1), not available (0)
642 * or needs to be tested for (-1).
643 * If we don't do this, we can segfault inside OpenGL.
644 * Ideally, the render SPU should no-op unsupported GL functions, but
645 * that's a bit complicated.
646 */
647static GLboolean
648HaveBufferObjectExtension(void)
649{
650 static GLint haveBufferObjectExt = -1;
651
652 if (haveBufferObjectExt == -1) {
653 const char *ext;
654 /* XXX this check is temporary. We need to make the tilesort SPU plug
655 * GetString into the diff'ing table in order for this to really work.
656 */
657 if (!diff_api.GetString) {
658 haveBufferObjectExt = 0;
659 return 0;
660 }
661 CRASSERT(diff_api.GetString);
662 ext = (const char *) diff_api.GetString(GL_EXTENSIONS);
663 if (crStrstr(ext, "GL_ARB_vertex_buffer_object") ||
664 crStrstr(ext, "GL_ARB_pixel_buffer_object")) {
665 haveBufferObjectExt = 1;
666 }
667 else {
668 haveBufferObjectExt = 0;
669 }
670 }
671 return haveBufferObjectExt;
672}
673
674
675void crStateBufferObjectDiff(CRBufferObjectBits *bb, CRbitvalue *bitID,
676 CRContext *fromCtx, CRContext *toCtx)
677{
678 CRBufferObjectState *from = &(fromCtx->bufferobject);
679 const CRBufferObjectState *to = &(toCtx->bufferobject);
680
681 if (!HaveBufferObjectExtension())
682 return;
683
684 /* ARRAY_BUFFER binding */
685 if (CHECKDIRTY(bb->arrayBinding, bitID)) {
686 if (from->arrayBuffer != to->arrayBuffer) {
687 GLuint bufferID = to->arrayBuffer ? to->arrayBuffer->name : 0;
688 diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, bufferID);
689 CLEARDIRTY2(bb->arrayBinding, bitID);
690 from->arrayBuffer = to->arrayBuffer;
691 }
692 }
693
694 if (to->arrayBuffer && CHECKDIRTY(to->arrayBuffer->dirty, bitID)) {
695 /* update array buffer data */
696 CRBufferObject *bufObj = to->arrayBuffer;
697 CRASSERT(bufObj);
698 if (bufObj->dirtyStart == 0 && bufObj->dirtyLength == (int) bufObj->size) {
699 /* update whole buffer */
700 diff_api.BufferDataARB(GL_ARRAY_BUFFER_ARB, bufObj->size,
701 bufObj->data, bufObj->usage);
702 }
703 else {
704 /* update sub buffer */
705 diff_api.BufferSubDataARB(GL_ARRAY_BUFFER_ARB,
706 bufObj->dirtyStart,
707 bufObj->dirtyLength,
708 (char *) bufObj->data
709 + bufObj->dirtyStart);
710 }
711 CLEARDIRTY2(bufObj->dirty, bitID);
712#if 0
713 bufObj->dirtyStart = 0;
714 bufObj->dirtyLength = 0;
715#endif
716 }
717
718 /* ELEMENTS_BUFFER binding */
719 if (CHECKDIRTY(bb->elementsBinding, bitID)) {
720 if (from->elementsBuffer != to->elementsBuffer) {
721 GLuint bufferID = to->elementsBuffer ? to->elementsBuffer->name : 0;
722 diff_api.BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferID);
723 CLEARDIRTY2(bb->elementsBinding, bitID);
724 from->elementsBuffer = to->elementsBuffer;
725 }
726 }
727
728 if (to->elementsBuffer && CHECKDIRTY(to->elementsBuffer->dirty, bitID)) {
729 /* update element buffer data */
730 CRBufferObject *bufObj = to->elementsBuffer;
731 CRASSERT(bufObj);
732 if (bufObj->dirtyStart == 0 && bufObj->dirtyLength == (int) bufObj->size) {
733 /* update whole buffer */
734 diff_api.BufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufObj->size,
735 bufObj->data, bufObj->usage);
736 }
737 else {
738 /* update sub buffer */
739 diff_api.BufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
740 bufObj->dirtyStart,
741 bufObj->dirtyLength,
742 (char *) bufObj->data
743 + bufObj->dirtyStart);
744 }
745 CLEARDIRTY2(bufObj->dirty, bitID);
746#if 0
747 bufObj->dirtyStart = 0;
748 bufObj->dirtyLength = 0;
749#endif
750 }
751}
752
753static void crStateBufferObjectSyncCB(unsigned long key, void *data1, void *data2)
754{
755 CRBufferObject *pBufferObj = (CRBufferObject *) data1;
756 CRBufferObjectState *pState = (CRBufferObjectState *) data2;
757
758 if (pBufferObj->data)
759 {
760 diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, pBufferObj->name);
761 diff_api.BufferDataARB(GL_ARRAY_BUFFER_ARB, pBufferObj->size, pBufferObj->data, pBufferObj->usage);
762
763 if (!pState->retainBufferData)
764 {
765 crFree(pBufferObj->data);
766 pBufferObj->data = NULL;
767 }
768 }
769}
770
771/*
772 * XXX this function might need some testing/fixing.
773 */
774void crStateBufferObjectSwitch(CRBufferObjectBits *bb, CRbitvalue *bitID,
775 CRContext *fromCtx, CRContext *toCtx)
776{
777 const CRBufferObjectState *from = &(fromCtx->bufferobject);
778 CRBufferObjectState *to = &(toCtx->bufferobject);
779 int i;
780
781 if (!HaveBufferObjectExtension())
782 return;
783
784 if (to->bResyncNeeded)
785 {
786 CRClientPointer *cp;
787
788 crHashtableWalk(to->buffers, crStateBufferObjectSyncCB, to);
789 to->bResyncNeeded = GL_FALSE;
790
791 /*@todo, move to state_client.c*/
792
793 cp = &toCtx->client.array.v;
794 if (cp->buffer->name)
795 {
796 diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->name);
797 diff_api.VertexPointer(cp->size, cp->type, cp->stride, cp->p);
798 }
799
800 cp = &toCtx->client.array.c;
801 if (cp->buffer->name)
802 {
803 diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->name);
804 diff_api.ColorPointer(cp->size, cp->type, cp->stride, cp->p);
805 }
806
807 cp = &toCtx->client.array.f;
808 if (cp->buffer->name)
809 {
810 diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->name);
811 diff_api.FogCoordPointerEXT(cp->type, cp->stride, cp->p);
812 }
813
814 cp = &toCtx->client.array.s;
815 if (cp->buffer->name)
816 {
817 diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->name);
818 diff_api.SecondaryColorPointerEXT(cp->size, cp->type, cp->stride, cp->p);
819 }
820
821 cp = &toCtx->client.array.e;
822 if (cp->buffer->name)
823 {
824 diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->name);
825 diff_api.EdgeFlagPointer(cp->stride, cp->p);
826 }
827
828 cp = &toCtx->client.array.i;
829 if (cp->buffer->name)
830 {
831 diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->name);
832 diff_api.IndexPointer(cp->type, cp->stride, cp->p);
833 }
834
835 cp = &toCtx->client.array.n;
836 if (cp->buffer->name)
837 {
838 diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->name);
839 diff_api.NormalPointer(cp->type, cp->stride, cp->p);
840 }
841
842 for (i = 0; i < CR_MAX_TEXTURE_UNITS; i++)
843 {
844 cp = &toCtx->client.array.t[i];
845 if (cp->buffer->name)
846 {
847 if (diff_api.ActiveTextureARB)
848 diff_api.ActiveTextureARB(i+GL_TEXTURE0_ARB);
849 diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->name);
850 diff_api.TexCoordPointer(cp->size, cp->type, cp->stride, cp->p);
851 }
852 }
853
854 if (diff_api.ActiveTextureARB)
855 diff_api.ActiveTextureARB(toCtx->client.curClientTextureUnit+GL_TEXTURE0_ARB);
856
857#ifdef CR_NV_vertex_program
858 for (i = 0; i < CR_MAX_VERTEX_ATTRIBS; i++)
859 {
860 cp = &toCtx->client.array.a[i];
861 if (cp->buffer->name)
862 {
863 diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->name);
864 diff_api.VertexAttribPointerARB(i, cp->size, cp->type, cp->normalized, cp->stride, cp->p);
865 }
866 }
867#endif
868 diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, to->arrayBuffer->name);
869 diff_api.BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, to->elementsBuffer->name);
870 }
871
872 /* ARRAY_BUFFER binding */
873 if (CHECKDIRTY(bb->arrayBinding, bitID)) {
874 if (from->arrayBuffer != to->arrayBuffer) {
875 GLuint bufferID = to->arrayBuffer ? to->arrayBuffer->name : 0;
876 diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, bufferID);
877 FILLDIRTY(bb->arrayBinding);
878 }
879 CLEARDIRTY2(bb->arrayBinding, bitID);
880 }
881
882 if (to->arrayBuffer && CHECKDIRTY(to->arrayBuffer->dirty, bitID)) {
883 /* update array buffer data */
884 CRBufferObject *bufObj = to->arrayBuffer;
885 CRASSERT(bufObj);
886 if (bufObj->dirtyStart == 0 && bufObj->dirtyLength == (int) bufObj->size) {
887 /* update whole buffer */
888 diff_api.BufferDataARB(GL_ARRAY_BUFFER_ARB, bufObj->size,
889 bufObj->data, bufObj->usage);
890 }
891 else {
892 /* update sub buffer */
893 diff_api.BufferSubDataARB(GL_ARRAY_BUFFER_ARB,
894 bufObj->dirtyStart,
895 bufObj->dirtyLength,
896 (char *) bufObj->data
897 + bufObj->dirtyStart);
898 }
899 FILLDIRTY(bufObj->dirty);
900 CLEARDIRTY2(bufObj->dirty, bitID);
901#if 0
902 bufObj->dirtyStart = 0;
903 bufObj->dirtyLength = 0;
904#endif
905 }
906
907 /* ELEMENTS_BUFFER binding */
908 if (CHECKDIRTY(bb->elementsBinding, bitID)) {
909 if (from->elementsBuffer != to->elementsBuffer) {
910 GLuint bufferID = to->elementsBuffer ? to->elementsBuffer->name : 0;
911 diff_api.BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferID);
912 FILLDIRTY(bb->elementsBinding);
913 }
914 CLEARDIRTY2(bb->elementsBinding, bitID);
915 }
916
917 if (to->elementsBuffer && CHECKDIRTY(to->elementsBuffer->dirty, bitID)) {
918 /* update element buffer data */
919 CRBufferObject *bufObj = to->elementsBuffer;
920 CRASSERT(bufObj);
921 if (bufObj->dirtyStart == 0 && bufObj->dirtyLength == (int) bufObj->size) {
922 /* update whole buffer */
923 diff_api.BufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufObj->size,
924 bufObj->data, bufObj->usage);
925 }
926 else {
927 /* update sub buffer */
928 diff_api.BufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
929 bufObj->dirtyStart,
930 bufObj->dirtyLength,
931 (char *) bufObj->data
932 + bufObj->dirtyStart);
933 }
934 FILLDIRTY(bufObj->dirty);
935 CLEARDIRTY2(bufObj->dirty, bitID);
936#if 0
937 bufObj->dirtyStart = 0;
938 bufObj->dirtyLength = 0;
939#endif
940 }
941}
942
943
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette