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