mesa: Disallow TransformFeedbackVaryings when active.
[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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /*
27 * Vertex transform feedback support.
28 *
29 * Authors:
30 * Brian Paul
31 */
32
33
34 #include "buffers.h"
35 #include "bufferobj.h"
36 #include "context.h"
37 #include "hash.h"
38 #include "macros.h"
39 #include "mtypes.h"
40 #include "transformfeedback.h"
41 #include "shaderapi.h"
42 #include "shaderobj.h"
43 #include "main/dispatch.h"
44
45 #include "program/prog_parameter.h"
46
47
48 /**
49 * Do reference counting of transform feedback buffers.
50 */
51 static void
52 reference_transform_feedback_object(struct gl_transform_feedback_object **ptr,
53 struct gl_transform_feedback_object *obj)
54 {
55 if (*ptr == obj)
56 return;
57
58 if (*ptr) {
59 /* Unreference the old object */
60 struct gl_transform_feedback_object *oldObj = *ptr;
61
62 ASSERT(oldObj->RefCount > 0);
63 oldObj->RefCount--;
64
65 if (oldObj->RefCount == 0) {
66 GET_CURRENT_CONTEXT(ctx);
67 if (ctx)
68 ctx->Driver.DeleteTransformFeedback(ctx, oldObj);
69 }
70
71 *ptr = NULL;
72 }
73 ASSERT(!*ptr);
74
75 if (obj) {
76 /* reference new object */
77 if (obj->RefCount == 0) {
78 _mesa_problem(NULL, "referencing deleted transform feedback object");
79 *ptr = NULL;
80 }
81 else {
82 obj->RefCount++;
83 obj->EverBound = GL_TRUE;
84 *ptr = obj;
85 }
86 }
87 }
88
89
90 /**
91 * Check that all the buffer objects currently bound for transform
92 * feedback actually exist. Raise a GL_INVALID_OPERATION error if
93 * any buffers are missing.
94 * \return GL_TRUE for success, GL_FALSE if error
95 */
96 GLboolean
97 _mesa_validate_transform_feedback_buffers(struct gl_context *ctx)
98 {
99 /* XXX to do */
100 return GL_TRUE;
101 }
102
103
104
105 /**
106 * Per-context init for transform feedback.
107 */
108 void
109 _mesa_init_transform_feedback(struct gl_context *ctx)
110 {
111 /* core mesa expects this, even a dummy one, to be available */
112 ASSERT(ctx->Driver.NewTransformFeedback);
113
114 ctx->TransformFeedback.DefaultObject =
115 ctx->Driver.NewTransformFeedback(ctx, 0);
116
117 assert(ctx->TransformFeedback.DefaultObject->RefCount == 1);
118
119 reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject,
120 ctx->TransformFeedback.DefaultObject);
121
122 assert(ctx->TransformFeedback.DefaultObject->RefCount == 2);
123
124 ctx->TransformFeedback.Objects = _mesa_NewHashTable();
125
126 _mesa_reference_buffer_object(ctx,
127 &ctx->TransformFeedback.CurrentBuffer,
128 ctx->Shared->NullBufferObj);
129 }
130
131
132
133 /**
134 * Callback for _mesa_HashDeleteAll().
135 */
136 static void
137 delete_cb(GLuint key, void *data, void *userData)
138 {
139 struct gl_context *ctx = (struct gl_context *) userData;
140 struct gl_transform_feedback_object *obj =
141 (struct gl_transform_feedback_object *) data;
142
143 ctx->Driver.DeleteTransformFeedback(ctx, obj);
144 }
145
146
147 /**
148 * Per-context free/clean-up for transform feedback.
149 */
150 void
151 _mesa_free_transform_feedback(struct gl_context *ctx)
152 {
153 /* core mesa expects this, even a dummy one, to be available */
154 ASSERT(ctx->Driver.NewTransformFeedback);
155
156 _mesa_reference_buffer_object(ctx,
157 &ctx->TransformFeedback.CurrentBuffer,
158 NULL);
159
160 /* Delete all feedback objects */
161 _mesa_HashDeleteAll(ctx->TransformFeedback.Objects, delete_cb, ctx);
162 _mesa_DeleteHashTable(ctx->TransformFeedback.Objects);
163
164 /* Delete the default feedback object */
165 assert(ctx->Driver.DeleteTransformFeedback);
166 ctx->Driver.DeleteTransformFeedback(ctx,
167 ctx->TransformFeedback.DefaultObject);
168
169 ctx->TransformFeedback.CurrentObject = NULL;
170 }
171
172
173 /** Default fallback for ctx->Driver.NewTransformFeedback() */
174 static struct gl_transform_feedback_object *
175 new_transform_feedback(struct gl_context *ctx, GLuint name)
176 {
177 struct gl_transform_feedback_object *obj;
178 obj = CALLOC_STRUCT(gl_transform_feedback_object);
179 if (obj) {
180 obj->Name = name;
181 obj->RefCount = 1;
182 obj->EverBound = GL_FALSE;
183 }
184 return obj;
185 }
186
187 /** Default fallback for ctx->Driver.DeleteTransformFeedback() */
188 static void
189 delete_transform_feedback(struct gl_context *ctx,
190 struct gl_transform_feedback_object *obj)
191 {
192 GLuint i;
193
194 for (i = 0; i < Elements(obj->Buffers); i++) {
195 _mesa_reference_buffer_object(ctx, &obj->Buffers[i], NULL);
196 }
197
198 free(obj->Label);
199 free(obj);
200 }
201
202
203 /** Default fallback for ctx->Driver.BeginTransformFeedback() */
204 static void
205 begin_transform_feedback(struct gl_context *ctx, GLenum mode,
206 struct gl_transform_feedback_object *obj)
207 {
208 /* nop */
209 }
210
211 /** Default fallback for ctx->Driver.EndTransformFeedback() */
212 static void
213 end_transform_feedback(struct gl_context *ctx,
214 struct gl_transform_feedback_object *obj)
215 {
216 /* nop */
217 }
218
219 /** Default fallback for ctx->Driver.PauseTransformFeedback() */
220 static void
221 pause_transform_feedback(struct gl_context *ctx,
222 struct gl_transform_feedback_object *obj)
223 {
224 /* nop */
225 }
226
227 /** Default fallback for ctx->Driver.ResumeTransformFeedback() */
228 static void
229 resume_transform_feedback(struct gl_context *ctx,
230 struct gl_transform_feedback_object *obj)
231 {
232 /* nop */
233 }
234
235
236 /**
237 * Plug in default device driver functions for transform feedback.
238 * Most drivers will override some/all of these.
239 */
240 void
241 _mesa_init_transform_feedback_functions(struct dd_function_table *driver)
242 {
243 driver->NewTransformFeedback = new_transform_feedback;
244 driver->DeleteTransformFeedback = delete_transform_feedback;
245 driver->BeginTransformFeedback = begin_transform_feedback;
246 driver->EndTransformFeedback = end_transform_feedback;
247 driver->PauseTransformFeedback = pause_transform_feedback;
248 driver->ResumeTransformFeedback = resume_transform_feedback;
249 }
250
251
252 /**
253 * Fill in the correct Size value for each buffer in \c obj.
254 *
255 * From the GL 4.3 spec, section 6.1.1 ("Binding Buffer Objects to Indexed
256 * Targets"):
257 *
258 * BindBufferBase binds the entire buffer, even when the size of the buffer
259 * is changed after the binding is established. It is equivalent to calling
260 * BindBufferRange with offset zero, while size is determined by the size of
261 * the bound buffer at the time the binding is used.
262 *
263 * Regardless of the size specified with BindBufferRange, or indirectly with
264 * BindBufferBase, the GL will never read or write beyond the end of a bound
265 * buffer. In some cases this constraint may result in visibly different
266 * behavior when a buffer overflow would otherwise result, such as described
267 * for transform feedback operations in section 13.2.2.
268 */
269 static void
270 compute_transform_feedback_buffer_sizes(
271 struct gl_transform_feedback_object *obj)
272 {
273 unsigned i = 0;
274 for (i = 0; i < MAX_FEEDBACK_BUFFERS; ++i) {
275 GLintptr offset = obj->Offset[i];
276 GLsizeiptr buffer_size
277 = obj->Buffers[i] == NULL ? 0 : obj->Buffers[i]->Size;
278 GLsizeiptr available_space
279 = buffer_size <= offset ? 0 : buffer_size - offset;
280 GLsizeiptr computed_size;
281 if (obj->RequestedSize[i] == 0) {
282 /* No size was specified at the time the buffer was bound, so allow
283 * writing to all available space in the buffer.
284 */
285 computed_size = available_space;
286 } else {
287 /* A size was specified at the time the buffer was bound, however
288 * it's possible that the buffer has shrunk since then. So only
289 * allow writing to the minimum of the specified size and the space
290 * available.
291 */
292 computed_size = MIN2(available_space, obj->RequestedSize[i]);
293 }
294
295 /* Legal sizes must be multiples of four, so round down if necessary. */
296 obj->Size[i] = computed_size & ~0x3;
297 }
298 }
299
300
301 /**
302 * Compute the maximum number of vertices that can be written to the currently
303 * enabled transform feedback buffers without overflowing any of them.
304 */
305 unsigned
306 _mesa_compute_max_transform_feedback_vertices(
307 const struct gl_transform_feedback_object *obj,
308 const struct gl_transform_feedback_info *info)
309 {
310 unsigned max_index = 0xffffffff;
311 unsigned i;
312
313 for (i = 0; i < info->NumBuffers; ++i) {
314 unsigned stride = info->BufferStride[i];
315 unsigned max_for_this_buffer;
316
317 /* Skip any inactive buffers, which have a stride of 0. */
318 if (stride == 0)
319 continue;
320
321 max_for_this_buffer = obj->Size[i] / (4 * stride);
322 max_index = MIN2(max_index, max_for_this_buffer);
323 }
324
325 return max_index;
326 }
327
328
329 /**
330 ** Begin API functions
331 **/
332
333
334 void GLAPIENTRY
335 _mesa_BeginTransformFeedback(GLenum mode)
336 {
337 struct gl_transform_feedback_object *obj;
338 struct gl_transform_feedback_info *info;
339 GLuint i;
340 unsigned vertices_per_prim;
341 GET_CURRENT_CONTEXT(ctx);
342
343 obj = ctx->TransformFeedback.CurrentObject;
344
345 if (ctx->Shader.CurrentVertexProgram == NULL) {
346 _mesa_error(ctx, GL_INVALID_OPERATION,
347 "glBeginTransformFeedback(no program active)");
348 return;
349 }
350
351 info = &ctx->Shader.CurrentVertexProgram->LinkedTransformFeedback;
352
353 if (info->NumOutputs == 0) {
354 _mesa_error(ctx, GL_INVALID_OPERATION,
355 "glBeginTransformFeedback(no varyings to record)");
356 return;
357 }
358
359 switch (mode) {
360 case GL_POINTS:
361 vertices_per_prim = 1;
362 break;
363 case GL_LINES:
364 vertices_per_prim = 2;
365 break;
366 case GL_TRIANGLES:
367 vertices_per_prim = 3;
368 break;
369 default:
370 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginTransformFeedback(mode)");
371 return;
372 }
373
374 if (obj->Active) {
375 _mesa_error(ctx, GL_INVALID_OPERATION,
376 "glBeginTransformFeedback(already active)");
377 return;
378 }
379
380 for (i = 0; i < info->NumBuffers; ++i) {
381 if (obj->BufferNames[i] == 0) {
382 _mesa_error(ctx, GL_INVALID_OPERATION,
383 "glBeginTransformFeedback(binding point %d does not have "
384 "a buffer object bound)", i);
385 return;
386 }
387 }
388
389 FLUSH_VERTICES(ctx, 0);
390 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
391
392 obj->Active = GL_TRUE;
393 ctx->TransformFeedback.Mode = mode;
394
395 compute_transform_feedback_buffer_sizes(obj);
396
397 if (_mesa_is_gles3(ctx)) {
398 /* In GLES3, we are required to track the usage of the transform
399 * feedback buffer and report INVALID_OPERATION if a draw call tries to
400 * exceed it. So compute the maximum number of vertices that we can
401 * write without overflowing any of the buffers currently being used for
402 * feedback.
403 */
404 unsigned max_vertices
405 = _mesa_compute_max_transform_feedback_vertices(obj, info);
406 obj->GlesRemainingPrims = max_vertices / vertices_per_prim;
407 }
408
409 assert(ctx->Driver.BeginTransformFeedback);
410 ctx->Driver.BeginTransformFeedback(ctx, mode, obj);
411 }
412
413
414 void GLAPIENTRY
415 _mesa_EndTransformFeedback(void)
416 {
417 struct gl_transform_feedback_object *obj;
418 GET_CURRENT_CONTEXT(ctx);
419
420 obj = ctx->TransformFeedback.CurrentObject;
421
422 if (!obj->Active) {
423 _mesa_error(ctx, GL_INVALID_OPERATION,
424 "glEndTransformFeedback(not active)");
425 return;
426 }
427
428 FLUSH_VERTICES(ctx, 0);
429 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
430
431 ctx->TransformFeedback.CurrentObject->Active = GL_FALSE;
432 ctx->TransformFeedback.CurrentObject->Paused = GL_FALSE;
433 ctx->TransformFeedback.CurrentObject->EndedAnytime = GL_TRUE;
434
435 assert(ctx->Driver.EndTransformFeedback);
436 ctx->Driver.EndTransformFeedback(ctx, obj);
437 }
438
439
440 /**
441 * Helper used by BindBufferRange() and BindBufferBase().
442 */
443 static void
444 bind_buffer_range(struct gl_context *ctx, GLuint index,
445 struct gl_buffer_object *bufObj,
446 GLintptr offset, GLsizeiptr size)
447 {
448 struct gl_transform_feedback_object *obj =
449 ctx->TransformFeedback.CurrentObject;
450
451 /* Note: no need to FLUSH_VERTICES or flag NewTransformFeedback, because
452 * transform feedback buffers can't be changed while transform feedback is
453 * active.
454 */
455
456 /* The general binding point */
457 _mesa_reference_buffer_object(ctx,
458 &ctx->TransformFeedback.CurrentBuffer,
459 bufObj);
460
461 /* The per-attribute binding point */
462 _mesa_reference_buffer_object(ctx,
463 &obj->Buffers[index],
464 bufObj);
465
466 obj->BufferNames[index] = bufObj->Name;
467
468 obj->Offset[index] = offset;
469 obj->RequestedSize[index] = size;
470 }
471
472
473 /**
474 * Specify a buffer object to receive vertex shader results. Plus,
475 * specify the starting offset to place the results, and max size.
476 * Called from the glBindBufferRange() function.
477 */
478 void
479 _mesa_bind_buffer_range_transform_feedback(struct gl_context *ctx,
480 GLuint index,
481 struct gl_buffer_object *bufObj,
482 GLintptr offset,
483 GLsizeiptr size)
484 {
485 struct gl_transform_feedback_object *obj;
486
487 obj = ctx->TransformFeedback.CurrentObject;
488
489 if (obj->Active) {
490 _mesa_error(ctx, GL_INVALID_OPERATION,
491 "glBindBufferRange(transform feedback active)");
492 return;
493 }
494
495 if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
496 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
497 return;
498 }
499
500 if (size & 0x3) {
501 /* must a multiple of four */
502 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)", (int) size);
503 return;
504 }
505
506 if (offset & 0x3) {
507 /* must be multiple of four */
508 _mesa_error(ctx, GL_INVALID_VALUE,
509 "glBindBufferRange(offset=%d)", (int) offset);
510 return;
511 }
512
513 bind_buffer_range(ctx, index, bufObj, offset, size);
514 }
515
516
517 /**
518 * Specify a buffer object to receive vertex shader results.
519 * As above, but start at offset = 0.
520 * Called from the glBindBufferBase() function.
521 */
522 void
523 _mesa_bind_buffer_base_transform_feedback(struct gl_context *ctx,
524 GLuint index,
525 struct gl_buffer_object *bufObj)
526 {
527 struct gl_transform_feedback_object *obj;
528
529 obj = ctx->TransformFeedback.CurrentObject;
530
531 if (obj->Active) {
532 _mesa_error(ctx, GL_INVALID_OPERATION,
533 "glBindBufferBase(transform feedback active)");
534 return;
535 }
536
537 if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
538 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
539 return;
540 }
541
542 bind_buffer_range(ctx, index, bufObj, 0, 0);
543 }
544
545
546 /**
547 * Specify a buffer object to receive vertex shader results, plus the
548 * offset in the buffer to start placing results.
549 * This function is part of GL_EXT_transform_feedback, but not GL3.
550 */
551 void GLAPIENTRY
552 _mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer,
553 GLintptr offset)
554 {
555 struct gl_transform_feedback_object *obj;
556 struct gl_buffer_object *bufObj;
557 GET_CURRENT_CONTEXT(ctx);
558
559 if (target != GL_TRANSFORM_FEEDBACK_BUFFER) {
560 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferOffsetEXT(target)");
561 return;
562 }
563
564 obj = ctx->TransformFeedback.CurrentObject;
565
566 if (obj->Active) {
567 _mesa_error(ctx, GL_INVALID_OPERATION,
568 "glBindBufferOffsetEXT(transform feedback active)");
569 return;
570 }
571
572 if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
573 _mesa_error(ctx, GL_INVALID_VALUE,
574 "glBindBufferOffsetEXT(index=%d)", index);
575 return;
576 }
577
578 if (offset & 0x3) {
579 /* must be multiple of four */
580 _mesa_error(ctx, GL_INVALID_VALUE,
581 "glBindBufferOffsetEXT(offset=%d)", (int) offset);
582 return;
583 }
584
585 if (buffer == 0) {
586 bufObj = ctx->Shared->NullBufferObj;
587 } else {
588 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
589 }
590
591 if (!bufObj) {
592 _mesa_error(ctx, GL_INVALID_OPERATION,
593 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer);
594 return;
595 }
596
597 bind_buffer_range(ctx, index, bufObj, offset, 0);
598 }
599
600
601 /**
602 * This function specifies the vertex shader outputs to be written
603 * to the feedback buffer(s), and in what order.
604 */
605 void GLAPIENTRY
606 _mesa_TransformFeedbackVaryings(GLuint program, GLsizei count,
607 const GLchar * const *varyings,
608 GLenum bufferMode)
609 {
610 struct gl_shader_program *shProg;
611 GLint i;
612 GET_CURRENT_CONTEXT(ctx);
613
614 /* From the ARB_transform_feedback2 specification:
615 * "The error INVALID_OPERATION is generated by TransformFeedbackVaryings
616 * if the current transform feedback object is active, even if paused."
617 */
618 if (ctx->TransformFeedback.CurrentObject->Active) {
619 _mesa_error(ctx, GL_INVALID_OPERATION,
620 "glTransformFeedbackVaryings(current object is active)");
621 return;
622 }
623
624 switch (bufferMode) {
625 case GL_INTERLEAVED_ATTRIBS:
626 break;
627 case GL_SEPARATE_ATTRIBS:
628 break;
629 default:
630 _mesa_error(ctx, GL_INVALID_ENUM,
631 "glTransformFeedbackVaryings(bufferMode)");
632 return;
633 }
634
635 if (count < 0 ||
636 (bufferMode == GL_SEPARATE_ATTRIBS &&
637 (GLuint) count > ctx->Const.MaxTransformFeedbackBuffers)) {
638 _mesa_error(ctx, GL_INVALID_VALUE,
639 "glTransformFeedbackVaryings(count=%d)", count);
640 return;
641 }
642
643 shProg = _mesa_lookup_shader_program(ctx, program);
644 if (!shProg) {
645 _mesa_error(ctx, GL_INVALID_VALUE,
646 "glTransformFeedbackVaryings(program=%u)", program);
647 return;
648 }
649
650 if (ctx->Extensions.ARB_transform_feedback3) {
651 if (bufferMode == GL_INTERLEAVED_ATTRIBS) {
652 unsigned buffers = 1;
653
654 for (i = 0; i < count; i++) {
655 if (strcmp(varyings[i], "gl_NextBuffer") == 0)
656 buffers++;
657 }
658
659 if (buffers > ctx->Const.MaxTransformFeedbackBuffers) {
660 _mesa_error(ctx, GL_INVALID_OPERATION,
661 "glTransformFeedbackVaryings(too many gl_NextBuffer "
662 "occurences)");
663 return;
664 }
665 } else {
666 for (i = 0; i < count; i++) {
667 if (strcmp(varyings[i], "gl_NextBuffer") == 0 ||
668 strcmp(varyings[i], "gl_SkipComponents1") == 0 ||
669 strcmp(varyings[i], "gl_SkipComponents2") == 0 ||
670 strcmp(varyings[i], "gl_SkipComponents3") == 0 ||
671 strcmp(varyings[i], "gl_SkipComponents4") == 0) {
672 _mesa_error(ctx, GL_INVALID_OPERATION,
673 "glTransformFeedbackVaryings(SEPARATE_ATTRIBS,"
674 "varying=%s)",
675 varyings[i]);
676 return;
677 }
678 }
679 }
680 }
681
682 /* free existing varyings, if any */
683 for (i = 0; i < (GLint) shProg->TransformFeedback.NumVarying; i++) {
684 free(shProg->TransformFeedback.VaryingNames[i]);
685 }
686 free(shProg->TransformFeedback.VaryingNames);
687
688 /* allocate new memory for varying names */
689 shProg->TransformFeedback.VaryingNames =
690 malloc(count * sizeof(GLchar *));
691
692 if (!shProg->TransformFeedback.VaryingNames) {
693 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTransformFeedbackVaryings()");
694 return;
695 }
696
697 /* Save the new names and the count */
698 for (i = 0; i < count; i++) {
699 shProg->TransformFeedback.VaryingNames[i] = _mesa_strdup(varyings[i]);
700 }
701 shProg->TransformFeedback.NumVarying = count;
702
703 shProg->TransformFeedback.BufferMode = bufferMode;
704
705 /* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since
706 * the varyings won't be used until shader link time.
707 */
708 }
709
710
711 /**
712 * Get info about the vertex shader's outputs which are to be written
713 * to the feedback buffer(s).
714 */
715 void GLAPIENTRY
716 _mesa_GetTransformFeedbackVarying(GLuint program, GLuint index,
717 GLsizei bufSize, GLsizei *length,
718 GLsizei *size, GLenum *type, GLchar *name)
719 {
720 const struct gl_shader_program *shProg;
721 const struct gl_transform_feedback_info *linked_xfb_info;
722 GET_CURRENT_CONTEXT(ctx);
723
724 shProg = _mesa_lookup_shader_program(ctx, program);
725 if (!shProg) {
726 _mesa_error(ctx, GL_INVALID_VALUE,
727 "glGetTransformFeedbackVarying(program=%u)", program);
728 return;
729 }
730
731 linked_xfb_info = &shProg->LinkedTransformFeedback;
732 if (index >= (GLuint) linked_xfb_info->NumVarying) {
733 _mesa_error(ctx, GL_INVALID_VALUE,
734 "glGetTransformFeedbackVarying(index=%u)", index);
735 return;
736 }
737
738 /* return the varying's name and length */
739 _mesa_copy_string(name, bufSize, length,
740 linked_xfb_info->Varyings[index].Name);
741
742 /* return the datatype and value's size (in datatype units) */
743 if (type)
744 *type = linked_xfb_info->Varyings[index].Type;
745 if (size)
746 *size = linked_xfb_info->Varyings[index].Size;
747 }
748
749
750
751 struct gl_transform_feedback_object *
752 _mesa_lookup_transform_feedback_object(struct gl_context *ctx, GLuint name)
753 {
754 if (name == 0) {
755 return ctx->TransformFeedback.DefaultObject;
756 }
757 else
758 return (struct gl_transform_feedback_object *)
759 _mesa_HashLookup(ctx->TransformFeedback.Objects, name);
760 }
761
762
763 /**
764 * Create new transform feedback objects. Transform feedback objects
765 * encapsulate the state related to transform feedback to allow quickly
766 * switching state (and drawing the results, below).
767 * Part of GL_ARB_transform_feedback2.
768 */
769 void GLAPIENTRY
770 _mesa_GenTransformFeedbacks(GLsizei n, GLuint *names)
771 {
772 GLuint first;
773 GET_CURRENT_CONTEXT(ctx);
774
775 if (n < 0) {
776 _mesa_error(ctx, GL_INVALID_VALUE, "glGenTransformFeedbacks(n < 0)");
777 return;
778 }
779
780 if (!names)
781 return;
782
783 /* we don't need contiguous IDs, but this might be faster */
784 first = _mesa_HashFindFreeKeyBlock(ctx->TransformFeedback.Objects, n);
785 if (first) {
786 GLsizei i;
787 for (i = 0; i < n; i++) {
788 struct gl_transform_feedback_object *obj
789 = ctx->Driver.NewTransformFeedback(ctx, first + i);
790 if (!obj) {
791 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTransformFeedbacks");
792 return;
793 }
794 names[i] = first + i;
795 _mesa_HashInsert(ctx->TransformFeedback.Objects, first + i, obj);
796 }
797 }
798 else {
799 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTransformFeedbacks");
800 }
801 }
802
803
804 /**
805 * Is the given ID a transform feedback object?
806 * Part of GL_ARB_transform_feedback2.
807 */
808 GLboolean GLAPIENTRY
809 _mesa_IsTransformFeedback(GLuint name)
810 {
811 struct gl_transform_feedback_object *obj;
812 GET_CURRENT_CONTEXT(ctx);
813
814 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
815
816 if (name == 0)
817 return GL_FALSE;
818
819 obj = _mesa_lookup_transform_feedback_object(ctx, name);
820 if (obj == NULL)
821 return GL_FALSE;
822
823 return obj->EverBound;
824 }
825
826
827 /**
828 * Bind the given transform feedback object.
829 * Part of GL_ARB_transform_feedback2.
830 */
831 void GLAPIENTRY
832 _mesa_BindTransformFeedback(GLenum target, GLuint name)
833 {
834 struct gl_transform_feedback_object *obj;
835 GET_CURRENT_CONTEXT(ctx);
836
837 if (target != GL_TRANSFORM_FEEDBACK) {
838 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTransformFeedback(target)");
839 return;
840 }
841
842 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
843 _mesa_error(ctx, GL_INVALID_OPERATION,
844 "glBindTransformFeedback(transform is active, or not paused)");
845 return;
846 }
847
848 obj = _mesa_lookup_transform_feedback_object(ctx, name);
849 if (!obj) {
850 _mesa_error(ctx, GL_INVALID_OPERATION,
851 "glBindTransformFeedback(name=%u)", name);
852 return;
853 }
854
855 reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject,
856 obj);
857 }
858
859
860 /**
861 * Delete the given transform feedback objects.
862 * Part of GL_ARB_transform_feedback2.
863 */
864 void GLAPIENTRY
865 _mesa_DeleteTransformFeedbacks(GLsizei n, const GLuint *names)
866 {
867 GLint i;
868 GET_CURRENT_CONTEXT(ctx);
869
870 if (n < 0) {
871 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteTransformFeedbacks(n < 0)");
872 return;
873 }
874
875 if (!names)
876 return;
877
878 for (i = 0; i < n; i++) {
879 if (names[i] > 0) {
880 struct gl_transform_feedback_object *obj
881 = _mesa_lookup_transform_feedback_object(ctx, names[i]);
882 if (obj) {
883 if (obj->Active) {
884 _mesa_error(ctx, GL_INVALID_OPERATION,
885 "glDeleteTransformFeedbacks(object %u is active)",
886 names[i]);
887 return;
888 }
889 _mesa_HashRemove(ctx->TransformFeedback.Objects, names[i]);
890 /* unref, but object may not be deleted until later */
891 reference_transform_feedback_object(&obj, NULL);
892 }
893 }
894 }
895 }
896
897
898 /**
899 * Pause transform feedback.
900 * Part of GL_ARB_transform_feedback2.
901 */
902 void GLAPIENTRY
903 _mesa_PauseTransformFeedback(void)
904 {
905 struct gl_transform_feedback_object *obj;
906 GET_CURRENT_CONTEXT(ctx);
907
908 obj = ctx->TransformFeedback.CurrentObject;
909
910 if (!_mesa_is_xfb_active_and_unpaused(ctx)) {
911 _mesa_error(ctx, GL_INVALID_OPERATION,
912 "glPauseTransformFeedback(feedback not active or already paused)");
913 return;
914 }
915
916 FLUSH_VERTICES(ctx, 0);
917 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
918
919 obj->Paused = GL_TRUE;
920
921 assert(ctx->Driver.PauseTransformFeedback);
922 ctx->Driver.PauseTransformFeedback(ctx, obj);
923 }
924
925
926 /**
927 * Resume transform feedback.
928 * Part of GL_ARB_transform_feedback2.
929 */
930 void GLAPIENTRY
931 _mesa_ResumeTransformFeedback(void)
932 {
933 struct gl_transform_feedback_object *obj;
934 GET_CURRENT_CONTEXT(ctx);
935
936 obj = ctx->TransformFeedback.CurrentObject;
937
938 if (!obj->Active || !obj->Paused) {
939 _mesa_error(ctx, GL_INVALID_OPERATION,
940 "glResumeTransformFeedback(feedback not active or not paused)");
941 return;
942 }
943
944 FLUSH_VERTICES(ctx, 0);
945 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
946
947 obj->Paused = GL_FALSE;
948
949 assert(ctx->Driver.ResumeTransformFeedback);
950 ctx->Driver.ResumeTransformFeedback(ctx, obj);
951 }