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