mesa: Disable certain error checks when transform feedback is paused
[mesa.git] / src / mesa / main / transformfeedback.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2010 VMware, Inc. All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25 /*
26 * Vertex transform feedback support.
27 *
28 * Authors:
29 * Brian Paul
30 */
31
32
33 #include "buffers.h"
34 #include "bufferobj.h"
35 #include "context.h"
36 #include "hash.h"
37 #include "mfeatures.h"
38 #include "mtypes.h"
39 #include "transformfeedback.h"
40 #include "shaderapi.h"
41 #include "shaderobj.h"
42 #include "main/dispatch.h"
43
44 #include "program/prog_parameter.h"
45
46
47 #if FEATURE_EXT_transform_feedback
48
49
50 /**
51 * Do reference counting of transform feedback buffers.
52 */
53 static void
54 reference_transform_feedback_object(struct gl_transform_feedback_object **ptr,
55 struct gl_transform_feedback_object *obj)
56 {
57 if (*ptr == obj)
58 return;
59
60 if (*ptr) {
61 /* Unreference the old object */
62 struct gl_transform_feedback_object *oldObj = *ptr;
63
64 ASSERT(oldObj->RefCount > 0);
65 oldObj->RefCount--;
66
67 if (oldObj->RefCount == 0) {
68 GET_CURRENT_CONTEXT(ctx);
69 if (ctx)
70 ctx->Driver.DeleteTransformFeedback(ctx, oldObj);
71 }
72
73 *ptr = NULL;
74 }
75 ASSERT(!*ptr);
76
77 if (obj) {
78 /* reference new object */
79 if (obj->RefCount == 0) {
80 _mesa_problem(NULL, "referencing deleted transform feedback object");
81 *ptr = NULL;
82 }
83 else {
84 obj->RefCount++;
85 *ptr = obj;
86 }
87 }
88 }
89
90
91 /**
92 * Check if the given primitive mode (as in glBegin(mode)) is compatible
93 * with the current transform feedback mode (if it's enabled).
94 * This is to be called from glBegin(), glDrawArrays(), glDrawElements(), etc.
95 *
96 * \return GL_TRUE if the mode is OK, GL_FALSE otherwise.
97 */
98 GLboolean
99 _mesa_validate_primitive_mode(struct gl_context *ctx, GLenum mode)
100 {
101 if (ctx->TransformFeedback.CurrentObject->Active &&
102 !ctx->TransformFeedback.CurrentObject->Paused) {
103 switch (mode) {
104 case GL_POINTS:
105 return ctx->TransformFeedback.Mode == GL_POINTS;
106 case GL_LINES:
107 case GL_LINE_STRIP:
108 case GL_LINE_LOOP:
109 return ctx->TransformFeedback.Mode == GL_LINES;
110 default:
111 return ctx->TransformFeedback.Mode == GL_TRIANGLES;
112 }
113 }
114 return GL_TRUE;
115 }
116
117
118 /**
119 * Check that all the buffer objects currently bound for transform
120 * feedback actually exist. Raise a GL_INVALID_OPERATION error if
121 * any buffers are missing.
122 * \return GL_TRUE for success, GL_FALSE if error
123 */
124 GLboolean
125 _mesa_validate_transform_feedback_buffers(struct gl_context *ctx)
126 {
127 /* XXX to do */
128 return GL_TRUE;
129 }
130
131
132
133 /**
134 * Per-context init for transform feedback.
135 */
136 void
137 _mesa_init_transform_feedback(struct gl_context *ctx)
138 {
139 /* core mesa expects this, even a dummy one, to be available */
140 ASSERT(ctx->Driver.NewTransformFeedback);
141
142 ctx->TransformFeedback.DefaultObject =
143 ctx->Driver.NewTransformFeedback(ctx, 0);
144
145 assert(ctx->TransformFeedback.DefaultObject->RefCount == 1);
146
147 reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject,
148 ctx->TransformFeedback.DefaultObject);
149
150 assert(ctx->TransformFeedback.DefaultObject->RefCount == 2);
151
152 ctx->TransformFeedback.Objects = _mesa_NewHashTable();
153
154 _mesa_reference_buffer_object(ctx,
155 &ctx->TransformFeedback.CurrentBuffer,
156 ctx->Shared->NullBufferObj);
157 }
158
159
160
161 /**
162 * Callback for _mesa_HashDeleteAll().
163 */
164 static void
165 delete_cb(GLuint key, void *data, void *userData)
166 {
167 struct gl_context *ctx = (struct gl_context *) userData;
168 struct gl_transform_feedback_object *obj =
169 (struct gl_transform_feedback_object *) data;
170
171 ctx->Driver.DeleteTransformFeedback(ctx, obj);
172 }
173
174
175 /**
176 * Per-context free/clean-up for transform feedback.
177 */
178 void
179 _mesa_free_transform_feedback(struct gl_context *ctx)
180 {
181 /* core mesa expects this, even a dummy one, to be available */
182 ASSERT(ctx->Driver.NewTransformFeedback);
183
184 _mesa_reference_buffer_object(ctx,
185 &ctx->TransformFeedback.CurrentBuffer,
186 NULL);
187
188 /* Delete all feedback objects */
189 _mesa_HashDeleteAll(ctx->TransformFeedback.Objects, delete_cb, ctx);
190 _mesa_DeleteHashTable(ctx->TransformFeedback.Objects);
191
192 /* Delete the default feedback object */
193 assert(ctx->Driver.DeleteTransformFeedback);
194 ctx->Driver.DeleteTransformFeedback(ctx,
195 ctx->TransformFeedback.DefaultObject);
196
197 ctx->TransformFeedback.CurrentObject = NULL;
198 }
199
200
201 #else /* FEATURE_EXT_transform_feedback */
202
203 /* forward declarations */
204 static struct gl_transform_feedback_object *
205 new_transform_feedback(struct gl_context *ctx, GLuint name);
206
207 static void
208 delete_transform_feedback(struct gl_context *ctx,
209 struct gl_transform_feedback_object *obj);
210
211 /* dummy per-context init/clean-up for transform feedback */
212 void
213 _mesa_init_transform_feedback(struct gl_context *ctx)
214 {
215 ctx->TransformFeedback.DefaultObject = new_transform_feedback(ctx, 0);
216 ctx->TransformFeedback.CurrentObject = ctx->TransformFeedback.DefaultObject;
217 _mesa_reference_buffer_object(ctx,
218 &ctx->TransformFeedback.CurrentBuffer,
219 ctx->Shared->NullBufferObj);
220 }
221
222 void
223 _mesa_free_transform_feedback(struct gl_context *ctx)
224 {
225 _mesa_reference_buffer_object(ctx,
226 &ctx->TransformFeedback.CurrentBuffer,
227 NULL);
228 ctx->TransformFeedback.CurrentObject = NULL;
229 delete_transform_feedback(ctx, ctx->TransformFeedback.DefaultObject);
230 }
231
232 #endif /* FEATURE_EXT_transform_feedback */
233
234
235 /** Default fallback for ctx->Driver.NewTransformFeedback() */
236 static struct gl_transform_feedback_object *
237 new_transform_feedback(struct gl_context *ctx, GLuint name)
238 {
239 struct gl_transform_feedback_object *obj;
240 obj = CALLOC_STRUCT(gl_transform_feedback_object);
241 if (obj) {
242 obj->Name = name;
243 obj->RefCount = 1;
244 }
245 return obj;
246 }
247
248 /** Default fallback for ctx->Driver.DeleteTransformFeedback() */
249 static void
250 delete_transform_feedback(struct gl_context *ctx,
251 struct gl_transform_feedback_object *obj)
252 {
253 GLuint i;
254
255 for (i = 0; i < Elements(obj->Buffers); i++) {
256 _mesa_reference_buffer_object(ctx, &obj->Buffers[i], NULL);
257 }
258
259 free(obj);
260 }
261
262
263 #if FEATURE_EXT_transform_feedback
264
265
266 /** Default fallback for ctx->Driver.BeginTransformFeedback() */
267 static void
268 begin_transform_feedback(struct gl_context *ctx, GLenum mode,
269 struct gl_transform_feedback_object *obj)
270 {
271 /* nop */
272 }
273
274 /** Default fallback for ctx->Driver.EndTransformFeedback() */
275 static void
276 end_transform_feedback(struct gl_context *ctx,
277 struct gl_transform_feedback_object *obj)
278 {
279 /* nop */
280 }
281
282 /** Default fallback for ctx->Driver.PauseTransformFeedback() */
283 static void
284 pause_transform_feedback(struct gl_context *ctx,
285 struct gl_transform_feedback_object *obj)
286 {
287 /* nop */
288 }
289
290 /** Default fallback for ctx->Driver.ResumeTransformFeedback() */
291 static void
292 resume_transform_feedback(struct gl_context *ctx,
293 struct gl_transform_feedback_object *obj)
294 {
295 /* nop */
296 }
297
298
299 /**
300 * Plug in default device driver functions for transform feedback.
301 * Most drivers will override some/all of these.
302 */
303 void
304 _mesa_init_transform_feedback_functions(struct dd_function_table *driver)
305 {
306 driver->NewTransformFeedback = new_transform_feedback;
307 driver->DeleteTransformFeedback = delete_transform_feedback;
308 driver->BeginTransformFeedback = begin_transform_feedback;
309 driver->EndTransformFeedback = end_transform_feedback;
310 driver->PauseTransformFeedback = pause_transform_feedback;
311 driver->ResumeTransformFeedback = resume_transform_feedback;
312 }
313
314
315 void
316 _mesa_init_transform_feedback_dispatch(struct _glapi_table *disp)
317 {
318 /* EXT_transform_feedback */
319 SET_BeginTransformFeedbackEXT(disp, _mesa_BeginTransformFeedback);
320 SET_EndTransformFeedbackEXT(disp, _mesa_EndTransformFeedback);
321 SET_BindBufferRangeEXT(disp, _mesa_BindBufferRange);
322 SET_BindBufferBaseEXT(disp, _mesa_BindBufferBase);
323 SET_BindBufferOffsetEXT(disp, _mesa_BindBufferOffsetEXT);
324 SET_TransformFeedbackVaryingsEXT(disp, _mesa_TransformFeedbackVaryings);
325 SET_GetTransformFeedbackVaryingEXT(disp, _mesa_GetTransformFeedbackVarying);
326 /* ARB_transform_feedback2 */
327 SET_BindTransformFeedback(disp, _mesa_BindTransformFeedback);
328 SET_DeleteTransformFeedbacks(disp, _mesa_DeleteTransformFeedbacks);
329 SET_GenTransformFeedbacks(disp, _mesa_GenTransformFeedbacks);
330 SET_IsTransformFeedback(disp, _mesa_IsTransformFeedback);
331 SET_PauseTransformFeedback(disp, _mesa_PauseTransformFeedback);
332 SET_ResumeTransformFeedback(disp, _mesa_ResumeTransformFeedback);
333 }
334
335
336 /**
337 ** Begin API functions
338 **/
339
340
341 void GLAPIENTRY
342 _mesa_BeginTransformFeedback(GLenum mode)
343 {
344 struct gl_transform_feedback_object *obj;
345 GET_CURRENT_CONTEXT(ctx);
346
347 obj = ctx->TransformFeedback.CurrentObject;
348
349 switch (mode) {
350 case GL_POINTS:
351 case GL_LINES:
352 case GL_TRIANGLES:
353 /* legal */
354 break;
355 default:
356 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginTransformFeedback(mode)");
357 return;
358 }
359
360 if (obj->Active) {
361 _mesa_error(ctx, GL_INVALID_OPERATION,
362 "glBeginTransformFeedback(already active)");
363 return;
364 }
365
366 FLUSH_VERTICES(ctx, _NEW_TRANSFORM_FEEDBACK);
367 obj->Active = GL_TRUE;
368 ctx->TransformFeedback.Mode = mode;
369
370 assert(ctx->Driver.BeginTransformFeedback);
371 ctx->Driver.BeginTransformFeedback(ctx, mode, obj);
372 }
373
374
375 void GLAPIENTRY
376 _mesa_EndTransformFeedback(void)
377 {
378 struct gl_transform_feedback_object *obj;
379 GET_CURRENT_CONTEXT(ctx);
380
381 obj = ctx->TransformFeedback.CurrentObject;
382
383 if (!obj->Active) {
384 _mesa_error(ctx, GL_INVALID_OPERATION,
385 "glEndTransformFeedback(not active)");
386 return;
387 }
388
389 FLUSH_VERTICES(ctx, _NEW_TRANSFORM_FEEDBACK);
390 ctx->TransformFeedback.CurrentObject->Active = GL_FALSE;
391 ctx->TransformFeedback.CurrentObject->Paused = GL_FALSE;
392 ctx->TransformFeedback.CurrentObject->EndedAnytime = GL_TRUE;
393
394 assert(ctx->Driver.EndTransformFeedback);
395 ctx->Driver.EndTransformFeedback(ctx, obj);
396 }
397
398
399 /**
400 * Helper used by BindBufferRange() and BindBufferBase().
401 */
402 static void
403 bind_buffer_range(struct gl_context *ctx, GLuint index,
404 struct gl_buffer_object *bufObj,
405 GLintptr offset, GLsizeiptr size)
406 {
407 struct gl_transform_feedback_object *obj =
408 ctx->TransformFeedback.CurrentObject;
409
410 /* Note: no need to FLUSH_VERTICES or flag _NEW_TRANSFORM_FEEDBACK, because
411 * transform feedback buffers can't be changed while transform feedback is
412 * active.
413 */
414
415 /* The general binding point */
416 _mesa_reference_buffer_object(ctx,
417 &ctx->TransformFeedback.CurrentBuffer,
418 bufObj);
419
420 /* The per-attribute binding point */
421 _mesa_reference_buffer_object(ctx,
422 &obj->Buffers[index],
423 bufObj);
424
425 obj->BufferNames[index] = bufObj->Name;
426
427 obj->Offset[index] = offset;
428 obj->Size[index] = size;
429 }
430
431
432 /**
433 * Specify a buffer object to receive vertex shader results. Plus,
434 * specify the starting offset to place the results, and max size.
435 */
436 void GLAPIENTRY
437 _mesa_BindBufferRange(GLenum target, GLuint index,
438 GLuint buffer, GLintptr offset, GLsizeiptr size)
439 {
440 struct gl_transform_feedback_object *obj;
441 struct gl_buffer_object *bufObj;
442 GET_CURRENT_CONTEXT(ctx);
443
444 if (target != GL_TRANSFORM_FEEDBACK_BUFFER) {
445 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferRange(target)");
446 return;
447 }
448
449 obj = ctx->TransformFeedback.CurrentObject;
450
451 if (obj->Active) {
452 _mesa_error(ctx, GL_INVALID_OPERATION,
453 "glBindBufferRange(transform feedback active)");
454 return;
455 }
456
457 if (index >= ctx->Const.MaxTransformFeedbackSeparateAttribs) {
458 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
459 return;
460 }
461
462 if ((size <= 0) || (size & 0x3)) {
463 /* must be positive and multiple of four */
464 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size%d)", (int) size);
465 return;
466 }
467
468 if (offset & 0x3) {
469 /* must be multiple of four */
470 _mesa_error(ctx, GL_INVALID_VALUE,
471 "glBindBufferRange(offset=%d)", (int) offset);
472 return;
473 }
474
475 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
476 if (!bufObj) {
477 _mesa_error(ctx, GL_INVALID_OPERATION,
478 "glBindBufferRange(invalid buffer=%u)", buffer);
479 return;
480 }
481
482 if (offset + size > bufObj->Size) {
483 _mesa_error(ctx, GL_INVALID_VALUE,
484 "glBindBufferRange(offset + size %d > buffer size %d)",
485 (int) (offset + size), (int) (bufObj->Size));
486 return;
487 }
488
489 bind_buffer_range(ctx, index, bufObj, offset, size);
490 }
491
492
493 /**
494 * Specify a buffer object to receive vertex shader results.
495 * As above, but start at offset = 0.
496 */
497 void GLAPIENTRY
498 _mesa_BindBufferBase(GLenum target, GLuint index, GLuint buffer)
499 {
500 struct gl_transform_feedback_object *obj;
501 struct gl_buffer_object *bufObj;
502 GLsizeiptr size;
503 GET_CURRENT_CONTEXT(ctx);
504
505 if (target != GL_TRANSFORM_FEEDBACK_BUFFER) {
506 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferBase(target)");
507 return;
508 }
509
510 obj = ctx->TransformFeedback.CurrentObject;
511
512 if (obj->Active) {
513 _mesa_error(ctx, GL_INVALID_OPERATION,
514 "glBindBufferBase(transform feedback active)");
515 return;
516 }
517
518 if (index >= ctx->Const.MaxTransformFeedbackSeparateAttribs) {
519 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
520 return;
521 }
522
523 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
524 if (!bufObj) {
525 _mesa_error(ctx, GL_INVALID_OPERATION,
526 "glBindBufferBase(invalid buffer=%u)", buffer);
527 return;
528 }
529
530 /* default size is the buffer size rounded down to nearest
531 * multiple of four.
532 */
533 size = bufObj->Size & ~0x3;
534
535 bind_buffer_range(ctx, index, bufObj, 0, size);
536 }
537
538
539 /**
540 * Specify a buffer object to receive vertex shader results, plus the
541 * offset in the buffer to start placing results.
542 * This function is part of GL_EXT_transform_feedback, but not GL3.
543 */
544 void GLAPIENTRY
545 _mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer,
546 GLintptr offset)
547 {
548 struct gl_transform_feedback_object *obj;
549 struct gl_buffer_object *bufObj;
550 GET_CURRENT_CONTEXT(ctx);
551 GLsizeiptr size;
552
553 if (target != GL_TRANSFORM_FEEDBACK_BUFFER) {
554 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferOffsetEXT(target)");
555 return;
556 }
557
558 obj = ctx->TransformFeedback.CurrentObject;
559
560 if (obj->Active) {
561 _mesa_error(ctx, GL_INVALID_OPERATION,
562 "glBindBufferOffsetEXT(transform feedback active)");
563 return;
564 }
565
566 if (index >= ctx->Const.MaxTransformFeedbackSeparateAttribs) {
567 _mesa_error(ctx, GL_INVALID_VALUE,
568 "glBindBufferOffsetEXT(index=%d)", index);
569 return;
570 }
571
572 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
573 if (!bufObj) {
574 _mesa_error(ctx, GL_INVALID_OPERATION,
575 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer);
576 return;
577 }
578
579 /* default size is the buffer size rounded down to nearest
580 * multiple of four.
581 */
582 size = (bufObj->Size - offset) & ~0x3;
583
584 bind_buffer_range(ctx, index, bufObj, offset, size);
585 }
586
587
588 /**
589 * This function specifies the vertex shader outputs to be written
590 * to the feedback buffer(s), and in what order.
591 */
592 void GLAPIENTRY
593 _mesa_TransformFeedbackVaryings(GLuint program, GLsizei count,
594 const GLchar **varyings, GLenum bufferMode)
595 {
596 struct gl_shader_program *shProg;
597 GLuint i;
598 GET_CURRENT_CONTEXT(ctx);
599
600 switch (bufferMode) {
601 case GL_INTERLEAVED_ATTRIBS:
602 break;
603 case GL_SEPARATE_ATTRIBS:
604 break;
605 default:
606 _mesa_error(ctx, GL_INVALID_ENUM,
607 "glTransformFeedbackVaryings(bufferMode)");
608 return;
609 }
610
611 if (count < 0 ||
612 (bufferMode == GL_SEPARATE_ATTRIBS &&
613 (GLuint) count > ctx->Const.MaxTransformFeedbackSeparateAttribs)) {
614 _mesa_error(ctx, GL_INVALID_VALUE,
615 "glTransformFeedbackVaryings(count=%d)", count);
616 return;
617 }
618
619 shProg = _mesa_lookup_shader_program(ctx, program);
620 if (!shProg) {
621 _mesa_error(ctx, GL_INVALID_VALUE,
622 "glTransformFeedbackVaryings(program=%u)", program);
623 return;
624 }
625
626 /* free existing varyings, if any */
627 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
628 free(shProg->TransformFeedback.VaryingNames[i]);
629 }
630 free(shProg->TransformFeedback.VaryingNames);
631
632 /* allocate new memory for varying names */
633 shProg->TransformFeedback.VaryingNames =
634 (GLchar **) malloc(count * sizeof(GLchar *));
635
636 if (!shProg->TransformFeedback.VaryingNames) {
637 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTransformFeedbackVaryings()");
638 return;
639 }
640
641 /* Save the new names and the count */
642 for (i = 0; i < (GLuint) count; i++) {
643 shProg->TransformFeedback.VaryingNames[i] = _mesa_strdup(varyings[i]);
644 }
645 shProg->TransformFeedback.NumVarying = count;
646
647 shProg->TransformFeedback.BufferMode = bufferMode;
648
649 /* No need to set _NEW_TRANSFORM_FEEDBACK (or invoke FLUSH_VERTICES) since
650 * the varyings won't be used until shader link time.
651 */
652 }
653
654
655 /**
656 * Get info about the vertex shader's outputs which are to be written
657 * to the feedback buffer(s).
658 */
659 void GLAPIENTRY
660 _mesa_GetTransformFeedbackVarying(GLuint program, GLuint index,
661 GLsizei bufSize, GLsizei *length,
662 GLsizei *size, GLenum *type, GLchar *name)
663 {
664 const struct gl_shader_program *shProg;
665 const GLchar *varyingName;
666 GLint v;
667 GET_CURRENT_CONTEXT(ctx);
668
669 shProg = _mesa_lookup_shader_program(ctx, program);
670 if (!shProg) {
671 _mesa_error(ctx, GL_INVALID_VALUE,
672 "glGetTransformFeedbackVaryings(program=%u)", program);
673 return;
674 }
675
676 if (index >= shProg->TransformFeedback.NumVarying) {
677 _mesa_error(ctx, GL_INVALID_VALUE,
678 "glGetTransformFeedbackVaryings(index=%u)", index);
679 return;
680 }
681
682 varyingName = shProg->TransformFeedback.VaryingNames[index];
683
684 v = _mesa_lookup_parameter_index(shProg->Varying, -1, varyingName);
685 if (v >= 0) {
686 struct gl_program_parameter *param = &shProg->Varying->Parameters[v];
687
688 /* return the varying's name and length */
689 _mesa_copy_string(name, bufSize, length, varyingName);
690
691 /* return the datatype and value's size (in datatype units) */
692 if (type)
693 *type = param->DataType;
694 if (size)
695 *size = param->Size;
696 }
697 else {
698 name[0] = 0;
699 if (length)
700 *length = 0;
701 if (type)
702 *type = 0;
703 if (size)
704 *size = 0;
705 }
706 }
707
708
709
710 struct gl_transform_feedback_object *
711 _mesa_lookup_transform_feedback_object(struct gl_context *ctx, GLuint name)
712 {
713 if (name == 0) {
714 return ctx->TransformFeedback.DefaultObject;
715 }
716 else
717 return (struct gl_transform_feedback_object *)
718 _mesa_HashLookup(ctx->TransformFeedback.Objects, name);
719 }
720
721
722 /**
723 * Create new transform feedback objects. Transform feedback objects
724 * encapsulate the state related to transform feedback to allow quickly
725 * switching state (and drawing the results, below).
726 * Part of GL_ARB_transform_feedback2.
727 */
728 void GLAPIENTRY
729 _mesa_GenTransformFeedbacks(GLsizei n, GLuint *names)
730 {
731 GLuint first;
732 GET_CURRENT_CONTEXT(ctx);
733
734 ASSERT_OUTSIDE_BEGIN_END(ctx);
735
736 if (n < 0) {
737 _mesa_error(ctx, GL_INVALID_VALUE, "glGenTransformFeedbacks(n < 0)");
738 return;
739 }
740
741 if (!names)
742 return;
743
744 /* we don't need contiguous IDs, but this might be faster */
745 first = _mesa_HashFindFreeKeyBlock(ctx->TransformFeedback.Objects, n);
746 if (first) {
747 GLsizei i;
748 for (i = 0; i < n; i++) {
749 struct gl_transform_feedback_object *obj
750 = ctx->Driver.NewTransformFeedback(ctx, first + i);
751 if (!obj) {
752 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTransformFeedbacks");
753 return;
754 }
755 names[i] = first + i;
756 _mesa_HashInsert(ctx->TransformFeedback.Objects, first + i, obj);
757 }
758 }
759 else {
760 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTransformFeedbacks");
761 }
762 }
763
764
765 /**
766 * Is the given ID a transform feedback object?
767 * Part of GL_ARB_transform_feedback2.
768 */
769 GLboolean GLAPIENTRY
770 _mesa_IsTransformFeedback(GLuint name)
771 {
772 GET_CURRENT_CONTEXT(ctx);
773
774 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
775
776 if (name && _mesa_lookup_transform_feedback_object(ctx, name))
777 return GL_TRUE;
778 else
779 return GL_FALSE;
780 }
781
782
783 /**
784 * Bind the given transform feedback object.
785 * Part of GL_ARB_transform_feedback2.
786 */
787 void GLAPIENTRY
788 _mesa_BindTransformFeedback(GLenum target, GLuint name)
789 {
790 struct gl_transform_feedback_object *obj;
791 GET_CURRENT_CONTEXT(ctx);
792
793 if (target != GL_TRANSFORM_FEEDBACK) {
794 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTransformFeedback(target)");
795 return;
796 }
797
798 if (ctx->TransformFeedback.CurrentObject->Active &&
799 !ctx->TransformFeedback.CurrentObject->Paused) {
800 _mesa_error(ctx, GL_INVALID_OPERATION,
801 "glBindTransformFeedback(transform is active, or not paused)");
802 return;
803 }
804
805 obj = _mesa_lookup_transform_feedback_object(ctx, name);
806 if (!obj) {
807 _mesa_error(ctx, GL_INVALID_OPERATION,
808 "glBindTransformFeedback(name=%u)", name);
809 return;
810 }
811
812 reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject,
813 obj);
814 }
815
816
817 /**
818 * Delete the given transform feedback objects.
819 * Part of GL_ARB_transform_feedback2.
820 */
821 void GLAPIENTRY
822 _mesa_DeleteTransformFeedbacks(GLsizei n, const GLuint *names)
823 {
824 GLint i;
825 GET_CURRENT_CONTEXT(ctx);
826
827 ASSERT_OUTSIDE_BEGIN_END(ctx);
828
829 if (n < 0) {
830 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteTransformFeedbacks(n < 0)");
831 return;
832 }
833
834 if (!names)
835 return;
836
837 for (i = 0; i < n; i++) {
838 if (names[i] > 0) {
839 struct gl_transform_feedback_object *obj
840 = _mesa_lookup_transform_feedback_object(ctx, names[i]);
841 if (obj) {
842 if (obj->Active) {
843 _mesa_error(ctx, GL_INVALID_OPERATION,
844 "glDeleteTransformFeedbacks(object %u is active)",
845 names[i]);
846 return;
847 }
848 _mesa_HashRemove(ctx->TransformFeedback.Objects, names[i]);
849 /* unref, but object may not be deleted until later */
850 reference_transform_feedback_object(&obj, NULL);
851 }
852 }
853 }
854 }
855
856
857 /**
858 * Pause transform feedback.
859 * Part of GL_ARB_transform_feedback2.
860 */
861 void GLAPIENTRY
862 _mesa_PauseTransformFeedback(void)
863 {
864 struct gl_transform_feedback_object *obj;
865 GET_CURRENT_CONTEXT(ctx);
866
867 obj = ctx->TransformFeedback.CurrentObject;
868
869 if (!obj->Active || obj->Paused) {
870 _mesa_error(ctx, GL_INVALID_OPERATION,
871 "glPauseTransformFeedback(feedback not active or already paused)");
872 return;
873 }
874
875 FLUSH_VERTICES(ctx, _NEW_TRANSFORM_FEEDBACK);
876 obj->Paused = GL_TRUE;
877
878 assert(ctx->Driver.PauseTransformFeedback);
879 ctx->Driver.PauseTransformFeedback(ctx, obj);
880 }
881
882
883 /**
884 * Resume transform feedback.
885 * Part of GL_ARB_transform_feedback2.
886 */
887 void GLAPIENTRY
888 _mesa_ResumeTransformFeedback(void)
889 {
890 struct gl_transform_feedback_object *obj;
891 GET_CURRENT_CONTEXT(ctx);
892
893 obj = ctx->TransformFeedback.CurrentObject;
894
895 if (!obj->Active || !obj->Paused) {
896 _mesa_error(ctx, GL_INVALID_OPERATION,
897 "glResumeTransformFeedback(feedback not active or not paused)");
898 return;
899 }
900
901 FLUSH_VERTICES(ctx, _NEW_TRANSFORM_FEEDBACK);
902 obj->Paused = GL_FALSE;
903
904 assert(ctx->Driver.ResumeTransformFeedback);
905 ctx->Driver.ResumeTransformFeedback(ctx, obj);
906 }
907
908 #endif /* FEATURE_EXT_transform_feedback */