st/mesa: call glthread_destroy() before _vbo_DestroyContext()
[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 * Transform feedback support.
28 *
29 * Authors:
30 * Brian Paul
31 */
32
33
34 #include "buffers.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 struct using_program_tuple
47 {
48 struct gl_program *prog;
49 bool found;
50 };
51
52 static void
53 active_xfb_object_references_program(GLuint key, void *data, void *user_data)
54 {
55 struct using_program_tuple *callback_data = user_data;
56 struct gl_transform_feedback_object *obj = data;
57 if (obj->Active && obj->program == callback_data->prog)
58 callback_data->found = true;
59 }
60
61 /**
62 * Return true if any active transform feedback object is using a program.
63 */
64 bool
65 _mesa_transform_feedback_is_using_program(struct gl_context *ctx,
66 struct gl_shader_program *shProg)
67 {
68 if (!shProg->last_vert_prog)
69 return false;
70
71 struct using_program_tuple callback_data;
72 callback_data.found = false;
73 callback_data.prog = shProg->last_vert_prog;
74
75 _mesa_HashWalk(ctx->TransformFeedback.Objects,
76 active_xfb_object_references_program, &callback_data);
77
78 /* Also check DefaultObject, as it's not in the Objects hash table. */
79 active_xfb_object_references_program(0, ctx->TransformFeedback.DefaultObject,
80 &callback_data);
81
82 return callback_data.found;
83 }
84
85 /**
86 * Do reference counting of transform feedback buffers.
87 */
88 static void
89 reference_transform_feedback_object(struct gl_transform_feedback_object **ptr,
90 struct gl_transform_feedback_object *obj)
91 {
92 if (*ptr == obj)
93 return;
94
95 if (*ptr) {
96 /* Unreference the old object */
97 struct gl_transform_feedback_object *oldObj = *ptr;
98
99 assert(oldObj->RefCount > 0);
100 oldObj->RefCount--;
101
102 if (oldObj->RefCount == 0) {
103 GET_CURRENT_CONTEXT(ctx);
104 if (ctx)
105 ctx->Driver.DeleteTransformFeedback(ctx, oldObj);
106 }
107
108 *ptr = NULL;
109 }
110 assert(!*ptr);
111
112 if (obj) {
113 /* reference new object */
114 if (obj->RefCount == 0) {
115 _mesa_problem(NULL, "referencing deleted transform feedback object");
116 *ptr = NULL;
117 }
118 else {
119 obj->RefCount++;
120 obj->EverBound = GL_TRUE;
121 *ptr = obj;
122 }
123 }
124 }
125
126
127 /**
128 * Check that all the buffer objects currently bound for transform
129 * feedback actually exist. Raise a GL_INVALID_OPERATION error if
130 * any buffers are missing.
131 * \return GL_TRUE for success, GL_FALSE if error
132 */
133 GLboolean
134 _mesa_validate_transform_feedback_buffers(struct gl_context *ctx)
135 {
136 /* XXX to do */
137 return GL_TRUE;
138 }
139
140
141
142 /**
143 * Per-context init for transform feedback.
144 */
145 void
146 _mesa_init_transform_feedback(struct gl_context *ctx)
147 {
148 /* core mesa expects this, even a dummy one, to be available */
149 assert(ctx->Driver.NewTransformFeedback);
150
151 ctx->TransformFeedback.DefaultObject =
152 ctx->Driver.NewTransformFeedback(ctx, 0);
153
154 assert(ctx->TransformFeedback.DefaultObject->RefCount == 1);
155
156 reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject,
157 ctx->TransformFeedback.DefaultObject);
158
159 assert(ctx->TransformFeedback.DefaultObject->RefCount == 2);
160
161 ctx->TransformFeedback.Objects = _mesa_NewHashTable();
162
163 _mesa_reference_buffer_object(ctx,
164 &ctx->TransformFeedback.CurrentBuffer,
165 ctx->Shared->NullBufferObj);
166 }
167
168
169
170 /**
171 * Callback for _mesa_HashDeleteAll().
172 */
173 static void
174 delete_cb(GLuint key, void *data, void *userData)
175 {
176 struct gl_context *ctx = (struct gl_context *) userData;
177 struct gl_transform_feedback_object *obj =
178 (struct gl_transform_feedback_object *) data;
179
180 ctx->Driver.DeleteTransformFeedback(ctx, obj);
181 }
182
183
184 /**
185 * Per-context free/clean-up for transform feedback.
186 */
187 void
188 _mesa_free_transform_feedback(struct gl_context *ctx)
189 {
190 /* core mesa expects this, even a dummy one, to be available */
191 assert(ctx->Driver.NewTransformFeedback);
192
193 _mesa_reference_buffer_object(ctx,
194 &ctx->TransformFeedback.CurrentBuffer,
195 NULL);
196
197 /* Delete all feedback objects */
198 _mesa_HashDeleteAll(ctx->TransformFeedback.Objects, delete_cb, ctx);
199 _mesa_DeleteHashTable(ctx->TransformFeedback.Objects);
200
201 /* Delete the default feedback object */
202 assert(ctx->Driver.DeleteTransformFeedback);
203 ctx->Driver.DeleteTransformFeedback(ctx,
204 ctx->TransformFeedback.DefaultObject);
205
206 ctx->TransformFeedback.CurrentObject = NULL;
207 }
208
209
210 /** Initialize the fields of a gl_transform_feedback_object. */
211 void
212 _mesa_init_transform_feedback_object(struct gl_transform_feedback_object *obj,
213 GLuint name)
214 {
215 if (!obj)
216 return;
217
218 obj->Name = name;
219 obj->RefCount = 1;
220 obj->EverBound = GL_FALSE;
221 }
222
223
224 /** Default fallback for ctx->Driver.NewTransformFeedback() */
225 static struct gl_transform_feedback_object *
226 new_transform_feedback(struct gl_context *ctx, GLuint name)
227 {
228 struct gl_transform_feedback_object *obj;
229 obj = CALLOC_STRUCT(gl_transform_feedback_object);
230 _mesa_init_transform_feedback_object(obj, name);
231 return obj;
232 }
233
234 /** Default fallback for ctx->Driver.DeleteTransformFeedback() */
235 static void
236 delete_transform_feedback(struct gl_context *ctx,
237 struct gl_transform_feedback_object *obj)
238 {
239 GLuint i;
240
241 for (i = 0; i < ARRAY_SIZE(obj->Buffers); i++) {
242 _mesa_reference_buffer_object(ctx, &obj->Buffers[i], NULL);
243 }
244
245 free(obj->Label);
246 free(obj);
247 }
248
249
250 /** Default fallback for ctx->Driver.BeginTransformFeedback() */
251 static void
252 begin_transform_feedback(struct gl_context *ctx, GLenum mode,
253 struct gl_transform_feedback_object *obj)
254 {
255 /* nop */
256 }
257
258 /** Default fallback for ctx->Driver.EndTransformFeedback() */
259 static void
260 end_transform_feedback(struct gl_context *ctx,
261 struct gl_transform_feedback_object *obj)
262 {
263 /* nop */
264 }
265
266 /** Default fallback for ctx->Driver.PauseTransformFeedback() */
267 static void
268 pause_transform_feedback(struct gl_context *ctx,
269 struct gl_transform_feedback_object *obj)
270 {
271 /* nop */
272 }
273
274 /** Default fallback for ctx->Driver.ResumeTransformFeedback() */
275 static void
276 resume_transform_feedback(struct gl_context *ctx,
277 struct gl_transform_feedback_object *obj)
278 {
279 /* nop */
280 }
281
282
283 /**
284 * Plug in default device driver functions for transform feedback.
285 * Most drivers will override some/all of these.
286 */
287 void
288 _mesa_init_transform_feedback_functions(struct dd_function_table *driver)
289 {
290 driver->NewTransformFeedback = new_transform_feedback;
291 driver->DeleteTransformFeedback = delete_transform_feedback;
292 driver->BeginTransformFeedback = begin_transform_feedback;
293 driver->EndTransformFeedback = end_transform_feedback;
294 driver->PauseTransformFeedback = pause_transform_feedback;
295 driver->ResumeTransformFeedback = resume_transform_feedback;
296 }
297
298
299 /**
300 * Fill in the correct Size value for each buffer in \c obj.
301 *
302 * From the GL 4.3 spec, section 6.1.1 ("Binding Buffer Objects to Indexed
303 * Targets"):
304 *
305 * BindBufferBase binds the entire buffer, even when the size of the buffer
306 * is changed after the binding is established. It is equivalent to calling
307 * BindBufferRange with offset zero, while size is determined by the size of
308 * the bound buffer at the time the binding is used.
309 *
310 * Regardless of the size specified with BindBufferRange, or indirectly with
311 * BindBufferBase, the GL will never read or write beyond the end of a bound
312 * buffer. In some cases this constraint may result in visibly different
313 * behavior when a buffer overflow would otherwise result, such as described
314 * for transform feedback operations in section 13.2.2.
315 */
316 static void
317 compute_transform_feedback_buffer_sizes(
318 struct gl_transform_feedback_object *obj)
319 {
320 unsigned i = 0;
321 for (i = 0; i < MAX_FEEDBACK_BUFFERS; ++i) {
322 GLintptr offset = obj->Offset[i];
323 GLsizeiptr buffer_size
324 = obj->Buffers[i] == NULL ? 0 : obj->Buffers[i]->Size;
325 GLsizeiptr available_space
326 = buffer_size <= offset ? 0 : buffer_size - offset;
327 GLsizeiptr computed_size;
328 if (obj->RequestedSize[i] == 0) {
329 /* No size was specified at the time the buffer was bound, so allow
330 * writing to all available space in the buffer.
331 */
332 computed_size = available_space;
333 } else {
334 /* A size was specified at the time the buffer was bound, however
335 * it's possible that the buffer has shrunk since then. So only
336 * allow writing to the minimum of the specified size and the space
337 * available.
338 */
339 computed_size = MIN2(available_space, obj->RequestedSize[i]);
340 }
341
342 /* Legal sizes must be multiples of four, so round down if necessary. */
343 obj->Size[i] = computed_size & ~0x3;
344 }
345 }
346
347
348 /**
349 * Compute the maximum number of vertices that can be written to the currently
350 * enabled transform feedback buffers without overflowing any of them.
351 */
352 unsigned
353 _mesa_compute_max_transform_feedback_vertices(struct gl_context *ctx,
354 const struct gl_transform_feedback_object *obj,
355 const struct gl_transform_feedback_info *info)
356 {
357 unsigned max_index = 0xffffffff;
358 unsigned i;
359
360 for (i = 0; i < ctx->Const.MaxTransformFeedbackBuffers; i++) {
361 if ((info->ActiveBuffers >> i) & 1) {
362 unsigned stride = info->Buffers[i].Stride;
363 unsigned max_for_this_buffer;
364
365 /* Skip any inactive buffers, which have a stride of 0. */
366 if (stride == 0)
367 continue;
368
369 max_for_this_buffer = obj->Size[i] / (4 * stride);
370 max_index = MIN2(max_index, max_for_this_buffer);
371 }
372 }
373
374 return max_index;
375 }
376
377
378 /**
379 ** Begin API functions
380 **/
381
382
383 /**
384 * Figure out which stage of the pipeline is the source of transform feedback
385 * data given the current context state, and return its gl_program.
386 *
387 * If no active program can generate transform feedback data (i.e. no vertex
388 * shader is active), returns NULL.
389 */
390 static struct gl_program *
391 get_xfb_source(struct gl_context *ctx)
392 {
393 int i;
394 for (i = MESA_SHADER_GEOMETRY; i >= MESA_SHADER_VERTEX; i--) {
395 if (ctx->_Shader->CurrentProgram[i] != NULL)
396 return ctx->_Shader->CurrentProgram[i];
397 }
398 return NULL;
399 }
400
401
402 void GLAPIENTRY
403 _mesa_BeginTransformFeedback(GLenum mode)
404 {
405 struct gl_transform_feedback_object *obj;
406 struct gl_transform_feedback_info *info = NULL;
407 GLuint i;
408 unsigned vertices_per_prim;
409 GET_CURRENT_CONTEXT(ctx);
410
411 obj = ctx->TransformFeedback.CurrentObject;
412
413 /* Figure out what pipeline stage is the source of data for transform
414 * feedback.
415 */
416 struct gl_program *source = get_xfb_source(ctx);
417 if (source == NULL) {
418 _mesa_error(ctx, GL_INVALID_OPERATION,
419 "glBeginTransformFeedback(no program active)");
420 return;
421 }
422
423 info = source->sh.LinkedTransformFeedback;
424
425 if (info->NumOutputs == 0) {
426 _mesa_error(ctx, GL_INVALID_OPERATION,
427 "glBeginTransformFeedback(no varyings to record)");
428 return;
429 }
430
431 switch (mode) {
432 case GL_POINTS:
433 vertices_per_prim = 1;
434 break;
435 case GL_LINES:
436 vertices_per_prim = 2;
437 break;
438 case GL_TRIANGLES:
439 vertices_per_prim = 3;
440 break;
441 default:
442 _mesa_error(ctx, GL_INVALID_ENUM, "glBeginTransformFeedback(mode)");
443 return;
444 }
445
446 if (obj->Active) {
447 _mesa_error(ctx, GL_INVALID_OPERATION,
448 "glBeginTransformFeedback(already active)");
449 return;
450 }
451
452 for (i = 0; i < ctx->Const.MaxTransformFeedbackBuffers; i++) {
453 if ((info->ActiveBuffers >> i) & 1) {
454 if (obj->BufferNames[i] == 0) {
455 _mesa_error(ctx, GL_INVALID_OPERATION,
456 "glBeginTransformFeedback(binding point %d does not "
457 "have a buffer object bound)", i);
458 return;
459 }
460 }
461 }
462
463 FLUSH_VERTICES(ctx, 0);
464 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
465
466 obj->Active = GL_TRUE;
467 ctx->TransformFeedback.Mode = mode;
468
469 compute_transform_feedback_buffer_sizes(obj);
470
471 if (_mesa_is_gles3(ctx)) {
472 /* In GLES3, we are required to track the usage of the transform
473 * feedback buffer and report INVALID_OPERATION if a draw call tries to
474 * exceed it. So compute the maximum number of vertices that we can
475 * write without overflowing any of the buffers currently being used for
476 * feedback.
477 */
478 unsigned max_vertices
479 = _mesa_compute_max_transform_feedback_vertices(ctx, obj, info);
480 obj->GlesRemainingPrims = max_vertices / vertices_per_prim;
481 }
482
483 if (obj->program != source) {
484 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedbackProg;
485 obj->program = source;
486 }
487
488 assert(ctx->Driver.BeginTransformFeedback);
489 ctx->Driver.BeginTransformFeedback(ctx, mode, obj);
490 }
491
492
493 void GLAPIENTRY
494 _mesa_EndTransformFeedback(void)
495 {
496 struct gl_transform_feedback_object *obj;
497 GET_CURRENT_CONTEXT(ctx);
498
499 obj = ctx->TransformFeedback.CurrentObject;
500
501 if (!obj->Active) {
502 _mesa_error(ctx, GL_INVALID_OPERATION,
503 "glEndTransformFeedback(not active)");
504 return;
505 }
506
507 FLUSH_VERTICES(ctx, 0);
508 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
509
510 assert(ctx->Driver.EndTransformFeedback);
511 ctx->Driver.EndTransformFeedback(ctx, obj);
512
513 ctx->TransformFeedback.CurrentObject->Active = GL_FALSE;
514 ctx->TransformFeedback.CurrentObject->Paused = GL_FALSE;
515 ctx->TransformFeedback.CurrentObject->EndedAnytime = GL_TRUE;
516 }
517
518
519 /**
520 * Helper used by BindBufferRange() and BindBufferBase().
521 */
522 static void
523 bind_buffer_range(struct gl_context *ctx,
524 struct gl_transform_feedback_object *obj,
525 GLuint index,
526 struct gl_buffer_object *bufObj,
527 GLintptr offset, GLsizeiptr size,
528 bool dsa)
529 {
530 /* Note: no need to FLUSH_VERTICES or flag NewTransformFeedback, because
531 * transform feedback buffers can't be changed while transform feedback is
532 * active.
533 */
534
535 if (!dsa) {
536 /* The general binding point */
537 _mesa_reference_buffer_object(ctx,
538 &ctx->TransformFeedback.CurrentBuffer,
539 bufObj);
540 }
541
542 /* The per-attribute binding point */
543 _mesa_set_transform_feedback_binding(ctx, obj, index, bufObj, offset, size);
544 }
545
546
547 /**
548 * Specify a buffer object to receive transform feedback results. Plus,
549 * specify the starting offset to place the results, and max size.
550 * Called from the glBindBufferRange() and glTransformFeedbackBufferRange
551 * functions.
552 */
553 void
554 _mesa_bind_buffer_range_transform_feedback(struct gl_context *ctx,
555 struct gl_transform_feedback_object *obj,
556 GLuint index,
557 struct gl_buffer_object *bufObj,
558 GLintptr offset,
559 GLsizeiptr size,
560 bool dsa)
561 {
562 const char *gl_methd_name;
563 if (dsa)
564 gl_methd_name = "glTransformFeedbackBufferRange";
565 else
566 gl_methd_name = "glBindBufferRange";
567
568
569 if (obj->Active) {
570 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(transform feedback active)",
571 gl_methd_name);
572 return;
573 }
574
575 if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
576 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
577 * generated if index is greater than or equal to the number of binding
578 * points for transform feedback, as described in section 6.7.1."
579 */
580 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d out of bounds)",
581 gl_methd_name, index);
582 return;
583 }
584
585 if (size & 0x3) {
586 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
587 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d must be a multiple of "
588 "four)", gl_methd_name, (int) size);
589 return;
590 }
591
592 if (offset & 0x3) {
593 /* OpenGL 4.5 core profile, 6.7, pdf page 103: multiple of 4 */
594 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d must be a multiple "
595 "of four)", gl_methd_name, (int) offset);
596 return;
597 }
598
599 if (offset < 0) {
600 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
601 * generated by BindBufferRange if offset is negative."
602 *
603 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
604 * is generated by TransformFeedbackBufferRange if offset is negative."
605 */
606 _mesa_error(ctx, GL_INVALID_VALUE, "%s(offset=%d must be >= 0)",
607 gl_methd_name,
608 (int) offset);
609 return;
610 }
611
612 if (size <= 0 && (dsa || bufObj != ctx->Shared->NullBufferObj)) {
613 /* OpenGL 4.5 core profile, 6.1, pdf page 82: "An INVALID_VALUE error is
614 * generated by BindBufferRange if buffer is non-zero and size is less
615 * than or equal to zero."
616 *
617 * OpenGL 4.5 core profile, 13.2, pdf page 445: "An INVALID_VALUE error
618 * is generated by TransformFeedbackBufferRange if size is less than or
619 * equal to zero."
620 */
621 _mesa_error(ctx, GL_INVALID_VALUE, "%s(size=%d must be > 0)",
622 gl_methd_name, (int) size);
623 return;
624 }
625
626 bind_buffer_range(ctx, obj, index, bufObj, offset, size, dsa);
627 }
628
629
630 /**
631 * Specify a buffer object to receive transform feedback results.
632 * As above, but start at offset = 0.
633 * Called from the glBindBufferBase() and glTransformFeedbackBufferBase()
634 * functions.
635 */
636 void
637 _mesa_bind_buffer_base_transform_feedback(struct gl_context *ctx,
638 struct gl_transform_feedback_object *obj,
639 GLuint index,
640 struct gl_buffer_object *bufObj,
641 bool dsa)
642 {
643 if (obj->Active) {
644 _mesa_error(ctx, GL_INVALID_OPERATION,
645 "%s(transform feedback active)",
646 dsa ? "glTransformFeedbackBufferBase" : "glBindBufferBase");
647 return;
648 }
649
650 if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
651 _mesa_error(ctx, GL_INVALID_VALUE, "%s(index=%d out of bounds)",
652 dsa ? "glTransformFeedbackBufferBase" : "glBindBufferBase",
653 index);
654 return;
655 }
656
657 bind_buffer_range(ctx, obj, index, bufObj, 0, 0, dsa);
658 }
659
660 /**
661 * Wrapper around lookup_transform_feedback_object that throws
662 * GL_INVALID_OPERATION if id is not in the hash table. After calling
663 * _mesa_error, it returns NULL.
664 */
665 static struct gl_transform_feedback_object *
666 lookup_transform_feedback_object_err(struct gl_context *ctx,
667 GLuint xfb, const char* func)
668 {
669 struct gl_transform_feedback_object *obj;
670
671 obj = _mesa_lookup_transform_feedback_object(ctx, xfb);
672 if (!obj) {
673 _mesa_error(ctx, GL_INVALID_OPERATION,
674 "%s(xfb=%u: non-generated object name)", func, xfb);
675 }
676
677 return obj;
678 }
679
680 /**
681 * Wrapper around _mesa_lookup_bufferobj that throws GL_INVALID_VALUE if id
682 * is not in the hash table. Specialised version for the
683 * transform-feedback-related functions. After calling _mesa_error, it
684 * returns NULL.
685 */
686 static struct gl_buffer_object *
687 lookup_transform_feedback_bufferobj_err(struct gl_context *ctx,
688 GLuint buffer, const char* func)
689 {
690 struct gl_buffer_object *bufObj;
691
692 /* OpenGL 4.5 core profile, 13.2, pdf page 444: buffer must be zero or the
693 * name of an existing buffer object.
694 */
695 if (buffer == 0) {
696 bufObj = ctx->Shared->NullBufferObj;
697 } else {
698 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
699 if (!bufObj) {
700 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid buffer=%u)", func,
701 buffer);
702 }
703 }
704
705 return bufObj;
706 }
707
708 void GLAPIENTRY
709 _mesa_TransformFeedbackBufferBase(GLuint xfb, GLuint index, GLuint buffer)
710 {
711 GET_CURRENT_CONTEXT(ctx);
712 struct gl_transform_feedback_object *obj;
713 struct gl_buffer_object *bufObj;
714
715 obj = lookup_transform_feedback_object_err(ctx, xfb,
716 "glTransformFeedbackBufferBase");
717 if(!obj) {
718 return;
719 }
720
721 bufObj = lookup_transform_feedback_bufferobj_err(ctx, buffer,
722 "glTransformFeedbackBufferBase");
723 if(!bufObj) {
724 return;
725 }
726
727 _mesa_bind_buffer_base_transform_feedback(ctx, obj, index, bufObj, true);
728 }
729
730 void GLAPIENTRY
731 _mesa_TransformFeedbackBufferRange(GLuint xfb, GLuint index, GLuint buffer,
732 GLintptr offset, GLsizeiptr size)
733 {
734 GET_CURRENT_CONTEXT(ctx);
735 struct gl_transform_feedback_object *obj;
736 struct gl_buffer_object *bufObj;
737
738 obj = lookup_transform_feedback_object_err(ctx, xfb,
739 "glTransformFeedbackBufferRange");
740 if(!obj) {
741 return;
742 }
743
744 bufObj = lookup_transform_feedback_bufferobj_err(ctx, buffer,
745 "glTransformFeedbackBufferRange");
746 if(!bufObj) {
747 return;
748 }
749
750 _mesa_bind_buffer_range_transform_feedback(ctx, obj, index, bufObj, offset,
751 size, true);
752 }
753
754 /**
755 * Specify a buffer object to receive transform feedback results, plus the
756 * offset in the buffer to start placing results.
757 * This function is part of GL_EXT_transform_feedback, but not GL3.
758 */
759 void GLAPIENTRY
760 _mesa_BindBufferOffsetEXT(GLenum target, GLuint index, GLuint buffer,
761 GLintptr offset)
762 {
763 struct gl_transform_feedback_object *obj;
764 struct gl_buffer_object *bufObj;
765 GET_CURRENT_CONTEXT(ctx);
766
767 if (target != GL_TRANSFORM_FEEDBACK_BUFFER) {
768 _mesa_error(ctx, GL_INVALID_ENUM, "glBindBufferOffsetEXT(target)");
769 return;
770 }
771
772 obj = ctx->TransformFeedback.CurrentObject;
773
774 if (obj->Active) {
775 _mesa_error(ctx, GL_INVALID_OPERATION,
776 "glBindBufferOffsetEXT(transform feedback active)");
777 return;
778 }
779
780 if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
781 _mesa_error(ctx, GL_INVALID_VALUE,
782 "glBindBufferOffsetEXT(index=%d)", index);
783 return;
784 }
785
786 if (offset & 0x3) {
787 /* must be multiple of four */
788 _mesa_error(ctx, GL_INVALID_VALUE,
789 "glBindBufferOffsetEXT(offset=%d)", (int) offset);
790 return;
791 }
792
793 if (buffer == 0) {
794 bufObj = ctx->Shared->NullBufferObj;
795 } else {
796 bufObj = _mesa_lookup_bufferobj(ctx, buffer);
797 }
798
799 if (!bufObj) {
800 _mesa_error(ctx, GL_INVALID_OPERATION,
801 "glBindBufferOffsetEXT(invalid buffer=%u)", buffer);
802 return;
803 }
804
805 bind_buffer_range(ctx, obj, index, bufObj, offset, 0, false);
806 }
807
808
809 /**
810 * This function specifies the transform feedback outputs to be written
811 * to the feedback buffer(s), and in what order.
812 */
813 void GLAPIENTRY
814 _mesa_TransformFeedbackVaryings(GLuint program, GLsizei count,
815 const GLchar * const *varyings,
816 GLenum bufferMode)
817 {
818 struct gl_shader_program *shProg;
819 GLint i;
820 GET_CURRENT_CONTEXT(ctx);
821
822 /* From the ARB_transform_feedback2 specification:
823 * "The error INVALID_OPERATION is generated by TransformFeedbackVaryings
824 * if the current transform feedback object is active, even if paused."
825 */
826 if (ctx->TransformFeedback.CurrentObject->Active) {
827 _mesa_error(ctx, GL_INVALID_OPERATION,
828 "glTransformFeedbackVaryings(current object is active)");
829 return;
830 }
831
832 switch (bufferMode) {
833 case GL_INTERLEAVED_ATTRIBS:
834 break;
835 case GL_SEPARATE_ATTRIBS:
836 break;
837 default:
838 _mesa_error(ctx, GL_INVALID_ENUM,
839 "glTransformFeedbackVaryings(bufferMode)");
840 return;
841 }
842
843 if (count < 0 ||
844 (bufferMode == GL_SEPARATE_ATTRIBS &&
845 (GLuint) count > ctx->Const.MaxTransformFeedbackBuffers)) {
846 _mesa_error(ctx, GL_INVALID_VALUE,
847 "glTransformFeedbackVaryings(count=%d)", count);
848 return;
849 }
850
851 shProg = _mesa_lookup_shader_program_err(ctx, program,
852 "glTransformFeedbackVaryings");
853 if (!shProg)
854 return;
855
856 if (ctx->Extensions.ARB_transform_feedback3) {
857 if (bufferMode == GL_INTERLEAVED_ATTRIBS) {
858 unsigned buffers = 1;
859
860 for (i = 0; i < count; i++) {
861 if (strcmp(varyings[i], "gl_NextBuffer") == 0)
862 buffers++;
863 }
864
865 if (buffers > ctx->Const.MaxTransformFeedbackBuffers) {
866 _mesa_error(ctx, GL_INVALID_OPERATION,
867 "glTransformFeedbackVaryings(too many gl_NextBuffer "
868 "occurrences)");
869 return;
870 }
871 } else {
872 for (i = 0; i < count; i++) {
873 if (strcmp(varyings[i], "gl_NextBuffer") == 0 ||
874 strcmp(varyings[i], "gl_SkipComponents1") == 0 ||
875 strcmp(varyings[i], "gl_SkipComponents2") == 0 ||
876 strcmp(varyings[i], "gl_SkipComponents3") == 0 ||
877 strcmp(varyings[i], "gl_SkipComponents4") == 0) {
878 _mesa_error(ctx, GL_INVALID_OPERATION,
879 "glTransformFeedbackVaryings(SEPARATE_ATTRIBS,"
880 "varying=%s)",
881 varyings[i]);
882 return;
883 }
884 }
885 }
886 }
887
888 /* free existing varyings, if any */
889 for (i = 0; i < (GLint) shProg->TransformFeedback.NumVarying; i++) {
890 free(shProg->TransformFeedback.VaryingNames[i]);
891 }
892 free(shProg->TransformFeedback.VaryingNames);
893
894 /* allocate new memory for varying names */
895 shProg->TransformFeedback.VaryingNames =
896 malloc(count * sizeof(GLchar *));
897
898 if (!shProg->TransformFeedback.VaryingNames) {
899 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTransformFeedbackVaryings()");
900 return;
901 }
902
903 /* Save the new names and the count */
904 for (i = 0; i < count; i++) {
905 shProg->TransformFeedback.VaryingNames[i] = strdup(varyings[i]);
906 }
907 shProg->TransformFeedback.NumVarying = count;
908
909 shProg->TransformFeedback.BufferMode = bufferMode;
910
911 /* No need to invoke FLUSH_VERTICES or flag NewTransformFeedback since
912 * the varyings won't be used until shader link time.
913 */
914 }
915
916
917 /**
918 * Get info about the transform feedback outputs which are to be written
919 * to the feedback buffer(s).
920 */
921 void GLAPIENTRY
922 _mesa_GetTransformFeedbackVarying(GLuint program, GLuint index,
923 GLsizei bufSize, GLsizei *length,
924 GLsizei *size, GLenum *type, GLchar *name)
925 {
926 const struct gl_shader_program *shProg;
927 struct gl_program_resource *res;
928 GET_CURRENT_CONTEXT(ctx);
929
930 shProg = _mesa_lookup_shader_program_err(ctx, program,
931 "glGetTransformFeedbackVarying");
932 if (!shProg)
933 return;
934
935 res = _mesa_program_resource_find_index((struct gl_shader_program *) shProg,
936 GL_TRANSFORM_FEEDBACK_VARYING,
937 index);
938 if (!res) {
939 _mesa_error(ctx, GL_INVALID_VALUE,
940 "glGetTransformFeedbackVarying(index=%u)", index);
941 return;
942 }
943
944 /* return the varying's name and length */
945 _mesa_copy_string(name, bufSize, length, _mesa_program_resource_name(res));
946
947 /* return the datatype and value's size (in datatype units) */
948 if (type)
949 _mesa_program_resource_prop((struct gl_shader_program *) shProg,
950 res, index, GL_TYPE, (GLint*) type,
951 "glGetTransformFeedbackVarying");
952 if (size)
953 _mesa_program_resource_prop((struct gl_shader_program *) shProg,
954 res, index, GL_ARRAY_SIZE, (GLint*) size,
955 "glGetTransformFeedbackVarying");
956 }
957
958
959
960 struct gl_transform_feedback_object *
961 _mesa_lookup_transform_feedback_object(struct gl_context *ctx, GLuint name)
962 {
963 /* OpenGL 4.5 core profile, 13.2 pdf page 444: "xfb must be zero, indicating
964 * the default transform feedback object, or the name of an existing
965 * transform feedback object."
966 */
967 if (name == 0) {
968 return ctx->TransformFeedback.DefaultObject;
969 }
970 else
971 return (struct gl_transform_feedback_object *)
972 _mesa_HashLookup(ctx->TransformFeedback.Objects, name);
973 }
974
975 static void
976 create_transform_feedbacks(struct gl_context *ctx, GLsizei n, GLuint *ids,
977 bool dsa)
978 {
979 GLuint first;
980 const char* func;
981
982 if (dsa)
983 func = "glCreateTransformFeedbacks";
984 else
985 func = "glGenTransformFeedbacks";
986
987 if (n < 0) {
988 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
989 return;
990 }
991
992 if (!ids)
993 return;
994
995 /* we don't need contiguous IDs, but this might be faster */
996 first = _mesa_HashFindFreeKeyBlock(ctx->TransformFeedback.Objects, n);
997 if (first) {
998 GLsizei i;
999 for (i = 0; i < n; i++) {
1000 struct gl_transform_feedback_object *obj
1001 = ctx->Driver.NewTransformFeedback(ctx, first + i);
1002 if (!obj) {
1003 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1004 return;
1005 }
1006 ids[i] = first + i;
1007 _mesa_HashInsert(ctx->TransformFeedback.Objects, first + i, obj);
1008 if (dsa) {
1009 /* this is normally done at bind time in the non-dsa case */
1010 obj->EverBound = GL_TRUE;
1011 }
1012 }
1013 }
1014 else {
1015 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1016 }
1017 }
1018
1019 /**
1020 * Create new transform feedback objects. Transform feedback objects
1021 * encapsulate the state related to transform feedback to allow quickly
1022 * switching state (and drawing the results, below).
1023 * Part of GL_ARB_transform_feedback2.
1024 */
1025 void GLAPIENTRY
1026 _mesa_GenTransformFeedbacks(GLsizei n, GLuint *names)
1027 {
1028 GET_CURRENT_CONTEXT(ctx);
1029
1030 /* GenTransformFeedbacks should just reserve the object names that a
1031 * subsequent call to BindTransformFeedback should actively create. For
1032 * the sake of simplicity, we reserve the names and create the objects
1033 * straight away.
1034 */
1035
1036 create_transform_feedbacks(ctx, n, names, false);
1037 }
1038
1039 /**
1040 * Create new transform feedback objects. Transform feedback objects
1041 * encapsulate the state related to transform feedback to allow quickly
1042 * switching state (and drawing the results, below).
1043 * Part of GL_ARB_direct_state_access.
1044 */
1045 void GLAPIENTRY
1046 _mesa_CreateTransformFeedbacks(GLsizei n, GLuint *names)
1047 {
1048 GET_CURRENT_CONTEXT(ctx);
1049
1050 create_transform_feedbacks(ctx, n, names, true);
1051 }
1052
1053
1054 /**
1055 * Is the given ID a transform feedback object?
1056 * Part of GL_ARB_transform_feedback2.
1057 */
1058 GLboolean GLAPIENTRY
1059 _mesa_IsTransformFeedback(GLuint name)
1060 {
1061 struct gl_transform_feedback_object *obj;
1062 GET_CURRENT_CONTEXT(ctx);
1063
1064 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1065
1066 if (name == 0)
1067 return GL_FALSE;
1068
1069 obj = _mesa_lookup_transform_feedback_object(ctx, name);
1070 if (obj == NULL)
1071 return GL_FALSE;
1072
1073 return obj->EverBound;
1074 }
1075
1076
1077 /**
1078 * Bind the given transform feedback object.
1079 * Part of GL_ARB_transform_feedback2.
1080 */
1081 void GLAPIENTRY
1082 _mesa_BindTransformFeedback(GLenum target, GLuint name)
1083 {
1084 struct gl_transform_feedback_object *obj;
1085 GET_CURRENT_CONTEXT(ctx);
1086
1087 if (target != GL_TRANSFORM_FEEDBACK) {
1088 _mesa_error(ctx, GL_INVALID_ENUM, "glBindTransformFeedback(target)");
1089 return;
1090 }
1091
1092 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
1093 _mesa_error(ctx, GL_INVALID_OPERATION,
1094 "glBindTransformFeedback(transform is active, or not paused)");
1095 return;
1096 }
1097
1098 obj = _mesa_lookup_transform_feedback_object(ctx, name);
1099 if (!obj) {
1100 _mesa_error(ctx, GL_INVALID_OPERATION,
1101 "glBindTransformFeedback(name=%u)", name);
1102 return;
1103 }
1104
1105 reference_transform_feedback_object(&ctx->TransformFeedback.CurrentObject,
1106 obj);
1107 }
1108
1109
1110 /**
1111 * Delete the given transform feedback objects.
1112 * Part of GL_ARB_transform_feedback2.
1113 */
1114 void GLAPIENTRY
1115 _mesa_DeleteTransformFeedbacks(GLsizei n, const GLuint *names)
1116 {
1117 GLint i;
1118 GET_CURRENT_CONTEXT(ctx);
1119
1120 if (n < 0) {
1121 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteTransformFeedbacks(n < 0)");
1122 return;
1123 }
1124
1125 if (!names)
1126 return;
1127
1128 for (i = 0; i < n; i++) {
1129 if (names[i] > 0) {
1130 struct gl_transform_feedback_object *obj
1131 = _mesa_lookup_transform_feedback_object(ctx, names[i]);
1132 if (obj) {
1133 if (obj->Active) {
1134 _mesa_error(ctx, GL_INVALID_OPERATION,
1135 "glDeleteTransformFeedbacks(object %u is active)",
1136 names[i]);
1137 return;
1138 }
1139 _mesa_HashRemove(ctx->TransformFeedback.Objects, names[i]);
1140 /* unref, but object may not be deleted until later */
1141 if (obj == ctx->TransformFeedback.CurrentObject) {
1142 reference_transform_feedback_object(
1143 &ctx->TransformFeedback.CurrentObject,
1144 ctx->TransformFeedback.DefaultObject);
1145 }
1146 reference_transform_feedback_object(&obj, NULL);
1147 }
1148 }
1149 }
1150 }
1151
1152
1153 /**
1154 * Pause transform feedback.
1155 * Part of GL_ARB_transform_feedback2.
1156 */
1157 void GLAPIENTRY
1158 _mesa_PauseTransformFeedback(void)
1159 {
1160 struct gl_transform_feedback_object *obj;
1161 GET_CURRENT_CONTEXT(ctx);
1162
1163 obj = ctx->TransformFeedback.CurrentObject;
1164
1165 if (!_mesa_is_xfb_active_and_unpaused(ctx)) {
1166 _mesa_error(ctx, GL_INVALID_OPERATION,
1167 "glPauseTransformFeedback(feedback not active or already paused)");
1168 return;
1169 }
1170
1171 FLUSH_VERTICES(ctx, 0);
1172 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
1173
1174 assert(ctx->Driver.PauseTransformFeedback);
1175 ctx->Driver.PauseTransformFeedback(ctx, obj);
1176
1177 obj->Paused = GL_TRUE;
1178 }
1179
1180
1181 /**
1182 * Resume transform feedback.
1183 * Part of GL_ARB_transform_feedback2.
1184 */
1185 void GLAPIENTRY
1186 _mesa_ResumeTransformFeedback(void)
1187 {
1188 struct gl_transform_feedback_object *obj;
1189 GET_CURRENT_CONTEXT(ctx);
1190
1191 obj = ctx->TransformFeedback.CurrentObject;
1192
1193 if (!obj->Active || !obj->Paused) {
1194 _mesa_error(ctx, GL_INVALID_OPERATION,
1195 "glResumeTransformFeedback(feedback not active or not paused)");
1196 return;
1197 }
1198
1199 /* From the ARB_transform_feedback2 specification:
1200 * "The error INVALID_OPERATION is generated by ResumeTransformFeedback if
1201 * the program object being used by the current transform feedback object
1202 * is not active."
1203 */
1204 if (obj->program != get_xfb_source(ctx)) {
1205 _mesa_error(ctx, GL_INVALID_OPERATION,
1206 "glResumeTransformFeedback(wrong program bound)");
1207 return;
1208 }
1209
1210 FLUSH_VERTICES(ctx, 0);
1211 ctx->NewDriverState |= ctx->DriverFlags.NewTransformFeedback;
1212
1213 obj->Paused = GL_FALSE;
1214
1215 assert(ctx->Driver.ResumeTransformFeedback);
1216 ctx->Driver.ResumeTransformFeedback(ctx, obj);
1217 }
1218
1219 extern void GLAPIENTRY
1220 _mesa_GetTransformFeedbackiv(GLuint xfb, GLenum pname, GLint *param)
1221 {
1222 struct gl_transform_feedback_object *obj;
1223 GET_CURRENT_CONTEXT(ctx);
1224
1225 obj = lookup_transform_feedback_object_err(ctx, xfb,
1226 "glGetTransformFeedbackiv");
1227 if(!obj) {
1228 return;
1229 }
1230
1231 switch(pname) {
1232 case GL_TRANSFORM_FEEDBACK_PAUSED:
1233 *param = obj->Paused;
1234 break;
1235 case GL_TRANSFORM_FEEDBACK_ACTIVE:
1236 *param = obj->Active;
1237 break;
1238 default:
1239 _mesa_error(ctx, GL_INVALID_ENUM,
1240 "glGetTransformFeedbackiv(pname=%i)", pname);
1241 }
1242 }
1243
1244 extern void GLAPIENTRY
1245 _mesa_GetTransformFeedbacki_v(GLuint xfb, GLenum pname, GLuint index,
1246 GLint *param)
1247 {
1248 struct gl_transform_feedback_object *obj;
1249 GET_CURRENT_CONTEXT(ctx);
1250
1251 obj = lookup_transform_feedback_object_err(ctx, xfb,
1252 "glGetTransformFeedbacki_v");
1253 if(!obj) {
1254 return;
1255 }
1256
1257 if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
1258 _mesa_error(ctx, GL_INVALID_VALUE,
1259 "glGetTransformFeedbacki_v(index=%i)", index);
1260 return;
1261 }
1262
1263 switch(pname) {
1264 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
1265 *param = obj->BufferNames[index];
1266 break;
1267 default:
1268 _mesa_error(ctx, GL_INVALID_ENUM,
1269 "glGetTransformFeedbacki_v(pname=%i)", pname);
1270 }
1271 }
1272
1273 extern void GLAPIENTRY
1274 _mesa_GetTransformFeedbacki64_v(GLuint xfb, GLenum pname, GLuint index,
1275 GLint64 *param)
1276 {
1277 struct gl_transform_feedback_object *obj;
1278 GET_CURRENT_CONTEXT(ctx);
1279
1280 obj = lookup_transform_feedback_object_err(ctx, xfb,
1281 "glGetTransformFeedbacki64_v");
1282 if(!obj) {
1283 return;
1284 }
1285
1286 if (index >= ctx->Const.MaxTransformFeedbackBuffers) {
1287 _mesa_error(ctx, GL_INVALID_VALUE,
1288 "glGetTransformFeedbacki64_v(index=%i)", index);
1289 return;
1290 }
1291
1292 compute_transform_feedback_buffer_sizes(obj);
1293 switch(pname) {
1294 case GL_TRANSFORM_FEEDBACK_BUFFER_START:
1295 *param = obj->Offset[index];
1296 break;
1297 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE:
1298 *param = obj->Size[index];
1299 break;
1300 default:
1301 _mesa_error(ctx, GL_INVALID_ENUM,
1302 "glGetTransformFeedbacki64_v(pname=%i)", pname);
1303 }
1304 }