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