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