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