mesa: Track the vertex program active at BeginTransformFeedback() time.
[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 obj->shader_program = ctx->Shader.CurrentVertexProgram;
410
411 assert(ctx->Driver.BeginTransformFeedback);
412 ctx->Driver.BeginTransformFeedback(ctx, mode, obj);
413 }
414
415
416 void GLAPIENTRY
417 _mesa_EndTransformFeedback(void)
418 {
419 struct gl_transform_feedback_object *obj;
420 GET_CURRENT_CONTEXT(ctx);
421
422 obj = ctx->TransformFeedback.CurrentObject;
423
424 if (!obj->Active) {
425 _mesa_error(ctx, GL_INVALID_OPERATION,
426 "glEndTransformFeedback(not active)");
427 return;
428 }
429
430 FLUSH_VERTICES(ctx, 0);
431 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
432
433 ctx->TransformFeedback.CurrentObject->Active = GL_FALSE;
434 ctx->TransformFeedback.CurrentObject->Paused = GL_FALSE;
435 ctx->TransformFeedback.CurrentObject->EndedAnytime = GL_TRUE;
436
437 assert(ctx->Driver.EndTransformFeedback);
438 ctx->Driver.EndTransformFeedback(ctx, obj);
439 }
440
441
442 /**
443 * Helper used by BindBufferRange() and BindBufferBase().
444 */
445 static void
446 bind_buffer_range(struct gl_context *ctx, GLuint index,
447 struct gl_buffer_object *bufObj,
448 GLintptr offset, GLsizeiptr size)
449 {
450 struct gl_transform_feedback_object *obj =
451 ctx->TransformFeedback.CurrentObject;
452
453 /* Note: no need to FLUSH_VERTICES or flag NewTransformFeedback, because
454 * transform feedback buffers can't be changed while transform feedback is
455 * active.
456 */
457
458 /* The general binding point */
459 _mesa_reference_buffer_object(ctx,
460 &ctx->TransformFeedback.CurrentBuffer,
461 bufObj);
462
463 /* The per-attribute binding point */
464 _mesa_reference_buffer_object(ctx,
465 &obj->Buffers[index],
466 bufObj);
467
468 obj->BufferNames[index] = bufObj->Name;
469
470 obj->Offset[index] = offset;
471 obj->RequestedSize[index] = size;
472 }
473
474
475 /**
476 * Specify a buffer object to receive vertex shader results. Plus,
477 * specify the starting offset to place the results, and max size.
478 * Called from the glBindBufferRange() function.
479 */
480 void
481 _mesa_bind_buffer_range_transform_feedback(struct gl_context *ctx,
482 GLuint index,
483 struct gl_buffer_object *bufObj,
484 GLintptr offset,
485 GLsizeiptr size)
486 {
487 struct gl_transform_feedback_object *obj;
488
489 obj = ctx->TransformFeedback.CurrentObject;
490
491 if (obj->Active) {
492 _mesa_error(ctx, GL_INVALID_OPERATION,
493 "glBindBufferRange(transform feedback active)");
494 return;
495 }
496
497 if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
498 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(index=%d)", index);
499 return;
500 }
501
502 if (size & 0x3) {
503 /* must a multiple of four */
504 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferRange(size=%d)", (int) size);
505 return;
506 }
507
508 if (offset & 0x3) {
509 /* must be multiple of four */
510 _mesa_error(ctx, GL_INVALID_VALUE,
511 "glBindBufferRange(offset=%d)", (int) offset);
512 return;
513 }
514
515 bind_buffer_range(ctx, index, bufObj, offset, size);
516 }
517
518
519 /**
520 * Specify a buffer object to receive vertex shader results.
521 * As above, but start at offset = 0.
522 * Called from the glBindBufferBase() function.
523 */
524 void
525 _mesa_bind_buffer_base_transform_feedback(struct gl_context *ctx,
526 GLuint index,
527 struct gl_buffer_object *bufObj)
528 {
529 struct gl_transform_feedback_object *obj;
530
531 obj = ctx->TransformFeedback.CurrentObject;
532
533 if (obj->Active) {
534 _mesa_error(ctx, GL_INVALID_OPERATION,
535 "glBindBufferBase(transform feedback active)");
536 return;
537 }
538
539 if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
540 _mesa_error(ctx, GL_INVALID_VALUE, "glBindBufferBase(index=%d)", index);
541 return;
542 }
543
544 bind_buffer_range(ctx, index, bufObj, 0, 0);
545 }
546
547
548 /**
549 * Specify a buffer object to receive vertex shader results, plus the
550 * offset in the buffer to start placing results.
551 * This function is part of GL_EXT_transform_feedback, but not GL3.
552 */
553 void GLAPIENTRY
554 _mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer,
555 GLintptr offset)
556 {
557 struct gl_transform_feedback_object *obj;
558 struct gl_buffer_object *bufObj;
559 GET_CURRENT_CONTEXT(ctx);
560
561 if (target != GL_TRANSFORM_FEEDBACK_BUFFER) {
562 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferOffsetEXT(target)");
563 return;
564 }
565
566 obj = ctx->TransformFeedback.CurrentObject;
567
568 if (obj->Active) {
569 _mesa_error(ctx, GL_INVALID_OPERATION,
570 "glBindBufferOffsetEXT(transform feedback active)");
571 return;
572 }
573
574 if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
575 _mesa_error(ctx, GL_INVALID_VALUE,
576 "glBindBufferOffsetEXT(index=%d)", index);
577 return;
578 }
579
580 if (offset & 0x3) {
581 /* must be multiple of four */
582 _mesa_error(ctx, GL_INVALID_VALUE,
583 "glBindBufferOffsetEXT(offset=%d)", (int) offset);
584 return;
585 }
586
587 if (buffer == 0) {
588 bufObj = ctx->Shared->NullBufferObj;
589 } else {
590 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
591 }
592
593 if (!bufObj) {
594 _mesa_error(ctx, GL_INVALID_OPERATION,
595 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer);
596 return;
597 }
598
599 bind_buffer_range(ctx, index, bufObj, offset, 0);
600 }
601
602
603 /**
604 * This function specifies the vertex shader outputs to be written
605 * to the feedback buffer(s), and in what order.
606 */
607 void GLAPIENTRY
608 _mesa_TransformFeedbackVaryings(GLuint program, GLsizei count,
609 const GLchar * const *varyings,
610 GLenum bufferMode)
611 {
612 struct gl_shader_program *shProg;
613 GLint i;
614 GET_CURRENT_CONTEXT(ctx);
615
616 /* From the ARB_transform_feedback2 specification:
617 * "The error INVALID_OPERATION is generated by TransformFeedbackVaryings
618 * if the current transform feedback object is active, even if paused."
619 */
620 if (ctx->TransformFeedback.CurrentObject->Active) {
621 _mesa_error(ctx, GL_INVALID_OPERATION,
622 "glTransformFeedbackVaryings(current object is active)");
623 return;
624 }
625
626 switch (bufferMode) {
627 case GL_INTERLEAVED_ATTRIBS:
628 break;
629 case GL_SEPARATE_ATTRIBS:
630 break;
631 default:
632 _mesa_error(ctx, GL_INVALID_ENUM,
633 "glTransformFeedbackVaryings(bufferMode)");
634 return;
635 }
636
637 if (count < 0 ||
638 (bufferMode == GL_SEPARATE_ATTRIBS &&
639 (GLuint) count > ctx->Const.MaxTransformFeedbackBuffers)) {
640 _mesa_error(ctx, GL_INVALID_VALUE,
641 "glTransformFeedbackVaryings(count=%d)", count);
642 return;
643 }
644
645 shProg = _mesa_lookup_shader_program(ctx, program);
646 if (!shProg) {
647 _mesa_error(ctx, GL_INVALID_VALUE,
648 "glTransformFeedbackVaryings(program=%u)", program);
649 return;
650 }
651
652 if (ctx->Extensions.ARB_transform_feedback3) {
653 if (bufferMode == GL_INTERLEAVED_ATTRIBS) {
654 unsigned buffers = 1;
655
656 for (i = 0; i < count; i++) {
657 if (strcmp(varyings[i], "gl_NextBuffer") == 0)
658 buffers++;
659 }
660
661 if (buffers > ctx->Const.MaxTransformFeedbackBuffers) {
662 _mesa_error(ctx, GL_INVALID_OPERATION,
663 "glTransformFeedbackVaryings(too many gl_NextBuffer "
664 "occurences)");
665 return;
666 }
667 } else {
668 for (i = 0; i < count; i++) {
669 if (strcmp(varyings[i], "gl_NextBuffer") == 0 ||
670 strcmp(varyings[i], "gl_SkipComponents1") == 0 ||
671 strcmp(varyings[i], "gl_SkipComponents2") == 0 ||
672 strcmp(varyings[i], "gl_SkipComponents3") == 0 ||
673 strcmp(varyings[i], "gl_SkipComponents4") == 0) {
674 _mesa_error(ctx, GL_INVALID_OPERATION,
675 "glTransformFeedbackVaryings(SEPARATE_ATTRIBS,"
676 "varying=%s)",
677 varyings[i]);
678 return;
679 }
680 }
681 }
682 }
683
684 /* free existing varyings, if any */
685 for (i = 0; i < (GLint) shProg->TransformFeedback.NumVarying; i++) {
686 free(shProg->TransformFeedback.VaryingNames[i]);
687 }
688 free(shProg->TransformFeedback.VaryingNames);
689
690 /* allocate new memory for varying names */
691 shProg->TransformFeedback.VaryingNames =
692 malloc(count * sizeof(GLchar *));
693
694 if (!shProg->TransformFeedback.VaryingNames) {
695 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTransformFeedbackVaryings()");
696 return;
697 }
698
699 /* Save the new names and the count */
700 for (i = 0; i < count; i++) {
701 shProg->TransformFeedback.VaryingNames[i] = _mesa_strdup(varyings[i]);
702 }
703 shProg->TransformFeedback.NumVarying = count;
704
705 shProg->TransformFeedback.BufferMode = bufferMode;
706
707 /* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since
708 * the varyings won't be used until shader link time.
709 */
710 }
711
712
713 /**
714 * Get info about the vertex shader's outputs which are to be written
715 * to the feedback buffer(s).
716 */
717 void GLAPIENTRY
718 _mesa_GetTransformFeedbackVarying(GLuint program, GLuint index,
719 GLsizei bufSize, GLsizei *length,
720 GLsizei *size, GLenum *type, GLchar *name)
721 {
722 const struct gl_shader_program *shProg;
723 const struct gl_transform_feedback_info *linked_xfb_info;
724 GET_CURRENT_CONTEXT(ctx);
725
726 shProg = _mesa_lookup_shader_program(ctx, program);
727 if (!shProg) {
728 _mesa_error(ctx, GL_INVALID_VALUE,
729 "glGetTransformFeedbackVarying(program=%u)", program);
730 return;
731 }
732
733 linked_xfb_info = &shProg->LinkedTransformFeedback;
734 if (index >= (GLuint) linked_xfb_info->NumVarying) {
735 _mesa_error(ctx, GL_INVALID_VALUE,
736 "glGetTransformFeedbackVarying(index=%u)", index);
737 return;
738 }
739
740 /* return the varying's name and length */
741 _mesa_copy_string(name, bufSize, length,
742 linked_xfb_info->Varyings[index].Name);
743
744 /* return the datatype and value's size (in datatype units) */
745 if (type)
746 *type = linked_xfb_info->Varyings[index].Type;
747 if (size)
748 *size = linked_xfb_info->Varyings[index].Size;
749 }
750
751
752
753 struct gl_transform_feedback_object *
754 _mesa_lookup_transform_feedback_object(struct gl_context *ctx, GLuint name)
755 {
756 if (name == 0) {
757 return ctx->TransformFeedback.DefaultObject;
758 }
759 else
760 return (struct gl_transform_feedback_object *)
761 _mesa_HashLookup(ctx->TransformFeedback.Objects, name);
762 }
763
764
765 /**
766 * Create new transform feedback objects. Transform feedback objects
767 * encapsulate the state related to transform feedback to allow quickly
768 * switching state (and drawing the results, below).
769 * Part of GL_ARB_transform_feedback2.
770 */
771 void GLAPIENTRY
772 _mesa_GenTransformFeedbacks(GLsizei n, GLuint *names)
773 {
774 GLuint first;
775 GET_CURRENT_CONTEXT(ctx);
776
777 if (n < 0) {
778 _mesa_error(ctx, GL_INVALID_VALUE, "glGenTransformFeedbacks(n < 0)");
779 return;
780 }
781
782 if (!names)
783 return;
784
785 /* we don't need contiguous IDs, but this might be faster */
786 first = _mesa_HashFindFreeKeyBlock(ctx->TransformFeedback.Objects, n);
787 if (first) {
788 GLsizei i;
789 for (i = 0; i < n; i++) {
790 struct gl_transform_feedback_object *obj
791 = ctx->Driver.NewTransformFeedback(ctx, first + i);
792 if (!obj) {
793 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTransformFeedbacks");
794 return;
795 }
796 names[i] = first + i;
797 _mesa_HashInsert(ctx->TransformFeedback.Objects, first + i, obj);
798 }
799 }
800 else {
801 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenTransformFeedbacks");
802 }
803 }
804
805
806 /**
807 * Is the given ID a transform feedback object?
808 * Part of GL_ARB_transform_feedback2.
809 */
810 GLboolean GLAPIENTRY
811 _mesa_IsTransformFeedback(GLuint name)
812 {
813 struct gl_transform_feedback_object *obj;
814 GET_CURRENT_CONTEXT(ctx);
815
816 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
817
818 if (name == 0)
819 return GL_FALSE;
820
821 obj = _mesa_lookup_transform_feedback_object(ctx, name);
822 if (obj == NULL)
823 return GL_FALSE;
824
825 return obj->EverBound;
826 }
827
828
829 /**
830 * Bind the given transform feedback object.
831 * Part of GL_ARB_transform_feedback2.
832 */
833 void GLAPIENTRY
834 _mesa_BindTransformFeedback(GLenum target, GLuint name)
835 {
836 struct gl_transform_feedback_object *obj;
837 GET_CURRENT_CONTEXT(ctx);
838
839 if (target != GL_TRANSFORM_FEEDBACK) {
840 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTransformFeedback(target)");
841 return;
842 }
843
844 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
845 _mesa_error(ctx, GL_INVALID_OPERATION,
846 "glBindTransformFeedback(transform is active, or not paused)");
847 return;
848 }
849
850 obj = _mesa_lookup_transform_feedback_object(ctx, name);
851 if (!obj) {
852 _mesa_error(ctx, GL_INVALID_OPERATION,
853 "glBindTransformFeedback(name=%u)", name);
854 return;
855 }
856
857 reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject,
858 obj);
859 }
860
861
862 /**
863 * Delete the given transform feedback objects.
864 * Part of GL_ARB_transform_feedback2.
865 */
866 void GLAPIENTRY
867 _mesa_DeleteTransformFeedbacks(GLsizei n, const GLuint *names)
868 {
869 GLint i;
870 GET_CURRENT_CONTEXT(ctx);
871
872 if (n < 0) {
873 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteTransformFeedbacks(n < 0)");
874 return;
875 }
876
877 if (!names)
878 return;
879
880 for (i = 0; i < n; i++) {
881 if (names[i] > 0) {
882 struct gl_transform_feedback_object *obj
883 = _mesa_lookup_transform_feedback_object(ctx, names[i]);
884 if (obj) {
885 if (obj->Active) {
886 _mesa_error(ctx, GL_INVALID_OPERATION,
887 "glDeleteTransformFeedbacks(object %u is active)",
888 names[i]);
889 return;
890 }
891 _mesa_HashRemove(ctx->TransformFeedback.Objects, names[i]);
892 /* unref, but object may not be deleted until later */
893 reference_transform_feedback_object(&obj, NULL);
894 }
895 }
896 }
897 }
898
899
900 /**
901 * Pause transform feedback.
902 * Part of GL_ARB_transform_feedback2.
903 */
904 void GLAPIENTRY
905 _mesa_PauseTransformFeedback(void)
906 {
907 struct gl_transform_feedback_object *obj;
908 GET_CURRENT_CONTEXT(ctx);
909
910 obj = ctx->TransformFeedback.CurrentObject;
911
912 if (!_mesa_is_xfb_active_and_unpaused(ctx)) {
913 _mesa_error(ctx, GL_INVALID_OPERATION,
914 "glPauseTransformFeedback(feedback not active or already paused)");
915 return;
916 }
917
918 FLUSH_VERTICES(ctx, 0);
919 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
920
921 obj->Paused = GL_TRUE;
922
923 assert(ctx->Driver.PauseTransformFeedback);
924 ctx->Driver.PauseTransformFeedback(ctx, obj);
925 }
926
927
928 /**
929 * Resume transform feedback.
930 * Part of GL_ARB_transform_feedback2.
931 */
932 void GLAPIENTRY
933 _mesa_ResumeTransformFeedback(void)
934 {
935 struct gl_transform_feedback_object *obj;
936 GET_CURRENT_CONTEXT(ctx);
937
938 obj = ctx->TransformFeedback.CurrentObject;
939
940 if (!obj->Active || !obj->Paused) {
941 _mesa_error(ctx, GL_INVALID_OPERATION,
942 "glResumeTransformFeedback(feedback not active or not paused)");
943 return;
944 }
945
946 FLUSH_VERTICES(ctx, 0);
947 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
948
949 obj->Paused = GL_FALSE;
950
951 assert(ctx->Driver.ResumeTransformFeedback);
952 ctx->Driver.ResumeTransformFeedback(ctx, obj);
953 }