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